Class: Proxmox::LXC

Inherits:
Hosted show all
Defined in:
lib/pve/proxmox.rb

Instance Attribute Summary

Attributes inherited from Base

#sid

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hosted

#===, #cnfset, #config, #current_status, #destroy, #migrate, #refresh!, #resize, #running?, #start, #stop, #stopped?, #wait

Methods inherited from Base

__new__, fetch, #method_missing, #refresh!, #respond_to?

Methods included from RestConnection

#bench, #rest_del, #rest_get, #rest_post, #rest_put

Constructor Details

#initializeLXC

Returns a new instance of LXC.



566
567
568
569
# File 'lib/pve/proxmox.rb', line 566

def initialize
  rest_prefix
  @sid = "ct:#{@vmid}"
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Proxmox::Base

Class Method Details

.allObject



468
469
470
# File 'lib/pve/proxmox.rb', line 468

def all
  Node.all.flat_map {|n| n.lxc }
end

.create(template, **options) ⇒ Object



519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
# File 'lib/pve/proxmox.rb', line 519

def create template, **options
  tmplt = PVE::CTTemplate.const_get( template&.classify || 'Default').new **options
  name = tmplt.name
  virts = Proxmox::LXC.all + Proxmox::Qemu.all
  vmid = tmplt.vmid
  if virt = virts.find {|v| v.vmid == vmid }
    raise Proxmox::AlreadyExists, "VT/VM with vmid [#{vmid}] already exists: #{virt.name}"
  end
  already_exists = virts.select {|v| v.name == name }
  unless already_exists.empty?
    raise Proxmox::AlreadyExists, "CT/VM named [#{name}] already exists: vmid: #{already_exists.map( &:vmid).inspect}"
  end
  node = Proxmox::Node.find_by_name! tmplt.node

  options = {
    ostemplate: tmplt.ostemplate,
    vmid: vmid.to_s,
    arch: tmplt.arch,
    cmode: tmplt.cmode,
    cores: tmplt.cores.to_i,
    description: tmplt.description,
    hostname: tmplt.hostname,
    memory: tmplt.memory,
    net0: tmplt.net0&.map {|k,v| "#{k}=#{v}" }&.join(','),
    net1: tmplt.net1&.map {|k,v| "#{k}=#{v}" }&.join(','),
    net2: tmplt.net2&.map {|k,v| "#{k}=#{v}" }&.join(','),
    net3: tmplt.net3&.map {|k,v| "#{k}=#{v}" }&.join(','),
    ostype: tmplt.ostype,
    :'ssh-public-keys' => tmplt.ssh_public_keys,
    storage: tmplt.storage,
    #rootfs: {
    #  volume: "root:vm-#{vmid}-disk-0", size: '4G'
    #}.map {|k,v| "#{k}=#{v}" }.join( ','),
    swap: tmplt.swap,
    unprivileged: tmplt.unprivileged,
  }.delete_if {|k,v| v.nil? }

  temp = LXC.send :__new__, node: node, vmid: options[:vmid], name: name, hostname: options[:hostname]
  upid = rest_post( "/nodes/%s/lxc" % node.node, **options)
  Task.send :__new__, node: node, host: temp, upid: upid
end

.find(name_or_id = nil, name: nil, vmid: nil) ⇒ Object



491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
# File 'lib/pve/proxmox.rb', line 491

def find name_or_id = nil, name: nil, vmid: nil
  if (name and vmid) or (name and name_or_id) or (name_or_id and vmid)
    raise ArgumentError, "name xor vmid needed to find CT, not both."
  elsif name
    find_by_name name
  elsif vmid
    find_by_vmid vmid.to_i
  elsif name_or_id =~ /\D/
    find_by_name name_or_id
  elsif name_or_id.is_a?( Numeric) or name_or_id =~ /\d/
    find_by_vmid name_or_id.to_i
  else
    raise ArgumentError, "name xor vmid needed to find CT."
  end
end

.find!(name) ⇒ Object



515
516
517
# File 'lib/pve/proxmox.rb', line 515

def find! name
  find( name) or raise( Proxmox::NotFound, "Container not found: #{name}")
end

.find_by_name(name) ⇒ Object



482
483
484
485
486
487
488
489
# File 'lib/pve/proxmox.rb', line 482

def find_by_name name
  Node.all.each do |n|
    n.lxc.each do |l|
      return l  if l.name == name
    end
  end
  nil
end

.find_by_name!(name) ⇒ Object



511
512
513
# File 'lib/pve/proxmox.rb', line 511

def find_by_name! name
  find_by_name( name) or raise( Proxmox::NotFound, "Container not found: #{name}")
end

.find_by_vmid(vmid) ⇒ Object



472
473
474
475
476
477
478
479
480
# File 'lib/pve/proxmox.rb', line 472

def find_by_vmid vmid
  vmid = vmid.to_s
  Node.all.each do |n|
    n.lxc.each do |l|
      return l  if l.vmid == vmid
    end
  end
  nil
end

.find_by_vmid!(name) ⇒ Object



507
508
509
# File 'lib/pve/proxmox.rb', line 507

def find_by_vmid! name
  find_by_vmid( name) or raise( Proxmox::NotFound, "Container not found: #{name}")
end

Instance Method Details

#enterObject



579
580
581
# File 'lib/pve/proxmox.rb', line 579

def enter
  node.enter 'pct', 'enter', vmid
end

#exec(*args) ⇒ Object



575
576
577
# File 'lib/pve/proxmox.rb', line 575

def exec *args
  node.exec 'pct', 'exec', vmid, '--', *args
end

#haObject



571
572
573
# File 'lib/pve/proxmox.rb', line 571

def ha
  HA.find self
end

#rest_prefixObject



562
563
564
# File 'lib/pve/proxmox.rb', line 562

def rest_prefix
  @rest_prefix ||= "/nodes/%s/lxc/%d" % [@node.node, @vmid]
end