forked from StackStorm/st2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupgrade-mistral-wf-0.8
executable file
·78 lines (68 loc) · 2.2 KB
/
upgrade-mistral-wf-0.8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env bash
#
## Mistral YAQL Migration tool
#
# This tool inspects a given Mistral workflow, parses it using a few
# regex filters, and attempts to rewrite the file to adhere to the
# updated YAQL syntax introduced in https://review.openstack.org/#/c/157741/
#
# There appear to be two different ways that the old style YAQL is
# represented in workflows. This includes:
# * bare - (node: $.node)
# * interpolated - (node: 'Node: {$.node}')
# * functions - (node: range(0, $.nodes).list() )
#
# This command runs sed with two passes attempting to clean up a given
# workflow. This command will also ensure a backup exists in the event
# that things do not go as expected to prevent data loss.
#######################################################################
function usage {
echo "Usage: `basename $0` <mistral file>"
}
# Exit Codes
OK=0
E_BADARGS=2
E_NOTAFILE=3
E_UNKNOWNOS=4
E_SEDMISSING=5
# Let's figure out what OS we're on to pass the right flags
# to sed. Silly GNU vs BSD!
if [[ "$OSTYPE" == "linux-gnu" ]]; then
SED_ARGS=""
elif [[ "$OSTYPE" == "darwin"* ]]; then
SED_ARGS="-E"
else
echo "Not sure how best to handle sed on your system... unknown OS"
exit $E_UNKNOWNOS
fi
SED=`which sed`
if [[ $? -ne 0 ]]; then
echo "sed not found on this system... exiting"
exit $E_SEDMISSING
fi
# Check for a proper number of command line args:
EXPECTED_ARGS=1
if [[ $# -ne $EXPECTED_ARGS ]]; then
usage
exit $E_BADARGS
else
WORKFLOW=$1
fi
# Make sure any arguments are actually files that exist
if [[ ! -f $WORKFLOW ]]; then
echo "Argument supplied is not a file..."
usage
exit $E_NOTAFILE
fi
# Great, got this far. Let's actually start to slice and dice.
## First, make a backup
cp $WORKFLOW ${WORKFLOW}.bak
## Pass through in phases, because a few too many variables to make a master regex.
cat $WORKFLOW | \
sed $SED_ARGS 's/{([][_$.0-9a-z+\s]+)}/<% \1 %>/g' | \
sed $SED_ARGS 's/([][_$.0-9a-z]+\(.*\))/<% \1 %>/g' | \
sed $SED_ARGS 's/(\$\.[][a-z0-9_.]+)/<% \1 %>/g' | \
sed $SED_ARGS 's/<% (<% [][a-z0-9._$]+ %>) %>/\1/g' | \
sed $SED_ARGS 's/<% ([][$.a-z0-9_]+) %>\)/\1)/g' > ${WORKFLOW}.new
## And put our file back where it belongs...
mv ${WORKFLOW}.new $WORKFLOW