puppet_transplant 0.0.1 → 0.0.2

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.
@@ -4,6 +4,8 @@ require 'rubygems/installer'
4
4
 
5
5
  module PuppetTransplant
6
6
  class Installer
7
+ class RelocationError < Exception; end
8
+
7
9
  ##
8
10
  # post_install takes a {Gem::Installer} instance expected to be passed from
9
11
  # a {Gem.post_install} callback and modifies an installed `puppet` gem to
@@ -13,9 +15,14 @@ module PuppetTransplant
13
15
  # @param [Gem::Installer] gem_installer The gem installer instance of the
14
16
  # gem just installed, passed from the Gem.post_install callback.
15
17
  def self.post_install(gem_installer)
18
+ # Do nothing unless we're dealing with Puppet.
16
19
  return unless gem_installer.spec.name == 'puppet'
20
+ # Perform the relocation
17
21
  installer = new(gem_installer)
18
22
  installer.relocate_puppet!
23
+ # Let the user know what happened
24
+ gem_installer.ui.debug "Transplanted confdir: #{installer.confdir}"
25
+ gem_installer.ui.debug "Transplanted vardir: #{installer.vardir}"
19
26
  end
20
27
 
21
28
  ##
@@ -100,40 +107,102 @@ module PuppetTransplant
100
107
  # installed then the run_mode.rb file will be modified directly after
101
108
  # creating a backup at run_mode.rb.orig
102
109
  #
103
- # @api private
110
+ # @return [Boolean] true if Puppet has been successfully relocated, false
111
+ # otherwise
112
+ #
113
+ # @api public
104
114
  def relocate_puppet!
105
115
  if puppet_override_api?
106
- write_override_files
116
+ rval = write_override_files
117
+ else
118
+ begin
119
+ rval = modify_run_mode_in_place
120
+ rescue RelocationError => detail
121
+ gem_installer.ui.alert_error "Modification of run_mode.rb failed: #{detail}"
122
+ return false
123
+ end
124
+ end
125
+ !!rval
126
+ end
127
+
128
+ ##
129
+ # modify_run_mode_in_place directly modifies Puppet's `run_mode.rb` file to
130
+ # change the default system confdir and vardir. This behavior is a
131
+ # fallback mechanism in the event Puppet does not support the so-called
132
+ # relocation API submitted at
133
+ # https://github.com/puppetlabs/puppet/pull/2236
134
+ #
135
+ # @api private
136
+ #
137
+ # @raise [RelocationError] if the method could not perform the relocation.
138
+ # The reason will be included in the exception message.
139
+ #
140
+ # @see #relocate_puppet!
141
+ # @see #puppet_override_api?
142
+ #
143
+ # @return [Boolean] true if `run_mode.rb` was successfully modified.
144
+ def modify_run_mode_in_place
145
+ # Locate the run_mode.rb file
146
+ dir = gem_installer.dir
147
+ spec = gem_installer.spec
148
+
149
+ if run_mode_file = spec.files.find() {|p| p.match(/\/run_mode.rb$/)}
150
+ run_mode_path = Pathname.new(File.join(dir, run_mode_file))
107
151
  else
108
- modify_run_mode_in_place
152
+ raise RelocationError, "Could not find run_mode.rb in the file list."
153
+ end
154
+
155
+ if not run_mode_path.readable?
156
+ raise RelocationError, "#{run_mode_path} is not readable."
157
+ end
158
+
159
+ if not run_mode_path.writable?
160
+ raise RelocationError, "#{run_mode_path} is not writable."
109
161
  end
162
+
163
+ # This file is small enough to fit into memory.
164
+ data = File.read(run_mode_path)
165
+
166
+ # Modify the file in place for confdir
167
+ if not data.gsub!(/\/etc\/puppet\b/, confdir)
168
+ raise RelocationError, "Found no occurrences of /etc/puppet to replace."
169
+ end
170
+ # Modify the file in place for vardir
171
+ if not data.gsub!(/\/var\/lib\/puppet\b/, vardir)
172
+ raise RelocationError, "Found no occurrences of /var/lib/puppet to replace."
173
+ end
174
+
175
+ # Write the file back out to the system.
176
+ File.open(run_mode_path, 'w') do |file|
177
+ file.write(data)
178
+ end
179
+ return true
110
180
  end
111
181
 
112
182
  ##
113
183
  # write_override_files writes files to the filesystem using the
114
184
  # {Puppet::Util::RunMode.override_path) public API method to determine
115
- # which files to write. These files are intended to override the default
185
+ # whch files to write. These files are intended to override the default
116
186
  # confdir and vardir for Puppet both as an application and as a library.
117
187
  # See {https://tickets.puppetlabs.com/browse/PUP-1406}
118
188
  #
119
189
  # @api private
120
190
  def write_override_files
121
191
  confdir_path = Puppet::Util::RunMode.override_path('confdir')
192
+ vardir_path = Puppet::Util::RunMode.override_path('vardir')
122
193
  msg = "# Automatically overriden by the puppet_transplant gem"
123
- File.open(confdir_path, "w+") do |f|
194
+
195
+ File.open(confdir_path, "w") do |f|
124
196
  f.puts(confdir)
125
197
  f.puts(msg)
126
198
  end
127
199
 
128
- vardir_path = Puppet::Util::RunMode.override_path('vardir')
129
- File.open(vardir_path, "w+") do |f|
200
+ File.open(vardir_path, "w") do |f|
130
201
  f.puts(vardir)
131
202
  f.puts(msg)
132
203
  end
133
-
134
- gem_installer.ui.alert "PuppetTransplant modified the system confdir and vardir defaults:"
135
- gem_installer.ui.alert "confdir='#{confdir}'"
136
- gem_installer.ui.alert "vardir='#{vardir}'"
204
+
205
+ return true
137
206
  end
138
207
 
139
208
  ##
@@ -229,15 +298,31 @@ TEXT
229
298
  end
230
299
  private :is_windows
231
300
 
301
+ ##
302
+ # confdir returns the path of the relocated default system confdir
303
+ #
304
+ # @api public
305
+ #
306
+ # @see #vardir
307
+ #
308
+ # @return [String] The fully qualified path of the relocated default system
309
+ # confdir.
232
310
  def confdir
233
311
  @confdir ||= "/etc/#{org}/puppet"
234
312
  end
235
- private :confdir
236
313
 
314
+ ##
315
+ # vardir returns the path of the relocated default system vardir
316
+ #
317
+ # @api public
318
+ #
319
+ # @see #confdir
320
+ #
321
+ # @return [String] The fully qualified path of the relocated default system
322
+ # vardir.
237
323
  def vardir
238
324
  @vardir ||= "/var/lib/#{org}/puppet"
239
325
  end
240
- private :vardir
241
326
 
242
327
  ##
243
328
  # bindir returns the bin directory where binstub scripts will be written.
@@ -1,3 +1,3 @@
1
1
  module PuppetTransplant
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppet_transplant
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-01-17 00:00:00.000000000 Z
12
+ date: 2014-01-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -111,7 +111,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
111
111
  version: '0'
112
112
  segments:
113
113
  - 0
114
- hash: -1993839482699150890
114
+ hash: -134858611691268826
115
115
  required_rubygems_version: !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
@@ -120,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
120
120
  version: '0'
121
121
  segments:
122
122
  - 0
123
- hash: -1993839482699150890
123
+ hash: -134858611691268826
124
124
  requirements: []
125
125
  rubyforge_project:
126
126
  rubygems_version: 1.8.23