sct 0.1.28 → 0.1.34

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2795077123ba193f891933659f88c61cb4a986ebc4a5c7bf8ced2fd9b9b54a97
4
- data.tar.gz: 8478af6c3a869f524b09bc607ae09acbc48eed401777366947fc270a39166a2b
3
+ metadata.gz: a44226fb281fd62b413a72c92fc8f747a02be4da29e4e76a5c2f431e9dfa27b7
4
+ data.tar.gz: 8ac335f5d43ba44212e9131b93296d3796e678442aac3e6f9039d23a497fa763
5
5
  SHA512:
6
- metadata.gz: deecf2931622dda191ee00ffc6412a41c7aa4ac8df88006f8eeab183c8d50c543fb2d25f5012c47157f0faa1208431d450716e9b6f50506991aaa1a5f3e95dbc
7
- data.tar.gz: afcf8914cbd3efba0e46764b5ed8633f570ec2e9b272a70a28cf48d11fd29bd41beaa1b9cd4896a263f99d657835fccb5329a245a160a009a47f17a4915589cb
6
+ metadata.gz: 58789cf672976004e0853f46924c62642266d312b3f5f160a30c10a9cf9edc9ad36c25ffea311c4b9a79fd29ea205cc5c8e552941fe673ef2072c96cbf3b8dbf
7
+ data.tar.gz: 51caa6da2bc2e458cdae40347f84f6d830896879c629d3d3d642b44e2781d7a7bd082321dcd1342954ebc1371021ba2f855eaf29aefd2dcf4621eca2613e478f
@@ -9,74 +9,71 @@ module Cluster
9
9
  self.new.run
10
10
  end
11
11
 
12
- def run
12
+ def run
13
13
  program :name, 'cluster'
14
14
  program :version, Sct::VERSION
15
15
  program :description, 'CLI for \'cluster\' - Manage your local kubernetes cluster'
16
16
 
17
17
  global_option('--verbose') { $verbose = true }
18
18
 
19
- command :up do |c|
19
+ command :start do |c|
20
+ c.syntax = "sct cluster start"
21
+ c.description = "start the cluster"
20
22
 
21
- c.syntax = "sct cluster up"
22
- c.description = "Start the cluster"
23
- c.option '--clean', 'start a clean cluster. Old cluster will be purged if available.'
24
-
25
- c.action do |args, options|
26
- if options.clean
27
- Cluster::Runner.new.reset
28
- else
29
- Cluster::Runner.new.launch
30
- end
23
+ c.action do |args, options|
24
+ Cluster::Runner.new.start
31
25
  end
32
26
 
33
27
  end
34
28
 
35
- command :down do |c|
36
-
37
- c.syntax = 'sct cluster down'
29
+ command :stop do |c|
30
+ c.syntax = 'sct cluster stop'
38
31
  c.description = 'stop the cluster'
39
32
 
40
- c.action do |args, options|
41
- Cluster::Runner.new.down
33
+ c.action do |args, options|
34
+ Cluster::Runner.new.stop
42
35
  end
43
36
  end
44
37
 
45
38
  command :restart do |c|
46
-
47
- c.syntax = 'sct cluster restart'
48
- c.description = 'restart the cluster (this is a short hand for sct cluster down && sct cluster up)'
39
+ c.syntax = "sct cluster restart"
40
+ c.description = "restart the cluster"
49
41
 
50
42
  c.action do |args, options|
51
- Cluster::Runner.new.down
52
- Cluster::Runner.new.launch
43
+ Cluster::Runner.new.restart
53
44
  end
45
+
54
46
  end
55
47
 
56
- command :reset do |c|
48
+ command :delete do |c|
49
+ c.syntax = "sct cluster delete"
50
+ c.description = "delete the cluster"
57
51
 
52
+ c.action do |args, options|
53
+ Cluster::Runner.new.delete
54
+ end
55
+
56
+ end
57
+
58
+ command :reset do |c|
58
59
  c.syntax = 'sct cluster reset'
59
- c.description = 'reset your cluster and start with a clean cluster'
60
+ c.description = 'delete the cluster and start a new cluster'
60
61
 
61
- c.action do |args, options|
62
+ c.action do |args, options|
62
63
  Cluster::Runner.new.reset
63
64
  end
64
65
  end
65
66
 
66
- alias_command :'setup', :'reset'
67
-
68
67
  command :status do |c|
69
-
70
68
  c.syntax = 'sct cluster status'
71
69
  c.description = 'see the status of your cluster'
