Class: Bosh::Deployer::InstanceManager::Openstack

Inherits:
Bosh::Deployer::InstanceManager show all
Defined in:
lib/deployer/instance_manager/openstack.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/openstack.rb', line 26

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

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

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

  @registry_config = Tempfile.new("openstack_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
# File 'lib/deployer/instance_manager/openstack.rb', line 129

def discover_bosh_ip
  if exists?
    server = cloud.openstack.servers.get(state.vm_cid)
    ip = server.public_ip_address
    ip = server.private_ip_address if ip.nil? || ip.empty?
    if ip.nil? || ip.empty?
      raise "Unable to discover bosh ip"
    else
      if ip["addr"] != Config.bosh_ip
        Config.bosh_ip = ip["addr"]
        logger.info("discovered bosh ip=#{Config.bosh_ip}")
      end
    end
  end

  super
end

#disk_size(cid) ⇒ Integer

Returns size in MiB.

Returns:

  • (Integer)

    size in MiB



153
154
155
156
# File 'lib/deployer/instance_manager/openstack.rb', line 153

def disk_size(cid)
  # OpenStack stores disk size in GiB but we work with MiB
  cloud.openstack.volumes.get(cid).size * 1024
end

#persistent_disk_changed?Boolean

Returns:

  • (Boolean)


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

def persistent_disk_changed?
  # since OpenStack stores disk size in GiB and we use 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
150
# File 'lib/deployer/instance_manager/openstack.rb', line 147

def service_ip
  ip = cloud.openstack.servers.get(state.vm_cid).private_ip_address
  ip["addr"] unless ip.nil? || ip.empty?
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/openstack.rb', line 66

def start
  configure()

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

  unless has_openstack_registry?
    raise "openstack_registry command not found - " +
      "run 'gem install bosh_openstack_registry'"
  end

  cmd = "openstack_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 openstack_registry: #{e.message}"
    end
  end
  logger.info("openstack_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/openstack.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["openstack_servers"] = db[:openstack_servers].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/openstack.rb', line 10

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

  properties["openstack"] =
    Config.spec_properties["openstack"] ||
    Config.cloud_options["properties"]["openstack"].dup

  properties["openstack"]["registry"] = Config.cloud_options["properties"]["registry"]
  properties["openstack"]["stemcell"] = Config.cloud_options["properties"]["stemcell"]

  spec.delete("networks")

  spec
end

#wait_until_agent_readyObject



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

def wait_until_agent_ready
  tunnel(@registry_port)
  super
end