Class: Azure::ServiceManagement::ASMInterface

Inherits:
AzureInterface show all
Includes:
AzureAPI
Defined in:
lib/azure/service_management/ASM_interface.rb

Instance Attribute Summary collapse

Attributes inherited from AzureInterface

#ui

Instance Method Summary collapse

Methods included from Helpers

#display_list, #msg_pair, #random_string, #strip_non_ascii

Methods included from CustomErrors

included

Constructor Details

#initialize(params = {}) ⇒ ASMInterface

Returns a new instance of ASMInterface.



29
30
31
32
33
# File 'lib/azure/service_management/ASM_interface.rb', line 29

def initialize(params = {})
  @rest = Rest.new(params)
  @connection = Azure::ServiceManagement::Connection.new(@rest)
  super
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



27
28
29
# File 'lib/azure/service_management/ASM_interface.rb', line 27

def connection
  @connection
end

Instance Method Details

#add_extension(name, params = {}) ⇒ Object



298
299
300
301
302
303
304
305
306
# File 'lib/azure/service_management/ASM_interface.rb', line 298

def add_extension(name, params = {})
  ui.info "Started with Chef Extension deployment on the server #{name}..."
  connection.roles.update(name, params)
  ui.info "\nSuccessfully deployed Chef Extension on the server #{name}."
rescue Exception => e
  Chef::Log.error("Failed to add extension to the server -- exception being rescued: #{e}")
  backtrace_message = "#{e.class}: #{e}\n#{e.backtrace.join("\n")}"
  Chef::Log.debug("#{backtrace_message}")
end

#cleanup_and_exit(remove_hosted_service_on_failure, remove_storage_service_on_failure) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/azure/service_management/ASM_interface.rb', line 258

def cleanup_and_exit(remove_hosted_service_on_failure, remove_storage_service_on_failure)
  Chef::Log.warn("Cleaning up resources...")

  if remove_hosted_service_on_failure
    ret_val = connection.hosts.delete(remove_hosted_service_on_failure)
    ret_val.content.empty? ? Chef::Log.warn("Deleted created DNS: #{remove_hosted_service_on_failure}.") : Chef::Log.warn("Deletion failed for created DNS:#{remove_hosted_service_on_failure}. " + ret_val.text)
  end

  if remove_storage_service_on_failure
    ret_val = connection.storageaccounts.delete(remove_storage_service_on_failure)
    ret_val.content.empty? ? Chef::Log.warn("Deleted created Storage Account: #{remove_storage_service_on_failure}.") : Chef::Log.warn("Deletion failed for created Storage Account: #{remove_storage_service_on_failure}. " + ret_val.text)
  end
  exit 1
end

#create_affinity_group(params = {}) ⇒ Object



219
220
221
# File 'lib/azure/service_management/ASM_interface.rb', line 219

def create_affinity_group(params = {})
  connection.ags.create(params)
end

#create_internal_lb(params = {}) ⇒ Object



191
192
193
# File 'lib/azure/service_management/ASM_interface.rb', line 191

def create_internal_lb(params = {})
  connection.lbs.create(params)
end

#create_server(params = {}) ⇒ Object



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
# File 'lib/azure/service_management/ASM_interface.rb', line 223

def create_server(params = {})
  remove_hosted_service_on_failure = params[:azure_dns_name]
  if connection.hosts.exists?(params[:azure_dns_name])
    remove_hosted_service_on_failure = nil
  end

  #If Storage Account is not specified, check if the geographic location has one to re-use
  if not params[:azure_storage_account]
    storage_accts = connection.storageaccounts.all
    storage = storage_accts.find { |storage_acct| storage_acct.location.to_s == params[:azure_service_location] }
    unless storage
      params[:azure_storage_account] = [strip_non_ascii(params[:azure_vm_name]), random_string].join.downcase
      remove_storage_service_on_failure = params[:azure_storage_account]
    else
      remove_storage_service_on_failure = nil
      params[:azure_storage_account] = storage.name.to_s
    end
  else
    if connection.storageaccounts.exists?(params[:azure_storage_account])
      remove_storage_service_on_failure = nil
    else
      remove_storage_service_on_failure = params[:azure_storage_account]
    end
  end

  begin
    connection.deploys.create(params)
  rescue Exception => e
    Chef::Log.error("Failed to create the server -- exception being rescued: #{e}")
    backtrace_message = "#{e.class}: #{e}\n#{e.backtrace.join("\n")}"
    Chef::Log.debug("#{backtrace_message}")
    cleanup_and_exit(remove_hosted_service_on_failure, remove_storage_service_on_failure)
  end
