Class: DanarchyDeploy::Templater
- Inherits:
-
Object
- Object
- DanarchyDeploy::Templater
- Defined in:
- lib/danarchy_deploy/templater.rb
Class Method Summary collapse
- .check_user_group(owner, group) ⇒ Object
- .diff(target, tmpfile) ⇒ Object
- .enable_erb(target, tmpfile) ⇒ Object
- .md5sum(target, tmpfile) ⇒ Object
- .new(templates, options) ⇒ Object
- .remove_templates(templates, options) ⇒ Object
- .verify_permissions(target, perms, options) ⇒ Object
- .write_tmpfile(source, data) ⇒ Object
Class Method Details
.check_user_group(owner, group) ⇒ Object
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 |
# File 'lib/danarchy_deploy/templater.rb', line 118 def self.check_user_group(owner, group) (uid, gid) = nil IO.popen("id -u #{owner} 2>/dev/null") do |id| uid = id.gets if uid == nil puts " ! User: #{owner} not found! Using: 'root'" owner = 'root' uid = 0 end uid = uid.chomp.to_i if uid.class == String end IO.popen("id -g #{group} 2>/dev/null") do |id| gid = id.gets if gid == nil puts " ! Group: #{group} not found! Using: 'root'" group = 'root' gid = 0 end gid = gid.chomp.to_i if gid.class == String end [owner, uid, group, gid] end |
.diff(target, tmpfile) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/danarchy_deploy/templater.rb', line 152 def self.diff(target, tmpfile) if File.exist?(target) && File.exist?(tmpfile) puts "\n!! Diff between #{target} <=> #{tmpfile}" IO.popen("diff -Naur #{target} #{tmpfile}") do |o| puts o.read end puts "\n!! End Diff \n\n" elsif File.exist?(target) && !File.exist?(tmpfile) return false end end |
.enable_erb(target, tmpfile) ⇒ Object
164 165 166 167 168 169 |
# File 'lib/danarchy_deploy/templater.rb', line 164 def self.enable_erb(target, tmpfile) puts "\n |+ Moving #{tmpfile} => #{target}" targetdir = File.dirname(target) Dir.exist?(targetdir) || FileUtils.mkdir_p(targetdir, mode: 0755) system("mv #{tmpfile} #{target}") end |
.md5sum(target, tmpfile) ⇒ Object
144 145 146 147 148 149 150 |
# File 'lib/danarchy_deploy/templater.rb', line 144 def self.md5sum(target, tmpfile) if File.exist?(target) && File.exist?(tmpfile) FileUtils.identical?(target, tmpfile) elsif File.exist?(target) && !File.exist?(tmpfile) return false end end |
.new(templates, options) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/danarchy_deploy/templater.rb', line 6 def self.new(templates, ) puts "\n" + self.name if templates.map(&:keys).flatten.include?(:remove) puts ' > Removing templates tagged for removal.' templates = remove_templates(templates, ) end templates.each do |template| next if template[:remove] abort("No target destination set for template: #{template[:source]}!") if !template[:target] abort("No source or data for template: #{template[:target]}") if !template[:source] && !template[:data] abort("Source file does not exist at: #{template[:source]}") if ! File.exist?(template[:source]) target = template[:target] source = template[:source] || '-- encoded data --' dir_perms = template[:dir_perms] file_perms = template[:file_perms] @variables = template[:variables] || nil puts "\n > Checking: #{target}" puts " Source: #{source}" puts " |> Dir Permissions: #{dir_perms || '--undefined--'}" puts " |> File Permissions: #{file_perms || '--undefined--'}" if [:vars_verbose] && @variables puts ' |> Variables: ' var = DanarchyDeploy::Helpers.hash_except(@variables, '^pass') puts var end tmpdir = [:deploy_dir] + '/' + File.basename(File.dirname(target)) Dir.exist?(tmpdir) || FileUtils.mkdir_p(tmpdir, mode: 0755) tmpfile = tmpdir + '/' + File.basename(target) + '.tmp' if source == '-- encoded data --' data = DanarchyDeploy::Helpers.decode_base64(template[:data]) source = tmpfile + '.erb' write_tmpfile(source, data) end File.open(tmpfile, 'w') do |f| result = ERB.new(File.read(source), nil, '-').result(binding) f.write(result) f.close end result = { write_erb: [], verify_permissions: {} } if [:pretend] diff(target, tmpfile) if [:vars_verbose] puts "\n - Fake Run: Not changing '#{target}'." result[:verify_permissions][File.dirname(tmpfile)] = (File.dirname(tmpfile), dir_perms, ) result[:verify_permissions][tmpfile] = (tmpfile, file_perms, ) elsif md5sum(target,tmpfile) == true puts "\n - No change in '#{target}': Nothing to update here." result[:verify_permissions][File.dirname(target)] = (File.dirname(target), dir_perms, ) result[:verify_permissions][target] = (target, file_perms, ) else diff(target, tmpfile) if [:vars_verbose] result[:write_erb] = enable_erb(target, tmpfile) puts " |+ #{target} was updated!" result[:verify_permissions][File.dirname(target)] = (File.dirname(target), dir_perms, ) result[:verify_permissions][target] = (target, file_perms, ) end FileUtils.rm_rf(File.dirname(tmpfile)) result end end |
.remove_templates(templates, options) ⇒ Object
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 |
# File 'lib/danarchy_deploy/templater.rb', line 175 def self.remove_templates(templates, ) templates.delete_if do |template| if template.keys.include?(:remove) if [:pretend] puts " - Fake Run - Would have removed: '#{template[:target]}'." false elsif File.exist?(template[:target]) puts " |- Removing: #{template[:target]}" File.delete(template[:target]) dirname = File.dirname(template[:target]) if Dir.exist?(dirname) && Dir.empty?(dirname) puts " |- Removing empty directory: #{dirname}" Dir.rmdir(dirname) if Dir.exist?(dirname) puts " ! Failed to remove directory!" else puts " - Removed directory." end end true end end end templates end |
.verify_permissions(target, perms, options) ⇒ Object
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 |
# File 'lib/danarchy_deploy/templater.rb', line 75 def self.(target, perms, ) (owner, group, mode) = nil chmod = nil puts "\n > Verifying ownership and permissions for '#{target}'" if perms puts " |+ Setting file mode to: #{perms[:mode]}" (owner, group, mode) = perms[:owner], perms[:group], perms[:mode] else if File.stat(target).mode & 07777 == '0777'.to_i(8) puts " ! '#{target}' has 0777 permissions! Setting those to something more sane." if File.ftype(target) == 'directory' puts " |+ Setting file mode to: 0775" chmod = File.chmod(0775, target) ? true : false if ![:pretend] elsif File.ftype(target) == 'file' puts " |+ Setting file mode to: 0644" chmod = File.chmod(0644, target) ? true : false if ![:pretend] end else puts " - Permissions were not defined for '#{target}'! Leaving them alone." end return [chmod] end (owner, uid, group, gid) = check_user_group(owner, group) updated = [] file_stat = File.stat(target) if file_stat.uid != uid || file_stat.gid != gid puts " |+ Setting ownership to: #{owner}:#{group}" chown = File.chown(uid, gid, target) ? true : false if ![:pretend] updated.push(chown) end if file_stat.mode & 07777 != mode.to_i(8) puts " |+ Setting file mode to: #{mode}" chmod = File.chmod(mode.to_i(8), target) ? true : false if ![:pretend] updated.push(chmod) end updated end |
.write_tmpfile(source, data) ⇒ Object
171 172 173 |
# File 'lib/danarchy_deploy/templater.rb', line 171 def self.write_tmpfile(source, data) File.write(source, data) end |