Class: Central::Machine::DigitalOcean::MasterProvisioner
- Inherits:
-
Object
- Object
- Central::Machine::DigitalOcean::MasterProvisioner
- Includes:
- CertHelper, RandomName
- Defined in:
- lib/central/machine/digital_ocean/master_provisioner.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#http_client ⇒ Object
readonly
Returns the value of attribute http_client.
Instance Method Summary collapse
- #erb(template, vars) ⇒ Object
- #generate_name ⇒ Object
-
#initialize(token) ⇒ MasterProvisioner
constructor
A new instance of MasterProvisioner.
- #master_running? ⇒ Boolean
- #run!(opts) ⇒ Object
- #ssh_key(public_key) ⇒ Object
- #user_data(vars) ⇒ Object
Methods included from CertHelper
Constructor Details
#initialize(token) ⇒ MasterProvisioner
Returns a new instance of MasterProvisioner.
16 17 18 |
# File 'lib/central/machine/digital_ocean/master_provisioner.rb', line 16 def initialize(token) @client = DropletKit::Client.new(access_token: token) end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
13 14 15 |
# File 'lib/central/machine/digital_ocean/master_provisioner.rb', line 13 def client @client end |
#http_client ⇒ Object (readonly)
Returns the value of attribute http_client.
13 14 15 |
# File 'lib/central/machine/digital_ocean/master_provisioner.rb', line 13 def http_client @http_client end |
Instance Method Details
#erb(template, vars) ⇒ Object
93 94 95 |
# File 'lib/central/machine/digital_ocean/master_provisioner.rb', line 93 def erb(template, vars) ERB.new(template, nil, '%<>-').result(OpenStruct.new(vars).instance_eval { binding }) end |
#generate_name ⇒ Object
79 80 81 |
# File 'lib/central/machine/digital_ocean/master_provisioner.rb', line 79 def generate_name "central-machine-#{super}-#{rand(1..9)}" end |
#master_running? ⇒ Boolean
87 88 89 90 91 |
# File 'lib/central/machine/digital_ocean/master_provisioner.rb', line 87 def master_running? http_client.get(path: '/').status == 200 rescue false end |
#run!(opts) ⇒ Object
20 21 22 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 |
# File 'lib/central/machine/digital_ocean/master_provisioner.rb', line 20 def run!(opts) abort('Invalid ssh key') unless File.exist?(File.(opts[:ssh_key])) ssh_key = ssh_key(File.read(File.(opts[:ssh_key])).strip) abort('Ssh key does not exist in Digital Ocean') unless ssh_key if opts[:ssl_cert] abort('Invalid ssl cert') unless File.exist?(File.(opts[:ssl_cert])) ssl_cert = File.read(File.(opts[:ssl_cert])) else ShellSpinner 'Generating self-signed SSL certificate' do ssl_cert = generate_self_signed_cert end 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], mongodb_uri: opts[:mongodb_uri] } droplet = DropletKit::Droplet.new( name: generate_name, region: opts[:region], image: 'coreos-stable', size: opts[:size], private_networking: true, user_data: user_data(userdata_vars), ssh_keys: [ssh_key.id] ) ShellSpinner "Creating DigitalOcean droplet #{droplet.name.colorize(:cyan)} " do droplet = client.droplets.create(droplet) until droplet.status == 'active' droplet = client.droplets.find(id: droplet.id) sleep 5 end end master_url = "https://#{droplet.public_ip}" Excon.defaults[:ssl_verify_peer] = false @http_client = Excon.new(master_url.to_s, connect_timeout: 10) ShellSpinner "Waiting for #{droplet.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 --name=#{droplet.name.sub('central-machine-', '')} #{master_url}".colorize(:light_black)} to complete Central Machine setup" end |
#ssh_key(public_key) ⇒ Object
83 84 85 |
# File 'lib/central/machine/digital_ocean/master_provisioner.rb', line 83 def ssh_key(public_key) client.ssh_keys.all.find { |key| key.public_key == public_key } end |
#user_data(vars) ⇒ Object
74 75 76 77 |
# File 'lib/central/machine/digital_ocean/master_provisioner.rb', line 74 def user_data(vars) cloudinit_template = File.join(__dir__, '/cloudinit_master.yml') erb(File.read(cloudinit_template), vars) end |