Class: Gleis::Application

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

Overview

The class implements the methods required to manage gleis applications

Class Method Summary collapse

Class Method Details

.config(app_name, env_var) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/gleis/application.rb', line 4

def self.config(app_name, env_var)
  token = Token.check
  if env_var.end_with? '='
    # Delete variable
    var_name = env_var.split('=')[0]
    response = API.request('delete', "config/#{app_name}/#{var_name}", token)
    puts "Deleted environment variable #{var_name}" if response.code == 204
  elsif env_var.include? '='
    # Update or create variable
    var_value = env_var.split('=')
    response = API.request('post', 'config', token, 'name': app_name, 'var': var_value[0], 'value': var_value[1])
    puts "Sucessfully created environment variable #{var_value[0]}" if response.code == 201
    puts "Sucessfully updated environment variable #{var_value[0]}" if response.code == 200
  else
    config_body = Config.get_env_vars(app_name, token)
    Utils.output_config_env_vars(config_body['env_vars'], app_name)
  end
end

.create(app_name) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/gleis/application.rb', line 23

def self.create(app_name)
  token = Token.check
  print "Creating new app named #{app_name}, you should be ready to go in about 10 seconds ... "
  body = API.request('post', 'create', token, 'name': app_name)
  # Check status of project creation in Gitlab
  if body['success'] == 1
    puts "done!\n"
    git_ssh_url_to_repo = body['ssh_url_to_repo']
    # Add git remote if .git/config file is detected
    if Utils.add_remote_to_git_config(git_ssh_url_to_repo) == false
      puts "Now add the gleis git remote to your app using: git remote add gleis #{git_ssh_url_to_repo}"
    else
      puts 'Git remote gleis added to your git config'
    end
    puts 'When ready to deploy your app, push it to the gleis git remote with: git push gleis master'
    puts "You can then access your app using the URL https://#{body['dns_name']}" if body.key? 'dns_name'
  else
    puts 'failed!'
  end
end

.deployments(app_name) ⇒ Object



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

def self.deployments(app_name)
  token = Token.check
  action = 'deployment/' + app_name
  body = API.request('get', action, token)
  if body['deployments'].any?
    body['deployments'].reverse_each do |d|
      puts "v#{d['version']} | #{d['commit'][0, 7]} | #{d['email']} | #{d['subject']}\n" \
        "deployed by #{d['name']} on #{Time.parse(d['created_at']).strftime('%c')}\n\n"
    end
  else
    puts 'No deployments found, please deploy your app first.'
  end
end

.destroy(app_name) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/gleis/application.rb', line 58

def self.destroy(app_name)
  token = Token.check
  if Utils.prompt_confirmation("Are you sure you want to destroy the #{app_name} app?")
    # Delete everything related to project/app
    body = API.request('post', 'delete', token, 'name': app_name)
    if body['success'] == 1
      puts 'App destroyed successfully'
    else
      puts 'Failed to destroy app: ' + body['message']
    end
  else
    puts 'Command cancelled'
  end
end

.exec(app_name, command) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/gleis/application.rb', line 73

def self.exec(app_name, command)
  token = Token.check
  config_body = Config.get_env_vars(app_name, token)
  # Get storage and generate mount parameter
  mount_param = Utils.generate_exec_mount_param(API.request('get', "storage/#{app_name}", token), app_name)
  # Get deployments and commit from last deployment
  dp_body = API.request('get', "deployment/#{app_name}", token)
  if dp_body['deployments'].any?
    # Get CLI parameters from API server
    p = Params.get_cli_parameters(token)
    system("ssh -t -q -o 'StrictHostKeyChecking=no' \
      -p #{p['run_port']} \
      #{p['run_username']}@#{p['run_server']} \
      '#{Utils.generate_exec_env_param(config_body['env_vars'])} #{mount_param} \
        #{p['registry_server']}/#{dp_body['namespace']}/#{app_name}:#{dp_body['deployments'].last['commit'][0..6]} \
        #{command}'")
  else
    puts 'No deployments found, please deploy your app first.'
  end
