Class: Dcmgr::Cli::Spec

Inherits:
Base
  • Object
show all
Defined in:
lib/dcmgr/cli/spec.rb

Constant Summary collapse

M =
Dcmgr::Models

Instance Method Summary collapse

Instance Method Details

#addObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/dcmgr/cli/spec.rb', line 17

def add
  UnknownUUIDError.raise(options[:account_id]) if M::Account[options[:account_id]].nil?
  UnsupportedArchError.raise(options[:arch]) unless M::HostNode::SUPPORTED_ARCH.member?(options[:arch])
  UnsupportedHypervisorError.raise(options[:hypervisor]) unless M::HostNode::SUPPORTED_HYPERVISOR.member?(options[:hypervisor])
  uuid = super(M::InstanceSpec,options)
  # add one interface as default
  invoke("addvif", [uuid, 'eth0'])
  invoke("adddrive", [uuid, 'local', 'ephemeral1'])
  puts uuid
end

#adddrive(uuid, type, name) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/dcmgr/cli/spec.rb', line 138

def adddrive(uuid, type, name)
  spec = M::InstanceSpec[uuid]

  index = if options[:index].nil?
            # find max index value.
            index = spec.drives.values.map { |i| i[:index] }.max
            index.nil? ? 0 : (index + 1)
          else
            options[:index].to_i
          end

  case type
  when 'local'
    spec.add_local_drive(name, index, options[:size].to_i)
  when 'volume'
    spec.add_volume_drive(name, index, options[:size].to_i)
  else
    raise "Unknown drive type: #{type}"
  end
  
  spec.save
end

#addvif(uuid, name) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/dcmgr/cli/spec.rb', line 99

def addvif(uuid, name)
  spec = M::InstanceSpec[uuid]

  index = if options[:index].nil?
            # find max index value.
            index = spec.vifs.values.map { |i| i[:index] }.max
            index.nil? ? 0 : (index + 1)
          else
            options[:index].to_i
          end
  
  spec.add_vif(name, index.to_i, options[:bandwidth].to_i)
  spec.save
end

#del(uuid) ⇒ Object



44
45
46
47
# File 'lib/dcmgr/cli/spec.rb', line 44

def del(uuid)
  UnknownUUIDError.raise(uuid) if M::InstanceSpec[uuid].nil?
  super(M::InstanceSpec, uuid)
end

#deldrive(uuid, name) ⇒ Object



162
163
164
165
166
# File 'lib/dcmgr/cli/spec.rb', line 162

def deldrive(uuid, name)
  spec = M::InstanceSpec[uuid]
  spec.remove_drive(name)
  spec.save
end

#delvif(uuid, name) ⇒ Object



115
116
117
118
119
# File 'lib/dcmgr/cli/spec.rb', line 115

def delvif(uuid, name)
  spec = M::InstanceSpec[uuid]
  spec.remove_vif(name)
  spec.save
end

#modify(uuid) ⇒ Object



36
37
38
39
40
41
# File 'lib/dcmgr/cli/spec.rb', line 36

def modify(uuid)
  UnknownUUIDError.raise(options[:account_id]) if options[:account_id] && M::Account[options[:account_id]].nil?
  UnsupportedArchError.raise(options[:arch]) unless options[:arch].nil? || M::HostNode::SUPPORTED_ARCH.member?(options[:arch])
  UnsupportedHypervisorError.raise(options[:hypervisor]) unless options[:hypervisor].nil? || M::HostNode::SUPPORTED_HYPERVISOR.member?(options[:hypervisor])
  super(M::InstanceSpec,uuid,options)
end

#modifydrive(uuid, name) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/dcmgr/cli/spec.rb', line 172

def modifydrive(uuid, name)
  spec = M::InstanceSpec[uuid]
  if options[:index]
    spec.update_drive_index(name, options[:index].to_i)
  end
  if options[:size]
    spec.update_drive_size(name, options[:size].to_i)
  end
  if options[:snapshot_id]
    spec.update_drive_snapshot_id(name, options[:snapshot_id])
  end
  spec.save
end

#modifyvif(uuid, name) ⇒ Object



124
125
126
127
128
129
130
131
132
133
# File 'lib/dcmgr/cli/spec.rb', line 124

def modifyvif(uuid, name)
  spec = M::InstanceSpec[uuid]
  if options[:index]
    spec.update_vif_index(name, options[:index].to_i)
  end
  if options[:bandwidth]
    spec.update_vif_bandwidth(name, options[:bandwidth].to_i)
  end
  spec.save
end

#show(uuid = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/dcmgr/cli/spec.rb', line 50

def show(uuid=nil)
  if uuid
    spec = M::InstanceSpec[uuid] || UnknownUUIDError.raise(uuid)
    print ERB.new(<<__END, nil, '-').result(binding)
UUID: <%= spec.canonical_uuid %>
Account ID: <%= spec.account_id %>
Hypervisor: <%= spec.hypervisor %>
Arch: <%= spec.arch %>
CPU Cores: <%= spec.cpu_cores %>
Memory Size: <%= spec.memory_size %>
Quota Weight: <%= spec.quota_weight %>
<%- unless spec.vifs.empty? -%>
Interfaces:
  <%- spec.vifs.each { |name, i| -%>
  [<%= i[:index] %>] <%= name %>:
Bandwidth: <%= i[:bandwidth] %> kbps
  <%- } -%>
<%- end -%>
<%- unless spec.drives.empty? -%>
Drives:
  <%- spec.drives.each { |name, i| -%>
  [<%= i[:index] %>] <%= name %>:
Type: <%= i[:type] %>
<%- if i[:size] -%>
Size: <%= i[:size] %> MB
<%- else -%>
Snapshot ID: <%= i[:snapshot_id] %>
<%- end -%>
  <%- } -%>
<%- end -%>
<%- unless spec.config.empty? -%>
Hypervisor Configuration:
  <%= spec.config.inspect %>
<%- end -%>
__END
  else
    cond = {}
    specs = M::InstanceSpec.filter(cond).all
    print ERB.new(<<__END, nil, '-').result(binding)
<%- specs.each { |row| -%>
<%= "%-20s  %-15s %-15s" % [row.canonical_uuid, row.account_id, row.arch] %>
<%- } -%>
__END
  end
end