Class: RBoss::Cli::Invoker

Inherits:
Object
  • Object
show all
Includes:
Mappings, ResultParser, Platform
Defined in:
lib/rboss/cli/invoker.rb

Constant Summary

Constants included from ResultParser

ResultParser::BOOLEAN, ResultParser::INT, ResultParser::LIST, ResultParser::LONG, ResultParser::OBJECT, ResultParser::STRING

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mappings

load_default_resources, load_resource, load_resources, resource_mappings

Methods included from Platform

#clear, #jboss_cli

Methods included from ResultParser

#eval_result, #format

Constructor Details

#initialize(params = {}) ⇒ Invoker

Returns a new instance of Invoker.



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
# File 'lib/rboss/cli/invoker.rb', line 43

def initialize(params = {})
  params = {
    :jboss_home => ENV['JBOSS_HOME'],
  }.merge! params

  @jboss_home = params[:jboss_home]

  @server = params[:server]
  @host = params[:host]
  @port = params[:port]
  @server ||= [@host, @port].join ':' if @host and @port

  @user = params[:user]
  @password = params[:password]

  @logger = params[:logger]
  unless @logger
    @logger = Logger::new STDOUT
    @logger.level = params[:log_level] || Logger::INFO
    formatter = Yummi::Formatter::LogFormatter.new do |severity, message|
      "#{severity} : #{message}"
    end
    @logger.formatter = formatter
  end

  @skip_optional = params[:skip_optional]
end

Instance Attribute Details

#domain_hostObject

Returns the value of attribute domain_host.



41
42
43
# File 'lib/rboss/cli/invoker.rb', line 41

def domain_host
  @domain_host
end

#domain_serverObject

Returns the value of attribute domain_server.



41
42
43
# File 'lib/rboss/cli/invoker.rb', line 41

def domain_server
  @domain_server
end

#hostObject (readonly)

Returns the value of attribute host.



40
41
42
# File 'lib/rboss/cli/invoker.rb', line 40

def host
  @host
end

#passwordObject (readonly)

Returns the value of attribute password.



40
41
42
# File 'lib/rboss/cli/invoker.rb', line 40

def password
  @password
end

#portObject (readonly)

Returns the value of attribute port.



40
41
42
# File 'lib/rboss/cli/invoker.rb', line 40

def port
  @port
end

#serverObject (readonly)

Returns the value of attribute server.



40
41
42
# File 'lib/rboss/cli/invoker.rb', line 40

def server
  @server
end

#userObject (readonly)

Returns the value of attribute user.



40
41
42
# File 'lib/rboss/cli/invoker.rb', line 40

def user
  @user
end

Instance Method Details

#commandObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rboss/cli/invoker.rb', line 83

def command
  command = "#{jboss_cli} --connect"
  command << " --controller=#@server" if @server
  if @user
    command << " --user='#@user'"
    unless @password
      puts 'Input server password: '.blue.bold
      @password = STDIN.noecho(&:gets).chomp
      puts 'Password saved for this session!'.green
    end
    command << " --password='#@password'"
  end
  command
end

#execute(commands) ⇒ Object



160
161
162
163
164
165
166
167
# File 'lib/rboss/cli/invoker.rb', line 160

def execute(commands)
  commands = [commands] if commands.is_a? String
  exec = "#{command} --command#{commands.size > 1 ? 's' : ''}=\"#{commands.join ','}\""
  @logger.debug exec
  result = `#{exec}`.chomp
  @logger.debug result
  result
end

#gets_and_invoke(path, operation, parameters) ⇒ Object



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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/rboss/cli/invoker.rb', line 113

def gets_and_invoke(path, operation, parameters)
  result = result("#{path}:read-operation-description(name=#{operation})")
  props = result['request-properties']
  props ||= {}
  builder = CommandBuilder::new operation
  help_printed = false
  info = 'Please input the requested parameters'.yellow
  props.each do |name, detail|
    next if (@skip_optional and (not detail['required'] or detail['default']))
    parameter_type = detail['type']
    input = parameters[name]
    unless input
      puts info unless help_printed
      help_printed = true
      input_message = name.bold.blue
      required = detail['required']
      default_value = detail['default']
      input_message << ' | ' << RBoss::Colorizers.type(parameter_type).colorize(parameter_type)
      input_message << ' | ' << 'Required'.red if required
      input_message << ' | ' << 'Optional'.cyan unless required
      input_message << ' | ' << "[#{default_value}]".blue if default_value
      input_message << "\n" << detail['description'].bold
      puts input_message
      input = get_input
      while required and input.empty?
        puts 'Required parameter!'.red
        input = get_input
      end
    end
    if input.empty?
      puts 'Parameter skipped!'.yellow
      next
    end
    begin
      builder << {:name => name, :value => parameter_type.convert(input)}
    rescue Exception => e
      puts 'Your input could not be converted:'.yellow
      puts e.message.red
      puts 'This will not affect other inputs, you can continue to input other values.'.yellow
      puts 'Press ENTER to continue'
      gets
    end
  end
  result = result("#{path}:#{builder}")
  format result
end

#invoke(operation, resources, parameters) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rboss/cli/invoker.rb', line 98

def invoke(operation, resources, parameters)
  buff = ''
  resources.each do |key, resource_names|
    if resource_mappings.has_key? key
      mapping = resource_mappings[key]
      resource = RBoss::Cli::Resource::new(self, mapping)
      resource.context[:domain_host] = @domain_host
      resource.context[:domain_server] = @domain_server
      result = resource.invoke(operation, resource_names, parameters)
      buff << result.to_s if result
    end
  end
  buff
end

#result(commands) ⇒ Object



169
170
171
# File 'lib/rboss/cli/invoker.rb', line 169

def result(commands)
  eval_result(execute commands)
end