eb-docker-deploy 0.5.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9c85a599060b7de1e5b98586206cf0fbb9daf3a9
4
- data.tar.gz: 922128fc059e9d63ee6e57afefaf72ececf1693e
3
+ metadata.gz: a649466f0fba47202a94d74a12a1e596445b8e7c
4
+ data.tar.gz: 29623779df7dc7e7566c998e856d00fec769646e
5
5
  SHA512:
6
- metadata.gz: f11e93101de17d7201f6752546e7c6175eda380fa5a757df90d6a28104bd5c728f33adbb1a61277d5c594e22edd3bb674fe3e4d4f43527a15d0752fb1aba8812
7
- data.tar.gz: f85183284ae14d547993b60c0fd6e5ae15829c8dea149b691e4570e41b24648430988882cd8c5580792d8a72d4d0ab66efdab53582517238927d651e6cca58c0
6
+ metadata.gz: 7ede7b3dd3c2e814653a1b9f5021674cb52aae02728aedfe993352e90310be400d90ef10fa4c8e4ee1da5b7e472ffde799c5a8d41337e678dd641c6131c6c1c9
7
+ data.tar.gz: 0eea490ea341e7858bf897a17033fa63c9e96f74dd63e2e177dcd0d963e3d5db279c6d7a2c653f6564179386913e5224411afb505e23881c4ce6f28994053447
data/bin/ebd CHANGED
@@ -2,4 +2,4 @@
2
2
 
3
3
  require 'deploy'
4
4
 
5
- Deploy::Deployer.start(ARGV)
5
+ Deploy::Runner.start(ARGV)
@@ -14,7 +14,7 @@ Gem::Specification.new do |spec|
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
17
- spec.executables = ['eb-deploy', 'ebd']
17
+ spec.executables = ['ebd']
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ['lib']
20
20
 
@@ -24,7 +24,7 @@ Gem::Specification.new do |spec|
24
24
  spec.add_dependency 'thor'
25
25
  spec.add_dependency 'highline'
26
26
 
27
- spec.add_dependency 'rest-client'
27
+ spec.add_dependency 'aws-sdk', '~> 2'
28
28
 
29
29
  spec.add_dependency 'slack-notifier'
30
30
  end
@@ -1,12 +1,15 @@
1
1
  require 'thor'
2
2
  require 'highline/import'
3
3
  require 'slack-notifier'
4
- require 'rest-client'
4
+ require 'aws-sdk'
5
5
 
6
6
  require 'deploy/output'
7
- require 'deploy/docker_hub_api'
7
+ require 'deploy/commands'
8
+ require 'deploy/checks'
9
+ require 'deploy/versions'
10
+ require 'deploy/utility'
8
11
 
9
- require 'deploy/deployer'
12
+ require 'deploy/runner'
10
13
 
11
14
  module Deploy
12
15
 
@@ -0,0 +1,26 @@
1
+ require 'deploy/utility'
2
+
3
+ module Deploy
4
+ module Checks
5
+ include Deploy::Utility
6
+
7
+ def check_setup
8
+ (shout('docker not installed'); exit(1)) unless command?('docker')
9
+ (shout('eb command not installed'); exit(1)) unless command?('eb')
10
+ (shout('elasticbeanstalk not configured for this project. run "eb init".'); exit(1)) unless File.exist?('.elasticbeanstalk')
11
+ (shout('AWS credentials not configured.'); exit(1)) unless ENV['AWS_ACCESS_KEY_ID'] && ENV['AWS_SECRET_ACCESS_KEY'] && ENV['AWS_REGION']
12
+ (shout('ENV DOCKER_REPO not set'); exit(1)) unless ENV['DOCKER_REPO']
13
+ end
14
+
15
+ def check_rollback_version(version, environment)
16
+ check_version(version, environment)
17
+ (shout('You can only rollback to a previous version'); exit(1)) unless application_versions_array.include?(version)
18
+ end
19
+
20
+ def check_version(version, environment)
21
+ (shout('You must pass a version with -v'); exit(1)) unless version
22
+ (shout('You are currently on that version'); exit(1)) if current_version_for_environment(environment) == version
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,44 @@
1
+ require 'deploy/utility'
2
+
3
+ module Deploy
4
+ module Commands
5
+ include Deploy::Utility
6
+
7
+ def build_image(repo, tag)
8
+ shout "Building Docker Image: #{repo}:#{tag}"
9
+ command = "docker build -t #{repo}:#{tag} ."
10
+ exit(1) unless system(command)
11
+ end
12
+
13
+ def tag_image_as_latest(repo, tag)
14
+ shout "Tagging #{tag} Docker Image as Latest"
15
+ command = "docker tag -f #{repo}:#{tag} #{repo}:latest"
16
+ exit(1) unless system(command)
17
+ end
18
+
19
+ def push_image(repo, tag)
20
+ shout "Pushing Docker Image: #{repo}:#{tag}"
21
+ command = "docker push #{repo}:#{tag}"
22
+ exit(1) unless system(command)
23
+ end
24
+
25
+ def pull_image(repo, tag)
26
+ shout "Pulling Docker Image: #{repo}:#{tag}"
27
+ command = "docker pull #{repo}:#{tag}"
28
+ exit(1) unless system(command)
29
+ end
30
+
31
+ def run_deploy(version, environment)
32
+ command = "eb deploy #{environment} --label #{version}"
33
+ shout "deploying #{version} to elastic beanstalk with command: #{command}"
34
+ exit(1) unless system(command)
35
+ end
36
+
37
+ def run_rollback(version, environment)
38
+ command = "eb deploy #{environment} --version #{version}"
39
+ shout "deploying #{version} to elastic beanstalk with command: #{command}"
40
+ exit(1) unless system(command)
41
+ end
42
+
43
+ end
44
+ end
@@ -1,14 +1,30 @@
1
1
  module Deploy
