Class: Anywhere::Base
- Inherits:
-
Object
- Object
- Anywhere::Base
- Defined in:
- lib/anywhere/base.rb
Instance Attribute Summary collapse
Instance Method Summary collapse
- #add_system_user(login) ⇒ Object
- #capture(path, compressed = false) ⇒ Object
- #do_execute(*args) ⇒ Object
- #execute(cmd, stdin = nil, stdout = nil) ⇒ Object
- #extract_tar(path, dst) ⇒ Object
- #file_exists?(path) ⇒ Boolean
- #home_dir ⇒ Object
- #md5sum(path) ⇒ Object
- #mkdir_p(path) ⇒ Object
- #root? ⇒ Boolean
- #run_as(user, cmd) ⇒ Object
- #sudo_cmd ⇒ Object
- #uncompress(string) ⇒ Object
- #user_exists?(login) ⇒ Boolean
- #whoami ⇒ Object
- #write_file(path, content, attributes = {}) ⇒ Object
Instance Attribute Details
Instance Method Details
#add_system_user(login) ⇒ Object
81 82 83 84 85 |
# File 'lib/anywhere/base.rb', line 81 def add_system_user(login) raise "user #{login} already exists" if user_exists?(login) logger.info "adding system user #{login}" execute!("#{sudo_cmd} adduser --system #{login}") end |
#capture(path, compressed = false) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/anywhere/base.rb', line 16 def capture(path, compressed = false) io = StringIO.new cmd = "cat #{path}" cmd << " | gzip " if compressed execute(cmd, nil, io) io.rewind out = io.read if compressed # not sure why this is not working with the uncompress method out, s = Open3.capture2("cat - | gunzip", stdin_data: out) out else out end end |
#do_execute(*args) ⇒ Object
58 59 60 |
# File 'lib/anywhere/base.rb', line 58 def do_execute(*args) raise "implement me in subclass" end |
#execute(cmd, stdin = nil, stdout = nil) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/anywhere/base.rb', line 42 def execute(cmd, stdin = nil, stdout = nil) do_execute(cmd, stdin, stdout) do |stream, data| data.split("\n").each do |line| if stream == :stderr logger.error line elsif !stdout logger.debug line end end end end |
#extract_tar(path, dst) ⇒ Object
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/anywhere/base.rb', line 130 def extract_tar(path, dst) ms = Benchmark.measure do data = File.open(path, "rb") do |f| f.read end logger.info "writing #{data.length} bytes" execute(%(mkdir -p #{dst} && cd #{dst} && tar xfz -), data) end logger.info "extracted archive in %.3f" % [ms.real] end |
#file_exists?(path) ⇒ Boolean
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/anywhere/base.rb', line 62 def file_exists?(path) execute("test -e #{path}") true rescue Anywhere::ExecutionError => err if err.result.exit_status == 1 false else raise end end |
#home_dir ⇒ Object
146 147 148 |
# File 'lib/anywhere/base.rb', line 146 def home_dir execute("env | grep HOME | cut -d = -f 2").stdout.strip end |
#md5sum(path) ⇒ Object
73 74 75 |
# File 'lib/anywhere/base.rb', line 73 def md5sum(path) execute("md5sum #{path} | awk '{ print $1 }'").stdout.strip end |
#mkdir_p(path) ⇒ Object
141 142 143 144 |
# File 'lib/anywhere/base.rb', line 141 def mkdir_p(path) logger.info "creating directory #{path}" execute("mkdir -p #{path}") end |
#root? ⇒ Boolean
12 13 14 |
# File 'lib/anywhere/base.rb', line 12 def root? whoami == "root" end |
#run_as(user, cmd) ⇒ Object
87 88 89 |
# File 'lib/anywhere/base.rb', line 87 def run_as(user, cmd) execute("sudo -- sudo -u #{user} -- #{cmd}") end |
#sudo_cmd ⇒ Object
77 78 79 |
# File 'lib/anywhere/base.rb', line 77 def sudo_cmd @sudo_cmd ||= root? ? "" : "sudo" end |
#uncompress(string) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/anywhere/base.rb', line 32 def uncompress(string) require "zlib" require "base64" zstream = Zlib::Inflate.new buf = zstream.inflate(string) zstream.finish zstream.close buf end |
#user_exists?(login) ⇒ Boolean
91 92 93 |
# File 'lib/anywhere/base.rb', line 91 def user_exists?(login) execute("id #{login} 2>/dev/null").success? end |
#whoami ⇒ Object
54 55 56 |
# File 'lib/anywhere/base.rb', line 54 def whoami @whoami ||= execute("whoami").stdout.strip end |
#write_file(path, content, attributes = {}) ⇒ Object
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 |
# File 'lib/anywhere/base.rb', line 95 def write_file(path, content, attributes = {}) md5 = Digest::MD5.hexdigest(content).to_s if file_exists?(path) logger.debug "file #{path} already exists" file_md5 = md5sum(path) if file_md5 == md5 logger.info "file #{path} did not change => not writing" return :not_changed end end logger.info "writing #{content.length} bytes to #{path} (md5: #{md5})" tmp_path = "/tmp/anywhere/files.#{md5}" execute("#{sudo_cmd} mkdir -p #{File.dirname(tmp_path)}") execute("#{sudo_cmd} chown #{whoami} #{File.dirname(tmp_path)}") logger.debug "writing to #{tmp_path}" execute(%(rm -f #{tmp_path}; cat - > #{tmp_path}), content) if mode = attributes[:mode] logger.info "changing mode to #{mode}" execute("chmod #{mode} #{tmp_path}") end if owner = attributes[:owner] logger.info "changing owner to #{owner}" execute("#{sudo_cmd} chown #{owner} #{tmp_path}") end execute("#{sudo_cmd} mkdir -p #{File.dirname(path)}") logger.debug "moving #{tmp_path} to #{path}" if file_exists?(path) logger.debug "diff #{path} #{tmp_path}" execute("diff #{tmp_path} #{path}; /bin/true").stdout.split("\n").each do |line| logger.debug line end end execute("#{sudo_cmd} mv #{tmp_path} #{path}") end |