Class: Central::Machine::Azure::NodeProvisioner

Inherits:
Object
  • Object
show all
Includes:
RandomName
Defined in:
lib/central/machine/azure/node_provisioner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_client, subscription_id, certificate) ⇒ NodeProvisioner

Returns a new instance of NodeProvisioner.

Parameters:

  • api_client (Central::Client)

    Central api client

  • subscription_id (String)

    Azure subscription id

  • certificate (String)

    Path to Azure management certificate



18
19
20
21
22
23
24
25
26
# File 'lib/central/machine/azure/node_provisioner.rb', line 18

def initialize(api_client, subscription_id, certificate)
  @api_client = api_client
  abort('Invalid management certificate') unless File.exist?(File.expand_path(certificate))

  @client = ::Azure
  client.management_certificate = certificate
  client.subscription_id        = subscription_id
  client.vm_management.initialize_external_logger(Logger.new) # We don't want all the output
end

Instance Attribute Details

#api_clientObject (readonly)

Returns the value of attribute api_client.



13
14
15
# File 'lib/central/machine/azure/node_provisioner.rb', line 13

def api_client
  @api_client
end

#clientObject (readonly)

Returns the value of attribute client.



13
14
15
# File 'lib/central/machine/azure/node_provisioner.rb', line 13

def client
  @client
end

Instance Method Details

#cloud_service(name) ⇒ Object



104
105
106
# File 'lib/central/machine/azure/node_provisioner.rb', line 104

def cloud_service(name)
  client.cloud_service_management.get_cloud_service(name)
end

#cloud_service_exist?(name) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/central/machine/azure/node_provisioner.rb', line 100

def cloud_service_exist?(name)
  cloud_service(name)
end

#create_virtual_network(name, location) ⇒ Object



112
113
114
115
116
# File 'lib/central/machine/azure/node_provisioner.rb', line 112

def create_virtual_network(name, location)
  address_space = ['10.0.0.0/20']
  options = { subnet: [{ name: 'subnet-1', ip_address: '10.0.0.0', cidr: 23 }] }
  client.network_management.set_network_configuration(name, location, address_space, options)
end

#erb(template, vars) ⇒ Object



96
97
98
# File 'lib/central/machine/azure/node_provisioner.rb', line 96

def erb(template, vars)
  ERB.new(template).result(OpenStruct.new(vars).instance_eval { binding })
end

#generate_cloud_service_name(name, stack) ⇒ Object



88
89
90
# File 'lib/central/machine/azure/node_provisioner.rb', line 88

def generate_cloud_service_name(name, stack)
  "central-#{stack}-#{name}"
end

#generate_nameObject



84
85
86
# File 'lib/central/machine/azure/node_provisioner.rb', line 84

def generate_name
  "#{super}-#{rand(1..99)}"
end

#run!(opts) ⇒ Object



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
# File 'lib/central/machine/azure/node_provisioner.rb', line 28

def run!(opts)
  abort('Invalid ssh key') unless File.exist?(File.expand_path(opts[:ssh_key]))
  node = nil
  vm_name = opts[:name] || generate_name
  cloud_service_name = generate_cloud_service_name(vm_name, opts[:stack])

  ShellSpinner "Creating Azure Virtual Machine #{vm_name.colorize(:cyan)}" do
    if opts[:virtual_network].nil?
      location = opts[:location].downcase.tr(' ', '-')
      default_network_name = "central-#{location}"
      create_virtual_network(default_network_name, opts[:location]) unless virtual_network_exist?(default_network_name)
      opts[:virtual_network] = default_network_name
      opts[:subnet] = 'subnet-1'
    end

    userdata_vars = {
      version: opts[:version],
      master_uri: opts[:master_uri],
      stack_token: opts[:stack_token]
    }

    params = {
      vm_name: vm_name,
      vm_user: 'core',
      location: opts[:location],
      image: '2b171e93f07c4903bcad35bda10acf22__CoreOS-Stable-766.3.0',
      custom_data: Base64.encode64(user_data(userdata_vars)),
      ssh_key: opts[:ssh_key]
    }
    options = {
      cloud_service_name: cloud_service_name,
      deployment_name: vm_name,
      virtual_network_name: opts[:virtual_network],
      subnet_name: opts[:subnet],
      tcp_endpoints: '80',
      private_key_file: opts[:ssh_key],
      ssh_port: 22,
      vm_size: opts[:size]
    }

    client.vm_management.create_virtual_machine(params, options)
  end
  ShellSpinner "Waiting for node #{vm_name.colorize(:cyan)} join to stack #{opts[:stack].colorize(:cyan)} " do
    sleep 2 until node = vm_exists_in_stack?(opts[:stack], vm_name)
  end
  if node
    labels = ["region=#{cloud_service(cloud_service_name).location}"]
    set_labels(node, labels)
  end
end

#set_labels(node, labels) ⇒ Object



118
119
120
121
122
# File 'lib/central/machine/azure/node_provisioner.rb', line 118

def set_labels(node, labels)
  data = {}
  data[:labels] = labels
  api_client.put("nodes/#{node['id']}", data, {}, 'Central-Stack-Token' => node['stack']['token'])
end

#user_data(vars) ⇒ Object



79
80
81
82
# File 'lib/central/machine/azure/node_provisioner.rb', line 79

def user_data(vars)
  cloudinit_template = File.join(__dir__, '/cloudinit.yml')
  erb(File.read(cloudinit_template), vars)
end

#virtual_network_exist?(name) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/central/machine/azure/node_provisioner.rb', line 108

def virtual_network_exist?(name)
  client.network_management.list_virtual_networks.find { |n| n.name == name }
end

#vm_exists_in_stack?(stack, name) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/central/machine/azure/node_provisioner.rb', line 92

def vm_exists_in_stack?(stack, name)
  api_client.get("stacks/#{stack}/nodes")['nodes'].find { |n| n['name'] == name }
end