Class: Azure::CloudServiceManagement::CloudServiceManagementService

Inherits:
BaseManagementService
  • Object
show all
Defined in:
lib/azure/cloud_service_management/cloud_service_management_service.rb

Instance Method Summary collapse

Constructor Details

#initializeCloudServiceManagementService



20
21
22
# File 'lib/azure/cloud_service_management/cloud_service_management_service.rb', line 20

def initialize
  super()
end

Instance Method Details

#create_cloud_service(name, options = {}) ⇒ Object

Public: Creates a new cloud service in Windows Azure.

Attributes

  • name - String. The name of the cloud service.

  • options - Hash. Optional parameters.

Options

Accepted key/value pairs in options parameter are:

  • :label -String. The label for this cloud service.

  • :description - String. A description for the hosted service. (optional)

  • :location - String. The regional data center location where the

cloud service will be created. Required if affinity group not specified (optional)

  • +:affinity_group_name - String. Name of the affinity group with

which to assocate the cloud service. Required if location not specified (optional)

  • :extended_properties - Hash. Key/Value pairs of extended

properties to add to the cloud service. The key is used as the property name and the value as its value. (optional)

See msdn.microsoft.com/en-us/library/windowsazure/gg441304.aspx

Returns None



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/azure/cloud_service_management/cloud_service_management_service.rb', line 49

def create_cloud_service(name, options = {})
  Loggerx.error_with_exit 'Cloud service name is not valid ' unless name
  if get_cloud_service(name)
    Loggerx.warn "Cloud service #{name} already exists. Skipped..."
  else
    Loggerx.info "Creating cloud service #{name}."
    request_path = '/services/hostedservices'
    body = Serialization.cloud_services_to_xml(name, options)
    request = ManagementHttpRequest.new(:post, request_path, body)
    request.call
  end
end

#create_deployment(deployment_name, cloud_service_name, package_url, service_configuration, options = {}) ⇒ Object

Public: Deploy a .cspkg hosted at a specific package_url to a a Cloud Service on a specific slot

Attributes

  • deployment_name - String. Name of the deployment

  • cloud_service_name - String. Name of the Cloud Service where the deployment

    needs to be created
    
  • package_url - String. URL of the blob storage where the .cspkg is being

    stored
    
  • package_url - String. URL of the blob storage where the .cspkg is being

    stored
    
  • service_configuration - Base64 encoded String. ServiceConfiguration.cscfg file

  • options - Hash. Optional parameters.

Options

Accepted key/value pairs in options parameter are:

  • :slot - String. Deployment slot. Valid values are either

    'production'(default) or 'staging'.
    
  • :label - String. The label for this cloud service.

  • :start_deployment - String. A description for the hosted service. (optional)

  • :treat_warnings_as_error - String.

  • :extended_properties - Hash. Key/Value pairs of extended properties to add to

    the cloud service. The key is used as the property name 
    and the value as its value.
    
  • fire_and_forget - Boolean(efault is false). If true, the client

    does not wait until the request is completed.
    
  • :upgrade_if_exists - Boolean(default is false). If true, then if a deployment

    already exists, then it is upgraded. Otherwise, an exception 
    is thrown.
    

More details at msdn.microsoft.com/en-us/library/azure/ee460813.aspx

Returns None



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
# File 'lib/azure/cloud_service_management/cloud_service_management_service.rb', line 96

