Class: VpsAdminOS::Converter::Vz6::ConfigItem

Inherits:
Object
  • Object
show all
Defined in:
lib/vpsadminos-converter/vz6/config_item.rb

Defined Under Namespace

Classes: Device

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ctid, k, v) ⇒ ConfigItem

Returns a new instance of ConfigItem



30
31
32
33
# File 'lib/vpsadminos-converter/vz6/config_item.rb', line 30

def initialize(ctid, k, v)
  @key = k
  @value = parse(ctid, v)
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key



28
29
30
# File 'lib/vpsadminos-converter/vz6/config_item.rb', line 28

def key
  @key
end

#valueObject (readonly)

Returns the value of attribute value



28
29
30
# File 'lib/vpsadminos-converter/vz6/config_item.rb', line 28

def value
  @value
end

Instance Method Details

#consumeObject



39
40
41
# File 'lib/vpsadminos-converter/vz6/config_item.rb', line 39

def consume
  @consumed = true
end

#consumed?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/vpsadminos-converter/vz6/config_item.rb', line 35

def consumed?
  @consumed.nil? ? false : @consumed
end

#parse(ctid, v) ⇒ Object (protected)



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
# File 'lib/vpsadminos-converter/vz6/config_item.rb', line 44

def parse(ctid, v)
  case key
  # Miscellaneous
  when 'NAME', 'DESCRIPTION', 'OSTEMPLATE', 'MOUNT_OPTS', 'ORIGIN_SAMPLE',
       'VE_LAYOUT'
    v

  when 'VE_ROOT', 'VE_PRIVATE'
    veid_subst(ctid, v)

  when 'ONBOOT', 'DISABLED'
    parse_bool(v)

  when 'BOOTORDER', 'STOP_TIMEOUT'
    parse_num(v)

  # Networking
  when 'HOSTNAME', 'NETFILTER'
    v

  when 'NAMESERVER', 'IP_ADDRESS'
    parse_addrs(v)

  when 'SEARCHDOMAIN'
    parse_list(v)

  # VSwap limits
  when 'PHYSPAGES', 'SWAPPAGES'
    parse_limit(v, pages: true)

  # CPU fair scheduler parameters
  when 'CPUUNITS', 'CPUS', 'CPULIMIT'
    parse_num(v)

  # User beancounters
  # TODO

  # Devices
  when 'DEVICES'
    parse_devices(v)

  # Capabilities & features
  when 'CAPABILITY', 'FEATURES'
    parse_onoff_list(v)

  else
    v
  end
end

#parse_addrs(v) ⇒ Object (protected)



119
120
121
# File 'lib/vpsadminos-converter/vz6/config_item.rb', line 119

def parse_addrs(v)
  parse_list(v).map { |addr| IPAddress.parse(addr) }
end

#parse_bool(v) ⇒ Object (protected)



98
99
100
101
102
103
104
105
106
107
# File 'lib/vpsadminos-converter/vz6/config_item.rb', line 98

def parse_bool(v)
  case v
  when 'yes'
    true
  when 'no'
    false
  else
    fail "unexpected boolean value #{key}=\"#{v}\""
  end
end

#parse_devices(v) ⇒ Object (protected)



157
158
159
# File 'lib/vpsadminos-converter/vz6/config_item.rb', line 157

def parse_devices(v)
  parse_list(v).map { |dev| Device.new(*dev.split(':')) }
end

#parse_limit(v, pages: nil) ⇒ Object (protected)



123
124
125
126
127
128
129
130
131
# File 'lib/vpsadminos-converter/vz6/config_item.rb', line 123

def parse_limit(v, pages: nil)
  if v.index(':')
    v.split(':')[0..1].map { |v| parse_unit(v, pages: pages) }

  else
    ret = parse_unit(v, pages: pages)
    [ret, ret]
  end
end

#parse_list(v) ⇒ Object (protected)



94
95
96
# File 'lib/vpsadminos-converter/vz6/config_item.rb', line 94

def parse_list(v)
  v.split(/\s/).map(&:strip).delete_if(&:empty?)
end

#parse_num(v) ⇒ Object (protected)



109
110
111
# File 'lib/vpsadminos-converter/vz6/config_item.rb', line 109

def parse_num(v)
  v.to_i
end

#parse_onoff_list(v) ⇒ Object (protected)



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/vpsadminos-converter/vz6/config_item.rb', line 161

def parse_onoff_list(v)
  Hash[
    parse_list(v).map do |cap|
      name, enabled = cap.split(':')

      case enabled
      when 'on'
        [name.downcase, true]
      when 'off'
        [name.downcase, false]
      else
        fail "unknown mode '#{name}:#{enabled}'"
      end
    end
  ]
end

#parse_unit(v, pages: false) ⇒ Object (protected)



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/vpsadminos-converter/vz6/config_item.rb', line 133

def parse_unit(v, pages: false)
  return 0 if v == 'unlimited'

  n = v.to_i
  suffix = v.strip[-1]
  return n if suffix =~ /^\d+$/

  if suffix.upcase == 'P'
    n

  elsif i = %w(B K M G T).index(suffix.upcase)
    i.times { n *= 1024 }

    if pages
      n / 4096
    else
      n
    end

  else
    fail "unsupported suffix '#{suffix}'"
  end
end

#veid_subst(ctid, v) ⇒ Object (protected)



113
114
115
116
117
# File 'lib/vpsadminos-converter/vz6/config_item.rb', line 113

def veid_subst(ctid, v)
  v.gsub!(/\$VEID/, ctid)
  v.gsub!(/\$\{VEID\}/, ctid)
  v
end