tinychef 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tinychef.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Fabrizio Regini
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,32 @@
1
+ # Tinychef
2
+
3
+ Collection of scripts to manage recipes with Chef Solo easily.
4
+
5
+ ## Installation
6
+
7
+ $ gem install tinychef
8
+
9
+ ## Usage
10
+
11
+ To create a tinychef compatible directory:
12
+
13
+ $ tinychef new [dirname]
14
+
15
+ This will create a new *dirname* folder with the following structure:
16
+
17
+ ├── cookbooks
18
+ ├── data_bags
19
+ ├── imported_cookbooks
20
+ ├── nodes
21
+ ├── roles
22
+ └── vendor
23
+
24
+ TODO: Write usage instructions here
25
+
26
+ ## Contributing
27
+
28
+ 1. Fork it
29
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
30
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
31
+ 4. Push to the branch (`git push origin my-new-feature`)
32
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,68 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
4
+
5
+ require 'optparse'
6
+ require 'tinychef'
7
+
8
+ parser = OptionParser.new do |opts|
9
+ opts.banner = "Usage: tinychef COMMAND [ARGS]"
10
+
11
+ opts.separator ""
12
+ opts.separator "Options:"
13
+
14
+ opts.on("-h", "--help", "Show this message") do
15
+ puts opts
16
+ exit
17
+ end
18
+
19
+ opts.separator ""
20
+ opts.separator "Commands:"
21
+ opts.separator " new DIR Creates a new tinychef directory"
22
+ opts.separator " boot HOST Prepares chef-able environment on HOST"
23
+ opts.separator " bag:create NAME Creates an empty databag"
24
+ opts.separator " bag:encrypt NAME Encrypts a databag"
25
+ opts.separator " bag:decrypt NAME Decrypts a databag"
26
+ opts.separator " key:lock Lock the key with a password"
27
+ opts.separator " key:unlock Unlock the key for a work session"
28
+ opts.separator " secure Remove all unsecure files from this folder"
29
+ opts.separator " unsecure Unlock key and decrypt all data bags"
30
+ opts.separator " run HOST NODE [RUN_LIST] Runs recipes for NODE on HOST with optional run_list"
31
+ opts.separator ""
32
+
33
+ end
34
+
35
+ parser.parse!
36
+
37
+ begin
38
+ case ARGV[0]
39
+ when 'new'
40
+ Tinychef::NewDirectory.new(ARGV[1]).write
41
+ when 'boot'
42
+ when 'bag:create'
43
+ when 'bag:encrypt'
44
+ when 'bag:decrypt'
45
+ when 'key:lock'
46
+ when 'key:unlock'
47
+ when 'secure'
48
+ when 'unsecure'
49
+ when 'run'
50
+ options = {}
51
+
52
+ options[:node] = ARGV[1]
53
+
54
+ if ARGV.size == 3 # assume the destination is not given
55
+ options[:dest] = nil ; options[:run_list] = ARGV[2]
56
+ else
57
+ options[:dest] = ARGV[2] ; options[:run_list] = ARGV[3]
58
+ end
59
+
60
+ Tinychef::NodeRun.new(options).start
61
+ else
62
+ puts parser.help
63
+ end
64
+ rescue Tinychef::NodeRun::OptionsError => e
65
+ abort e.message
66
+
67
+
68
+ end
@@ -0,0 +1,5 @@
1
+ require "tinychef/version"
2
+
3
+ module Tinychef; end
4
+
5
+ require 'tinychef/node_run'
@@ -0,0 +1,23 @@
1
+ module Tinychef
2
+
3
+ class Destination
4
+
5
+ attr_reader :user, :host
6
+
7
+ def initialize(dest)
8
+ if dest.include? '@'
9
+ @user, @host = dest.split('@')
10
+ else
11
+ @user = nil ; @host = dest
12
+ end
13
+ end
14
+
15
+ def to_s
16
+ if user.nil?
17
+ host
18
+ else
19
+ "#{user}@#{host}"
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,6 @@
1
+ module Tinychef
2
+ class NewDirectory
3
+ def initialize
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,41 @@
1
+ require 'pathname'
2
+
3
+ module Tinychef
4
+ class Node
5
+
6
+ EXT = '.json'
7
+
8
+ class NofileError < Exception; end
9
+
10
+ attr_reader :name, :path
11
+
12
+ def initialize(name)
13
+ @name = name
14
+ normalize_name
15
+ find_file
16
+ end
17
+
18
+ def host
19
+ path.basename.to_s.gsub(EXT, '')
20
+ end
21
+
22
+ private
23
+
24
+ def find_file
25
+ path = Pathname.new(File.join 'nodes', name)
26
+ path = Pathname.new(name) unless path.exist?
27
+ raise NofileError unless path.exist?
28
+ @path = path
29
+ end
30
+
31
+ def normalize_name
32
+ unless json_extension?
33
+ @name = "#{name}#{EXT}"
34
+ end
35
+ end
36
+
37
+ def json_extension?
38
+ name.end_with? EXT
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,78 @@
1
+ require 'tinychef/node'
2
+ require 'tinychef/destination'
3
+
4
+ module Tinychef
5
+ class NodeRun
6
+
7
+ class OptionsError < Exception
8
+ def initialize
9
+ super <<-EOH
10
+
11
+ Required arguments:
12
+
13
+ NODE: node file to execute
14
+ DEST: ssh username@host string to use to connect to host.
15
+ If missing, this is guessed by the node file name.
16
+ RUN_LIST: an optional RUN_LIST to be passed to chef-solo executable.
17
+
18
+ EOH
19
+ end
20
+ end
21
+
22
+ RECIPES_DIR="recipes"
23
+
24
+ attr_reader :dest, :node, :run_list, :run_list_command
25
+
26
+ def initialize(options)
27
+ node = options[:node]
28
+ dest = options[:dest]
29
+ run_list = options[:run_list]
30
+
31
+ @node = Tinychef::Node.new(node)
32
+ @dest = Tinychef::Destination.new(dest || @node.host)
33
+
34
+ @run_list = run_list
35
+
36
+ prepare_run_list_command
37
+
38
+ rescue => e
39
+ puts e.inspect
40
+ raise OptionsError
41
+ end
42
+
43
+ def start
44
+ sync_files
45
+ ensure_remote_data_bag_file_exits
46
+ run_code
47
+ remove_files
48
+ end
49
+
50
+ private
51
+
52
+ def prepare_run_list_command
53
+ unless run_list.nil?
54
+ @run_list_command = "-o #{run_list}"
55
+ end
56
+ end
57
+
58
+ def ensure_remote_data_bag_file_exits
59
+ end
60
+
61
+ def sync_files
62
+ puts "Destination is #{dest}"
63
+
64
+ system %Q{ rsync -rvcL --exclude .git --exclude vendor/* --exclude *.swp --exclude *.swo . #{dest}:#{RECIPES_DIR} }
65
+ system %Q{ ssh -t #{dest} "sudo mkdir -p /etc/chef " }
66
+ system %Q{ ssh -t #{dest} "sudo cp #{RECIPES_DIR}/data_bags/secret.key /etc/chef/encrypted_data_bag_secret" }
67
+ end
68
+
69
+ def run_code
70
+ system %Q{ ssh -t #{dest} "cd #{RECIPES_DIR} && sudo chef-solo -c solo.rb -j #{node.path} #{run_list_command}" }
71
+ end
72
+
73
+ def remove_files
74
+ system %Q{ ssh -t #{dest} "sudo rm -rf #{RECIPES_DIR} /etc/chef/encrypted_data_bag_secret" }
75
+ end
76
+
77
+ end
78
+ end
@@ -0,0 +1,3 @@
1
+ module Tinychef
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/tinychef/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Fabrizio Regini"]
6
+ gem.email = ["freegenie@gmail.com"]
7
+ gem.description = %q{Simple command line tool for Chef Solo recipes}
8
+ gem.summary = %q{Simple command line tool for Chef Solo recipes}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "tinychef"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Tinychef::VERSION
17
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tinychef
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Fabrizio Regini
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-29 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Simple command line tool for Chef Solo recipes
15
+ email:
16
+ - freegenie@gmail.com
17
+ executables:
18
+ - tinychef
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - LICENSE
25
+ - README.md
26
+ - Rakefile
27
+ - bin/tinychef
28
+ - lib/tinychef.rb
29
+ - lib/tinychef/destination.rb
30
+ - lib/tinychef/new_directory.rb
31
+ - lib/tinychef/node.rb
32
+ - lib/tinychef/node_run.rb
33
+ - lib/tinychef/version.rb
34
+ - tinychef.gemspec
35
+ homepage: ''
36
+ licenses: []
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 1.8.23
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: Simple command line tool for Chef Solo recipes
59
+ test_files: []
60
+ has_rdoc: