chef-solo-wrapper 0.0.3 → 0.0.4

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.
Files changed (4) hide show
  1. data/bin/cs +69 -58
  2. data/lib/config_helper.rb +151 -0
  3. data/lib/cookbooks_fetcher.rb +22 -0
  4. metadata +20 -4
data/bin/cs CHANGED
@@ -16,7 +16,8 @@
16
16
  # See the License for the specific language governing permissions and
17
17
  # limitations under the License.
18
18
 
19
- CHEF_SOLO_WRAPPER_VERSION = '0.0.3'
19
+ CHEF_SOLO_WRAPPER_VERSION = '0.0.4'
20
+ COOKBOOKS_SRC_DEST = '/usr/src/chef-cookbooks'
20
21
 
21
22
  require 'rubygems'
22
23
  require 'trollop'
@@ -36,74 +37,84 @@ EOS
36
37
  opt :config, "Use alternate Chef Solo configuration (default used, ~/solo.rb.)", :short => "-c" # flag --config, default false
37
38
  opt :json, "Use alternate Chef Solo JSON data (default used, ~/node.json.)", :short => "-j", :type => String # flag --json, default false
38
39
  opt :test, "Tests requiring chef only plus implies a dry run", :short => "-t" # flag --test, default false
39
- opt :setup, "Installs the Chef Rubygem." # flag --test, default false
40
+ opt :setup, "Installs the Chef Rubygem.", :type => String # flag --test, default false
41
+ opt :defaults, "Setups up configuration for default/initial.", :default => false # flag --test, default false
42
+ opt :fetch, "Fetches cookbooks.", :short => "-f", :type => String # flag --fetch, default false
40
43
  opt :dry, "Dry run only, don't run chef-solo.", :short => "-d" # flag --dry, default false
41
44
  opt :run, "Use alernative run_list for chef-solo run.", :short => "-r", :type => String # flag --run, default false
42
45
  opt :write, "Write back to local JSON file.", :short => "-w" # flag --write, default false
43
46
  opt :loglevel, "The Chef log level to use: debug, info, warn, error, fatal", :short => "-l", :default => "info", :type => String # flag --loglevel, default info
44
47
  opt :verbose, "Verbose mode.", :short => "-v" # flag --verbose, default false
45
- opt :debug, "Debug mode." # flag --debug, default faulse
46
- opt :help, "Print usage info and exit.", :short => "-h"
48
+ opt :debug, "Debug mode.", :default => false # flag --debug, default faulse
49
+ opt :archive, "Checkout cookbooks in archive mode." # flag --debug, default faulse
50
+ opt :help, "Print usage info and exit.", :short => "-h"
47
51
  end
48
52
  puts "options: #{opts.to_json}" unless !(opts.verbose || opts.debug and !opts.help)
49
- puts 'chef-solo-wrapper '+CHEF_SOLO_WRAPPER_VERSION
53
+ puts "chef-solo-wrapper #{CHEF_SOLO_WRAPPER_VERSION}"
54
+
55
+ if opts[:defaults]
56
+ SETUP_DEFAULTS = true
57
+ else
58
+ SETUP_DEFAULTS = false
59
+ end
60
+
61
+ if opts[:debug]
62
+ DEBUG = true
63
+ else
64
+ DEBUG = false
65
+ end
50
66
 
51
67
  solo = false
52
68
  server = false
53
69
 
54
- if opts.test
55
- begin
56
- puts 'Testing require of chef.'
57
- require 'chef'
58
- rescue
59
- puts 'Failed to require Chef RubyGem!'
60
- exit 1
61
- end
62
- puts 'Test passed.'
70
+ json_file = '/etc/chef/node.json'
71
+ solo_file = '/etc/chef/solo.rb'
72
+
73
+ require File.join(File.dirname(__FILE__), '../lib/config_helper.rb')
74
+ config = ConfigHelper.new(SETUP_DEFAULTS, DEBUG)
75
+
76
+ # Test mode (exits after test)
77
+ if opts[:test]
78
+ config.test_setup
63
79
  exit
64
80
  end
65
81
 