72
70
 
73
- c.action do |args, options|
71
+ c.action do |args, options|
74
72
  Cluster::Runner.new.status
75
73
  end
76
74
  end
77
75
 
78
76
  command :'update config' do |c|
79
-
80
77
  c.syntax = 'sct cluster update config'
81
78
  c.description = 'update the cluster configuration'
82
79
 
@@ -85,36 +82,16 @@ module Cluster
85
82
  end
86
83
  end
87
84
 
88
- command :'delete pods' do |c|
89
- c.syntax = 'sct cluster delete stalled pods'
90
- c.description = 'delete stalled pods from the cluster'
91
- c.option '--stalled', 'delete stalled pods'
92
- c.option '--all', 'delete all pods'
93
-
94
- c.action do |args, options|
95
-
96
- Cluster::Runner.new.delete_stalled_pods if options.stalled
97
- Cluster::Runner.new.delete_all_pods if options.all
98
-
99
- Cluster::Runner.new.delete_pods(args) if !args.empty?
100
-
101
- end
102
- end
103
-
104
- command :'apply pods' do |c|
105
- c.syntax = 'sct cluster apply pods'
106
- c.description = 'apply pods from the k8s folder'
85
+ command :'apply deployments' do |c|
86
+ c.syntax = 'sct cluster apply deployments'
87
+ c.description = 'apply deployments from the k8s folder'
107
88
 
108
89
  c.action do |args, options|
109
-
110
- Cluster::Runner.new.apply_pods
111
-
90
+ Cluster::Runner.new.apply_deployments
112
91
  end
113
92
  end
114
93
 
115
- default_command :status
116
-
117
94
  run!
118
95
  end
119
96
  end
120
- end
97
+ end
@@ -2,6 +2,6 @@ module Cluster
2
2
 
3
3
  # import the helper functionality from SCT
4
4
  Helpers = Sct::Helper
5
- UI = Sct::UI
5
+ UI = Sct::UI
6
6
 
7
- end
7
+ end
@@ -1,118 +1,165 @@
1
1
  module Cluster
2
2
  class Runner
3
3
 
4
- def launch
5
- return UI.error("SCT has not been initialized. Run 'sct init' first.") unless SctCore::Config.exists
4
+ def start
5
+ SctCore::Helper.ensure_windows_administrator # "sct hostfile" will need it later on
6
+
7
+ existing_cluster = cluster_exists?
8
+
6
9
  start_cluster
7
- run_command "kubectl delete pod -n kube-system #{pods("kube-system").map { |pod| pod[:name] if pod[:name].start_with? "registry-creds" } .compact.join(" ")}"
8
- run_command "kubectl rollout status -n kube-system deployment/registry-creds"
9
- post_start
10
- end
11
10
 
12
- def start_cluster
13
- if SctCore::Helper.operatingSystem == SctCore::Helper::MAC_OS
14
- run_command "#{SctCore::Helper.minikube} start --cpus=$(sysctl -n hw.ncpu) --memory=8G"
11
+ if existing_cluster
12
+ run_command "kubectl rollout restart -n kube-system deployment registry-creds"
13
+ run_command "kubectl rollout status -n kube-system deployment registry-creds"
15
14
  else
16
- run_command "#{SctCore::Helper.minikube} start --cpus=$(cat /proc/cpuinfo | grep processor | wc -l) --memory=10G"
15
+ create_secrets
16
+ enable_addons
17
+ wait_for_gcr_secret
18
+ run_command "kubectl apply -f ~/development/spend-cloud/k8s/ingress.yml"
19
+ wait_for_ingress_ip
20
+ run_command "kubectl apply -f ~/development/spend-cloud/k8s/dependencies.yml"
21
+ wait_for_pods
22
+ create_keycloak_database_user
23
+ run_command "kubectl apply -f ~/development/spend-cloud/k8s/keycloak-server.yml"
24
+ wait_for_pods
25
+ run_command "kubectl apply -f ~/development/spend-cloud/k8s/"
17
26
  end
18
- run_command "#{SctCore::Helper.minikube} ssh -- 'sudo su -c \"echo 10048576 > /proc/sys/fs/inotify/max_user_watches\"'"
19
- update_config
27
+
28
+ post_start
20
29
  end
21
30
 
