Class: ForemanCPPCloudstack::Cloudstack

Inherits:
ComputeResource
  • Object
show all
Defined in:
app/models/foreman_cpp_cloudstack/cloudstack.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Cloudstack

add additional params to the ‘new’ method



24
25
26
27
28
29
30
# File 'app/models/foreman_cpp_cloudstack/cloudstack.rb', line 24

def initialize(params)
    super
    if params
        attrs[:zone_id] = params[:zone]
        attrs[:hypervisor] = params[:hypervisor]
    end
end

Instance Attribute Details

#hypervisorObject

Returns the value of attribute hypervisor.



18
19
20
# File 'app/models/foreman_cpp_cloudstack/cloudstack.rb', line 18

def hypervisor
  @hypervisor
end

#zoneObject

Returns the value of attribute zone.



18
19
20
# File 'app/models/foreman_cpp_cloudstack/cloudstack.rb', line 18

def zone
  @zone
end

Class Method Details

.model_nameObject



125
126
127
# File 'app/models/foreman_cpp_cloudstack/cloudstack.rb', line 125

def self.model_name
    ComputeResource.model_name
end

Instance Method Details

#associated_host(vm) ⇒ Object



199
200
201
# File 'app/models/foreman_cpp_cloudstack/cloudstack.rb', line 199

def associated_host(vm)
    Host.authorized(:view_hosts, Host).where(:ip => [vm.nics[0]["ipaddress"], vm.floating_ip_address, vm.private_ip_address]).first
end

#available_imagesObject



156
157
158
# File 'app/models/foreman_cpp_cloudstack/cloudstack.rb', line 156

def available_images
    client.images
end

#capabilitiesObject



133
134
135
# File 'app/models/foreman_cpp_cloudstack/cloudstack.rb', line 133

def capabilities
    [:image]
end

#console(uuid) ⇒ Object



194
195
196
197
# File 'app/models/foreman_cpp_cloudstack/cloudstack.rb', line 194

def console(uuid)
    vm = find_vm_by_uuid(uuid)
    vm.console.body.merge({'timestamp' => Time.now.utc})
end

#create_vm(args = {}) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'app/models/foreman_cpp_cloudstack/cloudstack.rb', line 160

def create_vm(args = {})
    args[:security_group_ids] = nil
    args[:network_ids] = [args[:network_ids]] if args[:network_ids]
    args[:network_ids] = [args[:subnet_id]] if args[:subnet_id]
    args[:network_ids] = nil

    args[:zone_id] = get_zone

    args[:display_name] = args[:name]
    # name has to be hostname without domain: no dots allowed
    name = args[:name].split(/\.(?=[\w])/).first || args[:name]
    args[:name] = name

    options = vm_instance_defaults.merge(args.to_hash.symbolize_keys)
    vm = client.servers.create options
    vm.wait_for { nics.present? }
    logger.warn "captured ipaddress"
    logger.warn vm.nics[0]["ipaddress"]
    logger.warn vm.inspect
    vm
rescue => e
    message = JSON.parse(e.response.body)['badRequest']['message'] rescue (e.to_s)
    logger.warn "failed to create vm: #{message}"
    destroy_vm vm.id if vm
    raise message
end

#destroy_vm(uuid) ⇒ Object



187
188
189
190
191
192
# File 'app/models/foreman_cpp_cloudstack/cloudstack.rb', line 187

def destroy_vm uuid
    find_vm_by_uuid(uuid).destroy
rescue ActiveRecord::RecordNotFound
    # if the VM does not exists, we don't really care.
    true
end

#domainObject



97
98
99
# File 'app/models/foreman_cpp_cloudstack/cloudstack.rb', line 97

def domain
    attrs[:domain]
end

#domainsObject



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/models/foreman_cpp_cloudstack/cloudstack.rb', line 51

def domains
    return [] if url.blank? or user.blank? or password.blank?
    domainsobj = client.list_domains

    domains_array = []
    zonesobj["listdomainsresponse"]["domain"].each do |domain|
        z =  domain["name"]
        domains_array.push(z)
    end
    logger.info(domainsobj)
    logger.info(domains_array)
    return domains_array
end

#flavor_name(flavor_ref) ⇒ Object



208
209
210
# File 'app/models/foreman_cpp_cloudstack/cloudstack.rb', line 208

def flavor_name(flavor_ref)
    client.flavors.get(flavor_ref).try(:name)
end

#get_hypervisorObject



