Class: OsCtld::Commands::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/osctld/commands/base.rb

Direct Known Subclasses

Container::Assets, Container::Attach, Container::CGParamApply, Container::CGParamList, Container::CGParamReplace, Container::Cat, Container::Console, Container::DeviceList, Container::Exec, Container::Freeze, Container::List, Container::Mount, Container::MountList, Container::Passwd, Container::PrLimitList, Container::Reconfigure, Container::RecoverCleanup, Container::RecoverState, Container::Runscript, Container::SendCancel, Container::SendCleanup, Container::SendConfig, Container::SendNow, Container::SendRootfs, Container::SendState, Container::SendSync, Container::Show, Container::Su, Container::Unfreeze, Container::Wall, CpuScheduler::Disable, CpuScheduler::Enable, CpuScheduler::PackageDisable, CpuScheduler::PackageEnable, CpuScheduler::PackageList, CpuScheduler::Status, CpuScheduler::Upkeep, Dataset::Create, Dataset::Delete, Dataset::List, Debug::LockRegistry, Debug::ThreadList, Debug::UGidkRegistry, Event::Subscribe, GarbageCollector::Prune, Group::Assets, Group::CGParamApply, Group::CGParamList, Group::CGParamReplace, Group::CGSubsystems, Group::DeviceList, Group::List, Group::Show, History::List, IdRange::Assets, IdRange::List, IdRange::Show, IdRange::TableList, IdRange::TableShow, Logged, NetInterface::IpList, NetInterface::List, NetInterface::RouteList, NetInterface::Show, Pool::AbortExport, Pool::Assets, Pool::AutoStartCancel, Pool::AutoStartQueue, Pool::AutoStartTrigger, Pool::Export, Pool::Import, Pool::Install, Pool::List, Pool::Show, Pool::Uninstall, Receive::AuthKeyList, Repository::Assets, Repository::ImageList, Repository::List, Repository::Show, Self::AbortShutdown, Self::Activate, Self::Assets, Self::HealthCheck, Self::Ping, Self::Shutdown, Self::Status, Send::KeyPath, TrashBin::DatasetAdd, TrashBin::Prune, User::Assets, User::IdMapList, User::List, User::LxcUsernet, User::Register, User::Setup, User::Show, User::SubUGIds, User::Unregister, SendReceive::Commands::Base, UserControl::Commands::Base

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd_opts, opts) ⇒ Base

Returns a new instance of Base.



38
39
40
41
42
43
44
# File 'lib/osctld/commands/base.rb', line 38

def initialize(cmd_opts, opts)
  @opts = cmd_opts
  @id = opts[:id]
  @client_handler = opts[:handler]
  @client = @client_handler && @client_handler.socket
  @indirect = opts[:indirect] || false
end

Class Attribute Details

.cmdObject (readonly)

Returns the value of attribute cmd.



9
10
11
# File 'lib/osctld/commands/base.rb', line 9

def cmd
  @cmd
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



36
37
38
# File 'lib/osctld/commands/base.rb', line 36

def client
  @client
end

#client_handlerObject (readonly, protected)

Returns the value of attribute client_handler.



70
71
72
# File 'lib/osctld/commands/base.rb', line 70

def client_handler
  @client_handler
end

#idObject (readonly)

Returns the value of attribute id.



36
37
38
# File 'lib/osctld/commands/base.rb', line 36

def id
  @id
end

#optsObject (readonly)

Returns the value of attribute opts.



36
37
38
# File 'lib/osctld/commands/base.rb', line 36

def opts
  @opts
end

Class Method Details

.handle(name) ⇒ Object



3
4
5
6
# File 'lib/osctld/commands/base.rb', line 3

def self.handle(name)
  @cmd = name
  Command.register(name, self)
end

.run(internal: {}, **kwargs) ⇒ Object

Parameters:

  • kwargs (Hash)

    command options

  • internal (Hash) (defaults to: {})

    internal options

Options Hash (internal:):



