3

I am currently working on making a CICD script to deploy a complex environment into another environment. We have multiple technology involved and I currently want to optimize this script because it's taking too much time to fetch information on each environment.

In the OpenShift 3.6 section, I need to get the last successful deployment for each application for a specific project. I try to find a quick way to do so, but right now I only found this solution :

oc rollout history dc -n <Project_name>

This will give me the following output

deploymentconfigs "<Application_name>"
REVISION   STATUS    CAUSE
1          Complete  config change
2          Complete  config change
3          Failed    manual change
4          Running   config change

deploymentconfigs "<Application_name2>"
REVISION   STATUS    CAUSE
18         Complete  config change
19         Complete  config change
20         Complete  manual change
21         Failed    config change
....

I then take this output and parse each line to know which is the latest revision that have the status "Complete".

In the above example, I would get this list :

<Application_name> : 2
<Application_name2> : 20

Then for each application and each revision I do :

oc rollout history dc/<Application_name> -n <Project_name> --revision=<Latest_Revision>

In the above example the Latest_Revision for Application_name is 2 which is the latest complete revision not building and not failed. This will give me the output with the information I need which is the version of the ear and the version of the configuration that was used in the creation of the image use for this successful deployment.

But since I have multiple application, this process can take up to 2 minutes per environment.

Would anybody have a better way of fetching the information I required?

Unless I am mistaken, it looks like there are no "one liner" with the possibility to get the information on the currently running and accessible application.

Thanks

wincrasher
  • 103
  • 1
  • 10
  • Do you want to ignore the running application for , and what info do you need exactly besides the config version? – PhilipGough Jul 28 '18 at 18:20
  • The information I require is in some of the Environment info, also in the containers. for instance we are needing the Image, CONFIGMAP_LONGVERSION & PROJECT_LONGVERSION – wincrasher Jul 30 '18 at 13:40

1 Answers1

1

Assuming that the currently active deployment is the latest successful one, you may try the following:

 oc get dc -a --no-headers | awk '{print "oc rollout history dc "$1" --revision="$2}' | . /dev/stdin

It gets a list of deployments, feeds it to awk to extract the name $1 and revision $2, then compiles your command to extract the details, finally sends it to standard input to execute. It may be frowned upon for not using xargs or the like, but I found it easier for debugging (just drop the last part and see the commands printed out).

UPDATE: On second thoughts, you might actually like this one better:

oc get dc -a -o jsonpath='{range .items[*]}{.metadata.name}{"\n\t"}{.spec.template.spec.containers[0].env}{"\n\t"}{.spec.template.spec.containers[0].image}{"\n-------\n"}{end}'

The example output:

daily-checks
        [map[name:SQL_QUERIES_DIR value:daily-checks/]]
        docker-registry.default.svc:5000/ptrk-testing/daily-checks@sha256:b299434622b5f9e9958ae753b7211f1928318e57848e992bbf33a6e9ee0f6d94
-------
jboss-webserver31-tomcat

        registry.access.redhat.com/jboss-webserver-3/webserver31-tomcat7-openshift@sha256:b5fac47d43939b82ce1e7ef864a7c2ee79db7920df5764b631f2783c4b73f044
-------
jtask

        172.30.31.183:5000/ptrk-testing/app-txeq:build
-------
lifebicycle

        docker-registry.default.svc:5000/ptrk-testing/lifebicycle@sha256:a93cfaf9efd9b806b0d4d3f0c087b369a9963ea05404c2c7445cc01f07344a35

You get the idea, with expressions like .spec.template.spec.containers[0].env you can reach for specific variables, labels, etc. Unfortunately the jsonpath output is not available with oc rollout history.

UPDATE 2: You could also use post-deployment hooks to collect the data, if you can set up a listener for the hooks. Hopefully the information you need is inherited by the PODs. More info here: https://docs.openshift.com/container-platform/3.10/dev_guide/deployments/deployment_strategies.html#lifecycle-hooks

ptrk
  • 1,684
  • 1
  • 14
  • 21
  • `oc get dc -a` does not return the right information for example I have application_name that tell me revision 24. But revision 24 is actually a failed revision that gave an error. The active one is the revision 23. – wincrasher Aug 06 '18 at 16:22