ecr-deploy 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 1.9.3-p551
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ecr-deploy.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Keitaroh Kobayashi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,110 @@
1
+ # ecr-deploy
2
+
3
+ Simple script to help with AWS ECR deploys.
4
+
5
+ ## Usage Overview
6
+
7
+ 0. Set up services and JSON task definition configuration in S3
8
+ 1. Install `ecr-deploy`: `gem install ecr-deploy`
9
+ 2. Set up configuration file (see `configuration.yml`)
10
+ 3. Run `ecr-deploy`:
11
+
12
+ ```
13
+ $ ecr-deploy path_to_configuration.yml $ENVIRONMENT_NAME $IMAGE_TAG
14
+ ```
15
+
16
+ ## About
17
+
18
+ `ecr-deploy` makes a few assumptions about your ECR setup:
19
+
20
+ You have multiple **environments** of tasks and services. The name of a service
21
+ is formatted `ENVIRONMENT-NAME`. So, for example, the "web" service in a
22
+ production environment would have the service name of `production-web`.
23
+
24
+ Shared environment variables and ECS task definition files live in a S3 bucket.
25
+ Shared environment variables are environment-specific, and are contained in a
26
+ JSON file of the name `ENVIRONMENT-env.json`. Service and task configuration
27
+ templates are contained in JSON files with the name `ENVIRONMENT-NAME.json`
28
+ (where `NAME` is the service / task name specified in configuration.yml).
29
+
30
+ A quick overview of the steps `ecr-deploy` will take when you run it with
31
+ `$ ecr-deploy config.yml production 0abcdef`, when `config.yml` has a single
32
+ "web" service in the "production" environemnt:
33
+
34
+ 1. Download the `production-env.json`
35
+ 2. Download `production-web.json`
36
+ 1. Replace `[ENVIRONMENT]` with the environment loaded from `production-env.json`
37
+ 2. Replace `[CURRENT_IMAGE_TAG]` with `0abcdef`
38
+ 3. Register the task definition using the contents of the previous step
39
+ 4. Update the `production-web` service to use the new task definition registered in the previous step
40
+ 5. Wait for the `production-web` to reach a stable state (will throw an error after 10 minutes)
41
+
42
+ ## Task Definition templates
43
+
44
+ `ecr-deploy` uses the AWS SDK for Ruby which uses a slightly different format
45
+ of the task definition JSON (under_scored instead of camelCased).
46
+
47
+ ```json
48
+ {
49
+ "container_definitions": [
50
+ {
51
+ "volumes_from": [],
52
+ "port_mappings": [
53
+ {
54
+ "host_port": 80,
55
+ "container_port": 8000
56
+ }
57
+ ],
58
+ "command": [
59
+ "run_server"
60
+ ],
61
+ "environment": [
62
+ [ENVIRONMENT]
63
+ ],
64
+ "essential": true,
65
+ "entry_point": [],
66
+ "links": [],
67
+ "mount_points": [],
68
+ "memory": 500,
69
+ "name": "production-web",
70
+ "cpu": 256,
71
+ "image": "XXX.dkr.ecr.us-east-1.amazonaws.com/REPO_NAME:[CURRENT_IMAGE_TAG]"
72
+ }
73
+ ],
74
+ "volumes": [],
75
+ "family": "production-web"
76
+ }
77
+ ```
78
+
79
+ Notice `[ENVIRONMENT]` and `[CURRENT_IMAGE_TAG]` -- these tokens will be replaced
80
+ on deploy.
81
+
82
+ ## `configuration.yml`
83
+
84
+ ```yaml
85
+ ---
86
+ base_config:
87
+ # The S3 bucket that contains the task definition templates and environment
88
+ # files.
89
+ template_bucket: km-t01-config
90
+ # The prefix for template object file names. Used for disambiguation if you
91
+ # deploy multiple apps from the same bucket.
92
+ template_bucket_prefix: ecr-deploy-
93
+
94
+ # The name of the ECS cluster
95
+ cluster: default
96
+
97
+ production:
98
+ # Services that will be updated
99
+ services:
100
+ - web
101
+
102
+ # Tasks that will be run as part of the deploy process (good for automatic
103
+ # migrations, etc)
104
+ run_tasks:
105
+ - migrator
106
+
107
+ # Tasks that will only be registered, not run. Good for on-demand workers.
108
+ register_tasks:
109
+ - worker
110
+ ```
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ecr_deploy"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/configuration.yml ADDED
@@ -0,0 +1,23 @@
1
+ ---
2
+ base_config:
3
+ # The S3 bucket that contains the task definition templates and environment
4
+ # files.
5
+ template_bucket: km-t01-config
6
+ # The prefix for template object file names. Used for disambiguation if you
7
+ # deploy multiple apps from the same bucket.
8
+ template_bucket_prefix: ecr-deploy-
9
+
10
+ # The name of the ECS cluster
11
+ cluster: default
12
+
13
+ production:
14
+ # Services that will be updated
15
+ services:
16
+ - web
17
+
18
+ # Tasks that will be run as part of the deploy process (good for automatic
19
+ # migrations, etc)
20
+ run_tasks: []
21
+
22
+ # Tasks that will only be registered, not run. Good for on-demand workers.
23
+ register_tasks: []
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ecr_deploy/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ecr-deploy"
8
+ spec.version = EcrDeploy::VERSION
9
+ spec.authors = ["Keitaroh Kobayashi"]
10
+ spec.email = ["keita@kbys.me"]
11
+
12
+ spec.summary = %q{A simple script to deploy services and tasks on AWS ECS}
13
+ spec.homepage = "https://github.com/KotobaMedia/ecr-deploy"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "aws-sdk", "~> 2.1"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.11"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ end
data/exe/ecr-deploy ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "ecr_deploy"
4
+ require "ecr_deploy/optparse"
5
+ require "ecr_deploy/cli"
6
+
7
+ options = EcrDeploy::Optparse.parse(ARGV)
8
+ EcrDeploy::CLI.run!(options)
data/lib/ecr_deploy.rb ADDED
@@ -0,0 +1,7 @@
1
+ module EcrDeploy
2
+ end
3
+
4
+ require "aws-sdk"
5
+
6
+ require_relative "./ecr_deploy/config"
7
+ require_relative "./ecr_deploy/task"
@@ -0,0 +1,11 @@
1
+ class EcrDeploy::CLI
2
+ def self.run!(opts)
3
+ config = EcrDeploy::Config.new(opts.config_file)
4
+ task = EcrDeploy::Task.new(config, opts.environment_name)
5
+
6
+ task.deploy!(opts.image_tag)
7
+ task.wait_until_stable
8
+
9
+ puts "Deploy finished."
10
+ end
11
+ end
@@ -0,0 +1,27 @@
1
+ require "yaml"
2
+
3
+ class EcrDeploy::Config
4
+ def initialize(path)
5
+ @config = YAML.load(IO.read(path))
6
+ end
7
+
8
+ def base_config
9
+ @config["base_config"]
10
+ end
11
+
12
+ def services(environment)
13
+ @config[environment]["services"] || []
14
+ end
15
+
16
+ def run_tasks(environment)
17
+ @config[environment]["run_tasks"] || []
18
+ end
19
+
20
+ def register_tasks(environment)
21
+ @config[environment]["register_tasks"] || []
22
+ end
23
+
24
+ def environments
25
+ @config.keys.reject { |e| e == "base_config" }
26
+ end
27
+ end
@@ -0,0 +1,20 @@
1
+ require "ostruct"
2
+
3
+ class EcrDeploy::Optparse
4
+ def self.parse(args)
5
+ options = OpenStruct.new
6
+
7
+ options.config_file = args[0] || show_usage
8
+ options.environment_name = args[1] || show_usage
9
+ options.image_tag = args[2] || show_usage
10
+
11
+ options
12
+ end
13
+
14
+ private
15
+
16
+ def self.show_usage
17
+ puts "Usage: ecr-deploy path_to_configuration.yml ENVIRONMENT_NAME IMAGE_TAG"
18
+ exit 127
19
+ end
20
+ end
@@ -0,0 +1,80 @@
1
+ require 'json'
2
+
3
+ class EcrDeploy::Task
4
+ def initialize(config, environment)
5
+ fail ArgumentError, "#{environment} does not exist in the configuration." \
6
+ if !config.environments.include?(environment)
7
+
8
+ @ecs = Aws::ECS::Client.new
9
+ s3 = Aws::S3::Resource.new
10
+
11
+ @cluster_name = config.base_config["cluster"]
12
+
13
+ @service_names = config.services(environment)
14
+ @run_task_names = config.run_tasks(environment)
15
+ @register_task_names = config.register_tasks(environment)
16
+
17
+ @bucket = s3.bucket(config.base_config["template_bucket"])
18
+ @bucket_prefix = config.base_config["template_bucket_prefix"] || ""
19
+
20
+ @environment = environment
21
+ @env_vars = @bucket.
22
+ object(build_path("#{environment}-env.json")).get.body.string
23
+ end
24
+
25
+ def deploy!(image_tag_name)
26
+ task_def_names = (@service_names + @run_task_names + @register_task_names).uniq
27
+ task_def_arns = task_def_names.map { |name| register_task_definition(name, image_tag_name) }
28
+ task_def = Hash[task_def_names.zip(task_def_arns)]
29
+
30
+ @service_names.each do |name|
31
+ service_name = "#{@environment}-#{name}"
32
+ $stderr.puts "==> Updating service \"#{service_name}\""
33
+ @ecs.update_service(
34
+ cluster: @cluster_name,
35
+ service: service_name,
36
+ task_definition: task_def[name])
37
+ end
38
+
39
+ @run_task_names.each do |name|
40
+ $stderr.puts "==> Running task \"#{name}\" in \"#{@environment}\""
41
+ @ecs.run_task(
42
+ cluster: @cluster_name,
43
+ task_definition: task_def[name],
44
+ count: 1)
45
+ end
46
+ end
47
+
48
+ def wait_until_stable(wait_time = 600)
49
+ services = @service_names.map { |name| "#{@environment}-#{name}" }
50
+ $stderr.puts "==> Waiting for #{services.join ", "} to stabilize..."
51
+ started_at = Time.now
52
+ @ecs.wait_until(:services_stable, cluster: @cluster, services: services) do |w|
53
+ w.max_attempts = nil
54
+
55
+ w.before_wait do |attempts, response|
56
+ throw :failure if Time.now - started_at > wait_time
57
+ end
58
+ end
59
+ rescue Aws::Waiters::Errors::WaiterFailed => e
60
+ $stderr.puts "!!> An error occurred while waiting for services to stabilize. #{e}"
61
+ end
62
+
63
+ def register_task_definition(name, image_tag_name)
64
+ template = @bucket.object(build_path("#{@environment}-#{name}.json")).
65
+ get.body.string
66
+ template.gsub!("[CURRENT_IMAGE_TAG]", image_tag_name)
67
+ template.gsub!("[ENVIRONMENT]", @env_vars)
68
+
69
+ template_obj = JSON.parse(template, symbolize_names: true)
70
+ task = @ecs.register_task_definition(template_obj)
71
+
72
+ task.task_definition.task_definition_arn
73
+ end
74
+
75
+ private
76
+
77
+ def build_path(path)
78
+ @bucket_prefix + path
79
+ end
80
+ end
@@ -0,0 +1,3 @@
1
+ module EcrDeploy
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ecr-deploy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Keitaroh Kobayashi
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2016-04-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: aws-sdk
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.1'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '2.1'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.11'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.11'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '10.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '3.0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '3.0'
78
+ description:
79
+ email:
80
+ - keita@kbys.me
81
+ executables:
82
+ - ecr-deploy
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - .rspec
88
+ - .ruby-version
89
+ - .travis.yml
90
+ - Gemfile
91
+ - LICENSE.txt
92
+ - README.md
93
+ - Rakefile
94
+ - bin/console
95
+ - bin/setup
96
+ - configuration.yml
97
+ - ecr-deploy.gemspec
98
+ - exe/ecr-deploy
99
+ - lib/ecr_deploy.rb
100
+ - lib/ecr_deploy/cli.rb
101
+ - lib/ecr_deploy/config.rb
102
+ - lib/ecr_deploy/optparse.rb
103
+ - lib/ecr_deploy/task.rb
104
+ - lib/ecr_deploy/version.rb
105
+ homepage: https://github.com/KotobaMedia/ecr-deploy
106
+ licenses:
107
+ - MIT
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ! '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 1.8.23.2
127
+ signing_key:
128
+ specification_version: 3
129
+ summary: A simple script to deploy services and tasks on AWS ECS
130
+ test_files: []