ops_deploy 0.1.5 → 0.1.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1770134bbe27e803e4c5905c6bed626973443527
4
- data.tar.gz: 97f6cd9f48cb327737fa4341e8ad3cee9d673dc8
3
+ metadata.gz: b9e5870d3c3aeb5b01f87a22fe89785712961b8e
4
+ data.tar.gz: b15265f789b33d983bdf903138975cc43ff6ac69
5
5
  SHA512:
6
- metadata.gz: 49001ff3e7e53d504719b0d02e6a59d352bb627791c449126c7ccf38d400ebba51781c55cc625ce2e1e5e2a70f66242165ecebecd9d134a33b00ba354dbc5336
7
- data.tar.gz: cca776e64f629e8e4c022f53f3505c62669120ba04f95fe90d3c13f225fc002b37284c46fdeca405102491c46672fa8da18786912a95455da2439a709476e012
6
+ metadata.gz: 33fc3e1de65d22f4c2effd84005923c3b032197b2cd701ecabdc032b84e55980040cdb62aea8b60aad74b4443ee8f2831e2f3ac0a1ca346b3478d2bdd1c290ca
7
+ data.tar.gz: 59f358f786dcb92d434c7c0cc9bd79737839d9f4641c74aeb633d1c97801435709b7344f30b3f74c6d86810b0adec924db930a345ba5b947936f4b504669be66
data/README.md CHANGED
@@ -4,13 +4,13 @@ A simple gem to perform deployment & checks on AWS OpsWorks. You can deploy, wai
4
4
 
5
5
  ### Usage
6
6
 
7
- ```
7
+ ```shell
8
8
  gem install ops_deploy
9
9
  ```
10
10
 
11
11
  In your code:
12
12
 
13
- ```
13
+ ```ruby
14
14
  require 'ops_deploy'
15
15
  ops = OpsDeploy.new(aws_config)
16
16
 
@@ -19,25 +19,25 @@ success = ops.start_deployment(stack_id, app_id, migrate)
19
19
 
20
20
  # Wait for deployments (async)
21
21
  ops.deployments_callback = Proc.new {
22
- |aws_deployment_obj|
23
- # whatever
22
+ |aws_deployment_obj|
23
+ # whatever
24
24
  }
25
25
  a_thread = ops.wait_for_deployments(stack_id)
26
26
  a_thread.join
27
27
 
28
28
  # Check instances (async)
29
29
  ops.instances_check_callback = Proc.new {
30
- |aws_instance_obj, http_response, exception|
31
- # whatever
30
+ |aws_instance_obj, http_response, exception|
31
+ # whatever
32
32
  }
33
- a_thread = ops.check_instances(stack_id).join
33
+ a_thread = ops.check_instances(stack_id)
34
34
  a_thread.join
35
35
 
36
36
  ```
37
37
 
38
38
  or using the CLI:
39
39
 
40
- ```
40
+ ```shell
41
41
  opsdeploy <tasks> --aws-region=<aws_region> --aws-profile=<...> --stack=<stack_name_or_id> --slack-webhook-url=<...> --slack-username=<...> --slack-channel=<...>
42
42
  ```
43
43
 
@@ -50,7 +50,7 @@ The tasks are:
50
50
 
51
51
  Example:
52
52
 
53
- ```
53
+ ```shell
54
54
  opsdeploy deploy wait check --stack="Example" --aws-region="us-east-1"
55
55
  ```
56
56
 
data/bin/opsdeploy CHANGED
@@ -4,19 +4,22 @@ require 'ops_deploy'
4
4
 
5
5
  cli = OpsDeploy::CLI.new
6
6
 
7
- if OpsDeploy::CLI.argument("server")
7
+ if OpsDeploy::CLI.argument('server')
8
8
  cli.start_server
9
- elsif OpsDeploy::CLI.argument("check-server")
9
+ elsif OpsDeploy::CLI.argument('check-server')
10
10
  puts "The 'check-server' argument is deprecated. Please use 'server'."
11
11
  cli.start_server
12
12
  else
13
- stack = OpsDeploy::CLI.argument("stack", "STACK_ID")
14
- app_id = OpsDeploy::CLI.argument("app-id", "APP_ID")
15
- migrate = OpsDeploy::CLI.argument("migrate")
16
- check_via_proxy = OpsDeploy::CLI.argument("check-via-proxy")
17
- wait_via_proxy = OpsDeploy::CLI.argument("wait-via-proxy")
13
+ stack = OpsDeploy::CLI.argument('stack', 'STACK_ID')
14
+ app_id = OpsDeploy::CLI.argument('app-id', 'APP_ID')
15
+ deploy = OpsDeploy::CLI.argument('deploy')
16
+ migrate = OpsDeploy::CLI.argument('migrate')
17
+ check = OpsDeploy::CLI.argument('check')
18
+ check_via_proxy = OpsDeploy::CLI.argument('check-via-proxy')
19
+ wait = OpsDeploy::CLI.argument('wait')
20
+ wait_via_proxy = OpsDeploy::CLI.argument('wait-via-proxy')
18
21
 
