Class: VpsAdminOS::Converter::Vz6::Container

Inherits:
Object
  • Object
show all
Includes:
OsCtl::Lib::Utils::Log, OsCtl::Lib::Utils::System
Defined in:
lib/vpsadminos-converter/vz6/container.rb

Overview

Instances represent OpenVZ Legacy containers

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ctid) ⇒ Container

Returns a new instance of Container.



11
12
13
# File 'lib/vpsadminos-converter/vz6/container.rb', line 11

def initialize(ctid)
  @ctid = ctid
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/vpsadminos-converter/vz6/container.rb', line 9

def config
  @config
end

#ctidObject (readonly)

Returns the value of attribute ctid.



9
10
11
# File 'lib/vpsadminos-converter/vz6/container.rb', line 9

def ctid
  @ctid
end

Instance Method Details

#convert(user, group, opts = {}) ⇒ Container

Parameters:

  • user (User)
  • group (Group)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :netif (Hash)

Returns:



43
44
45
46
47
48
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
93
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
# File 'lib/vpsadminos-converter/vz6/container.rb', line 43

def convert(user, group, opts = {})
  ct = Container.new(ctid, user, group)

  [
    'VE_ROOT',
    'VE_PRIVATE',
    'VE_LAYOUT', # TODO: check?
    'NETFILTER'
  ].each { |v| config.consume(v) }

  ct.rootfs = if ploop?
                config.consume('VE_ROOT')
              else
                config.consume('VE_PRIVATE')
              end

  raise 'config missing OSTEMPLATE' unless config['OSTEMPLATE']

  # TODO: we should probably guarantee distribution names and allowed version
  #       specification... e.g. allow debian-9.0, forbid debian-stretch
  ct.distribution, ct.version = config.consume('OSTEMPLATE').split('-')

  ct.hostname = config.consume('HOSTNAME') || 'vps'

  if config['NAMESERVER']
    ct.dns_resolvers.concat(config.consume('NAMESERVER').map(&:to_s))
  end

  ct.autostart.enabled = true if config.consume('ONBOOT')

  if config['PHYSPAGES']
    mem = config.consume('PHYSPAGES')[1] * 4 * 1024

    if config['SWAPPAGES']
      ct.cgparams.set(
        'memory.memsw.limit_in_bytes',
        mem + (config.consume('SWAPPAGES')[1] * 4 * 1024)
      )
    end

    ct.cgparams.set('memory.limit_in_bytes', mem)
  end

  if config['IP_ADDRESS'] && opts[:netif]
    netif = NetInterface.for(opts[:netif][:type]).new(
      opts[:netif][:name],
      opts[:netif][:hwaddr]
    )

    case netif.type
    when :bridge
      netif.link = opts[:netif][:link]

    when :routed
      netif.routes = { 4 => [], 6 => [] }
    end

    all_ips = config.consume('IP_ADDRESS')

    [4, 6].each do |ip_v|
      netif.ip_addresses[ip_v] = all_ips.select do |ip|
        ip.send("ipv#{ip_v}?")
      end

      netif.routes[ip_v] = netif.ip_addresses[ip_v] if netif.type == :routed
    end

    ct.netifs << netif
  end

  if config['DEVICES']
    config.consume('DEVICES').each do |dev|
      ct.devices << dev.to_ct_device
    end
  end

  ct
end

#exist?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/vpsadminos-converter/vz6/container.rb', line 15

def exist?
  syscmd("vzlist #{ctid}", valid_rcs: [1]).success?
end

#layoutObject



122
123
124
125
126
# File 'lib/vpsadminos-converter/vz6/container.rb', line 122

def layout
  raise 'unable to determine VE_LAYOUT' unless config['VE_LAYOUT']

  config['VE_LAYOUT'].value
end

#loadObject

Load config from ‘/etc/vz/conf/%#ctid.conf`



34
35
36
# File 'lib/vpsadminos-converter/vz6/container.rb', line 34

def load
  @config = Vz6::Config.parse(ctid, "/etc/vz/conf/#{ctid}.conf")
end

#ploop?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/vpsadminos-converter/vz6/container.rb', line 128

def ploop?
  layout.start_with?('ploop')
end

#running?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/vpsadminos-converter/vz6/container.rb', line 29

def running?
  status[:running]
end

#statusObject



19
20
21
22
23
24
25
26
27
# File 'lib/vpsadminos-converter/vz6/container.rb', line 19

def status
  stats = syscmd("vzctl status #{ctid}").output.strip.split

  {
    exist: stats[2] == 'exist',
    mounted: stats[3] == 'mounted',
    running: stats[4] == 'running'
  }
end