moose-inventory 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 51f46f508815c46f1822f0c857366ae1db01276d
4
- data.tar.gz: 5b6b796bf75a8438d052b97ecdaab36b1a44fd1c
3
+ metadata.gz: a63b0a4a7ad6bd07ebcd5a046dcd9030968ee1ae
4
+ data.tar.gz: fa90582f07b70b10ac2e712e0535fd3c092074b7
5
5
  SHA512:
6
- metadata.gz: 457a392ad4ad90e450cca80fe6c5071009a76735caba56326f6d4bd5554774e4f3d1a7d637c9b1250d7d6c0ad708335df5261cfad68117104fcde72e266a3879
7
- data.tar.gz: 7d0a7d1aba9105e12a9fb1a930e201e094f5cc850d0f292c6582e636cebd756803c68bcc7b8c97c6c6c983dda34f27a890640c61bf8840f0794c771507e52b7e
6
+ metadata.gz: 8103f643b5d66b9f46b1ef076468afc8c7a9307f72e3a2bc211909862d796d9c08a4735c3c07382140e219313b9d038a7428d8d48a827034e6c6c011c1f9e4e3
7
+ data.tar.gz: 3a26d900604cb5dbcccd3c5da3f6182f1fde5db0210c7ecb662dacaf8c47b0ad3eb3e1c64ae377d3c7dbbc0dd2521a902802990402e6f125eed18f672ac7e4c6
data/README.md CHANGED
@@ -79,51 +79,30 @@ Additional parameters are also required in the **db** subsection, depending on t
79
79
 
80
80
  ## Usage
81
81
  ### The help system
82
- The tool itself provides a convenient help feature. For example,
82
+ The tool itself provides a convenient help feature. For example, try each of the following,
83
83
 
84
84
  > moose-inventory help
85
- Commands:
86
- moose-inventory group ACTION # Manipulate groups in…
87
- moose-inventory help [COMMAND] # Describe available c…
88
-
89
-
90
- and,
91
-
92
85
  > moose-inventory help group
93
- Commands:
94
- moose-inventory group add NAME # Add a group NAME to …
95
- moose-inventory group list # List the groups, tog…
96
-
97
-
98
- and,
99
-
100
86
  > moose-inventory group help add
101
- Usage:
102
- moose-inventory add NAME
103
-
104
- Options:
105
- [--hosts=HOSTS]
106
-
107
- Add a group NAME to the inventory
108
87
 
109
88
  ###Global switches
110
89
  Not described in the built-in help system are a handful of top-level switches, as follows.
111
90
 
112
- #### - -config FILE
113
- The *--config* flag sets the configuration file to be used. If specified, then the file must exist. This takes precedence over all other config files in other locations. If not provided, then the default is to see in standard locations, see later.
91
+ #### Option `--config <FILE>`
92
+ The *--config* flag sets the configuration file to be used. If specified, then the file must exist. This takes precedence over all other config files in other locations. If not provided, then the default is to search standard locations, see later.
114
93
 
115
94
  For example,
116
95
 
117
96
  > moose-inventory --config ./my_conf host list
118
97
 
119
- #### - -env SECTION
98
+ #### Option `--env <SECTION>`
120
99
  The *--env* flag sets the section in the configuration file to be used as the environment configuration. If set, then the section must exist. If not set, then what ever default is provided in the general::defaultenv parameter of the configuration file will be used.
121
100
 
122
101
  For example,
123
102
 
124
103
  > moose-inventory --env my_section host list
125
104
 
126
- #### - - format yaml|json|pjson
105
+ #### Option `--format <yaml|json|pjson>`
127
106
  The *--format* switch changes the output format from *list* and *get* operations. Valid formats are yaml, json, pjson (i.e. pretty JSON). If the switch is not given, then the default is json.
128
107
 
129
108
  For example,
@@ -138,7 +117,7 @@ For example,
138
117
  The *moose-inventory* tool performs database operations in a transactional manner. That is to say, either all operations of a command succeed, or they are all rolled back.
139
118
 
140
119
  ###Walk-through examples
141
- In this example, we will walk through the process of creating two hosts and two groups, assigning variables to each, and then then associating hosts with groups. Once done, we will then remove each association, variable, group and host.
120
+ This walk-through goes through the process of creating three hosts and three groups, assigning variables to some of each, and then associating hosts with groups. Once done, each association, variable, group, and host are removed.
142
121
 
143
122
  We start by creating three hosts, in this case named *host1*, *host2*, and *host3*. Note, we can add as many hosts as we desire via this single command. Also, although we have used short names here, we could equally have used fully qualified names.
144
123
 
@@ -330,10 +309,11 @@ Removing variables, groups, and hosts is just as easy. In the following example
330
309
  > moose-inventory host rm host1 host2 host3
331
310
 
