mamiya 0.1.2 → 0.2.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: 36bd9c34a4fd00eeaa86b5cf5beee9fb61f73a4a
4
- data.tar.gz: 94961d874eed9777d6f281a0e154c15807990c20
3
+ metadata.gz: 8f6320e41f8d3a3c92628c2f4a97b9c6cb650638
4
+ data.tar.gz: 63e601a5888ffb6c6bb8a09cb3e74ce9fd620097
5
5
  SHA512:
6
- metadata.gz: f9c8f23fc8874eb97f98c2f8591aa4d3f6c48d7d5c9b8593dc383280bda9401a2c5151e2785ccbeac3defbcabd12061c4d73d3fcff47747dac6bd7aa88cd02f5
7
- data.tar.gz: bc1b2048ec6f901d3cc98f08c0b02a5e84784c7e98374db56b4d736ced5fb59d64be8c2736df82b610ce8b3c94e3e1501b1b628a29916bb769a5ff9f23f7877d
6
+ metadata.gz: 2e934e133d36bb1386e99e86c786bb8ba41831b6e9c44a708c1e4aa38da34738364b01cb221a3965e07e2cf6efce0415fb919676eabfe5ab2730ab940c3a97b4
7
+ data.tar.gz: f781c7ef2f4c22d5f62462aac173f43381509835784b2e0da8ba0104f309daa02a9d1c503052b6774eda743b737012ed3828f27609c34428485eb0a01557c2de
data/lib/mamiya/cli.rb CHANGED
@@ -124,6 +124,16 @@ module Mamiya
124
124
  ).run!
125
125
  end
126
126
 
127
+ desc "prune NUMS_TO_KEEP", "Delete old packages, but keep last $NUMS_TO_KEEP packages"
128
+ def prune(nums_to_keep)
129
+ puts "Pruning packages from #{application} (keeping last #{nums_to_keep.to_i} packages)..."
130
+
131
+ removed = storage.prune(nums_to_keep.to_i)
132
+
133
+ puts "Pruned #{removed.size} packages:"
134
+ puts removed.join(?\n)
135
+ end
136
+
127
137
  desc "fetch PACKAGE DESTINATION", "Retrieve package from storage"
128
138
  def fetch(package_atom, destination)
