Module: AethernalAgent::Filesystem
Instance Method Summary collapse
- #aethernal_agent_file(source_file, target_file_path, options = {}) ⇒ Object
- #aethernal_agent_folder ⇒ Object
- #copy(source, target, options = {}) ⇒ Object
- #directory(path, options = {}) ⇒ Object
- #file(file_path, options = {}) ⇒ Object
- #file_settings(file_path, options = {}) ⇒ Object
- #files_folder ⇒ Object
- #files_path(file) ⇒ Object
- #home_folder_path(path = nil) ⇒ Object
- #meta_folder ⇒ Object
- #meta_path(file) ⇒ Object
- #set_ownership(path, user, group = nil) ⇒ Object
- #set_permissions(path, chmod) ⇒ Object
- #template_path(file) ⇒ Object
- #templates_folder ⇒ Object
- #write_template(in_path, out_path, vars, options = {}) ⇒ Object
Instance Method Details
#aethernal_agent_file(source_file, target_file_path, options = {}) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/aethernal_agent/filesystem.rb', line 144 def aethernal_agent_file(source_file, target_file_path, = {}) AethernalAgent.logger.debug("Attempting to copy file '#{source_file}' to '#{target_file_path}'") if File.exists?(target_file_path) AethernalAgent.logger.debug("File already exists, not copying.") else begin FileUtils.cp(File.join(files_folder,source_file), target_file_path) rescue SystemCallError => e AethernalAgent.logger.debug("'#{source_file}' could not be created, giving up. Error: '#{e}'") add_errors(e,source_file: source_file, target_file_path: target_file_path) return false end end (target_file_path, [:chmod]) if [:chmod] set_ownership(target_file_path, [:owner]) if [:owner] end |
#aethernal_agent_folder ⇒ Object
141 142 |
# File 'lib/aethernal_agent/filesystem.rb', line 141 def aethernal_agent_folder end |
#copy(source, target, options = {}) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/aethernal_agent/filesystem.rb', line 83 def copy(source, target, = {}) AethernalAgent.logger.debug "Applying action #{[:action]} on file or directory '#{source}'." if source.present? && target.present? if File.directory?(source) || File.exist?(source) if File.exists?(target) add_errors("File or folder already exists.", path: target) else begin if File.directory?(source) FileUtils.cp_r(source, target) else FileUtils.cp(source, target) end rescue SystemCallError => e add_errors(e, path: file_path) end end else add_errors("File or folder does not exist.", path: source) end else add_errors("Source or destination are missing.") end end |
#directory(path, options = {}) ⇒ Object
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 |
# File 'lib/aethernal_agent/filesystem.rb', line 108 def directory(path, = {}) .reverse_merge!(action: :create) AethernalAgent.logger.debug "Applying action #{[:action]} on directory '#{path}'." if [:action] == :create unless File.directory?(path) begin FileUtils.mkdir_p(path) rescue Errno::EEXIST, Errno::EACCES => e add_errors(e, path: path) return end else AethernalAgent.logger.debug "#{path} folder already exist, not creating." end set_ownership(path, [:owner]) if [:owner] (path, [:chmod]) if [:chmod] elsif [:action] == :delete if File.directory?(path) begin FileUtils.rm_r(path) rescue Errno::EACCES => e add_errors(e, path: path) end else AethernalAgent.logger.debug "#{path} does not exist, not removing." end end return true end |
#file(file_path, options = {}) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/aethernal_agent/filesystem.rb', line 59 def file(file_path, = {}) .reverse_merge!(action: :create) AethernalAgent.logger.debug "Applying action #{[:action]} on file '#{file_path}'." if [:action] == :delete if File.directory?(file_path) add_errors("File is directory, not going to remove it with file() method.", path: file_path) elsif File.exist?(file_path) begin FileUtils.rm(file_path) rescue SystemCallError => e add_errors(e, path: file_path) end else #add_errors("File does not exist.", path: file_path) AethernalAgent.logger.warn "File '#{file_path}' did not exist so not removing" end else set_ownership(file_path, [:owner]) if [:owner] (file_path, [:chmod]) if [:chmod] end end |
#file_settings(file_path, options = {}) ⇒ Object
53 54 55 56 57 |
# File 'lib/aethernal_agent/filesystem.rb', line 53 def file_settings(file_path, = {}) AethernalAgent.logger.debug "Setting file settings with options: #{}" (file_path, [:chmod]) if [:chmod] set_ownership(file_path, [:owner]) if [:owner] end |
#files_folder ⇒ Object
3 4 5 |
# File 'lib/aethernal_agent/filesystem.rb', line 3 def files_folder File.join(File.(File.dirname(plugin_path)), "files/") end |
#files_path(file) ⇒ Object
7 8 9 |
# File 'lib/aethernal_agent/filesystem.rb', line 7 def files_path(file) File.join(files_folder, file) end |
#home_folder_path(path = nil) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/aethernal_agent/filesystem.rb', line 27 def home_folder_path(path = nil) if !path.nil? File.join(Dir.home(self.user), path) else File.join(Dir.home(self.user)) end end |
#meta_folder ⇒ Object
11 12 13 |
# File 'lib/aethernal_agent/filesystem.rb', line 11 def File.join(File.(File.dirname(plugin_path)), "meta/") end |
#meta_path(file) ⇒ Object
15 16 17 |
# File 'lib/aethernal_agent/filesystem.rb', line 15 def (file) File.join(, file) end |
#set_ownership(path, user, group = nil) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/aethernal_agent/filesystem.rb', line 35 def set_ownership(path, user, group=nil) AethernalAgent.logger.debug("Setting ownership of '#{path}' to user '#{user}'") begin FileUtils.chown_R(user,group,path) rescue SystemCallError => e add_errors(e, path: path, user:user, group: group) end end |
#set_permissions(path, chmod) ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/aethernal_agent/filesystem.rb', line 44 def (path, chmod) AethernalAgent.logger.debug("Setting chmod of '#{path}' to chmod '#{chmod}'") begin FileUtils.chmod(chmod, path) rescue SystemCallError => e add_errors(e, path: path, chmod: chmod) end end |
#template_path(file) ⇒ Object
23 24 25 |
# File 'lib/aethernal_agent/filesystem.rb', line 23 def template_path(file) File.join(templates_folder, file) end |
#templates_folder ⇒ Object
19 20 21 |
# File 'lib/aethernal_agent/filesystem.rb', line 19 def templates_folder File.join(File.(File.dirname(plugin_path)), "templates/") end |
#write_template(in_path, out_path, vars, options = {}) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/aethernal_agent/filesystem.rb', line 163 def write_template(in_path, out_path, vars, ={}) out_folder = File.dirname(out_path) unless Dir.exist?(out_folder) FileUtils.mkdir_p(out_folder) (out_folder, [:chmod]) if [:chmod] set_ownership(out_folder, [:owner]) if [:owner] end AethernalAgent.logger.debug("Writing template '#{in_path}' to '#{out_path}'") template = AethernalAgent::Template.new(in_path,vars, ) if template.parse return template.write_to(out_path) end end |