1

I'm working on a small script to take some variables, packageName, newVersion. I've googled and tried an awful lot and it brings me to asking.

The script executes as: updatePackage.sh -p weblogic-pkg-1 -v 2.0.1-B3

This reads yaml:

## Weblogic Deployment
   'weblogic-pkg-1':
     ensure:            'present'
     deploymenttype:    'AppDeployment'
     versionidentifier: '2.0.0-B2'
     timeout:           60
     stagingmode:       "nostage"
     remote:            "1"
     upload:            "1"
     target:
       - "%{hiera('package_clustername')}"
     targettype:
       - 'Cluster'
     localpath:         '/opt/releases/staging/applications/weblogic-pkg-1/weblogic-pkg-1-2.0.0-B2.war'

I am trying to get the script to change the version number once it has matched the package, so the desired output is:

## Weblogic Deployment
   'weblogic-pkg-1':
     ensure:            'present'
     deploymenttype:    'AppDeployment'
     versionidentifier: '2.0.1-B3'
     timeout:           60
     stagingmode:       "nostage"
     remote:            "1"
     upload:            "1"
     target:
       - "%{hiera('package_clustername')}"
     targettype:
       - 'Cluster'
     localpath:         '/opt/releases/staging/applications/weblogic-pkg-1/weblogic-pkg-1-2.0.1-B3.war'

The YAML file has about 50 packages so I cannot just sed /version/newVersion sadly. The system is also pretty locked down so adding a yaml parser to the OS might be tricky.

Any tips?

Thanks

Dave

ds2000
  • 11
  • 3
  • Maybe the question is obvious for you, but you should add more details. What exactly are you trying to accomplish? I take what you posted to be the original file, if so then please post the file as you would like it to be modified by `sed` or `awk`. Also post the commands you've been using so others may suggest how to fix them. – simlev Jun 12 '17 at 13:51
  • Thanks for the reply. I've updated the original question so that it is hopefully a little more understandable :) – ds2000 Jun 13 '17 at 06:14

2 Answers2

0

Finally, I managed it with perl. I hope this helps someone else:

#perl -000 -n -i -e "s/versionidentifier: '2.0.0-B1'/versionidentifier: '2.0.0-B3'/m if /\'weblogic-pkg-1\':/; print;" /opt/releases/staging/hiera/dev/deployments/node.corp.yaml

For the directory update, change the delimiter to an @:

perl -000 -n -i -e "s@localpath:         '/opt/releases/staging/applications/weblogic-pkg-1/weblogic-pkg-1-2.0.0-B1.war''@localpath:         '/opt/releases/staging/applications/weblogic-pkg-1/weblogic-pkg-1-2.0.1-B2.war'@m if /\'vet-common-status-service\':/; print;" /opt/releases/staging/hiera/dev/deployments/node.corp.yaml
ds2000
  • 11
  • 3
0

I see you got the hang of it with perl. That's what I would have suggested:

perl -lape "if (/'weblogic-pkg-1':/../localpath:/ and /versionidentifier|localpath:/) {\$a = \$1 if /\s+versionidentifier: '(.+)'/;s/\$a/2.0.1-B3/}" input.txt

If you want to wrap it in a Bash script:

#!/usr/bin/env bash

while getopts ":p:v:" opt; do
  case $opt in
    p) p="$OPTARG"
    ;;
    v) v="$OPTARG"
    ;;
    \?) echo "Invalid option -$OPTARG" >&2
    ;;
  esac
done

perl -lape "if (/'$p':/../localpath:/ and /versionidentifier|localpath:/) {\$a = \$1 if /\s+versionidentifier: '(.+)'/;s/\$a/$v/}" input.txt

Note: I did not include the -i switch because it is dangerous: you should first experiment and tweak the script to your needs, and only then substitute perl -i -lane instead of perl -lane.

simlev
  • 3,782
  • 3
  • 14
  • 33