chef-sandwich 0.1.0 → 0.2.0

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/NEWS ADDED
@@ -0,0 +1,17 @@
1
+ * sandwich NEWS
2
+ ** 0.2.0 (2011-09-07)
3
+
4
+ - Add support for cookbook_file and template resources
5
+ - Add support for standalone sandwich scripts: sandwich can now be
6
+ used in shebang lines
7
+ - Save file caches and file backups to ~/.cache/sandwich instead of
8
+ /var/cache/chef for non-root users, fixes #3
9
+ - Report correct recipe filename in error messages, fixes #2
10
+
11
+ ** 0.1.0 (2011-08-09)
12
+
13
+ - First release
14
+
15
+ # Local Variables:
16
+ # mode: org
17
+ # End:
data/README.md CHANGED
@@ -16,7 +16,7 @@ For example, installing the package `htop` works like this:
16
16
 
17
17
  If you wanted to install zsh, make it the default shell for root and add a simple alias (without overwriting the existing zshrc), you could do something like this:
18
18
 
19
- cat << EOF | sudo sandwich -l info
19
+ cat <<EOF | sudo sandwich -l info
20
20
  package "zsh"
21
21
 
22
22
  user "root" do
@@ -42,17 +42,22 @@ Sandwich even makes managing cron jobs easy. Say you want to generate a new sign
42
42
  command "fortune bofh-excuses > ~/.signature.sample"
43
43
  end
44
44
  EOF
45
- sudo sandwich -l info -f fortune-signature.rb
45
+ sudo sandwich -l info fortune-signature.rb
46
46
 
47
47
  Installation
48
48
  ------------
49
49
 
50
+ Sandwich is available as a gem called `chef-sandwich`:
51
+
50
52
  sudo gem install chef-sandwich
51
53
 
52
- Limitations
53
- -----------
54
+ There's also a [chef-sandwich package for Debian/Ubuntu](https://launchpad.net/~sometimesfood/+archive/chef-sandwich/). Make sure to install a recent Chef version to go with Sandwich:
54
55
 