332
311
  ### Using moose-inventory with Ansible
333
- For integration with Ansible, a shim script should be used, in order to set the correct configuration file, environment, etc.
312
+ The *moose-inventory* tool is compliant with the Ansible specifications for [dynamic inventory sources](http://docs.ansible.com/developing_inventory.html).
313
+
314
+ However, to make use of *moose-inventory's* multiple environment and configuration file options, a shim script should be used as the target for the [external inventory script](http://docs.ansible.com/intro_dynamic_inventory.html). A trivial example may look something like this,
334
315
 
335
- A trivial shim script, to be provided to Ansible as the [external inventory script](http://docs.ansible.com/intro_dynamic_inventory.html), may look like this,
336
- ```bash
316
+ ```shell
337
317
  #!/bin/bash
338
318
 
339
319
  CONF='./example.conf'
@@ -343,20 +323,25 @@ moose-inventory --config $CONF --env $ENV $@
343
323
 
344
324
  exit $?
345
325
  ```
346
- When Ansible calls the external inventory script, it does so using the certain parameters, which *moose-inventory* recognises. The Ansible parameters, and their equivalent *moose-inventory* native parameters are shown below.
326
+ When Ansible calls the external inventory script, it does so using certain parameters, which *moose-inventory* automatically recognises and responds to. The Ansible parameters, and their equivalent *moose-inventory* parameters are shown below.
347
327
 
348
- Ansible params | moose-inventory params
328
+ Ansible | moose-inventory
349
329
  ---------------- |-------------
350
- -<sp>-hosts | host list
351
- -<sp>-hosts HOST | host get HOST
352
- --groups | group list
330
+ `--list` | `--ansible group list`
331
+ `--host HOSTNAME` | `--ansible host listvars HOSTNAME`
353
332
 
354
- Note, the above conversions are done automatically by the tool, and are included here only for reference.
333
+ Note, the above conversions are done **automatically** within *moose-inventory*.
355
334
 
356
- With *moose-inventory* installed and configured, and a shim script (e.g. shim.sh) in place, then using Ansible would be a bit like this:
335
+ With *moose-inventory* installed and configured, and a shim script (e.g. *shim.sh*) in place, then use by Ansible can be acheived via Ansible's `-i` option.
357
336
 
358
337
  ansible -i shim.sh -u ubuntu us-east-1d -m ping
359
338
 
339
+ Alternatively, if you are using an [Ansible configuration file](http://docs.ansible.com/intro_configuration.html), then you can set the [inventory](http://docs.ansible.com/intro_configuration.html#inventory) option,
340
+
341
+ inventory = ./shim.sh
342
+
343
+ Yet another option is to copy the shim script to */etc/ansible/hosts* and `chmod +x` it. However, since this would essentially fix the config file and environment used, doing so would defeat the flexibility intended for *moose-inventory*.
344
+
360
345
  ##Missing features
361
346
  The following desired features are yet to be implemented:
362
347
 
@@ -11,11 +11,12 @@ module Moose
11
11
  require_relative 'group_get'
12
12
  require_relative 'group_list'
13
13
  require_relative 'group_rm'
14
- require_relative 'group_addchild'
15
- require_relative 'group_rmchild'
14
+ #require_relative 'group_addchild'
15
+ #require_relative 'group_rmchild'
16
16
  require_relative 'group_addhost'
17
17
  require_relative 'group_rmhost'
18
18
  require_relative 'group_addvar'
19
+ require_relative 'group_listvars'
19
20
  require_relative 'group_rmvar'
20
21
  end
21
22
  end
@@ -10,16 +10,20 @@ module Moose
10
10
  #==========================
11
11
  desc 'list',
12
12
  'List the groups, together with any associated hosts and groupvars'
13
- option :ansiblestyle, type: :boolean
14
13
  def list # rubocop:disable Metrics/AbcSize
15
14
  # Convenience
16
15
  db = Moose::Inventory::DB
17
-
16
+ confopts = Moose::Inventory::Config._confopts
17
+
18
18
  # Process
19
19
  results = {}
20
20
  db.models[:group].all.each do |group|
21
+
21
22
  hosts = group.hosts_dataset.map(:name)
22
23
 
24
+ # Hide the automatic ungrouped group, if it's empty
25
+ next if group[:name] == 'ungrouped' && hosts.length == 0
26
+
23
27
  groupvars = {}
24
28
  group.groupvars_dataset.each do |gv|
25
29
  groupvars[gv[:name].to_sym] = gv[:value]
@@ -30,7 +34,11 @@ module Moose
30
34
  results[group[:name].to_sym][:hosts] = hosts
31
35
  end
32
36
  unless groupvars.length == 0
33
- results[group[:name].to_sym][:groupvars] = groupvars
37
+ if confopts[:ansible] == true
38
+ results[group[:name].to_sym][:vars] = groupvars
39
+ else
40
+ results[group[:name].to_sym][:groupvars] = groupvars
41
+ end
34
42
  end
35
43
  end
36
44
  Formatter.out(results)
@@ -0,0 +1,77 @@
1
+ require 'thor'
2
+ require 'json'
3
+
4
+ require_relative './formatter.rb'
5
+ require_relative '../db/exceptions.rb'
6
+
7
+ module Moose
8
+ module Inventory
9
+ module Cli
10
+ ##
11
+ # implementation of the "group listvars" method of the CLI
12
+ class Group
13
+ #==========================
14
+ desc 'listvar', 'List all variables associated with the group'
15
+ def listvars(*argv)
16
+ # Convenience
17
+ confopts = Moose::Inventory::Config._confopts
18
+
19
+ # Note, the Ansible spects don't call for a "--group GROUPNAME" method.
20
+ # So, strictly, there is no Ansible compatibility for this method.
21
+ # Instead, the Ansible compatibility included herein is for consistency
22
+ # with the "hosts listvars" method, which services the Ansible
23
+ # "--host HOSTNAME" specs.
24
+
25
+ # sanity
26
+ if confopts[:ansible] == true
27
+ if argv.length != 1
28
+ abort('ERROR: Wrong number of arguments for Ansible mode, '\
29
+ "#{args.length} for 1.")
30
+ end
31
+ else
32
+ if argv.length < 1
33
+ abort('ERROR: Wrong number of arguments, '\
34
+ "#{args.length} for 1 or more.")
35
+ end
36
+ end
37
+
38
+
39
+ # Convenience
40
+ db = Moose::Inventory::DB
41
+ fmt = Moose::Inventory::Cli::Formatter
42
+
43
+ # Arguments
44
+ names = argv.uniq.map(&:downcase)
45
+
46
+ #process
47
+ results = {}
48
+
49
+ if confopts[:ansible] == true
50
+ # This is the implementation per Ansible specs
51
+ name = names.first
52
+ group = db.models[:group].find(name: name)
53
+ if group.nil?
54
+ fmt.warn "The Group #{name} does not exist."
55
+ else
56
+ group.groupvars_dataset.each do |gv|
57
+ results[gv[:name].to_sym] = gv[:value]
58
+ end
59
+ end
60
+ else
61
+ # This our more flexible implementation
62
+ names.each do |name|
63
+ group = db.models[:group].find(name: name)
64
+ unless group.nil?
65
+ results[name.to_sym] = {}
66
+ group.groupvars_dataset.each do |gv|
67
+ results[name.to_sym][gv[:name].to_sym] = gv[:value]
68
+ end
69
+ end
70
+ end
71
+ end
72
+ fmt.dump(results)
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -17,6 +17,7 @@ module Moose
17
17
  require_relative 'host_addgroup'
18
18
  require_relative 'host_rmgroup'
19
19
  require_relative 'host_addvar'
20
+ require_relative 'host_listvars'
20
21
  require_relative 'host_rmvar'
21
22
  end
22
23
  end
@@ -0,0 +1,72 @@
1
+ require 'thor'
2
+ require 'json'
3
+
4
+ require_relative './formatter.rb'
5
+ require_relative '../db/exceptions.rb'
6
+
7
+ module Moose
8
+ module Inventory
9
+ module Cli
10
+ ##
11
+ # implementation of the "host listvars" method of the CLI
12
+ class Host
13
+ #==========================
14
+ desc 'listvar', 'List all variables associated with the host'
15
+ def listvars(*argv)
16
+ # Convenience
17
+ confopts = Moose::Inventory::Config._confopts
18
+
19
+ # sanity
20
+ if confopts[:ansible] == true
21
+ if argv.length != 1
22
+ abort('ERROR: Wrong number of arguments for Ansible mode, '\
23
+ "#{args.length} for 1.")
24
+ end
25
+ else
26
+ if argv.length < 1
27
+ abort('ERROR: Wrong number of arguments, '\
28
+ "#{args.length} for 1 or more.")
29
+ end
30
+ end
31
+
32
+
33
+ # Convenience
34
+ db = Moose::Inventory::DB
35
+ fmt = Moose::Inventory::Cli::Formatter
36
+
37
+ # Arguments
38
+ names = argv.uniq.map(&:downcase)
39
+
40
+ #process
41
+ results = {}
42
+
43
+ if confopts[:ansible] == true
44
+ # This is the implementation per Ansible specs
45
+ name = names.first
46
+ host = db.models[:host].find(name: name)
47
+ if host.nil?
48
+ fmt.warn "The host #{name} does not exist."
49
+ else
50
+ host.hostvars_dataset.each do |hv|
51
+ results[hv[:name].to_sym] = hv[:value]
52
+ end
53
+ end
54
+ else
55
+ # This our more flexible implementation, which is not compatible
56
+ # with the Ansible specs
57
+ names.each do |name|
58
+ host = db.models[:host].find(name: name)
59
+ unless host.nil?
60
+ results[name.to_sym] = {}
61
+ host.hostvars_dataset.each do |hv|
62
+ results[name.to_sym][hv[:name].to_sym] = hv[:value]
63
+ end
64
+ end
65
+ end
66
+ end
67
+ fmt.dump(results)
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -32,20 +32,29 @@ module Moose
32
32
 
33
33
  #----------------------
34
34
  def self.top_level_args
35
- # Certain top level flags affect the configuration.
35
+ # The following top-level flags affect the global configuration.
36
+ #
37
+ # --ansible => forces ansible mode in relevant responders
38
+ # Default is not use to use ansible mode
39
+ #
36
40
  # --config FILE => sets the configuration file to be used.
37
41
  # Default is to search standard locations.
42
+ #
38
43
  # --env ENV => sets the section to be used as the configuration.
39
- # Defaults to ""
44
+ # Defaults to "", which forces the use of the
45
+ # defaultenv parameter from the general section of
46
+ # the config file.
47
+ #
40
48
  # --format FORMAT=> See formatter for supported types.
41
49
  # Defaults to json.
50
+ #
51
+ # -- trace => Enable more complete exceptions for db transactions
52
+ # Default is not to trace.
42
53
 
43
- @_confopts = { env: '', format: 'json', trace: false }
54
+ @_confopts = { env: '', format: 'json', ansible: false, trace: false }
44
55
 
45
- # The following are a O(n^m) approach. TODO: O(n) version?
46
-
47
56
  # Check for two-part flags
48
- %w(env config format).each do |var|
57
+ %w(config env format).each do |var|
49
58
  @_argv.each_with_index do |val, index|
50
59
  next if val != "--#{var}"
51
60
  @_confopts[var.to_sym] = @_argv[index + 1]
@@ -55,7 +64,7 @@ module Moose
55
64
  end
56
65
 
57
66
  # Check for one-part flags
58
- %w(trace).each do |var|
67
+ %w(ansible trace).each do |var|
59
68
  @_argv.each_with_index do |val, index|
60
69
  next if val != "--#{var}"
61
70
  @_confopts[var.to_sym] = true
@@ -64,48 +73,49 @@ module Moose
64
73
  end
65
74
  end
66
75
 
76
+ # Sanity
77
+ # - Ansible output format must be json
78
+ # @_confopts[:format] = 'json' if @_confopts[:ansible] == true
79
+
67
80
  end
68
81
 
69
82
  #----------------------
70
83
  def self.top_level_help
71
84
  if @_argv[0] == 'help'
72
85
  puts "Global flags:"
86
+ printf " %-31s %-10s", "--ansible", "# Force Ansible mode (automatically set when using ansible flags)\n"
73
87
  printf " %-31s %-10s", "--config FILE", "# Specifies a configuration file to use\n"
74
88
  printf " %-31s %-10s", "--env ENV", "# Specifies the environment section of the config to use\n"
75
- printf " %-31s %-10s", "--format yaml|json|pjson", "# Format for the output of 'get' and 'list'\n"
89
+ printf " %-31s %-10s", "--format yaml|json|pjson", "# Format for the output of 'get', 'list', and 'listvars' subcommands\n"
90
+ printf " %-31s %-10s", "--trace", "# Enable more complete exception dumps for database transactions\n"
76
91
  puts "\nAnsible flags:"
77
- printf " %-31s %-10s", "--hosts", "# Retrieves the list of hosts (alias for 'host list'\n"
78
- printf " %-31s %-10s", "--hosts HOST", "# Retrieves the specified host (alias for 'host get HOST')\n"
79
- printf " %-31s %-10s", "--groups", "# Retrieves the list of groups (alias for 'group list')\n\n"
92
+ printf " %-31s %-10s", "--host HOSTNAME", "# Retrieves host variables for the specified host (alias for 'host listvars HOSTNAME')\n"
93
+ printf " %-31s %-10s", "--list", "# Retrieves the list of groups (alias for 'group list')\n\n"
80
94
  end
81
95
  end
82
96
 
83
97
  #----------------------
84
98
  def self.ansible_args # rubocop:disable Metrics/AbcSize
85
- # Handle Ansible flags
86
- # --hosts => host list
87
- # --hosts HOSTNAME => host get HOSTNAME
88
- # --groups => lgroup list
89
-
90
- if @_argv[0] == '--hosts'
91
- host = @_argv[1]
92
- if !host.nil?
99
+ #
100
+ # See http://docs.ansible.com/developing_inventory.html for Ansible specs
101
+ # for dynamic inventory sources
102
+
103
+ # --list => group list
104
+ # --host HOSTNAME => host getvars HOSTNAME
105
+
106
+ case @_argv[0]
107
+ when '--list'
108
+ @_confopts[:ansible] = true
109
+ @_confopts[:format] = 'json'
93
110
  @_argv.clear
94
- ['host', 'get', "#{host}"].each do |arg|
95
- @_argv << arg
96
- end
97
- else
111
+ @_argv.concat(['group', 'list']).flatten
112
+ when '--host'
113
+ @_confopts[:ansible] = true
114
+ @_confopts[:format] = 'json'
115
+ host = @_argv[1]
98
116
  @_argv.clear
99
- ['host', 'list'].each do |arg|
100
- @_argv << arg
101
- end
102
- end
103
- elsif @_argv[0] == '--groups'
104
- @_argv.clear
105
- ['group', 'list'].each do |arg|
106
- @_argv << arg
107
- end
108
- end
117
+ @_argv.concat(['host', 'listvars', "#{host}"]).flatten
118
+ end
109
119
  end
110
120
 
111
121
  #----------------------
@@ -2,6 +2,6 @@ module Moose
2
2
  ##
3
3
  # The Moose-Tools dynamic inventory management library
4
4
  module Inventory
5
- VERSION = '0.1.4'
5
+ VERSION = '0.1.5'
6
6
  end
7
7
  end
@@ -23,6 +23,7 @@ RSpec.describe Moose::Inventory::Cli::Group do
23
23
 
24
24
  @console = Moose::Inventory::Cli::Formatter
25
25
  @group = Moose::Inventory::Cli::Group
26
+ @cli = Moose::Inventory::Cli
26
27
  @app = Moose::Inventory::Cli::Application
27
28
  end
28
29
 
@@ -73,7 +74,7 @@ RSpec.describe Moose::Inventory::Cli::Group do
73
74
  end
74
75
 
75
76
  #---------------------
76
- it 'should be an alias of --groups (i.e. Ansible parameter)' do
77
+ it 'should be an alias of --list (i.e. Ansible parameter)' do
77
78
 
78
79
  host_name = 'test_host'
79
80
 
@@ -85,15 +86,14 @@ RSpec.describe Moose::Inventory::Cli::Group do
85
86
  end
86
87
 
87
88
  args = @mockargs.clone
88
- args << "--groups"
89
- cli = Moose::Inventory::Cli
89
+ args << "--list"
90
90
 
91
- actual = runner{ cli.start(args) }
91
+ actual = runner{ @cli.start(args) }
92
92
 
93
93
  #@console.out(actual, 'y')
94
94
 
95
95
  desired = { aborted: false, STDOUT: '', STDERR: '' }
96
- desired[:STDOUT] = mock.to_yaml
96
+ desired[:STDOUT] = mock.to_json + "\n"
97
97
 
98
98
  expected(actual, desired)
99
99
  end
@@ -0,0 +1,122 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Moose::Inventory::Cli::Group do
4
+ before(:all) do
5
+ # Set up the configuration object
6
+ @mockarg_parts = {
7
+ config: File.join(spec_root, 'config/config.yml'),
8
+ format: 'yaml',
9
+ env: 'test'
10
+ }
11
+
12
+ @mockargs = []
13
+ @mockarg_parts.each do |key, val|
14
+ @mockargs << "--#{key}"
15
+ @mockargs << val
16
+ end
17
+
18
+ @config = Moose::Inventory::Config
19
+ @config.init(@mockargs)
20
+
21
+ @db = Moose::Inventory::DB
22
+ @db.init if @db.db.nil?
23
+
24
+ @console = Moose::Inventory::Cli::Formatter
25
+ @group = Moose::Inventory::Cli::Group
26
+ @cli = Moose::Inventory::Cli
27
+ @app = Moose::Inventory::Cli::Application
28
+ end
29
+
30
+ before(:each) do
31
+ # We make some @cli calls, which changes config,
32
+ # so we must reset config on each pass
33
+ @config.init(@mockargs)
34
+ @db.reset
35
+ end
36
+
37
+ #==================
38
+ describe 'listvar' do
39
+ #-----------------
40
+ it 'should be responsive' do
41
+ result = @group.instance_methods(false).include?(:listvars)
42
+ expect(result).to eq(true)
43
+ end
44
+
45
+ #-----------------
46
+ it '<missing args> ... should abort with an error' do
47
+ actual = runner { @app.start(%w(group listvars)) }
48
+
49
+ # Check output
50
+ desired = { aborted: true}
51
+ desired[:STDERR] = "ERROR: Wrong number of arguments, 0 for 1 or more.\n"
52
+ expected(actual, desired)
53
+ end
54
+
55
+ #-----------------
56
+ it '--ansible <missing args> ... should abort with an error' do
57
+
58
+ args = @mockargs.clone
59
+ args.concat( %w(--ansible group listvars) ).flatten
60
+
61
+ actual = runner{ @cli.start(args) }
62
+
63
+ # Check output
64
+ desired = { aborted: true}
65
+ desired[:STDERR] = "ERROR: Wrong number of arguments for Ansible mode, 0 for 1.\n"
66
+ expected(actual, desired)
67
+ end
68
+
69
+ #------------------------
70
+ it 'GROUP ... should return a list of group variables grouped by group' do
71
+ group_name ='test_group'
72
+ group_vars = %w(foo=bar cow=chicken)
73
+
74
+ tmp = runner { @app.start(%W(group add #{group_name} )) }
75
+ tmp = runner { @app.start(%W(group addvar #{group_name} #{group_vars[0]} #{group_vars[1]})) }
76
+
77
+ actual = runner do
78
+ @app.start(%W(group listvars #{group_name}))
79
+ end
80
+
81
+ #@console.out(actual, 'y')
82
+
83
+ # Check output
84
+ mock = {}
85
+ mock[group_name.to_sym] = {}
86
+ group_vars.each do |hv|
87
+ hv_array = hv.split('=')
88
+ mock[group_name.to_sym][hv_array[0].to_sym] = hv_array[1]
89
+ end
90
+
91
+ desired = {}
92
+ desired[:STDOUT] = mock.to_yaml
93
+ expected(actual, desired)
94
+ end
95
+
96
+ #------------------------
97
+ it '--ansible GROUP ... should return a list of group variables, in a style akin to Ansible\'s \'--host HOSTNAME\'' do
98
+ group_name ='test_group'
99
+ group_vars = %w(foo=bar cow=chicken)
100
+
101
+ tmp = runner { @app.start(%W(group add #{group_name} )) }
102
+ tmp = runner { @app.start(%W(group addvar #{group_name} #{group_vars[0]} #{group_vars[1]})) }
103
+
104
+ actual = runner do
105
+ @cli.start(%W(--ansible group listvars #{group_name}))
106
+ end
107
+
108
+ #@console.out(actual, 'y')
109
+
110
+ # Check output
111
+ mock = {}
112
+ group_vars.each do |hv|
113
+ hv_array = hv.split('=')
114
+ mock[hv_array[0].to_sym] = hv_array[1]
115
+ end
116
+
117
+ desired = {}
118
+ desired[:STDOUT] = mock.to_json + "\n"
119
+ expected(actual, desired)
120
+ end
121
+ end
122
+ end
@@ -40,7 +40,7 @@ RSpec.describe Moose::Inventory::Cli::Host do
40
40
  result = @host.instance_methods(false).include?(:addvar)
41
41
  expect(result).to eq(true)
42
42
  end
43
-
43
+
44
44
  #-----------------
45
45
  it 'host addvar <missing args> ... should abort with an error' do
46
46
  actual = runner do
@@ -101,33 +101,5 @@ RSpec.describe Moose::Inventory::Cli::Host do
101
101
 
102
102
  expected(actual, desired)
103
103
  end
104
-
105
- #---------------------
106
- it 'should be an alias of --hosts HOST (i.e. Ansible parameter)' do
107
-
108
- mock = {}
109
- host = 'host1'
110
-
111
- runner { @app.start(%W(host add #{host})) }
112
- mock[host.to_sym] = {}
113
- mock[host.to_sym][:groups] = ['ungrouped']
114
-
115
- args = @mockargs.clone
116
- args << '--hosts'
117
- args << "#{host}"
118
- cli = Moose::Inventory::Cli
119
-
120
- # items should now be in the db
121
- actual = runner{ cli.start(args) }
122
-
123
- #@console.out(actual, 'y')
124
-
125
- desired = { aborted: false, STDOUT: '', STDERR: '' }
126
- desired[:STDOUT] = mock.to_yaml
127
-
128
- expected(actual, desired)
129
- end
130
-
131
-
132
104
  end
133
105
  end
@@ -75,31 +75,5 @@ RSpec.describe Moose::Inventory::Cli::Host do
75
75
 
76
76
  expected(actual, desired)
77
77
  end
78
- #---------------------
79
- it 'should be an alias of --hosts (i.e. Ansible parameter)' do
80
-
81
- mock = {}
82
- hosts = %w(host1 host2)
83
- hosts.each do |name|
84
- runner { @app.start(%W(host add #{name})) }
85
- mock[name.to_sym] = {}
86
- mock[name.to_sym][:groups] = ['ungrouped']
87
- end
88
-
89
- args = @mockargs.clone
90
- args << "--hosts"
91
- cli = Moose::Inventory::Cli
92
-
93
- # items should now be in the db
94
- actual = runner{ cli.start(args) }
95
-
96
- #@console.out(actual, 'y')
97
-
98
- desired = { aborted: false, STDOUT: '', STDERR: '' }
99
- desired[:STDOUT] = mock.to_yaml
100
-
101
- expected(actual, desired)
102
- end
103
-
104
78
  end
105
79
  end
@@ -0,0 +1,152 @@
1
+ require 'spec_helper'
2
+
3
+ # TODO: the usual respond_to? method doesn't seem to work on Thor objects.
4
+ # Why not? For now, we'll check against instance_methods.
5
+
6
+ RSpec.describe Moose::Inventory::Cli::Host do
7
+ before(:all) do
8
+ # Set up the configuration object
9
+ @mockarg_parts = {
10
+ config: File.join(spec_root, 'config/config.yml'),
11
+ format: 'yaml',
12
+ env: 'test'
13
+ }
14
+
15
+ @mockargs = []
16
+ @mockarg_parts.each do |key, val|
17
+ @mockargs << "--#{key}"
18
+ @mockargs << val
19
+ end
20
+
21
+ @config = Moose::Inventory::Config
22
+ @config.init(@mockargs)
23
+
24
+ @db = Moose::Inventory::DB
25
+ @db.init if @db.db.nil?
26
+
27
+ @console = Moose::Inventory::Cli::Formatter
28
+ @host = Moose::Inventory::Cli::Host
29
+ @cli = Moose::Inventory::Cli
30
+ @app = Moose::Inventory::Cli::Application
31
+ end
32
+
33
+ before(:each) do
34
+ # We make some @cli calls, which changes config,
35
+ # so we must reset config on each pass
36
+ @config.init(@mockargs)
37
+ @db.reset
38
+ end
39
+
40
+ #==================
41
+ describe 'listvar' do
42
+ #-----------------
43
+ it 'should be responsive' do
44
+ result = @host.instance_methods(false).include?(:listvars)
45
+ expect(result).to eq(true)
46
+ end
47
+
48
+ #-----------------
49
+ it '<missing args> ... should abort with an error' do
50
+ actual = runner { @app.start(%w(host listvars)) }
51
+
52
+ # Check output
53
+ desired = { aborted: true}
54
+ desired[:STDERR] = "ERROR: Wrong number of arguments, 0 for 1 or more.\n"
55
+ expected(actual, desired)
56
+ end
57
+
58
+ #-----------------
59
+ it '--ansible <missing args> ... should abort with an error' do
60
+
61
+ args = @mockargs.clone
62
+ args.concat( %w(--ansible host listvars) ).flatten
63
+
64
+ actual = runner{ @cli.start(args) }
65
+
66
+ # Check output
67
+ desired = { aborted: true}
68
+ desired[:STDERR] = "ERROR: Wrong number of arguments for Ansible mode, 0 for 1.\n"
69
+ expected(actual, desired)
70
+ end
71
+
72
+ #------------------------
73
+ it 'HOST ... should return a list of host variables grouped by host' do
74
+ host_name ='test_host'
75
+ host_vars = %w(foo=bar cow=chicken)
76
+
77
+ tmp = runner { @app.start(%W(host add #{host_name} )) }
78
+ tmp = runner { @app.start(%W(host addvar #{host_name} #{host_vars[0]} #{host_vars[1]})) }
79
+
80
+ actual = runner do
81
+ @app.start(%W(host listvars #{host_name}))
82
+ end
83
+
84
+ #@console.out(actual, 'y')
85
+
86
+ # Check output
87
+ mock = {}
88
+ mock[host_name.to_sym] = {}
89
+ host_vars.each do |hv|
90
+ hv_array = hv.split('=')
91
+ mock[host_name.to_sym][hv_array[0].to_sym] = hv_array[1]
92
+ end
93
+
94
+ desired = {}
95
+ desired[:STDOUT] = mock.to_yaml
96
+ expected(actual, desired)
97
+ end
98
+
99
+ #------------------------
100
+ it '--ansible HOST ... should return a list of host variables per Ansible specs' do
101
+ host_name ='test_host'
102
+ host_vars = %w(foo=bar cow=chicken)
103
+
104
+ tmp = runner { @app.start(%W(host add #{host_name} )) }
105
+ tmp = runner { @app.start(%W(host addvar #{host_name} #{host_vars[0]} #{host_vars[1]})) }
106
+
107
+ actual = runner do
108
+ @cli.start(%W(--ansible host listvars #{host_name}))
109
+ end
110
+
111
+ #@console.out(actual, 'y')
112
+
113
+ # Check output
114
+ mock = {}
115
+ host_vars.each do |hv|
116
+ hv_array = hv.split('=')
117
+ mock[hv_array[0].to_sym] = hv_array[1]
118
+ end
119
+
120
+ desired = {}
121
+ desired[:STDOUT] = mock.to_json + "\n"
122
+ expected(actual, desired)
123
+ end
124
+
125
+ #------------------------
126
+ it '--ansible HOST ... should be an alias for Ansible\'s --host HOST' do
127
+ host_name ='test_host'
128
+ host_vars = %w(foo=bar cow=chicken)
129
+
130
+ tmp = runner { @app.start(%W(host add #{host_name} )) }
131
+ tmp = runner { @app.start(%W(host addvar #{host_name} #{host_vars[0]} #{host_vars[1]})) }
132
+
133
+ actual = runner do
134
+ @cli.start(%W(--host #{host_name}))
135
+ end
136
+
137
+ #@console.out(actual, 'y')
138
+
139
+ # Check output
140
+ mock = {}
141
+ host_vars.each do |hv|
142
+ hv_array = hv.split('=')
143
+ mock[hv_array[0].to_sym] = hv_array[1]
144
+ end
145
+
146
+ desired = {}
147
+ desired[:STDOUT] = mock.to_json + "\n"
148
+ expected(actual, desired)
149
+ end
150
+
151
+ end
152
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moose-inventory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Russell Davies
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-08 00:00:00.000000000 Z
11
+ date: 2015-07-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: indentation
@@ -278,6 +278,7 @@ files:
278
278
  - lib/moose_inventory/cli/group_addvar.rb
279
279
  - lib/moose_inventory/cli/group_get.rb
280
280
  - lib/moose_inventory/cli/group_list.rb
281
+ - lib/moose_inventory/cli/group_listvars.rb
281
282
  - lib/moose_inventory/cli/group_rm.rb
282
283
  - lib/moose_inventory/cli/group_rmchild.rb
283
284
  - lib/moose_inventory/cli/group_rmhost.rb
@@ -288,6 +289,7 @@ files:
288
289
  - lib/moose_inventory/cli/host_addvar.rb
289
290
  - lib/moose_inventory/cli/host_get.rb
290
291
  - lib/moose_inventory/cli/host_list.rb
292
+ - lib/moose_inventory/cli/host_listvars.rb
291
293
  - lib/moose_inventory/cli/host_rm.rb
292
294
  - lib/moose_inventory/cli/host_rmgroup.rb
293
295
  - lib/moose_inventory/cli/host_rmvar.rb
@@ -311,6 +313,7 @@ files:
311
313
  - spec/lib/moose_inventory/cli/group_addvar_spec.rb
312
314
  - spec/lib/moose_inventory/cli/group_get_spec.rb
313
315
  - spec/lib/moose_inventory/cli/group_list_spec.rb
316
+ - spec/lib/moose_inventory/cli/group_listvar_spec.rb
314
317
  - spec/lib/moose_inventory/cli/group_rm_spec.rb
315
318
  - spec/lib/moose_inventory/cli/group_rmhost_spec.rb
316
319
  - spec/lib/moose_inventory/cli/group_rmvar_spec.rb
@@ -320,6 +323,7 @@ files:
320
323
  - spec/lib/moose_inventory/cli/host_addvar_spec.rb
321
324
  - spec/lib/moose_inventory/cli/host_get_spec.rb
322
325
  - spec/lib/moose_inventory/cli/host_list_spec.rb
326
+ - spec/lib/moose_inventory/cli/host_listvar_spec.rb
323
327
  - spec/lib/moose_inventory/cli/host_rm_spec.rb
324
328
  - spec/lib/moose_inventory/cli/host_rmgroup_spec.rb
325
329
  - spec/lib/moose_inventory/cli/host_rmvar_spec.rb
@@ -363,6 +367,7 @@ test_files:
363
367
  - spec/lib/moose_inventory/cli/group_addvar_spec.rb
364
368
  - spec/lib/moose_inventory/cli/group_get_spec.rb
365
369
  - spec/lib/moose_inventory/cli/group_list_spec.rb
370
+ - spec/lib/moose_inventory/cli/group_listvar_spec.rb
366
371
  - spec/lib/moose_inventory/cli/group_rm_spec.rb
367
372
  - spec/lib/moose_inventory/cli/group_rmhost_spec.rb
368
373
  - spec/lib/moose_inventory/cli/group_rmvar_spec.rb
@@ -372,6 +377,7 @@ test_files:
372
377
  - spec/lib/moose_inventory/cli/host_addvar_spec.rb
373
378
  - spec/lib/moose_inventory/cli/host_get_spec.rb
374
379
  - spec/lib/moose_inventory/cli/host_list_spec.rb
380
+ - spec/lib/moose_inventory/cli/host_listvar_spec.rb
375
381
  - spec/lib/moose_inventory/cli/host_rm_spec.rb
376
382
  - spec/lib/moose_inventory/cli/host_rmgroup_spec.rb
377
383
  - spec/lib/moose_inventory/cli/host_rmvar_spec.rb