2
2
  module Output
3
3
 
4
+ def announce(attachments)
5
+ shout("#{attachments[:title]} - #{attachments[:text]}")
6
+ notifier('', attachments)
7
+ end
8
+
9
+ def notifier(message, attachments)
10
+ if ENV['SLACK_WEBHOOK']
11
+ @notifier ||= Slack::Notifier.new(ENV['SLACK_WEBHOOK'])
12
+ @notifier.ping(message, {
13
+ attachments: [attachments]
14
+ })
15
+ else
16
+ shout 'You can send deployment notifications if you set the SLACK_WEBHOOK environment variable.'
17
+ end
18
+ end
19
+
4
20
  def shout(message)
5
21
  message_size = message.size
6
22
  if message_size < 77
7
23
  # lines are always 80 characters
8
24
  stars = '*' * (77 - message_size)
9
- puts(green("+ ") + "#{message} #{green(stars)}")
25
+ puts(red("+ ") + "#{message} #{red(stars)}")
10
26
  else
11
- puts(green('+ ') + message)
27
+ puts(red('+ ') + message)
12
28
  end
13
29
  end
14
30
 
@@ -0,0 +1,109 @@
1
+ module Deploy
2
+ class Runner < Thor
3
+ include Deploy::Output
4
+ include Deploy::Commands
5
+ include Deploy::Checks
6
+ include Deploy::Versions
7
+ include Deploy::Utility
8
+
9
+ method_option :version, aliases: '-v', desc: 'Version', required: true
10
+ method_option :environment, aliases: '-e', desc: 'Environment', required: true
11
+ method_option :build, aliases: '-b', desc: 'Build Image', default: true
12
+ desc 'deploy', 'deploy'
13
+ def deploy
14
+ check_setup
15
+
16
+ environment = options[:environment]
17
+ build = options[:build]
18
+
19
+ version = options[:version]
20
+ check_version(version, environment)
21
+
22
+ repo = ENV['DOCKER_REPO']
23
+
24
+ if build && !version_exists?(version)
25
+ announce_title = "Deployment started with build"
26
+ build_image(repo, version)
27
+ else
28
+ announce_title = "Deployment started without build"
29
+ pull_image(repo, version)
30
+ end
31
+
32
+ tag_image_as_latest(repo, version)
33
+
34
+ push_image(repo, version)
35
+ push_image(repo, 'latest')
36
+
37
+ announce({ color: '#6080C0', title: announce_title, text: "Deploying version #{version} to #{environment}" })
38
+ run_deploy(version, environment)
39
+ announce({ color: 'good', title: 'Deployment Succeeded!!', text: "The current version of #{environment} is #{version}" })
40
+ end
41
+
42
+ method_option :version, aliases: '-v', desc: 'Version', required: true
43
+ method_option :environment, aliases: '-e', desc: 'Environment', required: true
44
+ desc 'rollback', 'rollback'
45
+ def rollback
46
+ check_setup
47
+
48
+ environment = options[:environment]
49
+ version = options[:version]
50
+ check_rollback_version(version, environment)
51
+
52
+ repo = ENV['DOCKER_REPO']
53
+
54
+ announce({ color: '#6080C0', title: "Rollback started", text: "Rolling back to #{version} on #{environment}" })
55
+
56
+ pull_image(repo, version)
57
+
58
+ tag_image_as_latest(repo, version)
59
+
60
+ push_image(repo, 'latest')
61
+
62
+ run_rollback(version, environment)
63
+ announce({ color: 'good', title: 'Rollback Succeeded!!', text: "The current version of #{environment} is #{version}" })
64
+ end
65
+
66
+ desc 'send test notification', 'send test notification'
67
+ def test_slack
68
+ notifier('', { color: 'good', title: 'This is a test notification from eb-docker-deploy.' })
69
+ end
70
+
71
+ desc 'list versions', 'list all application versions'
72
+ def versions
73
+ check_setup
74
+
75
+ application_versions_array.each do |version|
76
+ shout version
77
+ end
78
+ end
79
+
80
+ method_option :environment, aliases: '-e', desc: 'Environment', required: true
81
+ desc 'show version', 'show environment version'
82
+ def version
83
+ check_setup
84
+
85
+ shout current_version_for_environment(options[:environment])
86
+ end
87
+
88
+ desc 'setup config', 'setup config'
89
+ def setup
90
+ (shout('AWS creds already configured in ~/.bashrc'); exit(1)) if ENV['AWS_ACCESS_KEY_ID'] && ENV['AWS_SECRET_ACCESS_KEY'] && ENV['AWS_REGION']
91
+
92
+ key = ask('Enter AWS Key:')
93
+ secret = ask('Enter AWS Secret:')
94
+ region = ask('Enter AWS Region:', default: 'us-west-2')
95
+
96
+ File.open(File.expand_path('~/.bashrc'), 'a') do |f|
97
+ f.puts ''
98
+ f.puts '# Variables defined by eb-docker-deploy:'
99
+ f.puts "export AWS_ACCESS_KEY_ID=#{key}"
100
+ f.puts "export AWS_SECRET_ACCESS_KEY=#{secret}"
101
+ f.puts "export AWS_REGION=#{region}"
102
+ end
103
+
104
+ shout('AWS creds successfully configured at ~/.bashrc.')
105
+ shout('You must now run "source ~/.bashrc"')
106
+ end
107
+
108
+ end
109
+ end
@@ -0,0 +1,9 @@
1
+ module Deploy
2
+ module Utility
3
+
4
+ def command?(command)
5
+ system("which #{command} > /dev/null 2>&1")
6
+ end
7
+
8
+ end
9
+ end
@@ -1,3 +1,3 @@
1
1
  module Deploy
