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
- #exists?(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
35 36 37 38 39 40 41 42 43 |
# File 'lib/luban/deployment/helpers/utils.rb', line 35 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
45 46 47 |
# File 'lib/luban/deployment/helpers/utils.rb', line 45 def assure_dirs(*dirs) dirs.each { |dir| assure(:directory, dir) { mkdir(dir) } } end |
#assure_symlink(source_path, target_path) ⇒ Object
49 50 51 52 53 |
# File 'lib/luban/deployment/helpers/utils.rb', line 49 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
176 177 178 |
# File 'lib/luban/deployment/helpers/utils.rb', line 176 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
75 76 77 |
# File 'lib/luban/deployment/helpers/utils.rb', line 75 def chmod(*opts, path) execute(:chmod, '-R', *opts, path) end |
#cp(*opts, source_path, target_path) ⇒ Object
87 88 89 |
# File 'lib/luban/deployment/helpers/utils.rb', line 87 def cp(*opts, source_path, target_path) execute(:cp, *opts, source_path, target_path) end |
#directory?(path) ⇒ Boolean
17 18 19 |
# File 'lib/luban/deployment/helpers/utils.rb', line 17 def directory?(path) test "[ -d #{path} ]" end |
#exists?(path) ⇒ Boolean
13 14 15 |
# File 'lib/luban/deployment/helpers/utils.rb', line 13 def exists?(path) test "[ -e #{path} ]" end |
#file?(path, test_op = "-f") ⇒ Boolean
21 22 23 |
# File 'lib/luban/deployment/helpers/utils.rb', line 21 def file?(path, test_op = "-f") test "[ #{test_op} #{path} ]" end |
#hardware_name ⇒ Object
119 120 121 |
# File 'lib/luban/deployment/helpers/utils.rb', line 119 def hardware_name @hardware_name ||= capture("uname -m") end |
#host ⇒ Object
197 198 199 |
# File 'lib/luban/deployment/helpers/utils.rb', line 197 def host @host ||= backend.host end |
#hostname ⇒ Object
201 202 203 |
# File 'lib/luban/deployment/helpers/utils.rb', line 201 def hostname @hostname ||= host.hostname end |
#ln(*opts, source_path, target_path) ⇒ Object
79 80 81 |
# File 'lib/luban/deployment/helpers/utils.rb', line 79 def ln(*opts, source_path, target_path) execute(:ln, '-nfs', *opts, source_path, target_path) end |
#match?(cmd, expect) ⇒ Boolean
29 30 31 32 33 |
# File 'lib/luban/deployment/helpers/utils.rb', line 29 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
95 96 97 |
# File 'lib/luban/deployment/helpers/utils.rb', line 95 def md5_for(path) file?(path) ? md5_for_file(path) : md5_for_dir(path) end |
#md5_for_dir(dir) ⇒ Object
103 104 105 |
# File 'lib/luban/deployment/helpers/utils.rb', line 103 def md5_for_dir(dir) capture(:find, "#{dir} -type f ! -name '*.md5' 2>/dev/null | LC_ALL=C sort -u | xargs cat | openssl md5")[/\h+$/] end |
#md5_for_file(file) ⇒ Object
99 100 101 |
# File 'lib/luban/deployment/helpers/utils.rb', line 99 def md5_for_file(file) capture(:cat, "#{file} 2>/dev/null | openssl md5")[/\h+$/] end |
#md5_matched?(path, md5) ⇒ Boolean
180 181 182 |
# File 'lib/luban/deployment/helpers/utils.rb', line 180 def md5_matched?(path, md5) file?(path) ? md5 == md5_for_file(path) : md5 == md5_for_dir(path) end |
#mkdir(*opts, path) ⇒ Object
55 56 57 |
# File 'lib/luban/deployment/helpers/utils.rb', line 55 def mkdir(*opts, path) execute(:mkdir, '-p', *opts, path) end |
#mv(*opts, source_path, target_path) ⇒ Object
83 84 85 |
# File 'lib/luban/deployment/helpers/utils.rb', line 83 def mv(*opts, source_path, target_path) execute(:mv, *opts, source_path, target_path) end |
#now ⇒ Object
205 206 207 |
# File 'lib/luban/deployment/helpers/utils.rb', line 205 def now Time.now().strftime("%d/%m/%Y %H:%M:%S") end |
#os_name ⇒ Object
111 112 113 |
# File 'lib/luban/deployment/helpers/utils.rb', line 111 def os_name @os_name ||= capture("uname -s") end |
#os_release ⇒ Object
115 116 117 |
# File 'lib/luban/deployment/helpers/utils.rb', line 115 def os_release @os_release ||= capture("uname -r") end |
#readlink(source_file) ⇒ Object
91 92 93 |
# File 'lib/luban/deployment/helpers/utils.rb', line 91 def readlink(source_file) capture("$(type -p readlink greadlink|head -1) #{source_file}") end |
#render_template(template_file, context: binding) ⇒ Object
159 160 161 162 163 |
# File 'lib/luban/deployment/helpers/utils.rb', line 159 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
165 166 167 |
# File 'lib/luban/deployment/helpers/utils.rb', line 165 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
67 68 69 |
# File 'lib/luban/deployment/helpers/utils.rb', line 67 def rm(*opts, path) execute(:rm, '-f', *opts, path) end |
#rmdir(*opts, path) ⇒ Object
71 72 73 |
# File 'lib/luban/deployment/helpers/utils.rb', line 71 def rmdir(*opts, path) execute(:rm, '-fr', *opts, path) end |
#sudo(*args) ⇒ Object
107 108 109 |
# File 'lib/luban/deployment/helpers/utils.rb', line 107 def sudo(*args) execute(:sudo, *args) end |
#symlink?(path) ⇒ Boolean
25 26 27 |
# File 'lib/luban/deployment/helpers/utils.rb', line 25 def symlink?(path) test "[ -L #{path} ]" end |
#touch(path) ⇒ Object
63 64 65 |
# File 'lib/luban/deployment/helpers/utils.rb', line 63 def touch(path) execute(:touch, path) end |
#truncate(path) ⇒ Object
59 60 61 |
# File 'lib/luban/deployment/helpers/utils.rb', line 59 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
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/luban/deployment/helpers/utils.rb', line 138 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
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/luban/deployment/helpers/utils.rb', line 127 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
123 124 125 |
# File 'lib/luban/deployment/helpers/utils.rb', line 123 def user_home @user_home ||= capture("eval echo ~") end |