Class: Deltacloud::Drivers::Rhevm::RhevmDriver

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

Constant Summary collapse

USER_NAME_MAX =
constraints(:collection => :instances, :feature => :user_name)[:max_length]

Constants inherited from BaseDriver

BaseDriver::MEMBER_SHOW_METHODS, BaseDriver::STATE_MACHINE_OPTS

Constants included from Deltacloud

API_VERSION, CIMI_API_VERSION

Instance Method Summary collapse

Methods inherited from BaseDriver

#address, #api_provider, #blob, #bucket, #catched_exceptions_list, #configured_providers, constraints, define_hardware_profile, define_instance_states, driver_name, feature, features, #filter_hardware_profiles, #filter_on, #find_hardware_profile, #firewall, #hardware_profile, #hardware_profiles, hardware_profiles, #has_capability?, #has_feature?, has_feature?, #image, #instance, #instance_actions_for, #instance_state_machine, instance_state_machine, #key, #name, #realm, #storage_snapshot, #storage_volume, #supported_collections

Methods included from Deltacloud

[], config, configure, connect, database, default_frontend, drivers, enabled_frontends, frontend_required?, frontends, generate_routes, initialize_database, need_database?, new, require_frontend!

Methods included from CollectionMethods

#collection_exists?, #collection_names, #collections

Methods included from Exceptions

exception_from_status, exceptions, included, logger, #safely

Instance Method Details

#create_image(credentials, opts = {}) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 104

def create_image(credentials, opts={})
  client = new_client(credentials)
  unless opts[:name]
    instance = instances(credentials, :id => opts[:id])
    raise "Specified instance '#{opts[:id]}' not found"
    template_name = "#{instance.first.name}-template"
  end
  safely do
    new_image = client.create_template(:vm => opts[:id], :name => (opts[:name] || template_name),
                                       :description => opts[:description] || template_name)
    convert_image(client, new_image)
  end
end

#create_instance(credentials, image_id, opts = {}) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 180

def create_instance(credentials, image_id, opts={})
  client = new_client(credentials)
  params = {}
  safely do
    if opts[:name]
      raise "Parameter name must be #{USER_NAME_MAX} characters or less" if opts[:name].length > USER_NAME_MAX
    end
    params[:name] = opts[:name]
    params[:template] = image_id
    params[:cluster] = opts[:realm_id] if opts[:realm_id]
    params[:hwp_id] = opts[:hwp_id] if opts[:hwp_id]
    params[:memory] = (opts[:hwp_memory].to_i * 1024 * 1024) if opts[:hwp_memory]
    params[:cores] = opts[:hwp_cpu] if opts[:hwp_cpu]
    params[:user_data] = opts[:user_data].gsub(/\n/,'') if opts[:user_data]
    params[:fileinject_path] = "deltacloud-user-data.txt"
    convert_instance(client, client.create_vm(params))
  end
end

#destroy_image(credentials, image_id) ⇒ Object



118
119
120
121
122
123
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 118

def destroy_image(credentials, image_id)
  client = new_client(credentials)
  safely do
    client.destroy_template(image_id)
  end
end

#destroy_instance(credentials, id) ⇒ Object



161
162
163
164
165
166
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 161

def destroy_instance(credentials, id)
  client = new_client(credentials)
  safely do
    raise "ERROR: Operation destroy failed" unless client.destroy_vm(id)
  end
end

#images(credentials, opts = {}) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 85

def images(credentials, opts={})
  client = new_client(credentials)
  img_arr = []
  safely do
    if opts[:id]
      begin
        img_arr << convert_image(client, client.template(opts[:id]))
      rescue OVIRT::OvirtException => e
        raise e unless e.message =~ /Resource Not Found/
        img_arr = []
      end
    else
      img_arr = client.templates.collect { |t| convert_image(client, t) }
    end
  end
  img_arr = filter_on( img_arr, :architecture, opts )
  img_arr.sort_by{|e| [e.owner_id, e.name]}
end

#instances(credentials, opts = {}) ⇒ Object



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

def instances(credentials, opts={})
  client = new_client(credentials)
  inst_arr = []
  safely do
    if opts[:id]
      begin
        vm = client.vm(opts[:id])
        inst_arr << convert_instance(client, vm)
      rescue => e
        raise e unless e.message =~ /Resource Not Found/
      end
    else
      vms = client.vms
      vms.each do |vm|
        inst_arr << convert_instance(client, vm)
      end
    end
  end
  inst_arr = filter_on( inst_arr, :id, opts )
  filter_on( inst_arr, :state, opts )
end

#providers(credentials) ⇒ Object



62
63
64
65
66
67
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 62

def providers(credentials)
  client = new_client(credentials)
  safely do
    client.datacenters.collect { |dc| convert_provider(dc) }
  end
end

#realms(credentials, opts = {}) ⇒ Object

Realms



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

def realms(credentials, opts={})
  client = new_client(credentials)
  realm_arr = []
  safely do
    realm_arr = client.clusters.collect do |r|
      convert_realm(r, client.datacenter(r.datacenter.id))
    end
  end
  realm_arr = filter_on( realm_arr, :id, opts )
  realm_arr
end

#start_instance(credentials, id) ⇒ Object



147
148
149
150
151
152
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 147

def start_instance(credentials, id)
  client = new_client(credentials)
  safely do
    raise "ERROR: Operation start failed" unless client.vm_action(id, :start)
  end
end

#stop_instance(credentials, id) ⇒ Object



154
155
156
157
158
159
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 154

def stop_instance(credentials, id)
  client = new_client(credentials)
  safely do
    raise "ERROR: Operation stop failed" unless client.vm_action(id, :shutdown)
  end
end

#storage_volumes(credentials, opts = {}) ⇒ Object



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

def storage_volumes(credentials, opts={})
  client = new_client(credentials)
  domains_arr = []
  safely do
    client.storagedomains.each do |s|
      domains_arr << convert_volume(s)
    end
  end
  domains_arr = filter_on( domains_arr, :id, opts )
  filter_on( domains_arr, :state, opts )
end

#valid_credentials?(credentials) ⇒ Boolean

Returns:

  • (Boolean)


199
200
201
202
203
204
205
206
207
# File 'lib/deltacloud/drivers/rhevm/rhevm_driver.rb', line 199

def valid_credentials?(credentials)
  begin
    realms(credentials) && true
  rescue => e
    safely do
      raise e
    end
  end
end