55
- Sandwich does not yet properly handle `cookbook_file` and `template` resources. It's on my TODO list, don't worry.
56
+ sudo apt-get install curl lsb-release python-software-properties
57
+ bash < <( curl -sL https://raw.github.com/sometimesfood/chef-admin-essentials/master/contrib/install-chef.sh )
58
+ sudo add-apt-repository ppa:sometimesfood/chef-sandwich
59
+ sudo apt-get update
60
+ sudo apt-get install chef-sandwich
56
61
 
57
62
  Extras
58
63
  ------
data/lib/sandwich/cli.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'mixlib/cli'
1
+ require 'optparse'
2
2
  require 'sandwich/runner'
3
3
  require 'sandwich/version'
4
4
 
@@ -6,52 +6,60 @@ module Sandwich
6
6
  # This class parses ARGV style command line options and starts a
7
7
  # configured {Sandwich::Runner}.
8
8
  class CLI
9
- include Mixlib::CLI
10
-
11
- option :help,
12
- :short => '-h',
13
- :long => '--help',
14
- :description => 'Show this message',
15
- :boolean => true,
16
- :on => :tail,
17
- :show_options => true,
18
- :exit => 0
19
-
20
- option :file,
21
- :short => '-f FILE',
22
- :long => '--file FILE',
23
- :description => 'Read recipe from FILE, defaults to standard input',
24
- :default => '-',
25
- :on => :head
26
-
27
- option :log_level,
28
- :short => '-l LEVEL',
29
- :long => '--log_level LEVEL',
30
- :description => 'Set the log level (debug, info, warn, error, fatal)',
31
- :default => :warn,
32
- :proc => lambda { |l| l.to_sym }
33
-
34
- option :version,
35
- :short => '-v',
36
- :long => '--version',
37
- :description => 'Show sandwich version',
38
- :boolean => true,
39
- :proc => lambda { |v| puts "sandwich: #{Sandwich::Version}" },
40
- :exit => 0
9
+ def initialize
10
+ @options = {}
11
+
12
+ # default log level
13
+ @options[:log_level] = :warn
14
+
15
+ @optparse = OptionParser.new do |opts|
16
+ opts.banner = 'Usage: sandwich [options] [sandwichfile [arguments]]'
17
+
18
+ opts.on_tail('-v',
19
+ '--version',
20
+ 'Show sandwich version') do
21
+ puts "sandwich: #{Sandwich::Version}"
22
+ exit
23
+ end
24
+
25
+ opts.on_tail('-h',
26
+ '--help',
27
+ 'Show this message') do
28
+ puts opts
29
+ exit
30
+ end
31
+
32
+ opts.on('-l',
33
+ '--log_level LEVEL',
34
+ 'Set the log level (debug, info, warn, error, fatal)') do |l|
35
+ @options[:log_level] = l.to_sym
36
+ end
37
+ end
38
+ end
41
39
 
42
40
  # Start Sandwich
43
41
  #
44
42
  # @param [Array] argv ARGV style command line options passed to sandwich
45
43
  # @return [void]
46
44
  def run(argv)
47
- parse_options(argv)
48
- if config[:file] == '-'
49
- runner = Sandwich::Runner.new(STDIN.read)
45
+ unparsed_arguments = @optparse.order!(argv)
46
+
47
+ # use first argument as sandwich script filename...
48
+ recipe_filename = unparsed_arguments.shift
49
+
50
+ # ...check for stdin...
51
+ if recipe_filename.nil? || recipe_filename == '-'
52
+ recipe_filename = '<STDIN>'
53
+ recipe_file = STDIN.read
50
54
  else
51
- file = File.read(config[:file])
52
- runner = Sandwich::Runner.new(file)
55
+ recipe_file = File.read(recipe_filename)
53
56
  end
54
- runner.run(config[:log_level])
57
+
58
+ # ...and pass remaining arguments on to script
59
+ ARGV.replace(unparsed_arguments)
60
+ runner = Sandwich::Runner.new(recipe_file, recipe_filename)
61
+
62
+ runner.run(@options[:log_level])
55
63
  end
56
64
  end
57
65
  end
@@ -0,0 +1,21 @@
1
+ require 'chef/cookbook_version'
2
+
3
+ # Chef::CookbookVersion, monkey patched to use simpler file source
4
+ # paths (always uses local files instead of manifest records)
5
+ class Chef::CookbookVersion
6
+ # Determines the absolute source filename on disk for various file
7
+ # resources from their relative path
8
+ #
9
+ # @param [Chef::Node] node the node object, ignored
10
+ # @param [Symbol] segment the segment of the current resource, ignored
11
+ # @param [String] filename the source file path
12
+ # @param [String] current_filepath the target file path, ignored
13
+ # @return [String] the preferred source filename
14
+ def preferred_filename_on_disk_location(node,
15
+ segment,
16
+ filename,
17
+ current_filepath=nil)
18
+ # keep absolute paths, convert relative paths into absolute paths
19
+ filename.start_with?('/') ? filename : File.join(Dir.getwd, filename)
20
+ end
21
+ end
@@ -5,8 +5,9 @@ class Chef::Recipe
5
5
  # Loads a recipe from a string
6
6
  #
7
7
  # @param [String] string the recipe definition
8
+ # @param [String] filename the filename to use in error messages
8
9
  # @return [void]
9
- def from_string(string)
10
- self.instance_eval(string, 'sandwich', 1)
10
+ def from_string(string, filename = '(eval)')
11
+ self.instance_eval(string, filename, 1)
11
12
  end
12
13
  end
@@ -1,16 +1,19 @@
1
1
  require 'chef'
2
2
  require 'sandwich/recipe'
3
+ require 'sandwich/cookbook_version'
3
4
 
4
5
  module Sandwich
5
6
  # This class constructs a {Chef::Recipe} from a recipe string and
6
7
  # applies it with Chef standalone mode
7
8
  class Runner
8
9
  # @param [String] recipe_string the recipe definition
9
- def initialize(recipe_string)
10
+ def initialize(recipe_string, filename)
10
11
  @client = solo_client
11
- @run_context = Chef::RunContext.new(@client.node, {})
12
- @recipe = Chef::Recipe.new(nil, nil, @run_context)
13
- @recipe.from_string(recipe_string)
12
+ cookbook_name = 'sandwich'
13
+ cookbook_collection = single_cookbook_collection(cookbook_name)
14
+ @run_context = Chef::RunContext.new(@client.node, cookbook_collection)
15
+ @recipe = Chef::Recipe.new(cookbook_name, nil, @run_context)
16
+ @recipe.from_string(recipe_string, filename)
14
17
  end
15
18
 
16
19
  # Run Chef in standalone mode, apply recipe
@@ -21,7 +24,7 @@ module Sandwich
21
24
  # +fatal+)
22
25
  # @return [void]
23
26
  def run(log_level = :warn)
24
- Chef::Log.level = log_level
27
+ configure_chef(log_level)
25
28
  Chef::Runner.new(@run_context).converge
26
29
  end
27
30
 
@@ -32,5 +35,22 @@ module Sandwich
32
35
  client.run_ohai
33
36
  client.build_node
34
37
  end
38
+
39
+ def configure_chef(log_level)
40
+ Chef::Log.level = log_level
41
+
42
+ # don't try to write to /var as non-root user
43
+ unless ENV['USER'] == 'root'
44
+ local_cache = File.join(ENV['HOME'], '.cache', 'sandwich')
45
+ Chef::Config[:cache_options][:path] = File.join(local_cache, 'cache')
46
+ Chef::Config[:file_backup_path] = File.join(local_cache, 'backup')
47
+ end
48
+ end
49
+
50
+ # create a cookbook collection containing a single empty cookbook
51
+ def single_cookbook_collection(cookbook_name)
52
+ cookbook = Chef::CookbookVersion.new(cookbook_name)
53
+ Chef::CookbookCollection.new({ cookbook_name => cookbook })
54
+ end
35
55
  end
36
56
  end
@@ -1,4 +1,4 @@
1
1
  module Sandwich
2
2
  # the current version of sandwich
3
- Version = '0.1.0'
3
+ Version = '0.2.0'
4
4
  end
metadata CHANGED
@@ -1,8 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chef-sandwich
3
3
  version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.1.0
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
6
11
  platform: ruby
7
12
  authors:
8
13
  - Sebastian Boehm
@@ -10,10 +15,24 @@ autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
17
 
13
- date: 2011-08-09 00:00:00 +02:00
18
+ date: 2011-09-07 00:00:00 +02:00
14
19
  default_executable:
15
- dependencies: []
16
-
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: chef
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 25
30
+ segments:
31
+ - 0
32
+ - 9
33
+ version: "0.9"
34
+ type: :runtime
35
+ version_requirements: *id001
17
36
  description: |
18
37
  Sandwich lets you apply Chef recipes to your system without having to
19
38
  worry about cookbooks or configuration.
@@ -29,13 +48,15 @@ files:
29
48
  - Rakefile
30
49
  - README.md
31
50
  - LICENSE
51
+ - NEWS
32
52
  - bin/sandwich
53
+ - lib/sandwich.rb
54
+ - lib/sandwich/version.rb
33
55
  - lib/sandwich/cli.rb
34
56
  - lib/sandwich/recipe.rb
57
+ - lib/sandwich/cookbook_version.rb
35
58
  - lib/sandwich/runner.rb
36
- - lib/sandwich/version.rb
37
- - lib/sandwich.rb
38
- has_rdoc: true
59
+ has_rdoc: yard
39
60
  homepage: https://github.com/sometimesfood/sandwich
40
61
  licenses: []
41
62
 
@@ -49,17 +70,23 @@ required_ruby_version: !ruby/object:Gem::Requirement
49
70
  requirements:
50
71
  - - ">="
51
72
  - !ruby/object:Gem::Version
73
+ hash: 3
74
+ segments:
75
+ - 0
52
76
  version: "0"
53
77
  required_rubygems_version: !ruby/object:Gem::Requirement
54
78
  none: false
55
79
  requirements:
56
80
  - - ">="
57
81
  - !ruby/object:Gem::Version
82
+ hash: 3
83
+ segments:
84
+ - 0
58
85
  version: "0"
59
86
  requirements: []
60
87
 
61
88
  rubyforge_project:
62
- rubygems_version: 1.6.2
89
+ rubygems_version: 1.3.7
63
90
  signing_key:
64
91
  specification_version: 3
65
92
  summary: The easiest way to get started as a chef