Class: Dopi::Command

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
DopCommon::Validator, State
Defined in:
lib/dopi/command.rb,
lib/dopi/command/dummy.rb,
lib/dopi/command/custom.rb,
lib/dopi/command/mco/rpc.rb,
lib/dopi/command/winrm/cmd.rb,
lib/dopi/command/ssh/custom.rb,
lib/dopi/command/ssh/reboot.rb,
lib/dopi/command/winrm/reboot.rb,
lib/dopi/command/ssh/file_deploy.rb,
lib/dopi/command/ssh/file_exists.rb,
lib/dopi/command/ssh/file_replace.rb,
lib/dopi/command/winrm/powershell.rb,
lib/dopi/command/ssh/file_contains.rb,
lib/dopi/command/winrm/file_exists.rb,
lib/dopi/command/ssh/wait_for_login.rb,
lib/dopi/command/winrm/file_contains.rb,
lib/dopi/command/ssh/puppet_agent_run.rb,
lib/dopi/command/winrm/wait_for_login.rb,
lib/dopi/command/winrm/puppet_agent_run.rb

Defined Under Namespace

Modules: Mco Classes: Custom, Dummy, Ssh, Winrm

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from State

#delete_on_signal, #on_signal, #reset_signals, #send_signal, #signal_procs, #signals, #state, #state_add_child, #state_auto_evaluate_children, #state_auto_evaluate_children=, #state_changed, #state_children, #state_children_done?, #state_children_failed?, #state_children_partial?, #state_children_ready?, #state_children_running?, #state_children_running_noop?, #state_children_starting?, #state_done?, #state_fail, #state_failed?, #state_finish, #state_partial?, #state_ready, #state_ready?, #state_reset, #state_reset_with_children, #state_run, #state_run_noop, #state_running?, #state_running_noop?, #state_start, #state_starting?, #to_yaml_properties, #update, #update_mutex

Constructor Details

#initialize(command_parser, step, node, is_verify_command) ⇒ Command

Returns a new instance of Command.



57
58
59
60
61
62
63
64
65
66
# File 'lib/dopi/command.rb', line 57

def initialize(command_parser, step, node, is_verify_command)
  @command_parser    = command_parser
  @step              = step
  @node              = node
  @is_verify_command = is_verify_command
  @hash              = merged_hash
  log(:debug, "Plugin created with merged command hash: #{hash.inspect}")
  # make sure verify commands are initialized as well
  verify_commands
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



55
56
57
# File 'lib/dopi/command.rb', line 55

def hash
  @hash
end

#is_verify_commandObject (readonly)

Returns the value of attribute is_verify_command.



55
56
57
# File 'lib/dopi/command.rb', line 55

def is_verify_command
  @is_verify_command
end

#nodeObject (readonly)

Returns the value of attribute node.



55
56
57
# File 'lib/dopi/command.rb', line 55

def node
  @node
end

Class Method Details

.create_plugin_instance(command_parser, step, node, is_verify_command = false) ⇒ Object



19
20
21
22
23
24
# File 'lib/dopi/command.rb', line 19

def self.create_plugin_instance(command_parser, step, node, is_verify_command = false)
  plugin_type = PluginManager.get_plugin_name(self) + '/'
  plugin_full_name = plugin_type + command_parser.plugin
  Dopi.log.debug("Creating instance of plugin #{plugin_full_name}")
  PluginManager.create_instance(plugin_full_name, command_parser, step, node, is_verify_command)
end

.delete_plugin_default(node_name, key) ⇒ Object

delete a specific default for the node



49
50
51
52
53
# File 'lib/dopi/command.rb', line 49

def self.delete_plugin_default(node_name, key)
  @plugin_defaults ||= {}
  @plugin_defaults[node_name] ||= {}
  @plugin_defaults[node_name].delete(key)
end

.delete_plugin_defaults(node_name) ⇒ Object

delete all the defaults on this plugin for the node



43
44
45
46
# File 'lib/dopi/command.rb', line 43

def self.delete_plugin_defaults(node_name)
  @plugin_defaults ||= {}
  @plugin_defaults[node_name] = {}
end

