|
| 1 | +"use strict"; |
| 2 | +const { OpenShiftClientX } = require("@bcgov/pipeline-cli"); |
| 3 | + |
| 4 | +const getTargetPhases = (env, phases) => { |
| 5 | + let target_phase = []; |
| 6 | + for (const phase in phases) { |
| 7 | + if (env.match(/^(all|transient)$/) && phases[phase].transient) { |
| 8 | + target_phase.push(phase); |
| 9 | + } else if (env === phase) { |
| 10 | + target_phase.push(phase); |
| 11 | + break; |
| 12 | + } |
| 13 | + } |
| 14 | + |
| 15 | + return target_phase; |
| 16 | +}; |
| 17 | + |
| 18 | +module.exports = settings => { |
| 19 | + const phases = settings.phases; |
| 20 | + const options = settings.options; |
| 21 | + const oc = new OpenShiftClientX(Object.assign({ namespace: phases.build.namespace }, options)); |
| 22 | + const target_phases = getTargetPhases(options.env, phases); |
| 23 | + |
| 24 | + target_phases.forEach(k => { |
| 25 | + if (phases.hasOwnProperty(k)) { |
| 26 | + const phase = phases[k]; |
| 27 | + |
| 28 | + let buildConfigs = oc.get("bc", { |
| 29 | + selector: `app=${phase.instance},env-id=${phase.changeId},!shared,github-repo=${oc.git.repository},github-owner=${oc.git.owner}`, |
| 30 | + namespace: phase.namespace, |
| 31 | + }); |
| 32 | + buildConfigs.forEach(bc => { |
| 33 | + if (bc.spec.output.to.kind == "ImageStreamTag") { |
| 34 | + oc.delete([`ImageStreamTag/${bc.spec.output.to.name}`], { |
| 35 | + "ignore-not-found": "true", |
| 36 | + wait: "true", |
| 37 | + namespace: phase.namespace, |
| 38 | + }); |
| 39 | + } |
| 40 | + }); |
| 41 | + |
| 42 | + let deploymentConfigs = oc.get("dc", { |
| 43 | + selector: `app=${phase.instance},env-id=${phase.changeId},env-name=${k},!shared,github-repo=${oc.git.repository},github-owner=${oc.git.owner}`, |
| 44 | + namespace: phase.namespace, |
| 45 | + }); |
| 46 | + deploymentConfigs.forEach(dc => { |
| 47 | + dc.spec.triggers.forEach(trigger => { |
| 48 | + if ( |
| 49 | + trigger.type == "ImageChange" && |
| 50 | + trigger.imageChangeParams.from.kind == "ImageStreamTag" |
| 51 | + ) { |
| 52 | + oc.delete([`ImageStreamTag/${trigger.imageChangeParams.from.name}`], { |
| 53 | + "ignore-not-found": "true", |
| 54 | + wait: "true", |
| 55 | + namespace: phase.namespace, |
| 56 | + }); |
| 57 | + } |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + oc.raw("delete", ["all"], { |
| 62 | + selector: `app=${phase.instance},env-id=${phase.changeId},!shared,github-repo=${oc.git.repository},github-owner=${oc.git.owner}`, |
| 63 | + wait: "true", |
| 64 | + namespace: phase.namespace, |
| 65 | + }); |
| 66 | + oc.raw( |
| 67 | + "delete", |
| 68 | + ["pvc,Secret,configmap,endpoints,RoleBinding,role,ServiceAccount,Endpoints"], |
| 69 | + { |
| 70 | + selector: `app=${phase.instance},env-id=${phase.changeId},!shared,github-repo=${oc.git.repository},github-owner=${oc.git.owner}`, |
| 71 | + wait: "true", |
| 72 | + namespace: phase.namespace, |
| 73 | + }, |
| 74 | + ); |
| 75 | + } |
| 76 | + }); |
| 77 | +}; |
0 commit comments