diff --git a/dockerize.rb b/dockerize.rb index 14ff6cf..343b758 100755 --- a/dockerize.rb +++ b/dockerize.rb @@ -13,7 +13,7 @@ def run program :version, '0.1.0' program :description, 'A script to add the necessary files to your application in order for it to use docker and deploy easily' - global_option('-t', '--test', 'Runs the dockerizing process into a test folder') { working_dir = "./test" } + global_option('-t', '--test', 'Runs the dockerizing process into a test folder') { working_dir("./test") } command :this do |c| c.syntax = 'dockerize this [options]' @@ -24,7 +24,6 @@ def run c.option '--circleci', 'Adds the files for a circle ci supported project' c.action do |args, options| # Do something or c.when_called Dockerize::Commands::Dockerize - @working_dir = "../" copy_files(options) set_project_info end @@ -39,7 +38,7 @@ def set_project_info puts "Please enter the information for your application:\n" app_name = ask("Project Name:") ecr_host = ask("ECR Host:") - version = ask("Version:") + version = ask("Version: (ie: 1-0-0)") replace_values(app_name, ecr_host, version) end @@ -70,8 +69,13 @@ def replace(before, after, files) end def copy_files(options) + type(choose("Web or Worker?", :web, :worker)) puts "Copying setup files...\n" FileUtils.cp_r './files/general/.', working_dir + + puts "Copying type specific deploy files...\n" + FileUtils.cp_r "./files/#{type}/.", working_dir + options.default.keys.each do |opt| copy_specific_files(option: opt) end unless options.default.empty? @@ -81,16 +85,22 @@ def copy_specific_files(option: opt) case option.to_s when "ruby" puts "Copying Ruby setup files" - FileUtils.cp_r './files/ruby/scripts/.', "#{working_dir}/scripts" + FileUtils.cp_r './files/ruby/general/scripts/.', "#{working_dir}/scripts" + + FileUtils.cp_r "./files/ruby/#{type}/scripts/.", "#{working_dir}/scripts" when "circleci" puts "Copying Circle CI setup files" FileUtils.cp_r './files/circle_ci/.', "#{working_dir}/." end end - def working_dir(dir = "./") + def working_dir(dir = "../") @working_dir ||= dir end + + def type(type = "web") + @type ||= type + end end Dockerize.new.run if $0 == __FILE__ diff --git a/files/general/scripts/docker/bin/deploy.sh b/files/general/scripts/docker/bin/deploy.sh index ba9b4af..16dc324 100755 --- a/files/general/scripts/docker/bin/deploy.sh +++ b/files/general/scripts/docker/bin/deploy.sh @@ -1,37 +1 @@ -#!/usr/bin/env bash - -#Set up the variables needed -VERSION=`cat ./VERSION` -APP_NAME=`cat ./APP_NAME` -APP_ENV=$1 #Environment (qa/prod) -TYPE=$2 #Type (web/worker) -APP_ENV_FULL=`if [ $APP_ENV = prod ]; then echo production; else echo $APP_ENV; fi` -AWS_KEY=`aws configure get aws_access_key_id` -AWS_SECRET=`aws configure get aws_secret_access_key` -AWS_REGION=`aws configure get region` - -#Copy the appropriate Dockerrun file to the root -cp ./scripts/docker/$APP_ENV-Dockerrun.aws.json ./Dockerrun.aws.json -git add Dockerrun.aws.json - -#Try to use the current eb environment -#If it exists deploy to existing environment -#If not then create a new one (blue/green deployment) -errors=`eb use $APP_ENV-$APP_NAME-$VERSION-$TYPE | grep "ERROR: Environment"` -if [$errors == ""]; then - eb deploy $APP_ENV-$APP_NAME-$VERSION-$TYPE --timeout 30 --staged -else - eb create $APP_ENV-$APP_NAME-$VERSION-$TYPE \ - --tags Name=$APP_ENV-$APP_NAME \ - --cfg $APP_NAME-$APP_ENV \ - -c $APP_ENV-$APP_NAME-$VERSION-$TYPE \ - --timeout 15 \ - --envvars AWS_ACCESS_KEY_ID=$AWS_KEY,\ - AWS_SECRET_ACCESS_KEY=$AWS_SECRET,\ - AWS_REGION=$AWS_REGION,\ - SECRET_KEY_BASE=$SECRET_KEY_BASE,\ - WEB_TYPE=$TYPE,\ - RAILS_ENV=$APP_ENV_FULL,\ - RAKE_ENV=$APP_ENV_FULL,\ - WEB_CONCURRENCY=3 -fi +##This will be replaced based on whether it's a web or worker diff --git a/files/ruby/scripts/docker/Dockerfile b/files/ruby/general/scripts/docker/Dockerfile similarity index 100% rename from files/ruby/scripts/docker/Dockerfile rename to files/ruby/general/scripts/docker/Dockerfile diff --git a/files/ruby/general/scripts/docker/bin/deploy.sh b/files/ruby/general/scripts/docker/bin/deploy.sh new file mode 100644 index 0000000..033af60 --- /dev/null +++ b/files/ruby/general/scripts/docker/bin/deploy.sh @@ -0,0 +1 @@ +#Will be replaced by the web/worker script for ruby diff --git a/files/ruby/scripts/start.sh b/files/ruby/general/scripts/start.sh similarity index 100% rename from files/ruby/scripts/start.sh rename to files/ruby/general/scripts/start.sh diff --git a/files/ruby/scripts/test.sh b/files/ruby/general/scripts/test.sh similarity index 100% rename from files/ruby/scripts/test.sh rename to files/ruby/general/scripts/test.sh diff --git a/files/ruby/scripts/docker/bin/deploy.sh b/files/ruby/scripts/docker/bin/deploy.sh deleted file mode 100644 index 5221cf1..0000000 --- a/files/ruby/scripts/docker/bin/deploy.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env bash - -#Set up the variables needed -VERSION=`cat ./VERSION` -APP_NAME=`cat ./APP_NAME` -APP_ENV=$1 #Environment (qa/prod) -TYPE=$2 #Type (web/worker) -APP_ENV_FULL=`if [ $APP_ENV = prod ]; then echo production; else echo $APP_ENV; fi` -AWS_KEY=`aws configure get aws_access_key_id` -AWS_SECRET=`aws configure get aws_secret_access_key` -AWS_REGION=`aws configure get region` -SECRET_KEY_BASE=`ruby ./scripts/setup/generate_secret_key.rb` - -#Copy the appropriate Dockerrun file to the root -cp ./scripts/docker/$APP_ENV-Dockerrun.aws.json ./Dockerrun.aws.json -git add Dockerrun.aws.json - -#Try to use the current eb environment -#If it exists deploy to existing environment -#If not then create a new one (blue/green deployment) -errors=`eb use $APP_ENV-$APP_NAME-$VERSION-$TYPE | grep "ERROR: Environment"` -if [$errors == ""]; then - eb deploy $APP_ENV-$APP_NAME-$VERSION-$TYPE --timeout 30 --staged -else - eb create $APP_ENV-$APP_NAME-$VERSION-$TYPE --cfg $APP_NAME-$APP_ENV -c $APP_ENV-$APP_NAME-$VERSION-$TYPE --timeout 15 --envvars AWS_ACCESS_KEY_ID=$AWS_KEY,AWS_SECRET_ACCESS_KEY=$AWS_SECRET,AWS_REGION=$AWS_REGION,SECRET_KEY_BASE=$SECRET_KEY_BASE,WEB_TYPE=$TYPE,RAILS_ENV=$APP_ENV_FULL,RAKE_ENV=$APP_ENV_FULL,WEB_CONCURRENCY=3 -fi diff --git a/files/ruby/scripts/setup/generate_secret_key.rb b/files/ruby/scripts/setup/generate_secret_key.rb deleted file mode 100755 index adbf30a..0000000 --- a/files/ruby/scripts/setup/generate_secret_key.rb +++ /dev/null @@ -1,3 +0,0 @@ -require "securerandom" - -puts SecureRandom.hex(60) diff --git a/files/ruby/web/scripts/docker/bin/deploy.sh b/files/ruby/web/scripts/docker/bin/deploy.sh new file mode 100644 index 0000000..4f176ec --- /dev/null +++ b/files/ruby/web/scripts/docker/bin/deploy.sh @@ -0,0 +1,65 @@ +#!/usr/bin/env bash + +raw_environment_name=$1 +raw_environment_type=$2 +AWS_ACCESS_KEY_ID=`aws configure get aws_access_key_id` +AWS_SECRET_ACCESS_KEY=`aws configure get aws_secret_access_key` +AWS_REGION=`aws configure get region` + +function qualified_environment_name { + echo $raw_environment_name-$(app_name)-$(app_version)-$raw_environment_type | sed 's/_/-/g' | sed 's/\./-/g' +} + +function qualified_rails_environment_name { + if [ $raw_environment_name == prod ]; then echo production; else echo $raw_environment_name; fi +} + +function secret_key_base { + echo $(base64 /dev/urandom | head -c 60) +} + +function app_version { + cat ./VERSION +} + +function app_name { + cat ./APP_NAME +} + +function is_existing_environment { + errors=$(eb use $(qualified_environment_name) | grep "ERROR: Environment") + if [$errors == ""]; then return 0; else return 1; fi +} + +function qualified_instance_type { + if [ $raw_environment_name == 'prod' ]; then echo 't2.medium'; else echo 't2.micro'; fi +} + +#Copy the appropriate Dockerrun file to the root +cp ./scripts/docker/$raw_environment_name-Dockerrun.aws.json ./Dockerrun.aws.json +git add Dockerrun.aws.json + +#Try to use the current eb environment +#If it exists deploy to existing environment +#If not then create a new one (blue/green deployment) +if is_existing_environment; then + echo "Deploy" + eb deploy $(qualified_environment_name) --timeout 30 --staged +else + echo "Create" + eb create $(qualified_environment_name) \ + -c $(qualified_environment_name) + -t web \ + -i $(qualified_instance_type) \ + --tags Name=$(app_name),Team='Your Team Here',Application='$(app_name)',Environment=$(qualified_rails_environment_name),Role='Web' \ + --envvars AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY,AWS_REGION=$AWS_REGION,SECRET_KEY_BASE=$(secret_key_base),RAILS_ENV=$(qualified_rails_environment_name),RACK_ENV=$(qualified_rails_environment_name) \ + --vpc.id 'vpc-12344' \ + --vpc.dbsubnets 'subnet-12345' \ + --vpc.ec2subnets 'subnet-12345 \ + --vpc.elbpublic \ + --vpc.publicip \ + --vpc.securitygroups 'sg-1235' \ + -k 'ec2-key-here' \ + -ip 'some_instance_profile' \ + --timeout 15 +fi diff --git a/files/ruby/worker/scripts/docker/bin/deploy.sh b/files/ruby/worker/scripts/docker/bin/deploy.sh new file mode 100644 index 0000000..e9f269a --- /dev/null +++ b/files/ruby/worker/scripts/docker/bin/deploy.sh @@ -0,0 +1,64 @@ +#!/usr/bin/env bash + +raw_environment_name=$1 +raw_environment_type=$2 +AWS_ACCESS_KEY_ID=`aws configure get aws_access_key_id` +AWS_SECRET_ACCESS_KEY=`aws configure get aws_secret_access_key` +AWS_REGION=`aws configure get region` + +function qualified_environment_name { + echo $raw_environment_name-$(app_name)-$(app_version)-$raw_environment_type | sed 's/_/-/g' | sed 's/\./-/g' +} + +function qualified_rails_environment_name { + if [ $raw_environment_name == prod ]; then echo production; else echo $raw_environment_name; fi +} + +function secret_key_base { + echo $(base64 /dev/urandom | head -c 60) +} + +function app_version { + cat ./VERSION +} + +function app_name { + cat ./APP_NAME +} + +function is_existing_environment { + errors=$(eb use $(qualified_environment_name) | grep "ERROR: Environment") + if [$errors == ""]; then return 0; else return 1; fi +} + +function qualified_instance_type { + if [ $raw_environment_name == 'prod' ]; then echo 't2.medium'; else echo 't2.micro'; fi +} + +#Copy the appropriate Dockerrun file to the root +cp ./scripts/docker/$raw_environment_name-Dockerrun.aws.json ./Dockerrun.aws.json +git add Dockerrun.aws.json + +#Try to use the current eb environment +#If it exists deploy to existing environment +#If not then create a new one (blue/green deployment) +if is_existing_environment; then + echo "Deploy" + eb deploy $(qualified_environment_name) --timeout 30 --staged +else + echo "Create" + eb create $(qualified_environment_name) \ + -t worker \ + -i $(qualified_instance_type) \ + --tags Name=$(app_name),Team='Your Team Here',Application='$(app_name)',Environment=$(qualified_rails_environment_name),Role='Web' \ + --envvars AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY,AWS_REGION=$AWS_REGION,SECRET_KEY_BASE=$(secret_key_base),RAILS_ENV=$(qualified_rails_environment_name),RACK_ENV=$(qualified_rails_environment_name) \ + --vpc.id 'vpc-12344' \ + --vpc.dbsubnets 'subnet-12345' \ + --vpc.ec2subnets 'subnet-12345 \ + --vpc.elbpublic \ + --vpc.publicip \ + --vpc.securitygroups 'sg-1235' \ + -k 'ec2-key-here' \ + -ip 'some_instance_profile' \ + --timeout 15 +fi diff --git a/files/web/scripts/docker/bin/deploy.sh b/files/web/scripts/docker/bin/deploy.sh new file mode 100644 index 0000000..9e030e6 --- /dev/null +++ b/files/web/scripts/docker/bin/deploy.sh @@ -0,0 +1,57 @@ +#!/usr/bin/env bash + +raw_environment_name=$1 +raw_environment_type=$2 +AWS_ACCESS_KEY_ID=`aws configure get aws_access_key_id` +AWS_SECRET_ACCESS_KEY=`aws configure get aws_secret_access_key` +AWS_REGION=`aws configure get region` + +function qualified_environment_name { + echo $raw_environment_name-$(app_name)-$(app_version)-$raw_environment_type | sed 's/_/-/g' | sed 's/\./-/g' +} + +function app_version { + cat ./VERSION +} + +function app_name { + cat ./APP_NAME +} + +function is_existing_environment { + errors=$(eb use $(qualified_environment_name) | grep "ERROR: Environment") + if [$errors == ""]; then return 0; else return 1; fi +} + +function qualified_instance_type { + if [ $raw_environment_name == 'prod' ]; then echo 't2.medium'; else echo 't2.micro'; fi +} + +#Copy the appropriate Dockerrun file to the root +cp ./scripts/docker/$raw_environment_name-Dockerrun.aws.json ./Dockerrun.aws.json +git add Dockerrun.aws.json + +#Try to use the current eb environment +#If it exists deploy to existing environment +#If not then create a new one (blue/green deployment) +if is_existing_environment; then + echo "Deploy" + eb deploy $(qualified_environment_name) --timeout 30 --staged +else + echo "Create" + eb create $(qualified_environment_name) \ + -c $(qualified_environment_name) + -t web \ + -i $(qualified_instance_type) \ + --tags Name=$(app_name),Team='Your Team Here',Application='$(app_name)',Environment=$(qualified_environment_name),Role='Web' \ + --envvars AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY,AWS_REGION=$AWS_REGION \ + --vpc.id 'vpc-12344' \ + --vpc.dbsubnets 'subnet-12345' \ + --vpc.ec2subnets 'subnet-12345 \ + --vpc.elbpublic \ + --vpc.publicip \ + --vpc.securitygroups 'sg-1235' \ + -k 'ec2-key-here' \ + -ip 'some_instance_profile' \ + --timeout 15 +fi diff --git a/files/worker/scripts/docker/bin/deploy.sh b/files/worker/scripts/docker/bin/deploy.sh new file mode 100644 index 0000000..d19155e --- /dev/null +++ b/files/worker/scripts/docker/bin/deploy.sh @@ -0,0 +1,56 @@ +#!/usr/bin/env bash + +raw_environment_name=$1 +raw_environment_type=$2 +AWS_ACCESS_KEY_ID=`aws configure get aws_access_key_id` +AWS_SECRET_ACCESS_KEY=`aws configure get aws_secret_access_key` +AWS_REGION=`aws configure get region` + +function qualified_environment_name { + echo $raw_environment_name-$(app_name)-$(app_version)-$raw_environment_type | sed 's/_/-/g' | sed 's/\./-/g' +} + +function app_version { + cat ./VERSION +} + +function app_name { + cat ./APP_NAME +} + +function is_existing_environment { + errors=$(eb use $(qualified_environment_name) | grep "ERROR: Environment") + if [$errors == ""]; then return 0; else return 1; fi +} + +function qualified_instance_type { + if [ $raw_environment_name == 'prod' ]; then echo 't2.medium'; else echo 't2.micro'; fi +} + +#Copy the appropriate Dockerrun file to the root +cp ./scripts/docker/$raw_environment_name-Dockerrun.aws.json ./Dockerrun.aws.json +git add Dockerrun.aws.json + +#Try to use the current eb environment +#If it exists deploy to existing environment +#If not then create a new one (blue/green deployment) +if is_existing_environment; then + echo "Deploy" + eb deploy $(qualified_environment_name) --timeout 30 --staged +else + echo "Create" + eb create $(qualified_environment_name) \ + -t worker \ + -i $(qualified_instance_type) \ + --tags Name=$(app_name),Team='Your Team Here',Application='$(app_name)',Environment=$(qualified_environment_name),Role='Web' \ + --envvars AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY,AWS_REGION=$AWS_REGION \ + --vpc.id 'vpc-12344' \ + --vpc.dbsubnets 'subnet-12345' \ + --vpc.ec2subnets 'subnet-12345 \ + --vpc.elbpublic \ + --vpc.publicip \ + --vpc.securitygroups 'sg-1235' \ + -k 'ec2-key-here' \ + -ip 'some_instance_profile' \ + --timeout 15 +fi