2
- VERSION = "0.5.2"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -0,0 +1,24 @@
1
+ require 'deploy/utility'
2
+
3
+ module Deploy
4
+ module Versions
5
+ include Deploy::Utility
6
+
7
+ def eb
8
+ Aws::ElasticBeanstalk::Client.new
9
+ end
10
+
11
+ def version_exists?(version)
12
+ application_versions_array.include?(version)
13
+ end
14
+
15
+ def current_version_for_environment(environment)
16
+ eb.describe_environments(environment_names: [environment]).environments.first.version_label
17
+ end
18
+
19
+ def application_versions_array
20
+ @array ||= eb.describe_application_versions.application_versions.reverse.map(&:version_label)
21
+ end
22
+
23
+ end
24
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eb-docker-deploy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-29 00:00:00.000000000 Z
11
+ date: 2015-08-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -67,19 +67,19 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: rest-client
70
+ name: aws-sdk
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ">="
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '0'
75
+ version: '2'
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ">="
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '0'
82
+ version: '2'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: slack-notifier
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -98,7 +98,6 @@ description: deploy with docker and aws eb
98
98
  email:
99
99
  - thogg4@gmail.com
100
100
  executables:
101
- - eb-deploy
102
101
  - ebd
103
102
  extensions: []
104
103
  extra_rdoc_files: []
@@ -108,14 +107,16 @@ files:
108
107
  - LICENSE.txt
109
108
  - README.md
110
109
  - Rakefile
111
- - bin/eb-deploy
112
110
  - bin/ebd
