Class: Puppet_X::Coi::Jboss::Internal::CliExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_x/coi/jboss/internal/cli_executor.rb

Overview

Class that will handle executions of commands

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(execution_state_wrapper) ⇒ CliExecutor

Constructor

Parameters:



5
6
7
8
# File 'lib/puppet_x/coi/jboss/internal/cli_executor.rb', line 5

def initialize(execution_state_wrapper)
  @execution_state_wrapper = execution_state_wrapper
  @sanitizer = Puppet_X::Coi::Jboss::Internal::Sanitizer.new
end

Instance Attribute Details

#execution_state_wrapper=(value) ⇒ Object (writeonly)

Standard settter for execution_state_wrapper



11
12
13
# File 'lib/puppet_x/coi/jboss/internal/cli_executor.rb', line 11

def execution_state_wrapper=(value)
  @execution_state_wrapper = value
end

Instance Method Details

#executeAndGet(cmd, runasdomain, ctrlcfg, retry_count, retry_timeout) ⇒ Object

Method that executes command and returns outut

Parameters:

  • cmd (String)

    command that will be executed

  • runasdomain (Boolean)

    if command will be executen in comain instance

  • ctrlcfg (Hash)

    hash with configuration

  • retry_count (Number)

    number of retry after failed command

  • retry_timeout (Number)

    timeout after failed command



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
# File 'lib/puppet_x/coi/jboss/internal/cli_executor.rb', line 46

def executeAndGet(cmd, runasdomain, ctrlcfg, retry_count, retry_timeout)
  ret = run_command(cmd, runasdomain, ctrlcfg, retry_count, retry_timeout)
  unless ret[:result]
    return {
      :result => false,
      :data => ret[:lines]
    }
  end

  begin
    evaluated_output = @sanitizer.sanitize(ret[:lines])
    undefined = nil
    evalines = eval(evaluated_output)
    return {
      :result => evalines['outcome'] == 'success',
      :data => (evalines['outcome'] == 'success' ? evalines['result'] : evalines['failure-description'])
    }

  rescue Exception => e
    Puppet.err e
    return {
      :result => false,
      :data => ret[:lines]
    }
  end
end

#executeWithFail(typename, cmd, way, resource) ⇒ Object

Method that executes command, if method fails it prints log message

Parameters:

  • typename (String)

    name of resource

  • cmd (String)

    command that will be executed

  • way (String)

    bring up|bring down to for logging

  • resource (Hash)

    standard puppet resource object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/puppet_x/coi/jboss/internal/cli_executor.rb', line 28

def executeWithFail(typename, cmd, way, resource)
  executed = wrap_execution(cmd, resource)
  unless executed[:result]
    ex = "\n#{typename} failed #{way}:\n[CLI command]: #{executed[:cmd]}\n[Error message]: #{executed[:lines]}"
    unless $add_log.nil? and $add_log > 0
      ex = "#{ex}\n#{printlog $add_log}"
    end
    raise ex
  end
  executed
end

#prepare_command(path, ctrlcfg) ⇒ Object

Method that prepares command to be executed

Parameters:

  • path (String)

    path for execution

  • ctrlcfg (Hash)

    hash with configuration that is need to execute command



76
77
78
79
80
81
82
83
84
85
# File 'lib/puppet_x/coi/jboss/internal/cli_executor.rb', line 76

def prepare_command(path, ctrlcfg)
  home = Puppet_X::Coi::Jboss::Configuration.config_value :home
  ENV['JBOSS_HOME'] = home

  jboss_home = "#{home}/bin/jboss-cli.sh"

  cmd = "#{jboss_home} #{timeout_cli} --connect --file=#{path} --controller=#{ctrlcfg[:controller]}"
  cmd += " --user=#{ctrlcfg[:ctrluser]}" unless ctrlcfg[:ctrluser].nil?
  cmd
end

#run_command(jbosscmd, runasdomain, ctrlcfg, retry_count, retry_timeout) ⇒ Hash

Method that will prepare and delegate execution of command

Parameters:

  • jbosscmd (String)

    command to be executeAndGet

  • runasdomain (Boolean)

    if jboss is run in domain mode

  • ctrlcfg (Hash)

    configuration Hash

  • retry_count (Integer)

    number of retries after command failure-description

  • retry_timeout (Integer)

    time after command is timeouted

Returns:

  • (Hash)

    hash with result of command executed, output and command



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
131
132
133
134
135
136
137
# File 'lib/puppet_x/coi/jboss/internal/cli_executor.rb', line 94

def run_command(jbosscmd, runasdomain, ctrlcfg, retry_count, retry_timeout)
  file = Tempfile.new 'jbosscli'
  path = file.path
  file.close
  file.unlink

  File.open(path, 'w') { |f| f.write(jbosscmd + "\n") }

  cmd = prepare_command(path, ctrlcfg)

  environment = ENV.to_hash

  unless ctrlcfg[:ctrlpasswd].nil?
    environment['__PASSWD'] = ctrlcfg[:ctrlpasswd]
    cmd += ' --password=$__PASSWD'
  end

  retries = 0
  result = ''
  lines = ''
  begin
    if retries > 0
      Puppet.warning "JBoss CLI command failed, try #{retries}/#{retry_count}, last status: #{result}, message: #{lines}"
      sleep retry_timeout.to_i
    end

    Puppet.debug 'Command send to JBoss CLI: ' + jbosscmd
    Puppet.debug('Cmd to be executed %s' % cmd)

    execution_state = @execution_state_wrapper.execute(cmd, jbosscmd, environment)
    result = execution_state.ret_code
    lines = execution_state.output

    retries += 1
  end while (result != 0 && retries <= retry_count)
  Puppet.debug('Output from JBoss CLI [%s]: %s' % [result.inspect, lines])
  # deletes the temp file
  File.unlink path
  {
    :cmd => jbosscmd,
    :result => result,
    :lines => lines
  }
end

#shell_executorObject

Standard getter for shell_executor



19
20
21
# File 'lib/puppet_x/coi/jboss/internal/cli_executor.rb', line 19

def shell_executor
  @execution_state_wrapper.shell_executor
end

#shell_executor=(shell_executor) ⇒ Object

Method that allows us to setup shell executor, used in tests



14
15
16
# File 'lib/puppet_x/coi/jboss/internal/cli_executor.rb', line 14

def shell_executor=(shell_executor)
  @execution_state_wrapper.shell_executor = shell_executor
end