Class: VagrantPlugins::WinAzure::Action::RunInstance

Inherits:
Object
  • Object
show all
Includes:
Vagrant::Util::Retryable
Defined in:
lib/vagrant-azure/action/run_instance.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ RunInstance

Returns a new instance of RunInstance.



18
19
20
21
# File 'lib/vagrant-azure/action/run_instance.rb', line 18

def initialize(app, env)
  @app = app
  @logger = Log4r::Logger.new('vagrant_azure::action::run_instance')
end

Instance Method Details

#call(env) ⇒ Object



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
# File 'lib/vagrant-azure/action/run_instance.rb', line 23

def call(env)
  config = env[:machine].provider_config

  # Add the mandatory parameters and options
  params = {
    vm_name: config.vm_name,
    vm_user: config.vm_user,
    image: config.vm_image
  }

  options = {
    cloud_service_name: config.cloud_service_name
  }


  # Add the optional parameters and options if not nil
  params[:password] = config.vm_password unless config.vm_password.nil?
  params[:location] = config.vm_location unless config.vm_location.nil?
  params[:affinity_group] = config.vm_affinity_group unless \
  config.vm_affinity_group.nil?

  options[:storage_account_name] = config.storage_acct_name unless \
  config.storage_acct_name.nil?
  options[:deployment_name] = config.deployment_name unless \
  config.deployment_name.nil?
  options[:tcp_endpoints] = config.tcp_endpoints unless \
    config.tcp_endpoints.nil?
  options[:private_key_file] = config.private_key_file unless \
    config.private_key_file.nil?
  options[:certificate_file] = config.certificate_file unless \
    config.certificate_file.nil?
  options[:ssh_port] = config.ssh_port unless \
  config.ssh_port.nil?
  options[:vm_size] = config.vm_size unless \
  config.vm_size.nil?
  options[:winrm_transport] = config.winrm_transport unless \
  config.winrm_transport.nil?
  options[:winrm_http_port] = config.winrm_http_port unless \
  config.winrm_http_port.nil?
  options[:winrm_https_port] = config.winrm_https_port unless \
  config.winrm_https_port.nil?
  options[:availability_set_name] = config.availability_set_name unless \
  config.availability_set_name.nil?
  options[:virtual_network_name] = config.vm_virtual_network_name unless \
  config.vm_virtual_network_name.nil?

  add_role = false

  env[:ui].info(params.inspect)
  env[:ui].info(options.inspect)

  server = VagrantPlugins::WinAzure::CLOUD_SERVICE_SEMAPHORE.synchronize do
    # Check if the cloud service exists and if yes, does it contain
    # a deployment.
    if config.cloud_service_name && !config.cloud_service_name.empty?
      begin
        cloud_service = ManagementHttpRequest.new(
          :get,
          "/services/hostedservices/#{config.cloud_service_name}?embed-detail=true"
        ).call

        deployments = cloud_service.css 'HostedService Deployments Deployment'

        # Lets see if any deployments exist. Set add_role = true if yes.
        # We're not worried about deployment slots, because the SDK has
        # hard coded 'Production' as deployment slot and you can have only
        # one deployment per deployment slot.
        add_role = deployments.length == 1
      rescue Exception => e
        add_role = false
      end
    end

    env[:ui].info("Add Role? - #{add_role}")

    if add_role
      env[:azure_vm_service].add_role(params.clone.merge(cloud_service_name: config.cloud_service_name), options)
    else
      env[:azure_vm_service].create_virtual_machine(params, options)
    end
  end

  if server.nil?
    raise Errors::CreateVMFailure
  end

  # The Ruby SDK returns any exception encountered on create virtual
  # machine as a string.

  if server.instance_of? String
    raise Errors::ServerNotCreated, message: server
  end

  env[:machine].id = "#{server.vm_name}@#{server.cloud_service_name}"

  @app.call(env)
end