def create_deployment(deployment_name, cloud_service_name, package_url, service_configuration, options = {})
  Loggerx.error_with_exit 'Cloud service name is not valid' unless cloud_service_name
  Loggerx.error_with_exit 'Deployment name is not valid' unless deployment_name
  Loggerx.error_with_exit 'Package url is not valid' unless package_url
  Loggerx.error_with_exit 'ServiceConfiguration.cscfg is not valid' unless service_configuration
  upgrade_if_exists = options[:upgrade_if_exists].nil? ? false : options[:upgrade_if_exists]

  slot = "production"
  unless options.nil? || options[:slot].nil?
    valid_slot = options[:slot].casecmp("staging") || options[:slot].casecmp("production")
    Loggerx.error_with_exit 'Deployment slot is not valid' unless valid_slot
    slot = options[:slot].downcase
  end

  # 2. Get the current deployment so one can verify that it can be upgraded

  deployment = get_deployment(cloud_service_name, {:slot => slot, :no_exit_on_failure => true})

  # 3. Create or upgrade the deployment

  if deployment.exists?
    if upgrade_if_exists
      upgrade_deployment(cloud_service_name, package_url, service_configuration, options)
    else
      Loggerx.error_with_exit "#{slot.capitalize} deployment '#{deployment_name}' on cloud service #{cloud_service_name} already exist."
    end
  else
    slot = "production"
    unless options.nil? || options[:slot].nil?
      valid_slot = options[:slot].casecmp("staging") || options[:slot].casecmp("production")
      Loggerx.error_with_exit 'Deployment slot is not valid' unless valid_slot
      slot = options[:slot].downcase
    end
    Loggerx.info "Creating deployment #{deployment_name}."
    request_path = "/services/hostedservices/#{cloud_service_name}/deploymentslots/#{slot}"
    body = Serialization.create_deployment_to_xml(deployment_name, package_url, service_configuration, options)
    request = ManagementHttpRequest.new(:post, request_path, body)
    request.call(options)
  end
end

#delete_cloud_service(cloud_service_name) ⇒ Object

Public: Deletes the specified cloud service of given subscription id from Windows Azure.

Attributes

  • name - String. Cloud service name.

Returns: None



325
326
327
328
329
330
# File 'lib/azure/cloud_service_management/cloud_service_management_service.rb', line 325

def delete_cloud_service(cloud_service_name)
  request_path = "/services/hostedservices/#{cloud_service_name}"
  request = ManagementHttpRequest.new(:delete, request_path)
  Loggerx.info "Deleting cloud service #{cloud_service_name}. \n"
  request.call
end

#delete_cloud_service_deployment(cloud_service_name, slot = 'production') ⇒ Object

Public: Deletes the specified deployment.

Attributes

  • cloud_service_name - String. Cloud service name.

  • slot - String. Either ‘production’ or ‘staging’. Optional parameters.

    Default if not specified is 'production'
    

See msdn.microsoft.com/en-us/library/windowsazure/ee460815.aspx

Returns NONE



343
344
345
346
347
348
349
# File 'lib/azure/cloud_service_management/cloud_service_management_service.rb', line 343

def delete_cloud_service_deployment(cloud_service_name, slot='production')
  slot = "production" unless slot
  request_path = "/services/hostedservices/#{cloud_service_name}/deploymentslots/#{slot}"
  request = ManagementHttpRequest.new(:delete, request_path)
  Loggerx.info "Deleting deployment of cloud service \"#{cloud_service_name}\" ..."
  request.call
end

#get_cloud_service(name) ⇒ Object

Public: Checks to see if the specified hosted service is available

Attributes

  • name - String. Cloud service name.

Returns: A boolean value indicating whether the cloud service exists. If true, the cloud service is available. If false, the cloud service does not exist.



299
300
301
302
303
304
305
306
307
308
309
# File 'lib/azure/cloud_service_management/cloud_service_management_service.rb', line 299

def get_cloud_service(name)
  return false if name.nil?
  flag = false
  list_cloud_services.each do |cloud_service|
    if cloud_service.name == name
      flag = true
      break
    end
  end
  flag
end

#get_cloud_service_properties(name) ⇒ Object



311
312
313
314
315
316
# File 'lib/azure/cloud_service_management/cloud_service_management_service.rb', line 311

def get_cloud_service_properties(name)
  request_path = "/services/hostedservices/#{name}?embed-detail=true"
  request = ManagementHttpRequest.new(:get, request_path)
  response = request.call
  Serialization.cloud_services_from_xml(response).first
end

#get_deployment(cloud_service_name, options = {}) ⇒ Object

Public: Gets a specific deployment of a Cloud Service based on either its name, or its slot

Attributes

  • cloud_service_name - String. Name of the Cloud Service where the deployment lives

  • options - Hash. Optional parameters.

Options

