Class: Gleis::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/gleis/utils.rb

Overview

This class gathers various utilities as small methods

Class Method Summary collapse

Class Method Details

.add_remote_to_git_config(remote_url) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/gleis/utils.rb', line 45

def self.add_remote_to_git_config(remote_url)
  config_file = 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_nameObject



29
30
31
# File 'lib/gleis/utils.rb', line 29

def self.app_name
  Dir.pwd.split('/').last
end

.check_for_local_pg_command(command) ⇒ Object



33
34
35
36
# 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
    system("which #{command} >/dev/null")
end

.convert_username_to_filename(username) ⇒ Object



4
5
6
# File 'lib/gleis/utils.rb', line 4

def self.convert_username_to_filename(username)
  username.sub('@', '_at_').tr('.', '_')
end

.generate_exec_env_param(env_vars) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/gleis/utils.rb', line 84

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) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/gleis/utils.rb', line 92

def self.generate_exec_mount_param(body, app_name)
  mount_param = ''
  if body['success'] == 1
    storage_type = body['storage_type']
    if storage_type
      storage = body['storage']
      mount_src = "#{storage_type['mount_source']}/#{body['namespace']}/#{app_name}"
      mount_param = "--mount type=#{storage_type['mount_type']},src=#{mount_src},dst=#{storage['mount_target']}"
    end
  end
  mount_param
end

.line_exists_in_file(file, line) ⇒ Object



60
61
62
# File 'lib/gleis/utils.rb', line 60

def self.line_exists_in_file(file, line)
  File.readlines(file).grep(/#{Regexp.escape(line)}/).size.positive?
end

.output_config_env_vars(env_vars, app_name) ⇒ Object



77
78
79
80
81
82
# File 'lib/gleis/utils.rb', line 77

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) ⇒ Object



70
71
72
73
74
75
# File 'lib/gleis/utils.rb', line 70

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) ⇒ Object



64
65
66
67
68
# File 'lib/gleis/utils.rb', line 64

def self.prompt_confirmation(text)
  print 'WARNING: ' + text + ' (write YES in uppercase to confirm): '
  answer = STDIN.gets.chomp
  answer == 'YES'
end

.prompt_passwordObject



8
9
10
11
12
13
14
15
# File 'lib/gleis/utils.rb', line 8

def self.prompt_password
  print 'Password: '
  system 'stty -echo'
  password = $stdin.gets.chomp
  system 'stty echo'
  puts
  password
end

.prompt_yes_no(text) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# 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

.validate_scale_count(count) ⇒ Object



38
39
40
41
42
43
# 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&.between?(1, 10)
  count_i
end