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)



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

def parse(ctid, v)
  # rubocop:disable Lint/DuplicateBranch

  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

  # rubocop:enable Lint/DuplicateBranch
end

#parse_addrs(v) ⇒ Object (protected)



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

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

#parse_bool(v) ⇒ Object (protected)



103
104
105
106
107
108
109
110
111
112
# File 'lib/vpsadminos-converter/vz6/config_item.rb', line 103

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

#parse_devices(v) ⇒ Object (protected)



162
163
164
# File 'lib/vpsadminos-converter/vz6/config_item.rb', line 162

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

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



128
129
130
131
132
133
134
135
136
# File 'lib/vpsadminos-converter/vz6/config_item.rb', line 128

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

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

#parse_list(v) ⇒ Object (protected)



99
100
101
# File 'lib/vpsadminos-converter/vz6/config_item.rb', line 99

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

#parse_num(v) ⇒ Object (protected)



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

def parse_num(v)
  v.to_i
end

#parse_onoff_list(v) ⇒ Object (protected)



166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/vpsadminos-converter/vz6/config_item.rb', line 166

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

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

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



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/vpsadminos-converter/vz6/config_item.rb', line 138

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
    raise "unsupported suffix '#{suffix}'"
  end
end

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



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

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