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
)
FILTERS =
%i(
  type
  link
)
DEFAULT_FIELDS =
%i(
  name
  type
  link
  veth
)
IP_FIELDS =
%i(
  pool
  ctid
  netif
  version
  addr
)
ROUTE_FIELDS =
%i(
  pool
  ctid
  netif
  version
  addr
  via
)

Instance Attribute Summary

Attributes inherited from Command

#args, #gopts, #opts

Instance Method Summary collapse

Methods inherited from Command

#cli_opt, #format_output, #initialize, #osctld_call, #osctld_fmt, #osctld_open, #osctld_resp, #require_args!, run

Methods included from Utils::Humanize

#break_interval, #format_long_duration, #format_percent, #format_short_duration, #humanize_data, #humanize_time_ns, #parse_data

Constructor Details

This class inherits a constructor from OsCtl::Cli::Command

Instance Method Details

#create_bridgeObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/osctl/cli/net_interface.rb', line 83

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

  cmd_opts = {
    id: args[0],
    pool: gopts[:pool],
    name: args[1],
    type: 'bridge',
    hwaddr: opts[:hwaddr],
    link: opts[:link],
    dhcp: opts[:dhcp],
  }

  parse_gateway(cmd_opts)

  osctld_fmt(:netif_create, cmd_opts)
end

#create_routedObject



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/osctl/cli/net_interface.rb', line 101

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

  cmd_opts = {
    id: args[0],
    pool: gopts[:pool],
    name: args[1],
    type: 'routed',
    hwaddr: opts[:hwaddr],
  }

  osctld_fmt(:netif_create, cmd_opts)
end

#deleteObject



115
116
117
118
# File 'lib/osctl/cli/net_interface.rb', line 115

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

#ip_addObject



205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/osctl/cli/net_interface.rb', line 205

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

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

#ip_delObject



218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/osctl/cli/net_interface.rb', line 218

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

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

#ip_listObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
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
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/osctl/cli/net_interface.rb', line 153

def ip_list
  if opts[:list]
    puts IP_FIELDS.join("\n")
    return
  end

  cmd_opts = {pool: gopts[:pool]}
  fmt_opts = {
    layout: :columns,
    sort: opts[:sort] && opts[:sort].split(',').map(&:to_sym),
  }

  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: addr,
        }
      end
    end
  end

  if opts[:output]
    cols = opts[:output].split(',').map(&:to_sym)

  elsif args.count >= 2
    cols = %i(version addr)

  elsif args.count >= 1
    cols = %i(netif version addr)

  else
    cols = IP_FIELDS
  end

  format_output(ret, cols, fmt_opts)
end

#listObject



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

def list
  if opts[:list]
    puts FIELDS.join("\n")
    return
  end

  cmd_opts = {pool: gopts[:pool]}
  fmt_opts = {
    layout: :columns,
    sort: opts[:sort] && opts[:sort].split(',').map(&:to_sym),
  }

  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']

  if opts[:output]
    cols = opts[:output].split(',').map(&:to_sym)

  elsif opts[:id]
    cols = DEFAULT_FIELDS

  else
    cols = %i(pool ctid) + DEFAULT_FIELDS
  end

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

#parse_gateway(cmd_opts) ⇒ Object (protected)



312
313
314
315
316
317
318
319
# File 'lib/osctl/cli/net_interface.rb', line 312

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] = Hash[gws.map do |v, opt|
    [v, opts[opt]]
  end]
end

#renameObject



120
121
122
123
124
125
126
127
128
129
# File 'lib/osctl/cli/net_interface.rb', line 120

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

#route_addObject



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

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

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

#route_delObject



298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/osctl/cli/net_interface.rb', line 298

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

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

#route_listObject



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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/osctl/cli/net_interface.rb', line 232

def route_list
  if opts[:list]
    puts ROUTE_FIELDS.join("\n")
    return
  end

  cmd_opts = {pool: gopts[:pool]}
  fmt_opts = {
    layout: :columns,
    sort: opts[:sort] && opts[:sort].split(',').map(&:to_sym),
  }

  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

  if opts[:output]
    cols = opts[:output].split(',').map(&:to_sym)

  elsif args.count >= 2
    cols = %i(version addr via)

  elsif args.count >= 1
    cols = %i(netif version addr via)

  else
    cols = ROUTE_FIELDS
  end

  format_output(ret, cols, fmt_opts)
end

#setObject



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

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[: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

  osctld_fmt(:netif_set, cmd_opts)
end