capchef 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -35,6 +35,9 @@ Your Capfile might look something like the following:
35
35
  require 'capchef/chef_recipes'
36
36
  require 'capchef/utility_recipes'
37
37
 
38
+ # Uncomment the following if running scripts under sudo isn't allowed
39
+ #Capchef.use_sudo = false
40
+
38
41
  role :node, *Capchef.all_nodes
39
42
 
40
43
  # Define the path to the chef-solo executable if it's not in /usr/bin or /usr/local/bin
@@ -60,7 +60,7 @@ Capistrano::Configuration.instance.load do
60
60
  Find.find('roles') do |role_file|
61
61
  # TODO: This does not work (how do I specify a role in JSON?
62
62
  if role_file.match(/\.yml$/)
63
- data = YAML.load(ERB.new(File.read(role_file)).result(binding)).to_json
63
+ data = YAML.load(ERB.new(File.read(role_file), nil, '-').result(binding)).to_json
64
64
  new_role_file = role_file.sub(/\.yml$/, '.json')
65
65
  tar.add_file_simple(new_role_file, :size=>data.size, :mode=>0644, :mtime=>File.mtime(role_file)) { |f| f.write(data) }
66
66
  else
@@ -71,7 +71,14 @@ Capistrano::Configuration.instance.load do
71
71
  gzip.close
72
72
  put sio_roles_tgz.string, remote_roles_tgz, :hosts => valid_hosts
73
73
  end
74
- Capchef.surun(self, "mkdir -p /etc/chef; cd /etc/chef; rm -rf cookbooks roles; tar zxf #{remote_cookbooks_tgz}; tar zxf #{remote_roles_tgz}; chef-solo -c #{remote_solo_file} -j #{remote_node_file}", :hosts => valid_hosts)
74
+ Capchef.surun(self, [
75
+ 'mkdir -p /etc/chef',
76
+ 'cd /etc/chef',
77
+ 'rm -rf cookbooks roles',
78
+ "tar zxf #{remote_cookbooks_tgz}",
79
+ "tar zxf #{remote_roles_tgz}",
80
+ "chef-solo -c #{remote_solo_file} -j #{remote_node_file}"
81
+ ], :hosts => valid_hosts)
75
82
  ensure
76
83
  tmp_cookbooks_tgz.unlink
77
84
  run "rm -rf #{remote_tmpdir}"
data/lib/capchef.rb CHANGED
@@ -27,36 +27,51 @@ module Capchef
27
27
  @path = "#{dir}:#{path}"
28
28
  end
29
29
 
30
- # Runs +command+ as root invoking the command with su -c
31
- # and handling the root password prompt.
30
+ def use_sudo=(val)
31
+ @use_sudo = val
32
+ end
33
+
34
+ def use_sudo?
35
+ return true if ENV['CAPCHEF_SUDO'] == 'true'
36
+ return false if ENV['CAPCHEF_SUDO'] == 'false'
37
+ # Default to true if unset
38
+ @use_sudo = true if @use_sudo.nil?
39
+ return @use_sudo
40
+ end
41
+
42
+ # Runs +command+ as root invoking the command with 'su -c' and handling the root password prompt.
32
43
  #
33
- # surun "/etc/init.d/apache reload"
44
+ # surun cap, "/etc/init.d/apache reload"
34
45
  # # Executes
35
46
  # # su - -c '/etc/init.d/apache reload'
36
47
  #
37
- def surun(cap, command, options={})
38
- @root_password ||= cap.fetch(:root_password, Capistrano::CLI.password_prompt("root password: "))
39
- cap.run("su -c 'cd; PATH=#{path}; #{command}'", options) do |channel, stream, output|
40
- puts "[#{channel[:host]}] #{output}" if output
41
- channel.send_data("#{@root_password}\n") if output && output =~ /^Password:/
42
- yield channel, stream, output if block_given?
48
+ # surun cap, 'my_install' do |channel, stream, output|
49
+ # channel.send_data("\n") if output && output =~ /to continue/
50
+ # channel.send_data("y\n") if output && output =~ /replace/
51
+ # end
52
+ def surun(cap, command, options={}, &block)
53
+ if use_sudo?
54
+ if command.kind_of?(Array)
55
+ my_surun_script(cap, 'surun', command, nil, options, &block)
56
+ else
57
+ sucmd = "#{cap.sudo} PATH=#{path} #{command}"
58
+ cap.run(sucmd, options)
59
+ end
60
+ else
61
+ @root_password ||= cap.fetch(:root_password, Capistrano::CLI.password_prompt("root password: "))
62
+ command = command.join(';') if command.kind_of?(Array)
63
+ sucmd = "su -c 'cd; PATH=#{path}; #{command}'"
64
+ cap.run(sucmd, options) do |channel, stream, output|
65
+ puts "[#{channel[:host]}] #{output}" if output
66
+ channel.send_data("#{@root_password}\n") if !use_sudo? && output && output =~ /^Password:/
67
+ yield channel, stream, output if block_given?
68
+ end
43
69
  end
