opsworks_ship 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: 66c032eab09bb2b04296a28d235b90531d9c47c4
4
+ data.tar.gz: 130a6889b02ac9cc624be1a62181bfc1a0e0bb60
5
+ SHA512:
6
+ metadata.gz: 74595c2ed4606193c189137d1b6d75abdafdc6484146610f0489d68408de279bddce73498f198e55edbfaa491ae5388d10196025da10aaf60c11e05f7f1a4e75
7
+ data.tar.gz: 7808aaac37125a07b8cfa021b17a559d9c9f9ae26a5a524608c0b0093f21bd5315d9414ac4d9a251985260b31fc4fbee4e40d56ca033624478a1a9f4bbe663a3
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
11
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in opsworks_ship.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Ritani
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,56 @@
1
+ # OpsworksShip
2
+
3
+ Provides a shipping script for AWS OpsWorks apps.
4
+
5
+ ## Installation
6
+
7
+ This requires the AWS CLI tools to be configured on the machine running the script. [Learn how](http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html)
8
+
9
+ Add this line to your application's Gemfile, probably in the `development` group:
10
+
11
+ ```ruby
12
+ gem 'opsworks_ship'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install opsworks_ship
22
+
23
+ Create a script (probably in `bin/deploy`) similar to the following:
24
+
25
+ #!/usr/bin/env ruby
26
+
27
+ gem 'opsworks_ship'
28
+ require 'opsworks_ship/deploy'
29
+
30
+ initialize(stack_name, revision, app_type, app_layer_name_regex, hipchat_auth_token = nil, hipchat_room_id = nil)
31
+
32
+ args = [ARGV[0], ARGV[1], 'rails', 'rails|sidekiq', 'abcde_this_is_a_token', 12345].compact
33
+ OpsworksShip::Deploy.new(*args).deploy
34
+
35
+ ## Usage examples
36
+
37
+ * `./bin/deploy help`
38
+ * `./bin/deploy staging heads/master rails rails|sidekiq`
39
+ * `./bin/deploy staging heads/master rails rails|sidekiq my_hipchat_token my_hipchat_room`
40
+ * `./bin/deploy production heads/master java "Java App Server" my_hipchat_token my_hipchat_room`
41
+
42
+ ## Development
43
+
44
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
45
+
46
+ 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).
47
+
48
+ ## Contributing
49
+
50
+ Bug reports and pull requests are welcome on GitHub at https://github.com/NEWECX/opsworks_ship.
51
+
52
+
53
+ ## License
54
+
55
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
56
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "opsworks_ship"
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
@@ -0,0 +1,192 @@
1
+ require 'json'
2
+
3
+ module OpsworksShip
4
+ class Deploy
5
+ def initialize(stack_name, revision, app_type, app_layer_name_regex, hipchat_auth_token = nil, hipchat_room_id = nil)
6
+ @stack_name = stack_name
7
+ raise "Invalid stack name, valid stacks are: #{all_stack_names}" unless all_stack_names.any?{|available_name| available_name == stack_name}
8
+
9
+ @revision = revision
10
+
11
+ @app_type = app_type
12
+ raise "Invalid app type #{@app_type}" unless opsworks_app_types.include?(@app_type)
13
+
14
+ @app_layer_name_regex = app_layer_name_regex
15
+
16
+ @hipchat_auth_token = hipchat_auth_token
17
+ @hipchat_room_id = hipchat_room_id
18
+ raise "Must supply both or neither hipchat params" if [@hipchat_auth_token, @hicphat_room_id].compact.size == 1
19
+ end
20
+
21
+ def syntax
22
+ puts "Arguments: #{method(:initialize).parameters.map{|p| "#{p.last} (#{p.first})"}.join(' ')}"
23
+ puts "\n"
24
+ puts "Valid stacks: #{all_stack_names}}"
25
+ end
26
+
27
+ def deploy
28
+ start_time = Time.now
29
+
30
+ puts "\n-------------------------------------"
31
+ puts "Deployment started"
32
+ puts "Revision: #{@revision}"
33
+ puts "Stack: #{@stack_name}"
34
+ puts "-------------------------------------\n\n"
35
+
36
+ set_revision_in_opsworks_app
37
+
38
+ deployment_id = start_deployment
39
+ final_status = monitor_deployment(deployment_id)
40
+
41
+ if final_status.downcase =~ /successful/
42
+ msg = "#{@app_type} deployment successful! Layers #{@app_layer_name_regex} now on #{@revision} deployed to #{@stack_name} by #{deployed_by}"
43
+ post_deployment_to_hipchat(msg)
44
+ else
45
+ raise "Deployment failed, status: #{final_status}"
46
+ end
47
+
48
+ run_time_seconds = Time.now.to_i - start_time.to_i
49
+ timestamped_puts "Deployment time #{run_time_seconds / 60}:%02i" % (run_time_seconds % 60)
50
+ end
51
+
52
+ def all_stack_names
53
+ @all_stack_names ||= stack_data.map{|s| s['Name']}.sort
54
+ end
55
+
56
+ def stack_data
57
+ @stack_data ||= begin
58
+ JSON.parse(`aws opsworks describe-stacks`)['Stacks']
59
+ end
60
+ end
61
+
62
+ def stack_id
63
+ stack = stack_data.select{|s| s['Name'].downcase == @stack_name.downcase}.first
64
+ if stack
65
+ stack['StackId']
66
+ else
67
+ raise "Stack not found. Available opsworks stacks: #{all_stack_names}"
68
+ end
69
+ end
70
+
71
+ def set_revision_in_opsworks_app
72
+ timestamped_puts "Setting revision #{@revision}"
73
+ cmd = "aws opsworks update-app --app-id #{app_id(stack_id)} --app-source Revision=#{@revision}"
74
+ timestamped_puts "#{cmd}"
75
+ `#{cmd}`
76
+ end
77
+
78
+ def app_id(stack_id)
79
+ JSON.parse(`aws opsworks describe-apps --stack-id=#{stack_id}`)['Apps'].select{|a| a['Type'] == @app_type}.first['AppId']
80
+ end
81
+
82
+ def timestamped_puts(str)
83
+ puts "#{Time.now} #{str}"
84
+ end
85
+
86
+ def deployed_by
87
+ git_user = `git config --global user.name`.chomp
88
+ git_email = `git config --global user.email`.chomp
89
+ "#{git_user} (#{git_email})"
90
+ end
91
+
92
+ def deploy_comment
93
+ "--comment \"rev. #{@revision}, deployed by #{deployed_by}\" "
94
+ end
95
+
96
+ def deploy_command
97
+ { :Name => "deploy" }.to_json.gsub('"', "\\\"")
98
+ end
99
+
100
+ def monitor_deployment(deployment_id)
101
+ deployment_finished = false
102
+ status = ""
103
+ while !deployment_finished
104
+ response = describe_deployments(deployment_id)
105
+ response["Deployments"].each do |deployment|
106
+ next if deployment["DeploymentId"] != deployment_id
107
+ status = deployment["Status"]
108
+ timestamped_puts "Status: #{status}"
109
+ deployment_finished = true if deployment["Status"].downcase != "running"
110
+ end
111
+ sleep(15) unless deployment_finished
112
+ end
113
+ timestamped_puts "Deployment #{status}"
114
+ status
115
+ end
116
+
117
+ def describe_deployments(deployment_id)
118
+ cmd = "aws opsworks describe-deployments --deployment-ids #{deployment_id}"
119
+ JSON.parse(`#{cmd}`)
120
+ end
121
+
122
+ def start_deployment
123
+ app_id = app_id(stack_id)
124
+
125
+ cmd = "aws opsworks create-deployment --app-id #{app_id} " +
126
+ "--stack-id #{stack_id} " +
127
+ "--command \"#{deploy_command}\" " +
128
+ "--instance-ids #{relevant_instance_ids(stack_id).join(' ')} " +
129
+ deploy_comment
130
+
131
+ timestamped_puts "Starting deployment..."
132
+ timestamped_puts cmd
133
+
134
+ response = JSON.parse(`#{cmd}`)
135
+ response["DeploymentId"]
136
+ end
137
+
138
+ def post_deployment_to_hipchat(msg)
139
+ room_id = (@hipchat_room_id || ENV['HIPCHAT_ROOM_ID']).to_i
140
+ auth_token = @hipchat_auth_token || ENV['HIPCHAT_AUTH_TOKEN']
141
+
142
+ return unless room_id > 0 && auth_token.present?
143
+
144
+ post_data = {
145
+ :name => "Deployments",
146
+ :privacy => 'private',
147
+ :is_archived => false,
148
+ :is_guest_accessible => true,
149
+ :topic => 'curl',
150
+ :message => msg,
151
+ :color => 'green',
152
+ :owner => { :id => 5 }
153
+ }
154
+
155
+ url = "https://api.hipchat.com/v2/room/#{room_id}/notification"
156
+ cmd = "curl --header \"content-type: application/json\" --header \"Authorization: Bearer #{auth_token}\" -X POST -d \"#{post_data.to_json.gsub('"', '\\"')}\" #{url}"
157
+ puts cmd
158
+ `#{cmd}`
159
+ end
160
+
161
+ def layer_instance_ids(layer_ids)
162
+ layer_ids.map do |layer_id|
163
+ JSON.parse(`aws opsworks describe-instances --layer-id=#{layer_id}`)['Instances'].map{|i| i['InstanceId']}
164
+ end.flatten
165
+ end
166
+
167
+ def relevant_instance_ids(stack_id)
168
+ layer_instance_ids(relevant_app_layer_ids(stack_id))
169
+ end
170
+
171
+ def relevant_app_layer_ids(stack_id)
172
+ stack_layers(stack_id).select{|l| l['Name'] =~ /#{@app_layer_name_regex}/i}.first['LayerId']
173
+ end
174
+
175
+ def stack_layers(stack_id)
176
+ JSON.parse(`aws opsworks describe-layers --stack-id=#{stack_id}`)['Layers']
177
+ end
178
+
179
+ def opsworks_app_types
180
+ [
181
+ 'aws-flow-ruby',
182
+ 'java',
183
+ 'rails',
184
+ 'php',
185
+ 'nodejs',
186
+ 'static',
187
+ 'other',
188
+ ]
189
+ end
190
+
191
+ end
192
+ end
@@ -0,0 +1,3 @@
1
+ module OpsworksShip
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "opsworks_ship/version"
2
+
3
+ module OpsworksShip
4
+ autoload :Deploy, 'opsworks_ship/deploy'
5
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'opsworks_ship/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "opsworks_ship"
8
+ spec.version = OpsworksShip::VERSION
9
+ spec.authors = ["Seth Ringling"]
10
+ spec.email = ["sethr@ritani.com"]
11
+
12
+ spec.summary = %q{Provides a shipping script for AWS ECS dockerized applications}
13
+ spec.description = %q{EZ-Deployment of a dockerized app in AWS!}
14
+ spec.homepage = "https://github.com/NEWECX/opsworks_ship"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "minitest"
25
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: opsworks_ship
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Seth Ringling
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-08-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: EZ-Deployment of a dockerized app in AWS!
56
+ email:
57
+ - sethr@ritani.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - bin/console
68
+ - bin/setup
69
+ - lib/opsworks_ship.rb
70
+ - lib/opsworks_ship/deploy.rb
71
+ - lib/opsworks_ship/version.rb
72
+ - opsworks_ship.gemspec
73
+ homepage: https://github.com/NEWECX/opsworks_ship
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.6.12
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Provides a shipping script for AWS ECS dockerized applications
97
+ test_files: []