lib/core/controlplane.rb in cpl-1.1.2.rc.0 vs lib/core/controlplane.rb in cpl-1.1.2

- old
+ new

@@ -22,17 +22,17 @@ perform_yaml(cmd).length.positive? end def profile_create(profile, token) cmd = "cpln profile create #{profile} --token #{token}" - cmd += " > /dev/null" if Shell.tmp_stderr + cmd += " > /dev/null" if Shell.should_hide_output? perform!(cmd) end def profile_delete(profile) cmd = "cpln profile delete #{profile}" - cmd += " > /dev/null" if Shell.tmp_stderr + cmd += " > /dev/null" if Shell.should_hide_output? perform!(cmd) end # image @@ -59,29 +59,29 @@ api.image_delete(org: org, image: image) end def image_login(org_name = config.org) cmd = "cpln image docker-login --org #{org_name}" - cmd += " > /dev/null 2>&1" if Shell.tmp_stderr + cmd += " > /dev/null 2>&1" if Shell.should_hide_output? perform!(cmd) end def image_pull(image) cmd = "docker pull #{image}" - cmd += " > /dev/null" if Shell.tmp_stderr + cmd += " > /dev/null" if Shell.should_hide_output? perform!(cmd) end def image_tag(old_tag, new_tag) cmd = "docker tag #{old_tag} #{new_tag}" - cmd += " > /dev/null" if Shell.tmp_stderr + cmd += " > /dev/null" if Shell.should_hide_output? perform!(cmd) end def image_push(image) cmd = "docker push #{image}" - cmd += " > /dev/null" if Shell.tmp_stderr + cmd += " > /dev/null" if Shell.should_hide_output? perform!(cmd) end # gvc @@ -146,11 +146,15 @@ cmd = "cpln workload get-replicas #{workload} #{gvc_org} --location #{location} -o yaml" perform_yaml(cmd) end def workload_get_replicas_safely(workload, location:) - cmd = "cpln workload get-replicas #{workload} #{gvc_org} --location #{location} -o yaml 2> /dev/null" + cmd = "cpln workload get-replicas #{workload} #{gvc_org} --location #{location} -o yaml" + cmd += " 2> /dev/null" if Shell.should_hide_output? + + Shell.debug("CMD", cmd) + result = `#{cmd}` $CHILD_STATUS.success? ? YAML.safe_load(result) : nil end def fetch_workload_deployments(workload) @@ -178,11 +182,11 @@ 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 + cmd += " > /dev/null" if Shell.should_hide_output? perform!(cmd) end def set_workload_env_var(workload, container:, name:, value:) data = fetch_workload!(workload) @@ -206,11 +210,11 @@ 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 + cmd += " > /dev/null" if Shell.should_hide_output? perform!(cmd) end def delete_workload(workload, a_gvc = gvc) api.delete_workload(org: org, gvc: a_gvc, workload: workload) @@ -278,44 +282,80 @@ # apply def apply_template(data) # rubocop:disable Metrics/MethodLength Tempfile.create do |f| f.write(data) f.rewind - cmd = "cpln apply #{gvc_org} --file #{f.path} > /dev/null" + cmd = "cpln apply #{gvc_org} --file #{f.path}" if Shell.tmp_stderr - cmd += " 2> #{Shell.tmp_stderr.path}" - perform(cmd) + cmd += " 2> #{Shell.tmp_stderr.path}" if Shell.should_hide_output? + + Shell.debug("CMD", cmd) + + result = `#{cmd}` + $CHILD_STATUS.success? ? parse_apply_result(result) : false else - perform!(cmd) + Shell.debug("CMD", cmd) + + result = `#{cmd}` + $CHILD_STATUS.success? ? parse_apply_result(result) : exit(false) end end end - def apply_hash(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) + def apply_hash(data) + apply_template(data.to_yaml) + end + + def parse_apply_result(result) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity + items = [] + + lines = result.split("\n") + lines.each do |line| + # The line can be in one of these formats: + # - "Created /org/shakacode-open-source-examples/gvc/my-app-staging" + # - "Created /org/shakacode-open-source-examples/gvc/my-app-staging/workload/redis" + # - "Updated gvc 'tutorial-app-test-1'" + # - "Updated workload 'redis'" + if line.start_with?("Created") + matches = line.match(%r{Created\s/org/[^/]+/gvc/([^/]+)($|(/([^/]+)/([^/]+)$))})&.captures + next unless matches + + app, _, __, kind, name = matches + if kind + items.push({ kind: kind, name: name }) + else + items.push({ kind: "app", name: app }) + end else - perform!(cmd) + matches = line.match(/Updated\s([^\s]+)\s'([^\s]+)'$/)&.captures + next unless matches + + kind, name = matches + kind = "app" if kind == "gvc" + items.push({ kind: kind, name: name }) end end + + items end private def perform(cmd) + Shell.debug("CMD", cmd) + system(cmd) end def perform!(cmd) + Shell.debug("CMD", cmd) + system(cmd) || exit(false) end def perform_yaml(cmd) + Shell.debug("CMD", cmd) + result = `#{cmd}` $CHILD_STATUS.success? ? YAML.safe_load(result) : exit(false) end def gvc_org