Skip to content

Commit

Permalink
refactor and add pupdb commandline prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
mose committed Dec 4, 2015
1 parent 7b8e4dd commit e5e910f
Show file tree
Hide file tree
Showing 14 changed files with 282 additions and 81 deletions.
2 changes: 1 addition & 1 deletion bin/hc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ $LOAD_PATH << File.expand_path("../../lib", __FILE__)

require 'hieracles'

opt = Hieracles::Optparse.new(ARGV)
opt = Hieracles::Options::Hc.new(ARGV)

if opt.options.has_key? :version
puts Hieracles.version
Expand Down
18 changes: 0 additions & 18 deletions bin/pdb

This file was deleted.

16 changes: 16 additions & 0 deletions bin/pupdb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env ruby

$LOAD_PATH << File.expand_path("../../lib", __FILE__)

require 'hieracles'

opt = Hieracles::Options::Pupdb.new(ARGV)

if opt.options.has_key? :version
puts Hieracles.version
exit(0)
end

command = opt.payload[0]
args = opt.payload[2..-1]

1 change: 1 addition & 0 deletions lib/hieracles.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'hieracles/notification'
require 'hieracles/interpolate'
require 'hieracles/optparse'
require 'hieracles/options/hc'
require 'hieracles/config'
require 'hieracles/hiera'
require 'hieracles/utils'
Expand Down
2 changes: 0 additions & 2 deletions lib/hieracles/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ def initconfig(file)
FileUtils.mkdir_p(File.dirname(file))
File.open(file, 'w') do |f|
f.puts '---'
f.puts '# uncomment if you use the CGI method for discovery'
f.puts '# server: puppetserver.example.com'
f.puts 'classpath: manifests/classes/%s.pp'
f.puts 'modulepath: modules'
f.puts 'encpath: enc'
Expand Down
4 changes: 2 additions & 2 deletions lib/hieracles/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,13 @@ def add_modules(line, modules)
end

def puppetdb_info
resp = puppetdb.request("nodes/#{@fqdn}")
resp = puppetdb.get_info(@fqdn)
resp.data
end

def puppet_facts
if Config.usedb
resp = puppetdb.request("nodes/#{@fqdn}/facts")
resp = puppetdb.get_facts(@fqdn)
@notifications = resp.notifications
if resp.total_records > 0
resp.data.reduce({}) do |a, v|
Expand Down
61 changes: 61 additions & 0 deletions lib/hieracles/options/hc.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
module Hieracles
module Options
class Hc < Hieracles::Optparse

def available_options
{
config: {
has_arg: true,
aliases: ['c', 'conf', 'config']
},
format: {
has_arg: true,
aliases: ['f', 'format']
},
params: {
has_arg: true,
aliases: ['p', 'params']
},
hierafile: {
has_arg: true,
aliases: ['h', 'hierafile']
},
basepath: {
has_arg: true,
aliases: ['b', 'basepath']
},
encpath: {
has_arg: true,
aliases: ['e', 'encpath']
},
version: {
has_arg: false,
aliases: ['v', 'version']
},
yaml_facts: {
has_arg: true,
aliases: ['y', 'yaml']
},
json_facts: {
has_arg: true,
aliases: ['j', 'json']
},
interactive: {
has_arg: false,
aliases: ['i', 'interactive']
},
db: {
has_arg: false,
aliases: ['db']
},
nodb: {
has_arg: false,
aliases: ['nodb', 'no-db', 'no']
}
}
end

end
end
end

61 changes: 61 additions & 0 deletions lib/hieracles/options/pupdb.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
module Hieracles
module Options
class Pupdb < Hieracles::Optparse

def available_options
{
config: {
has_arg: true,
aliases: ['c', 'conf', 'config']
},
format: {
has_arg: true,
aliases: ['f', 'format']
},
params: {
has_arg: true,
aliases: ['p', 'params']
},
hierafile: {
has_arg: true,
aliases: ['h', 'hierafile']
},
basepath: {
has_arg: true,
aliases: ['b', 'basepath']
},
encpath: {
has_arg: true,
aliases: ['e', 'encpath']
},
version: {
has_arg: false,
aliases: ['v', 'version']
},
yaml_facts: {
has_arg: true,
aliases: ['y', 'yaml']
},
json_facts: {
has_arg: true,
aliases: ['j', 'json']
},
interactive: {
has_arg: false,
aliases: ['i', 'interactive']
},
db: {
has_arg: false,
aliases: ['db']
},
nodb: {
has_arg: false,
aliases: ['nodb', 'no-db', 'no']
}
}
end

end
end
end

55 changes: 4 additions & 51 deletions lib/hieracles/optparse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,9 @@ class Optparse