Accepted key/value pairs in options parameter are:

  • :slot - String. Deployment slot. Valid values are either ‘production’(default)

    or 'staging'. If not defined, and 'name' is also not defined, then 
    default slot is set to 'production'
    
  • :id - String. Deployment’s id(aka deployment’s name, or private id).

  • :no_exit_on_failure - Boolean(optional). Default is false.

More details at msdn.microsoft.com/en-us/library/azure/ee460804.aspx



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/azure/cloud_service_management/cloud_service_management_service.rb', line 262

def get_deployment(cloud_service_name, options={})
  Loggerx.error_with_exit 'Cloud service name is not valid' unless cloud_service_name
  request_path = nil
  slot = (options[:slot].nil? && options[:id].nil?) ? "production" : options[:slot]
  if !slot.nil?
    invalid_slot = slot.casecmp("staging") != 0 && slot.casecmp("production") != 0
    Loggerx.error_with_exit 'slot is not valid' if invalid_slot
    request_path = "/services/hostedservices/#{cloud_service_name}/deploymentslots/#{slot}"
  else
    request_path = "/services/hostedservices/#{cloud_service_name}/deployments/#{options[:id]}"
  end

  request = ManagementHttpRequest.new(:get, request_path, nil)
  response = request.call(options)
  #puts response

  Serialization.deployment_from_xml(response)
end

#list_cloud_servicesObject

Public: Gets a list of hosted services available under the current subscription.

Returns an array of Azure::CloudServiceManagement::CloudService objects



283
284
285
286
287
288
# File 'lib/azure/cloud_service_management/cloud_service_management_service.rb', line 283

def list_cloud_services
  request_path = '/services/hostedservices'
  request = ManagementHttpRequest.new(:get, request_path, nil)
  response = request.call
  Serialization.cloud_services_from_xml(response)
end

#swap_deployment(cloud_service_name, options = {}) ⇒ Object

Public: Swap the staging and production deployment of a specific cloud service

Attributes

  • cloud_service_name - String. Name of the Cloud Service where the deployments

    are going to be swapped
    
  • options - Hash. Optional parameters.

Options

Accepted key/value pairs in options parameter are:

  • fire_and_forget - Boolean(efault is false). If true, the client

    does not wait until the request is completed.
    

More details at msdn.microsoft.com/en-us/library/azure/ee460814.aspx

Returns None



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/azure/cloud_service_management/cloud_service_management_service.rb', line 228

def swap_deployment(cloud_service_name, options={})
  Loggerx.error_with_exit 'Cloud service name is not valid' unless cloud_service_name

  staging_deployment = get_deployment(cloud_service_name, {:slot => "staging", :no_exit_on_failure => true})
  Loggerx.error_with_exit "Staging deployment on cloud service #{cloud_service_name} is transitioning. Wait until transitioning is over before swapping." if (staging_deployment.exists? && staging_deployment.is_transitioning?)
  production_deployment = get_deployment(cloud_service_name, {:slot => "production", :no_exit_on_failure => true})
  Loggerx.error_with_exit "Production deployment on cloud service #{cloud_service_name} is transitioning. Wait until transitioning is over before swapping." if (production_deployment.exists? && production_deployment.is_transitioning?)
  Loggerx.error_with_exit "There are no deployments on cloud service #{cloud_service_name}." unless (staging_deployment.exists? || production_deployment.exists?)

  Loggerx.info "Swapping deployments on cloud service #{cloud_service_name} starting"
  request_path = "/services/hostedservices/#{cloud_service_name}"
  body = Serialization.swap_deployments_to_xml(production_deployment.name, staging_deployment.name)
  request = ManagementHttpRequest.new(:post, request_path, body)
  request.call(options)
end

#upgrade_deployment(cloud_service_name, package_url, service_configuration, options = {}) ⇒ Object

Public: Upgrade an existing deployment with a new .cspkg hosted at a specific package_url.

You can choose to upgrade all roles within the deployment(default), or only select 
specific ones

