hammer_cli_katello_bridge 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'hammer_cli'
|
2
|
+
require 'pry'
|
3
|
+
require 'json'
|
4
|
+
require 'logging'
|
5
|
+
|
6
|
+
module HammerCLIKatelloBridge
|
7
|
+
|
8
|
+
class KatelloCommand < HammerCLI::AbstractCommand
|
9
|
+
|
10
|
+
option "--dry-run", :flag, "do not run the command just log what would have been executed"
|
11
|
+
|
12
|
+
class << self
|
13
|
+
attr_accessor :command_prefix
|
14
|
+
end
|
15
|
+
|
16
|
+
@command_prefix = ''
|
17
|
+
|
18
|
+
def execute
|
19
|
+
katello_params = options.select { |k,v| k != 'dry_run' }
|
20
|
+
safe_params = katello_params.map do |k,v|
|
21
|
+
if v.is_a?(TrueClass)
|
22
|
+
param = "--#{k}"
|
23
|
+
else
|
24
|
+
param = "--#{k}='%s'" % v.gsub("'","\\\\'")
|
25
|
+
end
|
26
|
+
param
|
27
|
+
end
|
28
|
+
katello [self.class.command_prefix] + safe_params
|
29
|
+
0
|
30
|
+
end
|
31
|
+
|
32
|
+
protected
|
33
|
+
|
34
|
+
def katello args
|
35
|
+
cml = args.join(' ')
|
36
|
+
|
37
|
+
username = context[:username]
|
38
|
+
password = context[:password]
|
39
|
+
|
40
|
+
username_param = username ? "-u #{username}" : ''
|
41
|
+
password_param = password ? "-p #{password}" : ''
|
42
|
+
|
43
|
+
command = "katello #{username_param} #{password_param} #{cml}"
|
44
|
+
|
45
|
+
logger.info "Passing control to: %s" % command.gsub(/\s\-p\s\S*/, ' -p ***')
|
46
|
+
|
47
|
+
exec command unless dry_run?
|
48
|
+
end
|
49
|
+
|
50
|
+
def logger
|
51
|
+
Logging.logger["KatelloBridge %s" % self.class.command_prefix]
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.load_commands(cli_definition)
|
56
|
+
json = JSON.parse(cli_definition)
|
57
|
+
commands = self.convert_commands(json, '')
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.convert_commands(command_list, prefix)
|
61
|
+
commands = {}
|
62
|
+
command_buffer = []
|
63
|
+
command_list.each do |command|
|
64
|
+
command_name = command['name']
|
65
|
+
commands[command_name] = Class.new KatelloCommand
|
66
|
+
|
67
|
+
command_prefix = prefix.empty? ? command_name : "#{prefix} #{command_name}"
|
68
|
+
commands[command_name].command_prefix = command_prefix
|
69
|
+
command['options'].each do |opt|
|
70
|
+
dest = (opt['dest'] || 'arg').upcase
|
71
|
+
commands[command_name].option opt['names'], dest, opt['description'], :required => opt['required']
|
72
|
+
end
|
73
|
+
self.convert_commands(command['subcommands'], command_prefix).each do |c|
|
74
|
+
commands[command_name].subcommand c[:name], c[:desc], c[:cls]
|
75
|
+
end
|
76
|
+
command_buffer << { :name=>command_name, :desc=>command['description'], :cls=>commands[command_name] }
|
77
|
+
end
|
78
|
+
command_buffer
|
79
|
+
end
|
80
|
+
|
81
|
+
json_file = HammerCLI::Settings[:katello_cli_description]
|
82
|
+
raise "Path to katello CLI description is not set or file does not exist" unless (json_file && File.exist?(json_file))
|
83
|
+
self.load_commands(File.read(json_file)).each do |command|
|
84
|
+
HammerCLI::MainCommand.subcommand command[:name], command[:desc], command[:cls]
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hammer_cli_katello_bridge
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Martin Bačovský
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-08-30 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: json
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: hammer_cli
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: ! "Katello commands for Hammer CLI. This plugin use Katello CLI to run
|
47
|
+
the commands. \n"
|
48
|
+
email: mbacovsk@redhat.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- lib/hammer_cli_katello_bridge.rb
|
54
|
+
- lib/hammer_cli_katello_bridge/version.rb
|
55
|
+
homepage: http://github.com/theforeman/hammer-cli-katello-bridge
|
56
|
+
licenses: []
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ! '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
requirements: []
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 1.8.23
|
76
|
+
signing_key:
|
77
|
+
specification_version: 3
|
78
|
+
summary: Katello commands for hammer-cli
|
79
|
+
test_files: []
|
80
|
+
has_rdoc:
|