You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltacloud.apache.org by lu...@apache.org on 2010/07/09 01:24:27 UTC

svn commit: r962126 - in /incubator/deltacloud/trunk/drivers/rhevm: config/ config/rhevm_config.yaml.EXAMPLE lib/rhevm_driver.rb

Author: lutter
Date: Thu Jul  8 23:24:27 2010
New Revision: 962126

URL: http://svn.apache.org/viewvc?rev=962126&view=rev
Log:
Add in a config file.

Added:
    incubator/deltacloud/trunk/drivers/rhevm/config/
    incubator/deltacloud/trunk/drivers/rhevm/config/rhevm_config.yaml.EXAMPLE
Modified:
    incubator/deltacloud/trunk/drivers/rhevm/lib/rhevm_driver.rb

Added: incubator/deltacloud/trunk/drivers/rhevm/config/rhevm_config.yaml.EXAMPLE
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/drivers/rhevm/config/rhevm_config.yaml.EXAMPLE?rev=962126&view=auto
==============================================================================
--- incubator/deltacloud/trunk/drivers/rhevm/config/rhevm_config.yaml.EXAMPLE (added)
+++ incubator/deltacloud/trunk/drivers/rhevm/config/rhevm_config.yaml.EXAMPLE Thu Jul  8 23:24:27 2010
@@ -0,0 +1 @@
+domain: demo
\ No newline at end of file

