Class: Wakame::Command::PropagateResource

Inherits:
Object
  • Object
show all
Includes:
Wakame, Wakame::Command
Defined in:
lib/wakame/command/propagate_resource.rb

Constant Summary

Constants included from Wakame

ED, VERSION

Instance Method Summary collapse

Methods included from Wakame

config, environment, gen_id, log, new_

Methods included from Wakame::Command

included, #options=, #params

Instance Method Details

#runObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/wakame/command/propagate_resource.rb', line 8

def run
  resname = @options["resource"]

  resobj = Service::Resource.find(Service::Resource.id(resname))
  if resobj.nil?
    raise "Unknown Resource: #{resname}" 
  end

  cloud_host_id = @options["cloud_host_id"]
  if cloud_host_id.nil?
    cloud_host = service_cluster.add_cloud_host { |h|
      if @options["vm_attr"].is_a? Hash
        h.vm_attr = @options["vm_attr"]
      end
    }
  else
    cloud_host = Service::CloudHost.find(cloud_host_id) || raise("Specified host was not found: #{cloud_host_id}")
    raise "Same resouce type is already assigned: #{resobj.class} on #{cloud_host_id}" if cloud_host.has_resource_type?(resobj)
  end
  

  num = options["number"] || 1
  raise "Invalid format of number: #{num}" unless /^(\d+)$/ =~ num.to_s
  num = num.to_i

  if num < 1 || resobj.max_instances < service_cluster.instance_count(resobj) + num
    raise "The number must be between 1 and #{resobj.max_instances - service_cluster.instance_count(resobj)} (max limit: #{resobj.max_instances})"
  end

  num.times {
    trigger_action(Wakame::Actions::PropagateResource.new(resobj, cloud_host.id))
  }

end