22
- def post_start
23
- wait_for_pods
24
- run_command "sudo sct hostfile"
25
- UI.success("\n✔️ You can visit your environment at 👉 https://spend-cloud.spend.cloud.local 👌")
31
+ def stop
32
+ run_command "minikube stop"
33
+ end
34
+
35
+ def restart
36
+ SctCore::Helper.ensure_windows_administrator # "sct hostfile" will need it later on
37
+
38
+ stop
39
+ start
26
40
  end
27
41
 
28
- def down
29
- run_command "#{SctCore::Helper.minikube} stop"
42
+ def delete
43
+ run_command "minikube delete"
30
44
  end
31
45
 
32
46
  def reset
33
- run_command "#{SctCore::Helper.minikube} delete"
34
- start_cluster
35
- create_secrets
36
- enable_addons
37
- wait_for_gcr_secret
38
- run_command "kubectl apply -f ~/development/spend-cloud/k8s/ingress.yml"
39
- wait_for_ingress_ip
40
- run_command "kubectl apply -f ~/development/spend-cloud/k8s/dependencies.yml"
41
- wait_for_pods
42
- run_command "kubectl apply -f ~/development/spend-cloud/k8s/"
43
- post_start
47
+ SctCore::Helper.ensure_windows_administrator # "sct hostfile" will need it later on
48
+
49
+ delete
50
+ start
44
51
  end
45
52
 
46
- def enable_addons
47
- enable_addon "registry-creds"
48
- enable_addon "ingress"
53
+ def status
54
+ print_contexts
55
+ print_minikube_status
56
+
57
+ if get_minikube_status.find_all { |status| status[1] == 'Stopped' }.count == 0
58
+ print_pods_status("kube-system")
59
+ print_pods_status
60
+ else
61
+ UI.important("Please check your minikube status. If all services are stopped you should start the minikube first.")
62
+ end
49
63
  end
50
64
 
51
- def enable_addon(addon)
52
- run_command "#{SctCore::Helper.minikube} addons enable #{addon}"
65
+ def update_config
66
+ run_command "kubectl config use-context minikube"
53
67
 
54
- deployment = deployments("kube-system").find { |deployment| deployment[:name].include? addon }
68
+ run_command "kubectl replace -n kube-system -f #{File.expand_path('resources/corefile.yml', __dir__)}"
69
+ run_command "kubectl rollout restart -n kube-system deployment coredns"
70
+ run_command "kubectl rollout status -n kube-system deployment coredns"
71
+ end
55
72
 
56
- run_command "kubectl rollout status -n kube-system deployment/#{deployment[:name]}"
73
+ def apply_deployments
74
+ run_command "kubectl apply -f ~/development/spend-cloud/k8s/"
57
75
  end
58
76
 
59
- def wait_for_pods
60
- UI.important("Waiting for pods to become ready...")
61
77
 
62
- while ! pods.all? { |pod| pod[:status] == "Running" }
63
- delete_stalled_pods
78
+ def cluster_exists?
79
+ if system "minikube status", { out: "/dev/null" }
80
+ # cluster exists and is running
81
+ return true
82
+ end
64
83
 
65
- sleep 5
84
+ if $?.exitstatus == 85
85
+ # cluster does not exist
86
+ return false
66
87
  end
67
88
 
68
- UI.success("Pods are now ready.")
89
+ # cluster exists but is stopped
90
+ return true
69
91
  end
70
92
 
71
- def delete_stalled_pods(feedback: false)
72
-
73
- return UI.success("No stalled pods found") unless !pods.to_a.empty?
74
-
75
- stalled_pods = pods.select { |pod| pod[:stalled] }
76
-
77
- if stalled_pods.empty?
78
- UI.success("There are no stalled pods.") if feedback
93
+ def start_cluster
94
+ if SctCore::Helper.operatingSystem == SctCore::Helper::MAC_OS
95
+ run_command "minikube start --driver=hyperkit --vm=true --cpus=$(sysctl -n hw.ncpu) --memory=8G"
79
96
  else
80
- run_command "kubectl delete pods #{stalled_pods.map { |pod| pod[:name] } .join(" ")}"
97
+ run_command "minikube start --driver=docker --cpus=$(cat /proc/cpuinfo | grep processor | wc -l) --memory=3G"
81
98
  end
99
+ update_config
82
100
  end
83
101
 
