manifests-vmc-plugin 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1 +1,47 @@
1
- require "bundler/gem_tasks"
1
+ require "rake"
2
+
3
+ $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
4
+ require "manifests-vmc-plugin/version"
5
+
6
+ task :default => :spec
7
+
8
+ desc "Run specs"
9
+ task :spec => ["bundler:install", "test:spec"]
10
+
11
+ desc "Run integration tests"
12
+ task :test => ["bundler:install", "test:integration"]
13
+
14
+ task :build do
15
+ sh "gem build manifests-vmc-plugin.gemspec"
16
+ end
17
+
18
+ task :install => :build do
19
+ sh "gem install --local manifests-vmc-plugin-#{VMCManifests::VERSION}"
20
+ end
21
+
22
+ task :uninstall do
23
+ sh "gem uninstall manifests-vmc-plugin"
24
+ end
25
+
26
+ task :reinstall => [:uninstall, :install]
27
+
28
+ task :release => :build do
29
+ sh "gem push manifests-vmc-plugin-#{VMCManifests::VERSION}.gem"
30
+ end
31
+
32
+ namespace "bundler" do
33
+ desc "Install gems"
34
+ task "install" do
35
+ sh("bundle install")
36
+ end
37
+ end
38
+
39
+ namespace "test" do
40
+ task "spec" do |t|
41
+ # nothing
42
+ end
43
+
44
+ task "integration" do |t|
45
+ sh("cd spec && bundle exec rake spec")
46
+ end
47
+ end
@@ -19,18 +19,20 @@ class Manifests < VMC::CLI
19
19
  [ :start, :instances, :logs, :file, :files, :env,
20
20
  :health, :stats, :scale
21
21
  ].each do |wrap|
22
- optional_name = change_argument(wrap, :name, :optional)
22
+ optional_name = change_argument(wrap, :app, :optional)
23
23
 
24
24
  around(wrap) do |cmd, input|
25
+ num = 0
25
26
  rest =
26
- specific_apps_or_all(input) do |app|
27
- cmd.call(input.without(:names).merge(:name => app[:name]))
28
- puts "" unless quiet?
27
+ specific_apps_or_all(input) do |info|
28
+ puts "" unless quiet? || num == 0
29
+ cmd.call(input.without(:apps).merge_given(:app => info[:name]))
30
+ num += 1
29
31
  end
30
32
 
31
33
  if rest
32
- rest.each do |n|
33
- cmd.call(input.merge(:name => n))
34
+ rest.each do |name|
35
+ cmd.call(input.without(:apps).merge_given(:app => name))
34
36
  end
35
37
 
36
38
  # fail manually for commands whose name we made optional
@@ -48,18 +50,20 @@ class Manifests < VMC::CLI
48
50
 
49
51
  reversed = []
50
52
  rest =
51
- specific_apps_or_all(input) do |app|
52
- reversed.unshift app[:name]
53
+ specific_apps_or_all(input) do |info|
54
+ reversed.unshift info[:name]
53
55
  end
54
56
 
55
57
  unless reversed.empty?
56
- cmd.call(input.merge(:names => reversed))
58
+ cmd.call(input.without(:apps).merge_given(:apps => reversed))
57
59
  end
58
60
 
59
61
  if rest
60
- cmd.call(input.merge(:names => rest)) unless rest.empty?
62
+ unless rest.empty?
63
+ cmd.call(input.without(:apps).merge_given(:apps => rest))
64
+ end
61
65
  else
62
- cmd.call(input.without(:names))
66
+ cmd.call(input.without(:apps))
63
67
  end
64
68
  end
65
69
  end
@@ -88,9 +92,16 @@ class Manifests < VMC::CLI
88
92
 
89
93
  with_filters(
90
94
  :push => {
91
- :push_app => proc { |a| setup_app(a, app); a }
95
+ :create_app => proc { |a|
96
+ setup_env(a, app)
97
+ a
98
+ },
99
+ :push_app => proc { |a|
100
+ setup_services(a, app)
101
+ a
102
+ }
92
103
  }) do
