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
].freeze
FILTERS =
%i[
  type
  link
].freeze
DEFAULT_FIELDS =
%i[
  name
  type
  link
  veth
  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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/osctl/cli/net_interface.rb', line 109

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_gateway(cmd_opts)
  parse_shaper(cmd_opts)

  osctld_fmt(:netif_create, cmd_opts:)
end

#create_routedObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/osctl/cli/net_interface.rb', line 130

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_shaper(cmd_opts)

  osctld_fmt(:netif_create, cmd_opts:)
end

#deleteObject



148
149
150
151
152
153
154
155
# File 'lib/osctl/cli/net_interface.rb', line 148

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

#ip_addObject



249
250
251
252
253
254
255
256
257
258
259
# File 'lib/osctl/cli/net_interface.rb', line 249

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



261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/osctl/cli/net_interface.rb', line 261

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



193
194
195
196
197
198
199
200
201
202
203
204
205
206
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
# File 'lib/osctl/cli/net_interface.rb', line 193

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

  if opts[:list]
    puts param_selector
    return
  end

  cmd_opts = { pool: gopts[:pool] }
  fmt_opts = {
    layout: :columns,
    sort: opts[:sort] && param_selector.parse_option(opts[: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

  format_output(ret, **fmt_opts)
end

#listObject



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

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

  if opts[:list]
    puts param_selector
    return
  end

  cmd_opts = { pool: gopts[:pool] }
  fmt_opts = {
    layout: :columns,
    sort: opts[:sort] && param_selector.parse_option(opts[: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

  fmt_opts[:cols] = cols

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

#parse_gateway(cmd_opts) ⇒ Object (protected)



359
360
361
362
363
364
365
366
# File 'lib/osctl/cli/net_interface.rb', line 359

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)



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/osctl/cli/net_interface.rb', line 368

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



157
158
159
160
161
162
163
164
165
# File 'lib/osctl/cli/net_interface.rb', line 157

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



333
334
335
336
337
338
339
340
341
342
343
# File 'lib/osctl/cli/net_interface.rb', line 333

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



345
346
347
348
349
350
351
352
353
354
355
# File 'lib/osctl/cli/net_interface.rb', line 345

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



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
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
# File 'lib/osctl/cli/net_interface.rb', line 274

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

  if opts[:list]
    puts param_selector
    return
  end

  cmd_opts = { pool: gopts[:pool] }
  fmt_opts = {
    layout: :columns,
    sort: opts[:sort] && param_selector.parse_option(opts[: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

  format_output(ret, **fmt_opts)
end

#setObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/osctl/cli/net_interface.rb', line 167

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

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

  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