Class: Bosh::Deployer::InstanceManager::Aws

Inherits:
Bosh::Deployer::InstanceManager show all
Defined in:
lib/bosh/deployer/instance_manager/aws.rb

Constant Summary

Constants inherited from Bosh::Deployer::InstanceManager

CONNECTION_EXCEPTIONS

Constants included from Helpers

Helpers::DEPLOYMENTS_FILE

Instance Attribute Summary

Attributes inherited from Bosh::Deployer::InstanceManager

#renderer, #state

Instance Method Summary collapse

Methods inherited from Bosh::Deployer::InstanceManager

#agent, #apply, #attach_disk, #attach_missing_disk, #check_dependencies, #check_persistent_disk, #cloud, create, #create, #create_deployment, #create_disk, #create_stemcell, #create_vm, #delete_deployment, #delete_disk, #destroy, #detach_disk, #disk_info, #disk_model, #exists?, #initialize, #instance_model, #logger, #migrate_disk, #mount_disk, #step, #unmount_disk, #update, #update_deployment, #update_persistent_disk, #update_vm_metadata, #with_lifecycle

Methods included from Helpers

#close_ssh_sessions, #cloud_plugin, #dig_hash, #is_tgz?, #process_exists?, #remote_tunnel, #socket_readable?, #strip_relative_path

Constructor Details

This class inherits a constructor from Bosh::Deployer::InstanceManager

Instance Method Details

#configureObject

rubocop:disable MethodLength



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
# File 'lib/bosh/deployer/instance_manager/aws.rb', line 22

def configure
  properties = Config.cloud_options['properties']
  @ssh_user = properties['aws']['ssh_user']
  @ssh_port = properties['aws']['ssh_port'] || 22
  @ssh_wait = properties['aws']['ssh_wait'] || 60

  key = properties['aws']['ec2_private_key']
  err 'Missing properties.aws.ec2_private_key' unless key
  @ssh_key = File.expand_path(key)
  unless File.exists?(@ssh_key)
    err "properties.aws.ec2_private_key '#{key}' does not exist"
  end

  uri = URI.parse(properties['registry']['endpoint'])
  user, password = uri.userinfo.split(':', 2)
  @registry_port = uri.port

  @registry_db = Tempfile.new('bosh_registry_db')

  @registry_connection_settings = {
      'adapter' => 'sqlite',
      'database' => @registry_db.path
  }

  registry_config = {
    'logfile' => './bosh-registry.log',
    'http' => {
      'port' => uri.port,
      'user' => user,
      'password' => password
    },
    'db' => @registry_connection_settings,
    'cloud' => {
      'plugin' => 'aws',
      'aws' => properties['aws']
    }
  }

  @registry_config = Tempfile.new('bosh_registry_yml')
  @registry_config.write(Psych.dump(registry_config))
  @registry_config.close
end

#discover_bosh_ipObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/bosh/deployer/instance_manager/aws.rb', line 127

def discover_bosh_ip
  if exists?
    # choose elastic IP over public, as any agent connecting to the
    # deployed micro bosh will be cut off from the public IP when
    # we re-deploy micro bosh
    if cloud.ec2.instances[state.vm_cid].has_elastic_ip?
      ip = cloud.ec2.instances[state.vm_cid].elastic_ip.public_ip
    else
      ip = cloud.ec2.instances[state.vm_cid].public_ip_address
    end

    if ip && ip != Config.bosh_ip
      Config.bosh_ip = ip
      logger.info("discovered bosh ip=#{Config.bosh_ip}")
    end
  end

  super
end

#disk_size(cid) ⇒ Integer



152
153
154
155
# File 'lib/bosh/deployer/instance_manager/aws.rb', line 152

def disk_size(cid)
  # AWS stores disk size in GiB but the CPI uses MiB
  cloud.ec2.volumes[cid].size * 1024
end

#persistent_disk_changed?Boolean



157
158
159
160
161
162
163
164
# File 'lib/bosh/deployer/instance_manager/aws.rb', line 157

def persistent_disk_changed?
  # since AWS stores disk size in GiB and the CPI uses MiB there
  # is a risk of conversion errors which lead to an unnecessary
  # disk migration, so we need to do a double conversion
  # here to avoid that
  requested = (Config.resources['persistent_disk'] / 1024.0).ceil * 1024
  requested != disk_size(state.disk_cid)
end

#service_ipObject



147
148
149
# File 'lib/bosh/deployer/instance_manager/aws.rb', line 147

def service_ip
  cloud.ec2.instances[state.vm_cid].private_ip_address
end

#startObject

rubocop:disable MethodLength



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
# File 'lib/bosh/deployer/instance_manager/aws.rb', line 67

def start
  configure

  Sequel.connect(@registry_connection_settings) do |db|
    migrate(db)
    instances = @deployments['registry_instances']
    db[:registry_instances].insert_multiple(instances) if instances
  end

  unless has_bosh_registry?
    err 'bosh-registry command not found - ' +
      "run 'gem install bosh-registry'"
  end

  cmd = "bosh-registry -c #{@registry_config.path}"

  @registry_pid = spawn(cmd)

  5.times do
    sleep 0.5
    if Process.waitpid(@registry_pid, Process::WNOHANG)
      err "`#{cmd}` failed, exit status=#{$?.exitstatus}"
    end
  end

  timeout_time = Time.now.to_f + (60 * 5)
  http_client = HTTPClient.new
  begin
    http_client.head("http://127.0.0.1:#{@registry_port}")
    sleep 0.5
  rescue URI::Error, SocketError, Errno::ECONNREFUSED, HTTPClient::ReceiveTimeoutError => e
    if timeout_time - Time.now.to_f > 0
      retry
    else
      err "Cannot access bosh-registry: #{e.message}"
    end
  end

  logger.info("bosh-registry is ready on port #{@registry_port}")
ensure
  @registry_config.unlink if @registry_config
end

#stopObject

rubocop:enable MethodLength



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/bosh/deployer/instance_manager/aws.rb', line 111

def stop
  if @registry_pid && process_exists?(@registry_pid)
    Process.kill('INT', @registry_pid)
    Process.waitpid(@registry_pid)
  end

  return unless @registry_connection_settings

  Sequel.connect(@registry_connection_settings) do |db|
    @deployments['registry_instances'] = db[:registry_instances].map { |row| row }
  end

  save_state
  @registry_db.unlink if @registry_db
end

#update_spec(spec) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/bosh/deployer/instance_manager/aws.rb', line 4

def update_spec(spec)
  properties = spec.properties

  # pick from micro_bosh.yml the aws settings in
  # `apply_spec` section (apply_spec.properties.aws),
  # and if it doesn't exist, use the bosh deployer
  # aws properties (cloud.properties.aws)
  properties['aws'] =
    Config.spec_properties['aws'] ||
    Config.cloud_options['properties']['aws'].dup

  properties['aws']['registry'] = Config.cloud_options['properties']['registry']
  properties['aws']['stemcell'] = Config.cloud_options['properties']['stemcell']

  spec.delete('networks')
end