Class: OsCtl::Cli::Group

Inherits:
Command
  • Object
show all
Includes:
Assets, Attributes, CGroupParams, Devices
Defined in:
lib/osctl/cli/group.rb

Constant Summary collapse

FIELDS =
%i[
  pool
  name
  path
  full_path
  cpu_limit
  memory_limit
  swap_limit
] + CGroupParams::CGPARAM_STATS
FILTERS =
%i[
  pool
].freeze
DEFAULT_FIELDS =
%i[
  pool
  name
  memory
  cpu_us
].freeze

Constants included from Devices

Devices::DEVICES_FIELDS

Constants included from CGroupParams

CGroupParams::CGPARAM_DEFAULT_FIELDS, CGroupParams::CGPARAM_FIELDS, CGroupParams::CGPARAM_FILTERS, CGroupParams::CGPARAM_STATS

Instance Method Summary collapse

Methods included from Attributes

#do_set_attr, #do_unset_attr

Methods included from Assets

#print_assets

Methods included from Devices

#do_device_add, #do_device_chmod, #do_device_delete, #do_device_inherit, #do_device_list, #do_device_promote, #do_device_replace, #do_device_set_inherit, #do_device_unset_inherit

Methods included from CGroupParams

#cg_add_raw_cgroup_params, #cg_add_stats, #cg_init_subsystems, #cg_list_raw_cgroup_params, #do_cgparam_apply, #do_cgparam_list, #do_cgparam_replace, #do_cgparam_set, #do_cgparam_unset, #do_set_cpu_limit, #do_set_memory, #do_unset_cpu_limit, #do_unset_memory, #parse_cgparams, #parse_subsystem

Methods inherited from Command

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

Instance Method Details

#assetsObject



251
252
253
254
# File 'lib/osctl/cli/group.rb', line 251

def assets
  require_args!('name')
  print_assets(:group_assets, name: args[0], pool: gopts[:pool])
end

#cgparam_applyObject



271
272
273
274
# File 'lib/osctl/cli/group.rb', line 271

def cgparam_apply
  require_args!('name')
  do_cgparam_apply(:group_cgparam_apply, name: args[0], pool: gopts[:pool])
end

#cgparam_listObject



256
257
258
259
# File 'lib/osctl/cli/group.rb', line 256

def cgparam_list
  require_args!('name', strict: false)
  do_cgparam_list(:group_cgparam_list, name: args[0], pool: gopts[:pool])
end

#cgparam_replaceObject



276
277
278
279
# File 'lib/osctl/cli/group.rb', line 276

def cgparam_replace
  require_args!('name')
  do_cgparam_replace(:group_cgparam_replace, name: args[0], pool: gopts[:pool])
end

#cgparam_setObject



261
262
263
264
# File 'lib/osctl/cli/group.rb', line 261

def cgparam_set
  require_args!('name', 'parameter', 'value', strict: false)
  do_cgparam_set(:group_cgparam_set, name: args[0], pool: gopts[:pool])
end

#cgparam_unsetObject



266
267
268
269
# File 'lib/osctl/cli/group.rb', line 266

def cgparam_unset
  require_args!('name', 'parameter')
  do_cgparam_unset(:group_cgparam_unset, name: args[0], pool: gopts[:pool])
end

#createObject



181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/osctl/cli/group.rb', line 181

def create
  require_args!('name')

  cmd_opts = {
    name: args[0],
    pool: opts[:pool] || gopts[:pool],
    parents: opts[:parents],
    cgparams: parse_cgparams
  }

  osctld_fmt(:group_create, cmd_opts:)
end

#deleteObject



194
195
196
197
# File 'lib/osctl/cli/group.rb', line 194

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

#device_addObject



286
287
288
289
# File 'lib/osctl/cli/group.rb', line 286

def device_add
  require_args!('name', 'type', 'major', 'minor', 'mode', optional: %w[device])
  do_device_add(:group_device_add, name: args[0], pool: gopts[:pool])
end

#device_chmodObject



296
297
298
299
# File 'lib/osctl/cli/group.rb', line 296

def device_chmod
  require_args!('name', 'type', 'major', 'minor', 'mode')
  do_device_chmod(:group_device_chmod, name: args[0], pool: gopts[:pool])
end

#device_deleteObject



291
292
293
294
# File 'lib/osctl/cli/group.rb', line 291

def device_delete
  require_args!('name', 'type', 'major', 'minor')
  do_device_delete(:group_device_delete, name: args[0], pool: gopts[:pool])
end

#device_inheritObject



306
307
308
309
# File 'lib/osctl/cli/group.rb', line 306

def device_inherit
  require_args!('name', 'type', 'major', 'minor')
  do_device_inherit(:group_device_inherit, name: args[0], pool: gopts[:pool])
end

#device_listObject



281
282
283
284
# File 'lib/osctl/cli/group.rb', line 281

def device_list
  require_args!('name')
  do_device_list(:group_device_list, name: args[0], pool: gopts[:pool])
end

#device_promoteObject



301
302
303
304
# File 'lib/osctl/cli/group.rb', line 301

def device_promote
  require_args!('name', 'type', 'major', 'minor')
  do_device_chmod(:group_device_promote, name: args[0], pool: gopts[:pool])
end

#device_replaceObject



321
322
323
324
# File 'lib/osctl/cli/group.rb', line 321