19
- cli.start_deployment(stack, app_id, migrate) if OpsDeploy::CLI.argument("deploy")
20
- cli.wait_for_deployments(stack, wait_via_proxy) if OpsDeploy::CLI.argument("wait") || wait_via_proxy
21
- cli.check_instances(stack, check_via_proxy) if OpsDeploy::CLI.argument("check") || check_via_proxy
22
+ cli.start_deployment(stack, app_id, migrate) if deploy
23
+ cli.wait_for_deployments(stack, wait_via_proxy) if wait || wait_via_proxy
24
+ cli.check_instances(stack, check_via_proxy) if check || check_via_proxy
22
25
  end
@@ -1,3 +1,4 @@
1
+ # Notifier
1
2
  class OpsDeploy::CLI::Notifier
2
3
  attr_accessor :options
3
4
  attr_accessor :messages
@@ -5,7 +6,7 @@ class OpsDeploy::CLI::Notifier
5
6
 
6
7
  def initialize(options)
7
8
  @options = options
8
- @options.delete(:slack) if @options[:slack].nil? or @options[:slack][:webhook_url].nil?
9
+ @options.delete(:slack) if @options[:slack].nil? || @options[:slack][:webhook_url].nil?
9
10
  @messages = OpsDeploy::CLI::Notifier::Messages.new
10
11
  @notification_type = :info
11
12
  end
@@ -24,15 +25,24 @@ class OpsDeploy::CLI::Notifier
24
25
  end
25
26
 
26
27
  def info_notify(stack, message)
27
- OpsDeploy::CLI::Notifier::Slack.new(stack, @options[:slack]).notify(message) if @options[:slack]
28
+ return unless @options[:slack]
29
+
30
+ OpsDeploy::CLI::Notifier::Slack.new(stack, @options[:slack])
31
+ .notify(message)
28
32
  end
29
33
 
30
34
  def success_notify(stack, message)
31
- OpsDeploy::CLI::Notifier::Slack.new(stack, @options[:slack]).success_notify(message) if @options[:slack]
35
+ return unless @options[:slack]
36
+
37
+ OpsDeploy::CLI::Notifier::Slack.new(stack, @options[:slack])
38
+ .success_notify(message)
32
39
  end
33
40
 
34
41
  def failure_notify(stack, message)
35
- OpsDeploy::CLI::Notifier::Slack.new(stack, @options[:slack]).failure_notify(message) if @options[:slack]
42
+ return unless @options[:slack]
43
+
44
+ OpsDeploy::CLI::Notifier::Slack.new(stack, @options[:slack])
45
+ .failure_notify(message) if @options[:slack]
36
46
  end
37
47
 
38
48
  def reset
@@ -41,6 +51,7 @@ class OpsDeploy::CLI::Notifier
41
51
  end
42
52
  end
43
53
 
54
+ # A messages container class
44
55
  class OpsDeploy::CLI::Notifier::Messages
45
56
  attr_accessor :info, :success, :failure
46
57
 
@@ -51,22 +62,24 @@ class OpsDeploy::CLI::Notifier::Messages
51
62
  end
52
63
  end
53
64
 
65
+ # A generic notification class from which all notification services will inherit
54
66
  class OpsDeploy::CLI::Notifier::Generic
55
67
  end
56
68
 
69
+ # A notification class for Slack
57
70
  class OpsDeploy::CLI::Notifier::Slack < OpsDeploy::CLI::Notifier::Generic
58
71
  attr_accessor :slack_notifier
59
72
 
60
73
  def initialize(stack, options)
61
74
  @stack = stack
62
75
  @options = options
63
- @options[:username] = "OpsDeploy" unless @options[:username]
76
+ @options[:username] = 'OpsDeploy' unless @options[:username]
64
77
  @slack_notifier = Slack::Notifier.new @options[:webhook_url], @options
65
78
  end
66
79
 
67
80
  def notify(message)
