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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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_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
    which(command)
end

.convert_username_to_filename(username) ⇒ Object



6
7
8
# 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) ⇒ Object



89
90
91
92
93
94
95
# 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) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# 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) ⇒ Object



112
113
114
115
116
117
# 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) ⇒ Object



65
66
67
# 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) ⇒ Object



82
83
84
85
86
87
# 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) ⇒ Object



75
76
77
78
79
80
# 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) ⇒ Object



69
70
71
72
73
# 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_passwordObject



10
11
12
13
14
15
# 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) ⇒ 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

.update_available?(actual_version) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/gleis/utils.rb', line 119

def self.update_available?(actual_version)
  Gem::Version.new(Gleis::VERSION) < Gem::Version.new(actual_version)
end

.validate_commit_hash(commit) ⇒ Object



45
46
47
48
# 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) ⇒ 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 && count_i.between?(1, 10)
  count_i
end

.which(command) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# 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