Modified: incubator/deltacloud/trunk/drivers/rhevm/lib/rhevm_driver.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/drivers/rhevm/lib/rhevm_driver.rb?rev=962126&r1=962125&r2=962126&view=diff
==============================================================================
--- incubator/deltacloud/trunk/drivers/rhevm/lib/rhevm_driver.rb (original)
+++ incubator/deltacloud/trunk/drivers/rhevm/lib/rhevm_driver.rb Thu Jul  8 23:24:27 2010
@@ -1,249 +1,252 @@
-
-require 'deltacloud/base_driver'
-require 'yaml'
-
-class RHEVMDriver < DeltaCloud::BaseDriver
-
-  SCRIPT_DIR = File.dirname(__FILE__) + '/../scripts'
-  SCRIPT_DIR_ARG = '"' + SCRIPT_DIR + '"'
-  DELIM_BEGIN="<_OUTPUT>"
-  DELIM_END="</_OUTPUT>"
-  POWERSHELL="c:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe"
-  NO_OWNER=""
-
-  #
-  # Execute a Powershell command, and convert the output
-  # to YAML in order to get back an array of maps.
-  #
-  def execute(credentials, command, *args)
-    args = args.to_a
-    argString = genArgString(credentials, args)
-    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
-
-  def genArgString(credentials, args)
-    if ( credentials[:name].nil? || credentials[:password].nil? || credentials[:name] == '' || credentials[:password] == '' )
-      raise DeltaCloud::AuthException.new
-    end
-    commonArgs = [SCRIPT_DIR_ARG, credentials[:name], credentials[:password], "demo"]
-    commonArgs.concat(args)
-    commonArgs.join(" ")
-  end
-
-  def toYAML(output)
-    yOutput = "- \n" + output
-    yOutput.gsub!(/^(\w*)[ ]*:[ ]*([A-Z0-9a-z._ -:{}]*)/,' \1: "\2"')
-    yOutput.gsub!(/^[ ]*$/,"- ")
-    puts(yOutput)
-    yOutput
-  end
-
-  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
-
-  #
-  # Flavors
-  #
-  FLAVORS = [
-    Flavor.new({
-      :id=>"rhevm",
-      :memory=>"Any Memory",
-      :storage=>"Any Storage",
-      :architecture=>"Any Architecture",
-    })
-  ]
-
-  def flavors(credentials, opts=nil)
-    return FLAVORS if ( opts.nil? || (! opts[:id]))
-    FLAVORS.select{|f| opts[:id] == f.id}
-  end
-
-
-  #
-  # Realms
-  #
-
-  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
-
-  def domain_to_realm(dom)
-    Realm.new({
-      :id => dom["StorageId"],
-      :name => dom["Name"],
-      :limit => dom["AvailableDiskSize"]
-    })
-  end
-
-
-
-  #
-  # Images
-  #
-
-  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
-
-  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
-
-  #
-  # Instances
-  #
-
-  STATE_ACTIONS = [
-    [ :begin, {
-        :stopped => :create}],
-    [ :pending, {
-        :shutting_down=>:stop,
-        :running=>:_auto_ } ],
-    [ :running, {
-        :pending=>:reboot,
-        :shutting_down=>:stop } ],
-    [ :shutting_down, {
-        :stopped=>:_auto_ } ],
-    [ :stopped, {
-        :pending=>:start,
-        :end=>:destroy }],
-  ]
-
-
-  def instance_states()
-    STATE_ACTIONS
-  end
-
-  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
-  end
-
-  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"]),
-      :flavor_id => "rhevm",
-      :actions => instance_actions_for(statify(vm["Status"])),
-    })
-  end
-
-  def start_instance(credentials, image_id)
-    vm = execute(credentials, "startVm.ps1", image_id)
-    vm_to_instance(vm[0])
-  end
-
-  def stop_instance(credentials, image_id)
-    vm = execute(credentials, "stopVm.ps1", image_id)
-    vm_to_instance(vm[0])
-  end
-
-  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")
-        realm_id = realms[0].id
-    end
-    vm = execute(credentials, "addVm.ps1", image_id, name, realm_id)
-    vm_to_instance(vm[0])
-  end
-
-  def reboot_instance(credentials, image_id)
-    vm = execute(credentials, "rebootVm.ps1", image_id)
-    vm_to_instance(vm[0])
-  end
-
-  def destroy_instance(credentials, image_id)
-    vm = execute(credentials, "deleteVm.ps1", image_id)
-    vm_to_instance(vm[0])
-  end
-
-  #
-  # Storage Volumes
-  #
-
-  def storage_volumes(credentials, ids=nil)
-    volumes = []
-    volumes
-  end
-
-  #
-  # Storage Snapshots
-  #
-
-  def storage_snapshots(credentials, ids=nil)
-    snapshots = []
-    snapshots
-  end
-
-end
+
+require 'deltacloud/base_driver'
+require 'yaml'
+
+class RHEVMDriver < DeltaCloud::BaseDriver
+
+  SCRIPT_DIR = File.dirname(__FILE__) + '/../scripts'
+  CONFIG = YAML.load_file(File.dirname(__FILE__) + '/../config/rhevm_config.yaml')
+  SCRIPT_DIR_ARG = '"' + SCRIPT_DIR + '"'
+  DELIM_BEGIN="<_OUTPUT>"
+  DELIM_END="</_OUTPUT>"
+  POWERSHELL="c:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe"
+  NO_OWNER=""
+
+  #
+  # Execute a Powershell command, and convert the output
+  # to YAML in order to get back an array of maps.
+  #
+  def execute(credentials, command, *args)
+    args = args.to_a
+    argString = genArgString(credentials, args)
+    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
+
+  def genArgString(credentials, args)
+    if ( credentials[:name].nil? || credentials[:password].nil? || credentials[:name] == '' || credentials[:password] == '' )
+      raise DeltaCloud::AuthException.new
+    end
+    puts CONFIG["domain"]
+    commonArgs = [SCRIPT_DIR_ARG, credentials[:name], credentials[:password], CONFIG["domain"]]
+    commonArgs.concat(args)
+    commonArgs.join(" ")
+  end
+
+  def toYAML(output)
+    yOutput = "- \n" + output
+    yOutput.gsub!(/^(\w*)[ ]*:[ ]*([A-Z0-9a-z._ -:{}]*)/,' \1: "\2"')
+    yOutput.gsub!(/^[ ]*$/,"- ")
+    puts(yOutput)
+    yOutput
+  end
+
+  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
+
+  #
+  # Flavors
+  #
+  FLAVORS = [
+    Flavor.new({
+      :id=>"rhevm",
+      :memory=>"Any Memory",
+      :storage=>"Any Storage",
+      :architecture=>"Any Architecture",
+    })
+  ]
+
+  def flavors(credentials, opts=nil)
+    return FLAVORS if ( opts.nil? || (! opts[:id]))
+    FLAVORS.select{|f| opts[:id] == f.id}
+  end
+
+
+  #
+  # Realms
+  #
+
+  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
+
+  def domain_to_realm(dom)
+    Realm.new({
+      :id => dom["StorageId"],
+      :name => dom["Name"],
+      :limit => dom["AvailableDiskSize"]
+    })
+  end
+
+
+
+  #
+  # Images
+  #
+
+  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
+
+  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
+
+  #
+  # Instances
+  #
+
+  STATE_ACTIONS = [
+    [ :begin, {
+        :stopped => :create}],
+    [ :pending, {
+        :shutting_down=>:stop,
+        :running=>:_auto_ } ],
+    [ :running, {
+        :pending=>:reboot,
+        :shutting_down=>:stop } ],
+    [ :shutting_down, {
+        :stopped=>:_auto_ } ],
+    [ :stopped, {
+        :pending=>:start,
+        :end=>:destroy }],
+  ]
+
+
+  def instance_states()
+    STATE_ACTIONS
+  end
+
+  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
+  end
+
+  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"]),
+      :flavor_id => "rhevm",
+      :actions => instance_actions_for(statify(vm["Status"])),
+    })
+  end
+
+  def start_instance(credentials, image_id)
+    vm = execute(credentials, "startVm.ps1", image_id)
+    vm_to_instance(vm[0])
+  end
+
+  def stop_instance(credentials, image_id)
+    vm = execute(credentials, "stopVm.ps1", image_id)
+    vm_to_instance(vm[0])
+  end
+
+  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
+
+  def reboot_instance(credentials, image_id)
+    vm = execute(credentials, "rebootVm.ps1", image_id)
+    vm_to_instance(vm[0])
+  end
+
+  def destroy_instance(credentials, image_id)
+    vm = execute(credentials, "deleteVm.ps1", image_id)
+    vm_to_instance(vm[0])
+  end
+
+  #
+  # Storage Volumes
+  #
+
+  def storage_volumes(credentials, ids=nil)
+    volumes = []
+    volumes
+  end
+
+  #
+  # Storage Snapshots
+  #
+
+  def storage_snapshots(credentials, ids=nil)
+    snapshots = []
+    snapshots
+  end
+
+end