93
- push.call(input.merge(app).merge(
104
+ push.call(input.merge_given(app).merge(
94
105
  :bind_services => false,
95
106
  :create_services => false))
96
107
  end
@@ -1,3 +1,3 @@
1
1
  module VMCManifests
2
- VERSION = "0.3.1"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -281,12 +281,12 @@ module VMCManifests
281
281
  # returns the names that were not paths
282
282
  def specific_apps_or_all(input = nil, use_name = true, &blk)
283
283
  names_or_paths =
284
- if input.given?(:names)
284
+ if input.given?(:apps)
285
285
  # names may be given but be [], which will still cause
286
286
  # interaction, so use #given instead of #[] here
287
- input.given(:names)
288
- elsif input.given?(:name)
289
- [input[:name]]
287
+ input.given(:apps)
288
+ elsif input.given?(:app)
289
+ [input[:app]]
290
290
  else
291
291
  []
292
292
  end
@@ -304,7 +304,7 @@ module VMCManifests
304
304
  return []
305
305
  end
306
306
 
307
- input = input.without(:name, :names)
307
+ input = input.without(:app, :apps)
308
308
 
309
309
  in_manifest = []
310
310
  external = []
@@ -373,8 +373,8 @@ module VMCManifests
373
373
  # redeploys the app if necessary (after prompting the user), e.g. for
374
374
  # runtime/framework change
375
375
  def sync_changes(info)
376
- app = client.app(info[:name])
377
- return unless app.exists?
376
+ app = client.app_by_name(info[:name])
377
+ return unless app
378
378
 
379
379
  diff = {}
380
380
  need_restage = []
@@ -393,8 +393,19 @@ module VMCManifests
393
393
  diff[k] = [old.inspect, v.inspect]
394
394
  app.env = v
395
395
  end
396
- when "framework", "runtime", "command"
396
+ when "framework", "runtime"
397
397
  old = app.send(k)
398
+ new = client.send("#{k}s").find do |x|
399
+ x.name == v
400
+ end
401
+
402
+ if old != new
403
+ diff[k] = [old.name, new.name]
404
+ app.send(:"#{k}=", new)
405
+ need_restage << k
406
+ end
407
+ when "command"
408
+ old = app.command
398
409
  if old != v
399
410
  diff[k] = [old, v]
400
411
  app.send(:"#{k}=", v)
@@ -444,12 +455,15 @@ module VMCManifests
444
455
  end
445
456
 
446
457
  if force? || ask("Redeploy?", :default => false)
458
+ bindings = app.services
459
+
447
460
  with_progress("Deleting #{c(app.name, :name)}") do
448
461
  app.delete!
449
462
  end
450
463
 
451
464
  with_progress("Recreating #{c(app.name, :name)}") do
452
465
  app.create!
466
+ app.bind(*bindings)
453
467
  end
454
468
  end
455
469
  end
@@ -458,24 +472,29 @@ module VMCManifests
458
472
  def ask_to_save(input, app)
459
473
  return if manifest_file
460
474
 
461
- services = app.services.collect { |name| client.service(name) }
475
+ service_instances = app.services
462
476
 
463
477
  meta = {
464
478
  "name" => app.name,
465
- "framework" => app.framework,
466
- "runtime" => app.runtime,
467
- "memory" => human_size(app.memory, 0),
479
+ "framework" => app.framework.name,
480
+ "runtime" => app.runtime.name,
481
+ "memory" => human_size(app.memory * 1024 * 1024, 0),
468
482
  "instances" => app.total_instances,
469
483
  "url" => app.url
470
484
  }
471
485
 
472
- unless services.empty?
486
+ unless service_instances.empty?
473
487
  meta["services"] = {}
474
488
 
475
- services.each do |s|
476
- meta["services"][s.name] = {
477
- "vendor" => s.vendor,
478
- "version" => s.version
489
+ service_instances.each do |i|
490
+ p = i.service_plan
491
+ s = p.service
492
+
493
+ meta["services"][i.name] = {
494
+ "label" => s.label,
495
+ "provider" => s.provider,
496
+ "version" => s.version,
497
+ "plan" => p.name
479
498
  }
480
499
  end
481
500
  end
@@ -485,9 +504,16 @@ module VMCManifests
485
504
  end
486
505
 
487
506
  if ask("Save configuration?", :default => false)
507
+ if input[:path] =~ /\.[[:alnum:]]+$/
508
+ root = ask("Application root", :default => ".")
509
+ meta["path"] = input[:path]
510
+ else
511
+ root = input[:path]
512
+ end
513
+
488
514
  File.open("manifest.yml", "w") do |io|
489
515
  YAML.dump(
490
- {"applications" => {(input[:path] || ".") => meta}},
516
+ { "applications" => { root => meta } },
491
517
  io)
492
518
  end
493
519
 
@@ -495,31 +521,91 @@ module VMCManifests
495
521
  end
496
522
  end
497
523
 
498
- def setup_app(app, info)
499
- app.env = info[:env]
524
+ def env_hash(val)
525
+ if val.is_a?(Hash)
526
+ val
527
+ else
528
+ hash = {}
529
+
530
+ val.each do |pair|
531
+ name, val = pair.split("=", 2)
532
+ hash[name] = val
533
+ end
534
+
535
+ hash
536
+ end
537
+ end
538
+
539
+ def setup_env(app, info)
540
+ return unless info[:env]
541
+ app.env = env_hash(info[:env])
542
+ end
500
543
 
544
+ def setup_services(app, info)
501
545
  return if !info[:services] || info[:services].empty?
502
546
 
503
- services = client.system_services
547
+ services = client.services
548
+
549
+ to_bind = []
504
550
 
505
551
  info[:services].each do |name, svc|
506
- service = client.service(name)
552
+ if instance = client.service_instance_by_name(name)
553
+ to_bind << instance
554
+ else
555
+ service = services.find { |s|
556
+ s.label == (svc["label"] || svc["type"] || svc["vendor"]) &&
557
+ (!svc["version"] || s.version == svc["version"]) &&
558
+ (s.provider == (svc["provider"] || "core"))
559
+ }
507
560
 
508
- unless service.exists?
509
- service.vendor = svc["vendor"] || svc["type"]
561
+ fail "Unknown service." unless service
510
562
 
511
- service_meta = services[service.vendor]
563
+ plan = service.service_plans.find { |p|
564
+ p.name == svc["plan"] || "D100"
565
+ }
512
566
 
513
- service.type = service_meta[:type]
514
- service.version = svc["version"] || service_meta[:versions].first
515
- service.tier = "free"
567
+ fail "Unknown service plan." unless plan
516
568
 
517
- with_progress("Creating service #{c(service.name, :name)}") do
518
- service.create!
519
- end
569
+ invoke :create_service,
570
+ :name => name,
571
+ :service => service,
572
+ :plan => plan,
573
+ :app => app
574
+ end
575
+ end
576
+
577
+ to_bind.each do |i|
578
+ # TODO: splat
579
+ invoke :bind_service,
580
+ :app => app,
581
+ :instance => i
582
+ end
583
+ end
584
+
585
+ def megabytes(str)
586
+ if str =~ /T$/i
587
+ str.to_i * 1024 * 1024
588
+ elsif str =~ /G$/i
589
+ str.to_i * 1024
590
+ elsif str =~ /M$/i
591
+ str.to_i
592
+ elsif str =~ /K$/i
593
+ str.to_i / 1024
594
+ else # assume megabytes
595
+ str.to_i
596
+ end
597
+ end
598
+
599
+ def human_size(num, precision = 1)
600
+ sizes = ["G", "M", "K"]
601
+ sizes.each.with_index do |suf, i|
602
+ pow = sizes.size - i
603
+ unit = 1024 ** pow
604
+ if num >= unit
605
+ return format("%.#{precision}f%s", num / unit, suf)
520
606
  end
521
607
  end
522
608
 
523
- app.services = info[:services].keys
609
+ format("%.#{precision}fB", num)
524
610
  end
525
611
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: manifests-vmc-plugin
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 3
9
- - 1
10
- version: 0.3.1
8
+ - 4
9
+ - 0
10
+ version: 0.4.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Alex Suraci
@@ -15,9 +15,24 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-07-03 00:00:00 Z
19
- dependencies: []
20
-
18
+ date: 2012-07-23 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: cfoundry
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 1
29
+ segments:
30
+ - 0
31
+ - 3
32
+ - 9
33
+ version: 0.3.9
34
+ type: :runtime
35
+ version_requirements: *id001
21
36
  description:
22
37
  email:
23
38
  - asuraci@vmware.com