84
- def delete_all_pods
85
- run_command "kubectl delete pods --all"
102
+ def post_start
103
+ wait_for_pods
104
+ copy_proactive_accounts_file
105
+ if SctCore::Helper.operatingSystem != SctCore::Helper::WINDOWS
106
+ UI.success("\nAdding SSH tunnel to port 443!")
107
+ run_command "sudo ssh -f -N -i $(minikube ssh-key) docker@$(minikube ip) -L 443:127.0.0.1:443"
108
+ end
109
+ # run_command "sudo sct hostfile"
110
+ run_command "minikube tunnel &", { out: "/dev/null", err: "/dev/null" } if SctCore::Helper::is_windows? # leave this running detached forever in the background
111
+ UI.success("\n✔️ You can visit your environment at 👉 https://spend-cloud.dev.spend.cloud 👌")
86
112
  end
87
113
 
88
- def delete_pods(args)
89
- run_command "kubectl delete pods #{args.join(" ")}"
114
+ def enable_addons
115
+ enable_addon "registry-creds"
116
+ enable_addon "ingress"
90
117
  end
91
118
 
92
- def apply_pods
93
- run_command "kubectl apply -f ~/development/spend-cloud/k8s/"
119
+ def enable_addon(addon)
120
+ run_command "minikube addons enable #{addon}"
121
+
122
+ deployment = deployments("kube-system").find { |deployment| deployment[:name].include? addon }
123
+
124
+ run_command "kubectl rollout status -n kube-system deployment #{deployment[:name]}"
94
125
  end
95
126
 
96
- def update_config
97
- if SctCore::Helper.operatingSystem == SctCore::Helper::WINDOWS
98
- windows_home_path = SctCore::Helper.windowsHomePath
99
- kube_file_path = windows_home_path+"/.kube/config"
127
+ def wait_for_pods
128
+ UI.important("Waiting for pods to become ready...")
129
+
130
+ previous_lines = 0
100
131
 
101
- if !File.exists?(kube_file_path)
102
- return UI.error("#{kube_file_path} doesn't exist")
132
+ while ! pods.all? { |pod| pod[:ready] }
133
+ pods_status_lines = get_pods_status.to_s.lines.map { |line| line.chomp }
134
+
135
+ current_lines = pods_status_lines.length
136
+
137
+ line_difference = current_lines - previous_lines
138
+
139
+ if line_difference < 0 # there are now less lines than before
140
+ line_difference.abs.times do
141
+ print "\033[1A\033[K" # move the cursor up a line and clear the line
142
+ end
143
+
144
+ print "\033[#{current_lines}A" # move the cursor all the way up to the start of the table
145
+ elsif previous_lines > 0
146
+ print "\033[#{previous_lines}A" # move the cursor all the way up to the start of the table
103
147
  end
104
148
 
105
- run_command "sed -e 's~\\\\~/~g' -e 's~C:~/mnt/c~g' < #{kube_file_path} > ~/.kube/minikube-config"
149
+ pods_status_lines.each do |line|
150
+ print "#{line}\033[K#{$/}" # print the content of the line, clear remaining characters and add a new line
151
+ end
152
+
153
+ previous_lines = current_lines
106
154
 
107
- UI.success("#{kube_file_path} copied to ~/.kube/minikube-config")
155
+ sleep 5
108
156
  end
109
157
 
110
- run_command "kubectl config use-context minikube"
158
+ previous_lines.times do
159
+ print "\033[1A\033[K" # move the cursor up a line and clear the line
160
+ end
111
161
 
112
- run_command "kubectl replace -n kube-system -f #{File.expand_path('resources/corefile.yml', __dir__)}"
113
- old_list = pods("kube-system").map { |pod| pod[:name] if pod[:name].start_with? "coredns" }.compact
114
- run_command "kubectl delete pod -n kube-system #{old_list.join(" ")}" unless old_list.to_a.empty?
115
- run_command "kubectl rollout status -n kube-system deployment/coredns"
162
+ UI.success("Pods are now ready.")
116
163
  end
117
164
 
118
165
  def deployments(namespace = "default")
@@ -150,13 +197,15 @@ module Cluster
150
197
  columns = line.split(" ")
151
198
 
152
199
  name = columns[0]
200
+ ready = columns[1].split("/").reduce { |l, r| l == r }
201
+ replicas = columns[1]
153
202
  status = columns[2]
154
- stalled = status == "ErrImagePull" || status == "ImagePullBackOff"
155
203
 
156
204
  {
157
205
  name: name,
158
- status: status,
159
- stalled: stalled
206
+ ready: ready,
207
+ replicas: replicas,
208
+ status: status
160
209
  }
161
210
  end
162
211
  end
@@ -193,18 +242,6 @@ module Cluster
193
242
  UI.success("Ingress IP is now available.")
