Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
brentley committed Feb 15, 2018
0 parents commit 3acd4e7
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
lib
*.dwarf
.shards
.git
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
lib
.shards
*.dwarf

11 changes: 11 additions & 0 deletions Dockerfile
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"]
3 changes: 3 additions & 0 deletions README.md
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
13 changes: 13 additions & 0 deletions buildspec-test.yml
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
18 changes: 18 additions & 0 deletions buildspec.yml
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:
- '**/*'
1 change: 1 addition & 0 deletions code_hash.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NOHASH
16 changes: 16 additions & 0 deletions mu.yml
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
6 changes: 6 additions & 0 deletions shard.lock
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

7 changes: 7 additions & 0 deletions shard.yml
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
33 changes: 33 additions & 0 deletions src/default_ip/default_ip.cr
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
55 changes: 55 additions & 0 deletions src/server.cr
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

0 comments on commit 3acd4e7

Please sign in to comment.