Class: DevService
- Inherits:
-
Object
show all
- Extended by:
- ApiClient
- Defined in:
- lib/uffizzi/services/dev_service.rb
Constant Summary
collapse
- DEFAULT_REGISTRY_REPO =
'registry.uffizzi.com'
- STARTUP_STATE =
'startup'
- CLUSTER_DEPLOYED_STATE =
'cluster_deployed'
Class Method Summary
collapse
Methods included from ApiClient
bulk_create_secrets, check_can_install, check_credential, create_access_token, create_account_controller_settings, create_ci_session, create_cluster, create_credential, create_deployment, create_project, create_session, delete_account_controller_settings, delete_cluster, delete_credential, delete_deployment, delete_project, delete_secret, deploy_containers, describe_compose_file, describe_deployment, destroy_session, fetch_account, fetch_account_projects, fetch_accounts, fetch_credentials, fetch_deployment_service_logs, fetch_deployment_services, fetch_deployments, fetch_events, fetch_project, fetch_projects, fetch_secrets, get_access_token, get_account_clusters, get_account_controller_settings, get_activity_items, get_cluster, get_cluster_ingresses, get_k8s_container_description, get_project_clusters, scale_down_cluster, scale_up_cluster, set_compose_file, sync_cluster, unset_compose_file, update_account, update_account_controller_settings, update_credential, update_deployment, validate_compose_file
Methods included from ApiRoutes
#access_token_url, #access_tokens_url, #account_can_install_uri, #account_clusters_uri, #account_controller_setting_uri, #account_controller_settings_uri, #account_projects_uri, #account_uri, #accounts_uri, #activity_items_uri, #browser_sign_in_url, #check_credential_uri, #ci_session_uri, #cluster_uri, #compose_file_uri, #create_projects_uri, #credential_uri, #credentials_uri, #deploy_containers_uri, #deployment_uri, #deployments_uri, #events_uri, #k8s_container_description_uri, #preview_service_logs_uri, #preview_services_uri, #project_cluster_ingresses_uri, #project_clusters_uri, #project_uri, #projects_uri, #scale_down_cluster_uri, #scale_up_cluster_uri, #secret_uri, #secrets_uri, #session_uri, #sync_cluster_uri, #validate_compose_file_uri
Class Method Details
.build_skaffold_dev_command(config_path, options) ⇒ Object
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/uffizzi/services/dev_service.rb', line 143
def build_skaffold_dev_command(config_path, options)
cmd = [
'skaffold dev',
"--filename='#{config_path}'",
"--default-repo='#{default_registry_repo(options[:'default-repo'])}'",
"--kubeconfig='#{default_kubeconfig_path(options[:kubeconfig])}'",
]
cmd.join(' ')
end
|
.check_environment_exist! ⇒ Object
25
26
27
28
29
|
# File 'lib/uffizzi/services/dev_service.rb', line 25
def check_environment_exist!
if dev_environment.empty?
Uffizzi.ui.say_error_and_exit('Uffizzi dev does not exist')
end
end
|
.check_no_running_process! ⇒ Object
13
14
15
16
17
|
# File 'lib/uffizzi/services/dev_service.rb', line 13
def check_no_running_process!
if process_running?
Uffizzi.ui.say_error_and_exit("You have already started uffizzi dev. To stop the process do 'uffizzi dev stop'")
end
end
|
.check_running_process! ⇒ Object
19
20
21
22
23
|
# File 'lib/uffizzi/services/dev_service.rb', line 19
def check_running_process!
unless process_running?
Uffizzi.ui.say_error_and_exit('Uffizzi dev is not running')
end
end
|
.check_skaffold_config_existence(config_path) ⇒ Object
121
122
123
124
125
126
127
128
129
|
# File 'lib/uffizzi/services/dev_service.rb', line 121
def check_skaffold_config_existence(config_path)
msg = 'A valid dev environment configuration is required. Please provide a valid config,'\
"\r\n"\
'or run `skaffold init` to generate a skaffold.yaml configuration.'\
"\r\n"\
'See the `uffizzi dev start --help` page for supported configs and usage details.'
Uffizzi.ui.say_error_and_exit(msg) unless File.exist?(config_path)
end
|
.check_skaffold_existence ⇒ Object
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/uffizzi/services/dev_service.rb', line 110
def check_skaffold_existence
cmd = 'skaffold version'
stdout_str, stderr_str = Uffizzi.ui.capture3(cmd)
return if stdout_str.present? && stderr_str.blank?
Uffizzi.ui.say_error_and_exit(stderr_str)
rescue StandardError => e
Uffizzi.ui.say_error_and_exit(e.message)
end
|
.clear_dev_environment_config ⇒ Object
209
210
211
|
# File 'lib/uffizzi/services/dev_service.rb', line 209
def clear_dev_environment_config
Uffizzi::ConfigFile.write_option(:dev_environment, {})
end
|
.default_kubeconfig_path(kubeconfig_path) ⇒ Object
158
159
160
161
162
|
# File 'lib/uffizzi/services/dev_service.rb', line 158
def default_kubeconfig_path(kubeconfig_path)
path = kubeconfig_path || KubeconfigService.default_path
File.expand_path(path)
end
|
.default_registry_repo(repo) ⇒ Object
154
155
156
|
# File 'lib/uffizzi/services/dev_service.rb', line 154
def default_registry_repo(repo)
repo || DEFAULT_REGISTRY_REPO
end
|
.delete_pid ⇒ Object
174
175
176
177
|
# File 'lib/uffizzi/services/dev_service.rb', line 174
def delete_pid
File.delete(pid_path) if File.exist?(pid_path)
File.delete(skaffold_pid_path) if File.exist?(skaffold_pid_path)
end
|
.dev_environment ⇒ Object
.find_skaffold_pid(pid) ⇒ Object
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
# File 'lib/uffizzi/services/dev_service.rb', line 217
def find_skaffold_pid(pid)
pid_regex = /\w*#{pid}.*skaffold dev/
io = Uffizzi.ui.popen('ps -ef')
processes = io.readlines.select { |l| l.match?(pid_regex) }
if processes.count.zero?
raise StandardError.new('Can\'t find skaffold process pid')
end
if processes.count == 1
current_pid = processes[0].gsub(/\s+/, ' ').lstrip.split[1]
return pid if current_pid.to_s == pid.to_s
raise StandardError.new('Can\'t find skaffold process pid')
end
parent_process = processes
.map { |ps| ps.gsub(/\s+/, ' ').lstrip.split }
.detect { |ps| ps[2].to_s == pid.to_s }
parent_process[1]
end
|
.logs_path ⇒ Object
139
140
141
|
# File 'lib/uffizzi/services/dev_service.rb', line 139
def logs_path
File.join(Uffizzi::ConfigFile::CONFIG_DIR, 'uffizzi_dev.log')
end
|
.pid_path ⇒ Object
131
132
133
|
# File 'lib/uffizzi/services/dev_service.rb', line 131
def pid_path
File.join(Uffizzi::ConfigFile::CONFIG_DIR, 'uffizzi_dev.pid')
end
|
.process_running? ⇒ Boolean
56
57
58
59
60
61
62
63
64
|
# File 'lib/uffizzi/services/dev_service.rb', line 56
def process_running?
pid = running_pid
return false unless pid.positive?
Uffizzi.process.kill(0, pid.to_i)
true
rescue Errno::ESRCH
false
end
|
.running_pid ⇒ Object
164
165
166
167
168
|
# File 'lib/uffizzi/services/dev_service.rb', line 164
def running_pid
return nil.to_i unless File.exist?(pid_path)
File.read(pid_path).to_i
end
|
.running_skaffold_pid ⇒ Object
179
180
181
182
183
|
# File 'lib/uffizzi/services/dev_service.rb', line 179
def running_skaffold_pid
return nil.to_i unless File.exist?(skaffold_pid_path)
File.read(skaffold_pid_path).to_i
end
|
.save_pid ⇒ Object
170
171
172
|
# File 'lib/uffizzi/services/dev_service.rb', line 170
def save_pid
File.write(pid_path, Uffizzi.process.pid)
end
|
.save_skaffold_pid(pid) ⇒ Object
185
186
187
|
# File 'lib/uffizzi/services/dev_service.rb', line 185
def save_skaffold_pid(pid)
File.write(skaffold_pid_path, pid)
end
|
.set_cluster_deployed_state ⇒ Object
.set_dev_environment_config(cluster_name, config_path, options) ⇒ Object
189
190
191
192
193
|
# File 'lib/uffizzi/services/dev_service.rb', line 189
def set_dev_environment_config(cluster_name, config_path, options)
params = options.merge(config_path: File.expand_path(config_path))
new_dev_environment = Uffizzi::ConfigHelper.set_dev_environment(cluster_name, params)
Uffizzi::ConfigFile.write_option(:dev_environment, new_dev_environment)
end
|
.set_startup_state ⇒ Object
195
196
197
198
|
# File 'lib/uffizzi/services/dev_service.rb', line 195
def set_startup_state
new_dev_environment = dev_environment.merge(state: STARTUP_STATE)
Uffizzi::ConfigFile.write_option(:dev_environment, new_dev_environment)
end
|
.skaffold_pid_path ⇒ Object
135
136
137
|
# File 'lib/uffizzi/services/dev_service.rb', line 135
def skaffold_pid_path
File.join(Uffizzi::ConfigFile::CONFIG_DIR, 'skaffold_dev.pid')
end
|
.start_basic_skaffold(config_path, options) ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/uffizzi/services/dev_service.rb', line 75
def start_basic_skaffold(config_path, options)
Uffizzi.ui.say('Start skaffold')
cmd = build_skaffold_dev_command(config_path, options)
Uffizzi.signal.trap('INT') {}
Uffizzi.ui.popen2e(cmd) do |_stdin, stdout_and_stderr, wait_thr|
pid = wait_thr.pid
skaffold_pid = find_skaffold_pid(pid)
save_skaffold_pid(skaffold_pid)
stdout_and_stderr.each { |l| Uffizzi.ui.say(l) }
wait_thr.value
end
end
|
.start_check_pid_file_existence ⇒ Object
66
67
68
69
70
71
72
73
|
# File 'lib/uffizzi/services/dev_service.rb', line 66
def start_check_pid_file_existence
Uffizzi.thread.new do
loop do
stop_process unless File.exist?(pid_path)
sleep(1)
end
end
end
|
.start_demonised_skaffold(config_path, options) ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/uffizzi/services/dev_service.rb', line 90
def start_demonised_skaffold(config_path, options)
File.write(logs_path, "Start skaffold\n")
cmd = build_skaffold_dev_command(config_path, options)
Uffizzi.ui.popen2e(cmd) do |_stdin, stdout_and_stderr, wait_thr|
pid = wait_thr.pid
skaffold_pid = find_skaffold_pid(pid)
save_skaffold_pid(skaffold_pid)
File.open(logs_path, 'a') do |f|
stdout_and_stderr.each do |line|
f.puts(line)
f.flush
end
end
wait_thr.value
end
end
|
.startup? ⇒ Boolean
205
206
207
|
# File 'lib/uffizzi/services/dev_service.rb', line 205
def startup?
dev_environment[:state] == STARTUP_STATE
end
|
.stop_process ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/uffizzi/services/dev_service.rb', line 31
def stop_process
dev_pid = running_pid
skaffold_pid = running_skaffold_pid
begin
Uffizzi.process.kill('INT', skaffold_pid)
rescue Errno::ESRCH
end
wait_process_stop(skaffold_pid)
delete_pid
Uffizzi.process.kill('INT', dev_pid)
rescue Errno::ESRCH
delete_pid
end
|
.wait_process_stop(pid) ⇒ Object
48
49
50
51
52
53
54
|
# File 'lib/uffizzi/services/dev_service.rb', line 48
def wait_process_stop(pid)
loop do
Uffizzi.process.kill(0, pid)
sleep(1)
end
rescue Errno::ESRCH
end
|