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

Inherits:
Bosh::Deployer::InstanceManager show all
Includes:
Bosh::Deployer::InstanceManagerHelpers
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 included from Bosh::Deployer::InstanceManagerHelpers

#process_exists?, #socket_readable?, #tunnel

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



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

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"]
  err "Missing properties.openstack.private_key" unless key
  @ssh_key = File.expand_path(key)
  unless File.exists?(@ssh_key)
    err "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



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

def discover_bosh_ip
  if exists?
    server = cloud.openstack.servers.get(state.vm_cid)
    # LP OpenStack Nova 185110:
    # Since OS API 1.1, server addresses exposes the network names
    # instead of the network types. so we need to fetch the
    # os-floating-ips and find if any of them is associated to the
    # server in order to get its public address.
    floating_ip = cloud.openstack.addresses.find {
                    |addr| addr.instance_id == server.id
                  }
    ip = floating_ip.nil? ? service_ip : floating_ip.ip
    err "Unable to discover bosh ip" if ip.nil?

    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



167
168
169
170
# File 'lib/deployer/instance_manager/openstack.rb', line 167

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)


172
173
174
175
176
177
178
179
# File 'lib/deployer/instance_manager/openstack.rb', line 172

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



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/deployer/instance_manager/openstack.rb', line 148

def service_ip
  server = cloud.openstack.servers.get(state.vm_cid)
  # LP OpenStack Nova 185110:
  # Since OS API 1.1, server addresses exposes the network names
  # instead of the network types, so we need to known which network
  # label is used in OS (label parm or "private" by default) to fetch
  # the service IP address.
  net_conf  = Config.net_conf
  net_label = net_conf["label"].nil? ? "private" : net_conf["label"]
  ip_addresses = server.addresses[net_label]
  unless ip_addresses.nil? || ip_addresses.empty?
    address = ip_addresses.select { |ip| ip["version"] == 4 }.first
    ip = address ? address["addr"] : nil
  end
  err "Unable to discover service ip" if ip.nil?
  ip
end

#startObject



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
93
94
95
96
97
98
99
100
101
102
# File 'lib/deployer/instance_manager/openstack.rb', line 62

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?
    err "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)
      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 => e
    if timeout_time - Time.now.to_f > 0
      retry
    else
      err "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



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/deployer/instance_manager/openstack.rb', line 104

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

TODO extract



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

def update_spec(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")
end

#wait_until_agent_readyObject



120
121
122
123
# File 'lib/deployer/instance_manager/openstack.rb', line 120

def wait_until_agent_ready
  tunnel(@registry_port)
  super
end