194
243
  end
195
244
 
196
- def status
197
- print_contexts
198
- print_minikube_status
199
-
200
- if get_minikube_status.find_all { |status| status[1] == 'Stopped' }.count == 0
201
- print_pods_status("kube-system")
202
- print_pods_status
203
- else
204
- UI.important("Please check your minikube status. If all services are stopped you should start the minikube first.")
205
- end
206
- end
207
-
208
245
  def print_contexts
209
246
  output = `kubectl config get-contexts`
210
247
 
@@ -223,12 +260,11 @@ module Cluster
223
260
  end
224
261
 
225
262
  def print_minikube_status
226
-
227
263
  puts Terminal::Table.new title: "Minikube status".green, headings: ['Name', 'Status'], rows: get_minikube_status
228
264
  end
229
265
 
230
266
  def get_minikube_status
231
- output = `#{SctCore::Helper.minikube} status`
267
+ output = `minikube status`
232
268
 
233
269
  lines = output.split "\n"
234
270
 
@@ -240,25 +276,66 @@ module Cluster
240
276
  end
241
277
 
242
278
  def print_pods_status(namespace = "default")
279
+ output = get_pods_status(namespace)
243
280
 
244
- pods_list = pods(namespace)
281
+ puts output if output
282
+ end
245
283
 
246
- if pods_list.to_a.empty?
247
- return
248
- end
284
+ def get_pods_status(namespace = "default")
285
+ rows = pods(namespace).map do |pod|
286
+ status = pod[:status]
287
+ replicas = pod[:replicas]
249
288
 
250
- rows = pods_list.map do |pod|
251
289
  [
252
290
  pod[:name],
253
- pod[:status] == "Running" ? pod[:status].green : pod[:status].red
291
+ status == "Running" ? status.green : status.red,
292
+ pod[:ready] ? replicas.green : replicas.red
254
293
  ]
255
294
  end
256
295
 
257
- puts Terminal::Table.new title: "Pods (namespace: #{namespace})".green, headings: ['Name', 'Status'], rows: rows
296
+ if rows.empty?
297
+ return
298
+ end
299
+
300
+ return Terminal::Table.new title: "Pods (namespace: #{namespace})".green, headings: ['Name', 'Status', 'Replicas ready'], rows: rows
301
+ end
302
+
303
+ def copy_proactive_accounts_file
304
+ container_name = "proactive-config"
305
+ pod_id = pods().select {|pod| pod[:name].start_with?(container_name)}.first[:name]
306
+
307
+ begin
308
+ UI.important("Checking ProActive accounts file...")
309
+ run_command "kubectl exec #{pod_id} -- test -e /data/proactive_accounts.ini", [:out, :err] => File::NULL
310
+ rescue => e
311
+ UI.important("Copying ProActive accounts file to deployments...")
312
+ src_path = "#{Dir.home()}/development/spend-cloud/k8s/conf/proactive_accounts.ini"
313
+ command = "kubectl cp #{src_path} #{pod_id}:/data/proactive_accounts.ini -c #{container_name}"
314
+ run_command command
315
+ end
316
+
317
+ UI.success("ProActive accounts file is available")
318
+ end
319
+
320
+ def create_keycloak_database_user
321
+ container_name = "mysql-service"
322
+ pod_id = pods().select {|pod| pod[:name].start_with?(container_name)}.first[:name]
323
+
324
+ UI.important("Creating keycloak user")
325
+ create_user = "CREATE USER 'keycloak'@'%' IDENTIFIED BY 'keycloak';"
326
+ run_command 'kubectl exec #{pod_id} -- mysql -e "#{create_user}" >> /dev/null'
327
+
328
+ UI.important("Creating keycloak database")
329
+ create_database = "CREATE DATABASE keycloak CHARACTER SET utf8 COLLATE UTF8_UNICODE_CI;"
330
+ run_command 'kubectl exec #{pod_id} -- mysql -e "#{create_database}" >> /dev/null'
331
+
332
+ UI.important("Granting privileges to keycloak user")
333
+ grant_privileges = "GRANT ALL PRIVILEGES ON keycloak.* TO 'keycloak'@'%';"
334
+ run_command 'kubectl exec #{pod_id} -- mysql -e "#{grant_privileges}" >> /dev/null'
258
335
  end
259
336
 
260
- def run_command command
261
- if ! system command
337
+ def run_command command, options = {}
338
+ if ! system command, options
262
339
  raise command.red
263
340
  end
264
341
  end