class Gleis::Utils

This class gathers various utilities as small methods

Public Class Methods

add_remote_to_git_config(remote_url) click to toggle source
# File lib/gleis/utils.rb, line 50
def self.add_remote_to_git_config(remote_url)
  config_file = File.join(Dir.pwd, '.git', 'config')
  return false unless File.exist?(config_file)

  # Check if gleis remote already exists
  git_remote_line = '[remote "gleis"]'
  return if line_exists_in_file(config_file, git_remote_line)

  f = File.open(config_file, 'a')
  f.puts "\n#{git_remote_line}"
  f.puts "\turl = #{remote_url}"
  f.puts "\tfetch = +refs/heads/*:refs/remotes/origin/*"
  f.close
end
app_name() click to toggle source
# File lib/gleis/utils.rb, line 29
def self.app_name
  Dir.pwd.split('/').last
end
check_for_local_pg_command(command) click to toggle source
# File lib/gleis/utils.rb, line 33
def self.check_for_local_pg_command(command)
  abort("The PostgreSQL client required to run the command #{command}) is not installed on this system.") unless
    which(command)
end
convert_username_to_filename(username) click to toggle source
# File lib/gleis/utils.rb, line 6
def self.convert_username_to_filename(username)
  username.sub('@', '_at_').tr('.', '_')
end
generate_exec_env_param(env_vars) click to toggle source
# File lib/gleis/utils.rb, line 89
def self.generate_exec_env_param(env_vars)
  env_vars_param = ''
  env_vars.each do |key, value|
    env_vars_param << " -e #{key}=#{value}"
  end
  env_vars_param
end
generate_exec_mount_param(body, app_name) click to toggle source
# File lib/gleis/utils.rb, line 97
def self.generate_exec_mount_param(body, app_name)
  mount_param = ''
  if body['success'] == 1
    if body['data']['storage'].any?
      storage = body['data']['storage'].pop
      storage_type = get_storage_type_by_name(body['data']['storage_types'], storage['storage_type_name'])
      if storage_type
        mount_src = "#{storage_type['mount_source']}/#{body['data']['namespace']}/#{app_name}"
        mount_param = "--mount type=#{storage_type['mount_type']},src=#{mount_src},dst=#{storage['mount_target']}"
      end
    end
  end
  mount_param
end
get_storage_type_by_name(storage_types, name) click to toggle source
# File lib/gleis/utils.rb, line 112
def self.get_storage_type_by_name(storage_types, name)
  storage_types.each do |storage_type|
    return storage_type if storage_type['name'] == name
  end
  nil
end
line_exists_in_file(file, line) click to toggle source
# File lib/gleis/utils.rb, line 65
def self.line_exists_in_file(file, line)
  File.readlines(file).grep(/#{Regexp.escape(line)}/).size >= 1
end
output_config_env_vars(env_vars, app_name) click to toggle source
# File lib/gleis/utils.rb, line 82
def self.output_config_env_vars(env_vars, app_name)
  puts "Configuration (environment variables) for #{app_name}:\n\n"
  env_vars.each do |key, value|
    puts "\t#{key}=#{value}"
  end
end
output_tasks(tasks, type) click to toggle source
# File lib/gleis/utils.rb, line 75
def self.output_tasks(tasks, type)
  tasks.each do |task|
    timestamp_formatted = Time.parse(task['timestamp']).localtime.strftime('%c')
    puts "#{type}.#{task['slot']}: #{task['status']} #{timestamp_formatted}"
  end
end
prompt_confirmation(text) click to toggle source
# File lib/gleis/utils.rb, line 69
def self.prompt_confirmation(text)
  print 'WARNING: ' + text + ' (write YES in uppercase to confirm): '
  answer = STDIN.gets.chomp
  answer == 'YES'
end
prompt_password() click to toggle source
# File lib/gleis/utils.rb, line 10
def self.prompt_password
  print 'Password: '
  password = STDIN.noecho(&:gets).chomp
  puts
  password
end
prompt_yes_no(text) click to toggle source
# File lib/gleis/utils.rb, line 17
def self.prompt_yes_no(text)
  loop do
    print text + ' [y/n]: '
    case STDIN.gets.strip
    when 'Y', 'y', 'yes'
      return true
    when /\A[nN]o?\Z/
      return false
    end
  end
end
update_available?(actual_version) click to toggle source
# File lib/gleis/utils.rb, line 119
def self.update_available?(actual_version)
  Gem::Version.new(Gleis::VERSION) < Gem::Version.new(actual_version)
end
uri_path_valid?(path) click to toggle source
# File lib/gleis/utils.rb, line 134
def self.uri_path_valid?(path)
  begin
    URI.parse("http://localhost:3000#{path}")
  rescue URI::InvalidURIError
    return false
  end
  true
end
validate_commit_hash(commit) click to toggle source
# File lib/gleis/utils.rb, line 45
def self.validate_commit_hash(commit)
  abort('Please specifiy a valid commit hash (e.g. 3efb741) pointing to another deployment of your app.') unless
    commit =~ /^\h{7,40}$/
end
validate_scale_count(count) click to toggle source
# File lib/gleis/utils.rb, line 38
def self.validate_scale_count(count)
  count_i = count.to_i
  abort('Please specifiy a number between 1 and 10 as parameter in order to scale your app.') unless
    count_i && count_i.between?(1, 10)
  count_i
end
which(command) click to toggle source
# File lib/gleis/utils.rb, line 123
def self.which(command)
  file_extensions = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    file_extensions.each do |extension|
      executable = File.join(path, "#{command}#{extension}")
      return executable if File.executable?(executable) && !File.directory?(executable)
    end
  end
  nil
end