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

#blob, #blob_data, #blobs, #bucket, #buckets, #catched_exceptions_list, #create_blob, #create_bucket, declare_feature, define_hardware_profile, define_instance_states, #delete_blob, #delete_bucket, 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_snapshots, #storage_volume, #storage_volumes

Instance Method Details

#create_instance(credentials, image_id, opts) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 218

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



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

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

#domain_to_realm(dom) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 114

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.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 46

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



69
70
71
72
73
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 69

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



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 128

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



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 176

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



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

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



231
232
233
234
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 231

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



208
209
210
211
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 208

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

#statify(state) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 83

def statify(state)
  st = state.nil? ? "" : state.upcase()
  case st
  when "UP"
    "RUNNING"
  when "DOWN"
    "STOPPED"
  when "POWERING UP"
    "PENDING"
  end
end

#stop_instance(credentials, image_id) ⇒ Object



213
214
215
216
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 213

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

#supported_collectionsObject



38
39
40
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 38

def supported_collections
  DEFAULT_COLLECTIONS.reject { |c| [ :storage_volumes, :storage_snapshots ].include?(c) }
end

#template_to_image(templ) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 144

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



75
76
77
78
79
80
81
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 75

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



194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 194

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