Class: Deltacloud::Drivers::RHEVM::RHEVMDriver

Inherits:
BaseDriver
  • Object
show all
Defined in:
lib/deltacloud/drivers/rhevm/rhevm_driver.rb

Constant Summary collapse

SCRIPT_DIR =
File.dirname(__FILE__) + '/scripts'
CONFIG =
YAML.load_file(File.dirname(__FILE__) + '/../../../../config/rhevm_config.yml')
SCRIPT_DIR_ARG =
'"' + SCRIPT_DIR + '"'
DELIM_BEGIN =
"<_OUTPUT>"
DELIM_END =
"</_OUTPUT>"
POWERSHELL =
"c:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe"
NO_OWNER =
""

Instance Method Summary collapse

Methods inherited from BaseDriver

#catched_exceptions_list, declare_feature, define_hardware_profile, define_instance_states, feature, feature_decl_for, feature_decls, #features, features, #filter_hardware_profiles, #filter_on, #find_hardware_profile, #hardware_profile, hardware_profiles, #hardware_profiles, #has_collection?, #image, #instance, #instance_actions_for, instance_state_machine, #instance_state_machine, #realm, #safely, #storage_snapshot, #storage_volume, #supported_collections

Instance Method Details

#create_instance(credentials, image_id, opts) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 210

def create_instance(credentials, image_id, opts)
  name = opts[:name]
  name = "Inst-#{rand(10000)}" if (name.nil? or name.empty?)
  realm_id = opts[:realm_id]
  if (realm_id.nil?)
      realms = filter_on(realms(credentials, opts), :name, :name => "data")
      puts realms[0]
      realm_id = realms[0].id
  end
  vm = execute(credentials, "addVm.ps1", image_id, name, realm_id)
  vm_to_instance(vm[0])
end

#destroy_instance(credentials, image_id) ⇒ Object



228
229
230
231
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 228

def destroy_instance(credentials, image_id)
  vm = execute(credentials, "deleteVm.ps1", image_id)
  vm_to_instance(vm[0])
end

#domain_to_realm(dom) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 106

def domain_to_realm(dom)
  Realm.new({
    :id => dom["StorageId"],
    :name => dom["Name"],
    :limit => dom["AvailableDiskSize"]
  })
end

#execute(credentials, command, *args) ⇒ Object

Execute a Powershell command, and convert the output to YAML in order to get back an array of maps.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 42

def execute(credentials, command, *args)
  args = args.to_a
  argString = genArgString(credentials, args)
  puts argString
  outputMaps = {}
  output = `#{POWERSHELL} -command "&{#{File.join(SCRIPT_DIR, command)} #{argString}; exit $LASTEXITCODE}`
  exitStatus = $?.exitstatus
  puts(output)
  puts("EXITSTATUS #{exitStatus}")
  st = output.index(DELIM_BEGIN)
  if (st)
    st += DELIM_BEGIN.length
    ed = output.index(DELIM_END)
    output = output.slice(st, (ed-st))
    # Lets make it yaml
    output.strip!
    if (output.length > 0)
      outputMaps = YAML.load(self.toYAML(output))
    end
  end
  outputMaps
end

#genArgString(credentials, args) ⇒ Object



65
66
67
68
69
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 65

def genArgString(credentials, args)
  commonArgs = [SCRIPT_DIR_ARG, credentials.user, credentials.password, CONFIG["domain"]]
  commonArgs.concat(args)
  commonArgs.join(" ")
end

#images(credentials, opts = nil) ⇒ Object

Images



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 120

def images(credentials, opts=nil )
  templates = []
  if (opts.nil?)
    templates = execute(credentials, "templates.ps1")
  else
    if (opts[:id])
      templates = execute(credentials, "templateById.ps1", opts[:id])
    end
  end
  images = []
  templates.each do |templ|
    images << template_to_image(templ)
  end
  images
end

#instances(credentials, opts = nil) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 168

def instances(credentials, opts=nil)
  vms = []
  if (opts.nil?)
    vms = execute(credentials, "vms.ps1")
  else
    if (opts[:id])
      vms = execute(credentials, "vmById.ps1", opts[:id])
    end
  end
  instances = []
  vms.each do |vm|
    instances << vm_to_instance(vm)
  end
  instances = filter_on( instances, :id, opts )
  instances = filter_on( instances, :state, opts )
  instances
end

#realms(credentials, opts = nil) ⇒ Object

Realms



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 93

def realms(credentials, opts=nil)
  domains = execute(credentials, "storageDomains.ps1")
  if (!opts.nil? && opts[:id])
      domains = domains.select{|d| opts[:id] == d["StorageId"]}
  end

  realms = []
  domains.each do |dom|
    realms << domain_to_realm(dom)
  end
  realms
end

#reboot_instance(credentials, image_id) ⇒ Object



223
224
225
226
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 223

def reboot_instance(credentials, image_id)
  vm = execute(credentials, "rebootVm.ps1", image_id)
  vm_to_instance(vm[0])
end

#start_instance(credentials, image_id) ⇒ Object



200
201
202
203
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 200

def start_instance(credentials, image_id)
  vm = execute(credentials, "startVm.ps1", image_id)
  vm_to_instance(vm[0])
end

#statify(state) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 79

def statify(state)
  st = state.nil? ? "" : state.upcase()
  return "running" if st == "UP"
  return "stopped" if st == "DOWN"
  return "pending" if st == "POWERING UP"
  st
end

#stop_instance(credentials, image_id) ⇒ Object



205
206
207
208
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 205

def stop_instance(credentials, image_id)
  vm = execute(credentials, "stopVm.ps1", image_id)
  vm_to_instance(vm[0])
end

#storage_snapshots(credentials, ids = nil) ⇒ Object

Storage Snapshots



246
247
248
249
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 246

def storage_snapshots(credentials, ids=nil)
  snapshots = []
  snapshots
end

#storage_volumes(credentials, ids = nil) ⇒ Object

Storage Volumes



237
238
239
240
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 237

def storage_volumes(credentials, ids=nil)
  volumes = []
  volumes
end

#template_to_image(templ) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 136

def template_to_image(templ)
  Image.new({
    :id => templ["TemplateId"],
    :name => templ["Name"],
    :description => templ["Description"],
    :architecture => templ["OperatingSystem"],
    :owner_id => NO_OWNER,
    :mem_size_md => templ["MemSizeMb"],
    :instance_count => templ["ChildCount"],
    :state => templ["Status"],
    :capacity => templ["SizeGB"]
  })
end

#toYAML(output) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 71

def toYAML(output)
  yOutput = "- \n" + output
  yOutput.gsub!(/^(\w*)[ ]*:[ ]*([A-Z0-9a-z._ -:{}]*)/,' \1: "\2"')
  yOutput.gsub!(/^[ ]*$/,"- ")
  puts(yOutput)
  yOutput
end

#vm_to_instance(vm) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 186

def vm_to_instance(vm)
  Instance.new({
    :id => vm["VmId"],
    :description => vm["Description"],
    :name => vm["Name"],
    :architecture => vm["OperatingSystem"],
    :owner_id => NO_OWNER,
    :image_id => vm["TemplateId"],
    :state => statify(vm["Status"]),
    :instance_profile => InstanceProfile.new("rhevm"),
    :actions => instance_actions_for(statify(vm["Status"])),
  })
end