hammer_cli 0.0.18 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +3 -314
  3. data/bin/hammer +45 -6
  4. data/config/cli.modules.d/module_config_template.yml +4 -0
  5. data/config/cli_config.template.yml +9 -11
  6. data/doc/developer_docs.md +1 -0
  7. data/doc/i18n.md +85 -0
  8. data/doc/installation.md +321 -0
  9. data/lib/hammer_cli.rb +3 -0
  10. data/lib/hammer_cli/abstract.rb +15 -24
  11. data/lib/hammer_cli/apipie/command.rb +13 -7
  12. data/lib/hammer_cli/apipie/options.rb +14 -16
  13. data/lib/hammer_cli/apipie/read_command.rb +6 -1
  14. data/lib/hammer_cli/apipie/resource.rb +48 -58
  15. data/lib/hammer_cli/apipie/write_command.rb +5 -1
  16. data/lib/hammer_cli/completer.rb +77 -21
  17. data/lib/hammer_cli/connection.rb +44 -0
  18. data/lib/hammer_cli/exception_handler.rb +15 -4
  19. data/lib/hammer_cli/exceptions.rb +6 -0
  20. data/lib/hammer_cli/i18n.rb +95 -0
  21. data/lib/hammer_cli/logger.rb +3 -3
  22. data/lib/hammer_cli/main.rb +12 -11
  23. data/lib/hammer_cli/modules.rb +19 -6
  24. data/lib/hammer_cli/options/normalizers.rb +42 -7
  25. data/lib/hammer_cli/options/option_definition.rb +2 -2
  26. data/lib/hammer_cli/output.rb +1 -0
  27. data/lib/hammer_cli/output/adapter/abstract.rb +20 -0
  28. data/lib/hammer_cli/output/adapter/base.rb +49 -78
  29. data/lib/hammer_cli/output/adapter/csv.rb +5 -5
  30. data/lib/hammer_cli/output/adapter/table.rb +41 -10
  31. data/lib/hammer_cli/output/dsl.rb +1 -1
  32. data/lib/hammer_cli/output/field_filter.rb +21 -0
  33. data/lib/hammer_cli/output/fields.rb +44 -78
  34. data/lib/hammer_cli/output/formatters.rb +38 -0
  35. data/lib/hammer_cli/settings.rb +28 -6
  36. data/lib/hammer_cli/shell.rb +58 -57
  37. data/lib/hammer_cli/utils.rb +14 -0
  38. data/lib/hammer_cli/validator.rb +5 -5
  39. data/lib/hammer_cli/version.rb +1 -1
  40. data/locale/Makefile +64 -0
  41. data/locale/hammer-cli.pot +203 -0
  42. data/locale/zanata.xml +29 -0
  43. data/test/unit/apipie/command_test.rb +42 -25
  44. data/test/unit/apipie/read_command_test.rb +10 -7
  45. data/test/unit/apipie/write_command_test.rb +9 -8
  46. data/test/unit/completer_test.rb +206 -21
  47. data/test/unit/connection_test.rb +68 -0
  48. data/test/unit/fixtures/apipie/architectures.json +153 -0
  49. data/test/unit/fixtures/apipie/documented.json +79 -0
  50. data/test/unit/fixtures/json_input/invalid.json +12 -0
  51. data/test/unit/fixtures/json_input/valid.json +12 -0
  52. data/test/unit/history_test.rb +71 -0
  53. data/test/unit/main_test.rb +9 -0
  54. data/test/unit/modules_test.rb +22 -6
  55. data/test/unit/options/field_filter_test.rb +27 -0
  56. data/test/unit/options/normalizers_test.rb +53 -0
  57. data/test/unit/output/adapter/base_test.rb +162 -10
  58. data/test/unit/output/adapter/csv_test.rb +16 -3
  59. data/test/unit/output/adapter/table_test.rb +97 -13
  60. data/test/unit/output/dsl_test.rb +74 -6
  61. data/test/unit/output/fields_test.rb +93 -62
  62. data/test/unit/output/formatters_test.rb +47 -0
  63. data/test/unit/settings_test.rb +35 -4
  64. data/test/unit/utils_test.rb +45 -0
  65. metadata +85 -4
  66. data/test/unit/apipie/fake_api.rb +0 -101
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4e015fb0516ebe432c0ce953815b7dda042fdac4
4
- data.tar.gz: bcd2facb22a2ab15dc7289141551354e5026f533
3
+ metadata.gz: 83ee93e64de515a960acf80a4ed0452d700fb5cd
4
+ data.tar.gz: fb55752f1a394e1261a342bf3d8e9a68ed98386f
5
5
  SHA512:
6
- metadata.gz: 216864d3cb0d81752c118b7993a7c0ef7d4969ca3d9b7ca0ebf20bdf64a3519d85fc9229e18cfaf1ecb8f3b545177e7452c08a199bb691b6707556324458696e
7
- data.tar.gz: 3256dcc6aa084a3a13c8973f02f8aed087e102c98249171f581aaadac38e9eb23e88074db6a724cf2c3f361cd5664b74e62beb3c015cb473d89df7423efb0019
6
+ metadata.gz: 772c2a0849cde3c134977dfe3856cd4e6deb4b3cdbaa0568eb7656e64c9a9d722b11ba00a61a65a99e75978fd377e069f2aa9b4adb9e1fe7f8535dc4cc1edb19
7
+ data.tar.gz: dff56f16e56b6632e1e82e5327f1c0ae7fd39e218ce6aaaf5d0d4c30643b12dc3864e83fd9279066e504f0c740de9b666fe103498eeaf086caf11633ecf62bba
data/README.md CHANGED
@@ -8,326 +8,15 @@ Hammer-cli provides just the core functionality. The core is extensible using pl
8
8
  This architecture allows for easy customization according to your application. Nearly any Ruby script can be turned into a Hammer command, so the possibilities are endless.
9
9
 
10
10
  Available plugins are currently:
11
- - [hammer-cli-foreman](https://github.com/theforeman/hammer-cli-foreman) - commands corresponding to Foreman API
12
- - [hammer-cli-katello-bridge](https://github.com/theforeman/hammer-cli-katello-bridge) - set of commands provided by Katello CLI
11
+ - [hammer-cli-foreman](https://github.com/theforeman/hammer-cli-foreman) - commands corresponding to Foreman API
12
+ - [hammer-cli-katello](https://github.com/theforeman/hammer-cli-katello) - commands for managing Katello
13
13
 
14
14
  You also can easily add custom commands for your specific use, such as bulk actions or admin tasks.
15
15
 
16
16
 
17
17
  Installation
18
18
  ------------
19
-
20
- Hammer CLI is packaged for the following RPM based distributions:
21
-
22
- - RHEL and derivatives, version 6
23
- - Fedora 18, 19
24
- - Debian Wheezy, Squeezy
25
- - Ubuntu Precise
26
-
27
- ### Installation from RPMs
28
-
29
- #### Step 1: setup yum repositories
30
-
31
- For Foreman 1.3 stable the hammer packages are part of your installation repo and you can skip this step.
32
-
33
- You can choose from stable or nightly repo. Nightly has more recent version of hammer packages, but it was subject to less testing so there is a higher risk of issues.
34
- Add the Foreman yum repository to your yum repo files. For Fedora installations replace 'el6' with 'f18' or 'f19' as appropriate.
35
-
36
-
37
- Using stable
38
-
39
- ```bash
40
- yum -y install http://yum.theforeman.org/releases/1.3/el6/x86_64/foreman-release.rpm
41
- ```
42
-
43
- or nightly
44
-
45
- ```bash
46
- cat > /etc/yum.repos.d/foreman.repo << EOF
47
- [foreman]
48
- name=Foreman Nightly
49
- baseurl=http://yum.theforeman.org/nightly/el6/x86_64
50
- gpgcheck=0
51
- enabled=1
52
- EOF
53
- ```
54
-
55
- On RHEL systems you will also have to add [EPEL repository](https://fedoraproject.org/wiki/EPEL) as it contains some of the required dependencies.
56
-
57
-
58
- #### Step 2: install hammer core
59
-
60
- ```bash
61
- yum install rubygem-hammer_cli
62
- ```
63
-
64
- #### Step 3: install plugins
65
- Currently, there are two plugins, both available as rpm packages.
66
-
67
- - commands for managing foreman
68
-
69
- ```bash
70
- yum install rubygem-hammer_cli_foreman
71
- ```
72
-
73
- - 1:1 bridge to [katello cli](https://github.com/Katello/katello)
74
-
75
- ```bash
76
- yum install rubygem-hammer_cli_katello_bridge
77
- ```
78
-
79
- To install any other hammer plugin just make sure the appropriate gem is installed and follow with the configuration.
80
-
81
-
82
- ### Installation from DEBs
83
-
84
- #### Step 1: setup apt repositories
85
-
86
- For Foreman 1.3 stable the hammer packages are part of your installation repo and you can skip this step.
87
-
88
- You can choose from stable or nightly repo. Nightly has more recent version of hammer packages, but it was subject to less testing so there is a highr risk of issues.
89
-
90
- Choose stable (don't forget to replace "squeeze" with version name of your system)
91
-
92
- ```bash
93
- echo "deb http://deb.theforeman.org/ squeeze stable" > /etc/apt/sources.list.d/foreman.list
94
- ```
95
-
96
- or nightly
97
-
98
- ```bash
99
- echo "deb http://deb.theforeman.org/ squeeze nightly" > /etc/apt/sources.list.d/foreman.list
100
- ```
101
-
102
- and update the keys
103
-
104
- ```bash
105
- wget -q http://deb.theforeman.org/foreman.asc -O- | apt-key add -
106
- ```
107
-
108
- #### Step 2: install hammer core
109
-
110
- ```bash
111
- apt-get update && apt-get install ruby-hammer-cli
112
- ```
113
-
114
- #### Step 3: install plugins
115
- Currently, there are two plugins, both available as deb packages.
116
-
117
- - commands for managing foreman
118
-
119
- ```bash
120
- $ apt-get install ruby-hammer-cli-foreman
121
- ```
122
-
123
- - 1:1 bridge to [katello cli](https://github.com/Katello/katello)
124
-
125
- ```bash
126
- $ apt-get install ruby-hammer-cli-katello-bridge
127
- ```
128
-
129
- To install any other hammer plugin just make sure the appropriate gem is installed and follow with the configuration.
130
-
131
-
132
- ### Installation from GEMs
133
-
134
- Make sure you have ```gem``` command installed on your system
135
-
136
- #### Step 1: install hammer core
137
-
138
- ```bash
139
- $ gem install hammer_cli
140
- ```
141
-
142
- #### Step 2: install plugins
143
- Currently, there are two plugins, both available on rubygems.org
144
-
145
- - commands for managing foreman
146
-
147
- ```bash
148
- $ gem install hammer_cli_foreman
149
- ```
150
-
151
- - 1:1 bridge to [katello cli](https://github.com/Katello/katello)
152
-
153
- ```bash
154
- $ gem install hammer_cli_katello_bridge
155
- ```
156
-
157
- To install any other hammer plugin just install the appropriate gem and follow with the configuration.
158
-
159
-
160
- ### Installation from SOURCE
161
-
162
- If you can install hammer from git checkouts, you will just need ```rake``` installed on your system.
163
- Clone and install CLI core
164
-
165
- ```bash
166
- $ git clone https://github.com/theforeman/hammer-cli.git
167
- $ cd hammer-cli
168
- $ rake install
169
- $ cd ..
170
- ```
171
-
172
- clone plugin with foreman commands
173
-
174
- ```bash
175
- $ git clone https://github.com/theforeman/hammer-cli-foreman.git
176
- $ cd hammer-cli-foreman
177
- $ rake install
178
- $ cd ..
179
- ```
180
-
181
- and optionally other plugins via any of the methods mentioned above.
182
-
183
-
184
- Configuration
185
- -------------
186
-
187
- ### Format and locations
188
-
189
- Configuration is set based on the following files, loaded in this order:
190
-
191
- - ```/etc/foreman/cli_config.yml```.
192
- - ```~/.foreman/cli_config.yml```
193
- - ```./config/cli_config.yml``` (config dir in CWD)
194
- - custom location specified on command line - ```-c CONF_FILE_PATH```
195
-
196
- Later files have precedence if they redefines the same option.
197
-
198
- Hammer uses yaml formatting for its configuration. The config file template is contained in the hammer_cli gem
199
-
200
- ```bash
201
- gem contents hammer_cli|grep cli_config.template.yml
202
- ```
203
- and can be copied to one of the locations above and changed as needed. The packaged version of hammer copies the template to /etc for you.
204
-
205
-
206
- ### Plugins
207
-
208
- Plugins are disabled by default. You have to edit the config file and enable them manually under ```modules``` option, as can be seen in the sample config below.
209
-
210
- Plugin specific configuration should be nested under plugin's name.
211
-
212
- ### Options
213
-
214
- - ```:log_dir: <path>``` - directory where the logs are stored. The default is ```/var/log/foreman/``` and the log file is named ```hammer.log```
215
- - ```:log_level: <level>``` - logging level. One of ```debug```, ```info```, ```warning```, ```error```, ```fatal```
216
- - ```:log_owner: <owner>``` - logfile owner
217
- - ```:log_group: <group>``` - logfile group
218
- - ```:log_size: 1048576``` - size in bytes, when exceeded the log rotates. Default is 1MB
219
- - ```:watch_plain: false``` - turn on/off syntax highlighting of data being logged in debug mode
220
-
221
- ### Sample config
222
-
223
- ```yaml
224
- :modules:
225
- - hammer_cli_foreman
226
- - hammer_cli_katello_bridge
227
-
228
- :foreman:
229
- :host: 'https://localhost/'
230
- :username: 'admin'
231
- :password: 'changeme'
232
-
233
- :katello_bridge:
234
- :cli_description: '/home/mbacovsk/work/theforeman/hammer-cli-katello-bridge/katello.json'
235
-
236
-
237
- :log_dir: '/var/log/foreman/'
238
- :log_level: 'debug'
239
- ```
240
-
241
- Use the hammer
242
- --------------
243
-
244
- Confirm your setup by running ```$ hammer -h``` and check that the desired commands are listed.
245
-
246
- ```
247
- $ hammer -h
248
- Usage:
249
- hammer [OPTIONS] SUBCOMMAND [ARG] ...
250
-
251
- Parameters:
252
- SUBCOMMAND subcommand
253
- [ARG] ... subcommand arguments
254
-
255
- Subcommands:
256
- architecture Manipulate Foreman's architectures.
257
- global_parameter Manipulate Foreman's global parameters.
258
- compute_resource Manipulate Foreman's compute resources.
259
- domain Manipulate Foreman's domains.
260
- fact Search Foreman's facts.
261
- report Browse and read reports.
262
- puppet_class Browse and read reports.
263
- host Manipulate Foreman's hosts.
264
- hostgroup Manipulate Foreman's hostgroups.
265
- location Manipulate Foreman's locations.
266
- medium Manipulate Foreman's installation media.
267
- model Manipulate Foreman's hardware models.
268
- os Manipulate Foreman's operating system.
269
- organization Manipulate Foreman's organizations.
270
- partition_table Manipulate Foreman's partition tables.
271
- proxy Manipulate Foreman's smart proxies.
272
- subnet Manipulate Foreman's subnets.
273
- template Manipulate Foreman's config templates.
274
- about status of the katello server and its subcomponents
275
- activation_key activation key specific actions in the katello server
276
- admin various administrative actions
277
- changeset changeset specific actions in the katello server
278
- client client specific actions in the katello server
279
- content content namespace command
280
- distribution repo specific actions in the katello server
281
- distributor distributor specific actions in the katello server
282
- environment environment specific actions in the katello server
283
- errata errata specific actions in the katello server
284
- gpg_key GPG key specific actions in the katello server
285
- node node specific actions in the katello server
286
- org organization specific actions in the katello server
287
- package package specific actions in the katello server
288
- package_group package group specific actions in the katello server
289
- permission permission specific actions in the katello server
290
- ping get the status of the katello server
291
- product product specific actions in the katello server
292
- provider provider specific actions in the katello server
293
- puppet_module puppet module specific actions in the katello server
294
- repo repo specific actions in the katello server
295
- shell run the cli as a shell
296
- sync_plan synchronization plan specific actions in the katello server
297
- system system specific actions in the katello server
298
- system_group system group specific actions in the katello server
299
- task commands for retrieving task information
300
- user user specific actions in the katello server
301
- user_role user role specific actions in the katello server
302
- version get the version of the katello server
303
-
304
- Options:
305
- -v, --verbose be verbose
306
- -c, --config CFG_FILE path to custom config file
307
- -u, --username USERNAME username to access the remote system
308
- -p, --password PASSWORD password to access the remote system
309
- --version show version
310
- --show-ids Show ids of associated resources
311
- --csv Output as CSV (same as --adapter=csv)
312
- --output ADAPTER Set output format. One of [base, table, silent, csv]
313
- --csv-separator SEPARATOR Character to separate the values
314
- -P, --ask-pass Ask for password
315
- --autocomplete LINE Get list of possible endings
316
- -h, --help print help
317
- ```
318
-
319
-
320
- And you are Done. Your hammer client is configured and ready to use.
321
-
322
-
323
- Autocompletion
324
- --------------
325
-
326
- It is necessary to copy the hammer_cli_complete script to the bash_completion.d directory.
327
-
328
- $ sudo cp hammer-cli/hammer_cli_complete /etc/bash_completion.d/
329
-
330
- Then after starting a new shell the completion should work.
19
+ We build rpms, debs and gems. Alternatively you can install hammer form a git checkout. See our [installation instructions](doc/installation.md#installation) for details.
331
20
 
332
21
 
333
22
  Further reading
data/bin/hammer CHANGED
@@ -5,10 +5,21 @@ require 'clamp'
5
5
  require 'highline'
6
6
  HighLine.color_scheme = HighLine::SampleColorScheme.new
7
7
 
8
+ # load i18n to get translations
9
+ require 'hammer_cli/i18n'
10
+
11
+ require 'hammer_cli/options/normalizers'
8
12
  # create fake command instance to use some global args before we start
9
13
  class PreParser < Clamp::Command
10
- option ["-v", "--verbose"], :flag, "be verbose"
11
- option ["-c", "--config"], "CFG_FILE", "path to custom config file"
14
+ option ["-v", "--verbose"], :flag, _("be verbose")
15
+ option ["-d", "--debug"], :flag, _("show debugging output")
16
+ option ["-c", "--config"], "CFG_FILE", _("path to custom config file")
17
+ option ["-u", "--username"], "USERNAME", _("username to access the remote system")
18
+ option ["-p", "--password"], "PASSWORD", _("password to access the remote system")
19
+ option ["--interactive"], "INTERACTIVE", _("Explicitly turn interactive mode on/off") do |value|
20
+ bool_normalizer = HammerCLI::Options::Normalizers::Bool.new
21
+ bool_normalizer.format(value)
22
+ end
12
23
  end
13
24
 
14
25
  preparser = PreParser.new File.basename($0), {}
@@ -20,26 +31,48 @@ end
20
31
  # load user's settings
21
32
  require 'hammer_cli/settings'
22
33
 
23
- CFG_PATH = ['./config/cli_config.yml', '~/.foreman/cli_config.yml', '/etc/foreman/cli_config.yml']
34
+ CFG_PATH_LEGACY = ['~/.foreman/', '/etc/foreman/', "#{::RbConfig::CONFIG['sysconfdir']}/foreman/"].uniq
35
+ HammerCLI::Settings.load_from_paths CFG_PATH_LEGACY
36
+ if HammerCLI::Settings.path_history.empty?
37
+ CFG_PATH = ['~/.hammer/', '/etc/hammer/', "#{::RbConfig::CONFIG['sysconfdir']}/hammer/"].uniq
38
+ HammerCLI::Settings.load_from_paths CFG_PATH
39
+ else
40
+ warn _("Warning: Legacy config paths detected, move the following files")
41
+ HammerCLI::Settings.path_history.each { |p| warn " #{p} -> #{p.gsub('hammer.modules.d', 'cli.modules.d').gsub('foreman/', 'hammer/')}"}
42
+ end
43
+
44
+ CFG_PATH_LOCAL = ['./config/']
24
45
 
25
46
  if preparser.config
26
- CFG_PATH.unshift preparser.config
47
+ CFG_PATH_LOCAL.unshift preparser.config
27
48
  end
28
49
 
29
- HammerCLI::Settings.load_from_file CFG_PATH
50
+ HammerCLI::Settings.load_from_paths CFG_PATH_LOCAL
51
+
52
+ # store username and password in settings
53
+ HammerCLI::Settings.load({
54
+ :_params => {
55
+ :username => preparser.username,
56
+ :password => preparser.password,
57
+ :interactive => preparser.interactive,
58
+ :verbose => preparser.verbose? || preparser.debug?
59
+ }})
30
60
 
31
61
  # setup logging
32
62
  require 'hammer_cli/logger'
33
63
  logger = Logging.logger['Init']
34
64
 
35
- if preparser.verbose?
65
+ if preparser.verbose? || preparser.debug?
36
66
  root_logger = Logging.logger.root
37
67
  root_logger.appenders = root_logger.appenders << ::Logging.appenders.stderr(:layout => HammerCLI::Logger::COLOR_LAYOUT)
68
+ root_logger.level = 'debug' if preparser.debug?
38
69
  end
39
70
 
40
71
  require 'hammer_cli/version'
41
72
  hammer_version = HammerCLI.version.to_s
42
73
  logger.info "Initialization of Hammer CLI (#{hammer_version}) has started..."
74
+ logger.debug "Running at ruby #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"
75
+
43
76
 
44
77
  # log which config was loaded (now when we have logging)
45
78
  HammerCLI::Settings.path_history.each do |path|
@@ -58,4 +91,10 @@ rescue => e
58
91
  exit HammerCLI::EX_SOFTWARE
59
92
  end
60
93
 
94
+ # log information about locale
95
+ logger.debug "Using locale '#{HammerCLI::I18n.locale}'"
96
+ HammerCLI::I18n.domains.each do |domain|
97
+ logger.debug "'#{domain.type}' files for locale domain '#{domain.domain_name}' loaded from '#{File.expand_path(domain.locale_dir)}'"
98
+ end
99
+
61
100
  exit HammerCLI::MainCommand.run || HammerCLI::EX_OK
@@ -0,0 +1,4 @@
1
+ # :sample_module:
2
+ # :enable_module: false
3
+ # :some_config: 'some_value'
4
+
@@ -1,17 +1,15 @@
1
- :modules:
2
- # - hammer_cli_foreman
3
- # - hammer_cli_katello_bridge
1
+ :ui:
2
+ :interactive: true
3
+ :per_page: 20
4
+ :history_file: '~/.hammer/history'
4
5
 
5
- :foreman:
6
- :host: 'https://localhost/'
7
- :username: 'admin'
8
- :password: 'changeme'
9
6
 
10
- # :watch_plain: true disables color output of logger.watch in Clamp commands
7
+ # enable/disable color output of logger in Clamp commands
11
8
  :watch_plain: false
12
9
 
13
-
14
- #:log_dir: '/var/log/foreman'
15
- :log_dir: '~/.foreman/log'
10
+ :log_dir: '~/.hammer/log'
16
11
  :log_level: 'error'
17
12
  :log_api_calls: false
13
+ #:log_owner: 'foreman'
14
+ #:log_group: 'foreman'
15
+ #:log_size: 5 #MB