Top Level Namespace
Instance Method Summary collapse
-
#abs(path) ⇒ Object
absolute path.
-
#central(configurations) ⇒ Object
run central configuration.
-
#chdir(dir) ⇒ Object
change current working directory.
-
#check_tool(name, check) ⇒ Object
function used to check that system has all required tools installed.
-
#chmod(path, permissions, recursive = false) ⇒ Object
change file permissions.
-
#curl(url, path) ⇒ Object
download url into a path using curl.
-
#dir_exists?(path) ⇒ Boolean
check if directory exists.
-
#erb(file, output_file = nil) ⇒ Object
process erb template into an output_file.
-
#file_dir(path) ⇒ Object
get directory name of a file.
-
#file_exists?(path) ⇒ Boolean
check if file exists.
-
#git(url, path) ⇒ Object
git clone url into a path.
-
#hostname ⇒ Object
get hostname.
-
#ls(path, options = {}) ⇒ Object
list directory content.
-
#mkdir(path) ⇒ Object
make directory including intermediate directories.
-
#os ⇒ Object
get operating system.
-
#pwd ⇒ Object
current working directory.
-
#read(file) ⇒ Object
read content of a file.
-
#rm(path, recursive = false) ⇒ Object
remove file/directory.
-
#rmdir(path) ⇒ Object
remove directory recursively.
-
#run(file) ⇒ Object
run configuration.rb file.
-
#run_if_exists(file) ⇒ Object
run configuration.rb file only if it exists.
-
#source(file, source) ⇒ Object
source file in sh/bash/zsh script.
-
#sudo(command, sudo = false) ⇒ Object
run command second time with sudo privileges if it previously failed because of insufficient permissions.
-
#symlink(from, to) ⇒ Object
symlink path.
-
#symlink?(symlink) ⇒ Boolean
check if file is symlink.
-
#symlink_path(symlink) ⇒ Object
get full path of symlink.
-
#touch(path) ⇒ Object
touch file.
-
#write(file, content) ⇒ Object
write content into a file.
Instance Method Details
#abs(path) ⇒ Object
absolute path
78 79 80 |
# File 'lib/central.rb', line 78 def abs(path) path = File.absolute_path(File.(path)) end |
#central(configurations) ⇒ Object
run central configuration
333 334 335 336 337 338 339 340 341 |
# File 'lib/central.rb', line 333 def central(configurations) if configurations.instance_of?(Array) && configurations.length > 0 configurations.each {|configuration| run configuration } elsif configurations.instance_of?(String) run configurations else run 'configuration.rb' end end |
#chdir(dir) ⇒ Object
change current working directory
83 84 85 |
# File 'lib/central.rb', line 83 def chdir(dir) Dir.chdir(abs(dir)) end |
#check_tool(name, check) ⇒ Object
function used to check that system has all required tools installed
56 57 58 59 60 61 62 |
# File 'lib/central.rb', line 56 def check_tool(name,check) output = sudo("#{check} 2>&1 1>/dev/null").downcase if output.include?('command not found') STDERR.puts "#{name} not found, please install it to use central" exit 1 end end |
#chmod(path, permissions, recursive = false) ⇒ Object
change file permissions
158 159 160 161 162 163 164 165 166 |
# File 'lib/central.rb', line 158 def chmod(path,,recursive=false) path = abs(path) if recursive recursive = '-R ' else recursive = '' end sudo("chmod #{recursive}#{permissions} \"#{path}\"") end |
#curl(url, path) ⇒ Object
download url into a path using curl
209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/central.rb', line 209 def curl(url,path) path = abs(path) output = sudo("curl -s \"#{url}\"") unless $?.exitstatus == 0 STDERR.puts "Couldn't download file from #{url}..." exit 1 end if File.exists?(path) && File.read(path) == output return end File.write(path,output) puts "Downloaded #{url} → #{path}" end |
#dir_exists?(path) ⇒ Boolean
check if directory exists
94 95 96 97 |
# File 'lib/central.rb', line 94 def dir_exists?(path) path = abs(path) Dir.exists?(path) end |
#erb(file, output_file = nil) ⇒ Object
process erb template into an output_file
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/central.rb', line 288 def erb(file,output_file = nil) file = abs(file) if output_file == nil if file.end_with?('.erb') output_file = file[0...-4] else output_file = file+'.out' end end if file_exists?(file) output = ERB.new(File.read(file)).result if File.exists?(output_file) && File.read(output_file) == output return end File.write(output_file,output) puts "Processed erb #{file} → #{output_file}" else STDERR.puts "Couldn't process erb file #{file}..." exit 1 end end |
#file_dir(path) ⇒ Object
get directory name of a file
100 101 102 |
# File 'lib/central.rb', line 100 def file_dir(path) File.dirname(abs(path)) end |
#file_exists?(path) ⇒ Boolean
check if file exists
88 89 90 91 |
# File 'lib/central.rb', line 88 def file_exists?(path) path = abs(path) File.file?(path) && File.readable?(path) end |
#git(url, path) ⇒ Object
git clone url into a path
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/central.rb', line 190 def git(url,path) path = abs(path) if dir_exists?(path) && dir_exists?("#{path}/.git") cwd = pwd() chdir path out = sudo('git pull') unless out.downcase.include? "already up-to-date" puts out puts "Git repository pulled: #{url} → #{path}" end chdir cwd else out = sudo("git clone #{url} \"#{path}\"") puts out puts "Git repository cloned: #{url} → #{path}" end end |
#hostname ⇒ Object
get hostname
10 11 12 |
# File 'lib/central.rb', line 10 def hostname Socket.gethostname end |
#ls(path, options = {}) ⇒ Object
list directory content
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 |
# File 'lib/central.rb', line 253 def ls(path,={}) path = abs(path) if [:dotfiles] dotfiles = '-a ' else dotfiles = '' end command = "ls -1 #{dotfiles}\"#{path}\"" if .key?(:grep) && [:grep].length > 0 command += " | grep #{options[:grep]}" end output = sudo(command) if output.downcase.end_with?('no such file or directory') STDERR.puts "Couldn't ls directory #{path}..." exit 1 end ls = output.split("\n") dir = true file = true if .key?(:dir) dir = [:dir] end if .key?(:file) file = [:file] end unless dir ls = ls.keep_if {|f| !File.directory?("#{path}/#{f}") } end unless file ls = ls.keep_if {|f| !File.file?("#{path}/#{f}") } end return ls end |
#mkdir(path) ⇒ Object
make directory including intermediate directories
115 116 117 118 119 120 121 |
# File 'lib/central.rb', line 115 def mkdir(path) path = abs(path) unless dir_exists?(path) out = sudo("mkdir -p \"#{path}\"") puts "Created directory: #{path}" end end |
#os ⇒ Object
get operating system
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/central.rb', line 15 def os if RUBY_PLATFORM.include?('linux') return 'linux' elsif RUBY_PLATFORM.include?('darwin') return 'osx' elsif RUBY_PLATFORM.include?('freebsd') return 'freebsd' elsif RUBY_PLATFORM.include?('solaris') return 'solaris' end end |
#pwd ⇒ Object
current working directory
73 74 75 |
# File 'lib/central.rb', line 73 def pwd Dir.pwd end |
#read(file) ⇒ Object
read content of a file
224 225 226 227 228 229 230 231 232 |
# File 'lib/central.rb', line 224 def read(file) file = abs(file) if file_exists?(file) return File.read(file) else STDERR.puts "Couldn't read file #{file}..." exit 1 end end |
#rm(path, recursive = false) ⇒ Object
remove file/directory
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/central.rb', line 124 def rm(path,recursive=false) path = abs(path) if recursive recursive = '-R ' else recursive = '' end is_dir = dir_exists?(path) is_symlink = symlink?(path) out = sudo("rm #{recursive}-f \"#{path}\"") if is_dir puts "Removed directory: #{path}" elsif is_symlink puts "Removed symlink: #{path}" else puts "Removed file: #{path}" end end |
#rmdir(path) ⇒ Object
remove directory recursively
144 145 146 |
# File 'lib/central.rb', line 144 def rmdir(path) rm(path,true) end |
#run(file) ⇒ Object
run configuration.rb file
311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/central.rb', line 311 def run(file) cwd = pwd() file = abs(file) unless file_exists?(file) puts "No configuration file: #{file} found" return end puts "Running configuration: "+file file_cwd = file_dir(file) chdir file_cwd load file chdir cwd end |
#run_if_exists(file) ⇒ Object
run configuration.rb file only if it exists
326 327 328 329 330 |
# File 'lib/central.rb', line 326 def run_if_exists(file) if file_exists?(file) run file end end |
#source(file, source) ⇒ Object
source file in sh/bash/zsh script
241 242 243 244 245 246 247 248 249 250 |
# File 'lib/central.rb', line 241 def source(file,source) file = abs(file) source = abs(source) source_line = "source \"#{source}\"" out = sudo("grep -Fx '#{source_line}' \"#{file}\"") if out == "" sudo("echo '#{source_line}' >> \"#{file}\"") puts "Added source #{source} line to #{file}" end end |
#sudo(command, sudo = false) ⇒ Object
run command second time with sudo privileges if it previously failed because of insufficient permissions
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 |
# File 'lib/central.rb', line 28 def sudo(command,sudo=false) if sudo sudo = 'sudo ' else sudo = '' end command = sudo+command out = `#{command} 2>&1` # removing line feed if out.length > 0 && out[-1].ord == 10 out = out[0...-1] end # removing cariage return if out.length > 0 && out[-1].ord == 13 out = out[0...-1] end if out.downcase.end_with?('permission denied') if sudo STDERR.puts "Couldn't execute #{command} due to permission denied\nrun central with sudo privileges" exit 1 else out = sudo(command,true) end end return out end |
#symlink(from, to) ⇒ Object
symlink path
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/central.rb', line 169 def symlink(from,to) from = abs(from) to = abs(to) if symlink?(from) if symlink_path(from) != to rm from symlink from, to end elsif file_exists?(from) STDERR.puts "File #{from} exists in place of symlink..." exit 1 elsif dir_exists?(from) STDERR.puts "Directory #{from} exists in place of symlink..." exit 1 else out = sudo("ln -s \"#{to}\" \"#{from}\"") puts "Created symlink: #{from} → #{to}" end end |
#symlink?(symlink) ⇒ Boolean
check if file is symlink
105 106 107 |
# File 'lib/central.rb', line 105 def symlink?(symlink) File.symlink?(abs(symlink)) end |
#symlink_path(symlink) ⇒ Object
get full path of symlink
110 111 112 |
# File 'lib/central.rb', line 110 def symlink_path(symlink) sudo("readlink \"#{abs(symlink)}\"") end |
#touch(path) ⇒ Object
touch file
149 150 151 152 153 154 155 |
# File 'lib/central.rb', line 149 def touch(path) path = abs(path) unless file_exists?(path) out = sudo("touch \"#{path}\"") puts "Touched file: #{path}" end end |
#write(file, content) ⇒ Object
write content into a file
235 236 237 238 |
# File 'lib/central.rb', line 235 def write(file,content) file = abs(file) File.write(file,content) end |