68
- message = message.gsub(/\[[0-9;]+?m/, "")
69
- @slack_notifier.ping "", channel: @options[:channel], attachments: [
81
+ message = message.gsub(/\[[0-9;]+?m/, '')
82
+ @slack_notifier.ping '', channel: @options[:channel], attachments: [
70
83
  {
71
84
  fallback: message,
72
85
  author_name: @stack.name,
@@ -77,33 +90,35 @@ class OpsDeploy::CLI::Notifier::Slack < OpsDeploy::CLI::Notifier::Generic
77
90
  end
78
91
 
79
92
  def success_notify(message)
80
- message = message.gsub(/\[[0-9;]+?m/, "")
81
- @slack_notifier.ping "", channel: @options[:channel], attachments: [
93
+ message = message.gsub(/\[[0-9;]+?m/, '')
94
+ @slack_notifier.ping '', channel: @options[:channel], attachments: [
82
95
  {
83
96
  fallback: message,
84
97
  text: message,
85
98
  author_name: @stack.name,
86
99
  author_link: stack_link(@stack),
87
- color: "good"
100
+ color: 'good'
88
101
  }
89
102
  ]
90
103
  end
91
104
 
92
105
  def failure_notify(message)
93
- message = message.gsub(/\[[0-9;]+?m/, "")
94
- @slack_notifier.ping "", channel: @options[:channel], attachments: [
106
+ message = message.gsub(/\[[0-9;]+?m/, '')
107
+ @slack_notifier.ping '', channel: @options[:channel], attachments: [
95
108
  {
96
109
  fallback: message,
97
110
  text: "#{message} <!channel>",
98
111
  author_name: @stack.name,
99
112
  author_link: stack_link(@stack),
100
- color: "danger"
113
+ color: 'danger'
101
114
  }
102
115
  ]
103
116
  end
104
117
 
105
118
  def stack_link(stack)
106
- region = OpsDeploy::CLI.argument("aws-region", "AWS_REGION") || "us-east-1"
107
- "https://console.aws.amazon.com/opsworks/home?region=#{region}#/stack/#{stack.stack_id}/stack"
119
+ region = OpsDeploy::CLI.argument('aws-region', 'AWS_REGION') || 'us-east-1'
120
+ params = "?region=#{region}#/stack/#{stack.stack_id}/stack"
121
+
122
+ "https://console.aws.amazon.com/opsworks/home#{params}"
108
123
  end
109
124
  end
@@ -2,21 +2,19 @@
2
2
  class OpsDeploy::CLI
3
3
  def initialize
4
4
  config = {
5
- region: OpsDeploy::CLI.argument("aws-region", "AWS_REGION", true),
5
+ region: OpsDeploy::CLI.argument('aws-region', 'AWS_REGION', true)
6
6
  }
7
7
 
8
- profile = OpsDeploy::CLI.argument("aws-profile", "AWS_PROFILE")
8
+ profile = OpsDeploy::CLI.argument('aws-profile', 'AWS_PROFILE')
9
9
  config[:credentials] = Aws::SharedCredentials.new(profile_name: profile) if profile
10
10
 
11
11
  @main = OpsDeploy.new(config)
12
12
  @stacks = {}
13
- @notifier = OpsDeploy::CLI::Notifier.new({
14
- slack: {
15
- webhook_url: OpsDeploy::CLI.argument("slack-webhook-url", "SLACK_WEBHOOK_URL"),
16
- username: OpsDeploy::CLI.argument("slack-username", "SLACK_USERNAME"),
17
- channel: OpsDeploy::CLI.argument("slack-channel", "SLACK_CHANNEL")
18
- }
19
- })
13
+ @notifier = OpsDeploy::CLI::Notifier.new(slack: {
14
+ webhook_url: OpsDeploy::CLI.argument('slack-webhook-url', 'SLACK_WEBHOOK_URL'),
15
+ username: OpsDeploy::CLI.argument('slack-username', 'SLACK_USERNAME'),
16
+ channel: OpsDeploy::CLI.argument('slack-channel', 'SLACK_CHANNEL')
17
+ })
20
18
 
21
19
  @notification_messages = {
22
20
  info: [],
@@ -30,10 +28,10 @@ class OpsDeploy::CLI
30
28
  def start_deployment(stack_id_name_or_object, application_id = nil, migrate = false)
31
29
  stack = find_stack(stack_id_name_or_object)
32
30
 
33
- step_msg("Starting deployment on stack", "'#{stack.name.blue}'...")
31
+ step_msg('Starting deployment on stack', "'#{stack.name.blue}'...")
34
32
 
35
33
  if @main.start_deployment(stack, application_id, migrate)
36
- success_msg("Deployment started on stack", "'#{stack.name.green}'")
34
+ success_msg('Deployment started on stack', "'#{stack.name.green}'")
37
35
  send_notification(stack)
38
36
 
39
37
  true
@@ -49,34 +47,29 @@ class OpsDeploy::CLI
49
47
  stack = find_stack(stack_id_name_or_object)
50
48
 
51
49
  if via_proxy
52
- Pusher.url = OpsDeploy::CLI.argument("pusher-url", "PUSHER_URL", true)
53
- Pusher['OpsDeploy'].trigger('wait_for_deployments', {
54
- stack: stack.stack_id
55
- })
50
+ Pusher.url = OpsDeploy::CLI.argument('pusher-url', 'PUSHER_URL', true)
51
+ Pusher['OpsDeploy'].trigger('wait_for_deployments', stack: stack.stack_id)
56
52
  else
57
- step_msg("Checking deployments...")
58
- @main.deployments_callback = Proc.new {
59
- |deployment|
60
-
53
+ step_msg('Checking deployments...')
54
+ @main.deployments_callback = proc { |deployment|
61
55
  puts
62
-
63
- if (deployment.status == "successful")
64
- success_msg("Deployment", "OK".green.bold, deployment.duration ? "(#{deployment.duration.to_s}s)" : "")
56
+ if (deployment.status == 'successful')
57
+ success_msg('Deployment', 'OK'.green.bold, deployment.duration ? "(#{deployment.duration}s)" : '')
65
58
  else
66
- failure_msg("Deployment", "Failed".red.bold, deployment.duration ? "(#{deployment.duration.to_s}s)" : "")
59
+ failure_msg('Deployment', 'Failed'.red.bold, deployment.duration ? "(#{deployment.duration}s)" : '')
67
60
  end
68
61
  }
69
62
 
70
63
  waiter = @main.wait_for_deployments(stack)
71
64
  if waiter
72
- step_msg("Waiting for deployments to finish...")
65
+ step_msg('Waiting for deployments to finish...')
73
66
 
74
- print ".".blue
75
- print ".".blue until waiter.join(1)
67
+ print '.'.blue
68
+ print '.'.blue until waiter.join(1)
76
69
 
77
- info_msg("Deployments finished")
70
+ info_msg('Deployments finished')
78
71
  else
79
- info_msg("No running deployments on stack", "'#{stack_id_name_or_object.blue}'")
72
+ info_msg('No running deployments on stack', "'#{stack_id_name_or_object.blue}'")
80
73
  end
81
74
 
82
75
  send_notification(stack)
@@ -87,22 +80,17 @@ class OpsDeploy::CLI
87
80
  stack = find_stack(stack_id_name_or_object)
88
81
 
89
82
  if via_proxy
90
- Pusher.url = OpsDeploy::CLI.argument("pusher-url", "PUSHER_URL", true)
91
- Pusher['OpsDeploy'].trigger('check_instances', {
92
- stack: stack.stack_id
93
- })
83
+ Pusher.url = OpsDeploy::CLI.argument('pusher-url', 'PUSHER_URL', true)
84
+ Pusher['OpsDeploy'].trigger('check_instances', stack: stack.stack_id)
94
85
  else
95
- @main.instances_check_callback = Proc.new {
96
- |instance, response, error|
97
-
86
+ @main.instances_check_callback = proc { |instance, response, error|
98
87
  puts
99
-
100
- if (error.nil? and response.code == 200)
101
- success_msg("Response from", "#{instance.hostname.green}:", "200 OK".green)
88
+ if error.nil? && response.code == 200
89
+ success_msg('Response from', "#{instance.hostname.green}:", '200 OK'.green)
102
90
  elsif error.nil?
103
- failure_msg("Response from", "#{instance.hostname.red}:", "#{response.code}".red)
91
+ failure_msg('Response from', "#{instance.hostname.red}:", "#{response.code}".red)
104
92
  else
105
- failure_msg("Error checking", "#{instance.hostname.red}:", error.to_s)
93
+ failure_msg('Error checking', "#{instance.hostname.red}:", error.to_s)
106
94
  end
107
95
  }
108
96
 
@@ -110,12 +98,12 @@ class OpsDeploy::CLI
110
98
  if waiter
111
99
  step_msg("Checking instances' HTTP response...")
112
100
 
113
- print ".".blue
114
- print ".".blue until waiter.join(1)
101
+ print '.'.blue
102
+ print '.'.blue until waiter.join(1)
115
103
 
116
- info_msg("Response check finished")
104
+ info_msg('Response check finished')
117
105
  else
118
- info_msg("No online instances on stack", "'#{stack_id_name_or_object.blue}'")
106
+ info_msg('No online instances on stack', "'#{stack_id_name_or_object.blue}'")
119
107
  end
120
108
 
121
109
  send_notification(stack)
@@ -123,16 +111,16 @@ class OpsDeploy::CLI
123
111
  end
124
112
 
125
113
  def start_server
126
- pusher_comp = URI.parse(OpsDeploy::CLI.argument("pusher-url", "PUSHER_URL", true))
114
+ pusher_comp = URI.parse(OpsDeploy::CLI.argument('pusher-url', 'PUSHER_URL', true))
127
115
  PusherClient.logger.level = Logger::ERROR
128
116
  socket = PusherClient::Socket.new(pusher_comp.user, secure: true)
129
- socket.subscribe("OpsDeploy")
117
+ socket.subscribe('OpsDeploy')
130
118
 
131
- socket["OpsDeploy"].bind("check_instances") do |data|
119
+ socket['OpsDeploy'].bind('check_instances') do |data|
132
120
  begin
133
121
  info = data
134
- info = JSON.parse(data) if data.kind_of?(String)
135
- stack_id = info["stack"] || info[:stack]
122
+ info = JSON.parse(data) if data.is_a?(String)
123
+ stack_id = info['stack'] || info[:stack]
136
124
 
137
125
  check_instances(stack_id)
138
126
  rescue StandardError => e
@@ -140,11 +128,11 @@ class OpsDeploy::CLI
140
128
  end
141
129
  end
142
130
 
143
- socket["OpsDeploy"].bind("wait_for_deployments") do |data|
131
+ socket['OpsDeploy'].bind('wait_for_deployments') do |data|
144
132
  begin
145
133
  info = data
146
- info = JSON.parse(data) if data.kind_of?(String)
147
- stack_id = info["stack"] || info[:stack]
134
+ info = JSON.parse(data) if data.is_a?(String)
135
+ stack_id = info['stack'] || info[:stack]
148
136
 
149
137
  wait_for_deployments(stack_id)
150
138
  rescue StandardError => e
@@ -152,7 +140,7 @@ class OpsDeploy::CLI
152
140
  end
153
141
  end
154
142
 
155
- info_msg("Started OpsDeploy server")
143
+ info_msg("Started OpsDeploy server #{OpsDeploy::VERSION}")
156
144
  socket.connect
157
145
  end
158
146
 
@@ -164,16 +152,18 @@ class OpsDeploy::CLI
164
152
  value = ARGV.include?(argv_name) ? true : nil
165
153
  return value if value
166
154
 
167
- ARGV.each {
168
- |arg|
155
+ ARGV.each do |arg|
169
156
  if arg.start_with?("--#{argv_name}=")
170
157
  value = arg.split("--#{argv_name}=").last
171
158
  end
172
- }
159
+ end
173
160
  end
174
161
 
175
162
  if required && value.nil?
176
- raise StandardError.new("Argument '#{argv_name}' unspecified. Please set #{env_name ? 'the environment variable '+env_name+' or ' : ''}the argument --#{argv_name}=<value>")
163
+ env_name_message = env_name ? "the environment variable #{env_name} or " : ''
164
+ error = "Argument '#{argv_name}' unspecified."
165
+ suggestion = "Please set #{env_name_message}the argument --#{argv_name}=<value>"
166
+ fail StandardError.new("#{error} #{suggestion}")
177
167
  end
178
168
 
179
169
  value
@@ -182,13 +172,13 @@ class OpsDeploy::CLI
182
172
  private
183
173
 
184
174
  def find_stack(stack_id_name_or_object)
185
- if stack_id_name_or_object.kind_of?(String)
175
+ if stack_id_name_or_object.is_a?(String)
186
176
  hash = stack_id_name_or_object.hash
187
177
 
188
178
  if @stacks[hash].nil?
189
- step_msg("Getting stack", "'#{stack_id_name_or_object.blue}'...")
179
+ step_msg('Getting stack', "'#{stack_id_name_or_object.blue}'...")
190
180
  stack = @main.find_stack(stack_id_name_or_object)
191
- step_msg("Found stack", "'#{stack.name.blue}' (#{stack.stack_id})")
181
+ step_msg('Found stack', "'#{stack.name.blue}' (#{stack.stack_id})")
192
182
 
193
183
  @notifier.messages.info.pop
194
184
  @notifier.messages.info.pop
@@ -203,7 +193,7 @@ class OpsDeploy::CLI
203
193
  end
204
194
 
205
195
  def success_msg(*args)
206
- message = "".green+args.join(" ")
196
+ message = ''.green + args.join(' ')
207
197
  @notifier.messages.success << message
208
198
  @notifier.notification_type = :success unless @notifier.notification_type == :failure
209
199
  puts message
@@ -211,14 +201,14 @@ class OpsDeploy::CLI
211
201
  end
212
202
 
213
203
  def info_msg(*args)
214
- message = "// ".blue+args.join(" ")
204
+ message = '// '.blue + args.join(' ')
215
205
  @notifier.messages.info << message
216
206
  puts message
217
207
  message
218
208
  end
219
209
 
220
210
  def failure_msg(*args)
221
- message = "".red+args.join(" ")
211
+ message = ''.red + args.join(' ')
222
212
  @notifier.messages.failure << message
223
213
  @notifier.notification_type = :failure
224
214
  puts message
@@ -226,7 +216,7 @@ class OpsDeploy::CLI
226
216
  end
227
217
 
228
218
  def step_msg(*args)
229
- message = "-> ".cyan+args.join(" ")
219
+ message = '-> '.cyan + args.join(' ')
230
220
  @notifier.messages.info << message
231
221
  puts message
232
222
  message
@@ -236,6 +226,16 @@ class OpsDeploy::CLI
236
226
  @notifier.notify(stack)
237
227
  @notifier.reset
238
228
  end
229
+
230
+ def puts(message = nil)
231
+ $stdout.puts(message) if message
232
+ $stdout.flush
233
+ end
234
+
235
+ def print(message = nil)
236
+ $stdout.print(message) if message
237
+ $stdout.flush
238
+ end
239
239
  end
240
240
 
241
- require_relative "cli/notifier"
241
+ require_relative 'cli/notifier'
@@ -1,3 +1,3 @@
1
- module OpsDeploy
2
- VERSION = "0.1.5"
1
+ class OpsDeploy
2
+ VERSION = '0.1.6.1'
3
3
  end
@@ -1,14 +1,15 @@
1
+ # A class defining a generic 'waiter' thread that waits for tasks to finish
1
2
  class OpsDeploy::Waiter < Thread
2
3
  attr_accessor :end_when, :callback, :data
3
4
 
4
5
  def initialize(&task)
5
6
  @task = task
6
7
 
7
- super() {
8
+ super() do
8
9
  error = nil
9
10
 
10
11
  begin
11
- until false
12
+ loop do
12
13
  @data = @task.call
13
14
  break if @end_when.call(@data)
14
15
  sleep 5
@@ -20,47 +21,56 @@ class OpsDeploy::Waiter < Thread
20
21
  @callback.call(@data, error) if @callback
21
22
 
22
23
  @data
23
- }
24
+ end
24
25
  end
25
26
  end
26
27
 
27
- class OpsDeploy::DeploymentWaiter < OpsDeploy::Waiter
28
+ # A waiter for deployments
29
+ class DeploymentWaiter < OpsDeploy::Waiter
28
30
  def initialize(opsworks, deployment, callback = nil)
29
- super() {
30
- deploy = opsworks.describe_deployments(deployment_ids: [deployment.deployment_id]).deployments.first
31
+ super() do
32
+ find_deployment(opsworks, deployment.deployment_id)
33
+ end
31
34
 
32
- # Retry if there's no duration
33
- if deploy.status != "running" and deploy.duration.nil?
34
- deploy = opsworks.describe_deployments(deployment_ids: [deployment.deployment_id]).deployments.first
35
- end
35
+ @end_when = proc do |deployment_obj|
36
+ deployment_obj.status != 'running'
37
+ end
38
+
39
+ @callback = callback
40
+ end
36
41
 
37
- deploy
38
- }
42
+ private
39
43
 
40
- @end_when = Proc.new {
41
- |deployment_obj|
42
- deployment_obj.status != "running"
43
- }
44
+ def find_deployment(opsworks, deployment_id)
45
+ deploy = opsworks.describe_deployments(deployment_ids: [deployment_id])
46
+ .deployments.first
44
47
 
45
- @callback = callback
48
+ # Retry if there's no duration
49
+ if deploy.status != 'running' && deploy.duration.nil?
50
+ deploy = opsworks.describe_deployments(deployment_ids: [deployment_id])
51
+ .deployments.first
52
+ end
53
+
54
+ deploy
46
55
  end
47
56
  end
48
57
 
49
- class OpsDeploy::InstanceResponseWaiter < OpsDeploy::Waiter
58
+ # A waiter for instance response checks
59
+ class InstanceResponseWaiter < OpsDeploy::Waiter
50
60
  class HTTParty::Basement
61
+ # Overriding HTTParty's timeout
51
62
  default_timeout 30
52
63
  end
53
64
 
54
- def initialize(opsworks, instance, callback = nil)
55
- super() {
65
+ def initialize(_opsworks, instance, callback = nil)
66
+ super() do
56
67
  instance_ip = instance.public_ip || instance.private_ip
57
68
  HTTParty.get("http://#{instance_ip}", verify: false)
58
- }
69
+ end
59
70
 
60
- @end_when = Proc.new { true }
61
- @callback = Proc.new {
62
- |data, error|
71
+ @end_when = proc { true }
72
+ @callback = proc do |data, error|
63
73
  callback.call(instance, data, error)
64
- }
74
+ end
65
75
  end
66
76
  end
data/lib/ops_deploy.rb CHANGED
@@ -1,9 +1,9 @@
1
- require "aws-sdk"
2
- require "httparty"
3
- require "colorize"
4
- require "slack-notifier"
5
- require "pusher"
6
- require "pusher-client"
1
+ require 'aws-sdk'
2
+ require 'httparty'
3
+ require 'colorize'
4
+ require 'slack-notifier'
5
+ require 'pusher'
6
+ require 'pusher-client'
7
7
 
8
8
  class OpsDeploy
9
9
  attr_accessor :waiter
@@ -11,8 +11,8 @@ class OpsDeploy
11
11
  attr_accessor :instances_check_callback
12
12
 
13
13
  def initialize(config = nil)
14
- config = config || {
15
- region: "us-east-1"
14
+ config ||= {
15
+ region: 'us-east-1'
16
16
  }
17
17
 
18
18
  @opsworks = Aws::OpsWorks::Client.new(config)
@@ -22,48 +22,43 @@ class OpsDeploy
22
22
  @opsworks.describe_stacks.stacks
23
23
  end
24
24
 
25
- def start_deployment(stack_id_name_or_object, application_id_name_or_object = nil, migrate = false)
25
+ def start_deployment(stack_id_name_or_object,
26
+ application_id_name_or_object = nil,
27
+ migrate = false)
26
28
  stack = find_stack(stack_id_name_or_object)
27
29
  app = find_app(stack, application_id_name_or_object)
28
30
 
29
- command = { name: "deploy" }
30
- command["args"] = { migrate: ["true"] } if migrate
31
+ command = { name: 'deploy' }
32
+ command['args'] = { migrate: ['true'] } if migrate
31
33
 
32
- resp = @opsworks.create_deployment({
33
- stack_id: stack.stack_id,
34
- app_id: app.app_id,
35
- command: command
36
- })
34
+ resp = @opsworks.create_deployment(stack_id: stack.stack_id,
35
+ app_id: app.app_id,
36
+ command: command)
37
37
 
38
- return (resp && resp.deployment_id)
38
+ (resp && resp.deployment_id)
39
39
  end
40
40
 
41
41
  def wait_for_deployments(stack_id_name_or_object)
42
42
  stack = find_stack(stack_id_name_or_object)
43
43
 
44
- running_deployments = @opsworks.describe_deployments(stack_id: stack.stack_id).deployments.select {
45
- |deployment|
46
-
47
- deployment.status == "running"
48
- }
44
+ running_deployments = @opsworks.describe_deployments(stack_id: stack.stack_id)
45
+ .deployments.select do |deployment|
46
+ deployment.status == 'running'
47
+ end
49
48
 
50
49
  if running_deployments.empty?
51
50
  false
52
51
  else
53
52
  waiters = []
54
53
 
55
- running_deployments.each {
56
- |deployment|
57
-
54
+ running_deployments.each do |deployment|
58
55
  waiters << OpsDeploy::DeploymentWaiter.new(@opsworks, deployment, @deployments_callback)
59
- }
60
-
61
- @waiter = Thread.new(waiters) {
62
- |deploy_threads|
56
+ end
63
57
 
58
+ @waiter = Thread.new(waiters) do |deploy_threads|
64
59
  deploy_threads.each(&:run)
65
60
  deploy_threads.each(&:join)
66
- }
61
+ end
67
62
 
68
63
  @waiter.run
69
64
  end
@@ -72,25 +67,19 @@ class OpsDeploy
72
67
  def check_instances(stack_id_name_or_object)
73
68
  stack = find_stack(stack_id_name_or_object)
74
69
 
75
- running_instances = @opsworks.describe_instances(stack_id: stack.stack_id).instances.select {
76
- |instance|
77
-
78
- instance.status == "online"
79
- }
70
+ running_instances = @opsworks.describe_instances(stack_id: stack.stack_id).instances.select do |instance|
71
+ instance.status == 'online'
72
+ end
80
73
 
81
74
  waiters = []
82
- running_instances.each {
83
- |instance|
84
-
75
+ running_instances.each do |instance|
85
76
  waiters << OpsDeploy::InstanceResponseWaiter.new(@opsworks, instance, @instances_check_callback)
86
- }
87
-
88
- @waiter = Thread.new(waiters) {
89
- |check_threads|
77
+ end
90
78
 
79
+ @waiter = Thread.new(waiters) do |check_threads|
91
80
  check_threads.each(&:run)
92
81
  check_threads.each(&:join)
93
- }
82
+ end
94
83
 
95
84
  @waiter.run
96
85
  end
@@ -98,29 +87,29 @@ class OpsDeploy
98
87
  def find_stack(stack_id_name_or_object)
99
88
  found_stack = nil
100
89
 
101
- if stack_id_name_or_object.kind_of?(String)
90
+ if stack_id_name_or_object.is_a?(String)
102
91
  begin
103
92
  if stack_id_name_or_object.match(/^[0-9a-f\-]+$/)
104
93
  found_stack = @opsworks.describe_stacks(stack_ids: [stack_id_name_or_object]).stacks.first
105
94
  end
106
95
  rescue Aws::OpsWorks::Errors::ResourceNotFoundException
96
+ found_stack = nil
107
97
  end
108
98
 
109
99
  if found_stack.nil?
110
- @opsworks.describe_stacks.stacks.each {
111
- |stack|
112
-
100
+ @opsworks.describe_stacks.stacks.each do |stack|
113
101
  if stack.name == stack_id_name_or_object
114
102
  found_stack = stack
115
103
  break
116
104
  end
117
- }
105
+ end
118
106
  end
119
107
  end
120
108
 
121
109
  found_stack = stack_id_name_or_object if found_stack.nil?
122
- invalid_stack_error = StandardError.new("Invalid stack #{found_stack} (#{stack_id_name_or_object}).")
123
- raise invalid_stack_error unless found_stack.kind_of?(Aws::OpsWorks::Types::Stack)
110
+ error_message = "Invalid stack #{found_stack} (#{stack_id_name_or_object})."
111
+ invalid_stack_error = StandardError.new(error_message)
112
+ fail invalid_stack_error unless found_stack.is_a?(Aws::OpsWorks::Types::Stack)
124
113
 
125
114
  found_stack
126
115
  end
@@ -128,25 +117,22 @@ class OpsDeploy
128
117
  def find_app(stack, application_id_name_or_object)
129
118
  found_app = nil
130
119
 
131
- if application_id_name_or_object.kind_of?(String)
120
+ if application_id_name_or_object.is_a?(String)
132
121
  begin
133
122
  if application_id_name_or_object.match(/^[0-9a-f\-]+$/)
134
- found_app = @opsworks.describe_apps({
135
- app_ids: [application_id_name_or_object]
136
- }).apps.first
123
+ found_app = @opsworks.describe_apps(app_ids: [application_id_name_or_object]).apps.first
137
124
  end
138
125
  rescue Aws::OpsWorks::Errors::ResourceNotFoundException
126
+ found_app = nil
139
127
  end
140
128
 
141
129
  if found_app.nil?
142
- @opsworks.describe_apps(stack_id: stack.stack_id).apps.each {
143
- |app|
144
-
130
+ @opsworks.describe_apps(stack_id: stack.stack_id).apps.each do |app|
145
131
  if app.name == application_id_name_or_object
146
132
  found_app = app
147
133
  break
148
134
  end
149
- }
135
+ end
150
136
  end
151
137
  elsif application_id_name_or_object.nil?
152
138
  apps = @opsworks.describe_apps(stack_id: stack.stack_id).apps
@@ -154,12 +140,14 @@ class OpsDeploy
154
140
  end
155
141
 
156
142
  found_app = application_id_name_or_object if found_app.nil?
157
- invalid_app_error = StandardError.new("Invalid app #{found_app} (#{application_id_name_or_object}).")
158
- raise invalid_app_error unless found_app.kind_of?(Aws::OpsWorks::Types::App)
143
+ error_message = "Invalid app #{found_app} (#{application_id_name_or_object})."
144
+ invalid_app_error = StandardError.new(error_message)
145
+ fail invalid_app_error unless found_app.is_a?(Aws::OpsWorks::Types::App)
159
146
 
160
147
  found_app
161
148
  end
162
149
  end
163
150
 
164
- require_relative "ops_deploy/waiter"
165
- require_relative "ops_deploy/cli"
151
+ require_relative 'ops_deploy/waiter'
152
+ require_relative 'ops_deploy/version'
153
+ require_relative 'ops_deploy/cli'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ops_deploy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mahdi Bchetnia
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-08 00:00:00.000000000 Z
11
+ date: 2016-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk
@@ -136,3 +136,4 @@ signing_key:
136
136
  specification_version: 4
137
137
  summary: Perform deployment & checks on AWS OpsWorks
138
138
  test_files: []
139
+ has_rdoc: