Class: OsCtl::Cli::NetInterface

Inherits:
Command
  • Object
show all
Defined in:
lib/osctl/cli/net_interface.rb

Constant Summary collapse

FIELDS =
%i[
  pool
  ctid
  name
  index
  type
  link
  dhcp
  gateways
  veth
  hwaddr
  tx_queues
  rx_queues
  max_tx
  max_rx
  enable
].freeze
FILTERS =
%i[
  type
  link
].freeze
DEFAULT_FIELDS =
%i[
  name
  type
  link
  veth
  enable
  max_tx
  max_rx
].freeze
IP_FIELDS =
%i[
  pool
  ctid
  netif
  version
  addr
].freeze
ROUTE_FIELDS =
%i[
  pool
  ctid
  netif
  version
  addr
  via
].freeze

Instance Method Summary collapse

Methods inherited from Command

#cli_opt, #format_output, #osctld_call, #osctld_fmt, #osctld_open, #osctld_resp, run

Instance Method Details

#create_bridgeObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/osctl/cli/net_interface.rb', line 119

def create_bridge
  require_args!('id', 'name')

  cmd_opts = {
    id: args[0],
    pool: gopts[:pool],
    name: args[1],
    type: 'bridge',
    hwaddr: opts[:hwaddr],
    tx_queues: opts['tx-queues'],
    rx_queues: opts['rx-queues'],
    link: opts[:link],
    dhcp: opts[:dhcp]
  }

  parse_enable(cmd_opts)
  parse_gateway(cmd_opts)
  parse_shaper(cmd_opts)

  osctld_fmt(:netif_create, cmd_opts:)
end

#create_routedObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/osctl/cli/net_interface.rb', line 141

def create_routed
  require_args!('id', 'name')

  cmd_opts = {
    id: args[0],
    pool: gopts[:pool],
    name: args[1],
    type: 'routed',
    hwaddr: opts[:hwaddr],
    tx_queues: opts['tx-queues'],
    rx_queues: opts['rx-queues']
  }

  parse_enable(cmd_opts)
  parse_shaper(cmd_opts)

  osctld_fmt(:netif_create, cmd_opts:)
end

#deleteObject



160
161
162
163
164
165
166
167
# File 'lib/osctl/cli/net_interface.rb', line 160

def delete
  require_args!('id', 'name')
  osctld_fmt(:netif_delete, cmd_opts: {
    id: args[0],
    pool: gopts[:pool],
    name: args[1]
  })
end

#ip_addObject



271
272
273
274
275
276
277
278
279
280
281
# File 'lib/osctl/cli/net_interface.rb', line 271

def ip_add
  require_args!('id', 'name', 'addr')

  osctld_fmt(:netif_ip_add, cmd_opts: {
    id: args[0],
    pool: gopts[:pool],
    name: args[1],
    addr: args[2],
    route: opts['route-as'] || opts[:route]
  })
end

#ip_delObject



283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/osctl/cli/net_interface.rb', line 283

def ip_del
  require_args!('id', 'name', 'addr')

  osctld_fmt(:netif_ip_del, cmd_opts: {
    id: args[0],
    pool: gopts[:pool],
    name: args[1],
    addr: args[2],
    keep_route: opts['keep-route'],
    version: opts[:version]
  })
end

#ip_listObject



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/osctl/cli/net_interface.rb', line 207

