Class: OsCtld::Commands::Base

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

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

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



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

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

.cmdObject



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

def self.cmd
  @cmd
end

.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(cmd_opts = {}, opts = {}) ⇒ Object

Parameters:

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

    command options

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

    options

Options Hash (opts):



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

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

.run!(*args) ⇒ Object



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

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

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

  elsif !ret[:status]
    fail 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, opts = {}) ⇒ Object (protected)



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

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

#call_cmd!(*args) ⇒ Object (protected)



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

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

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

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

  ret
end

#error(msg) ⇒ Object (protected)



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

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

#error!(msg) ⇒ Object (protected)

Raises:



138
139
140
# File 'lib/osctld/commands/base.rb', line 138

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)



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

def handled
  {status: :handled}
end

#indirect?Boolean (protected)

Returns:

  • (Boolean)


148
149
150
# File 'lib/osctld/commands/base.rb', line 148

def indirect?
  @indirect
end

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

Parameters:

  • manipulable (Object, Array<Object>)

Yields:

  • block called with the lock held



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
122
123
124
# File 'lib/osctld/commands/base.rb', line 92

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: block) || (fail '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: block, &codeblock)
  end
end

#manipulation_holderObject



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

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

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



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

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

#progress(msg) ⇒ Object (protected)



142
143
144
145
146
# File 'lib/osctld/commands/base.rb', line 142

def progress(msg)
  if @client_handler && (opts[:progress].nil? || opts[:progress])
    @client_handler.send_update(msg)
  end
end

#request_stopObject

Implement to prematurely stop the client thread



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

def request_stop

end