Class: Central::Machine::Azure::MasterProvisioner

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RandomName

#generate_name

Constructor Details

#initialize(subscription_id, certificate) ⇒ MasterProvisioner

Returns a new instance of MasterProvisioner.

Parameters:

  • subscription_id (String)

    Azure subscription id

  • certificate (String)

    Path to Azure management certificate



16
17
18
19
20
21
22
23
# File 'lib/central/machine/azure/master_provisioner.rb', line 16

def initialize(subscription_id, certificate)
  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

#clientObject (readonly)

Returns the value of attribute client.



12
13
14
# File 'lib/central/machine/azure/master_provisioner.rb', line 12

def client
  @client
end

#http_clientObject (readonly)

Returns the value of attribute http_client.



12
13
14
# File 'lib/central/machine/azure/master_provisioner.rb', line 12

def http_client
  @http_client
end

Instance Method Details

#cloud_service(name) ⇒ Object



109
110
111
# File 'lib/central/machine/azure/master_provisioner.rb', line 109

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

#create_virtual_network(name, location) ⇒ Object



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

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



90
91
92
# File 'lib/central/machine/azure/master_provisioner.rb', line 90

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

#generate_cloud_service_nameObject



105
106
107
# File 'lib/central/machine/azure/master_provisioner.rb', line 105

def generate_cloud_service_name
  "central-machine-#{generate_name}-#{rand(1..99)}"
end

#master_running?Boolean

Returns:

  • (Boolean)


99
100
101
102
103
# File 'lib/central/machine/azure/master_provisioner.rb', line 99

def master_running?
  http_client.get(path: '/').status == 200
rescue
  false
end

#run!(opts) ⇒ Object



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

def run!(opts)
  abort('Invalid ssh key') unless File.exist?(File.expand_path(opts[:ssh_key]))
  if opts[:ssl_cert]
    abort('Invalid ssl cert') unless File.exist?(File.expand_path(opts[:ssl_cert]))
    ssl_cert = File.read(File.expand_path(opts[:ssl_cert]))
  end
  cloud_service_name = generate_cloud_service_name
  vm_name = cloud_service_name
  master_url = ''
  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 = {
      ssl_cert: ssl_cert,
      auth_server: opts[:auth_server],
      version: opts[:version],
      vault_secret: opts[:vault_secret],
      vault_iv: opts[:vault_iv]
    }

    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,443',
      private_key_file: opts[:ssh_key],
      ssh_port: 22,
      vm_size: opts[:size]
    }

    virtual_machine = client.vm_management.create_virtual_machine(params, options)

    master_url = if opts[:ssl_cert]
                   "https://#{virtual_machine.ipaddress}"
                 else
                   "http://#{virtual_machine.ipaddress}"
    end
  end
  Excon.defaults[:ssl_verify_peer] = false
  @http_client = Excon.new(master_url.to_s, connect_timeout: 10)

  ShellSpinner "Waiting for #{vm_name.colorize(:cyan)} to start" do
    sleep 5 until master_running?
  end

  puts "Central Machine is now running at #{master_url}"
  puts "Use #{"cm login #{master_url}".colorize(:light_black)} to complete Central Machine setup"
end

#user_data(vars) ⇒ Object



94
95
96
97
# File 'lib/central/machine/azure/master_provisioner.rb', line 94

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

#virtual_network_exist?(name) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/central/machine/azure/master_provisioner.rb', line 113

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