supply_drop 0.11.1 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/supply_drop.rb CHANGED
@@ -1,198 +1,12 @@
1
1
  require 'supply_drop/rsync'
2
2
  require 'supply_drop/async_enumerable'
3
+ require 'supply_drop/plugin'
3
4
  require 'supply_drop/syntax_checker'
4
5
  require 'supply_drop/thread_pool'
5
6
  require 'supply_drop/util'
6
7
  require 'supply_drop/writer/batched'
7
8
  require 'supply_drop/writer/file'
8
9
  require 'supply_drop/writer/streaming'
10
+ require 'supply_drop/tasks'
9
11
 
10
- Capistrano::Configuration.instance.load do
11
- namespace :puppet do
12
- set :puppet_source, '.'
13
- set :puppet_destination, '/tmp/supply_drop'
14
- set :puppet_command, 'puppet apply'
15
- set :puppet_lib, lambda { "#{puppet_destination}/modules" }
16
- set :puppet_parameters, lambda { puppet_verbose ? '--debug --trace puppet.pp' : 'puppet.pp' }
17
- set :puppet_verbose, false
18
- set :puppet_excludes, %w(.git .svn)
19
- set :puppet_stream_output, false
20
- set :puppet_parallel_rsync, true
21
- set :puppet_parallel_rsync_pool_size, 10
22
- set :puppet_syntax_check, false
23
- set :puppet_write_to_file, nil
24
- set :puppet_runner, nil
25
- set :puppet_lock_file, '/tmp/puppet.lock'
26
-
27
- namespace :bootstrap do
28
- desc "installs puppet via rubygems on an osx host"
29
- task :osx do
30
- if fetch(:use_sudo, true)
31
- run "#{sudo} gem install puppet --no-ri --no-rdoc"
32
- else
33
- run "gem install puppet --no-ri --no-rdoc"
34
- end
35
- end
36
-
37
- desc "installs puppet via apt on an ubuntu host"
38
- task :ubuntu do
39
- run "mkdir -p #{puppet_destination}"
40
- run "#{sudo} apt-get update"
41
- run "#{sudo} apt-get install -y puppet rsync"
42
- end
43
-
44
- desc "installs puppet via yum on a centos/red hat host"
45
- task :redhat do
46
- run "mkdir -p #{puppet_destination}"
47
- run "#{sudo} yum -y install puppet rsync"
48
- end
49
- end
50
-
51
- desc "checks the syntax of all *.pp and *.erb files"
52
- task :syntax_check do
53
- checker = SupplyDrop::SyntaxChecker.new(puppet_source)
54
- logger.info "Sytax Checking..."
55
- errors = false
56
- checker.validate_puppet_files.each do |file, error|
57
- logger.important "Puppet error: #{file}"
58
- logger.important error
59
- errors = true
60
- end
61
- checker.validate_templates.each do |file, error|
62
- logger.important "Template error: #{file}"
63
- logger.important error
64
- errors = true
65
- end
66
- raise "syntax errors" if errors
67
- end
68
-
69
- desc "pushes the current puppet configuration to the server"
70
- task :update_code, :except => { :nopuppet => true } do
71
- servers = SupplyDrop::Util.optionally_async(find_servers_for_task(current_task), puppet_parallel_rsync)
72
- failed_servers = servers.map do |server|
73
- rsync_cmd = SupplyDrop::Rsync.command(
74
- puppet_source,
75
- SupplyDrop::Rsync.remote_address(server.user || fetch(:user, ENV['USER']), server.host, puppet_destination),
76
- :delete => true,
77
- :excludes => puppet_excludes,
78
- :ssh => { :keys => ssh_options[:keys], :config => ssh_options[:config], :port => fetch(:port, nil) }
79
- )
80
- logger.debug rsync_cmd
81
- server.host unless system rsync_cmd
82
- end.compact
83
-
84
- raise "rsync failed on #{failed_servers.join(',')}" if failed_servers.any?
85
- end
86
-
87
- before :'puppet:update_code' do
88
- _set_threadpool_size
89
- syntax_check if puppet_syntax_check
90
- end
91
-
92
- before 'puppet:noop' do
93
- _prep_destination
94
- _lock
95
- end
96
-
97
- before 'puppet:apply' do
98
- _prep_destination
99
- _lock
100
- end
101
-
102
- desc "runs puppet with --noop flag to show changes"
103
- task :noop, :except => { :nopuppet => true } do
104
- transaction do
105
- on_rollback { _unlock }
106
- update_code
107
- _puppet :noop
108
- end
109
- end
110
-
111
- desc "applies the current puppet config to the server"
112
- task :apply, :except => { :nopuppet => true } do
113
- transaction do
114
- on_rollback { _unlock }
115
- update_code
116
- _puppet :apply
117
- end
118
- end
119
-
120
- desc "clears the puppet lockfile on the server."
121
- task :remove_lock, :except => { :nopuppet => true} do
122
- _unlock
123
- end
124
-
125
- after 'puppet:noop' do
126
- _unlock
127
- end
128
-
129
- after 'puppet:apply' do
130
- _unlock
131
- end
132
- end
133
-
134
- def _set_threadpool_size
135
- SupplyDrop::Util.thread_pool_size = puppet_parallel_rsync_pool_size
136
- end
137
-
138
- def _red_text(text)
139
- "\033[0;31m#{text}\033[0m"
140
- end
141
-
142
- def _prep_destination
143
- run "mkdir -p #{puppet_destination}"
144
- run "#{sudo} chown -R $USER: #{puppet_destination}"
145
- end
146
-
147
- def _lock
148
- if _should_lock?
149
- run <<-GETLOCK
150
- if [ ! -f #{puppet_lock_file} ]; then
151
- touch #{puppet_lock_file};
152
- else
153
- stat -c "#{_red_text("Puppet in progress, #{puppet_lock_file} owned by %U since %x")}" #{puppet_lock_file} >&2;
154
- exit 1;
155
- fi
156
- GETLOCK
157
- end
158
- end
159
-
160
- def _unlock
161
- run "#{sudo} rm -f #{puppet_lock_file}; true" if _should_lock?
162
- end
163
-
164
- def _should_lock?
165
- puppet_lock_file && !ENV['NO_PUPPET_LOCK']
166
- end
167
-
168
- def _puppet(command = :noop)
169
- puppet_cmd = "cd #{puppet_destination} && #{_sudo_cmd} #{puppet_command} --modulepath=#{puppet_lib} #{puppet_parameters}"
170
- flag = command == :noop ? '--noop' : ''
171
-
172
- writer = if puppet_stream_output
173
- SupplyDrop::Writer::Streaming.new(logger)
174
- else
175
- SupplyDrop::Writer::Batched.new(logger)
176
- end
177
-
178
- writer = SupplyDrop::Writer::File.new(writer, puppet_write_to_file) unless puppet_write_to_file.nil?
179
-
180
- begin
181
- run "#{puppet_cmd} #{flag}" do |channel, stream, data|
182
- writer.collect_output(channel[:host], data)
183
- end
184
- logger.debug "Puppet #{command} complete."
185
- ensure
186
- writer.all_output_collected
187
- end
188
- end
189
-
190
- def _sudo_cmd
191
- if fetch(:use_sudo, true)
192
- sudo(:as => puppet_runner)
193
- else
194
- logger.info "NOTICE: puppet_runner configuration invalid when use_sudo is false, ignoring..." unless puppet_runner.nil?
195
- ''
196
- end
197
- end
198
- end
12
+ Capistrano.plugin :supply_drop, SupplyDrop::Plugin
@@ -0,0 +1,93 @@
1
+ module SupplyDrop
2
+ module Plugin
3
+
4
+ def rsync
5
+ SupplyDrop::Util.thread_pool_size = puppet_parallel_rsync_pool_size
6
+ servers = SupplyDrop::Util.optionally_async(find_servers_for_task(current_task), puppet_parallel_rsync)
7
+ failed_servers = servers.map do |server|
8
+ rsync_cmd = SupplyDrop::Rsync.command(
9
+ puppet_source,
10
+ SupplyDrop::Rsync.remote_address(server.user || fetch(:user, ENV['USER']), server.host, puppet_destination),
11
+ :delete => true,
12
+ :excludes => puppet_excludes,
13
+ :ssh => { :keys => ssh_options[:keys], :config => ssh_options[:config], :port => fetch(:port, nil) }
14
+ )
15
+ logger.debug rsync_cmd
16
+ server.host unless system rsync_cmd
17
+ end.compact
18
+
19
+ raise "rsync failed on #{failed_servers.join(',')}" if failed_servers.any?
20
+ end
21
+
22
+ def prepare
23
+ run "mkdir -p #{puppet_destination}"
24
+ run "#{sudo} chown -R $USER: #{puppet_destination}"
25
+ end
26
+
27
+ def noop
28
+ puppet(:noop)
29
+ end
30
+
31
+ def apply
32
+ puppet(:apply)
33
+ end
34
+
35
+ def lock
36
+ if should_lock?
37
+ run <<-GETLOCK
38
+ if [ ! -f #{puppet_lock_file} ]; then
39
+ touch #{puppet_lock_file};
40
+ else
41
+ stat -c "#{red_text("Puppet in progress, #{puppet_lock_file} owned by %U since %x")}" #{puppet_lock_file} >&2;
42
+ exit 1;
43
+ fi
44
+ GETLOCK
45
+ end
46
+ end
47
+
48
+ def unlock
49
+ run "#{sudo} rm -f #{puppet_lock_file}; true" if should_lock?
50
+ end
51
+
52
+ private
53
+
54
+ def should_lock?
55
+ puppet_lock_file && !ENV['NO_PUPPET_LOCK']
56
+ end
57
+
58
+ def puppet(command = :noop)
59
+ puppet_cmd = "cd #{puppet_destination} && #{sudo_cmd} #{puppet_command} --modulepath=#{puppet_lib} #{puppet_parameters}"
60
+ flag = command == :noop ? '--noop' : ''
61
+
62
+ writer = if puppet_stream_output
63
+ SupplyDrop::Writer::Streaming.new(logger)
64
+ else
65
+ SupplyDrop::Writer::Batched.new(logger)
66
+ end
67
+
68
+ writer = SupplyDrop::Writer::File.new(writer, puppet_write_to_file) unless puppet_write_to_file.nil?
69
+
70
+ begin
71
+ run "#{puppet_cmd} #{flag}" do |channel, stream, data|
72
+ writer.collect_output(channel[:host], data)
73
+ end
74
+ logger.debug "Puppet #{command} complete."
75
+ ensure
76
+ writer.all_output_collected
77
+ end
78
+ end
79
+
80
+ def red_text(text)
81
+ "\033[0;31m#{text}\033[0m"
82
+ end
83
+
84
+ def sudo_cmd
85
+ if fetch(:use_sudo, true)
86
+ sudo(:as => puppet_runner)
87
+ else
88
+ logger.info "NOTICE: puppet_runner configuration invalid when use_sudo is false, ignoring..." unless puppet_runner.nil?
89
+ ''
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,96 @@
1
+ Capistrano::Configuration.instance.load do
2
+ namespace :puppet do
3
+ set :puppet_source, '.'
4
+ set :puppet_destination, '/tmp/supply_drop'
5
+ set :puppet_command, 'puppet apply'
6
+ set :puppet_lib, lambda { "#{puppet_destination}/modules" }
7
+ set :puppet_parameters, lambda { puppet_verbose ? '--debug --trace puppet.pp' : 'puppet.pp' }
8
+ set :puppet_verbose, false
9
+ set :puppet_excludes, %w(.git .svn)
10
+ set :puppet_stream_output, false
11
+ set :puppet_parallel_rsync, true
12
+ set :puppet_parallel_rsync_pool_size, 10
13
+ set :puppet_syntax_check, false
14
+ set :puppet_write_to_file, nil
15
+ set :puppet_runner, nil
16
+ set :puppet_lock_file, '/tmp/puppet.lock'
17
+
18
+ namespace :bootstrap do
19
+ desc "installs puppet via rubygems on an osx host"
20
+ task :osx do
21
+ if fetch(:use_sudo, true)
22
+ run "#{sudo} gem install puppet --no-ri --no-rdoc"
23
+ else
24
+ run "gem install puppet --no-ri --no-rdoc"
25
+ end
26
+ end
27
+
28
+ desc "installs puppet via apt on an ubuntu host"
29
+ task :ubuntu do
30
+ run "mkdir -p #{puppet_destination}"
31
+ run "#{sudo} apt-get update"
32
+ run "#{sudo} apt-get install -y puppet rsync"
33
+ end
34
+
35
+ desc "installs puppet via yum on a centos/red hat host"
36
+ task :redhat do
37
+ run "mkdir -p #{puppet_destination}"
38
+ run "#{sudo} yum -y install puppet rsync"
39
+ end
40
+ end
41
+
42
+ desc "checks the syntax of all *.pp and *.erb files"
43
+ task :syntax_check do
44
+ checker = SupplyDrop::SyntaxChecker.new(puppet_source)
45
+ logger.info "Sytax Checking..."
46
+ errors = false
47
+ checker.validate_puppet_files.each do |file, error|
48
+ logger.important "Puppet error: #{file}"
49
+ logger.important error
50
+ errors = true
51
+ end
52
+ checker.validate_templates.each do |file, error|
53
+ logger.important "Template error: #{file}"
54
+ logger.important error
55
+ errors = true
56
+ end
57
+ raise "syntax errors" if errors
58
+ end
59
+
60
+ desc "pushes the current puppet configuration to the server"
61
+ task :update_code, :except => { :nopuppet => true } do
62
+ syntax_check if puppet_syntax_check
63
+ supply_drop.rsync
64
+ end
65
+
66
+ desc "runs puppet with --noop flag to show changes"
67
+ task :noop, :except => { :nopuppet => true } do
68
+ transaction do
69
+ on_rollback { supply_drop.unlock }
70
+ supply_drop.prepare
71
+ supply_drop.lock
72
+ update_code
73
+ supply_drop.noop
74
+ supply_drop.unlock
75
+ end
76
+ end
77
+
78
+ desc "applies the current puppet config to the server"
79
+ task :apply, :except => { :nopuppet => true } do
80
+ transaction do
81
+ on_rollback { supply_drop.unlock }
82
+ supply_drop.prepare
83
+ supply_drop.lock
84
+ update_code
85
+ supply_drop.apply
86
+ supply_drop.unlock
87
+ end
88
+ end
89
+
90
+ desc "clears the puppet lockfile on the server."
91
+ task :remove_lock, :except => { :nopuppet => true} do
92
+ supply_drop.lock
93
+ end
94
+ end
95
+ end
96
+
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: supply_drop
3
3
  version: !ruby/object:Gem::Version
4
- hash: 49
5
- prerelease:
4
+ hash: 47
5
+ prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 11
9
- - 1
10
- version: 0.11.1
8
+ - 12
9
+ - 0
10
+ version: 0.12.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tony Pitluga
@@ -16,7 +16,8 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2012-08-15 00:00:00 Z
19
+ date: 2012-10-16 00:00:00 -05:00
20
+ default_executable:
20
21
  dependencies:
21
22
  - !ruby/object:Gem::Dependency
22
23
  name: capistrano
@@ -48,14 +49,17 @@ files:
48
49
  - README.md
49
50
  - Rakefile
50
51
  - lib/supply_drop/async_enumerable.rb
52
+ - lib/supply_drop/plugin.rb
51
53
  - lib/supply_drop/rsync.rb
52
54
  - lib/supply_drop/syntax_checker.rb
55
+ - lib/supply_drop/tasks.rb
53
56
  - lib/supply_drop/thread_pool.rb
54
57
  - lib/supply_drop/util.rb
55
58
  - lib/supply_drop/writer/batched.rb
56
59
  - lib/supply_drop/writer/file.rb
57
60
  - lib/supply_drop/writer/streaming.rb
58
61
  - lib/supply_drop.rb
62
+ has_rdoc: true
59
63
  homepage: http://github.com/pitluga/supply_drop
60
64
  licenses: []
61
65
 
@@ -85,7 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
89
  requirements: []
86
90
 
87
91
  rubyforge_project:
88
- rubygems_version: 1.8.17
92
+ rubygems_version: 1.3.7
89
93
  signing_key:
90
94
  specification_version: 3
91
95
  summary: Masterless puppet with capistrano