puppetsdb 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/bin/puppetsdb ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'puppetsdb/cli'
4
+
5
+ PuppetSDB::CLI.run
@@ -0,0 +1,126 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'subcommand'
5
+ require 'puppetsdb'
6
+ require 'pp'
7
+
8
+ include Subcommands
9
+
10
+ module PuppetSDB
11
+ class CLI
12
+ class << self
13
+ COL_WIDTH = 40
14
+ def ask_confirm(question)
15
+ print "Are you sure you want to delete this node? (yes/[no]) "
16
+ answer = $stdin.gets.chomp
17
+ accepted_confirms = ['y','yes']
18
+ accepted_confirms.include?(answer.downcase)
19
+ end
20
+
21
+ def run
22
+ options = {}
23
+ global_cmd = global_options do |opts|
24
+ opts.banner = "Usage: ", $PROGRAM_NAME, " [subcommand [options]]"
25
+ opts.description = "CLI for Puppet ENC on Amazon's SimpleDB"
26
+ opts.separator ""
27
+ opts.separator "Global options are:"
28
+ opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
29
+ options[:verbose] = v
30
+ end
31
+ end
32
+
33
+ list_cmd = command :list do |opts|
34
+ opts.banner = "Usage: list"
35
+ opts.description = "List all nodes in the ENC"
36
+ end
37
+ listdomains_cmd = command :listdomains do |opts|
38
+ opts.banner = "Usage: listdomains"
39
+ opts.description = "List all SimpleDB domains in the AWS account"
40
+ end
41
+ createdomain_cmd = command :createdomain do |opts|
42
+ opts.banner = "Usage: createdomain domain_name"
43
+ opts.description = "Create a SimpleDB domain"
44
+ end
45
+ deletedomain_cmd = command :deletedomain do |opts|
46
+ opts.banner = "Usage: deletedomain [options] domain_name"
47
+ opts.description = "Delete a SimpleDB domain"
48
+ opts.on("-f", "--force", "Do not prompt for confirmation") do |v|
49
+ options[:deletedomain_force] = v
50
+ end
51
+ opts.on("-i", "--items", "Delete the domain and all associated items") do |v|
52
+ options[:deletedomain_items] = v
53
+ end
54
+ end
55
+ get_cmd = command :get do |opts|
56
+ opts.banner = "Usage: get [options] nodename"
57
+ opts.description = "Get YAML data for a node"
58
+ end
59
+ set_cmd = command :set do |opts|
60
+ opts.banner = "Usage: set [options] nodename [yaml_file]"
61
+ opts.description = "Set YAML data for a node. If yaml_file is not specified, the yaml data is read from STDIN."
62
+ end
63
+ delete_cmd = command :delete do |opts|
64
+ opts.banner = "Usage: delete [options] nodename [...]"
65
+ opts.description = "Delete the ENC record(s)"
66
+ opts.on("-f", "--force", "Do not prompt for confirmation") do |v|
67
+ options[:delete_force] = v
68
+ end
69
+ end
70
+
71
+ cmd = opt_parse
72
+ puppet_enc = PuppetSDB.new
73
+ case cmd
74
+ when "list"
75
+ strfmt = "%-#{COL_WIDTH.to_s}s %s"
76
+ puts strfmt.%(['Node','Time Modified'])
77
+ puts strfmt.%(['-'*COL_WIDTH,'-'*COL_WIDTH])
78
+ puppet_enc.list_items_detailed.each do |node|
79
+ puts strfmt.%([node[0],node[1]['Time Modified']])
80
+ end
81
+ when "listdomains"
82
+ strfmt = "%s"
83
+ puts strfmt.%(['Domain'])
84
+ puts strfmt.%(['-'*COL_WIDTH])
85
+ puppet_enc.list_domains.each { |domain| puts strfmt.%([domain]) }
86
+ when "createdomain"
87
+ domain_name = ARGV.shift
88
+ puppet_enc.create_domain(domain_name)
89
+ puts "Domain create successfully"
90
+ when "deletedomain"
91
+ domain_name = ARGV.shift
92
+ unless options[:deletedomain_force]
93
+ (puts "Cancelled..."; exit 0) unless ask_confirm("Are you sure you want to delete this domain? (yes/[no]) ")
94
+ end
95
+ if options[:deletedomain_items]
96
+ puppet_enc.delete_domain(domain_name, with_items=true)
97
+ else
98
+ puppet_enc.delete_domain(domain_name, with_items=false)
99
+ end
100
+ puts "Domain deleted successfully"
101
+ when "get"
102
+ (puts get_cmd.call.help; exit 1) if ARGV.empty?
103
+ yaml = puppet_enc.get_node_yaml(ARGV[0])
104
+ puts yaml unless yaml.empty?
105
+ when "set"
106
+ (puts set_cmd.call.help; exit 1) if ARGV.empty?
107
+ nodename = ARGV.shift
108
+ if ARGV.empty?
109
+ yaml_data = $stdin.read
110
+ else
111
+ yaml_file = ARGV.shift
112
+ yaml_data = File.new(yaml_file).read
113
+ end
114
+ puppet_enc.write_node_data(nodename, yaml_data)
115
+ when "delete"
116
+ (puts delete_cmd.call.help; exit 1) if ARGV.empty?
117
+ unless options[:delete_force]
118
+ (puts "Cancelled..."; exit 0) unless ask_confirm("Are you sure you want to delete this node? (yes/[no]) ")
119
+ end
120
+ ARGV.each { |node| puppet_enc.delete_item(node) }
121
+ end
122
+ (add_subcommand_help; puts global_cmd.help; exit 1) unless cmd
123
+ end
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,3 @@
1
+ module PuppetSDB
2
+ VERSION = "0.0.1"
3
+ end
data/lib/puppetsdb.rb ADDED
@@ -0,0 +1,119 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'pathname'
4
+ require 'base64'
5
+ require 'rubygems'
6
+ require 'aws'
7
+ require 'yaml'
8
+
9
+ module PuppetSDB
10
+ class PuppetSDB
11
+ attr_accessor :simpledb, :domain
12
+
13
+ def initialize(domain=nil)
14
+ cfg_file_paths = ['/etc/puppetsdb/config.yml']
15
+ if ENV['HOME']
16
+ cfg_file_paths << '~/.puppetsdb/config.yml'
17
+ else
18
+ cfg_file_paths << '~puppet/.puppetsdb/config.yml'
19
+ end
20
+ conf = {}
21
+ cfg_file_paths.each do |file|
22
+ file = File.expand_path(file)
23
+ next unless File.exists?(file)
24
+ conf.merge!(YAML.load(File.read(file)))
25
+ end
26
+
27
+ AWS.config(conf['aws'])
28
+ domain ||= conf['puppetsdb']['domain']
29
+ @simpledb = AWS::SimpleDB.new
30
+ @domain = domain
31
+ end
32
+
33
+ def list_domains
34
+ @simpledb.domains.collect(&:name)
35
+ end
36
+
37
+ def create_domain(domain_name)
38
+ @simpledb.domains.create(domain_name)
39
+ end
40
+
41
+ def delete_domain(domain_name, with_items=false)
42
+ if with_items
43
+ @simpledb.domains[domain_name].delete!
44
+ else
45
+ @simpledb.domains[domain_name].delete
46
+ end
47
+ end
48
+
49
+ def list_items
50
+ @simpledb.domains[@domain].items.collect(&:name)
51
+ end
52
+
53
+ def list_items_detailed
54
+ attrs = ['Time Modified']
55
+ results = []
56
+ @simpledb.domains[@domain].items.select(*attrs).each do |item_data|
57
+ results << [item_data.name, item_data.attributes]
58
+ end
59
+ results
60
+ end
61
+
62
+ def list_item_attrs(item)
63
+ @simpledb.domains[@domain].items[item].attributes.collect(&:name)
64
+ end
65
+
66
+ def delete_item(item)
67
+ @simpledb.domains[@domain].items[item].delete
68
+ end
69
+
70
+ def get_item_attrs(item)
71
+ @simpledb.domains[@domain].items[item].data.attributes
72
+ end
73
+
74
+ def get_node_yaml(item)
75
+ attrs = get_item_attrs(item)
76
+ if attrs.empty?
77
+ ""
78
+ else
79
+ decode_base64(attrs['Node Data'][0])
80
+ end
81
+ end
82
+
83
+ def update_item(item, attrs)
84
+ @simpledb.domains[@domain].items[item].attributes.put(
85
+ :replace => attrs)
86
+ end
87
+
88
+ def encode_yaml(yaml)
89
+ Base64.encode64(yaml)
90
+ end
91
+
92
+ def decode_base64(base64)
93
+ Base64.decode64(base64)
94
+ end
95
+
96
+ def get_timestamp_utc
97
+ Time.now.utc
98
+ end
99
+
100
+ def construct_attrs(yaml)
101
+ attrs = {'Time Modified' => get_timestamp_utc,
102
+ 'Node Data' => encode_yaml(yaml)}
103
+ end
104
+
105
+ def validate_yaml(yaml)
106
+ begin
107
+ YAML.load(yaml)
108
+ rescue
109
+ puts "Validation of YAML failed"
110
+ raise
111
+ end
112
+ end
113
+
114
+ def write_node_data(nodename, yaml)
115
+ validate_yaml(yaml)
116
+ update_item(nodename, construct_attrs(yaml))
117
+ end
118
+ end
119
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: puppetsdb
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Brian Wong
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2012-10-26 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: subcommand
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ name: aws-sdk
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ segments:
40
+ - 0
41
+ version: "0"
42
+ type: :runtime
43
+ version_requirements: *id002
44
+ description: A simple interface to Puppet's ENC stored on Amazon's SimpleDB
45
+ email: bwong114@gmail.com
46
+ executables:
47
+ - puppetsdb
48
+ extensions: []
49
+
50
+ extra_rdoc_files: []
51
+
52
+ files:
53
+ - lib/puppetsdb.rb
54
+ - lib/puppetsdb/cli.rb
55
+ - lib/puppetsdb/version.rb
56
+ - bin/puppetsdb
57
+ has_rdoc: true
58
+ homepage: http://rubygems.org/gems/puppetsdb
59
+ licenses: []
60
+
61
+ post_install_message:
62
+ rdoc_options: []
63
+
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ requirements: []
81
+
82
+ rubyforge_project:
83
+ rubygems_version: 1.3.6
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: Puppet ENC script stored against Amazon's SimpleDB
87
+ test_files: []
88
+