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
43
# 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!'
    puts "\nReason for failure: #{body['message']}" unless body['message'].nil?
  end
end

.deployments(app_name) ⇒ Object



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

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|
      deployed = d['deployed'] ? '(*)' : ''
      puts "v#{d['version']}#{deployed} | #{d['commit'][0, 7]} | #{d['email']} | #{d['subject']}\n" \
        "deployed by #{d['name']} on #{Time.parse(d['created_at']).strftime('%c')}\n\n"
    end
    puts "\t(*) current version in use\n\n"
  else
    puts 'No deployments found, please deploy your app first.'
  end
end

.destroy(app_name) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/gleis/application.rb', line 61

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



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

def self.exec(app_name, command)
  token = Token.check
  abort('The SSH client command ssh is not installed on this system.') unless Utils.which('ssh')
  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, quiet) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/gleis/application.rb', line 98

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

.logs(app_name, follow, process) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/gleis/application.rb', line 113

def self.logs(app_name, follow, process)
  token = Token.check
  action = "logs/#{app_name}/#{process}?follow=#{follow}"
  if follow == true
    puts "Following log for the #{app_name} #{process} process:\n\n"
    API.request('stream', action, token)
  else
    body = API.request('get', action, token)
    if body['log'].nil?
      puts 'Failed to get logs: ' + body['message']
    elsif body['log'].empty?
      puts 'No log entries found yet.'
    else
      puts "Most recent entries of consolidated log for the #{app_name} #{process} process:\n\n"
      puts body['log']
    end
  end
end

.maintenance(app_name, mode = nil) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/gleis/application.rb', line 132

def self.maintenance(app_name, mode = nil)
  token = Token.check
  body = API.request('get', "maintenance/#{app_name}", token)
  current_mode_text = body['data'] == true ? 'on' : 'off'
  if mode.nil?
    puts "Maintenance mode is currently #{current_mode_text}"
  else
    abort("Maintenance mode is already #{current_mode_text}") if body['data'] == mode
    body = API.request('put', 'maintenance', token, 'name': app_name, 'mode': mode)
    new_mode_text = mode == true ? 'on' : 'off'
    if body['success'] == 1
      puts "Successfully turned #{new_mode_text} maintenance mode"
    else
      puts "Failed to turn #{new_mode_text} maintenance mode"
    end
  end
end

.ps(app_name) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/gleis/application.rb', line 193

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'] && body['data'].size && body['data'].size >= 1
    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

.rebuild(app_name) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/gleis/application.rb', line 150

def self.rebuild(app_name)
  token = Token.check
  puts 'Please wait while rebuilding, it could take a few minutes depending on app dependencies and complexity...'
  body = API.request('post', 'rebuild', token, 'name': app_name)
  if body['success'] == 1
    puts "\nSuccessfully rebuilt app from git repo, detailed output from image build below:"
    puts body['data']
  else
    puts 'Failed to rebuild app: ' + body['message']
  end
end

.restart(app_name) ⇒ Object



162
163
164
165
166
167
168
169
170
# File 'lib/gleis/application.rb', line 162

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['message']
  end
end

.rollback(app_name, commit) ⇒ Object



172
173
174
175
176
177
178
179
180
181
# File 'lib/gleis/application.rb', line 172

def self.rollback(app_name, commit)
  token = Token.check
  Utils.validate_commit_hash(commit)
  body = API.request('post', 'rollback', token, 'name': app_name, 'commit': commit)
  if body['success'] == 1
    puts "Successfully rolled back app to deployment with commit hash #{commit}"
  else
    puts "Failed to rollback app: #{body['message']}"
  end
end

.scale(app_name, replica) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/gleis/application.rb', line 212

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



183
184
185
186
187
188
189
190
191
# File 'lib/gleis/application.rb', line 183

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



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/gleis/application.rb', line 226

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['data']
    deployment = if status['deployment_commit'] && status['deployment_version']
                   "#{status['deployment_commit']} (v#{status['deployment_version']})"
                 else
                   'app not deployed yet'
                 end
    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" \
         "\tDeployment:\t#{deployment}\n" \
         "\tUpdate count:\t#{status['forceupdate']}\n" \
         "\tReplicas:\t#{status['replicas']}"
  else
    puts "\tStatus:\t\tnot running"
  end
end

.stop(app_name) ⇒ Object



249
250
251
252
253
254
255
256
257
# File 'lib/gleis/application.rb', line 249

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