def ip_list
  param_selector = OsCtl::Lib::Cli::ParameterSelector.new(
    all_params: IP_FIELDS
  )

  if opts[:list]
    puts param_selector
    return
  end

  sort = opts[:sort] && param_selector.parse_option(opts[:sort])

  cmd_opts = { pool: gopts[:pool] }
  fmt_opts = {
    layout: :columns,
    sort:
  }

  cmd_opts[:id] = args[0] if args[0]
  cmd_opts[:name] = args[1] if args[1]

  fmt_opts[:header] = false if opts['hide-header']

  ret = []
  data = osctld_call(:netif_ip_list, **cmd_opts)

  data.each do |netif|
    [4, 6].each do |ip_v|
      next if opts[:version] && opts[:version] != ip_v

      netif[ip_v.to_s.to_sym].each do |addr|
        ret << {
          pool: netif[:pool],
          ctid: netif[:ctid],
          netif: netif[:netif],
          version: ip_v,
          addr:
        }
      end
    end
  end

  fmt_opts[:cols] = param_selector.parse_option(opts[:output])

  if opts[:output].nil?
    if args.count >= 2
      fmt_opts[:cols].insert(0, :version)
      fmt_opts[:cols].insert(1, :addr)
    elsif args.count >= 1
      fmt_opts[:cols].insert(0, :netif)
      fmt_opts[:cols].insert(1, :version)
      fmt_opts[:cols].insert(2, :addr)
    end
  end

  if sort
    sort.each do |v|
      fmt_opts[:cols] << v unless fmt_opts[:cols].include?(v)
    end
  end

  format_output(ret, **fmt_opts)
end

#listObject



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
# File 'lib/osctl/cli/net_interface.rb', line 56

def list
  param_selector = OsCtl::Lib::Cli::ParameterSelector.new(
    all_params: FIELDS,
    default_params: DEFAULT_FIELDS
  )

  if opts[:list]
    puts param_selector
    return
  end

  sort = opts[:sort] && param_selector.parse_option(opts[:sort])

  cmd_opts = { pool: gopts[:pool] }
  fmt_opts = {
    layout: :columns,
    sort:,
    opts: %i[max_tx max_rx].to_h do |limit|
      [limit, {
        label: limit.to_s.upcase,
        align: 'right',
        display: proc do |v|
          if gopts[:parsable] \
             || gopts[:json] \
             || (!v.is_a?(Integer) && /^\d+$/ !~ v)
            v
          else
            humanize_data(v.to_i)
          end
        end
      }]
    end
  }

  cmd_opts[:id] = args[0] if args[0]

  FILTERS.each do |v|
    next unless opts[v]

    cmd_opts[v] = opts[v].split(',')
  end

  cmd_opts[:ids] = args if args.count > 0
  fmt_opts[:header] = false if opts['hide-header']

  cols = param_selector.parse_option(opts[:output])

  if opts[:output].nil? && opts[:id].nil?
    cols.insert(0, :pool)
    cols.insert(1, :ctid)
  end

  if sort
    sort.each do |v|
      cols << v unless cols.include?(v)
    end
  end

  fmt_opts[:cols] = cols

  osctld_fmt(:netif_list, cmd_opts:, fmt_opts:)
end

#parse_enable(cmd_opts) ⇒ Object (protected)



389
390
391
392
393
394
395
396
397
# File 'lib/osctl/cli/net_interface.rb', line 389

def parse_enable(cmd_opts)
  if opts[:enable] && opts[:disable]
    raise GLI::BadCommandLine, 'set either --enable or --disable, not both'
  elsif opts[:enable] === true
    cmd_opts[:enable] = true
  elsif opts[:disable] === true
    cmd_opts[:enable] = false
  end
end

#parse_gateway(cmd_opts) ⇒ Object (protected)



399
400
401
402
403
404
405
406
# File 'lib/osctl/cli/net_interface.rb', line 399

def parse_gateway(cmd_opts)
  gws = [4, 6].map { |v| [v, "gateway-v#{v}"] }.select { |_v, opt| opts[opt] }
  return if gws.empty?

  cmd_opts[:gateways] = gws.transform_values do |opt|
    opts[opt]
  end
end

#parse_shaper(cmd_opts) ⇒ Object (protected)



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/osctl/cli/net_interface.rb', line 408

def parse_shaper(cmd_opts)
  if opts['max-tx']
    cmd_opts[:max_tx] =
      if opts['max-tx'] == 'unlimited'
        0
      else
        parse_data(opts['max-tx'])
      end
  end

  return unless opts['max-rx']

  cmd_opts[:max_rx] =
    if opts['max-rx'] == 'unlimited'
      0
    else
      parse_data(opts['max-rx'])
    end
