forked from ManageIQ/manageiq-providers-google
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloud_manager.rb
339 lines (283 loc) · 11 KB
/
cloud_manager.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
class ManageIQ::Providers::Google::Inventory::Parser::CloudManager < ManageIQ::Providers::Google::Inventory::Parser
include ManageIQ::Providers::Google::RefreshHelperMethods
def initialize
super
@cloud_volume_url_to_source_image_id = {}
@cloud_volume_url_to_id = {}
end
def parse
log_header = "MIQ(#{self.class.name}.#{__method__}) Collecting data for EMS name: [#{collector.manager.name}] id: [#{collector.manager.id}]"
_log.info("#{log_header}...")
availability_zones
flavors
key_pairs
cloud_volumes
cloud_volume_snapshots do |persister_miq_template, snapshot|
image_os(persister_miq_template, snapshot)
end
images do |persister_miq_template, image|
image_os(persister_miq_template, image)
end
instances do |persister_vm, parent_image_uid, flavor, instance|
instance_os(persister_vm, parent_image_uid)
instance_hardware(persister_vm, flavor) do |persister_hardware|
hardware_disks(persister_hardware, instance)
end
instance_key_pairs(persister_vm, instance)
instance_advanced_settings(persister_vm, instance)
end
_log.info("#{log_header}...Complete")
end
private
def availability_zones
collector.availability_zones.each do |az|
persister.availability_zones.build(
:ems_ref => az.name,
:name => az.name
)
end
end
def flavors
collector.flavors.each do |flavor|
flavor(flavor)
end
end
def flavor_by_uid_and_zone_uid(flavor_uid, availability_zone_uid)
flavor = collector.flavor(flavor_uid, availability_zone_uid)
flavor(flavor)
end
def flavor(flavor)
persister.flavors.build(
:cpus => flavor.guest_cpus,
:description => flavor.description,
:ems_ref => flavor.name,
:enabled => !flavor.deprecated,
:memory => flavor.memory_mb * 1.megabyte,
:name => flavor.name
)
end
def cloud_volumes
collector.cloud_volumes.each do |cloud_volume|
zone_id = parse_uid_from_url(cloud_volume.zone)
persister.cloud_volumes.build(
:availability_zone => persister.availability_zones.lazy_find(zone_id),
:base_snapshot => persister.cloud_volume_snapshots.lazy_find(cloud_volume.source_snapshot),
:creation_time => cloud_volume.creation_timestamp,
:description => cloud_volume.description,
:ems_ref => cloud_volume.id.to_s,
:name => cloud_volume.name,
:size => cloud_volume.size_gb.to_i.gigabyte,
:status => cloud_volume.status,
:volume_type => parse_uid_from_url(cloud_volume.type)
)
# Take note of the source_image_id so we can expose it in parse_instance
@cloud_volume_url_to_source_image_id[cloud_volume.self_link] = cloud_volume.source_image_id
@cloud_volume_url_to_id[cloud_volume.self_link] = cloud_volume.id.to_s
end
end
def cloud_volume_snapshots
collector.cloud_volume_snapshots.each do |snapshot|
persister.cloud_volume_snapshots.build(
:creation_time => snapshot.creation_timestamp,
:description => snapshot.description,
:ems_ref => snapshot.id.to_s,
:name => snapshot.name,
:size => snapshot.disk_size_gb.to_i.gigabytes,
:status => snapshot.status,
:cloud_volume => persister.cloud_volumes.lazy_find(snapshot.source_disk)
)
persister_miq_template = image(snapshot)
yield persister_miq_template, snapshot
end
end
def images
collector.images.each do |image|
persister_miq_template = image(image)
yield(persister_miq_template, image)
end
end
def image(image)
uid = image.id.to_s
persister.miq_templates.build(
:deprecated => image.kind == "compute#image" ? !image.deprecated.nil? : false,
:ems_ref => uid,
:location => image.self_link,
:name => image.name || uid,
:publicly_available => true,
:raw_power_state => "never",
:template => true,
:uid_ems => uid,
:vendor => VENDOR_GOOGLE
)
end
def instances
collector.instances.each do |instance|
uid = instance.id.to_s
flavor_uid = parse_uid_from_url(instance.machine_type)
zone_uid = parse_uid_from_url(instance.zone)
# TODO(mslemr) lazy_find (now result needed immediately)
flavor = persister.flavors.find(flavor_uid)
# If the flavor isn't found in our index, check if it is a custom flavor
# that we have to get directly
flavor = flavor_by_uid_and_zone_uid(flavor_uid, zone_uid) if flavor.nil?
parent_image_uid = parse_instance_parent_image(instance)
persister_vm = persister.vms.build(
:availability_zone => persister.availability_zones.lazy_find(zone_uid),
:description => instance.description,
:ems_ref => uid,
:flavor => flavor,
:location => "unknown", # TODO(mslemr) instance.self_link?,
:name => instance.name || uid,
:genealogy_parent => persister.miq_templates.lazy_find(parent_image_uid),
:raw_power_state => instance.status,
:uid_ems => uid,
:vendor => VENDOR_GOOGLE,
)
yield persister_vm, parent_image_uid, flavor, instance
end
end
# Adding global key-pairs (needed when instances count == 0)
def key_pairs
project_key_pairs.each do |ssh_key|
persister.key_pairs.build(
:name => ssh_key[:name],
:fingerprint => ssh_key[:fingerprint]
)
end
end
# @param persister_vm [InventoryObject<ManageIQ::Providers::Google::CloudManager::Vm>]
# @param instance [Fog::Compute::Google::Server]
def instance_key_pairs(persister_vm, instance)
# Add project common ssh-keys with keys specific to this instance
instance_ssh_keys = project_key_pairs | parse_compute_metadata_ssh_keys(instance.metadata)
instance_ssh_keys.each do |ssh_key|
key_pair = persister.key_pairs.build(
:name => ssh_key[:name], # manager_ref
:fingerprint => ssh_key[:fingerprint] # manager_ref
)
key_pair.vms ||= []
key_pair.vms << persister_vm
end
end
# @param persister_vm [InventoryObject<ManageIQ::Providers::Google::CloudManager::Vm>]
# @param series [InventoryObject<ManageIQ::Providers::Google::CloudManager::Flavor>]
def instance_hardware(persister_vm, series)
persister_hardware = persister.hardwares.build(
:vm_or_template => persister_vm, # manager_ref
:cpu_total_cores => series[:cpus],
:memory_mb => series[:memory] / 1.megabyte
)
yield persister_hardware
end
# @param persister_vm [InventoryObject<ManageIQ::Providers::Google::CloudManager::Vm>]
# @param instance [Fog::Compute::Google::Server]
def instance_advanced_settings(persister_vm, instance)
persister.vms_and_templates_advanced_settings.build(
:resource => persister_vm, # manager_ref
:name => "preemptible?",
:display_name => N_("Is VM Preemptible"),
:description => N_("Whether or not the VM is 'preemptible'. See"\
" https://cloud.google.com/compute/docs/instances/preemptible for more details."),
:value => instance.scheduling[:preemptible].to_s,
:read_only => true
)
end
# @param persister_template [InventoryObject<ManageIQ::Providers::Google::CloudManager::Template]
# @param image [Fog::Compute::Google::Snapshot, Fog::Compute::Google::Image]
def image_os(persister_template, image)
persister.operating_systems.build(
:vm_or_template => persister_template, # manager_ref
:product_name => get_os_product_name(image)
)
end
# Operating system for Vm (i.e. instance)
# It's name is taken from parent image's OS
# Note: Not added when parent image not found
#
# @param persister_vm [InventoryObject<ManageIQ::Providers::Google::CloudManager::Vm>]
# @param parent_image_uid [String] UID of vm's template
def instance_os(persister_vm, parent_image_uid)
persister.operating_systems.build(
:vm_or_template => persister_vm,
:product_name => persister.operating_systems.lazy_find(
persister.miq_templates.lazy_find(parent_image_uid),
:key => :product_name
)
)
end
# @param persister_hardware [InventoryObject<Hardware>]
# @param instance [Fog::Compute::Google::Server]
def hardware_disks(persister_hardware, instance)
instance.disks.each do |attached_disk|
cloud_volume_ems_ref = @cloud_volume_url_to_id[attached_disk[:source]]
persister_cloud_volume = persister.cloud_volumes.find(cloud_volume_ems_ref)
next if persister_cloud_volume.nil?
persister.disks.build(
:backing => persister_cloud_volume,
:backing_type => 'CloudVolume',
:controller_type => VENDOR_GOOGLE,
:device_name => attached_disk[:device_name], # manager_ref
:device_type => "disk",
:hardware => persister_hardware, # manager_ref
:location => attached_disk[:index],
:size => persister_cloud_volume.size
)
end
end
# ---
def get_os_product_name(storage)
if storage.kind == 'compute#image'
OperatingSystem.normalize_os_name(storage.name)
else
'unknown'
end
end
# Get image's (miq_template's) ems_ref from
# instance disks (connected to cloud_volume's `self_link`)
def parse_instance_parent_image(instance)
parent_image_uid = nil
instance.disks.each do |disk|
parent_image_uid = @cloud_volume_url_to_source_image_id[disk[:source]]
next if parent_image_uid.nil?
break
end
parent_image_uid
end
# @param ssh_key [Hash]
def key_pairs_ems_ref(ssh_key)
"#{ssh_key[:name]}:#{ssh_key[:fingerprint]}"
end
# Ssh keys that are common to all instances in the project
def project_key_pairs
if @project_key_pairs.nil?
project_common_metadata = collector.project_instance_metadata
@project_key_pairs = parse_compute_metadata_ssh_keys(project_common_metadata)
end
@project_key_pairs
end
def parse_compute_metadata(metadata, key)
metadata_item = metadata[:items].to_a.detect { |x| x[:key] == key }
metadata_item.to_h[:value]
end
def parse_compute_metadata_ssh_keys(metadata)
require 'sshkey'
ssh_keys = []
# Find the sshKeys property in the instance metadata
metadata_ssh_keys = parse_compute_metadata(metadata, "sshKeys")
metadata_ssh_keys.to_s.split("\n").each do |ssh_key|
# Google returns the key in the form username:public_key
name, public_key = ssh_key.split(":", 2)
begin
fingerprint = SSHKey.sha1_fingerprint(public_key)
ssh_keys << {
:name => name,
:public_key => public_key,
:fingerprint => fingerprint
}
rescue => err
_log.warn("Failed to parse public key #{name}: #{err}")
end
end
ssh_keys
end
end