Attributes

  • cloud_service_name - String. Name of the Cloud Service where the deployment

    needs to be created
    
  • package_url - String. URL of the blob storage where the .cspkg is being

    stored
    
  • service_configuration - Base64 encoded String. ServiceConfiguration.cscfg file

  • options - Hash. Optional parameters.

Options

Accepted key/value pairs in options parameter are:

  • :slot - String. Deployment slot. Valid values are either

    'production'(default) or 'staging'.
    
  • :name - String. Deployment’s name.

  • :mode - String. Specifies the type of update to initiate.Valid values

    are either 'Auto'(default), 'Manual', or 'Simultaneous'
    
  • :label - String. The label for this cloud service.

  • :role_to_upgrade - String. Specifies the name of the specific role instance to update.

  • :force - Boolean(default false). Indicates whether the rollback should

    proceed even when it will cause local data to be lost from some 
    role instances
    
  • :extended_properties - Hash. Key/Value pairs of extended properties to add to

    the cloud service. The key is used as the property name 
    and the value as its value.
    
  • fire_and_forget - Boolean(efault is false). If true, the client

    does not wait until the request is completed.
    

More details at msdn.microsoft.com/en-us/library/azure/ee460793.aspx

Returns None



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
# File 'lib/azure/cloud_service_management/cloud_service_management_service.rb', line 170

def upgrade_deployment(cloud_service_name, package_url, service_configuration, options = {})
  Loggerx.error_with_exit 'Cloud service name is not valid' unless cloud_service_name
  Loggerx.error_with_exit 'Package url is not valid' unless package_url
  Loggerx.error_with_exit 'ServiceConfiguration.cscfg is not valid' unless service_configuration

  # 1. Identify which type of query needs to be processes

  request_path = nil
  info_msg = nil
  error_msg_body = "nil"
  slot = (options[:slot].nil? && options[:name].nil?) ? "production" : options[:slot]
  if !slot.nil?
    invalid_slot = slot.casecmp("staging") != 0 && slot.casecmp("production") != 0
    Loggerx.error_with_exit 'slot is not valid' if invalid_slot
    request_path = "/services/hostedservices/#{cloud_service_name}/deploymentslots/#{slot}/?comp=upgrade"
    info_msg = "Upgrading #{slot} deployment on #{cloud_service_name} cloud service."
    error_msg_body = "#{slot.capitalize} deployment on cloud service #{cloud_service_name}"
  else
    request_path = "/services/hostedservices/#{cloud_service_name}/deployments/#{options[:name]}/?comp=upgrade"
    info_msg = "Upgrading deployment #{options[:name]} on #{cloud_service_name} cloud service."
    error_msg_body = "Deployment #{options[:name]} on cloud service #{cloud_service_name}"
  end
  options[:label] = "#{slot} deployment #{cloud_service_name}" if options[:label].nil?
  
  # 2. Get the current deployment so one can verify that it can be upgraded

  options[:no_exit_on_failure] = true
  deployment = get_deployment(cloud_service_name, options)

  # 3. Upgrade the deployment if it is in a state where it can be upgraded

  if !deployment.exists?
    Loggerx.error_with_exit "#{error_msg_body} does not exist, and therefore cannot be upgraded."
  elsif deployment.is_transitioning?
    Loggerx.error_with_exit "#{error_msg_body} is currently transitioning. Wait until it is done transitioning before doing an upgrade."
  else
    Loggerx.info info_msg
    body = Serialization.upgrade_deployment_to_xml(package_url, service_configuration, options)
    request = ManagementHttpRequest.new(:post, request_path, body)
    request.call(options)
  end
end

#upload_certificate(cloud_service_name, ssh) ⇒ Object



351
352
353
354
355
356
357
358
# File 'lib/azure/cloud_service_management/cloud_service_management_service.rb', line 351

def upload_certificate(cloud_service_name, ssh)
  data = export_der(ssh[:cert], ssh[:key])
  request_path = "/services/hostedservices/#{cloud_service_name}/certificates"
  body = Serialization.add_certificate_to_xml(data)
  Loggerx.info "Uploading certificate to cloud service #{cloud_service_name}..."
  request = ManagementHttpRequest.new(:post, request_path, body)
  request.call
end