attr_reader :options, :payload

OPTIONS = {
config: {
has_arg: true,
aliases: ['c', 'conf', 'config']
},
format: {
has_arg: true,
aliases: ['f', 'format']
},
params: {
has_arg: true,
aliases: ['p', 'params']
},
hierafile: {
has_arg: true,
aliases: ['h', 'hierafile']
},
basepath: {
has_arg: true,
aliases: ['b', 'basepath']
},
encpath: {
has_arg: true,
aliases: ['e', 'encpath']
},
version: {
has_arg: false,
aliases: ['v', 'version']
},
yaml_facts: {
has_arg: true,
aliases: ['y', 'yaml']
},
json_facts: {
has_arg: true,
aliases: ['j', 'json']
},
interactive: {
has_arg: false,
aliases: ['i', 'interactive']
},
db: {
has_arg: false,
aliases: ['db']
},
nodb: {
has_arg: false,
aliases: ['nodb', 'no-db', 'no']
}
}
def available_options
{}
end

def initialize(array)
@options = {}
Expand All @@ -79,7 +32,7 @@ def initialize(array)

def optionkeys
back = {}
OPTIONS.each do |k, v|
available_options.each do |k, v|
v[:aliases].each do |a|
back[a] = { var: k, has_args: v[:has_arg] }
end
Expand Down
8 changes: 8 additions & 0 deletions lib/hieracles/puppetdb/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ def request(endpoint, query = nil, opts = {})
end
end

def get_info(fqdn)
request("nodes/#{fqdn}")
end

def get_facts(fqdn)
request("nodes/#{fqdn}/facts")
end

end
end
end
19 changes: 19 additions & 0 deletions lib/hieracles/puppetdb/request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Hieracles
module Puppetdb
class Request

def initialize(client)
@client = client
end

def info(fqdn)
@client.request "nodes/#{fqdn}"
end

def facts(fqdn)
@client.request "nodes/#{fqdn}/facts"
end

end
end
end
81 changes: 81 additions & 0 deletions spec/lib/options/hc_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
require 'spec_helper'

describe Hieracles::Options::Hc do
describe '.initialize' do
context 'with proper arguments' do
let(:array) { ['arg1', 'arg2', '-c', 'path/to/config-file', '-f', 'thatformat'] }
let(:expected_payload) { ['arg1', 'arg2'] }
let(:expected_options) do
{ config: 'path/to/config-file', format: 'thatformat' }
end
subject { Hieracles::Options::Hc.new array }
it "populates payload" do
expect(subject.payload).to eq expected_payload
end
it 'populates options' do
expect(subject.options).to eq expected_options
end
end

context 'with proper arguments in random order' do
let(:array) { ['-c', 'path/to/config-file', 'arg1', 'arg2', '-f', 'thatformat'] }
let(:expected_payload) { ['arg1', 'arg2'] }
let(:expected_options) do
{ config: 'path/to/config-file', format: 'thatformat' }
end
subject { Hieracles::Options::Hc.new array }
it "populates payload" do
expect(subject.payload).to eq expected_payload
end
it 'populates options' do
expect(subject.options).to eq expected_options
end
end

context 'with funnily ordered arguments' do
let(:array) { ['arg1', '-u', 'path/to/config-file', 'arg2', '-f', 'thatformat'] }
let(:expected_payload) { ['arg1', 'arg2'] }
let(:expected_options) do
{ format: 'thatformat' }
end
subject { Hieracles::Options::Hc.new array }
it "populates payload" do
expect(subject.payload).to eq expected_payload
end
it 'populates options' do
expect(subject.options).to eq expected_options
end
end

context 'with arguments in alternative syntax' do
let(:array) { ['arg1', 'arg2', '-config', 'path/to/config-file', '--format', 'thatformat'] }
let(:expected_payload) { ['arg1', 'arg2'] }
let(:expected_options) do
{ config: 'path/to/config-file', format: 'thatformat' }
end
subject { Hieracles::Options::Hc.new array }
it "populates payload" do
expect(subject.payload).to eq expected_payload
end
it 'populates options' do
expect(subject.options).to eq expected_options
end
end

context 'with arguments containing boolean element' do
let(:array) { ['arg1', 'arg2', '-i', '-format', 'thatformat'] }
let(:expected_payload) { ['arg1', 'arg2'] }
let(:expected_options) do
{ format: 'thatformat', interactive: true }
end
subject { Hieracles::Options::Hc.new array }
it "populates payload" do
expect(subject.payload).to eq expected_payload
end
it 'populates options' do
expect(subject.options).to eq expected_options
end
end

end
end

0 comments on commit e5e910f

Please sign in to comment.