module Luban::Deployment::Helpers::Utils

Constants

LogLevels

Attributes

backend[R]

Public Instance Methods

assure(type, *args) { || ... } click to toggle source
# 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) click to toggle source
# File lib/luban/deployment/helpers/utils.rb, line 45
def assure_dirs(*dirs)
  dirs.each { |dir| assure(:directory, dir) { mkdir(dir) } }
end
capture(*args, &blk) click to toggle source
# File lib/luban/deployment/helpers/utils.rb, line 183
def capture(*args, &blk)
  backend.capture(*args, raise_on_non_zero_exit: false, &blk).chomp
end
check_pass?(type, *args) click to toggle source
# File lib/luban/deployment/helpers/utils.rb, line 9
def check_pass?(type, *args)
  send("#{type}?", *args)
end
chmod(*opts, path) click to toggle source
# File lib/luban/deployment/helpers/utils.rb, line 75
def chmod(*opts, path)
  execute(:chmod, '-R', *opts, path)
end
cleanup_files(path, keep_copies: 1) click to toggle source
# File lib/luban/deployment/helpers/utils.rb, line 107
def cleanup_files(path, keep_copies: 1)
  files = capture(:ls, '-xtd', path).split(" ")
  if files.count > keep_copies
    files.last(files.count - keep_copies).each { |f| execute(:rm, '-fr', f) }
  end
end
cp(*opts, source_path, target_path) click to toggle source
# 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) click to toggle source
# File lib/luban/deployment/helpers/utils.rb, line 17
def directory?(path)
  test "[ -d #{path} ]"
end
exists?(path) click to toggle source
# File lib/luban/deployment/helpers/utils.rb, line 13
def exists?(path)
  test "[ -e #{path} ]"
end
file?(path, test_op = "-f") click to toggle source
# File lib/luban/deployment/helpers/utils.rb, line 21
def file?(path, test_op = "-f")
  test "[ #{test_op} #{path} ]"
end
hardware_name() click to toggle source
# File lib/luban/deployment/helpers/utils.rb, line 126
def hardware_name
  @hardware_name ||= capture("uname -m")
end
host() click to toggle source
# File lib/luban/deployment/helpers/utils.rb, line 204
def host
  @host ||= backend.host
end
hostname() click to toggle source
# File lib/luban/deployment/helpers/utils.rb, line 208
def hostname
  @hostname ||= host.hostname
end
ln(*opts, source_path, target_path) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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 | $(type -p md5sum md5 | head -1)")[/^\h+/]
end
md5_for_file(file) click to toggle source
# File lib/luban/deployment/helpers/utils.rb, line 99
def md5_for_file(file)
  capture(:cat, "#{file} 2>/dev/null | $(type -p md5sum md5 | head -1)")[/^\h+/]
end
md5_matched?(path, md5) click to toggle source
# File lib/luban/deployment/helpers/utils.rb, line 187
def md5_matched?(path, md5)
  file?(path) ? md5 == md5_for_file(path) : md5 == md5_for_dir(path)
end
mkdir(*opts, path) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/luban/deployment/helpers/utils.rb, line 212
def now
  Time.now().strftime("%d/%m/%Y %H:%M:%S")
end
os_name() click to toggle source
# File lib/luban/deployment/helpers/utils.rb, line 118
def os_name
  @os_name ||= capture("uname -s")
end
os_release() click to toggle source
# File lib/luban/deployment/helpers/utils.rb, line 122
def os_release
  @os_release ||= capture("uname -r")
end
render_template(template_file, context: binding) click to toggle source
# File lib/luban/deployment/helpers/utils.rb, line 166
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) click to toggle source
# File lib/luban/deployment/helpers/utils.rb, line 172
def revision_match?(file_to_upload, revision)
  file?(file_to_upload) and match?("grep \"Revision: \" #{file_to_upload}; true", revision)
end
rm(*opts, path) click to toggle source
# File lib/luban/deployment/helpers/utils.rb, line 67
def rm(*opts, path)
  execute(:rm, '-f', *opts, path)
end
rmdir(*opts, path) click to toggle source
# File lib/luban/deployment/helpers/utils.rb, line 71
def rmdir(*opts, path)
  execute(:rm, '-fr', *opts, path)
end
sudo(*args) click to toggle source
# File lib/luban/deployment/helpers/utils.rb, line 114
def sudo(*args)
  execute(:sudo, *args)
end
touch(path) click to toggle source
# File lib/luban/deployment/helpers/utils.rb, line 63
def touch(path)
  execute(:touch, path)
end
truncate(path) click to toggle source
# 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| ... } click to toggle source
# File lib/luban/deployment/helpers/utils.rb, line 145
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) click to toggle source
# File lib/luban/deployment/helpers/utils.rb, line 134
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() click to toggle source
# File lib/luban/deployment/helpers/utils.rb, line 130
def user_home
  @user_home ||= capture("eval echo ~")
end
version_match?(version, version_requirement) click to toggle source
# File lib/luban/deployment/helpers/utils.rb, line 221
def version_match?(version, version_requirement)
  with_clean_env do
    Gem::Requirement.new(version_requirement).satisfied_by?(Gem::Version.new(version))
  end
end
with_clean_env() { || ... } click to toggle source
# File lib/luban/deployment/helpers/utils.rb, line 216
def with_clean_env
  return unless block_given?
  defined?(Bundler) ? Bundler.with_clean_env { yield } : yield
end