129
139
  Mamiya::Steps::Fetch.new(
@@ -293,7 +303,12 @@ module Mamiya
293
303
  end
294
304
 
295
305
  def application
296
- options[:application] || config[:application] || script.application
306
+ @_application ||=
307
+ options[:application] \
308
+ || ENV['MAMIYA_APP'] \
309
+ || ENV['MAMIYA_APPLICATION'] \
310
+ || config[:application] \
311
+ || script.application
297
312
  end
298
313
 
299
314
  def storage
@@ -178,86 +178,16 @@ not distributed: #{dist['not_distributed_count']} agents
178
178
  method_option :no_switch, type: :boolean, default: false
179
179
  method_option :synced_release, type: :boolean, default: false
180
180
  def deploy(package)
181
- @deploy_exception = nil
182
- synced_release = options[:synced_release] || (config && config.synced_release)
183
-
184
- # TODO: move this run on master node side
185
- puts "=> Deploying #{application}/#{package}"
186
- puts " * onto agents which labeled: #{options[:labels].inspect}" if options[:labels] && !options[:labels].empty?
187
- puts " * releasing will be synced in all agents" if synced_release
188
-
189
- show_package(package)
190
-
191
- if config
192
- config.set :application, application
193
- config.set :package_name, package
194
- config.set :package, @meta
195
-
196
- config.before_deploy_or_rollback[]
197
- config.before_deploy[]
198
- end
199
-
200
- do_prep = -> do
201
- puts "=> Preparing..."
202
- prepare(package)
203
- end
204
-
205
- do_prep[]
206
-
207
- puts " * Wait until prepared"
208
- puts ""
209
-
210
- i = 0
211
- loop do
212
- i += 1
213
- do_prep[] if i % 25 == 0
214
-
215
- s = pkg_status(package, :short)
216
- puts ""
217
- break if 0 < s['participants_count'] && s['non_participants'].empty? && s['participants_count'] == s['prepare']['done'].size
218
- sleep 2
219
- end
220
-
221
- ###
222
- #
223
-
224
- unless options[:no_switch]
225
- puts "=> Switching..."
226
- switch_(package, no_release: synced_release)
227
-
228
- puts " * Wait until switch"
229
- puts ""
230
- loop do
231
- s = pkg_status(package, :short)
232
- puts ""
233
- break if s['participants_count'] == s['switch']['done'].size
234
- sleep 2
235
- end
236
-
237
- if synced_release
238
- puts "=> Releasing..."
239
- switch_(package, do_release: true)
240
-
241
- puts " * due to current implementation's limitation, releasing will be untracked."
242
- end
243
- end
244
- rescue Exception => e
245
- @deploy_exception = e
246
- $stderr.puts "ERROR: #{e.inspect}"
247
- $stderr.puts "\t#{e.backtrace.join("\n\t")}"
248
- ensure
249
- config.after_deploy[@deploy_exception] if config
250
- config.after_deploy_or_rollback[@deploy_exception] if config
251
- puts "=> Done."
181
+ deploy_or_rollback(:deploy, package)
252
182
  end
253
183
 
254
- desc "rollback", "Switch back to previous release then finalize"
184
+ desc "rollback", "Switch back to previous release"
255
185
  method_option :labels, type: :string
256
186
  method_option :no_release, type: :boolean, default: false
257
187
  method_option :config, aliases: '-C', type: :string
188
+ method_option :no_switch, type: :boolean, default: false
189
+ method_option :synced_release, type: :boolean, default: false
258
190
  def rollback
259
- @deploy_exception = nil
260
- # TODO: move this run on master node side
261
191
  appstatus = master_get("/applications/#{application}/status", options[:labels] ? {labels: options[:labels]} : {})
262
192
  package = appstatus['common_previous_release']
263
193
 
@@ -265,37 +195,7 @@ not distributed: #{dist['not_distributed_count']} agents
265
195
  raise 'there is no common_previous_release for specified application'
266
196
  end
267
197
 
268
- puts "=> Rolling back #{application} to #{package}"
269
- puts " * with labels: #{options[:labels].inspect}" if options[:labels] && !options[:labels].empty?
270
-
271
- show_package(package)
272
-
273
- if config
274
- config.set :application, application
275
- config.set :package_name, package
276
- config.set :package, @meta
277
-
278
- config.before_deploy_or_rollback[]
279
- config.before_rollback[]
280
- end
281
-
282
- switch(package)
283
-
284
- puts " * Wait until switch"
285
- puts ""
286
- loop do
287
- s = pkg_status(package, :short)
288
- puts ""
289
- break if 0 < s['participants_count'] && s['participants_count'] == s['switch']['done'].size
290
- sleep 2
291
- end
292
- rescue Exception => e
293
- @deploy_exception = e
294
- raise e
295
- ensure
296
- config.after_rollback[@deploy_exception] if config
297
- config.after_deploy_or_rollback[@deploy_exception] if config
298
- puts "=> Done."
198
+ deploy_or_rollback(:rollback, package)
299
199
  end
300
200
 
301
201
  desc "join HOST", "let serf to join to HOST"
@@ -311,7 +211,11 @@ not distributed: #{dist['not_distributed_count']} agents
311
211
  end
312
212
 
313
213
  def application
314
- options[:application] or fatal!('specify application')
214
+ @_application ||=
215
+ ENV['MAMIYA_APP'] \
216
+ || ENV['MAMIYA_APPLICATION'] \
217
+ || options[:application] \
218
+ or fatal!('specify application')
315
219
  end
316
220
 
317
221
  def config
@@ -365,6 +269,88 @@ not distributed: #{dist['not_distributed_count']} agents
365
269
  p master_post("/packages/#{application}/#{package}/switch", params.merge(type: :json))
366
270
  end
367
271
 
272
+ def deploy_or_rollback(type, package)
273
+ @deploy_exception = nil
274
+ synced_release = options[:synced_release] || (config && config.synced_release)
275
+
276
+ # TODO: move this run on master node side
277
+ if type == :deploy
278
+ puts "=> Deploying #{application}/#{package}"
279
+ else
280
+ puts "=> Rolling back #{application} to #{package}"
281
+ end
282
+ puts " * onto agents which labeled: #{options[:labels].inspect}" if options[:labels] && !options[:labels].empty?
283
+ puts " * releasing will be synced in all agents" if synced_release
284
+
285
+ show_package(package)
286
+
287
+ if config
288
+ config.set :application, application
289
+ config.set :package_name, package
290
+ config.set :package, @meta
291
+
292
+ config.before_deploy_or_rollback[]
293
+ (type == :deploy ? config.before_deploy : config.before_rollback)[]
294
+ end
295
+
296
+ if type == :deploy
297
+ do_prep = -> do
298
+ puts "=> Preparing..."
299
+ prepare(package)
300
+ end
301
+
302
+ do_prep[]
303
+
304
+ puts " * Wait until prepared"
305
+ puts ""
306
+
307
+ i = 0
308
+ loop do
309
+ i += 1
310
+ do_prep[] if i % 25 == 0
311
+
312
+ s = pkg_status(package, :short)
313
+ puts ""
314
+ break if 0 < s['participants_count'] && s['non_participants'].empty? && s['participants_count'] == s['prepare']['done'].size
315
+ sleep 2
316
+ end
317
+ end
318
+
319
+ ###
320
+ #
321
+
322
+ unless options[:no_switch]
323
+ puts "=> Switching..."
324
+ switch_(package, no_release: synced_release)
325
+
326
+ puts " * Wait until switch"
327
+ puts ""
328
+ loop do
329
+ s = pkg_status(package, :short)
330
+ puts ""
331
+ break if s['participants_count'] == s['switch']['done'].size
332
+ sleep 2
333
+ end
334
+
335
+ if synced_release
336
+ puts "=> Releasing..."
337
+ switch_(package, do_release: true)
338
+
339
+ puts " * due to current implementation's limitation, releasing will be untracked."
340
+ end
341
+ end
342
+ rescue Exception => e
343
+ @deploy_exception = e
344
+ $stderr.puts "ERROR: #{e.inspect}"
345
+ $stderr.puts "\t#{e.backtrace.join("\n\t")}"
346
+ ensure
347
+
348
+ (type == :deploy ? config.after_deploy : config.after_rollback)[@deploy_exception] if config
349
+ config.after_deploy_or_rollback[@deploy_exception] if config
350
+ puts "=> Done."
351
+
352
+ end
353
+
368
354
  def master_http
369
355
  url = master_url
370
356
  Net::HTTP.new(url.host, url.port).tap do |http|
@@ -75,8 +75,12 @@ module Mamiya
75
75
  def task___switch__finish(status, task)
76
76
  status['currents'] ||= {}
77
77
  status['currents'][task['app']] = task['pkg']
78
- end
79
78
 
79
+ status['releases'] ||= {}
80
+ status['releases'][task['app']] ||= []
81
+ status['releases'][task['app']].push task['pkg']
82
+ status['releases'][task['app']].uniq!
83
+ end
80
84
 
81
85
  def pkg__remove(status, payload, event)
82
86
  status['packages'] ||= {}
@@ -39,7 +39,7 @@ module Mamiya
39
39
 
40
40
  def prune(nums_to_keep)
41
41
  packages = self.packages()
42
- (packages - packages.last(nums_to_keep)).each do |package|
42
+ packages[0...-nums_to_keep].each do |package|
43
43
  self.remove(package)
44
44
  end
45
45
  end
@@ -1,3 +1,3 @@
1
1
  module Mamiya
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -714,7 +714,7 @@ describe Mamiya::Master::AgentMonitor do
714
714
  describe "switch" do
715
715
  describe "success" do
716
716
  let(:status) do
717
- {currents: {}}
717
+ {currents: {}, releases: {}}
718
718
  end
719
719
 
720
720
  it "updates current" do
@@ -724,6 +724,40 @@ describe Mamiya::Master::AgentMonitor do
724
724
  expect(new_status["currents"]['myapp']).to eq "pkg"
725
725
  end
726
726
 
727
+ it "adds release" do
728
+ commit('mamiya:task:finish',
729
+ task: {task: 'switch', app: 'myapp', pkg: 'pkg'})
730
+
731
+ expect(new_status["releases"]['myapp']).to eq ["pkg"]
732
+ end
733
+
734
+ context "with existing release" do
735
+ let(:status) do
736
+ {currents: {}, releases: {'myapp' => ['pkg2']}}
737
+ end
738
+
739
+ it "adds release" do
740
+ commit('mamiya:task:finish',
741
+ task: {task: 'switch', app: 'myapp', pkg: 'pkg'})
742
+
743
+ expect(new_status["releases"]['myapp']).to eq ["pkg2", 'pkg']
744
+ end
745
+ end
746
+
747
+
748
+ context "with release with same name" do
749
+ let(:status) do
750
+ {currents: {}, releases: {'myapp' => ['pkg2', 'pkg']}}
751
+ end
752
+
753
+ it "adds release" do
754
+ commit('mamiya:task:finish',
755
+ task: {task: 'switch', app: 'myapp', pkg: 'pkg'})
756
+
757
+ expect(new_status["releases"]['myapp']).to eq ["pkg2", "pkg"]
758
+ end
759
+ end
760
+
727
761
  context "with existing current" do
728
762
  let(:status) do
729
763
  {currents: {'myapp' => 'pkg1'}}
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mamiya
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shota Fukumori (sora_h)
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-09 00:00:00.000000000 Z
11
+ date: 2014-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor