-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
server.rb
276 lines (241 loc) · 11.6 KB
/
server.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
module Fog
module Compute
class AzureRM
# This class is giving implementation of create/save and
# delete/destroy for Virtual Machine.
class Server < Fog::Model
attribute :id
identity :name
attribute :location
attribute :resource_group
attribute :vm_size
attribute :storage_account_name
attribute :os_disk_name
attribute :os_disk_id
attribute :os_disk_vhd_uri
attribute :os_disk_caching
attribute :publisher
attribute :offer
attribute :sku
attribute :version
attribute :username
attribute :password
attribute :data_disks
attribute :disable_password_authentication
attribute :ssh_key_path
attribute :ssh_key_data
attribute :platform
attribute :provision_vm_agent
attribute :enable_automatic_updates
attribute :network_interface_card_ids
attribute :availability_set_id
attribute :custom_data
attribute :vhd_path
attribute :managed_disk_storage_type
attribute :os_disk_size
attribute :tags
attribute :platform_update_domain
attribute :platform_fault_domain
attribute :image_ref
def self.parse(vm)
hash = {}
hash['id'] = vm.id
hash['name'] = vm.name
hash['location'] = vm.location
hash['resource_group'] = get_resource_group_from_id(vm.id)
hash['vm_size'] = vm.hardware_profile.vm_size unless vm.hardware_profile.vm_size.nil?
unless vm.storage_profile.nil?
hash['os_disk_name'] = vm.storage_profile.os_disk.name
subscription_id = get_subscription_id(vm.id)
hash['os_disk_id'] = "/subscriptions/#{subscription_id}/resourceGroups/#{hash['resource_group']}/providers/Microsoft.Compute/disks/#{hash['os_disk_name']}"
hash['os_disk_size'] = vm.storage_profile.os_disk.disk_size_gb
if vm.storage_profile.os_disk.vhd.nil?
hash['managed_disk_storage_type'] = vm.storage_profile.os_disk.managed_disk.storage_account_type
else
hash['os_disk_vhd_uri'] = vm.storage_profile.os_disk.vhd.uri
hash['storage_account_name'] = hash['os_disk_vhd_uri'].split('/')[2].split('.')[0]
end
hash['os_disk_caching'] = vm.storage_profile.os_disk.caching
unless vm.storage_profile.image_reference.nil?
hash['publisher'] = vm.storage_profile.image_reference.publisher
hash['offer'] = vm.storage_profile.image_reference.offer
hash['sku'] = vm.storage_profile.image_reference.sku
hash['version'] = vm.storage_profile.image_reference.version
end
end
hash['username'] = vm.os_profile.admin_username
hash['custom_data'] = vm.os_profile.custom_data
hash['data_disks'] = []
unless vm.storage_profile.data_disks.nil?
vm.storage_profile.data_disks.each do |disk|
data_disk = Fog::Compute::AzureRM::DataDisk.new
hash['data_disks'] << data_disk.merge_attributes(Fog::Compute::AzureRM::DataDisk.parse(disk))
end
end
hash['disable_password_authentication'] = false
hash['disable_password_authentication'] = vm.os_profile.linux_configuration.disable_password_authentication unless vm.os_profile.linux_configuration.nil?
if vm.os_profile.windows_configuration
hash['provision_vm_agent'] = vm.os_profile.windows_configuration.provision_vmagent
hash['enable_automatic_updates'] = vm.os_profile.windows_configuration.enable_automatic_updates
end
hash['network_interface_card_ids'] = vm.network_profile.network_interfaces.map(&:id)
hash['availability_set_id'] = vm.availability_set.id unless vm.availability_set.nil?
hash['tags'] = vm.tags
unless vm.instance_view.nil?
hash['platform_update_domain'] = vm.instance_view.platform_update_domain
hash['platform_fault_domain'] = vm.instance_view.platform_fault_domain
end
hash
end
def save(async = false)
requires :name, :location, :resource_group, :vm_size, :username, :network_interface_card_ids
requires :publisher, :offer, :sku, :version if vhd_path.nil? && image_ref.nil?
requires :storage_account_name if managed_disk_storage_type.nil?
requires :managed_disk_storage_type if storage_account_name.nil?
if platform_is_linux?(platform)
requires :disable_password_authentication
else
requires :password
end
ssh_key_path = "/home/#{username}/.ssh/authorized_keys" unless ssh_key_data.nil?
if async
service.create_virtual_machine(virtual_machine_params(ssh_key_path), true)
else
vm = service.create_virtual_machine(virtual_machine_params(ssh_key_path))
vm = service.get_virtual_machine(resource_group, name, false)
merge_attributes(Fog::Compute::AzureRM::Server.parse(vm))
end
end
def destroy(async = false)
response = service.delete_virtual_machine(resource_group, name, async)
async ? create_fog_async_response(response) : response
end
def generalize(async = false)
response = service.generalize_virtual_machine(resource_group, name, async)
async ? create_fog_async_response(response) : response
end
def power_off(async = false)
response = service.power_off_virtual_machine(resource_group, name, async)
async ? create_fog_async_response(response) : response
end
def start(async = false)
response = service.start_virtual_machine(resource_group, name, async)
async ? create_fog_async_response(response) : response
end
def restart(async = false)
response = service.restart_virtual_machine(resource_group, name, async)
async ? create_fog_async_response(response) : response
end
def deallocate(async = false)
response = service.deallocate_virtual_machine(resource_group, name, async)
async ? create_fog_async_response(response) : response
end
def redeploy(async = false)
response = service.redeploy_virtual_machine(resource_group, name, async)
async ? create_fog_async_response(response) : response
end
def list_available_sizes(async = false)
response = service.list_available_sizes_for_virtual_machine(resource_group, name, async)
async ? create_fog_async_response(response) : response
end
def vm_status(async = false)
service.check_vm_status(resource_group, name, async)
end
def attach_data_disk(disk_name, disk_size, storage_account_name, async = false)
response = service.attach_data_disk_to_vm(data_disk_params(disk_name, disk_size, storage_account_name), async)
async ? create_fog_async_response(response) : merge_attributes(Fog::Compute::AzureRM::Server.parse(response))
end
def detach_data_disk(disk_name, async = false)
response = service.detach_data_disk_from_vm(resource_group, name, disk_name, async)
async ? create_fog_async_response(response) : merge_attributes(Fog::Compute::AzureRM::Server.parse(response))
end
def attach_managed_disk(disk_name, disk_resource_group, async = false, caching = 'None')
response = service.attach_data_disk_to_vm(data_disk_params(disk_name, nil, nil, disk_resource_group, caching), async)
async ? create_fog_async_response(response) : merge_attributes(Fog::Compute::AzureRM::Server.parse(response))
end
def detach_managed_disk(disk_name, async = false)
response = service.detach_data_disk_from_vm(resource_group, name, disk_name, async)
async ? create_fog_async_response(response) : merge_attributes(Fog::Compute::AzureRM::Server.parse(response))
end
def delete_extra_resources
unless vhd_path.nil? || !managed_disk_storage_type.nil?
service.delete_image(resource_group, name)
delete_storage_account_or_container(resource_group, storage_account_name, name)
end
end
def update_attributes
vm = service.get_virtual_machine(resource_group, name, false)
merge_attributes(Fog::Compute::AzureRM::Server.parse(vm))
end
private
def platform_is_linux?(platform)
platform.strip.casecmp(PLATFORM_LINUX).zero?
end
def create_fog_async_response(response, delete_extra_resource = false)
server = Fog::Compute::AzureRM::Server.new(service: service)
Fog::AzureRM::AsyncResponse.new(server, response, delete_extra_resource)
end
def virtual_machine_params(ssh_key_path)
{
resource_group: resource_group,
name: name,
location: location,
vm_size: vm_size,
storage_account_name: storage_account_name,
username: username,
password: password,
disable_password_authentication: disable_password_authentication,
ssh_key_path: ssh_key_path,
ssh_key_data: ssh_key_data,
network_interface_card_ids: network_interface_card_ids,
availability_set_id: availability_set_id,
publisher: publisher,
offer: offer,
sku: sku,
version: version,
platform: platform,
provision_vm_agent: provision_vm_agent,
enable_automatic_updates: enable_automatic_updates,
custom_data: custom_data,
vhd_path: vhd_path,
os_disk_caching: os_disk_caching,
managed_disk_storage_type: managed_disk_storage_type,
os_disk_size: os_disk_size,
tags: tags,
image_ref: image_ref,
os_disk_name: os_disk_name,
os_disk_vhd_uri: os_disk_vhd_uri
}
end
def data_disk_params(disk_name, disk_size = nil, storage_account = nil, disk_resource_group = nil, caching = nil)
{
vm_name: name,
vm_resource_group: resource_group,
disk_name: disk_name,
disk_size_gb: disk_size,
storage_account_name: storage_account,
disk_resource_group: disk_resource_group,
caching: caching
}
end
def delete_storage_account_or_container(resource_group, storage_account_name, vm_name)
if storage_account_name.nil?
delete_storage_account(resource_group)
else
delete_storage_container(resource_group, storage_account_name, vm_name)
end
end
def delete_storage_container(resource_group, storage_account_name, vm_name)
@storage_service = service.instance_variable_get(:@storage_service)
access_key = @storage_service.get_storage_access_keys(resource_group, storage_account_name).first.value
container_name = "customvhd-#{vm_name.downcase}-os-image"
@storage_service.directories.delete_temporary_container(storage_account_name, access_key, container_name)
end
def delete_storage_account(resource_group)
@storage_service = service.instance_variable_get(:@storage_service)
@storage_service.storage_accounts.delete_storage_account_from_tag(resource_group, TEMPORARY_STORAGE_ACCOUNT_TAG_KEY, TEMPORARY_STORAGE_ACCOUNT_TAG_VALUE)
end
end
end
end
end