end

#create_vnet(params = {}) ⇒ Object



205
206
207
# File 'lib/azure/service_management/ASM_interface.rb', line 205

def create_vnet(params = {})
  connection.vnets.create(params)
end

#delete_server(params = {}) ⇒ Object



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
# File 'lib/azure/service_management/ASM_interface.rb', line 84

def delete_server(params = {})
  server = find_server({ name: params[:name], azure_dns_name: params[:azure_dns_name] })

  unless server
    puts "\n"
    ui.error("Server #{params[:name]} does not exist")
    exit!
  end

  puts "\n"
  msg_pair(ui, "DNS Name", server.hostedservicename + ".cloudapp.net")
  msg_pair(ui, "VM Name", server.name)
  msg_pair(ui, "Size", server.size)
  msg_pair(ui, "Public Ip Address", server.publicipaddress)
  puts "\n"

  begin
    ui.confirm("Do you really want to delete this server")
  rescue SystemExit   # Need to handle this as confirming with N/n raises SystemExit exception
    server = nil      # Cleanup is implicitly performed in other cloud plugins
    exit!
  end

  params[:azure_dns_name] = server.hostedservicename

  connection.roles.delete(params)

  puts "\n"
  ui.warn("Deleted server #{server.name}")
end

#deployment(path) ⇒ Object



286
287
288
# File 'lib/azure/service_management/ASM_interface.rb', line 286

def deployment(path)
  connection.query_azure(path)
end

#deployment_name(dns_name) ⇒ Object



282
283
284
# File 'lib/azure/service_management/ASM_interface.rb', line 282

def deployment_name(dns_name)
  connection.deploys.get_deploy_name_for_hostedservice(dns_name)
end

#find_server(params = {}) ⇒ Object



80
81
82
# File 'lib/azure/service_management/ASM_interface.rb', line 80

def find_server(params = {})
  server = connection.roles.find(params[:name], params = { :azure_dns_name => params[:azure_dns_name] })
end

#get_extension(name, publisher) ⇒ Object



278
279
280
# File 'lib/azure/service_management/ASM_interface.rb', line 278

def get_extension(name, publisher)
  connection.query_azure("resourceextensions/#{publisher}/#{name}")
end

#get_role_server(dns_name, vm_name) ⇒ Object



273
274
275
276
# File 'lib/azure/service_management/ASM_interface.rb', line 273

def get_role_server(dns_name, vm_name)
   deploy = connection.deploys.queryDeploy(dns_name)
   deploy.find_role(vm_name)
end

#list_affinity_groupsObject



209
210
211
212
213
214
215
216
217
# File 'lib/azure/service_management/ASM_interface.rb', line 209

def list_affinity_groups
  affinity_groups = connection.ags.all
  cols = %w{Name Location Description}
  rows = []
  affinity_groups.each do |affinity_group|
    cols.each { |col| rows << affinity_group.send(col.downcase).to_s }
  end
  display_list(ui, cols, rows)
end

#list_imagesObject



35
36
37
# File 'lib/azure/service_management/ASM_interface.rb', line 35

def list_images
  connection.images.all
end

#list_internal_lbObject



181
182
183
184
185
186
187
188
189
# File 'lib/azure/service_management/ASM_interface.rb', line 181

def list_internal_lb
  lbs = connection.lbs.all
  cols = %w{Name Service Subnet VIP}
  rows = []
  lbs.each do |lb|
    cols.each { |col| rows << lb.send(col.downcase).to_s }
  end
  display_list(ui, cols, rows)
end

#list_serversObject



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
# File 'lib/azure/service_management/ASM_interface.rb', line 39