17
18
19
20
21
# File 'lib/osctld/commands/base.rb', line 17

def self.run(internal: {}, **kwargs)
  kwargs[:id] ||= Command.get_id
  c = new(kwargs, internal)
  c.base_execute
end

.run!Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/osctld/commands/base.rb', line 23

def self.run!(**)
  ret = run(**)

  if !ret.is_a?(Hash)
    raise "invalid return value '#{ret.inspect}'"

  elsif !ret[:status]
    raise ret[:message]
  end

  ret
end

Instance Method Details

#base_executeObject

This method is for command templates, do not override it in your command



47
48
49
# File 'lib/osctld/commands/base.rb', line 47

def base_execute
  execute
end

#call_cmd(klass) ⇒ Object (protected)



72
73
74
# File 'lib/osctld/commands/base.rb', line 72

def call_cmd(klass, **)
  klass.run(internal: { handler: client_handler, indirect: true }, **)
end

#call_cmd!Object (protected)



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/osctld/commands/base.rb', line 76

def call_cmd!(*, **)
  ret = call_cmd(*, **)

  if !ret.is_a?(Hash)
    error!("invalid return value '#{ret.inspect}'")

  elsif !ret[:status]
    error!(ret[:message])
  end

  ret
end

#error(msg) ⇒ Object (protected)



131
132
133
# File 'lib/osctld/commands/base.rb', line 131

def error(msg)
  { status: false, message: msg }
end

#error!(msg) ⇒ Object (protected)

Raises:



135
136
137
# File 'lib/osctld/commands/base.rb', line 135

def error!(msg)
  raise CommandFailed, msg
end

#executeObject

Implement this method in your command, or follow instructions from your command template.

Raises:

  • (NotImplementedError)


53
54
55
# File 'lib/osctld/commands/base.rb', line 53

def execute
  raise NotImplementedError
end

#handledObject (protected)



127
128
129
# File 'lib/osctld/commands/base.rb', line 127

def handled
  { status: :handled }
end

#indirect?Boolean (protected)

Returns:

  • (Boolean)


145
146
147
# File 'lib/osctld/commands/base.rb', line 145

def indirect?
  @indirect
end

#manipulate(manipulable) { ... } ⇒ Object (protected)

Parameters:

  • manipulable (Object, Array<Object>)

Yields:

  • block called with the lock held



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
118
119
120
121
# File 'lib/osctld/commands/base.rb', line 91

def manipulate(manipulable, &codeblock)
  block = opts[:manipulation_lock] == 'wait'

  if opts[:manipulation_lock] == 'ignore'
    codeblock.call

  elsif manipulable.is_a?(Array)
    locked = []

    # Acquire all locks
    begin
      manipulable.each do |m|
        m.acquire_manipulation_lock(self, block:) || (raise 'unable to lock')
        locked << m
      end
    rescue ResourceLocked
      locked.reverse_each(&:release_manipulation_lock)
      raise
    end

    # Call the block and release locks
    begin
      codeblock.call
    ensure
      locked.reverse_each(&:release_manipulation_lock)
    end

  else
    manipulable.manipulate(self, block:, &codeblock)
  end
end

#manipulation_holderObject



60
61
62
63
64
65
66
# File 'lib/osctld/commands/base.rb', line 60

def manipulation_holder
  if opts[:cli]
    "'#{opts[:cli]}'"
  else
    self.class.cmd.to_s
  end
end

#ok(resp = nil) ⇒ Object (protected)



123
124
125
# File 'lib/osctld/commands/base.rb', line 123

def ok(resp = nil)
  { status: true, output: resp }
end

#progress(msg) ⇒ Object (protected)



139
140
141
142
143
# File 'lib/osctld/commands/base.rb', line 139

def progress(msg)
  return unless @client_handler && (opts[:progress].nil? || opts[:progress])

  @client_handler.send_update(msg)
end

#request_stopObject

Implement to prematurely stop the client thread



58
# File 'lib/osctld/commands/base.rb', line 58

def request_stop; end