Class: Etch::Client
- Inherits:
-
Object
- Object
- Etch::Client
- Defined in:
- lib/etch/client.rb
Constant Summary collapse
- VERSION =
'5.0.0'- CONFIRM_PROCEED =
1- CONFIRM_SKIP =
2- CONFIRM_QUIT =
3- PRIVATE_KEY_PATHS =
["/etc/ssh/ssh_host_rsa_key", "/etc/ssh_host_rsa_key"]
- DEFAULT_CONFIGDIR =
'/etc'- DEFAULT_VARDIR =
'/var/etch'- DEFAULT_DETAILED_RESULTS =
['SERVER']
- ORIG_STDOUT =
We need these in relation to the output capturing
STDOUT.dup
- ORIG_STDERR =
STDERR.dup
- OUTPUT_CAPTURE_TIMEOUT =
We limit capturing to 5 minutes. That should be plenty of time for etch to handle any given file, including running any setup/pre/post commands.
5 * 60
- OUTPUT_CAPTURE_INTERACTIVE_TIMEOUT =
In interactive mode bump the timeout up to something absurdly large
14 * 24 * 60 * 60
Instance Attribute Summary collapse
-
#exec_once_per_run ⇒ Object
readonly
Returns the value of attribute exec_once_per_run.
Instance Method Summary collapse
- #check_for_disable_etch_file ⇒ Object
-
#compare_file_contents(file, newcontents) ⇒ Object
Returns true if the new contents are the same as the current file, false if the contents differ or if the file does not currently exist.
-
#compare_link_destination(file, newdest) ⇒ Object
Returns true if the given file is a symlink which points to the given destination, false if the link destination is different or if the file is not a link or does not currently exist.
-
#compare_ownership(file, uid, gid) ⇒ Object
Returns true if the ownership of the given file match the given UID and GID, false otherwise.
-
#compare_permissions(file, perms) ⇒ Object
Returns true if the permissions of the given file match the given permissions, false otherwise.
- #get_blank_request ⇒ Object
- #get_local_requests(file) ⇒ Object
- #get_orig_contents(file) ⇒ Object
- #get_orig_sum(file) ⇒ Object
- #get_private_key_path ⇒ Object
- #get_user_confirmation ⇒ Object
-
#initialize(options) ⇒ Client
constructor
A new instance of Client.
- #lock_file(file) ⇒ Object
- #lookup_gid(group) ⇒ Object
- #lookup_uid(user) ⇒ Object
- #make_backup(file) ⇒ Object
- #nonrecursive_copy(sourcedir, sourcefile, destdir) ⇒ Object
- #nonrecursive_copy_and_rename(sourcedir, sourcefile, destname) ⇒ Object
- #process_command(command, commandname) ⇒ Object
-
#process_commands(commandname, response) ⇒ Object
Raises an exception if any fatal error is encountered Returns a boolean, true unless the user indicated in interactive mode that further processing should be halted.
- #process_exec(exectype, exec, file = '') ⇒ Object
-
#process_file(file, response) ⇒ Object
Raises an exception if any fatal error is encountered Returns a boolean, true unless the user indicated in interactive mode that further processing should be halted.
- #process_guard(guard, commandname) ⇒ Object
- #process_post(file, config) ⇒ Object
- #process_pre(file, config) ⇒ Object
- #process_setup(file, config) ⇒ Object
- #process_test(file, config) ⇒ Object
- #process_test_before_post(file, config) ⇒ Object
- #process_until_done(files, commands) ⇒ Object
- #recursive_copy(sourcedir, sourcefile, destdir) ⇒ Object
- #recursive_copy_and_rename(sourcedir, sourcefile, destname) ⇒ Object
- #remove_file(file) ⇒ Object
-
#remove_stale_lock_files ⇒ Object
Any etch lockfiles more than a couple hours old are most likely stale and can be removed.
- #reset_already_processed ⇒ Object
- #restore_backup(file, backup) ⇒ Object
-
#save_history(file) ⇒ Object
This subroutine maintains a revision history for the file in @historybase.
-
#save_orig(file, save_directory_contents = nil) ⇒ Object
Save an original copy of the file if that hasn’t been done already.
-
#set_history_permissions(file) ⇒ Object
Ensures that the history log file has appropriate permissions to avoid leaking information.
-
#sign_post!(post, key) ⇒ Object
This method takes in a Net::HTTP::Post and a path to a private key.
- #start_output_capture ⇒ Object
- #stop_output_capture ⇒ Object
-
#tempdir(file) ⇒ Object
Ruby 1.8.7 and later have Dir.mktmpdir, but we support ruby 1.8.5 for RHEL/CentOS 5.
- #unlock_all_files ⇒ Object
- #unlock_file(file) ⇒ Object
Constructor Details
#initialize(options) ⇒ Client
Returns a new instance of Client.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/etch/client.rb', line 60 def initialize() @server = [:server] ? [:server] : 'https://etch' @configdir = [:configdir] ? [:configdir] : DEFAULT_CONFIGDIR @vardir = [:vardir] ? [:vardir] : DEFAULT_VARDIR @tag = [:tag] @local = [:local] ? File.([:local]) : nil @debug = [:debug] @dryrun = [:dryrun] @listfiles = [:listfiles] @interactive = [:interactive] @filenameonly = [:filenameonly] @fullfile = [:fullfile] @key = [:key] ? [:key] : get_private_key_path @disableforce = [:disableforce] @lockforce = [:lockforce] @last_response = "" @file_system_root = '/' # Not sure if this needs to be more portable # This option is only intended for use by the test suite if [:file_system_root] @file_system_root = [:file_system_root] @vardir = File.join(@file_system_root, @vardir) @configdir = File.join(@file_system_root, @configdir) end @configfile = File.join(@configdir, 'etch.conf') @detailed_results = [] if File.exist?(@configfile) IO.foreach(@configfile) do |line| line.chomp! next if (line =~ /^\s*$/); # Skip blank lines next if (line =~ /^\s*#/); # Skip comments line.strip! # Remove leading/trailing whitespace key, value = line.split(/\s*=\s*/, 2) if key == 'server' # A setting for the server to use which comes from upstream # (generally from a command line option) takes precedence # over the config file if ![:server] @server = value # Warn the user, as this could potentially be confusing # if they don't realize there's a config file lying # around warn "Using server #{@server} from #{@configfile}" if @debug else # "command line override" isn't necessarily accurate, we don't # know why the caller passed us an option to override the config # file, but most of the time it will be due to a command line # option and I want the message to be easily understood by users. # If someone can come up with some better wording without turning # the message into something as long as this comment that would be # welcome. warn "Ignoring 'server' option in #{@configfile} due to command line override" if @debug end elsif key == 'local' if ![:local] && ![:server] @local = value warn "Using local directory #{@local} from #{@configfile}" if @debug else warn "Ignoring 'local' option in #{@configfile} due to command line override" if @debug end elsif key == 'key' if ![:key] @key = value warn "Using key #{@key} from #{@configfile}" if @debug else warn "Ignoring 'key' option in #{@configfile} due to command line override" if @debug end elsif key == 'path' ENV['PATH'] = value elsif key == 'detailed_results' warn "Adding detailed results destination '#{value}'" if @debug @detailed_results << value end end end if @key && !File.readable?(@key) @key = nil end if !@key warn "No readable private key found, messages to server will not be signed and may be rejected depending on server configuration" end if @detailed_results.empty? @detailed_results = DEFAULT_DETAILED_RESULTS end @origbase = File.join(@vardir, 'orig') @historybase = File.join(@vardir, 'history') @lockbase = File.join(@vardir, 'locks') @requestbase = File.join(@vardir, 'requests') @facts = Facter.to_hash if @facts['operatingsystemrelease'] # Some versions of Facter have a bug that leaves extraneous # whitespace on this fact. Work around that with strip. I.e. on # CentOS you'll get '5 ' or '5.2 '. @facts['operatingsystemrelease'].strip! end if @local logger = Logger.new(STDOUT) dlogger = Logger.new(STDOUT) if @debug dlogger.level = Logger::DEBUG else dlogger.level = Logger::INFO end blankrequest = {} @facts.each_pair { |key, value| blankrequest[key] = value.to_s } blankrequest['fqdn'] = @facts['fqdn'] @facts = blankrequest @etch = Etch.new(logger, dlogger) else # Make sure the server URL ends in a / so that we can append paths # to it using URI.join if @server !~ %r{/$} @server << '/' end @filesuri = URI.join(@server, 'files') @resultsuri = URI.join(@server, 'results') @blankrequest = {} # If the user specified a non-standard key then override the # sshrsakey fact so that authentication works if @key @facts['sshrsakey'] = IO.read(@key+'.pub').chomp.split[1] end @facts.each_pair { |key, value| @blankrequest["facts[#{key}]"] = value.to_s } @blankrequest['fqdn'] = @facts['fqdn'] if @debug @blankrequest['debug'] = '1' end if @tag @blankrequest['tag'] = @tag end end @locked_files = {} @first_update = {} @already_processed = {} @exec_already_processed = {} @exec_once_per_run = {} @results = {} # See start/stop_output_capture for these @output_pipes = [] @lchown_supported = nil @lchmod_supported = nil end |
Instance Attribute Details
#exec_once_per_run ⇒ Object (readonly)
Returns the value of attribute exec_once_per_run.
58 59 60 |
# File 'lib/etch/client.rb', line 58 def exec_once_per_run @exec_once_per_run end |
Instance Method Details
#check_for_disable_etch_file ⇒ Object
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 |
# File 'lib/etch/client.rb', line 530 def check_for_disable_etch_file disable_etch = File.join(@vardir, 'disable_etch') = '' if File.exist?(disable_etch) if !@disableforce = "Etch disabled:\n" << IO.read(disable_etch) puts return false, else puts "Ignoring disable_etch file" end end return true, end |
#compare_file_contents(file, newcontents) ⇒ Object
Returns true if the new contents are the same as the current file, false if the contents differ or if the file does not currently exist.
1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 |
# File 'lib/etch/client.rb', line 1591 def compare_file_contents(file, newcontents) # If the file currently exists and is a regular file then check to see # if the new contents are the same. if File.file?(file) && !File.symlink?(file) # As elsewhere we have no idea how arbitrary files that the user is # managing are encoded, so tell Ruby to read the file in binary mode. contents = nil if RUBY_VERSION.split('.')[0..1].join('.').to_f >= 1.9 contents = IO.read(file, :encoding => 'ASCII-8BIT') else contents = IO.read(file) end if newcontents == contents return true end end false end |
#compare_link_destination(file, newdest) ⇒ Object
Returns true if the given file is a symlink which points to the given destination, false if the link destination is different or if the file is not a link or does not currently exist.
1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 |
# File 'lib/etch/client.rb', line 1613 def compare_link_destination(file, newdest) # If the file currently exists and is a link, check to see if the # new destination is different. if File.symlink?(file) currentdest = File.readlink(file) if currentdest == newdest return true end end false end |
#compare_ownership(file, uid, gid) ⇒ Object
Returns true if the ownership of the given file match the given UID and GID, false otherwise.
2235 2236 2237 2238 2239 2240 2241 2242 2243 |
# File 'lib/etch/client.rb', line 2235 def compare_ownership(file, uid, gid) if File.exist?(file) st = File.lstat(file) if st.uid == uid && st.gid == gid return true end end false end |
#compare_permissions(file, perms) ⇒ Object
Returns true if the permissions of the given file match the given permissions, false otherwise.
2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 |
# File 'lib/etch/client.rb', line 2221 def (file, perms) if File.exist?(file) st = File.lstat(file) # Mask off the file type fileperms = st.mode & 07777 if perms == fileperms return true end end false end |
#get_blank_request ⇒ Object
546 547 548 |
# File 'lib/etch/client.rb', line 546 def get_blank_request @blankrequest.dup end |
#get_local_requests(file) ⇒ Object
1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 |
# File 'lib/etch/client.rb', line 1914 def get_local_requests(file) requestdir = File.join(@requestbase, file) requests = [] if File.directory?(requestdir) Dir.foreach(requestdir) do |entry| next if entry == '.' next if entry == '..' requestfile = File.join(requestdir, entry) requests << IO.read(requestfile) end end requests end |
#get_orig_contents(file) ⇒ Object
1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 |
# File 'lib/etch/client.rb', line 1628 def get_orig_contents(file) origpath = save_orig(file) orig_contents = nil # We only send back the actual original file contents if the original is # a regular file, otherwise we send back an empty string. if (origpath =~ /\.ORIG$/ || origpath =~ /\.TMP$/) && File.file?(origpath) && !File.symlink?(origpath) # As elsewhere we have no idea how arbitrary files that the user is # managing are encoded, so tell Ruby to read the file in binary mode. orig_contents = nil if RUBY_VERSION.split('.')[0..1].join('.').to_f >= 1.9 orig_contents = IO.read(origpath, :encoding => 'ASCII-8BIT') else orig_contents = IO.read(origpath) end else orig_contents = '' end orig_contents end |
#get_orig_sum(file) ⇒ Object
1625 1626 1627 |
# File 'lib/etch/client.rb', line 1625 def get_orig_sum(file) Digest::SHA1.hexdigest(get_orig_contents(file)) end |
#get_private_key_path ⇒ Object
2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 |
# File 'lib/etch/client.rb', line 2498 def get_private_key_path key = nil PRIVATE_KEY_PATHS.each do |path| if File.readable?(path) key = path break end end key end |
#get_user_confirmation ⇒ Object
2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 |
# File 'lib/etch/client.rb', line 2245 def get_user_confirmation while true print "Proceed/Skip/Quit? " case @last_response when /p|P/ then print "[P|s|q] " when /s|S/ then print "[p|S|q] " when /q|Q/ then print "[p|s|Q] " else print "[p|s|q] " end response = $stdin.gets.chomp if response.empty? response = @last_response end if response =~ /p/i @last_response = response return CONFIRM_PROCEED elsif response =~ /s/i @last_response = response return CONFIRM_SKIP elsif response =~ /q/i @last_response = response return CONFIRM_QUIT end end end |
#lock_file(file) ⇒ Object
2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 |
# File 'lib/etch/client.rb', line 2312 def lock_file(file) lockpath = File.join(@lockbase, "#{file}.LOCK") # Make sure the directory tree for this file exists in the # lock directory lockdir = File.dirname(lockpath) if ! File.directory?(lockdir) puts "Making directory tree #{lockdir}" if (@debug) FileUtils.mkpath(lockdir) if (!@dryrun) end return if (@dryrun) # Make 30 attempts (1s sleep after each attempt) 30.times do |i| begin fd = File.sysopen(lockpath, Fcntl::O_WRONLY|Fcntl::O_CREAT|Fcntl::O_EXCL) puts "Lock acquired for #{file}" if (@debug) File.open(fd) { |lockfile| lockfile.puts $$ } @locked_files[file] = true return rescue Errno::EEXIST puts "Attempt to acquire lock for #{file} failed, sleeping 1s" sleep 1 end end raise "Unable to acquire lock for #{file} after repeated attempts" end |
#lookup_gid(group) ⇒ Object
2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 |
# File 'lib/etch/client.rb', line 2201 def lookup_gid(group) if group.to_i.to_s == group.to_s # If the group was specified as a numeric GID, use it directly. gid = group.to_i else # Otherwise attempt to look up the group to get a GID. Default # to GID 0 if the group can't be found. begin gr = Etc.getgrnam(group) gid = gr.gid rescue ArgumentError puts "config requests group #{group}, but that group can't be found. Using GID 0." if @debug gid = 0 end end gid end |
#lookup_uid(user) ⇒ Object
2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 |
# File 'lib/etch/client.rb', line 2183 def lookup_uid(user) if user.to_i.to_s == user.to_s # If the user was specified as a numeric UID, use it directly. uid = user.to_i else # Otherwise attempt to look up the username to get a UID. # Default to UID 0 if the username can't be found. begin pw = Etc.getpwnam(user) uid = pw.uid rescue ArgumentError puts "config requests user #{user}, but that user can't be found. Using UID 0." if @debug uid = 0 end end uid end |
#make_backup(file) ⇒ Object
1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 |
# File 'lib/etch/client.rb', line 1941 def make_backup(file) backup = nil filebase = File.basename(file) filedir = File.dirname(file) if !@dryrun backup = tempdir(file) else # Use a fake placeholder name for use in dry run/debug messages backup = "#{file}.XXXX" end backuppath = File.join(backup, filebase) puts "Making backup: #{file} -> #{backuppath}" if !@dryrun if File.exist?(file) || File.symlink?(file) recursive_copy(filedir, filebase, backup) else # If there's no file to back up then leave a marker file so # that restore_backup does the right thing File.open("#{backuppath}.NOORIG", "w") { |markerfile| } end end backup end |
#nonrecursive_copy(sourcedir, sourcefile, destdir) ⇒ Object
2301 2302 2303 2304 |
# File 'lib/etch/client.rb', line 2301 def nonrecursive_copy(sourcedir, sourcefile, destdir) system("cd #{sourcedir} && echo #{sourcefile} | cpio -pdum #{destdir}") or raise "Non-recursive copy #{sourcedir}/#{sourcefile} to #{destdir} failed" end |
#nonrecursive_copy_and_rename(sourcedir, sourcefile, destname) ⇒ Object
2305 2306 2307 2308 2309 2310 |
# File 'lib/etch/client.rb', line 2305 def nonrecursive_copy_and_rename(sourcedir, sourcefile, destname) tmpdir = tempdir(destname) nonrecursive_copy(sourcedir, sourcefile, tmpdir) File.rename(File.join(tmpdir, sourcefile), destname) Dir.delete(tmpdir) end |
#process_command(command, commandname) ⇒ Object
2088 2089 2090 2091 |
# File 'lib/etch/client.rb', line 2088 def process_command(command, commandname) puts "Processing command" process_exec(:command, command, commandname) end |
#process_commands(commandname, response) ⇒ Object
Raises an exception if any fatal error is encountered Returns a boolean, true unless the user indicated in interactive mode that further processing should be halted
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 |
# File 'lib/etch/client.rb', line 1445 def process_commands(commandname, response) continue_processing = true save_results = true exception = nil # We may not have configuration for this file, if it does not apply # to this host. The server takes care of detecting any errors that # might involve, so here we can just silently return. command = response[:commands][commandname] if !command puts "No configuration for command #{commandname}, skipping" if (@debug) return continue_processing end # Skip commands we've already processed in response to <depend> # statements. if @already_processed.has_key?(commandname) puts "Skipping already processed command #{commandname}" if (@debug) return continue_processing end # Prep the results capturing for this command result = {} result['success'] = true result['message'] = '' # catch/throw for expected/non-error events that end processing # begin/raise for error events that end processing # Within this block you should throw :process_done if you've reached # a natural stopping point and nothing further needs to be done. You # should raise an exception if you encounter an error condition. # Do not 'return' or 'abort'. catch :process_done do begin start_output_capture puts "Processing command #{commandname}" if (@debug) # The %locked_files hash provides a convenient way to # detect circular dependancies. It doesn't give us an ordered # list of dependencies, which might be handy to help the user # debug the problem, but I don't think it's worth maintaining a # seperate array just for that purpose. if @locked_files.has_key?(commandname) raise "Circular command dependancy detected. " + "Dependancy list (unsorted) contains:\n " + @locked_files.keys.join(', ') end # This needs to be after the circular dependency check lock_file(commandname) # Process any other commands that this command depends on command[:depend] && command[:depend].each do |depend| puts "Processing command dependency #{depend}" if (@debug) continue_processing = process_commands(depend, response) if !continue_processing throw :process_done end end # Process any files that this command depends on command[:dependfile] && command[:dependfile].each do |dependfile| puts "Processing file dependency #{dependfile}" if (@debug) continue_processing = process_file(dependfile, response) if !continue_processing throw :process_done end end # Perform each step command[:steps] && command[:steps].each do |outerstep| if step = outerstep[:step] # Run guards, display only in debug (a la setup) guard_result = true step[:guard] && step[:guard].each do |guard| guard_result &= process_guard(guard, commandname) end if !guard_result # Tell the user what we're going to do puts "Will run command '#{step[:command].join('; ')}'" # If the user requested interactive mode ask them for # confirmation to proceed. if @interactive case get_user_confirmation() when CONFIRM_PROCEED # No need to do anything when CONFIRM_SKIP save_results = false throw :process_done when CONFIRM_QUIT continue_processing = false save_results = false throw :process_done else raise "Unexpected result from get_user_confirmation()" end end # Run command, always display (a la pre/post) step[:command] && step[:command].each do |cmd| process_command(cmd, commandname) end # Re-run guard, always display, abort if fails guard_recheck_result = true step[:guard] && step[:guard].each do |guard| guard_recheck_result &= process_guard(guard, commandname) end if !guard_recheck_result raise "Guard #{step[:guard].join('; ')} still fails for #{commandname} after running command #{step[:command].join('; ')}" end end end end rescue Exception result['success'] = false exception = $! end # End begin block end # End :process_done catch block unlock_file(commandname) output = stop_output_capture if exception output << exception. output << exception.backtrace.join("\n") if @debug end result['message'] << output if save_results @results[commandname] = result end if exception raise exception end @already_processed[commandname] = true continue_processing end |
#process_exec(exectype, exec, file = '') ⇒ Object
2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 |
# File 'lib/etch/client.rb', line 2093 def process_exec(exectype, exec, file='') r = true # Because the setup and guard commands are processed every time (rather # than just when the file has changed as with pre/post) we don't want to # print a message for them. puts " Executing '#{exec}'" if (![:setup, :guard].include?(exectype) || @debug) # Actually run the command unless we're in a dry run, or if we're in # a damp run and the command is a setup command. if ! @dryrun || exectype == :guard || (@dryrun == 'damp' && exectype == :setup) etch_priority = nil if [:post, :command].include?(exectype) # Etch is likely running at a lower priority than normal. # However, we don't want to run post commands at that # priority. If they restart processes (for example, # restarting sshd) the restarted process will be left # running at that same lower priority. sshd is particularly # nefarious, because further commands started by users via # that low priority sshd will also run at low priority. etch_priority = Process.getpriority(Process::PRIO_PROCESS, 0) if etch_priority != 0 puts " Etch is running at priority #{etch_priority}, " + "temporarily adjusting priority to 0 to run #{exectype} command" if (@debug) Process.setpriority(Process::PRIO_PROCESS, 0, 0) end end # Explicitly invoke using /bin/sh so that syntax like # "FOO=bar myprogram" works. r = system('/bin/sh', '-c', exec) if [:post, :command].include?(exectype) if etch_priority != 0 puts " Returning priority to #{etch_priority}" if (@debug) Process.setpriority(Process::PRIO_USER, 0, etch_priority) end end end # If the command exited with error if !r # We don't normally print the command we're executing for setup and # guard commands (see above). But that makes it hard to figure out # what's going on if it fails. So include the command in the message if # there was a failure. execmsg = '' execmsg = "'#{exec}' " if ([:setup, :guard].include?(exectype)) # Normally we include the filename of the file that this command # is associated with in the messages we print. But for "exec once # per run" commands that doesn't apply. Assemble a variable # that has the filename if we have it, to be included in the # error message we're going to print. filemsg = '' filemsg = "for #{file} " if (!file.empty?) # Setup and pre commands are almost always used to install # software prerequisites, and bad things generally happen if # those software installs fail. So consider it a fatal error if # that occurs. if [:setup, :pre].include?(exectype) raise " Setup/Pre command " + execmsg + filemsg + "exited with non-zero value" # Post commands are generally used to restart services. While # it is unfortunate if they fail, there is little to be gained # by having etch exit if they do so. So simply warn if a post # command fails. elsif exectype == :post puts " Post command " + execmsg + filemsg + "exited with non-zero value" # process_commands takes the appropriate action when guards and commands # fail, so we just warn of any failures here. elsif exectype == :guard puts " Guard " + execmsg + filemsg + "exited with non-zero value" elsif exectype == :command puts " Command " + execmsg + filemsg + "exited with non-zero value" # For test commands we need to warn the user and then return a # value indicating the failure so that a rollback can be # performed. elsif exectype =~ /^test/ puts " Test command " + execmsg + filemsg + "exited with non-zero value" end end r end |
#process_file(file, response) ⇒ Object
Raises an exception if any fatal error is encountered Returns a boolean, true unless the user indicated in interactive mode that further processing should be halted
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 |
# File 'lib/etch/client.rb', line 553 def process_file(file, response) continue_processing = true save_results = true exception = nil # We may not have configuration for this file, if it does not apply # to this host. The server takes care of detecting any errors that # might involve, so here we can just silently return. config = response[:configs][file] if !config puts "No configuration for #{file}, skipping" if (@debug) return continue_processing end # Skip files we've already processed in response to <depend> # statements. if @already_processed.has_key?(file) puts "Skipping already processed #{file}" if (@debug) return continue_processing end # Prep the results capturing for this file result = {} result['success'] = true result['message'] = '' # catch/throw for expected/non-error events that end processing # begin/raise for error events that end processing # Within this block you should throw :process_done if you've reached # a natural stopping point and nothing further needs to be done. You # should raise an exception if you encounter an error condition. # Do not 'return' or 'abort'. catch :process_done do begin start_output_capture puts "Processing #{file}" if (@debug) # The %locked_files hash provides a convenient way to # detect circular dependancies. It doesn't give us an ordered # list of dependencies, which might be handy to help the user # debug the problem, but I don't think it's worth maintaining a # seperate array just for that purpose. if @locked_files.has_key?(file) raise "Circular dependancy detected. " + "Dependancy list (unsorted) contains:\n " + @locked_files.keys.join(', ') end # This needs to be after the circular dependency check lock_file(file) # Process any other files that this file depends on config[:depend] && config[:depend].each do |depend| puts "Processing dependency #{depend}" if (@debug) continue_processing = process_file(depend, response) if !continue_processing throw :process_done end end # Process any commands that this file depends on config[:dependcommand] && config[:dependcommand].each do |dependcommand| puts "Processing command dependency #{dependcommand}" if (@debug) continue_processing = process_commands(dependcommand, response) if !continue_processing throw :process_done end end # See what type of action the user has requested # Check to see if the user has requested that we revert back to the # original file. if config[:revert] origpathbase = File.join(@origbase, file) origpath = nil # Restore the original file if it is around revert_occurred = false if File.exist?("#{origpathbase}.ORIG") origpath = "#{origpathbase}.ORIG" origdir = File.dirname(origpath) origbase = File.basename(origpath) filedir = File.dirname(file) # Remove anything we might have written out for this file remove_file(file) if (!@dryrun) puts "Restoring #{origpath} to #{file}" recursive_copy_and_rename(origdir, origbase, file) if (!@dryrun) revert_occurred = true elsif File.exist?("#{origpathbase}.TAR") origpath = "#{origpathbase}.TAR" filedir = File.dirname(file) # Remove anything we might have written out for this file remove_file(file) if (!@dryrun) puts "Restoring #{file} from #{origpath}" system("cd #{filedir} && tar xf #{origpath}") if (!@dryrun) revert_occurred = true elsif File.exist?("#{origpathbase}.NOORIG") origpath = "#{origpathbase}.NOORIG" puts "Original #{file} didn't exist, restoring that state" # Remove anything we might have written out for this file remove_file(file) if (!@dryrun) revert_occurred = true end # Update the history log if revert_occurred save_history(file) end # Now remove the backed-up original so that future runs # don't do anything if origpath remove_file(origpath) if (!@dryrun) end throw :process_done end # Perform any setup commands that the user has requested. # These are occasionally needed to install software that is # required to generate the file (think m4 for sendmail.cf) or to # install a package containing a sample config file which we # then edit with a script, and thus doing the install in <pre> # is too late. process_setup(file, config) if config[:file] # Regular file newcontents = nil if config[:file][:contents] newcontents = Base64.decode64(config[:file][:contents]) end permstring = config[:file][:perms].to_s perms = permstring.oct owner = config[:file][:owner] group = config[:file][:group] uid = lookup_uid(owner) gid = lookup_gid(group) set_file_contents = false if newcontents set_file_contents = !compare_file_contents(file, newcontents) end = nil set_ownership = nil # If the file is currently something other than a plain file then # always set the flags to set the permissions and ownership. # Checking the permissions/ownership of whatever is there currently # is useless. if set_file_contents && (!File.file?(file) || File.symlink?(file)) = true set_ownership = true else = !(file, perms) set_ownership = !compare_ownership(file, uid, gid) end # Proceed if: # - The new contents are different from the current file # - The permissions or ownership requested don't match the # current permissions or ownership if !set_file_contents && ! && !set_ownership puts "No change to #{file} necessary" if (@debug) throw :process_done else # Tell the user what we're going to do if set_file_contents # If the new contents are different from the current file # show that to the user in the format they've requested. # If the requested permissions are not world-readable then # use the filenameonly format so that we don't disclose # non-public data, unless we're in interactive mode if @filenameonly || (permstring.to_i(8) & 0004 == 0 && !@interactive) puts "Will write out new #{file}" elsif @fullfile # Grab the first 8k of the contents first8k = newcontents.slice(0, 8192) # Then check it for null characters. If it has any it's # likely a binary file. hasnulls = true if (first8k =~ /\0/) if !hasnulls puts "Generated contents for #{file}:" puts "=============================================" puts newcontents puts "=============================================" else puts "Will write out new #{file}, but " + "generated contents are not plain text so " + "they will not be displayed" end else # Default is to show a diff of the current file and the # newly generated file. puts "Will make the following changes to #{file}, diff -c:" tempfile = nil if RUBY_VERSION.split('.')[0..1].join('.').to_f >= 1.9 tempfile = Tempfile.new(File.basename(file), :encoding => 'ASCII-8BIT') else tempfile = Tempfile.new(File.basename(file)) end tempfile.write(newcontents) tempfile.close puts "=============================================" if File.file?(file) && !File.symlink?(file) system("diff -c #{file} #{tempfile.path}") else # Either the file doesn't currently exist, # or is something other than a normal file # that we'll be replacing with a file. In # either case diffing against /dev/null will # produce the most logical output. system("diff -c /dev/null #{tempfile.path}") end puts "=============================================" tempfile.delete end end if puts "Will set permissions on #{file} to #{permstring}" end if set_ownership puts "Will set ownership of #{file} to #{uid}:#{gid}" end # If the user requested interactive mode ask them for # confirmation to proceed. if @interactive case get_user_confirmation() when CONFIRM_PROCEED # No need to do anything when CONFIRM_SKIP save_results = false throw :process_done when CONFIRM_QUIT continue_processing = false save_results = false throw :process_done else raise "Unexpected result from get_user_confirmation()" end end # Perform any pre-action commands that the user has requested process_pre(file, config) # If the original "file" is a directory and the user hasn't # specifically told us we can overwrite it then raise an exception. # # The test is here, rather than a bit earlier where you might # expect it, because the pre section may be used to address # originals which are directories. So we don't check until # after any pre commands are run. if File.directory?(file) && !File.symlink?(file) && !config[:file][:overwrite_directory] raise "Can't proceed, original of #{file} is a directory,\n" + " consider the overwrite_directory flag if appropriate." end # Give save_orig a definitive answer on whether or not to save the # contents of an original directory. origpath = save_orig(file, true) # Update the history log save_history(file) # Make sure the directory tree for this file exists filedir = File.dirname(file) if !File.directory?(filedir) puts "Making directory tree #{filedir}" FileUtils.mkpath(filedir) if (!@dryrun) end # Make a backup in case we need to roll back. We have no use # for a backup if there are no test commands defined (since we # only use the backup to roll back if the test fails), so don't # bother to create a backup unless there is a test command defined. backup = nil if config[:test_before_post] || config[:test] backup = make_backup(file) puts "Created backup #{backup}" end # If the new contents are different from the current file, # replace the file. if set_file_contents if !@dryrun # Write out the new contents into a temporary file filebase = File.basename(file) filedir = File.dirname(file) newfile = nil if RUBY_VERSION.split('.')[0..1].join('.').to_f >= 1.9 newfile = Tempfile.new(filebase, filedir, :encoding => 'ASCII-8BIT') else newfile = Tempfile.new(filebase, filedir) end # Set the proper permissions on the file before putting # data into it. newfile.chmod(perms) begin newfile.chown(uid, gid) rescue Errno::EPERM raise if Process.euid == 0 end puts "Writing new contents of #{file} to #{newfile.path}" if (@debug) newfile.write(newcontents) newfile.close # If the current file is not a plain file, remove it. # Plain files are left alone so that the replacement is # atomic. if File.symlink?(file) || (File.exist?(file) && ! File.file?(file)) puts "Current #{file} is not a plain file, removing it" if (@debug) remove_file(file) end # Move the new file into place File.rename(newfile.path, file) # Check the permissions and ownership now to ensure they # end up set properly = !(file, perms) set_ownership = !compare_ownership(file, uid, gid) end end # Ensure the permissions are set properly if File.chmod(perms, file) if (!@dryrun) end # Ensure the ownership is set properly if set_ownership begin File.chown(uid, gid, file) if (!@dryrun) rescue Errno::EPERM raise if Process.euid == 0 end end # Perform any test_before_post commands that the user has requested if !process_test_before_post(file, config) restore_backup(file, backup) raise "test_before_post failed" end # Perform any post-action commands that the user has requested process_post(file, config) # Perform any test commands that the user has requested if config[:test] if !process_test(file, config) restore_backup(file, backup) # Re-run any post commands process_post(file, config) end end # Clean up the backup, we don't need it anymore if config[:test_before_post] || config[:test] puts "Removing backup #{backup}" remove_file(backup) if (!@dryrun) end # Update the history log again save_history(file) throw :process_done end end if config[:link] # Symbolic link dest = config[:link][:dest] set_link_destination = !compare_link_destination(file, dest) absdest = File.(dest, File.dirname(file)) permstring = config[:link][:perms].to_s perms = permstring.oct owner = config[:link][:owner] group = config[:link][:group] uid = lookup_uid(owner) gid = lookup_gid(group) # lchown and lchmod are not supported on many platforms. The server # always includes ownership and permissions settings with any link # (pulling them from defaults if the user didn't specify them in # the config file.) As such link management would always fail # on systems which don't support lchown/lchmod, which seems like bad # behavior. So instead we check to see if they are implemented, and # if not just ignore ownership/permissions settings. I suppose the # ideal would be for the server to tell the client whether the # ownership/permissions were specifically requested (in the config) # rather than just defaults, and then for the client to always try to # manage ownership/permissions if the settings are not defaults (and # fail in the event that they aren't implemented.) if @lchown_supported.nil? lchowntestlink = Tempfile.new('etchlchowntest').path lchowntestfile = Tempfile.new('etchlchowntest').path File.delete(lchowntestlink) File.symlink(lchowntestfile, lchowntestlink) begin File.lchown(0, 0, lchowntestfile) @lchown_supported = true rescue NotImplementedError @lchown_supported = false rescue Errno::EPERM raise if Process.euid == 0 end File.delete(lchowntestlink) end if @lchmod_supported.nil? lchmodtestlink = Tempfile.new('etchlchmodtest').path lchmodtestfile = Tempfile.new('etchlchmodtest').path File.delete(lchmodtestlink) File.symlink(lchmodtestfile, lchmodtestlink) begin File.lchmod(0644, lchmodtestfile) @lchmod_supported = true rescue NotImplementedError @lchmod_supported = false end File.delete(lchmodtestlink) end = false if @lchmod_supported # If the file is currently something other than a link then # always set the flags to set the permissions and ownership. # Checking the permissions/ownership of whatever is there currently # is useless. if set_link_destination && !File.symlink?(file) = true else = !(file, perms) end end set_ownership = false if @lchown_supported if set_link_destination && !File.symlink?(file) set_ownership = true else set_ownership = !compare_ownership(file, uid, gid) end end # Proceed if: # - The new link destination differs from the current one # - The permissions or ownership requested don't match the # current permissions or ownership if !set_link_destination && ! && !set_ownership puts "No change to #{file} necessary" if (@debug) throw :process_done # Check that the link destination exists, and refuse to create # the link unless it does exist or the user told us to go ahead # anyway. # # Note that the destination may be a relative path, and the # target directory may not exist yet, so we have to convert the # destination to an absolute path and test that for existence. # expand_path should handle paths that are already absolute # properly. elsif ! File.exist?(absdest) && ! File.symlink?(absdest) && ! config[:link][:allow_nonexistent_dest] puts "Destination #{dest} for link #{file} does not exist," + " consider the allow_nonexistent_dest flag if appropriate." throw :process_done else # Tell the user what we're going to do if set_link_destination puts "Linking #{file} -> #{dest}" end if puts "Will set permissions on #{file} to #{permstring}" end if set_ownership puts "Will set ownership of #{file} to #{uid}:#{gid}" end # If the user requested interactive mode ask them for # confirmation to proceed. if @interactive case get_user_confirmation() when CONFIRM_PROCEED # No need to do anything when CONFIRM_SKIP save_results = false throw :process_done when CONFIRM_QUIT continue_processing = false save_results = false throw :process_done else raise "Unexpected result from get_user_confirmation()" end end # Perform any pre-action commands that the user has requested process_pre(file, config) # If the original "file" is a directory and the user hasn't # specifically told us we can overwrite it then raise an exception. # # The test is here, rather than a bit earlier where you might # expect it, because the pre section may be used to address # originals which are directories. So we don't check until # after any pre commands are run. if File.directory?(file) && !File.symlink?(file) && !config[:link][:overwrite_directory] raise "Can't proceed, original of #{file} is a directory,\n" + " consider the overwrite_directory flag if appropriate." end # Give save_orig a definitive answer on whether or not to save the # contents of an original directory. origpath = save_orig(file, true) # Update the history log save_history(file) # Make sure the directory tree for this link exists filedir = File.dirname(file) if !File.directory?(filedir) puts "Making directory tree #{filedir}" FileUtils.mkpath(filedir) if (!@dryrun) end # Make a backup in case we need to roll back. We have no use # for a backup if there are no test commands defined (since we # only use the backup to roll back if the test fails), so don't # bother to create a backup unless there is a test command defined. backup = nil if config[:test_before_post] || config[:test] backup = make_backup(file) puts "Created backup #{backup}" end # Create the link if set_link_destination remove_file(file) if (!@dryrun) File.symlink(dest, file) if (!@dryrun) # Check the permissions and ownership now to ensure they # end up set properly if @lchmod_supported = !(file, perms) end if @lchown_supported set_ownership = !compare_ownership(file, uid, gid) end end # Ensure the permissions are set properly if # Note: lchmod File.lchmod(perms, file) if (!@dryrun) end # Ensure the ownership is set properly if set_ownership begin # Note: lchown File.lchown(uid, gid, file) if (!@dryrun) rescue Errno::EPERM raise if Process.euid == 0 end end # Perform any test_before_post commands that the user has requested if !process_test_before_post(file, config) restore_backup(file, backup) raise "test_before_post failed" end # Perform any post-action commands that the user has requested process_post(file, config) # Perform any test commands that the user has requested if config[:test] if !process_test(file, config) restore_backup(file, backup) # Re-run any post commands process_post(file, config) end end # Clean up the backup, we don't need it anymore if config[:test_before_post] || config[:test] puts "Removing backup #{backup}" remove_file(backup) if (!@dryrun) end # Update the history log again save_history(file) throw :process_done end end if config[:directory] # Directory # A little safety check if !config[:directory][:create] raise "No create element found in directory section" end permstring = config[:directory][:perms].to_s perms = permstring.oct owner = config[:directory][:owner] group = config[:directory][:group] uid = lookup_uid(owner) gid = lookup_gid(group) set_directory = !File.directory?(file) || File.symlink?(file) = nil set_ownership = nil # If the file is currently something other than a directory then # always set the flags to set the permissions and ownership. # Checking the permissions/ownership of whatever is there currently # is useless. if set_directory = true set_ownership = true else = !(file, perms) set_ownership = !compare_ownership(file, uid, gid) end # Proceed if: # - The current file is not a directory # - The permissions or ownership requested don't match the # current permissions or ownership if !set_directory && ! && !set_ownership puts "No change to #{file} necessary" if (@debug) throw :process_done else # Tell the user what we're going to do if set_directory puts "Making directory #{file}" end if puts "Will set permissions on #{file} to #{permstring}" end if set_ownership puts "Will set ownership of #{file} to #{uid}:#{gid}" end # If the user requested interactive mode ask them for # confirmation to proceed. if @interactive case get_user_confirmation() when CONFIRM_PROCEED # No need to do anything when CONFIRM_SKIP save_results = false throw :process_done when CONFIRM_QUIT continue_processing = false save_results = false throw :process_done else raise "Unexpected result from get_user_confirmation()" end end # Perform any pre-action commands that the user has requested process_pre(file, config) # Give save_orig a definitive answer on whether or not to save the # contents of an original directory. origpath = save_orig(file, false) # Update the history log save_history(file) # Make sure the directory tree for this directory exists filedir = File.dirname(file) if !File.directory?(filedir) puts "Making directory tree #{filedir}" FileUtils.mkpath(filedir) if (!@dryrun) end # Make a backup in case we need to roll back. We have no use # for a backup if there are no test commands defined (since we # only use the backup to roll back if the test fails), so don't # bother to create a backup unless there is a test command defined. backup = nil if config[:test_before_post] || config[:test] backup = make_backup(file) puts "Created backup #{backup}" end # Create the directory if set_directory remove_file(file) if (!@dryrun) Dir.mkdir(file) if (!@dryrun) # Check the permissions and ownership now to ensure they # end up set properly = !(file, perms) set_ownership = !compare_ownership(file, uid, gid) end # Ensure the permissions are set properly if File.chmod(perms, file) if (!@dryrun) end # Ensure the ownership is set properly if set_ownership begin File.chown(uid, gid, file) if (!@dryrun) rescue Errno::EPERM raise if Process.euid == 0 end end # Perform any test_before_post commands that the user has requested if !process_test_before_post(file, config) restore_backup(file, backup) raise "test_before_post failed" end # Perform any post-action commands that the user has requested process_post(file, config) # Perform any test commands that the user has requested if config[:test] if !process_test(file, config) restore_backup(file, backup) # Re-run any post commands process_post(file, config) end end # Clean up the backup, we don't need it anymore if config[:test_before_post] || config[:test] puts "Removing backup #{backup}" remove_file(backup) if (!@dryrun) end # Update the history log again save_history(file) throw :process_done end end if config[:delete] # Delete whatever is there # A little safety check if !config[:delete][:proceed] raise "No proceed element found in delete section" end # Proceed only if the file currently exists if !File.exist?(file) && !File.symlink?(file) throw :process_done else # Tell the user what we're going to do puts "Removing #{file}" # If the user requested interactive mode ask them for # confirmation to proceed. if @interactive case get_user_confirmation() when CONFIRM_PROCEED # No need to do anything when CONFIRM_SKIP save_results = false throw :process_done when CONFIRM_QUIT continue_processing = false save_results = false throw :process_done else raise "Unexpected result from get_user_confirmation()" end end # Perform any pre-action commands that the user has requested process_pre(file, config) # If the original "file" is a directory and the user hasn't # specifically told us we can overwrite it then raise an exception. # # The test is here, rather than a bit earlier where you might # expect it, because the pre section may be used to address # originals which are directories. So we don't check until # after any pre commands are run. if File.directory?(file) && !File.symlink?(file) && !config[:delete][:overwrite_directory] raise "Can't proceed, original of #{file} is a directory,\n" + " consider the overwrite_directory flag if appropriate." end # Give save_orig a definitive answer on whether or not to save the # contents of an original directory. origpath = save_orig(file, true) # Update the history log save_history(file) # Make a backup in case we need to roll back. We have no use # for a backup if there are no test commands defined (since we # only use the backup to roll back if the test fails), so don't # bother to create a backup unless there is a test command defined. backup = nil if config[:test_before_post] || config[:test] backup = make_backup(file) puts "Created backup #{backup}" end # Remove the file remove_file(file) if (!@dryrun) # Perform any test_before_post commands that the user has requested if !process_test_before_post(file, config) restore_backup(file, backup) raise "test_before_post failed" end # Perform any post-action commands that the user has requested process_post(file, config) # Perform any test commands that the user has requested if config[:test] if !process_test(file, config) restore_backup(file, backup) # Re-run any post commands process_post(file, config) end end # Clean up the backup, we don't need it anymore if config[:test_before_post] || config[:test] puts "Removing backup #{backup}" remove_file(backup) if (!@dryrun) end # Update the history log again save_history(file) throw :process_done end end rescue Exception result['success'] = false exception = $! end # End begin block end # End :process_done catch block unlock_file(file) output = stop_output_capture if exception output << exception. output << exception.backtrace.join("\n") if @debug end result['message'] << output if save_results @results[file] = result end if exception raise exception end @already_processed[file] = true continue_processing end |
#process_guard(guard, commandname) ⇒ Object
2082 2083 2084 2085 2086 2087 |
# File 'lib/etch/client.rb', line 2082 def process_guard(guard, commandname) # Because the guard commands are processed every time etch runs we don't # want to print a message for them unless we're in debug mode. puts "Processing guard" if (@debug) process_exec(:guard, guard, commandname) end |
#process_post(file, config) ⇒ Object
2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 |
# File 'lib/etch/client.rb', line 2020 def process_post(file, config) execs = [] if [:post, :post_once, :post_once_per_run].any?{|p| config[p]} puts "Processing post commands" end # Add the "post once" items into the list of commands to process # if this is the first time etch has updated this file, and if # we haven't already run the command. if @first_update[file] && config[:post_once] config[:post_once].each do |once| if !@exec_already_processed.has_key?(once) execs << once @exec_already_processed[once] = true else puts "Skipping '#{once}', it has already " + "been executed once this run" if (@debug) end end end # Add in the regular post items if config[:post] execs.concat config[:post] end # post failures are considered non-fatal, so we ignore the # return value from process_exec (it takes care of warning # the user). execs.each { |exec| process_exec(:post, exec, file) } if config[:post_once_per_run] config[:post_once_per_run].each do |popr| # Stuff the "post once per run" nodes into the global hash to # be run after we've processed all files. puts "Adding '#{popr}' to 'post once per run' list" if (@debug) @exec_once_per_run[popr] = true end end end |
#process_pre(file, config) ⇒ Object
2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 |
# File 'lib/etch/client.rb', line 2007 def process_pre(file, config) if config[:pre] puts "Processing pre commands" config[:pre].each do |pre| r = process_exec(:pre, pre, file) # process_exec currently raises an exception if a setup or pre command # fails. In case that ever changes make sure we propagate # the error. return r if (!r) end end true end |
#process_setup(file, config) ⇒ Object
1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 |
# File 'lib/etch/client.rb', line 1991 def process_setup(file, config) if config[:setup] # Because the setup commands are processed every time etch runs # (rather than just when the file has changed, as with pre/post) we # don't want to print a message for them unless we're in debug mode. puts "Processing setup commands" if (@debug) config[:setup].each do |setup| r = process_exec(:setup, setup, file) # process_exec currently raises an exception if a setup or pre command # fails. In case that ever changes make sure we propagate # the error. return r if (!r) end end true end |
#process_test(file, config) ⇒ Object
2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 |
# File 'lib/etch/client.rb', line 2072 def process_test(file, config) if config[:test] puts "Processing test commands" config[:test].each do |test| r = process_exec(:test, test, file) # If the test failed we need to propagate that error return r if (!r) end end end |
#process_test_before_post(file, config) ⇒ Object
2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 |
# File 'lib/etch/client.rb', line 2061 def process_test_before_post(file, config) if config[:test_before_post] puts "Processing test_before_post commands" config[:test_before_post].each do |tbp| r = process_exec(:test_before_post, tbp, file) # If the test failed we need to propagate that error return r if (!r) end end true end |
#process_until_done(files, commands) ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 |
# File 'lib/etch/client.rb', line 214 def process_until_done(files, commands) # Our overall status. Will be reported to the server and used as the # return value for this method. Command-line clients should use it as # their exit value. Zero indicates no errors. status = 0 = '' # A variable to collect filenames if operating in @listfiles mode files_to_list = {} # Prep http instance http = nil if !@local puts "Connecting to #{@filesuri}" if (@debug) http = Net::HTTP.new(@filesuri.host, @filesuri.port) if @filesuri.scheme == "https" http.use_ssl = true if File.exist?(File.join(@configdir, 'etch', 'ca.pem')) http.ca_file = File.join(@configdir, 'etch', 'ca.pem') http.verify_mode = OpenSSL::SSL::VERIFY_PEER elsif File.directory?(File.join(@configdir, 'etch', 'ca')) http.ca_path = File.join(@configdir, 'etch', 'ca') http.verify_mode = OpenSSL::SSL::VERIFY_PEER end end http.start end # catch/throw for expected/non-error events that end processing # begin/raise for error events that end processing catch :stop_processing do begin enabled, = check_for_disable_etch_file if !enabled # 200 is the arbitrarily picked exit value indicating # that etch is disabled status = 200 throw :stop_processing end remove_stale_lock_files # Assemble the initial request request = nil if @local request = {} if files && !files.empty? request[:files] = {} files.each do |file| request[:files][file] = {:orig => save_orig(file)} local_requests = get_local_requests(file) if local_requests request[:files][file][:local_requests] = local_requests end end end if commands && !commands.empty? request[:commands] = {} commands.each do |command| request[:commands][command] = {} end end else request = get_blank_request if (files && !files.empty?) || (commands && !commands.empty?) if files files.each do |file| request["files[#{CGI.escape(file)}][sha1sum]"] = get_orig_sum(file) get_local_requests(file).each_with_index do |lr, i| request["files[#{CGI.escape(file)}][local_requests][#{i}]"] = lr end end end if commands commands.each do |command| request["commands[#{CGI.escape(command)}]"] = '1' end end else request['files[GENERATEALL]'] = '1' end end # # Loop back and forth with the server sending requests for files and # responding to the server's requests for original contents or sums # it needs # Signal.trap('EXIT') do STDOUT.reopen(ORIG_STDOUT) STDERR.reopen(ORIG_STDERR) unlock_all_files end # It usually takes a few back and forth exchanges with the server to # exchange all needed data and get a complete set of configuration. # The number of iterations is capped at 10 to prevent any unplanned # infinite loops. The limit of 10 was chosen somewhat arbitrarily but # seems fine in practice. 10.times do # # Send request to server # if @local response = @etch.generate(@local, @facts, request) else puts "Sending request to server #{@filesuri}: #{request.inspect}" if (@debug) post = Net::HTTP::Post.new(@filesuri.path) post.set_form_data(request) post['Accept'] = 'application/x-yaml' sign_post!(post, @key) httpresponse = http.request(post) if !httpresponse.kind_of?(Net::HTTPSuccess) $stderr.puts httpresponse.body # error! raises an exception httpresponse.error! end puts "Response from server:\n'#{httpresponse.body}'" if (@debug) if httpresponse['Content-Type'].split(';').first != 'application/x-yaml' raise "MIME type #{httpresponse['Content-Type']} is not yaml" end if httpresponse.body.nil? || httpresponse.body.empty? puts " Response is empty" if (@debug) break end response = YAML.load(httpresponse.body) end # # Process the response from the server # # Prep a clean request hash in case we need to make a # followup request if @local request = {} if response[:need_orig] && !response[:need_orig].empty? request[:files] = {} end if response[:retrycommands] && !response[:retrycommands].empty? request[:commands] = {} end else request = get_blank_request end # With generateall we expect to make at least two round trips # to the server. # 1) Send GENERATEALL request, get back a list of need_sums # 2) Send sums, possibly get back some need_origs # 3) Send origs, get back generated files need_to_loop = false reset_already_processed # Process configs first, as they may contain setup entries that are # needed to create the original files. response[:configs].each_key do |file| puts "Processing config for #{file}" if (@debug) if !@listfiles continue_processing = process_file(file, response) if !continue_processing throw :stop_processing end else files_to_list[file] = true end end response[:need_sum] && response[:need_sum].each do |need_sum| puts "Processing request for sum of #{need_sum}" if (@debug) if @local # If this happens we screwed something up, the local mode # code never requests sums. raise "No support for sums in local mode" else request["files[#{CGI.escape(need_sum)}][sha1sum]"] = get_orig_sum(need_sum) end local_requests = get_local_requests(need_sum) if local_requests if @local request[:files][need_sum][:local_requests] = local_requests else local_requests.each_with_index do |lr, i| request["files[#{CGI.escape(need_sum)}][local_requests][#{i}]"] = lr end end end need_to_loop = true end response[:need_orig] && response[:need_orig].each do |need_orig| puts "Processing request for contents of #{need_orig}" if (@debug) if @local request[:files][need_orig] = {:orig => save_orig(need_orig)} else request["files[#{CGI.escape(need_orig)}][contents]"] = Base64.encode64(get_orig_contents(need_orig)) request["files[#{CGI.escape(need_orig)}][sha1sum]"] = get_orig_sum(need_orig) end local_requests = get_local_requests(need_orig) if local_requests if @local request[:files][need_orig][:local_requests] = local_requests else local_requests.each_with_index do |lr, i| request["files[#{CGI.escape(need_orig)}][local_requests][#{i}]"] = lr end end end need_to_loop = true end response[:commands] && response[:commands].each_key do |commandname| puts "Processing commands #{commandname}" if (@debug) continue_processing = process_commands(commandname, response) if !continue_processing throw :stop_processing end end response[:retrycommands] && response[:retrycommands].each_key do |commandname| puts "Processing request to retry command #{commandname}" if (@debug) if @local request[:commands][commandname] = true else request["commands[#{CGI.escape(commandname)}]"] = '1' end need_to_loop = true end if !need_to_loop break end end puts "Processing 'exec once per run' commands" if (!exec_once_per_run.empty?) exec_once_per_run.keys.each do |exec| process_exec('post', exec) end rescue Exception => e status = 1 $stderr.puts e. << e. $stderr.puts e.backtrace.join("\n") if @debug << e.backtrace.join("\n") if @debug end # begin/rescue end # catch if @listfiles puts "Files under management:" files_to_list.keys.sort.each {|file| puts file} end # Send results to server if !@dryrun && !@local rails_results = [] # A few of the fields here are numbers or booleans and need a # to_s to make them compatible with CGI.escape, which expects a # string. rails_results << "fqdn=#{CGI.escape(@facts['fqdn'])}" rails_results << "status=#{CGI.escape(status.to_s)}" rails_results << "message=#{CGI.escape()}" if @detailed_results.include?('SERVER') @results.each do |file, result| # Strangely enough this works. Even though the key is not unique to # each result the Rails parameter parsing code keeps track of keys it # has seen, and if it sees a duplicate it starts a new hash. rails_results << "results[][file]=#{CGI.escape(file)}" rails_results << "results[][success]=#{CGI.escape(result['success'].to_s)}" rails_results << "results[][message]=#{CGI.escape(result['message'])}" end end puts "Sending results to server #{@resultsuri}" if (@debug) resultspost = Net::HTTP::Post.new(@resultsuri.path) # We have to bypass Net::HTTP's set_form_data method in this case # because it expects a hash and we can't provide the results in the # format we want in a hash because we'd have duplicate keys (see above). results_as_string = rails_results.join('&') resultspost.body = results_as_string resultspost.content_type = 'application/x-www-form-urlencoded' sign_post!(resultspost, @key) response = http.request(resultspost) case response when Net::HTTPSuccess puts "Response from server:\n'#{response.body}'" if (@debug) else $stderr.puts "Error submitting results:" $stderr.puts response.body end end if !@dryrun @detailed_results.each do |detail_dest| # If any of the destinations look like a file (start with a /) then we # log to that file if detail_dest =~ %r{^/} FileUtils.mkpath(File.dirname(detail_dest)) File.open(detail_dest, 'a') do |file| # Add a header for the overall status of the run file.puts "Etch run at #{Time.now}" file.puts "Status: #{status}" if !.empty? file.puts "Message:\n#{}\n" end # Then the detailed results @results.each do |resultfile, result| file.puts "File #{resultfile}, result #{result['success']}:\n" file.puts result['message'] end end end end end status end |
#recursive_copy(sourcedir, sourcefile, destdir) ⇒ Object
2286 2287 2288 2289 2290 2291 2292 2293 2294 |
# File 'lib/etch/client.rb', line 2286 def recursive_copy(sourcedir, sourcefile, destdir) # Note that cp -p will follow symlinks. GNU cp has a -d option to # prevent that, but Solaris cp does not, so we resort to cpio. # GNU cpio has a --quiet option, but Solaris cpio does not. Sigh. # GNU find and cpio also have -print0/--null to handle filenames with # spaces or special characters, but that's not standard either. system("cd #{sourcedir} && find #{sourcefile} | cpio -pdum #{destdir}") or raise "Recursive copy #{sourcedir}/#{sourcefile} to #{destdir} failed" end |
#recursive_copy_and_rename(sourcedir, sourcefile, destname) ⇒ Object
2295 2296 2297 2298 2299 2300 |
# File 'lib/etch/client.rb', line 2295 def recursive_copy_and_rename(sourcedir, sourcefile, destname) tmpdir = tempdir(destname) recursive_copy(sourcedir, sourcefile, tmpdir) File.rename(File.join(tmpdir, sourcefile), destname) Dir.delete(tmpdir) end |
#remove_file(file) ⇒ Object
2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 |
# File 'lib/etch/client.rb', line 2271 def remove_file(file) if ! File.exist?(file) && ! File.symlink?(file) puts "remove_file: #{file} doesn't exist" if (@debug) else # The secure delete mechanism doesn't seem to work consistently # when not root (in the ever-so-helpful way of not actually # removing the file and not indicating any error) if Process.euid == 0 FileUtils.rmtree(file, :secure => true) else FileUtils.rmtree(file) end end end |
#remove_stale_lock_files ⇒ Object
Any etch lockfiles more than a couple hours old are most likely stale and can be removed. If told to force we remove all lockfiles.
2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 |
# File 'lib/etch/client.rb', line 2373 def remove_stale_lock_files twohoursago = Time.at(Time.now - 60 * 60 * 2) if File.exist?(@lockbase) Find.find(@lockbase) do |file| next unless file =~ /\.LOCK$/ next unless File.file?(file) if @lockforce || File.mtime(file) < twohoursago puts "Removing stale lock file #{file}" File.delete(file) end end end end |
#reset_already_processed ⇒ Object
2388 2389 2390 |
# File 'lib/etch/client.rb', line 2388 def reset_already_processed @already_processed.clear end |
#restore_backup(file, backup) ⇒ Object
1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 |
# File 'lib/etch/client.rb', line 1968 def restore_backup(file, backup) filebase = File.basename(file) backuppath = File.join(backup, filebase) puts "Restoring #{backuppath} to #{file}" if !@dryrun # Clean up whatever we wrote out that caused the test to fail remove_file(file) # Then restore the backup if File.exist?(backuppath) || File.symlink?(backuppath) File.rename(backuppath, file) remove_file(backup) elsif File.exist?("#{backuppath}.NOORIG") # There was no original file, so we don't need to do # anything except remove our NOORIG marker file remove_file(backup) else raise "No backup found in #{backup} to restore to #{file}" end end end |
#save_history(file) ⇒ Object
This subroutine maintains a revision history for the file in @historybase
1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 |
# File 'lib/etch/client.rb', line 1785 def save_history(file) histdir = File.join(@historybase, "#{file}.HISTORY") current = File.join(histdir, 'current') # Migrate old RCS history if File.file?(histdir) && File.directory?(File.join(File.dirname(histdir), 'RCS')) if !@dryrun puts "Migrating old RCS history to new format" rcsmax = nil IO.popen("rlog #{histdir}") do |pipe| pipe.each do |line| if line =~ /^head: 1.(.*)/ rcsmax = $1.to_i break end end end if !rcsmax raise "Failed to parse RCS history rlog output" end tmphistdir = tempdir(histdir) 1.upto(rcsmax) do |rcsrev| rcsrevcontents = `co -q -p1.#{rcsrev} #{histdir}` # rcsrev-1 because RCS starts revisions at 1.1 and we start files # at 0000 File.open(File.join(tmphistdir, sprintf('%04d', rcsrev-1)), 'w') do |rcsrevfile| rcsrevfile.write(rcsrevcontents) end end FileUtils.copy(histdir, File.join(tmphistdir, 'current')) File.delete(histdir) File.rename(tmphistdir, histdir) end end # Make sure the directory tree for this file exists in the # directory we save history in. if !File.exist?(histdir) puts "Making history directory #{histdir}" FileUtils.mkpath(histdir) if (!@dryrun) end # If the history log doesn't exist and we didn't just create the original # backup then that indicates that the original backup was made previously # but the history log was not started at the same time. There are a # variety of reasons why this might be the case (the most likely is that # the original was saved manually by someone) but whatever the reason is # we want to use the original backup to start the history log before # updating the history log with the current file. if !File.exist?(File.join(histdir, 'current')) && !@first_update[file] origpath = save_orig(file) if File.file?(origpath) && !File.symlink?(origpath) puts "Starting history log with saved original file: " + "#{origpath} -> #{current}" FileUtils.copy(origpath, current) if (!@dryrun) else puts "Starting history log with 'ls -ld' output for " + "saved original file: #{origpath} -> #{current}" system("ls -ld #{origpath} > #{current} 2>&1") if (!@dryrun) end (file) end # Make temporary copy of file newcurrent = current+'.new' if File.file?(file) && !File.symlink?(file) puts "Updating history log: #{file} -> #{current}" if File.exist?(newcurrent) remove_file(newcurrent) end FileUtils.copy(file, newcurrent) if (!@dryrun) else puts "Updating history log with 'ls -ld' output: #{file} -> #{current}" system("ls -ld #{file} > #{newcurrent} 2>&1") if (!@dryrun) end # Roll current to next XXXX if current != XXXX if File.exist?(current) nextfile = '0000' maxfile = Dir.entries(histdir).select{|e|e=~/^\d+/}.max if maxfile if compare_file_contents(File.join(histdir, maxfile), File.read(current)) nextfile = nil else nextfile = sprintf('%04d', maxfile.to_i + 1) end end if nextfile File.rename(current, File.join(histdir, nextfile)) if (!@dryrun) end end # Move temporary copy to current File.rename(current+'.new', current) if (!@dryrun) (file) end |
#save_orig(file, save_directory_contents = nil) ⇒ Object
Save an original copy of the file if that hasn’t been done already. save_directory_contents can take three different values:
true: If the original is a directory then the contents should be
saved by putting them into a tarball
false: If the original is a directory do not save the contents,
just save the metadata of that directory (ownership and perms)
nil: We haven't yet received a full configuration for the file,
just a request for the original file checksum or contents.
As such we don't know yet what to do with a directory's
contents, nor do we want to save the final version of
non-directories as future setup commands or activity
outside of etch might create or change the original file
before etch is configured to change it. I.e. we save an
original file the first time etch changes that particular
file, not the first time etch runs on the box.
Return the path to that original copy.
1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 |
# File 'lib/etch/client.rb', line 1664 def save_orig(file, save_directory_contents=nil) origpathbase = File.join(@origbase, file) origpath = nil tmporigpath = "#{origpathbase}.TMP" if File.exist?("#{origpathbase}.ORIG") || File.symlink?("#{origpathbase}.ORIG") origpath = "#{origpathbase}.ORIG" elsif File.exist?("#{origpathbase}.NOORIG") origpath = "#{origpathbase}.NOORIG" elsif File.exist?("#{origpathbase}.TAR") origpath = "#{origpathbase}.TAR" else # The original file has not yet been saved first_update = true # Make sure the directory tree for this file exists in the # directory we save originals in. origdir = File.dirname(origpathbase) if !File.directory?(origdir) puts "Making directory tree #{origdir}" FileUtils.mkpath(origdir) if (!@dryrun) end if File.directory?(file) && !File.symlink?(file) # The original "file" is a directory if save_directory_contents # Tar up the original directory origpath = "#{origpathbase}.TAR" filedir = File.dirname(file) filebase = File.basename(file) puts "Saving contents of original directory #{file} as #{origpath}" system("cd #{filedir} && tar cf #{origpath} #{filebase}") if (!@dryrun) # There may be contents in that directory that the # user doesn't want exposed. Without a way to know, # the safest thing is to set restrictive permissions # on the tar file. File.chmod(0400, origpath) if (!@dryrun) elsif save_directory_contents.nil? # We have a timing issue, in that we generally save original # files before we have the configuration for that file. For # directories that's a problem, because we save directories # differently depending on whether we're configuring them to # remain a directory, or replacing the directory with something # else (file or symlink). So if we don't have a definitive # directive on how to save the directory # (i.e. save_directory_contents is nil) then just save a # placeholder until we do get a definitive directive. origpath = tmporigpath if !File.directory?(tmporigpath) || File.symlink?(tmporigpath) puts "Creating temporary original placeholder #{tmporigpath} for directory #{file}" remove_file(tmporigpath) if (!@dryrun) Dir.mkdir(tmporigpath) if (!@dryrun) end first_update = false else # Just create a directory in the originals repository with # ownership and permissions to match the original directory. origpath = "#{origpathbase}.ORIG" st = File::Stat.new(file) puts "Saving ownership/permissions of original directory #{file} as #{origpath}" Dir.mkdir(origpath, st.mode) if (!@dryrun) begin File.chown(st.uid, st.gid, origpath) if (!@dryrun) rescue Errno::EPERM raise if Process.euid == 0 end end elsif File.exist?(file) || File.symlink?(file) # The original file exists, and is not a directory proceed = true if save_directory_contents.nil? origpath = tmporigpath if File.exist?(tmporigpath) && !File.symlink?(tmporigpath) && compare_file_contents(tmporigpath, File.read(file)) proceed = false else puts "Saving temporary copy of original file: #{file} -> #{origpath}" end else origpath = "#{origpathbase}.ORIG" puts "Saving original file: #{file} -> #{origpath}" end if proceed filedir = File.dirname(file) filebase = File.basename(file) recursive_copy_and_rename(filedir, filebase, origpath) if (!@dryrun) end else # If the original doesn't exist, we need to flag that so # that we don't try to save our generated file as an # original on future runs proceed = true if save_directory_contents.nil? origpath = tmporigpath if File.exist?(tmporigpath) && !File.symlink?(tmporigpath) && File.stat(tmporigpath).zero? proceed = false else puts "Original file #{file} doesn't exist, saving that state temporarily as #{tmporigpath}" end else origpath = "#{origpathbase}.NOORIG" puts "Original file #{file} doesn't exist, saving that state permanently as #{origpath}" end if proceed File.open(origpath, 'w') { |origfile| } if (!@dryrun) end end @first_update[file] = first_update end # Remove the TMP placeholder if it exists and no longer applies if origpath != tmporigpath && File.exists?(tmporigpath) puts "Removing old temp orig placeholder #{tmporigpath}" if (@debug) remove_file(tmporigpath) end origpath end |
#set_history_permissions(file) ⇒ Object
Ensures that the history log file has appropriate permissions to avoid leaking information.
1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 |
# File 'lib/etch/client.rb', line 1885 def (file) origpath = File.join(@origbase, "#{file}.ORIG") histdir = File.join(@historybase, "#{file}.HISTORY") # We set the permissions to the more restrictive of the original # file permissions and the current file permissions. origperms = 0777 if File.exist?(origpath) st = File.lstat(origpath) # Mask off the file type origperms = st.mode & 07777 end fileperms = 0777 if File.exist?(file) st = File.lstat(file) # Mask off the file type fileperms = st.mode & 07777 end histperms = origperms & fileperms if File.directory?(histdir) Dir.foreach(histdir) do |histfile| next if histfile == '.' next if histfile == '..' File.chmod(histperms, File.join(histdir, histfile)) if (!@dryrun) end end end |
#sign_post!(post, key) ⇒ Object
This method takes in a Net::HTTP::Post and a path to a private key. It will insert a ‘timestamp’ parameter to the post body, hash the body of the post, sign the hash using the private key, and insert that signature in the HTTP Authorization header field in the post.
2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 |
# File 'lib/etch/client.rb', line 2513 def sign_post!(post, key) if key post.body << "×tamp=#{CGI.escape(Time.now.to_s)}" private_key = OpenSSL::PKey::RSA.new(File.read(key)) hashed_body = Digest::SHA1.hexdigest(post.body) signature = Base64.encode64(private_key.private_encrypt(hashed_body)) # encode64 breaks lines at 60 characters with newlines. Having newlines # in an HTTP header screws things up (the lines get interpreted as # separate headers) so strip them out. The Base64 standards seem to # generally have a limit on line length, but Ruby's decode64 doesn't # seem to complain. If it ever becomes a problem the server could # rebreak the lines. signature.gsub!("\n", '') post['Authorization'] = "EtchSignature #{signature}" end end |
#start_output_capture ⇒ Object
2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 |
# File 'lib/etch/client.rb', line 2398 def start_output_capture # Establish a pipe, spawn a child process, and redirect stdout/stderr # to the pipe. The child gathers up anything sent over the pipe and # when we close the pipe later it sends the captured output back to us # over a second pipe. pread = pwrite = oread = owrite = nil if RUBY_VERSION.split('.')[0..1].join('.').to_f >= 1.9 pread, pwrite = IO.pipe(Encoding.default_external, 'UTF-8', :invalid => :replace, :undef => :replace) oread, owrite = IO.pipe(Encoding.default_external, 'UTF-8', :invalid => :replace, :undef => :replace) else pread, pwrite = IO.pipe oread, owrite = IO.pipe end if fork # Parent pread.close owrite.close # Can't use $stdout and $stderr here, child processes don't # inherit them and process() spawns a variety of child # processes which have output we want to capture. oldstdout = STDOUT.dup oldstderr = STDERR.dup STDOUT.reopen(pwrite) STDERR.reopen(pwrite) pwrite.close @output_pipes << [oread, oldstdout, oldstderr] else # Child # We need to catch any exceptions in the child because the parent # might spawn this process in the context of a begin block (in fact # it does at the time of this writing), in which case if we throw an # exception here execution will jump back to that block, therefore not # exiting the child where we want but rather making a big mess of # things by continuing to execute the main body of code in parallel # with the parent process. begin pwrite.close oread.close # If we're somewhere past the first level of the recursion in # processing files the stdout/stderr we inherit from our parent will # actually be a pipe to the previous file's child process. We want # every child process to talk directly to the real filehandles, # otherwise every file that has dependencies will end up with the # output for those dependencies gathered with its output. STDOUT.reopen(ORIG_STDOUT) STDERR.reopen(ORIG_STDERR) # stdout is line buffered by default, so if we didn't enable sync here # then we wouldn't see the output of the putc below until we output a # newline. $stdout.sync = true output = '' begin # A surprising number of apps that we restart are ill-behaved and do # not properly close stdin/stdout/stderr. With etch's output # capturing feature this results in etch hanging around forever # waiting for the pipes to close. We time out after a suitable # period of time so that etch processes don't hang around forever. timeout = nil if @interactive timeout = OUTPUT_CAPTURE_INTERACTIVE_TIMEOUT else timeout = OUTPUT_CAPTURE_TIMEOUT end Timeout.timeout(timeout) do while char = pread.getc putc(char) output << char.chr end end rescue Timeout::Error $stderr.puts "Timeout in output capture, some app restarted via post probably didn't daemonize properly" end pread.close owrite.write(output) owrite.close rescue Exception => e $stderr.puts "Exception in output capture child: " + e. $stderr.puts e.backtrace.join("\n") if @debug end # Exit in such a way that we don't trigger any signal handlers that # we might have inherited from the parent process exit! end end |
#stop_output_capture ⇒ Object
2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 |
# File 'lib/etch/client.rb', line 2482 def stop_output_capture oread, oldstdout, oldstderr = @output_pipes.pop # The reopen and close closes the parent's end of the pipe to the child STDOUT.reopen(oldstdout) STDERR.reopen(oldstderr) oldstdout.close oldstderr.close # Which triggers the child to send us the gathered output over the # second pipe output = oread.read oread.close # And then the child exits Process.wait output end |
#tempdir(file) ⇒ Object
Ruby 1.8.7 and later have Dir.mktmpdir, but we support ruby 1.8.5 for RHEL/CentOS 5. So this is a basic substitute. FIXME: consider “backport” for Dir.mktmpdir
1931 1932 1933 1934 1935 1936 1937 1938 1939 |
# File 'lib/etch/client.rb', line 1931 def tempdir(file) filebase = File.basename(file) filedir = File.dirname(file) tmpfile = Tempfile.new(filebase, filedir) tmpdir = tmpfile.path tmpfile.close! Dir.mkdir(tmpdir) tmpdir end |
#unlock_all_files ⇒ Object
2367 2368 2369 |
# File 'lib/etch/client.rb', line 2367 def unlock_all_files @locked_files.each_key { |file| unlock_file(file) } end |
#unlock_file(file) ⇒ Object
2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 |
# File 'lib/etch/client.rb', line 2342 def unlock_file(file) lockpath = File.join(@lockbase, "#{file}.LOCK") # Since we don't create lock files in dry run mode the rest of this # method won't behave properly return if (@dryrun) if File.exist?(lockpath) pid = nil File.open(lockpath) { |f| pid = f.gets.chomp.to_i } if pid == $$ puts "Unlocking #{file}" if (@debug) File.delete(lockpath) @locked_files.delete(file) else # This shouldn't happen, if it does it's a bug raise "Process #{Process.pid} asked to unlock #{file} which is locked by another process (pid #{pid})" end else # This shouldn't happen either warn "Lock for #{file} lost" @locked_files.delete(file) end end |