def list_servers
  servers = connection.roles.all
  cols = ["DNS Name", "VM Name", "Status", "IP Address", "SSH Port", "WinRM Port", "RDP Port"]
  rows = []
  servers.each do |server|
    rows << server.hostedservicename.to_s + ".cloudapp.net" # Info about the DNS name at http://msdn.microsoft.com/en-us/library/ee460806.aspx
    rows << server.name.to_s
    rows << begin
                     state = server.status.to_s.downcase
                     case state
                     when "shutting-down", "terminated", "stopping", "stopped"
                       ui.color(state, :red)
                     when "pending"
                       ui.color(state, :yellow)
                     else
                       ui.color("ready", :green)
                     end
                   end
    rows << server.publicipaddress.to_s
    rows << server.sshport.to_s
    rows << server.winrmport.to_s
    ports = server.tcpports
    rows << rdp_port(ports)
  end
  display_list(ui, cols, rows)
end

#list_vnetsObject



195
196
197
198
199
200
201
202
203
# File 'lib/azure/service_management/ASM_interface.rb', line 195

def list_vnets
  vnets = connection.vnets.all
  cols = ["Name", "Affinity Group", "State"]
  rows = []
  vnets.each do |vnet|
    %w{name affinity_group state}.each { |col| rows << vnet.send(col).to_s }
  end
  display_list(ui, cols, rows)
end

#rdp_port(arr_ports) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/azure/service_management/ASM_interface.rb', line 66

def rdp_port(arr_ports)
  if !arr_ports
    return ""
  end
  if arr_ports.length > 0
    arr_ports.each do |port|
      if port["Name"] == "Remote Desktop"
        return port["PublicPort"]
      end
    end
  end
  ""
end

#show_server(name) ⇒ Object



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
# File 'lib/azure/service_management/ASM_interface.rb', line 115

def show_server(name)
  role = connection.roles.find name

  puts ""
  if role
    details = Array.new
    details << ui.color("Role name", :bold, :cyan)
    details << role.name
    details << ui.color("Status", :bold, :cyan)
    details << role.status
    details << ui.color("Size", :bold, :cyan)
    details << role.size
    details << ui.color("Hosted service name", :bold, :cyan)
    details << role.hostedservicename
    details << ui.color("Deployment name", :bold, :cyan)
    details << role.deployname
    details << ui.color("Host name", :bold, :cyan)
    details << role.hostname
    unless role.sshport.nil?
      details << ui.color("SSH port", :bold, :cyan)
      details << role.sshport
    end
    unless role.winrmport.nil?
      details << ui.color("WinRM port", :bold, :cyan)
      details << role.winrmport
    end
    details << ui.color("Public IP", :bold, :cyan)
    details << role.publicipaddress.to_s

    unless role.thumbprint.empty?
      details << ui.color("Thumbprint", :bold, :cyan)
      details << role.thumbprint
    end
    puts ui.list(details, :columns_across, 2)
    if role.tcpports.length > 0 || role.udpports.length > 0
      details.clear
      details << ui.color("Ports open", :bold, :cyan)
      details << ui.color("Local port", :bold, :cyan)
      details << ui.color("IP", :bold, :cyan)
      details << ui.color("Public port", :bold, :cyan)
      if role.tcpports.length > 0
        role.tcpports.each do |port|
          details << "tcp"
          details << port["LocalPort"]
          details << port["Vip"]
          details << port["PublicPort"]
        end
      end
      if role.udpports.length > 0
        role.udpports.each do |port|
          details << "udp"
          details << port["LocalPort"]
          details << port["Vip"]
          details << port["PublicPort"]
        end
      end
      puts ui.list(details, :columns_across, 4)
    end
  else
    puts "No VM found"
  end

rescue => error
  puts "#{error.class} and #{error.message}"
end

#valid_image?(name) ⇒ Boolean

Returns:

  • (Boolean)


290
291
292
# File 'lib/azure/service_management/ASM_interface.rb', line 290

def valid_image?(name)
  connection.images.exists?(name)
end

#vm_image?(name) ⇒ Boolean

Returns:

  • (Boolean)


294
295
296
# File 'lib/azure/service_management/ASM_interface.rb', line 294

def vm_image?(name)
  connection.images.is_vm_image(name)
end