Module: Luban::Deployment::Helpers::Utils
- Included in:
- Worker::Base
- Defined in:
- lib/luban/deployment/helpers/utils.rb
Constant Summary collapse
- LogLevels =
i(fatal error warn info debug trace)
Instance Attribute Summary collapse
-
#backend ⇒ Object
readonly
Returns the value of attribute backend.
Instance Method Summary collapse
- #assure(type, *args) ⇒ Object
- #assure_dirs(*dirs) ⇒ Object
- #assure_symlink(source_path, target_path) ⇒ Object
- #capture(*args, &blk) ⇒ Object
- #check_pass?(type, *args) ⇒ Boolean
- #chmod(*opts, path) ⇒ Object
- #cp(*opts, source_path, target_path) ⇒ Object
- #directory?(path) ⇒ Boolean
- #file?(path, test_op = "-f") ⇒ Boolean
- #hardware_name ⇒ Object
- #host ⇒ Object
- #hostname ⇒ Object
- #ln(*opts, source_path, target_path) ⇒ Object
- #match?(cmd, expect) ⇒ Boolean
- #md5_for(path) ⇒ Object
- #md5_for_dir(dir) ⇒ Object
- #md5_for_file(file) ⇒ Object
- #md5_matched?(path, md5) ⇒ Boolean
- #mkdir(*opts, path) ⇒ Object
- #mv(*opts, source_path, target_path) ⇒ Object
- #now ⇒ Object
- #os_name ⇒ Object
- #os_release ⇒ Object
- #readlink(source_file) ⇒ Object
- #render_template(template_file, context: binding) ⇒ Object
- #revision_match?(file_to_upload, revision) ⇒ Boolean
- #rm(*opts, path) ⇒ Object
- #rmdir(*opts, path) ⇒ Object
- #sudo(*args) ⇒ Object
- #symlink?(path) ⇒ Boolean
- #touch(path) ⇒ Object
- #truncate(path) ⇒ Object
- #upload_by_template(file_to_upload:, template_file:, header_file: find_template_file('header.erb'), footer_file: nil, auto_revision: false, **opts) {|file_to_upload| ... } ⇒ Object
- #url_exists?(url) ⇒ Boolean
- #user_home ⇒ Object
Instance Attribute Details
#backend ⇒ Object (readonly)
Returns the value of attribute backend.
7 8 9 |
# File 'lib/luban/deployment/helpers/utils.rb', line 7 def backend @backend end |
Instance Method Details
#assure(type, *args) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/luban/deployment/helpers/utils.rb', line 31 def assure(type, *args) unless check_pass?(type, *args) if block_given? yield else abort "Aborted! #{type} dependency with #{args.inspect} are not met and no block is given to resolve it." end end end |
#assure_dirs(*dirs) ⇒ Object
41 42 43 |
# File 'lib/luban/deployment/helpers/utils.rb', line 41 def assure_dirs(*dirs) dirs.each { |dir| assure(:directory, dir) { mkdir(dir) } } end |
#assure_symlink(source_path, target_path) ⇒ Object
45 46 47 48 49 |
# File 'lib/luban/deployment/helpers/utils.rb', line 45 def assure_symlink(source_path, target_path) unless symlink?(target_path) and readlink(target_path) == source_path.to_s ln(source_path, target_path) end end |
#capture(*args, &blk) ⇒ Object
173 174 175 |
# File 'lib/luban/deployment/helpers/utils.rb', line 173 def capture(*args, &blk) backend.capture(*args, raise_on_non_zero_exit: false, &blk).chomp end |
#check_pass?(type, *args) ⇒ Boolean
9 10 11 |
# File 'lib/luban/deployment/helpers/utils.rb', line 9 def check_pass?(type, *args) send("#{type}?", *args) end |
#chmod(*opts, path) ⇒ Object
71 72 73 |
# File 'lib/luban/deployment/helpers/utils.rb', line 71 def chmod(*opts, path) execute(:chmod, '-R', *opts, path) end |
#cp(*opts, source_path, target_path) ⇒ Object
83 84 85 |
# File 'lib/luban/deployment/helpers/utils.rb', line 83 def cp(*opts, source_path, target_path) execute(:cp, *opts, source_path, target_path) end |
#directory?(path) ⇒ Boolean
13 14 15 |
# File 'lib/luban/deployment/helpers/utils.rb', line 13 def directory?(path) test "[ -d #{path} ]" end |
#file?(path, test_op = "-f") ⇒ Boolean
17 18 19 |
# File 'lib/luban/deployment/helpers/utils.rb', line 17 def file?(path, test_op = "-f") test "[ #{test_op} #{path} ]" end |
#hardware_name ⇒ Object
116 117 118 |
# File 'lib/luban/deployment/helpers/utils.rb', line 116 def hardware_name @hardware_name ||= capture("uname -m") end |
#host ⇒ Object
194 195 196 |
# File 'lib/luban/deployment/helpers/utils.rb', line 194 def host @host ||= backend.host end |
#hostname ⇒ Object
198 199 200 |
# File 'lib/luban/deployment/helpers/utils.rb', line 198 def hostname @hostname ||= host.hostname end |
#ln(*opts, source_path, target_path) ⇒ Object
75 76 77 |
# File 'lib/luban/deployment/helpers/utils.rb', line 75 def ln(*opts, source_path, target_path) execute(:ln, '-nfs', *opts, source_path, target_path) end |
#match?(cmd, expect) ⇒ Boolean
25 26 27 28 29 |
# File 'lib/luban/deployment/helpers/utils.rb', line 25 def match?(cmd, expect) expect = Regexp.new(Regexp.escape(expect.to_s)) unless expect.is_a?(Regexp) output = capture(cmd) output =~ expect end |
#md5_for(path) ⇒ Object
91 92 93 |
# File 'lib/luban/deployment/helpers/utils.rb', line 91 def md5_for(path) file?(path) ? md5_for_file(path) : md5_for_dir(path) end |
#md5_for_dir(dir) ⇒ Object
99 100 101 102 |
# File 'lib/luban/deployment/helpers/utils.rb', line 99 def md5_for_dir(dir) #capture(:tar, "-cf - #{dir} 2>/dev/null | openssl md5")[/\h+$/] capture(:find, "#{dir} -type f 2>/dev/null | LC_ALL=C sort -u | xargs cat | openssl md5")[/\h+$/] end |
#md5_for_file(file) ⇒ Object
95 96 97 |
# File 'lib/luban/deployment/helpers/utils.rb', line 95 def md5_for_file(file) capture(:cat, "#{file} 2>/dev/null | openssl md5")[/\h+$/] end |
#md5_matched?(path, md5) ⇒ Boolean
177 178 179 |
# File 'lib/luban/deployment/helpers/utils.rb', line 177 def md5_matched?(path, md5) file?(path) ? md5 == md5_for_file(path) : md5 == md5_for_dir(path) end |
#mkdir(*opts, path) ⇒ Object
51 52 53 |
# File 'lib/luban/deployment/helpers/utils.rb', line 51 def mkdir(*opts, path) execute(:mkdir, '-p', *opts, path) end |
#mv(*opts, source_path, target_path) ⇒ Object
79 80 81 |
# File 'lib/luban/deployment/helpers/utils.rb', line 79 def mv(*opts, source_path, target_path) execute(:mv, *opts, source_path, target_path) end |
#now ⇒ Object
202 203 204 |
# File 'lib/luban/deployment/helpers/utils.rb', line 202 def now Time.now().strftime("%d/%m/%Y %H:%M:%S") end |
#os_name ⇒ Object
108 109 110 |
# File 'lib/luban/deployment/helpers/utils.rb', line 108 def os_name @os_name ||= capture("uname -s") end |
#os_release ⇒ Object
112 113 114 |
# File 'lib/luban/deployment/helpers/utils.rb', line 112 def os_release @os_release ||= capture("uname -r") end |
#readlink(source_file) ⇒ Object
87 88 89 |
# File 'lib/luban/deployment/helpers/utils.rb', line 87 def readlink(source_file) capture("$(type -p readlink greadlink|head -1) #{source_file}") end |
#render_template(template_file, context: binding) ⇒ Object
156 157 158 159 160 |
# File 'lib/luban/deployment/helpers/utils.rb', line 156 def render_template(template_file, context: binding) require 'erb' template = File.read(template_file) ERB.new(template, nil, '-').result(context) end |
#revision_match?(file_to_upload, revision) ⇒ Boolean
162 163 164 |
# File 'lib/luban/deployment/helpers/utils.rb', line 162 def revision_match?(file_to_upload, revision) file?(file_to_upload) and match?("grep \"Revision: \" #{file_to_upload}; true", revision) end |
#rm(*opts, path) ⇒ Object
63 64 65 |
# File 'lib/luban/deployment/helpers/utils.rb', line 63 def rm(*opts, path) execute(:rm, '-f', *opts, path) end |
#rmdir(*opts, path) ⇒ Object
67 68 69 |
# File 'lib/luban/deployment/helpers/utils.rb', line 67 def rmdir(*opts, path) execute(:rm, '-fr', *opts, path) end |
#sudo(*args) ⇒ Object
104 105 106 |
# File 'lib/luban/deployment/helpers/utils.rb', line 104 def sudo(*args) execute(:sudo, *args) end |
#symlink?(path) ⇒ Boolean
21 22 23 |
# File 'lib/luban/deployment/helpers/utils.rb', line 21 def symlink?(path) test "[ -L #{path} ]" end |
#touch(path) ⇒ Object
59 60 61 |
# File 'lib/luban/deployment/helpers/utils.rb', line 59 def touch(path) execute(:touch, path) end |
#truncate(path) ⇒ Object
55 56 57 |
# File 'lib/luban/deployment/helpers/utils.rb', line 55 def truncate(path) execute(:cat, "/dev/null", ">", path) end |
#upload_by_template(file_to_upload:, template_file:, header_file: find_template_file('header.erb'), footer_file: nil, auto_revision: false, **opts) {|file_to_upload| ... } ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/luban/deployment/helpers/utils.rb', line 135 def upload_by_template(file_to_upload:, template_file:, header_file: find_template_file('header.erb'), footer_file: nil, auto_revision: false, **opts) content = render_template(template_file, context: binding) revision = '' if auto_revision require 'digest/md5' revision = Digest::MD5.hexdigest(content) return false if revision_match?(file_to_upload, revision) end header = header_file.nil? ? '' : render_template(header_file, context: binding) = .nil? ? '' : render_template(, context: binding) upload!(StringIO.new(header + content + ), file_to_upload) yield file_to_upload if block_given? true end |
#url_exists?(url) ⇒ Boolean
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/luban/deployment/helpers/utils.rb', line 124 def url_exists?(url) # Sent HEAD request to avoid downloading the file contents test("curl -s -L -I -o /dev/null -f #{url}") or # In case HEAD request is refused, # only the first byte of the file is requested test("curl -s -L -o /dev/null -f -r 0-0 #{url}") # Alternatively, http code (200) can be validated # capture("curl -s -L -I -o /dev/null -w '%{http_code}' #{url}") == '200' end |
#user_home ⇒ Object
120 121 122 |
# File 'lib/luban/deployment/helpers/utils.rb', line 120 def user_home @user_home ||= capture("eval echo ~") end |