113
111
  - eb-docker-deploy.gemspec
114
112
  - lib/deploy.rb
115
- - lib/deploy/deployer.rb
116
- - lib/deploy/docker_hub_api.rb
113
+ - lib/deploy/checks.rb
114
+ - lib/deploy/commands.rb
117
115
  - lib/deploy/output.rb
116
+ - lib/deploy/runner.rb
117
+ - lib/deploy/utility.rb
118
118
  - lib/deploy/version.rb
119
+ - lib/deploy/versions.rb
119
120
  homepage: ''
120
121
  licenses:
121
122
  - MIT
@@ -1,5 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'deploy'
4
-
5
- Deploy::Deployer.start(ARGV)
@@ -1,187 +0,0 @@
1
- module Deploy
2
- class Deployer < Thor
3
- include Deploy::Output
4
- include DockerHubApi
5
-
6
- desc 'setup config', 'setup config'
7
- def setup
8
- # setup aws config
9
-
10
- (shout('AWS creds already configured at ~/.aws/config'); exit(1)) if File.exist?(File.expand_path('~/.aws/config'))
11
-
12
- key = ask('Enter AWS Key:')
13
- secret = ask('Enter AWS Secret:')
14
-
15
- Dir.mkdir(File.expand_path('~/.aws'))
16
-
17
- File.open(File.expand_path('~/.aws/config'), 'w') do |f|
18
- f.puts '[default]'
19
- f.puts "aws_access_key_id = #{key}"
20
- f.puts "aws_secret_access_key = #{secret}"
21
- end
22
- shout('AWS creds successfully configured at ~/.aws/config') if File.exist?(File.expand_path('~/.aws/config'))
23
-
24
- # setup docker hub creds
25
- (shout('Docker Hub creds already configured at ~/.docker/config.json'); exit(1)) if File.exist?(File.expand_path('~/.docker/config.json'))
26
- command = "docker login"
27
- exit(1) unless system(command)
28
- end
29
-
30
- method_option :version, aliases: '-v', desc: 'Version'
31
- method_option :environment, aliases: '-e', desc: 'Environment'
32
- method_option :no_build, aliases: '--no-build', desc: 'Do not Build Image'
33
- desc 'deploy', 'deploy'
34
- def deploy
35
- check_setup
36
-
37
- environment = options[:environment]
38
- no_build = options[:no_build]
39
-
40
- version = options[:version]
41
- check_version(version)
42
-
43
- repo = ENV['DOCKER_REPO']
44
-
45
- if !no_build
46
- announce({ color: '#6080C0', title: "Deployment started with build", text: "Deploying version #{version} to #{environment || 'stage'}" })
47
- build_image(repo, version)
48
-
49
- tag_image_as_latest(repo, version)
50
-
51
- push_image(repo, version)
52
- push_image(repo, 'latest')
53
- else
54
- announce({ color: '#6080C0', title: "Deployment started without build", text: "Deploying version #{version} to #{environment || 'stage'}" })
55
- end
56
-
57
- run_deploy(version, environment)
58
- announce({ color: 'good', title: 'Deployment Succeeded!!', text: "The current version of #{environment || 'stage'} is #{version}" })
59
- end
60
-
61
- method_option :version, aliases: '-v', desc: 'Version'
62
- method_option :environment, aliases: '-e', desc: 'Environment'
63
- desc 'rollback', 'rollback'
64
- def rollback
65
- check_setup
66
-
67
- environment = options[:environment]
68
- version = options[:version]
69
- check_rollback_version(version)
70
-
71
- repo = ENV['DOCKER_REPO']
72
-
73
- announce({ color: '#6080C0', title: "Rollback started", text: "Rolling back to #{version} on #{environment || 'stage'}" })
74
-
75
- pull_image(repo, version)
76
-
77
- tag_image_as_latest(repo, version)
78
-
79
- push_image(repo, 'latest')
80
-
81
- run_rollback(version, environment)
82
- announce({ color: 'good', title: 'Rollback Succeeded!!', text: "The current version of #{environment || 'stage'} is #{version}" })
83
- end
84
-
85
- desc 'send test notification', 'send test notification'
86
- def test_slack
87
- notifier('', { color: 'good', title: 'This is a test notification from eb-docker-deploy.' })
88
- end
89
-
90
- desc 'list versions', 'list versions'
91
- def versions
92
- check_setup
93
-
94
- versions_array.each do |v|
95
- shout v
96
- end
97
- end
98
-
99
- no_commands do
100
-
101
- def announce(attachments)
102
- shout("#{attachments[:title]} - #{attachments[:text]}")
103
- notifier('', attachments)
104
- end
105
-
106
- def notifier(message, attachments)
107
- if ENV['SLACK_WEBHOOK']
108
- @notifier ||= Slack::Notifier.new(ENV['SLACK_WEBHOOK'])
109
- @notifier.ping(message, {
110
- attachments: [attachments]
111
- })
112
- else
113
- shout 'You can send deployment notifications if you set the SLACK_WEBHOOK environment variable.'
114
- end
115
- end
116
-
117
- def build_image(repo, tag)
118
- shout "Building Docker Image: #{repo}:#{tag}"
119
- command = "docker build -t #{repo}:#{tag} ."
120
- exit(1) unless system(command)
121
- end
122
-
123
- def tag_image_as_latest(repo, tag)
124
- shout "Tagging #{tag} Docker Image as Latest"
125
- command = "docker tag -f #{repo}:#{tag} #{repo}:latest"
126
- exit(1) unless system(command)
127
- end
128
-
129
- def push_image(repo, tag)
130
- shout "Pushing Docker Image: #{repo}:#{tag}"
131
- command = "docker push #{repo}:#{tag}"
132
- exit(1) unless system(command)
133
- end
134
-
135
- def pull_image(repo, tag)
136
- shout "Pulling Docker Image: #{repo}:#{tag}"
137
- command = "docker pull #{repo}:#{tag}"
138
- exit(1) unless system(command)
139
- end
140
-
141
- def run_deploy(version, environment=nil)
142
- command = environment ? "eb deploy #{environment} --label #{version}" : "eb deploy --label #{version}"
143
- shout "deploying #{version} to elastic beanstalk with command: #{command}"
144
- exit(1) unless system(command)
145
- end
146
-
147
- def run_rollback(version, environment=nil)
148
- command = environment ? "eb deploy #{environment} --version #{version}" : "eb deploy --version #{version}"
149
- shout "deploying #{version} to elastic beanstalk with command: #{command}"
150
- exit(1) unless system(command)
151
- end
152
-
153
- def check_setup
154
- (shout('docker not installed'); exit(1)) unless command?('docker')
155
- (shout('eb command not installed'); exit(1)) unless command?('eb')
156
- (shout('elasticbeanstalk not configured for this project. run "eb init".'); exit(1)) unless File.exist?('.elasticbeanstalk')
157
- (shout('AWS credentials not configured.'); exit(1)) unless File.exist?(File.expand_path('~/.aws/config'))
158
- (shout('Docker Hub credentials not configured.'); exit(1)) unless File.exist?(File.expand_path('~/.docker/config.json'))
159
- (shout('ENV DOCKER_REPO not set'); exit(1)) unless ENV['DOCKER_REPO']
160
- end
161
-
162
- def command?(command)
163
- system("which #{command} > /dev/null 2>&1")
164
- end
165
-
166
- def check_rollback_version(version)
167
- check_version(version)
168
- (shout('You can only rollback to a previous version'); exit(1)) unless versions_array.include?(version)
169
- end
170
-
171
- def check_version(version)
172
- (shout('You must pass a version with -v'); exit(1)) unless version
173
- (shout('You are currently on that version'); exit(1)) if current_version == version
174
- end
175
-
176
- def current_version
177
- versions_array.last
178
- end
179
-
180
- def versions_array
181
- get("/v1/repositories/#{ENV['DOCKER_REPO']}/tags").map { |tag| tag['name'] }
182
- end
183
-
184
- end
185
-
186
- end
187
- end
@@ -1,15 +0,0 @@
1
- module DockerHubApi
2
-
3
- ROOT_PATH = 'https://registry.hub.docker.com'
4
-
5
- def get(url)
6
- body = RestClient.get("#{ROOT_PATH}#{url}", { Authorization: "Basic #{get_token}" }).body
7
- JSON.parse(body)
8
- end
9
-
10
- def get_token
11
- auths_hash = JSON.parse(File.open(File.expand_path('~/.docker/config.json')).read)['auths']
12
- docker_hub_token = auths_hash['https://index.docker.io/v1/']['auth']
13
- end
14
-
15
- end