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

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

Constant Summary

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_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, #with_lifecycle

Methods included from Helpers

#cloud_plugin, #dig_hash, #is_tgz?

Constructor Details

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

Instance Method Details

#configureObject



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

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"]
  unless key
    raise ConfigError, "Missing properties.aws.ec2_private_key"
  end
  @ssh_key = File.expand_path(key)
  unless File.exists?(@ssh_key)
    raise ConfigError, "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("aws_registry_db")
  @registry_db_url = "sqlite://#{@registry_db.path}"

  registry_config = {
    "logfile" => "./aws_registry.log",
    "http" => {
      "port" => uri.port,
      "user" => user,
      "password" => password
    },
    "db" => {
      "database" => @registry_db_url
    },
    "aws" => properties["aws"]
  }

  @registry_config = Tempfile.new("aws_registry_yml")
  @registry_config.write(YAML.dump(registry_config))
  @registry_config.close
end

#discover_bosh_ipObject



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

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 != Config.bosh_ip
      Config.bosh_ip = ip
      logger.info("discovered bosh ip=#{Config.bosh_ip}")
    end
  end

  super
end

#disk_size(cid) ⇒ Integer

Returns size in MiB.

Returns:

  • (Integer)

    size in MiB



154
155
156
157
# File 'lib/deployer/instance_manager/aws.rb', line 154

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

Returns:

  • (Boolean)


159
160
161
162
163
164
165
166
# File 'lib/deployer/instance_manager/aws.rb', line 159

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



149
150
151
# File 'lib/deployer/instance_manager/aws.rb', line 149

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

#startObject



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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/deployer/instance_manager/aws.rb', line 66

def start
  configure()

  Sequel.connect(@registry_db_url) do |db|
    migrate(db)
    instances = @deployments["aws_instances"]
    db[:aws_instances].insert_multiple(instances) if instances
  end

  unless has_aws_registry?
    raise "aws_registry command not found - " +
      "run 'gem install bosh_aws_registry'"
  end

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

  @registry_pid = spawn(cmd)

  5.times do
    sleep 0.5
    if Process.waitpid(@registry_pid, Process::WNOHANG)
      raise Error, "`#{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 => e
    if timeout_time - Time.now.to_f > 0
      retry
    else
      raise "Cannot access aws_registry: #{e.message}"
    end
  end
  logger.info("aws_registry is ready on port #{@registry_port}")
ensure
  @registry_config.unlink if @registry_config
end

#stopObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/deployer/instance_manager/aws.rb', line 108

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

  return unless @registry_db_url

  Sequel.connect(@registry_db_url) do |db|
    @deployments["aws_instances"] = db[:aws_instances].map {|row| row}
  end

  save_state
  @registry_db.unlink if @registry_db
end

#update_spec(spec) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/deployer/instance_manager/aws.rb', line 10

def update_spec(spec)
  spec = super(spec)
  properties = spec["properties"]

  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")

  spec
end

#wait_until_agent_readyObject



124
125
126
127
# File 'lib/deployer/instance_manager/aws.rb', line 124

def wait_until_agent_ready
  tunnel(@registry_port)
  super
end