end

#renameObject



169
170
171
172
173
174
175
176
177
# File 'lib/osctl/cli/net_interface.rb', line 169

def rename
  require_args!('id', 'old-name', 'new-name')
  osctld_fmt(:netif_rename, cmd_opts: {
    id: args[0],
    pool: gopts[:pool],
    old_name: args[1],
    new_name: args[2]
  })
end

#route_addObject



363
364
365
366
367
368
369
370
371
372
373
# File 'lib/osctl/cli/net_interface.rb', line 363

def route_add
  require_args!('id', 'name', 'addr')

  osctld_fmt(:netif_route_add, cmd_opts: {
    id: args[0],
    pool: gopts[:pool],
    name: args[1],
    addr: args[2],
    via: opts[:via]
  })
end

#route_delObject



375
376
377
378
379
380
381
382
383
384
385
# File 'lib/osctl/cli/net_interface.rb', line 375

def route_del
  require_args!('id', 'name', 'addr')

  osctld_fmt(:netif_route_del, cmd_opts: {
    id: args[0],
    pool: gopts[:pool],
    name: args[1],
    addr: args[2],
    version: opts[:version]
  })
end

#route_listObject



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/osctl/cli/net_interface.rb', line 296

def route_list
  param_selector = OsCtl::Lib::Cli::ParameterSelector.new(
    all_params: ROUTE_FIELDS
  )

  if opts[:list]
    puts param_selector
    return
  end

  sort = opts[:sort] && param_selector.parse_option(opts[:sort])

  cmd_opts = { pool: gopts[:pool] }
  fmt_opts = {
    layout: :columns,
    sort:
  }

  cmd_opts[:id] = args[0] if args[0]
  cmd_opts[:name] = args[1] if args[1]

  fmt_opts[:header] = false if opts['hide-header']

  ret = []
  data = osctld_call(:netif_route_list, **cmd_opts)

  data.each do |netif|
    [4, 6].each do |ip_v|
      next if opts[:version] && opts[:version] != ip_v

      netif[ip_v.to_s.to_sym].each do |addr|
        ret << {
          pool: netif[:pool],
          ctid: netif[:ctid],
          netif: netif[:netif],
          version: ip_v,
          addr: addr[:address],
          via: addr[:via]
        }
      end
    end
  end

  fmt_opts[:cols] = param_selector.parse_option(opts[:output])

  if opts[:output].nil?
    if args.count >= 2
      fmt_opts[:cols].insert(0, :version)
      fmt_opts[:cols].insert(1, :addr)
      fmt_opts[:cols].insert(2, :via)
    elsif args.count >= 1
      fmt_opts[:cols].insert(0, :netif)
      fmt_opts[:cols].insert(1, :version)
      fmt_opts[:cols].insert(2, :addr)
      fmt_opts[:cols].insert(3, :via)
    end
  end

  if sort
    sort.each do |v|
      fmt_opts[:cols] << v unless fmt_opts[:cols].include?(v)
    end
  end

  format_output(ret, **fmt_opts)
end

#setObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/osctl/cli/net_interface.rb', line 179

def set
  require_args!('id', 'name')

  cmd_opts = {
    id: args[0],
    pool: gopts[:pool],
    name: args[1]
  }

  parse_enable(cmd_opts)

  cmd_opts[:hwaddr] = (opts[:hwaddr] == '-' ? nil : opts[:hwaddr]) if opts[:hwaddr]
  cmd_opts[:tx_queues] = opts['tx-queues'] if opts['tx-queues']
  cmd_opts[:rx_queues] = opts['rx-queues'] if opts['rx-queues']
  cmd_opts[:link] = opts[:link] if opts[:link]
  parse_gateway(cmd_opts)

  if opts['enable-dhcp']
    cmd_opts[:dhcp] = true
  elsif opts['disable-dhcp']
    cmd_opts[:dhcp] = false
  end

  parse_shaper(cmd_opts)

  osctld_fmt(:netif_set, cmd_opts:)
end