lib/core/controlplane.rb in cpl-0.6.0 vs lib/core/controlplane.rb in cpl-0.7.0
- old
+ new
@@ -33,14 +33,17 @@
perform!(cmd)
end
# image
- def image_build(image, dockerfile:, push: true)
- cmd = "cpln image build --org #{org} --name #{image} --dir #{config.app_dir} --dockerfile #{dockerfile}"
- cmd += " --push" if push
+ 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
def image_query(app_name = config.app, org_name = config.org)
cmd = "cpln image query --org #{org_name} -o yaml --max -1 --prop repository=#{app_name}"
perform_yaml(cmd)
@@ -95,11 +98,11 @@
def fetch_gvc!(a_gvc = gvc)
gvc_data = fetch_gvc(a_gvc)
return gvc_data if gvc_data
- raise "Can't find GVC '#{gvc}', please create it with 'cpl setup gvc -a #{config.app}'."
+ raise "Can't find app '#{gvc}', please create it with 'cpl setup-app -a #{config.app}'."
end
def gvc_delete(a_gvc = gvc)
api.gvc_delete(gvc: a_gvc, org: org)
end
@@ -120,13 +123,19 @@
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 setup #{workload} -a #{config.app}'."
+ raise "Can't find workload '#{workload}', please create it with 'cpl apply-template #{workload} -a #{config.app}'."
end
+ def query_workloads(workload, partial_match: false)
+ op = partial_match ? "~" : "="
+
+ api.query_workloads(org: org, gvc: gvc, workload: workload, op_type: op)
+ end
+
def workload_get_replicas(workload, location:)
cmd = "cpln workload get-replicas #{workload} #{gvc_org} --location #{location} -o yaml"
perform_yaml(cmd)
end
@@ -134,40 +143,53 @@
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
+ def fetch_workload_deployments(workload)
+ api.workload_deployments(workload: workload, gvc: gvc, org: org)
+ end
+
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
- def workload_set_suspend(workload, value)
+ 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
+
+ def set_workload_suspend(workload, value)
+ data = fetch_workload!(workload)
data["spec"]["defaultOptions"]["suspend"] = value
- apply(data)
+
+ api.update_workload(org: org, gvc: gvc, workload: workload, data: data)
end
def workload_force_redeployment(workload)
cmd = "cpln workload force-redeployment #{workload} #{gvc_org}"
cmd += " > /dev/null" if Shell.tmp_stderr
perform!(cmd)
end
- def workload_delete(workload)
- cmd = "cpln workload delete #{workload} #{gvc_org}"
- cmd += " 2> /dev/null"
- perform(cmd)
+ def delete_workload(workload)
+ api.delete_workload(org: org, gvc: gvc, workload: workload)
end
- def workload_delete!(workload)
- cmd = "cpln workload delete #{workload} #{gvc_org}"
- perform!(cmd)
- end
-
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)
@@ -176,9 +198,43 @@
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
+
+ # domain
+
+ 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
+
+ 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
+
+ def get_domain_workload(data)
+ route = find_domain_route(data)
+ route["workloadLink"].split("/").last
+ end
+
+ 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
# logs
def logs(workload:)