end

.git(app_name) ⇒ Object



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

def self.git(app_name)
  token = Token.check
  action = 'git/' + app_name
  body = API.request('get', action, token)
  if body['success'] == 1
    puts "The URL to the git repo of the #{app_name} app is: #{body['data']}"
  else
    puts 'Failed to fetch git URL for app.'
  end
end

.logs(app_name) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/gleis/application.rb', line 105

def self.logs(app_name)
  token = Token.check
  action = 'logs/' + app_name
  body = API.request('get', action, token)
  if body['log'].nil? || body['log'].empty?
    puts 'No log entries found yet.'
  else
    puts "Last 100 lines of consolidated log output for #{app_name}:"
    puts
    puts body['log']
  end
end

.ps(app_name) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/gleis/application.rb', line 152

def self.ps(app_name)
  token = Token.check
  body = API.request('get', "ps/#{app_name}", token)
  puts 'Failed to get processes.' unless body['success'] == 1
  if body['data']&.size&.positive?
    body['data'].each_with_index do |service, index|
      puts "=== #{service[0]}: `#{service[1]['command']}`"
      if service[1]['tasks'].empty?
        puts 'No processes running'
      else
        Utils.output_tasks(service[1]['tasks'], service[1]['type'])
      end
      puts '' if index != body['data'].size - 1
    end
  else
    puts 'No deployments found, please deploy your app first.'
  end
end

.restart(app_name) ⇒ Object



118
119
120
121
122
123
124
125
126
# File 'lib/gleis/application.rb', line 118

def self.restart(app_name)
  token = Token.check
  body = API.request('post', 'restart', token, 'name': app_name)
  if body['success'] == 1
    puts 'Successfully restarted app using rolling update'
  else
    puts 'Failed to restart app: ' + body['error_text']
  end
end

.scale(app_name, replica) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/gleis/application.rb', line 128

def self.scale(app_name, replica)
  token = Token.check
  count = Utils.validate_scale_count(replica)
  body = API.request('post', 'scale', token, 'name': app_name, 'count': count)
  if body['success'] == 1
    puts "Successfully scaled app to #{count} replica"
    if body['message']
      puts 'Note that your app is currently not running, hence scaling will take effect as soon as you start it'
    end
  else
    puts "Failed to scale app: #{body['message']}"
  end
end

.start(app_name) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/gleis/application.rb', line 142

def self.start(app_name)
  token = Token.check
  body = API.request('post', 'start', token, 'name': app_name)
  if body['success'] == 1
    puts 'Successfully started app'
  else
    puts 'Failed to start app: ' + body['message']
  end
end

.status(app_name) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/gleis/application.rb', line 171

def self.status(app_name)
  token = Token.check
  action = 'status/' + app_name
  body = API.request('get', action, token)
  puts "Status of app #{app_name}:\n\n"
  if body['success'] == 1
    status = body['status']
    puts "\tStatus:\t\trunning\n" \
         "\tStarted at:\t#{Time.parse(status['createdat']).localtime.strftime('%c')}\n" \
         "\tUpdated at:\t#{Time.parse(status['updatedat']).localtime.strftime('%c')}\n" \
         "\tUpdate count:\t#{status['forceupdate']}\n" \
         "\tReplicas:\t#{status['replicas']}"
  else
    puts "\tStatus:\t\tnot running"
  end
end

.stop(app_name) ⇒ Object



188
189
190
191
192
193
194
195
196
# File 'lib/gleis/application.rb', line 188

def self.stop(app_name)
  token = Token.check
  body = API.request('post', 'stop', token, 'name': app_name)
  if body['success'] == 1
    puts 'Succesfully stopped app'
  else
    puts 'Failed to stop app: ' + body['message']
  end
end