Class: Gogetit::GogetLXD
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#conn ⇒ Object
readonly
Returns the value of attribute conn.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#maas ⇒ Object
readonly
Returns the value of attribute maas.
Instance Method Summary collapse
- #container_exists?(name) ⇒ Boolean
- #create(name, options = {}) ⇒ Object
- #destroy(name, args = {}) ⇒ Object
- #generate_cloud_init_config(options, config, args) ⇒ Object
-
#generate_devices(args, options) ⇒ Object
To configure devices.
- #generate_network_config(args, options) ⇒ Object
-
#generate_user_data(args, options) ⇒ Object
to generate ‘user.user-data’.
- #get_state(name) ⇒ Object
-
#initialize(conf, maas, logger) ⇒ GogetLXD
constructor
A new instance of GogetLXD.
- #list ⇒ Object
- #reserve_ips(name, options, container) ⇒ Object
- #wait_until_state(name, state) ⇒ Object
Methods included from Util
#check_ip_available, #get_gateway, #get_http_content, #get_provider_of, #is_port_open?, #knife_bootstrap_chef, #knife_bootstrap_zero, #knife_remove, #ping_available?, #run_command, #run_through_ssh, #ssh_available?, #update_databags, #wait_until_available
Constructor Details
#initialize(conf, maas, logger) ⇒ GogetLXD
Returns a new instance of GogetLXD.
13 14 15 16 17 18 19 20 21 |
# File 'lib/providers/lxd.rb', line 13 def initialize(conf, maas, logger) @config = conf @conn = Hyperkit::Client.new( api_endpoint: config[:lxd][:nodes][0][:url], verify_ssl: false ) @maas = maas @logger = logger end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
11 12 13 |
# File 'lib/providers/lxd.rb', line 11 def config @config end |
#conn ⇒ Object (readonly)
Returns the value of attribute conn.
11 12 13 |
# File 'lib/providers/lxd.rb', line 11 def conn @conn end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
11 12 13 |
# File 'lib/providers/lxd.rb', line 11 def logger @logger end |
#maas ⇒ Object (readonly)
Returns the value of attribute maas.
11 12 13 |
# File 'lib/providers/lxd.rb', line 11 def maas @maas end |
Instance Method Details
#container_exists?(name) ⇒ Boolean
28 29 30 31 32 33 34 |
# File 'lib/providers/lxd.rb', line 28 def container_exists?(name) logger.info("Calling <#{__method__.to_s}> for #{name}") list.each do |c| return true if c == name end false end |
#create(name, options = {}) ⇒ Object
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 |
# File 'lib/providers/lxd.rb', line 395 def create(name, = {}) logger.info("Calling <#{__method__.to_s}>") abort("Container #{name} already exists!") \ if container_exists?(name) abort("Domain #{name}.#{maas.get_domain} already exists!") \ if maas.domain_name_exists?(name) unless ['no-maas'] args = {} if ['alias'].nil? or ['alias'].empty? args[:alias] = config[:lxd][:default_alias] else args[:alias] = ['alias'] end args = generate_user_data(args, ) args = generate_network_config(args, ) args = generate_devices(args, ) args[:sync] ||= true conn.create_container(name, args) container = conn.container(name) container.devices = args[:devices].merge!(container.devices.to_hash) # https://github.com/jeffshantz/hyperkit/blob/master/lib/hyperkit/client/containers.rb#L240 # Adding configurations that are necessary for shipping MAAS on lxc if ['no-maas'] and ['maas-on-lxc'] container.config = container.config.to_hash # https://docs.maas.io/2.4/en/installconfig-lxd-install container.config[:"raw.lxc"] = "\ lxc.cgroup.devices.allow = c 10:237 rwm\n\ lxc.aa_profile = unconfined\n\ lxc.cgroup.devices.allow = b 7:* rwm" end conn.update_container(name, container) # Fetch container object again container = conn.container(name) reserve_ips(name, , container) \ if ['vlans'] or ['ipaddresses'] \ unless ['no-maas'] conn.start_container(name, :sync=>"true") if ['no-maas'] ip_or_fqdn = ['ip_to_access'] else ip_or_fqdn = name + '.' + maas.get_domain end if conn.execute_command(name, "ls /etc/lsb-release")[:metadata][:return] == 0 default_user = 'ubuntu' elsif conn.execute_command(name, "ls /etc/redhat-release")[:metadata][:return] == 0 default_user = 'centos' else default_user = config[:default][:user] end args[:default_user] = default_user wait_until_available(ip_or_fqdn, default_user) logger.info("#{name} has been created.") if ['no-maas'] puts "ssh #{default_user}@#{['ip_to_access']}" else puts "ssh #{default_user}@#{name}" end { result: true, info: args } end |
#destroy(name, args = {}) ⇒ Object
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 |
# File 'lib/providers/lxd.rb', line 472 def destroy(name, args = {}) logger.info("Calling <#{__method__.to_s}>") container = conn.container(name) args[:sync] ||= true info = container.to_hash if get_state(name) == 'Running' conn.stop_container(name, args) end wait_until_state(name, 'Stopped') if YAML.load(container[:config][:"user.user-data"])['maas'] logger.info("This is a MAAS enabled container.") if container[:config][:"user.network-config"] net_conf = YAML.load( container[:config][:"user.network-config"] )['config'] # To remove DNS configuration net_conf.shift net_conf.each do |nic| if nic['subnets'][0]['type'] == 'static' # It assumes we only assign a single subnet on a VLAN. # Subnets in a VLAN, VLANs in a Fabric ip = nic['subnets'][0]['address'].split('/')[0] if maas.ipaddresses_reserved?(ip) maas.ipaddresses('release', { 'ip' => ip }) end end end end maas.delete_dns_record(name) end conn.delete_container(name, args) # When multiple static IPs were reserved, it will not delete anything # since they are deleted when releasing the IPs above. logger.info("#{name} has been destroyed.") { result: true, info: info } end |
#generate_cloud_init_config(options, config, args) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/providers/lxd.rb', line 94 def generate_cloud_init_config(, config, args) logger.info("Calling <#{__method__.to_s}>") if ['no-maas'] # When there is no MAAS, containers should be able to resolve # their name with hosts file. args[:config][:'user.user-data']['manage_etc_hosts'] = true end # To add truested root CA certificates # https://cloudinit.readthedocs.io/en/latest/topics/examples.html # #configure-an-instances-trusted-ca-certificates # if config[:cloud_init] && config[:cloud_init][:ca_certs] args[:config][:'user.user-data']['ca-certs'] = {} certs = [] config[:cloud_init][:ca_certs].each do |ca| content = get_http_content(ca) certs.push( /^-----BEGIN CERTIFICATE-----.*-/m.match(content).to_s ) if content end args[:config][:'user.user-data']['ca-certs'] = { 'trusted' => certs } end # To get CA public key to be used for SSH authentication # https://cloudinit.readthedocs.io/en/latest/topics/examples.html # #writing-out-arbitrary-files if config[:cloud_init] && config[:cloud_init][:ssh_ca_public_key] args[:config][:'user.user-data']['write_files'] = [] content = get_http_content(config[:cloud_init][:ssh_ca_public_key][:key_url]) if content file = { 'content' => content.chop!, 'path' => config[:cloud_init][:ssh_ca_public_key][:key_path], 'owner' => config[:cloud_init][:ssh_ca_public_key][:owner], 'permissions' => config[:cloud_init][:ssh_ca_public_key][:permissions] } args[:config][:'user.user-data']['write_files'].push(file) args[:config][:'user.user-data']['bootcmd'] = [] args[:config][:'user.user-data']['bootcmd'].push( "cloud-init-per once ssh-ca-pub-key \ echo \"TrustedUserCAKeys #{file['path']}\" >> /etc/ssh/sshd_config" ) end if config[:cloud_init][:ssh_ca_public_key][:revocation_url] content = get_http_content(config[:cloud_init][:ssh_ca_public_key][:revocation_url]) if content args[:config][:'user.user-data']['bootcmd'].push( "cloud-init-per once download-key-revocation-list \ curl -o #{config[:cloud_init][:ssh_ca_public_key][:revocation_path]} \ #{config[:cloud_init][:ssh_ca_public_key][:revocation_url]}" ) args[:config][:'user.user-data']['bootcmd'].push( "cloud-init-per once ssh-user-key-revocation-list \ echo \"RevokedKeys #{config[:cloud_init][:ssh_ca_public_key][:revocation_path]}\" \ >> /etc/ssh/sshd_config" ) end end end # To add users # https://cloudinit.readthedocs.io/en/latest/topics/examples.html # #including-users-and-groups if config[:cloud_init] && config[:cloud_init][:users] args[:config][:'user.user-data']['users'] = [] args[:config][:'user.user-data']['users'].push('default') config[:cloud_init][:users].each do |user| args[:config][:'user.user-data']['users'].push(Hashie.stringify_keys user) end end return args end |
#generate_devices(args, options) ⇒ Object
To configure devices
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'lib/providers/lxd.rb', line 269 def generate_devices(args, ) logger.info("Calling <#{__method__.to_s}>") args[:devices] = {} if ['no-maas'] args[:devices] = YAML.load_file(['file'])['devices'] # https://docs.maas.io/2.4/en/installconfig-lxd-install for i in 0..7 i = i.to_s args[:devices]["loop" + i] = {} args[:devices]["loop" + i]["path"] = "/dev/loop" + i args[:devices]["loop" + i]["type"] = "unix-block" end # Now, LXD API can handle integer as a value of a map args[:devices].each do |k, v| v.each do |kk, vv| if vv.is_a? Integer v[kk] = vv.to_s end end end args[:devices] = (Hashie.symbolize_keys args[:devices]) elsif ['ipaddresses'] [:ifaces].each_with_index do |iface,index| if index == 0 if iface['vlan']['name'] == 'untagged' # or vid == 0 args[:devices][:"eth#{index}"] = { mtu: iface['vlan']['mtu'].to_s, #This must be string name: "eth#{index}", nictype: 'bridged', parent: config[:default][:root_bridge], type: 'nic' } elsif iface['vlan']['name'] != 'untagged' # or vid != 0 args[:devices][:"eth#{index}"] = { mtu: iface['vlan']['mtu'].to_s, #This must be string name: "eth#{index}", nictype: 'bridged', parent: config[:default][:root_bridge] + "-" + iface['vlan']['vid'].to_s, type: 'nic' } end # When options[:ifaces][0]['vlan']['name'] == 'untagged' and index > 0, # it does not need to generate more devices # since it will configure the IPs with tagged VLANs. elsif [:ifaces][0]['vlan']['name'] != 'untagged' args[:devices][:"eth#{index}"] = { mtu: iface['vlan']['mtu'].to_s, #This must be string name: "eth#{index}", nictype: 'bridged', parent: config[:default][:root_bridge] + "-" + iface['vlan']['vid'].to_s, type: 'nic' } end end else abort("root_bridge #{config[:default][:root_bridge]} does not exist.") \ unless conn.networks.include? config[:default][:root_bridge] root_bridge_mtu = nil # It assumes you only use one fabric as of now, # since there might be more fabrics with each untagged vlans on them, # which might make finding exact mtu fail as following process. default_fabric = 'fabric-0' maas.get_subnets.each do |subnet| if subnet['vlan']['name'] == 'untagged' and \ subnet['vlan']['fabric'] == default_fabric root_bridge_mtu = subnet['vlan']['mtu'] break end end args[:devices] = {} args[:devices][:"eth0"] = { mtu: root_bridge_mtu.to_s, #This must be string name: 'eth0', nictype: 'bridged', parent: config[:default][:root_bridge], type: 'nic' } end return args end |
#generate_network_config(args, options) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/providers/lxd.rb', line 173 def generate_network_config(args, ) logger.info("Calling <#{__method__.to_s}>") if ['no-maas'] args[:config][:'user.network-config'] = \ YAML.load_file(['file'])['network'] # physical device will be the gate device args[:config][:"user.network-config"]['config'].each do |iface| if iface['type'] == "physical" ['ip_to_access'] = iface['subnets'][0]['address'].split('/')[0] end end args[:config][:"user.network-config"] = \ YAML.dump(args[:config][:"user.network-config"])[4..-1] elsif ['ipaddresses'] [:ifaces] = check_ip_available(['ipaddresses'], maas) abort("There is no dns server specified for the gateway network.") \ unless [:ifaces][0]['dns_servers'][0] abort("There is no gateway specified for the gateway network.") \ unless [:ifaces][0]['gateway_ip'] args[:config][:'user.network-config'] = { 'version' => 1, 'config' => [ { 'type' => 'nameserver', 'address' => [:ifaces][0]['dns_servers'][0] } ] } # to generate configuration for [:config][:'user.network-config']['config'] [:ifaces].each_with_index do |iface,index| if index == 0 iface_conf = { 'type' => 'physical', 'name' => "eth#{index}", 'subnets' => [ { 'type' => 'static', 'ipv4' => true, 'address' => iface['ip'] + '/' + iface['cidr'].split('/')[1], 'gateway' => iface['gateway_ip'], 'mtu' => iface['vlan']['mtu'], 'control' => 'auto' } ] } elsif index > 0 if [:ifaces][0]['vlan']['name'] != 'untagged' iface_conf = { 'type' => 'physical', 'name' => "eth#{index}", 'subnets' => [ { 'type' => 'static', 'ipv4' => true, 'address' => iface['ip'] + '/' + iface['cidr'].split('/')[1], 'mtu' => iface['vlan']['mtu'], 'control' => 'auto' } ] } elsif [:ifaces][0]['vlan']['name'] == 'untagged' iface_conf = { 'type' => 'vlan', 'name' => "eth0.#{iface['vlan']['vid'].to_s}", 'vlan_id' => iface['vlan']['vid'].to_s, 'vlan_link' => 'eth0', 'subnets' => [ { 'type' => 'static', 'ipv4' => true, 'address' => iface['ip'] + '/' + iface['cidr'].split('/')[1], 'mtu' => iface['vlan']['mtu'], 'control' => 'auto' } ] } end end args[:config][:'user.network-config']['config'].push(iface_conf) end args[:config][:"user.network-config"] = \ YAML.dump(args[:config][:"user.network-config"])[4..-1] end return args end |
#generate_user_data(args, options) ⇒ Object
to generate ‘user.user-data’
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/providers/lxd.rb', line 49 def generate_user_data(args, ) logger.info("Calling <#{__method__.to_s}>") args[:config] = {} if ['no-maas'] args[:config][:"user.user-data"] = {} if ['maas-on-lxc'] args[:config][:"security.privileged"] = "true" end else sshkeys = maas.get_sshkeys pkg_repos = maas.get_package_repos args[:config][:'user.user-data'] = { 'ssh_authorized_keys' => [] } sshkeys.each do |key| args[:config][:'user.user-data']['ssh_authorized_keys'].push(key['key']) end pkg_repos.each do |repo| if repo['name'] == 'main_archive' args[:config][:'user.user-data']['apt_mirror'] = repo['url'] end end args[:config][:"user.user-data"]['source_image_alias'] = args[:alias] args[:config][:"user.user-data"]['maas'] = true end args[:config][:"user.user-data"]['gogetit'] = true # To disable to update apt database on first boot # so chef client can keep doing its job. args[:config][:'user.user-data']['package_update'] = false args[:config][:'user.user-data']['package_upgrade'] = false generate_cloud_init_config(, config, args) args[:config][:"user.user-data"] = \ "#cloud-config\n" + YAML.dump(args[:config][:"user.user-data"])[4..-1] return args end |
#get_state(name) ⇒ Object
36 37 38 39 |
# File 'lib/providers/lxd.rb', line 36 def get_state(name) logger.info("Calling <#{__method__.to_s}>") conn.container(name)[:status] end |
#list ⇒ Object
23 24 25 26 |
# File 'lib/providers/lxd.rb', line 23 def list logger.info("Calling <#{__method__.to_s}>") conn.containers end |
#reserve_ips(name, options, container) ⇒ Object
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'lib/providers/lxd.rb', line 359 def reserve_ips(name, , container) logger.info("Calling <#{__method__.to_s}>") # Generate params to reserve IPs [:ifaces].each_with_index do |iface,index| if index == 0 params = { 'subnet' => iface['cidr'], 'ip' => iface['ip'], 'hostname' => name, 'mac' => container[:expanded_config][:"volatile.eth#{index}.hwaddr"] } elsif index > 0 # if dot, '.', is used as a conjunction instead of '-', # it fails ocuring '404 not found'. # if under score, '_', is used as a conjunction instead of '-', # it breaks MAAS DNS somehow.. if [:ifaces][0]['vlan']['name'] == 'untagged' params = { 'subnet' => iface['cidr'], 'ip' => iface['ip'], 'hostname' => 'eth0' + '-' + iface['vlan']['vid'].to_s + '-' + name, 'mac' => container[:expanded_config][:"volatile.eth0.hwaddr"] } elsif [:ifaces][0]['vlan']['name'] != 'untagged' params = { 'subnet' => iface['cidr'], 'ip' => iface['ip'], 'hostname' => "eth#{index}" + '-' + name, 'mac' => container[:expanded_config][:"volatile.eth#{index}.hwaddr"] } end end maas.ipaddresses('reserve', params) end end |
#wait_until_state(name, state) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/providers/lxd.rb', line 41 def wait_until_state(name, state) logger.info("Calling <#{__method__.to_s}> for being #{state}..") until get_state(name) == state sleep 3 end end |