shiprails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8093eda442437b91f2aba6f63e12f4f7e98a5ead
4
+ data.tar.gz: 5ae7b8820202411d15fdf7be7c57294829e9d467
5
+ SHA512:
6
+ metadata.gz: 8daa5f07fc78ba4bc67ac15253620c09e4abc904e73a37525043e16444db397dcca9230bcc56e598fe293e2842fe37b216ca5bd597ca49baf07ffc56de960234
7
+ data.tar.gz: 3cc82233c2be7a576cfecbb517f35fa7380c8056c863a46f34180f47de82d4d77dd1998c866051958f5a549a60ad8d3b4d46bcf372e763577788c94f8139e1d4
data/.DS_Store ADDED
Binary file
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .rbenv-vars
11
+
12
+ # Ignore work products
13
+ .shiprails.yml
14
+ .env.example
15
+ docker-compose.yml
16
+ Dockerfile
17
+
18
+ # Ignore Docker ENV
19
+ /.env
@@ -0,0 +1,3 @@
1
+ AWS_ACCESS_KEY_ID=FILL_ME_IN
2
+ AWS_SECRET_ACCESS_KEY=FILL_ME_IN
3
+ AWS_REGION=FILL_ME_IN
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ rails2017_shiprails
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.3.1
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.1
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in s3_config.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2016 Zane Shannon
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # shiprails
2
+
3
+ Shiprails is a command-line interface for deploying Rails apps to AWS ECS.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'shiprails'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install shiprails
20
+
21
+ ## Setup
22
+
23
+ - $`shiprails install`
24
+
25
+ ## Usage
26
+
27
+ *TODO*
28
+
29
+ ## Development
30
+
31
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
32
+
33
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
34
+
35
+ ## Contributing
36
+
37
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rails2017/shiprails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
38
+
39
+
40
+ ## License
41
+
42
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
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 "shiprails"
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,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/exe/.DS_Store ADDED
Binary file
data/exe/port ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "shiprails"
4
+ require "shiprails/port"
5
+
6
+ Shiprails::Port.start
data/exe/ship ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "shiprails"
4
+ require "shiprails/ship"
5
+
6
+ Shiprails::Ship.start
@@ -0,0 +1,13 @@
1
+ require "aws-sdk"
2
+ require "erb"
3
+ require "yaml"
4
+
5
+ module Shiprails
6
+ class Application
7
+
8
+ def initialize(options = {})
9
+ @options = options.inject({}) { |m, (k, v)| m[k.to_sym] = v; m }
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,53 @@
1
+ require "active_support/all"
2
+ require "thor"
3
+
4
+ module Shiprails
5
+ class Port < Thor
6
+ include Thor::Actions
7
+
8
+ desc "config", "Configure services"
9
+
10
+ method_option "path",
11
+ aliases: ["-p"],
12
+ default: ".shiprails.yml",
13
+ desc: "Specify a configuration file path"
14
+
15
+ def config(*command)
16
+ say "TODO: store development config vars"
17
+ end
18
+
19
+ desc "up", "Hoist app and services"
20
+ def up
21
+ run "docker-compose up"
22
+ end
23
+
24
+ desc "bundle", "Run bundler commands"
25
+ def bundle(*command)
26
+ command_string = command.join(' ')
27
+ if command_string.start_with?("exec")
28
+ run "docker-compose run --rm app bundle #{command_string}"
29
+ else
30
+ run "docker-compose run app bundle #{command_string}"
31
+ end
32
+ end
33
+
34
+ desc "rails", "Run rails commands"
35
+ def rails(*command)
36
+ command_string = command.join(' ')
37
+ run "docker-compose run --rm app bundle exec rails #{command_string}"
38
+ end
39
+
40
+ desc "bash", "Run bash commands"
41
+ def bash(*command)
42
+ command_string = command.join(' ')
43
+ run "docker-compose run --rm app bash #{command_string}"
44
+ end
45
+
46
+ private
47
+
48
+ def configuration
49
+ YAML.load(File.read(".shiprails.yml")).deep_symbolize_keys
50
+ end
51
+
52
+ end
53
+ end
@@ -0,0 +1,121 @@
1
+ require "active_support/all"
2
+ require "aws-sdk"
3
+ require "git"
4
+ require "thor/group"
5
+
6
+ module Shiprails
7
+ class Ship < Thor
8
+ class Config < Thor::Group
9
+ include Thor::Actions
10
+
11
+ class_option "path",
12
+ aliases: ["-p"],
13
+ default: ".",
14
+ desc: "Specify a configuration path"
15
+
16
+ def update_s3_config
17
+ command_string = args.join ' '
18
+ result = `S3_CONFIG_BUCKET=#{configuration[:config_s3_bucket]} bundle exec config #{command_string}`
19
+ puts result
20
+ @version = result.match(/New version: v([0-9]+)/)[1] rescue false
21
+ end
22
+
23
+ def update_ecs_tasks
24
+ return unless @version
25
+ say "Updating config version for ECS tasks..."
26
+ configuration[:services].each do |service_name, service|
27
+ service[:regions].each do |region_name, region|
28
+ ecs = Aws::ECS::Client.new(region: region_name.to_s)
29
+ region[:environments].each do |environment_name|
30
+ cluster_name = "#{project_name}_#{environment_name}"
31
+ task_name = "#{project_name}_#{service_name}_#{environment_name}"
32
+ begin
33
+ task_definition_description = ecs.describe_task_definition({task_definition: task_name})
34
+ task_definition = task_definition_description.task_definition.to_hash
35
+ task_definition.delete :task_definition_arn
36
+ task_definition.delete :revision
37
+ task_definition.delete :status
38
+ task_definition.delete :requires_attributes
39
+ rescue Aws::ECS::Errors::ClientException => e
40
+ say "Missing ECS task for #{task_name}!", :red
41
+ say "Run `ship setup`", :red
42
+ exit
43
+ end
44
+ task_definition[:container_definitions][0][:environment].map! do |e|
45
+ if e[:name] == "S3_CONFIG_REVISION"
46
+ e[:value] = "#{@version}"
47
+ end
48
+ e
49
+ end
50
+ task_definition_response = ecs.register_task_definition(task_definition)
51
+ say "Updated #{task_name} task."
52
+ end
53
+ end
54
+ end
55
+ say "ECS tasks updated.", :green
56
+ end
57
+
58
+ def update_ecs_services
59
+ return unless @version
60
+ say "Updating ECS services..."
61
+ configuration[:services].each do |service_name, service|
62
+ service[:regions].each do |region_name, region|
63
+ ecs = Aws::ECS::Client.new(region: region_name.to_s)
64
+ region[:environments].each do |environment_name|
65
+ cluster_name = "#{project_name}_#{environment_name}"
66
+ task_name = "#{project_name}_#{service_name}_#{environment_name}"
67
+ begin
68
+ task_definition_response = ecs.describe_task_definition({task_definition: task_name})
69
+ task_definition = task_definition_response.task_definition.to_hash
70
+ rescue Aws::ECS::Errors::ClientException => e
71
+ say "Missing ECS task for #{task_name}!", :red
72
+ say "Run `ship setup`", :red
73
+ exit
74
+ end
75
+ begin
76
+ service_response = ecs.update_service({
77
+ cluster: cluster_name,
78
+ service: service_name,
79
+ task_definition: task_definition_response.task_definition.task_definition_arn
80
+ })
81
+ say "Updated #{service_name} service."
82
+ rescue Aws::ECS::Errors::ServiceNotFoundException, Aws::ECS::Errors::ServiceNotActiveException => e
83
+ say "Missing ECS service for #{task_name}!", :red
84
+ say "Run `ship setup`", :red
85
+ exit
86
+ end
87
+ end
88
+ end
89
+ end
90
+ say "ECS services updated.", :green
91
+ end
92
+
93
+ def done
94
+ unless @version
95
+ say "No config updates.", :green
96
+ else
97
+ say "Config update complete!", :green
98
+ end
99
+ end
100
+
101
+ private
102
+
103
+ def aws_access_key_id
104
+ @aws_access_key_id ||= ask "AWS Access Key ID", default: ENV.fetch("AWS_ACCESS_KEY_ID")
105
+ end
106
+
107
+ def aws_access_key_secret
108
+ @aws_access_key_secret ||= ask "AWS Access Key Secret", default: ENV.fetch("AWS_SECRET_ACCESS_KEY")
109
+ end
110
+
111
+ def configuration
112
+ YAML.load(File.read("#{options[:path]}/.shiprails.yml")).deep_symbolize_keys
113
+ end
114
+
115
+ def project_name
116
+ configuration[:project_name]
117
+ end
118
+
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,191 @@
1
+ require "active_support/all"
2
+ require "aws-sdk"
3
+ require "git"
4
+ require "thor/group"
5
+
6
+ module Shiprails
7
+ class Ship < Thor
8
+ class Deploy < Thor::Group
9
+ include Thor::Actions
10
+
11
+ class_option "path",
12
+ aliases: ["-p"],
13
+ default: ".",
14
+ desc: "Specify a configuration path"
15
+
16
+ def check_git_status
17
+ if git.status.added.any? or git.status.changed.any? or git.status.deleted.any?
18
+ say "You have uncommitted changes. Commit and try again.", :red
19
+ exit
20
+ end
21
+ end
22
+
23
+ def build_docker_images
24
+ say "Building images..."
25
+ s3_config_bucket = configuration[:config_s3_bucket].to_s
26
+ commands = []
27
+ configuration[:services].each do |service_name, service|
28
+ image_name = "#{compose_project_name}_#{service[:image]}"
29
+ service[:regions].each do |region_name, region|
30
+ aws_region = region_name.to_s
31
+ region[:environments].each do |environment_name|
32
+ result = `S3_CONFIG_BUCKET=#{s3_config_bucket} bundle exec config list #{environment_name}`
33
+ s3_config_revision = result.match(/#{environment_name} \(v([0-9]+)\)/)[1] rescue 0
34
+ commands << "docker build -t #{image_name} --build-arg AWS_ACCESS_KEY_ID='#{aws_access_key_id}' --build-arg AWS_SECRET_ACCESS_KEY='#{aws_access_key_secret}' --build-arg AWS_REGION='#{aws_region}' --build-arg S3_CONFIG_BUCKET='#{s3_config_bucket}' --build-arg S3_CONFIG_REVISION='#{s3_config_revision}' -f Dockerfile.production ."
35
+ end
36
+ end
37
+ end
38
+ commands.uniq!
39
+ commands.each { |c| run c } # TODO: check that this succeeded
40
+ say "Build complete", :green
41
+ end
42
+
43
+ def tag_docker_images
44
+ say "Tagging images..."
45
+ commands = []
46
+ configuration[:services].each do |service_name, service|
47
+ image_name = "#{compose_project_name}_#{service[:image]}"
48
+ service[:regions].each do |region_name, region|
49
+ repository_url = region[:repository_url]
50
+ commands << "docker tag #{image_name} #{repository_url}:#{git_sha}"
51
+ end
52
+ end
53
+ commands.uniq!
54
+ commands.each { |c| run c } # TODO: check that this succeeded
55
+ say "Tagging complete.", :green
56
+ end
57
+
58
+ def push_docker_images
59
+ say "Pushing images..."
60
+ repository_urls_to_regions = {}
61
+ configuration[:services].each do |service_name, service|
62
+ service[:regions].each do |region, values|
63
+ repository_urls_to_regions[values[:repository_url]] = region
64
+ end
65
+ end
66
+ repository_urls_to_regions.each do |repository_url, region|
67
+ run "`aws ecr get-login --region #{region}`"
68
+ run "docker push #{repository_url}:#{git_sha}" # TODO: check that this succeeded
69
+ end
70
+ say "Push complete.", :green
71
+ end
72
+
73
+ def update_ecs_tasks
74
+ say "Updating ECS tasks..."
75
+ configuration[:services].each do |service_name, service|
76
+ image_name = "#{project_name}_#{service_name}"
77
+ service[:regions].each do |region_name, region|
78
+ ecs = Aws::ECS::Client.new(region: region_name.to_s)
79
+ region[:environments].each do |environment_name|
80
+ cluster_name = "#{project_name}_#{environment_name}"
81
+ task_name = "#{project_name}_#{service_name}_#{environment_name}"
82
+ image_name = "#{region[:repository_url]}:#{git_sha}"
83
+ begin
84
+ task_definition_description = ecs.describe_task_definition({task_definition: task_name})
85
+ task_definition = task_definition_description.task_definition.to_hash
86
+ task_definition.delete :task_definition_arn
87
+ task_definition.delete :revision
88
+ task_definition.delete :status
89
+ task_definition.delete :requires_attributes
90
+ rescue Aws::ECS::Errors::ClientException => e
91
+ say "Missing ECS task for #{task_name}!", :red
92
+ say "Run `ship setup`", :red
93
+ exit
94
+ end
95
+ if container = task_definition[:container_definitions].find{ |container| container[:name] == service_name.to_s }
96
+ container[:cpu] = service[:resources][:cpu_units]
97
+ container[:image] = image_name
98
+ container[:memory] = service[:resources][:memory_units]
99
+ config_s3_version = container[:environment].find{|e| e[:name] == "S3_CONFIG_REVISION" }[:value]
100
+ container[:environment] = [
101
+ { name: "AWS_REGION", value: region_name.to_s },
102
+ { name: "GIT_SHA", value: git_sha },
103
+ { name: "RACK_ENV", value: environment_name },
104
+ { name: "S3_CONFIG_BUCKET", value: config_s3_bucket },
105
+ { name: "S3_CONFIG_REVISION", value: config_s3_version }
106
+ ]
107
+ say "Updated #{service_name} container (#{image_name})."
108
+ end
109
+ task_definition_response = ecs.register_task_definition(task_definition)
110
+ say "Updated #{task_name}.", :green
111
+ end
112
+ end
113
+ end
114
+ say "ECS tasks updated.", :green
115
+ end
116
+
117
+ def update_ecs_services
118
+ say "Updating ECS services..."
119
+ configuration[:services].each do |service_name, service|
120
+ service[:regions].each do |region_name, region|
121
+ ecs = Aws::ECS::Client.new(region: region_name.to_s)
122
+ region[:environments].each do |environment_name|
123
+ cluster_name = "#{project_name}_#{environment_name}"
124
+ task_name = "#{project_name}_#{service_name}_#{environment_name}"
125
+ begin
126
+ task_definition_response = ecs.describe_task_definition({task_definition: task_name})
127
+ task_definition = task_definition_response.task_definition.to_hash
128
+ rescue Aws::ECS::Errors::ClientException => e
129
+ say "Missing ECS task for #{task_name}!", :red
130
+ say "Run `ship setup`", :red
131
+ exit
132
+ end
133
+ begin
134
+ service_response = ecs.update_service({
135
+ cluster: cluster_name,
136
+ service: service_name,
137
+ task_definition: task_definition_response.task_definition.task_definition_arn
138
+ })
139
+ say "Updated #{service_name}.", :green
140
+ rescue Aws::ECS::Errors::ServiceNotFoundException, Aws::ECS::Errors::ServiceNotActiveException => e
141
+ say "Missing ECS service for #{task_name}!", :red
142
+ say "Run `ship setup`", :red
143
+ exit
144
+ end
145
+ end
146
+ end
147
+ end
148
+ say "ECS services updated.", :green
149
+ end
150
+
151
+ def done
152
+ say "Deploy complete!", :green
153
+ end
154
+
155
+ private
156
+
157
+ def aws_access_key_id
158
+ @aws_access_key_id ||= ENV.fetch("AWS_ACCESS_KEY_ID") { ask "AWS Access Key ID" }
159
+ end
160
+
161
+ def aws_access_key_secret
162
+ @aws_access_key_secret ||= ENV.fetch("AWS_SECRET_ACCESS_KEY") { ask "AWS Access Key Secret" }
163
+ end
164
+
165
+ def configuration
166
+ YAML.load(File.read("#{options[:path]}/.shiprails.yml")).deep_symbolize_keys
167
+ end
168
+
169
+ def git
170
+ @_git ||= Git.open(Dir.getwd)
171
+ end
172
+
173
+ def git_sha
174
+ @_git_sha ||= git.object('HEAD').sha
175
+ end
176
+
177
+ def project_name
178
+ configuration[:project_name]
179
+ end
180
+
181
+ def compose_project_name
182
+ project_name.gsub /[^0-9a-zA-Z]/i, ''
183
+ end
184
+
185
+ def config_s3_bucket
186
+ configuration[:config_s3_bucket]
187
+ end
188
+
189
+ end
190
+ end
191
+ end