forked from brentley/ecsdemo-crystal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3acd4e7
Showing
12 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
lib | ||
*.dwarf | ||
.shards | ||
.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
lib | ||
.shards | ||
*.dwarf | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
FROM crystallang/crystal:latest | ||
WORKDIR /src/ | ||
COPY . . | ||
RUN shards install | ||
RUN crystal build --release --link-flags="-static" src/server.cr | ||
|
||
FROM alpine:latest | ||
COPY --from=0 /src/server /server | ||
COPY --from=0 /src/code_hash.txt /code_hash.txt | ||
EXPOSE 3000 | ||
CMD ["/server"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Amazon ECS Workshop | ||
|
||
This is part of an Amazon ECS workshop at https://ecsworkshop.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
version: 0.2 | ||
|
||
phases: | ||
install: | ||
commands: | ||
- export IMAGE_TAG=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7) | ||
- export ACCOUNT=$(echo $CODEBUILD_BUILD_ARN |cut -f5 -d:) | ||
- export $(cat mu-env.sh) | ||
- echo '***** This is the current env:' | ||
- printenv | ||
build: | ||
commands: | ||
- curl -m3 -sL -w "%{http_code}\\n" "$BASE_URL" -o /dev/null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
version: 0.2 | ||
|
||
phases: | ||
install: | ||
commands: | ||
- export IMAGE_TAG=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7) | ||
- export ACCOUNT=$(echo $CODEBUILD_BUILD_ARN |cut -f5 -d:) | ||
- echo '***** This is the current env:' | ||
- printenv | ||
build: | ||
commands: | ||
post_build: | ||
commands: | ||
- printf '[{"name":"ecsdemo-crystal","imageUri":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json | ||
- echo $IMAGE_TAG > code_hash.txt | ||
artifacts: | ||
files: | ||
- '**/*' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
NOHASH |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- | ||
service: | ||
desiredCount: 3 | ||
maxSize: 6 | ||
port: 3000 | ||
priority: 2 | ||
pathPatterns: | ||
- /crystal | ||
networkMode: awsvpc | ||
parameters: | ||
'mu-service-ecsdemo-crystal-acceptance': | ||
ElbHttpListenerArn: | ||
mu-loadbalancer-acceptance-BackendLBHttpListenerArn | ||
'mu-service-ecsdemo-crystal-production': | ||
ElbHttpListenerArn: | ||
mu-loadbalancer-production-BackendLBHttpListenerArn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
version: 1.0 | ||
shards: | ||
interface_address: | ||
github: t-richards/interface_address | ||
version: 1.0.0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
name: shards | ||
version: 0.1.0 | ||
|
||
dependencies: | ||
interface_address: | ||
github: t-richards/interface_address | ||
version: ~> 1.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require "interface_address" | ||
|
||
class DefaultIf | ||
@@default_if : String = "NONE" | ||
@@default_ip : String = "NONE" | ||
|
||
def self.getif | ||
interface = IO::Memory.new | ||
command = "awk '$2 == 00000000 { print $1 }' /proc/net/route" | ||
getroute = Process.run(command, shell: true, output: interface) | ||
if !getroute.success? || !getroute.normal_exit? || getroute.signal_exit? | ||
command = "route -n get default | awk '/interface/ {print $2}'" | ||
getroute = Process.run(command, shell: true, output: interface) | ||
end | ||
interface = interface.to_s | ||
interface = interface.gsub "\n","" | ||
@@default_if = interface | ||
end | ||
|
||
def self.getip | ||
@@default_if = DefaultIf.getif | ||
InterfaceAddress.get_interface_addresses.each do |a| | ||
iface_name = a.interface_name | ||
iface_ip = a.ip_address.address | ||
if @@default_if == iface_name | ||
if iface_ip.includes? "." | ||
@@default_ip = iface_ip | ||
return @@default_ip | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
require "logger" | ||
require "http/server" | ||
require "./default_ip" | ||
|
||
log = Logger.new(STDOUT) | ||
log.level = Logger::DEBUG | ||
|
||
log.debug("Created logger") | ||
log.info("Program started") | ||
log.warn("Nothing to do!") | ||
|
||
code_hash = File.read("code_hash.txt") | ||
log.debug("code_hash is: #{code_hash}") | ||
|
||
default_ip = DefaultIf.getip | ||
log.debug("default_ip is: #{default_ip}") | ||
|
||
if default_ip | ||
net_octet = default_ip.split(".")[2] | ||
log.debug("net_octet is: #{net_octet}") | ||
end | ||
|
||
case net_octet | ||
when "100" | ||
az = "1a" | ||
when "101" | ||
az = "1b" | ||
when "102" | ||
az = "1c" | ||
else | ||
az = "unknown" | ||
end | ||
log.debug("az is: #{az}") | ||
|
||
server = HTTP::Server.new("0.0.0.0", 3000, | ||
[ | ||
HTTP::ErrorHandler.new, | ||
HTTP::LogHandler.new, | ||
HTTP::CompressHandler.new, | ||
]) do |context| | ||
if context.request.path == "/crystal" || context.request.path == "/crystal/" | ||
context.response.content_type = "text/plain" | ||
context.response.print "Crystal backend: Hello! from #{default_ip} in AZ-#{az} commit #{code_hash}" | ||
elsif context.request.path == "/health" | ||
context.response.content_type = "text/plain" | ||
context.response.print "Healthy!" | ||
else | ||
context.response.status_code = 404 | ||
context.response.headers["Content-Type"] = "text/plain" | ||
context.response.puts "Not Found" | ||
end | ||
end | ||
|
||
puts "Listening on http://0.0.0.0:3000" | ||
server.listen |