44
70
  end
45
71
 
46
- def surun_script(cap, script, options={})
72
+ def surun_script(cap, script, args=nil, options={}, &block)
47
73
  raise "No such file: #{script}" unless File.exist?(script)
48
- basename = File.basename(script)
49
- script_dir = "#{tmpdir}/#{basename}.#$$"
50
- remote_script = "#{script_dir}/#{basename}"
51
- cap.run "mkdir #{script_dir}", options
52
- cap.upload script, remote_script, options
53
- cap.run "chmod 0755 #{remote_script}", options
54
- if block_given?
55
- yield remote_script
56
- else
57
- surun cap, remote_script, options
58
- end
59
- cap.run "rm -rf #{script_dir}", options
74
+ my_surun_script(cap, script, nil, args, options, &block)
60
75
  end
61
76
 
62
77
  def nodes_config
@@ -67,7 +82,7 @@ module Capchef
67
82
  nodes_file = ENV['NODES_FILE'] || 'nodes.yml'
68
83
  raise "No file #{nodes_file}" unless File.exist?(nodes_file)
69
84
  @config_pass += 1
70
- config = YAML.load(ERB.new(File.read(nodes_file)).result(binding))
85
+ config = YAML.load(ERB.new(File.read(nodes_file), nil, '-').result(binding))
71
86
  @config_pass -= 1
72
87
  config
73
88
  end
@@ -77,4 +92,26 @@ module Capchef
77
92
  return [] if @config_pass && @config_pass > 1
78
93
  return nodes_config.keys
79
94
  end
95
+
96
+ private
97
+
98
+ # Hack way to allow either a script or a list of commands
99
+ def my_surun_script(cap, script, command_array, args, options, &block)
100
+ basename = File.basename(script)
101
+ script_dir = "#{tmpdir}/#{basename}.#$$"
102
+ remote_script = "#{script_dir}/#{basename}"
103
+ cap.run "mkdir #{script_dir}", options
104
+ if command_array
105
+ commands = command_array.join("\n")
106
+ script_text = "#!/bin/sh\n#{commands}\n"
107
+ cap.put script_text, remote_script, options
108
+ else
109
+ cap.upload script, remote_script, options
110
+ end
111
+ cap.run "chmod 0755 #{remote_script}", options
112
+ cmd = remote_script
113
+ cmd += ' ' + args if args
114
+ surun cap, cmd, options, &block
115
+ cap.run "rm -rf #{script_dir}", options
116
+ end
80
117
  end
metadata CHANGED
@@ -1,12 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capchef
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 0
8
- - 3
9
- version: 0.0.3
4
+ prerelease:
5
+ version: 0.0.4
10
6
  platform: ruby
11
7
  authors:
12
8
  - Brad Pardee
@@ -14,18 +10,17 @@ autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
12
 
17
- date: 2011-09-08 00:00:00 -04:00
13
+ date: 2011-09-09 00:00:00 -04:00
18
14
  default_executable:
19
15
  dependencies:
20
16
  - !ruby/object:Gem::Dependency
21
17
  name: capistrano
22
18
  prerelease: false
23
19
  requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
24
21
  requirements:
25
22
  - - ">="
26
23
  - !ruby/object:Gem::Version
27
- segments:
28
- - 0
29
24
  version: "0"
30
25
  type: :runtime
31
26
  version_requirements: *id001
@@ -33,11 +28,10 @@ dependencies:
33
28
  name: json
34
29
  prerelease: false
35
30
  requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
36
32
  requirements:
37
33
  - - ">="
38
34
  - !ruby/object:Gem::Version
39
- segments:
40
- - 0
41
35
  version: "0"
42
36
  type: :runtime
43
37
  version_requirements: *id002
@@ -45,11 +39,10 @@ dependencies:
45
39
  name: minitar
46
40
  prerelease: false
47
41
  requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
48
43
  requirements:
49
44
  - - ">="
50
45
  - !ruby/object:Gem::Version
51
- segments:
52
- - 0
53
46
  version: "0"
54
47
  type: :runtime
55
48
  version_requirements: *id003
@@ -79,23 +72,21 @@ rdoc_options: []
79
72
  require_paths:
80
73
  - lib
81
74
  required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
82
76
  requirements:
83
77
  - - ">="
84
78
  - !ruby/object:Gem::Version
85
- segments:
86
- - 0
87
79
  version: "0"
88
80
  required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
89
82
  requirements:
90
83
  - - ">="
91
84
  - !ruby/object:Gem::Version
92
- segments:
93
- - 0
94
85
  version: "0"
95
86
  requirements: []
96
87
 
97
88
  rubyforge_project:
98
- rubygems_version: 1.3.6
89
+ rubygems_version: 1.5.1
99
90
  signing_key:
100
91
  specification_version: 3
101
92
  summary: Chef capistrano recipes