.inherited(klass) ⇒ Object



15
16
17
# File 'lib/dopi/command.rb', line 15

def self.inherited(klass)
  PluginManager << klass
end

.plugin_defaults(node_name) ⇒ Object



32
33
34
35
# File 'lib/dopi/command.rb', line 32

def self.plugin_defaults(node_name)
  @plugin_defaults ||= {}
  @plugin_defaults[node_name] ||= {}
end

.set_plugin_defaults(node_name, hash) ⇒ Object



26
27
28
29
30
# File 'lib/dopi/command.rb', line 26

def self.set_plugin_defaults(node_name, hash)
  @plugin_defaults ||= {}
  @plugin_defaults[node_name] ||= {}
  @plugin_defaults[node_name].merge!(DopCommon::HashParser.symbolize_keys(hash))
end

.wipe_plugin_defaultsObject

wipe all the defaults for this plugin



38
39
40
# File 'lib/dopi/command.rb', line 38

def self.wipe_plugin_defaults
  @plugin_defaults = {}
end

Instance Method Details

#load_state(state_hash) ⇒ Object



147
148
149
150
151
152
153
# File 'lib/dopi/command.rb', line 147

def load_state(state_hash)
  command_state = state_hash[:command_state] || :ready
  unless command_state == :ready
    @state = command_state
    state_changed
  end
end

#merged_hashObject



68
69
70
71
72
73
74
# File 'lib/dopi/command.rb', line 68

def merged_hash
  if @command_parser.hash.kind_of?(Hash)
    self.class.plugin_defaults(@node.name).merge(@command_parser.hash)
  else
    self.class.plugin_defaults(@node.name)
  end
end

#meta_run(noop = false) ⇒ Object



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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/dopi/command.rb', line 79

def meta_run(noop = false)
  return if skip_run?(noop)
  state_run unless noop
  # Nest timeout in itself to fix working in combination with popen3() used
  # by command connectors
  Timeout::timeout(plugin_timeout) do
    Timeout::timeout(plugin_timeout) do
      log(:info, "Running command #{name}") unless @is_verify_command
      if noop
        run_noop
      else
        if run
          if verify_after_run
            verify_commands_ok? or
              raise CommandExecutionError, "Verify commands failed to confirm a successful run"
          end
          state_finish
          log(:info, "#{name} [OK]") if state_done?
        else
          state_fail
          if @is_verify_command
            log(:info, "#{name} [FAILED]")
          else
            log(:error, "#{name} [FAILED]")
            log_file = @step.plan.context_logger.current_log_file
            log(:error, "Check the log file #{log_file} for the full debug log of the node")
          end
        end
      end
    end
  end
rescue GracefulExit
  log(:info, "Command excited gracefuly, resetting to ready")
  state_reset(true) unless noop
rescue Timeout::Error
  log(:error, "Command timed out (plugin_timeout is set to #{plugin_timeout})", false)
  state_fail unless noop
  send_signal(:abort)
rescue CommandExecutionError => e
  log(:error, "Command failed: #{e.message}", false)
  log_file = @step.plan.context_logger.current_log_file
  log(:error, "Check the log file #{log_file} for the full debug log of the node", false)
  Dopi.log.error(e) if DopCommon.config.trace
  state_fail unless noop
rescue => e
  log(:error, "Unexpected error!!! This is a Bug", false)
  Dopi.log.error(e.class)
  Dopi.log.error(e.message)
  Dopi.log.error(e.backtrace)
  state_fail unless noop
  raise e
end

#meta_valid?Boolean

Returns:

  • (Boolean)


132
133
134
135
136
137
138
139
140
141
142
# File 'lib/dopi/command.rb', line 132

def meta_valid?
  validity = valid?
  validity = false unless verify_commands.all? do |verify_command|
    begin
      verify_command.meta_valid?
    rescue PluginLoaderError => e
      Dopi.log.error("Step '#{@step.name}': Can't load plugin #{verify_command.plugin}: #{e.message}")
    end
  end
  validity
end

#state_hashObject



155
156
157
# File 'lib/dopi/command.rb', line 155

def state_hash
  {:command_state => @state}
end