117
118
119
# File 'app/models/foreman_cpp_cloudstack/cloudstack.rb', line 117

def get_hypervisor
    self.attrs[:hypervisor]
end

#get_zoneObject



82
83
84
# File 'app/models/foreman_cpp_cloudstack/cloudstack.rb', line 82

def get_zone
    self.attrs[:zone_id]
end

#hypervisorsObject



86
87
88
89
90
91
92
93
94
95
# File 'app/models/foreman_cpp_cloudstack/cloudstack.rb', line 86

def hypervisors
    return [] if url.blank? or user.blank? or password.blank?
    hypervisorsobj = client.list_hypervisors

    hypervisors_array = []
    hypervisorsobj["listhypervisorsresponse"]["hypervisor"].each do |hypervisor|
        hypervisors_array.push(hypervisor["name"])
    end
    return hypervisors_array
end

#image_param_nameObject



129
130
131
# File 'app/models/foreman_cpp_cloudstack/cloudstack.rb', line 129

def image_param_name
    :image_ref
end

#ip_address(uuid) ⇒ Object



203
204
205
206
# File 'app/models/foreman_cpp_cloudstack/cloudstack.rb', line 203

def ip_address uuid
    vm = find_vm_by_uuid(uuid)
    vm.nics[0]["ipaddress"]
end

#networksObject



137
138
139
140
141
142
143
144
145
146
147
# File 'app/models/foreman_cpp_cloudstack/cloudstack.rb', line 137

def networks
    fog_ntwrks = []
    networks_array = client.list_networks["listnetworksresponse"]["network"]
    networks_array.each do |network|
        ntwrk = Fog::Compute::Cloudstack::Address.new
        ntwrk.id = network["id"]
        ntwrk.network_id = network["name"]
        fog_ntwrks.push(ntwrk)
    end
    return fog_ntwrks
end

#provided_attributesObject



121
122
123
# File 'app/models/foreman_cpp_cloudstack/cloudstack.rb', line 121

def provided_attributes
    super.merge({ :ip => :ip_address, :mac => :mac_address })
end

#provider_friendly_nameObject



212
213
214
# File 'app/models/foreman_cpp_cloudstack/cloudstack.rb', line 212

def provider_friendly_name
    "Cloudstack"
end

#set_hypervisor=(hypervisor) ⇒ Object



113
114
115
# File 'app/models/foreman_cpp_cloudstack/cloudstack.rb', line 113

def set_hypervisor=(hypervisor)
    self.attrs[:hypervisor] = hypervisor
end

#set_zone=(zone_id) ⇒ Object

save the zone



78
79
80
# File 'app/models/foreman_cpp_cloudstack/cloudstack.rb', line 78

def set_zone=(zone_id)
    self.attrs[:zone_id] = zone_id
end

#templates_isosObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/foreman_cpp_cloudstack/cloudstack.rb', line 32

def templates_isos
    list = Array.new
    templates = client.list_templates("self")["listtemplatesresponse"]["template"]
    if templates.any?
        list << {:id => "-1", :name => "--- Templates ---"}
        templates.each do |template|
            list << {:id => template["id"], :name => template["name"]+" ("+template["zonename"]+")"}
        end
    end
    isos = client.list_isos["listisosresponse"]["iso"]
    if isos.any?
        list << {:id => "-1", :name => "--- ISOs ---"}
        isos.each do |iso|
            list << {:id => iso["id"], :name => iso["name"]+" ("+iso["zonename"]+")"}
        end
    end
    list
end

#test_connection(options = {}) ⇒ Object



149
150
151
152
153
154
# File 'app/models/foreman_cpp_cloudstack/cloudstack.rb', line 149

def test_connection options = {}
    client
    errors[:url].empty? and errors[:user].empty? and errors[:password].empty? and zones
rescue Fog::Compute::Cloudstack::Error => e
    errors[:base] << e.message
end

#zone_idObject



105
106
107
# File 'app/models/foreman_cpp_cloudstack/cloudstack.rb', line 105

def zone_id
    return client.list_zones["listzonesresponse"]["zone"][0]["id"]
end

#zonesObject



65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/foreman_cpp_cloudstack/cloudstack.rb', line 65

def zones
    return [] if url.blank? or user.blank? or password.blank?
    zonesobj = client.list_zones

    zones_array = []
    zonesobj["listzonesresponse"]["zone"].each do |zone|
        z =  {:name => zone["name"], :id => zone["id"]}
        zones_array.push(z)
    end
    return zones_array
end