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, #run_conf, #run_conf_template, #twiddle

Methods included from ResultParser

#eval_result

Constructor Details

#initialize(params = {}) ⇒ Invoker

Returns a new instance of Invoker.



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

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.



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

def domain_host
  @domain_host
end

#domain_serverObject

Returns the value of attribute domain_server.



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

def domain_server
  @domain_server
end

#hostObject (readonly)

Returns the value of attribute host.



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

def host
  @host
end

#passwordObject (readonly)

Returns the value of attribute password.



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

def password
  @password
end

#portObject (readonly)

Returns the value of attribute port.



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

def port
  @port
end

#serverObject (readonly)

Returns the value of attribute server.



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

def server
  @server
end

#userObject (readonly)

Returns the value of attribute user.



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

def user
  @user
end

Instance Method Details

#commandObject



78
79
80
81
82
83
84
# File 'lib/rboss/cli/invoker.rb', line 78

def command
  command = "#{jboss_cli} --connect"
  command << " --controller=#@server" if @server
  command << " --user='#@user'" if @user
  command << " --password='#@password'" if @password
  command
end

#execute(commands) ⇒ Object



149
150
151
152
153
154
# File 'lib/rboss/cli/invoker.rb', line 149

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

#gets_and_invoke(path, operation, parameters) ⇒ Object



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
138
139
140
141
142
143
144
145
146
147
# File 'lib/rboss/cli/invoker.rb', line 101

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 = Yummi::colorize('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 = Yummi::colorize(name, :intense_blue)
      required = detail['required']
      default_value = detail['default']
      input_message << ' | ' << RBoss::Colorizers.type(parameter_type).colorize(parameter_type)
      input_message << ' | ' << Yummi::colorize('Required', :red) if required
      input_message << ' | ' << Yummi::colorize('Optional', :cyan) unless required
      input_message << ' | ' << Yummi::colorize("[#{default_value}]", :blue) if default_value
      input_message << "\n" << Yummi::colorize(detail['description'], 'bold.black')
      puts input_message
      input = get_input
      while required and input.empty?
        puts Yummi::colorize('Required parameter!', :red)
        input = get_input
      end
    end
    if input.empty?
      puts Yummi::colorize('Parameter skipped!', :yellow)
      next
    end
    begin
      builder << {:name => name, :value => parameter_type.convert(input)}
    rescue Exception => e
      puts Yummi::colorize('Your input could not be converted:', :yellow)
      puts Yummi::colorize(e.message, :red)
      puts Yummi::colorize('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}")
  #TODO print a table using the returned parameters
  YAML::dump(result).on_box
end

#invoke(operation, resources, parameters) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rboss/cli/invoker.rb', line 86

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



156
157
158
# File 'lib/rboss/cli/invoker.rb', line 156

def result(commands)
  eval_result(execute commands)
end