Class: Central::Machine::Aws::MasterProvisioner

Inherits:
Object
  • Object
show all
Includes:
Common, CertHelper, RandomName
Defined in:
lib/central/machine/aws/master_provisioner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CertHelper

#generate_self_signed_cert

Methods included from Common

#default_subnet, #default_vpc, #resolve_ami

Constructor Details

#initialize(access_key_id, secret_key, region) ⇒ MasterProvisioner

Returns a new instance of MasterProvisioner.

Parameters:

  • access_key_id (String)

    aws_access_key_id

  • secret_key (String)

    aws_secret_access_key

  • region (String)


19
20
21
22
23
24
# File 'lib/central/machine/aws/master_provisioner.rb', line 19

def initialize(access_key_id, secret_key, region)
  @ec2 = ::Aws::EC2::Resource.new(
    region: region,
    credentials: ::Aws::Credentials.new(access_key_id, secret_key)
  )
end

Instance Attribute Details

#ec2Object (readonly)

Returns the value of attribute ec2.



14
15
16
# File 'lib/central/machine/aws/master_provisioner.rb', line 14

def ec2
  @ec2
end

#http_clientObject (readonly)

Returns the value of attribute http_client.



14
15
16
# File 'lib/central/machine/aws/master_provisioner.rb', line 14

def http_client
  @http_client
end

#regionString (readonly)

Returns:

  • (String)


144
145
146
# File 'lib/central/machine/aws/master_provisioner.rb', line 144

def region
  @region
end

Instance Method Details

#create_security_group(name, vpc_id = nil) ⇒ Object

creates security_group and authorizes default port ranges

Parameters:

  • name (String)
  • vpc_id (String, NilClass) (defaults to: nil)

Returns:

  • Aws::EC2::SecurityGroup



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/central/machine/aws/master_provisioner.rb', line 117

def create_security_group(name, vpc_id = nil)
  sg = ec2.create_security_group(
    group_name: name,
    description: 'Central Machine',
    vpc_id: vpc_id)
  sg.create_tags(tags: [
                   { key: 'Name', value: name }
                 ])
  sg.authorize_ingress( # SSHD
    ip_protocol: 'tcp',
    from_port:    22,
    to_port:      22,
    cidr_ip:     '24.7.32.100/32')
  sg.authorize_ingress( # HTTPS
    ip_protocol: 'tcp',
    from_port:    443,
    to_port:      443,
    cidr_ip:     '0.0.0.0/0')
  sg.authorize_ingress( # OAUTH
    ip_protocol: 'tcp',
    from_port:    5000,
    to_port:      5000,
    cidr_ip:     '24.7.32.100/32')
  sg
end

#ensure_security_group(vpc_id) ⇒ Aws::EC2::SecurityGroup

Parameters:

  • vpc_id (String)

Returns:

  • (Aws::EC2::SecurityGroup)


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/central/machine/aws/master_provisioner.rb', line 96

def ensure_security_group(vpc_id)
  group_name = 'central_machine'
  sg = ec2.security_groups(
    filters: [
      { name: 'group-name', values: [group_name] },
      { name: 'vpc-id', values: [vpc_id] }
    ]
  ).first
  unless sg
    ShellSpinner 'Creating AWS security group' do
      sg = create_security_group(group_name, vpc_id)
    end
  end
  sg
end

#erb(template, vars) ⇒ Object



163
164
165
166
167
# File 'lib/central/machine/aws/master_provisioner.rb', line 163

def erb(template, vars)
  ERB.new(template, nil, '%<>-').result(
    OpenStruct.new(vars).instance_eval { binding }
  )
end

#generate_nameObject



153
154
155
# File 'lib/central/machine/aws/master_provisioner.rb', line 153

def generate_name
  "central-machine-#{super}-#{rand(1..9)}"
end

#master_running?(http_client) ⇒ Boolean

Returns:

  • (Boolean)


157
158
159
160
161
# File 'lib/central/machine/aws/master_provisioner.rb', line 157

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

#run!(opts) ⇒ Object

Parameters:

  • opts (Hash)


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

def run!(opts)
  ssl_cert = nil
  if opts[:ssl_cert]
    unless File.exist?(File.expand_path(opts[:ssl_cert]))
      abort('Invalid ssl cert')
    end
    ssl_cert = File.read(File.expand_path(opts[:ssl_cert]))
  else
    ShellSpinner 'Generating self-signed SSL certificate' do
      ssl_cert = generate_self_signed_cert
    end
  end

  ami = resolve_ami(region)
  abort('No valid AMI found for region') unless ami
  opts[:vpc] = default_vpc.vpc_id unless opts[:vpc]
  subnet = if opts[:subnet].nil?
             default_subnet(opts[:vpc], region + opts[:zone])
           else
             ec2.subnet(opts[:subnet])
           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]
  }

  security_group = ensure_security_group(opts[:vpc])
  name = generate_name
  ec2_instance = ec2.create_instances(
    image_id: ami,
    min_count: 1,
    max_count: 1,
    instance_type: opts[:type],
    security_group_ids: [security_group.group_id],
    key_name: opts[:key_pair],
    subnet_id: subnet.subnet_id,
    user_data: Base64.encode64(user_data(userdata_vars)),
    block_device_mappings: [
      {
        device_name: '/dev/xvda',
        virtual_name: 'Root',
        ebs: {
          volume_size: opts[:storage],
          volume_type: 'gp2'
        }
      }
    ]
  ).first
  ec2_instance.create_tags(tags: [{ key: 'Name', value: name }])
  ShellSpinner "Creating AWS instance #{name.colorize(:cyan)} " do
    sleep 5 until ec2_instance.reload.state.name == 'running'
  end
  master_url = "https://#{ec2_instance.public_ip_address}"
  Excon.defaults[:ssl_verify_peer] = false
  http_client = Excon.new(master_url, connect_timeout: 10)
  ShellSpinner "Waiting for #{name.colorize(:cyan)} to start " do
    sleep 5 until master_running?(http_client)
  end

  puts "Central Machine is now running at #{master_url}"
  puts "Use #{"cm login --name=#{name.sub('central-machine-', '')} #{master_url}".colorize(:yellow)} to complete Central Machine setup"
end

#user_data(vars) ⇒ Object



148
149
150
151
# File 'lib/central/machine/aws/master_provisioner.rb', line 148

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