def device_replace
  require_args!('name')
  do_device_replace(:group_device_replace, name: args[0], pool: gopts[:pool])
end

#device_set_inheritObject



311
312
313
314
# File 'lib/osctl/cli/group.rb', line 311

def device_set_inherit
  require_args!('name', 'type', 'major', 'minor')
  do_device_set_inherit(:group_device_set_inherit, name: args[0], pool: gopts[:pool])
end

#device_unset_inheritObject



316
317
318
319
# File 'lib/osctl/cli/group.rb', line 316

def device_unset_inherit
  require_args!('name', 'type', 'major', 'minor')
  do_device_unset_inherit(:group_device_unset_inherit, name: args[0], pool: gopts[:pool])
end

#listObject



34
35
36
37
38
39
40
41
42
43
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/osctl/cli/group.rb', line 34

def list
  c = osctld_open
  cg_init_subsystems(c)

  cgparams = cg_list_raw_cgroup_params

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

  if opts[:list]
    puts param_selector
    return
  end

  cmd_opts = {}
  fmt_opts = {
    layout: :columns,
    sort: opts[:sort] && param_selector.parse_option(opts[:sort]),
    opts: {
      memory_limit: {
        align: 'right',
        display: proc do |v|
          if v.nil? || gopts[:parsable] || gopts[:json]
            v
          else
            humanize_data(v)
          end
        end
      },
      swap_limit: {
        align: 'right',
        display: proc do |v|
          if v.nil? || gopts[:parsable] || gopts[:json]
            v
          else
            humanize_data(v)
          end
        end
      }
    }
  }

  cmd_opts[:names] = args if args.count > 0

  FILTERS.each do |v|
    [gopts, opts].each do |options|
      next unless options[v]

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

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

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

  groups = cg_add_stats(
    c.cmd_data!(:group_list, **cmd_opts),
    ->(g) { g[:full_path] },
    cols,
    gopts[:parsable]
  )
  c.close

  cg_add_raw_cgroup_params(
    groups,
    ->(g) { g[:full_path] },
    cols & cgparams.map(&:to_sym)
  )

  format_output(groups, **fmt_opts)
end

#set_attrObject



232
233
234
235
236
237
238
239
240
# File 'lib/osctl/cli/group.rb', line 232

def set_attr
  require_args!('name', 'attribute', 'value')
  do_set_attr(
    :group_set,
    { name: args[0], pool: gopts[:pool] },
    args[1],
    args[2]
  )
end

#set_cpu_limitObject



199
200
201
202
# File 'lib/osctl/cli/group.rb', line 199

def set_cpu_limit
  require_args!('name', 'limit')
  do_set_cpu_limit(:group_cgparam_set, name: args[0], pool: gopts[:pool])
end

#set_memory_limitObject



213
214
215
216
217
218
219
220
221
# File 'lib/osctl/cli/group.rb', line 213

def set_memory_limit
  require_args!('name', 'memory', optional: %w[swap])
  do_set_memory(
    :group_cgparam_set,
    :group_cgparam_unset,
    name: args[0],
    pool: gopts[:pool]
  )
end

#showObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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
# File 'lib/osctl/cli/group.rb', line 115

def show
  c = osctld_open
  cg_init_subsystems(c)

  cgparams = cg_list_raw_cgroup_params

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

  if opts[:list]
    puts param_selector
    return
  end

  require_args!('name')

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

  fmt_opts = {
    layout: :rows,
    cols:,
    opts: {
      memory_limit: {
        align: 'right',
        display: proc do |v|
          if v.nil? || gopts[:parsable] || gopts[:json]
            v
          else
            humanize_data(v)
          end
        end
      },
      swap_limit: {
        align: 'right',
        display: proc do |v|
          if v.nil? || gopts[:parsable] || gopts[:json]
            v
          else
            humanize_data(v)
          end
        end
      }
    }
  }
  fmt_opts[:header] = false if opts['hide-header']

  group = c.cmd_data!(:group_show, name: args[0], pool: gopts[:pool])
  cg_add_stats(
    group,
    group[:full_path],
    cols,
    gopts[:parsable]
  )
  c.close

  cg_add_raw_cgroup_params(
    group,
    group[:full_path],
    cols & cgparams.map(&:to_sym)
  )

  format_output(group, **fmt_opts)
end

#treeObject



110
111
112
113
# File 'lib/osctl/cli/group.rb', line 110

def tree
  require_args!('pool')
  Tree.print(args[0], parsable: gopts[:parsable], color: gopts[:color])
end

#unset_attrObject



242
243
244
245
246
247
248
249
# File 'lib/osctl/cli/group.rb', line 242

def unset_attr
  require_args!('name', 'attribute')
  do_unset_attr(
    :group_unset,
    { name: args[0], pool: gopts[:pool] },
    args[1]
  )
end

#unset_cpu_limitObject



204
205
206
207
208
209
210
211
# File 'lib/osctl/cli/group.rb', line 204

def unset_cpu_limit
  require_args!('name')
  do_unset_cpu_limit(
    :group_cgparam_unset,
    name: args[0],
    pool: gopts[:pool]
  )
end

#unset_memory_limitObject



223
224
225
226
227
228
229
230
# File 'lib/osctl/cli/group.rb', line 223

def unset_memory_limit
  require_args!('name')
  do_unset_memory(
    :group_cgparam_unset,
    name: args[0],
    pool: gopts[:pool]
  )
end