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.



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

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



472
473
474
# File 'lib/pve/proxmox.rb', line 472

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

.create(template, **options) ⇒ Object



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
560
561
562
563
# File 'lib/pve/proxmox.rb', line 523

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



495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# File 'lib/pve/proxmox.rb', line 495

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



519
520
521
# File 'lib/pve/proxmox.rb', line 519

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

.find_by_name(name) ⇒ Object



486
487
488
489
490
491
492
493
# File 'lib/pve/proxmox.rb', line 486

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



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

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

.find_by_vmid(vmid) ⇒ Object



476
477
478
479
480
481
482
483
484
# File 'lib/pve/proxmox.rb', line 476

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



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

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

Instance Method Details

#enterObject



583
584
585
# File 'lib/pve/proxmox.rb', line 583

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

#exec(*args) ⇒ Object



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

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

#haObject



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

def ha
  HA.find self
end

#rest_prefixObject



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

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