66
- if opts.setup
67
- begin
68
- puts 'Installing Chef RubyGem...'
69
- system('gem install chef --no-rdoc --no-ri')
70
- rescue
71
- puts 'Failed to install Chef Rubygem!'
72
- exit 1
82
+ # Setup routines
83
+ if opts[:setup]
84
+ case opts[:setup]
85
+ when 'chef'
86
+ config.install_chef
87
+ when 'rc'
88
+ config.install_rest_connection
89
+ when 'config'
90
+ config.setup_solo_rb(solo_file)
91
+ when 'node'
92
+ config.setup_node_json(json_file)
93
+ when 'all'
94
+ config.install_chef
95
+ config.install_rest_connection
96
+ config.setup_node_json(json_file)
97
+ config.setup_solo_rb(solo_file)
98
+ when 'show'
99
+ config.show(solo_file, json_file)
100
+ exit
73
101
  end
74
- puts 'Chef successfully installed.'
75
- puts 'Install rest_connection (y/n)?'
76
- install_rc = false
77
- install_rc = gets.chomp
78
- ( puts 'Installing rest_connection RubyGem...'; system('gem install rest_connection --no-rdoc --no-ri') ) unless install_rc != 'y'
102
+ puts "\nSetup complete."
79
103
  exit
80
104
  end
81
105
 
82
- # ensure a solo.rb exists for run
83
- if File.file?('/etc/chef/solo.rb')
84
- solo = '/etc/chef/solo.rb'
85
- else
86
- puts ' DEBUG: /etc/chef/solo.rb: not found.' unless !opts.debug
87
- end
88
- if File.file?("#{ENV['HOME']}/solo.rb")
89
- solo = "#{ENV['HOME']}/solo.rb"
90
- else
91
- puts ' DEBUG: ~/solo.rb: not found.' unless !opts.debug
92
- end
93
- unless solo
94
- puts 'FATAL: No solo.rb file found (see http://wiki.opscode.com/display/chef/Chef+Solo), exiting.'
95
- exit 1
96
- else
97
- puts "==> Using #{solo}." unless !opts.debug
98
- if File.zero?(solo) then
99
- puts "FATAL: #{solo} is empty, exiting."
100
- exit 1
101
- end
102
- puts File.new(solo, 'r').read unless !opts.debug
106
+ # Fetch cookbooks option
107
+ # e.g. cs --fetch git://github.com/flaccid/cookbooks_public.git
108
+ if opts[:fetch]
109
+ require File.join(File.dirname(__FILE__), '../lib/cookbooks_fetcher.rb')
110
+ cookbooks_fetcher = CookbooksFetcher.new
111
+ cookbooks_fetcher.fetch(opts[:fetch], COOKBOOKS_SRC_DEST)
103
112
  end
104
113
 
114
+ config.pre_checks
115
+
105
116
  # get json if available
106
- if opts.json
117
+ if opts[:json]
107
118
  attributes = File.new(opts.json, "r").read
108
119
  else
109
120
  if File.file?('/etc/chef/node.json')
@@ -120,7 +131,7 @@ else
120
131
  end
121
132
 
122
133
  # when a rs server is specified
123
- if opts.server
134
+ if opts[:server]
124
135
 
125
136
  # import rest_connection
126
137
  puts 'Importing RestConnection RubyGem.' unless !opts.verbose
@@ -193,12 +204,12 @@ if opts.write and server_attributes
193
204
  fh.close
194
205
  end
195
206
 
196
- # prepare options
197
- chef_config = " -c #{opts.config}" unless !opts.config
198
- chef_json = " -j #{opts.json}" unless !opts.json
207
+ # pre append options
208
+ chef_config = " -c #{opts[:config]}" if opts[:config]
209
+ chef_json = " -j #{opts[:json]}" if opts[:json]
199
210
 
200
- # depict if sandbox chef-solo binary is used
201
- if opts.sandbox
211
+ # set the chef-solo command depending if rs sandbox
212
+ if opts[:sandbox]
202
213
  cs = '/opt/rightscale/sandbox/bin/ruby /opt/rightscale/sandbox/bin/chef-solo'
203
214
  else
204
215
  cs = 'chef-solo'
@@ -206,21 +217,21 @@ end
206
217
 
207
218
  # build chef solo command
208
219
  cmd = "#{cs}#{chef_config}#{chef_json} --log_level #{opts.loglevel} || ( echo 'Chef run failed!'; cat /var/chef-solo/chef-stacktrace.out; exit 1 )"
