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
-
#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_file(file) ⇒ Object
-
#md5_matched?(file_path, md5) ⇒ Boolean
-
#method_missing(sym, *args, &blk) ⇒ Object
-
#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
-
#upload_by_template(file_to_upload:, template_file:, header_file: find_template_file('header.erb'), auto_revision: false, **opts) {|file_to_upload| ... } ⇒ Object
-
#url_exists?(url) ⇒ Boolean
-
#user_home ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &blk) ⇒ Object
189
190
191
|
# File 'lib/luban/deployment/helpers/utils.rb', line 189
def method_missing(sym, *args, &blk)
backend.respond_to?(sym) ? backend.send(sym, *args, &blk) : super
end
|
Instance Attribute Details
#backend ⇒ Object
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
156
157
158
|
# File 'lib/luban/deployment/helpers/utils.rb', line 156
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
63
64
65
|
# File 'lib/luban/deployment/helpers/utils.rb', line 63
def chmod(*opts, path)
execute(:chmod, '-R', *opts, path)
end
|
#cp(*opts, source_path, target_path) ⇒ Object
75
76
77
|
# File 'lib/luban/deployment/helpers/utils.rb', line 75
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
99
100
101
|
# File 'lib/luban/deployment/helpers/utils.rb', line 99
def hardware_name
@hardware_name ||= capture("uname -m")
end
|
#host ⇒ Object
177
178
179
|
# File 'lib/luban/deployment/helpers/utils.rb', line 177
def host
@host ||= backend.host
end
|
#hostname ⇒ Object
181
182
183
|
# File 'lib/luban/deployment/helpers/utils.rb', line 181
def hostname
@hostname ||= host.hostname
end
|
#ln(*opts, source_path, target_path) ⇒ Object
67
68
69
|
# File 'lib/luban/deployment/helpers/utils.rb', line 67
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_file(file) ⇒ Object
83
84
85
|
# File 'lib/luban/deployment/helpers/utils.rb', line 83
def md5_for_file(file)
capture(:cat, "#{file} 2>/dev/null | openssl md5")[/\h+$/]
end
|
#md5_matched?(file_path, md5) ⇒ Boolean
160
161
162
|
# File 'lib/luban/deployment/helpers/utils.rb', line 160
def md5_matched?(file_path, md5)
file?(file_path) and md5 == md5_for_file(file_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
71
72
73
|
# File 'lib/luban/deployment/helpers/utils.rb', line 71
def mv(*opts, source_path, target_path)
execute(:mv, *opts, source_path, target_path)
end
|
#now ⇒ Object
185
186
187
|
# File 'lib/luban/deployment/helpers/utils.rb', line 185
def now
Time.now().strftime("%d/%m/%Y %H:%M:%S")
end
|
#os_name ⇒ Object
91
92
93
|
# File 'lib/luban/deployment/helpers/utils.rb', line 91
def os_name
@os_name ||= capture("uname -s")
end
|
#os_release ⇒ Object
95
96
97
|
# File 'lib/luban/deployment/helpers/utils.rb', line 95
def os_release
@os_release ||= capture("uname -r")
end
|
#readlink(source_file) ⇒ Object
79
80
81
|
# File 'lib/luban/deployment/helpers/utils.rb', line 79
def readlink(source_file)
capture("$(type -p readlink greadlink|head -1) #{source_file}")
end
|
#render_template(template_file, context: binding) ⇒ Object
139
140
141
142
143
|
# File 'lib/luban/deployment/helpers/utils.rb', line 139
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
145
146
147
|
# File 'lib/luban/deployment/helpers/utils.rb', line 145
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
55
56
57
|
# File 'lib/luban/deployment/helpers/utils.rb', line 55
def rm(*opts, path)
execute(:rm, '-f', *opts, path)
end
|
#rmdir(*opts, path) ⇒ Object
59
60
61
|
# File 'lib/luban/deployment/helpers/utils.rb', line 59
def rmdir(*opts, path)
execute(:rm, '-fr', *opts, path)
end
|
#sudo(*args) ⇒ Object
87
88
89
|
# File 'lib/luban/deployment/helpers/utils.rb', line 87
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
|
#upload_by_template(file_to_upload:, template_file:, header_file: find_template_file('header.erb'), auto_revision: false, **opts) {|file_to_upload| ... } ⇒ Object
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/luban/deployment/helpers/utils.rb', line 121
def upload_by_template(file_to_upload:, template_file:,
header_file: find_template_file('header.erb'),
auto_revision: false, **opts)
content = render_template(template_file, context: binding)
revision = ''
if auto_revision
require 'digest/md5'
revision = Digest::MD5.hexdigest(content)
return if revision_match?(file_to_upload, revision)
end
= render_template(, context: binding)
upload!(StringIO.new( + content), file_to_upload)
yield file_to_upload if block_given?
end
|
#url_exists?(url) ⇒ Boolean
107
108
109
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/luban/deployment/helpers/utils.rb', line 107
def url_exists?(url)
test("curl -s -L -I -o /dev/null -f #{url}")
end
|
#user_home ⇒ Object
103
104
105
|
# File 'lib/luban/deployment/helpers/utils.rb', line 103
def user_home
@user_home ||= capture("eval echo ~")
end
|