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

Instance Method Summary collapse

Instance Attribute Details

#backendObject (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


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

Returns:

  • (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

Returns:

  • (Boolean)


17
18
19
# File 'lib/luban/deployment/helpers/utils.rb', line 17

def directory?(path)
  test "[ -d #{path} ]"
end

#exists?(path) ⇒ Boolean

Returns:

  • (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

Returns:

  • (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_nameObject



119
120
121
# File 'lib/luban/deployment/helpers/utils.rb', line 119

def hardware_name
  @hardware_name ||= capture("uname -m")
end

#hostObject



197
198
199
# File 'lib/luban/deployment/helpers/utils.rb', line 197

def host
  @host ||= backend.host
end

#hostnameObject



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

Returns:

  • (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

Returns:

  • (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

#nowObject



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_nameObject



111
112
113
# File 'lib/luban/deployment/helpers/utils.rb', line 111

def os_name
  @os_name ||= capture("uname -s")
end

#os_releaseObject



115
116
117
# File 'lib/luban/deployment/helpers/utils.rb', line 115

def os_release
  @os_release ||= capture("uname -r")
end


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

Returns:

  • (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

Returns:

  • (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

Yields:

  • (file_to_upload)


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)
  footer = footer_file.nil? ? '' : render_template(footer_file, context: binding)

  upload!(StringIO.new(header + content + footer), file_to_upload)
  yield file_to_upload if block_given?
  true
end

#url_exists?(url) ⇒ Boolean

Returns:

  • (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_homeObject



123
124
125
# File 'lib/luban/deployment/helpers/utils.rb', line 123

def user_home
  @user_home ||= capture("eval echo ~")
end