Module: AethernalAgent::Filesystem

Included in:
Apache, App, Template
Defined in:
lib/aethernal_agent/filesystem.rb

Instance Method Summary collapse

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, options = {})
  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

  set_permissions(target_file_path, options[:chmod]) if options[:chmod]
  set_ownership(target_file_path, options[:owner]) if options[:owner]
end

#aethernal_agent_folderObject



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, options = {})
  AethernalAgent.logger.debug "Applying action #{options[: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, options = {})
  options.reverse_merge!(action: :create)

  AethernalAgent.logger.debug "Applying action #{options[:action]} on directory '#{path}'."
  if options[: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, options[:owner]) if options[:owner]
    set_permissions(path, options[:chmod]) if options[:chmod]
  elsif options[: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, options = {})
  options.reverse_merge!(action: :create)

  AethernalAgent.logger.debug "Applying action #{options[:action]} on file '#{file_path}'."

  if options[: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, options[:owner]) if options[:owner]
    set_permissions(file_path, options[:chmod]) if options[: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, options = {})
  AethernalAgent.logger.debug "Setting file settings with options: #{options}"
  set_permissions(file_path, options[:chmod]) if options[:chmod]
  set_ownership(file_path, options[:owner]) if options[:owner]
end

#files_folderObject



3
4
5
# File 'lib/aethernal_agent/filesystem.rb', line 3

def files_folder
  File.join(File.expand_path(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_folderObject



11
12
13
# File 'lib/aethernal_agent/filesystem.rb', line 11

def meta_folder
  File.join(File.expand_path(File.dirname(plugin_path)), "meta/")
end

#meta_path(file) ⇒ Object



15
16
17
# File 'lib/aethernal_agent/filesystem.rb', line 15

def meta_path(file)
  File.join(meta_folder, 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 set_permissions(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_folderObject



19
20
21
# File 'lib/aethernal_agent/filesystem.rb', line 19

def templates_folder
  File.join(File.expand_path(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, options={})
  out_folder = File.dirname(out_path)
  unless Dir.exist?(out_folder)
    FileUtils.mkdir_p(out_folder)
    set_permissions(out_folder, options[:chmod]) if options[:chmod]
    set_ownership(out_folder, options[:owner]) if options[:owner]
  end

  AethernalAgent.logger.debug("Writing template '#{in_path}' to '#{out_path}'")
  template = AethernalAgent::Template.new(in_path,vars, options)

  if template.parse
    return template.write_to(out_path)
  end
end