Class: Controlplane
- Inherits:
-
Object
- Object
- Controlplane
- Defined in:
- lib/core/controlplane.rb
Overview
rubocop:disable Metrics/ClassLength
Instance Attribute Summary collapse
-
#api ⇒ Object
readonly
Returns the value of attribute api.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#gvc ⇒ Object
readonly
Returns the value of attribute gvc.
-
#org ⇒ Object
readonly
Returns the value of attribute org.
Instance Method Summary collapse
-
#apply(data) ⇒ Object
apply.
- #delete_workload(workload, a_gvc = gvc) ⇒ Object
- #fetch_gvc(a_gvc = gvc, a_org = org) ⇒ Object
- #fetch_gvc!(a_gvc = gvc) ⇒ Object
-
#fetch_gvcs ⇒ Object
gvc.
- #fetch_workload(workload) ⇒ Object
- #fetch_workload!(workload) ⇒ Object
- #fetch_workload_deployments(workload) ⇒ Object
-
#fetch_workloads(a_gvc = gvc) ⇒ Object
workload.
- #fetch_workloads_by_org(a_org = org) ⇒ Object
- #find_domain_for(workloads) ⇒ Object
-
#find_domain_route(data) ⇒ Object
domain.
- #get_domain_workload(data) ⇒ Object
- #gvc_delete(a_gvc = gvc) ⇒ Object
- #gvc_query(app_name = config.app) ⇒ Object
- #image_build(image, dockerfile:, build_args: [], push: true) ⇒ Object
- #image_delete(image) ⇒ Object
- #image_login(org_name = config.org) ⇒ Object
- #image_pull(image) ⇒ Object
- #image_push(image) ⇒ Object
- #image_tag(old_tag, new_tag) ⇒ Object
-
#initialize(config) ⇒ Controlplane
constructor
A new instance of Controlplane.
- #log_get(workload:, from:, to:) ⇒ Object
-
#logs(workload:) ⇒ Object
logs.
- #profile_create(profile, token) ⇒ Object
- #profile_delete(profile) ⇒ Object
- #profile_exists?(profile) ⇒ Boolean
-
#profile_switch(profile) ⇒ Object
profile.
-
#query_images(a_gvc = gvc, a_org = org, partial_gvc_match: nil) ⇒ Object
image.
- #query_workloads(workload, a_gvc = gvc, a_org = org, partial_workload_match: false, partial_gvc_match: nil) ⇒ Object
- #set_domain_workload(data, workload) ⇒ Object
- #set_workload_env_var(workload, container:, name:, value:) ⇒ Object
- #set_workload_suspend(workload, value) ⇒ Object
- #workload_connect(workload, location:, container: nil, shell: nil) ⇒ Object
- #workload_deployment_version_ready?(version, next_version, expected_status:) ⇒ Boolean
- #workload_deployments_ready?(workload, expected_status:) ⇒ Boolean
- #workload_exec(workload, location:, container: nil, command: nil) ⇒ Object
- #workload_force_redeployment(workload) ⇒ Object
- #workload_get_replicas(workload, location:) ⇒ Object
- #workload_get_replicas_safely(workload, location:) ⇒ Object
- #workload_set_image_ref(workload, container:, image:) ⇒ Object
Constructor Details
#initialize(config) ⇒ Controlplane
Returns a new instance of Controlplane.
6 7 8 9 10 11 |
# File 'lib/core/controlplane.rb', line 6 def initialize(config) @config = config @api = ControlplaneApi.new @gvc = config.app @org = config.org end |
Instance Attribute Details
#api ⇒ Object (readonly)
Returns the value of attribute api.
4 5 6 |
# File 'lib/core/controlplane.rb', line 4 def api @api end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
4 5 6 |
# File 'lib/core/controlplane.rb', line 4 def config @config end |
#gvc ⇒ Object (readonly)
Returns the value of attribute gvc.
4 5 6 |
# File 'lib/core/controlplane.rb', line 4 def gvc @gvc end |
#org ⇒ Object (readonly)
Returns the value of attribute org.
4 5 6 |
# File 'lib/core/controlplane.rb', line 4 def org @org end |
Instance Method Details
#apply(data) ⇒ Object
apply
277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/core/controlplane.rb', line 277 def apply(data) # rubocop:disable Metrics/MethodLength Tempfile.create do |f| f.write(data.to_yaml) f.rewind cmd = "cpln apply #{gvc_org} --file #{f.path} > /dev/null" if Shell.tmp_stderr cmd += " 2> #{Shell.tmp_stderr.path}" perform(cmd) else perform!(cmd) end end end |
#delete_workload(workload, a_gvc = gvc) ⇒ Object
212 213 214 |
# File 'lib/core/controlplane.rb', line 212 def delete_workload(workload, a_gvc = gvc) api.delete_workload(org: org, gvc: a_gvc, workload: workload) end |
#fetch_gvc(a_gvc = gvc, a_org = org) ⇒ Object
98 99 100 |
# File 'lib/core/controlplane.rb', line 98 def fetch_gvc(a_gvc = gvc, a_org = org) api.gvc_get(gvc: a_gvc, org: a_org) end |
#fetch_gvc!(a_gvc = gvc) ⇒ Object
102 103 104 105 106 107 |
# File 'lib/core/controlplane.rb', line 102 def fetch_gvc!(a_gvc = gvc) gvc_data = fetch_gvc(a_gvc) return gvc_data if gvc_data raise "Can't find app '#{gvc}', please create it with 'cpl setup-app -a #{config.app}'." end |
#fetch_gvcs ⇒ Object
gvc
85 86 87 |
# File 'lib/core/controlplane.rb', line 85 def fetch_gvcs api.gvc_list(org: org) end |
#fetch_workload(workload) ⇒ Object
123 124 125 |
# File 'lib/core/controlplane.rb', line 123 def fetch_workload(workload) api.workload_get(workload: workload, gvc: gvc, org: org) end |
#fetch_workload!(workload) ⇒ Object
127 128 129 130 131 132 |
# File 'lib/core/controlplane.rb', line 127 def fetch_workload!(workload) workload_data = fetch_workload(workload) return workload_data if workload_data raise "Can't find workload '#{workload}', please create it with 'cpl apply-template #{workload} -a #{config.app}'." end |
#fetch_workload_deployments(workload) ⇒ Object
153 154 155 |
# File 'lib/core/controlplane.rb', line 153 def fetch_workload_deployments(workload) api.workload_deployments(workload: workload, gvc: gvc, org: org) end |
#fetch_workloads(a_gvc = gvc) ⇒ Object
workload
115 116 117 |
# File 'lib/core/controlplane.rb', line 115 def fetch_workloads(a_gvc = gvc) api.workload_list(gvc: a_gvc, org: org) end |
#fetch_workloads_by_org(a_org = org) ⇒ Object
119 120 121 |
# File 'lib/core/controlplane.rb', line 119 def fetch_workloads_by_org(a_org = org) api.workload_list_by_org(org: a_org) end |
#find_domain_for(workloads) ⇒ Object
242 243 244 245 246 247 248 249 250 |
# File 'lib/core/controlplane.rb', line 242 def find_domain_for(workloads) domains = api.list_domains(org: org)["items"] domains.find do |domain_data| route = find_domain_route(domain_data) next false if route.nil? workloads.any? { |workload| route["workloadLink"].split("/").last == workload } end end |
#find_domain_route(data) ⇒ Object
domain
232 233 234 235 236 237 238 239 240 |
# File 'lib/core/controlplane.rb', line 232 def find_domain_route(data) port = data["spec"]["ports"].find { |current_port| current_port["number"] == 80 || current_port["number"] == 443 } return nil if port.nil? || port["routes"].nil? route = port["routes"].find { |current_route| current_route["prefix"] == "/" } return nil if route.nil? route end |
#get_domain_workload(data) ⇒ Object
252 253 254 255 |
# File 'lib/core/controlplane.rb', line 252 def get_domain_workload(data) route = find_domain_route(data) route["workloadLink"].split("/").last end |
#gvc_delete(a_gvc = gvc) ⇒ Object
109 110 111 |
# File 'lib/core/controlplane.rb', line 109 def gvc_delete(a_gvc = gvc) api.gvc_delete(gvc: a_gvc, org: org) end |
#gvc_query(app_name = config.app) ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/core/controlplane.rb', line 89 def gvc_query(app_name = config.app) # When `match_if_app_name_starts_with` is `true`, we query for any gvc containing the name, # otherwise we query for a gvc with the exact name. op = config.should_app_start_with?(app_name) ? "~" : "=" cmd = "cpln gvc query --org #{org} -o yaml --prop name#{op}#{app_name}" perform_yaml(cmd) end |
#image_build(image, dockerfile:, build_args: [], push: true) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/core/controlplane.rb', line 46 def image_build(image, dockerfile:, build_args: [], push: true) cmd = "docker build -t #{image} -f #{dockerfile}" build_args.each { |build_arg| cmd += " --build-arg #{build_arg}" } cmd += " #{config.app_dir}" perform!(cmd) image_push(image) if push end |
#image_delete(image) ⇒ Object
55 56 57 |
# File 'lib/core/controlplane.rb', line 55 def image_delete(image) api.image_delete(org: org, image: image) end |
#image_login(org_name = config.org) ⇒ Object
59 60 61 62 63 |
# File 'lib/core/controlplane.rb', line 59 def image_login(org_name = config.org) cmd = "cpln image docker-login --org #{org_name}" cmd += " > /dev/null 2>&1" if Shell.tmp_stderr perform!(cmd) end |
#image_pull(image) ⇒ Object
65 66 67 68 69 |
# File 'lib/core/controlplane.rb', line 65 def image_pull(image) cmd = "docker pull #{image}" cmd += " > /dev/null" if Shell.tmp_stderr perform!(cmd) end |
#image_push(image) ⇒ Object
77 78 79 80 81 |
# File 'lib/core/controlplane.rb', line 77 def image_push(image) cmd = "docker push #{image}" cmd += " > /dev/null" if Shell.tmp_stderr perform!(cmd) end |
#image_tag(old_tag, new_tag) ⇒ Object
71 72 73 74 75 |
# File 'lib/core/controlplane.rb', line 71 def image_tag(old_tag, new_tag) cmd = "docker tag #{old_tag} #{new_tag}" cmd += " > /dev/null" if Shell.tmp_stderr perform!(cmd) end |
#log_get(workload:, from:, to:) ⇒ Object
271 272 273 |
# File 'lib/core/controlplane.rb', line 271 def log_get(workload:, from:, to:) api.log_get(org: org, gvc: gvc, workload: workload, from: from, to: to) end |
#logs(workload:) ⇒ Object
logs
266 267 268 269 |
# File 'lib/core/controlplane.rb', line 266 def logs(workload:) cmd = "cpln logs '{workload=\"#{workload}\"}' --org #{org} -t -o raw --limit 200" perform!(cmd) end |
#profile_create(profile, token) ⇒ Object
25 26 27 28 29 |
# File 'lib/core/controlplane.rb', line 25 def profile_create(profile, token) cmd = "cpln profile create #{profile} --token #{token}" cmd += " > /dev/null" if Shell.tmp_stderr perform!(cmd) end |
#profile_delete(profile) ⇒ Object
31 32 33 34 35 |
# File 'lib/core/controlplane.rb', line 31 def profile_delete(profile) cmd = "cpln profile delete #{profile}" cmd += " > /dev/null" if Shell.tmp_stderr perform!(cmd) end |
#profile_exists?(profile) ⇒ Boolean
20 21 22 23 |
# File 'lib/core/controlplane.rb', line 20 def profile_exists?(profile) cmd = "cpln profile get #{profile} -o yaml" perform_yaml(cmd).length.positive? end |
#profile_switch(profile) ⇒ Object
profile
15 16 17 18 |
# File 'lib/core/controlplane.rb', line 15 def profile_switch(profile) ENV["CPLN_PROFILE"] = profile ControlplaneApiDirect.reset_api_token end |
#query_images(a_gvc = gvc, a_org = org, partial_gvc_match: nil) ⇒ Object
image
39 40 41 42 43 44 |
# File 'lib/core/controlplane.rb', line 39 def query_images(a_gvc = gvc, a_org = org, partial_gvc_match: nil) partial_gvc_match = config.should_app_start_with?(a_gvc) if partial_gvc_match.nil? gvc_op = partial_gvc_match ? "~" : "=" api.query_images(org: a_org, gvc: a_gvc, gvc_op_type: gvc_op) end |
#query_workloads(workload, a_gvc = gvc, a_org = org, partial_workload_match: false, partial_gvc_match: nil) ⇒ Object
134 135 136 137 138 139 140 |
# File 'lib/core/controlplane.rb', line 134 def query_workloads(workload, a_gvc = gvc, a_org = org, partial_workload_match: false, partial_gvc_match: nil) partial_gvc_match = config.should_app_start_with?(a_gvc) if partial_gvc_match.nil? gvc_op = partial_gvc_match ? "~" : "=" workload_op = partial_workload_match ? "~" : "=" api.query_workloads(org: a_org, gvc: a_gvc, workload: workload, gvc_op_type: gvc_op, workload_op_type: workload_op) end |
#set_domain_workload(data, workload) ⇒ Object
257 258 259 260 261 262 |
# File 'lib/core/controlplane.rb', line 257 def set_domain_workload(data, workload) route = find_domain_route(data) route["workloadLink"] = "/org/#{org}/gvc/#{gvc}/workload/#{workload}" api.update_domain(org: org, domain: data["name"], data: data) end |
#set_workload_env_var(workload, container:, name:, value:) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/core/controlplane.rb', line 184 def set_workload_env_var(workload, container:, name:, value:) data = fetch_workload!(workload) data["spec"]["containers"].each do |container_data| next unless container_data["name"] == container container_data["env"].each do |env_data| next unless env_data["name"] == name env_data["value"] = value end end api.update_workload(org: org, gvc: gvc, workload: workload, data: data) end |
#set_workload_suspend(workload, value) ⇒ Object
199 200 201 202 203 204 |
# File 'lib/core/controlplane.rb', line 199 def set_workload_suspend(workload, value) data = fetch_workload!(workload) data["spec"]["defaultOptions"]["suspend"] = value api.update_workload(org: org, gvc: gvc, workload: workload, data: data) end |
#workload_connect(workload, location:, container: nil, shell: nil) ⇒ Object
216 217 218 219 220 221 |
# File 'lib/core/controlplane.rb', line 216 def workload_connect(workload, location:, container: nil, shell: nil) cmd = "cpln workload connect #{workload} #{gvc_org} --location #{location}" cmd += " --container #{container}" if container cmd += " --shell #{shell}" if shell perform!(cmd) end |
#workload_deployment_version_ready?(version, next_version, expected_status:) ⇒ Boolean
157 158 159 160 161 162 163 164 |
# File 'lib/core/controlplane.rb', line 157 def workload_deployment_version_ready?(version, next_version, expected_status:) return false unless version["workload"] == next_version version["containers"]&.all? do |_, container| ready = container.dig("resources", "replicas") == container.dig("resources", "replicasReady") expected_status == true ? ready : !ready end end |
#workload_deployments_ready?(workload, expected_status:) ⇒ Boolean
166 167 168 169 170 171 172 173 174 175 |
# File 'lib/core/controlplane.rb', line 166 def workload_deployments_ready?(workload, expected_status:) deployments = fetch_workload_deployments(workload)["items"] deployments.all? do |deployment| next_version = deployment.dig("status", "expectedDeploymentVersion") deployment.dig("status", "versions")&.all? do |version| workload_deployment_version_ready?(version, next_version, expected_status: expected_status) end end end |
#workload_exec(workload, location:, container: nil, command: nil) ⇒ Object
223 224 225 226 227 228 |
# File 'lib/core/controlplane.rb', line 223 def workload_exec(workload, location:, container: nil, command: nil) cmd = "cpln workload exec #{workload} #{gvc_org} --location #{location}" cmd += " --container #{container}" if container cmd += " -- #{command}" perform!(cmd) end |
#workload_force_redeployment(workload) ⇒ Object
206 207 208 209 210 |
# File 'lib/core/controlplane.rb', line 206 def workload_force_redeployment(workload) cmd = "cpln workload force-redeployment #{workload} #{gvc_org}" cmd += " > /dev/null" if Shell.tmp_stderr perform!(cmd) end |
#workload_get_replicas(workload, location:) ⇒ Object
142 143 144 145 |
# File 'lib/core/controlplane.rb', line 142 def workload_get_replicas(workload, location:) cmd = "cpln workload get-replicas #{workload} #{gvc_org} --location #{location} -o yaml" perform_yaml(cmd) end |
#workload_get_replicas_safely(workload, location:) ⇒ Object
147 148 149 150 151 |
# File 'lib/core/controlplane.rb', line 147 def workload_get_replicas_safely(workload, location:) cmd = "cpln workload get-replicas #{workload} #{gvc_org} --location #{location} -o yaml 2> /dev/null" result = `#{cmd}` $CHILD_STATUS.success? ? YAML.safe_load(result) : nil end |
#workload_set_image_ref(workload, container:, image:) ⇒ Object
177 178 179 180 181 182 |
# File 'lib/core/controlplane.rb', line 177 def workload_set_image_ref(workload, container:, image:) cmd = "cpln workload update #{workload} #{gvc_org}" cmd += " --set spec.containers.#{container}.image=/org/#{config.org}/image/#{image}" cmd += " > /dev/null" if Shell.tmp_stderr perform!(cmd) end |