209
- puts " DEBUG: running #{cmd}" unless !opts.debug
220
+ puts " DEBUG: running #{cmd}" if opts[:debug]
210
221
 
211
222
  # import chef
212
- puts 'Importing Chef RubyGem.' unless !opts.verbose
223
+ puts 'Importing Chef RubyGem.' if opts[:verbose]
213
224
  require 'chef'
214
225
 
215
226
  # prepend sudo if not run as root
216
227
  if Process.uid != 0
228
+ puts " DEBUG: Non-root user, appending sudo (#{cmd})." if opts[:debug]
217
229
  cmd.insert(0, 'sudo ')
218
- puts " DEBUG: Non-root user, appending sudo (#{cmd})." unless !opts.debug
219
230
  end
220
231
 
221
232
  # finally, run chef-solo
222
233
  puts 'Starting Chef Solo.' unless !(opts.verbose || opts.debug)
223
- unless opts.dry
234
+ unless opts[:dry]
224
235
  system(cmd)
225
236
  else
226
237
  puts 'Dry run only, exiting.'
@@ -0,0 +1,151 @@
1
+ class ConfigHelper
2
+ # constructor method
3
+ def initialize(setup_defaults, debug)
4
+ @setup_defaults, @debug = setup_defaults, debug
5
+ end
6
+
7
+ # accessor methods
8
+ def install_gem(gem)
9
+ install_opts = '--no-rdoc --no-ri'
10
+ system("gem install #{gem} #{install_opts}")
11
+ Gem.clear_paths
12
+ end
13
+
14
+ def show(solo_file, json_file)
15
+ puts
16
+ puts '* Chef Solo Setup *'
17
+ puts
18
+ puts "#{solo_file}:"
19
+ puts '--'
20
+ puts File.open(solo_file, "r").read
21
+ puts '--'
22
+ puts
23
+ puts "#{json_file}:"
24
+ puts '--'
25
+ puts File.open(json_file, "r").read
26
+ puts '--'
27
+ puts
28
+ end
29
+
30
+ def install_chef
31
+ puts
32
+ puts '=> Setting up Chef Solo.'
33
+ puts 'Installing Chef RubyGem...'
34
+ gem = 'chef'
35
+ begin
36
+ puts "#{gem} already installed, version: #{Gem::Specification.find_by_name(gem).version}."
37
+ rescue Gem::LoadError
38
+ install_gem(gem)
39
+ rescue
40
+ install_gem(gem) unless Gem.available?(gem)
41
+ rescue
42
+ raise 'Failed to install Chef Rubygem!'
43
+ end
44
+ puts
45
+ end
46
+
47
+ def setup_solo_rb(file, auto=@setup_defaults)
48
+ puts "=> Setting up #{file}."
49
+ if auto
50
+ default_solo = true
51
+ else
52
+ puts ' Use default solo.rb [y/n] <enter> ?'
53
+ default_solo = false
54
+ default_solo = gets.chomp
55
+ end
56
+ if default_solo
57
+ File.open(file, "w") {|f| f.write 'file_cache_path "/var/chef-solo"'+"\n"+'cookbook_path [ "/usr/src/chef-cookbooks/default" ]'+"\n"'json_attribs "/etc/chef/node.json"'+"\n"}
58
+ system("mkdir -p /usr/src/chef-cookbooks/default")
59
+ File.new('/usr/src/chef-cookbooks/default/chefignore', "w").close
60
+ return
61
+ end
62
+ puts " Type or paste your solo.rb (type EOF then press <enter> to finish):"
63
+ $/ = "EOF"
64
+ stdin = STDIN.gets
65
+ File.open(file, "w") {|f| f.write stdin }
66
+ end
67
+
68
+ def setup_node_json(file, auto=@setup_defaults)
69
+ puts "=> Setting up #{file}."
70
+ if auto
71
+ create_empty = 'y'
72
+ else
73
+ puts ' Create empty node.json [y/n] <enter> ?'
74
+ create_empty = 'n'
75
+ create_empty = gets.chomp
76
+ end
77
+ if create_empty.downcase == 'y'
78
+ json = '{}'
79
+ else
80
+ puts " Type or paste your node.json (type EOF then press <enter> to finish):"
81
+ $/ = "EOF"
82
+ json = STDIN.gets
83
+ end
84
+ File.open(file, "w") {|f| f.write json.gsub('EOF', '')}
85
+ end
86
+
87
+ def install_rest_connection(auto=false)
88
+ puts '=> Setting up rest_connection.'
89
+ begin
90
+ if auto or @setup_defaults
91
+ install_rc = 'y'
92
+ else
93
+ puts ' Install rest_connection [y/n] <enter> ?'
94
+ install_rc = 'n'
95
+ install_rc = gets.chomp
96
+ end
97
+ if install_rc.downcase == 'y'
98
+ puts 'Installing rest_connection RubyGem...'
99
+ gem = 'rest_connection'
100
+ begin
101
+ puts "#{gem} already installed, version: #{Gem::Specification.find_by_name(gem).version}."
102
+ rescue Gem::LoadError
103
+ install_gem(gem)
104
+ rescue
105
+ install_gem(gem) unless Gem.available?(gem)
106
+ rescue
107
+ raise 'Failed to install rest_connection!'
108
+ end
109
+ end
110
+ end
111
+ puts
112
+ end
113
+
114
+ def test_setup
115
+ begin
116
+ puts 'Testing require of chef.'
117
+ require 'chef'
118
+ rescue
119
+ puts 'Failed to require Chef RubyGem!'
120
+ exit 1
121
+ end
122
+ puts 'Test passed.'
123
+ exit
124
+ end
125
+
126
+ def pre_checks
127
+ # ensure a solo.rb exists for run
128
+ if File.file?('/etc/chef/solo.rb')
129
+ solo = '/etc/chef/solo.rb'
130
+ else
131
+ puts ' DEBUG: /etc/chef/solo.rb: not found.' if @debug
132
+ end
133
+ if File.file?("#{ENV['HOME']}/solo.rb")
134
+ solo = "#{ENV['HOME']}/solo.rb"
135
+ else
136
+ puts ' DEBUG: ~/solo.rb: not found.' if @debug
137
+ end
138
+ unless solo
139
+ puts 'FATAL: No solo.rb file found (see http://wiki.opscode.com/display/chef/Chef+Solo), exiting.'
140
+ exit 1
141
+ else
142
+ puts "==> Using #{solo}." if @debug
143
+ if File.zero?(solo) then
144
+ puts "FATAL: #{solo} is empty, exiting."
145
+ exit 1
146
+ end
147
+ puts File.new(solo, 'r').read if @debug
148
+ end
149
+ end
150
+
151
+ end
@@ -0,0 +1,22 @@
1
+ class CookbooksFetcher
2
+ # constructor method
3
+ def initialize()
4
+
5
+ end
6
+
7
+ def fetch(cookbooks_src, cookbooks_dest, archive=true)
8
+ if Process.uid != 0
9
+ cookbooks_dest = "#{File.expand_path("~")}/chef-cookbooks"
10
+ end
11
+ system("mkdir -p #{cookbooks_dest}")
12
+ puts "Cookbooks destination: #{cookbooks_dest}."
13
+ if archive
14
+ system("cd #{cookbooks_dest}; git clone --depth=1 #{cookbooks_src}")
15
+ #system("git archive --format=tar --remote=#{opts[:fetch]} master | tar -xf -")
16
+ else
17
+ system("cd #{cookbooks_dest}; git clone #{cookbooks_src}")
18
+ end
19
+ exit
20
+ end
21
+
22
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chef-solo-wrapper
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Chris Fordham
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-06-02 00:00:00 Z
18
+ date: 2012-06-03 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: json
@@ -31,6 +31,20 @@ dependencies:
31
31
  version: "0"
32
32
  type: :runtime
33
33
  version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: trollop
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :runtime
47
+ version_requirements: *id002
34
48
  description: A basic wrapper for chef-solo with RightScale integration.
35
49
  email: chris@xhost.com.au
36
50
  executables:
@@ -40,6 +54,8 @@ extensions: []
40
54
  extra_rdoc_files: []
41
55
 
42
56
  files:
57
+ - lib/config_helper.rb
58
+ - lib/cookbooks_fetcher.rb
43
59
  - bin/cs
44
60
  homepage: https://github.com/flaccid/chef-solo-wrapper
45
61
  licenses: []