forked from tliron/puccini
-
Notifications
You must be signed in to change notification settings - Fork 0
/
puccini-csar
executable file
·138 lines (104 loc) · 3.24 KB
/
puccini-csar
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/bin/bash
set -e
# See:
# https://docs.oasis-open.org/tosca/TOSCA-Simple-Profile-YAML/v1.3/TOSCA-Simple-Profile-YAML-v1.3.html#_Toc302251718
# https://docs.oasis-open.org/tosca/TOSCA-Simple-Profile-YAML/v1.2/TOSCA-Simple-Profile-YAML-v1.2.html#_Toc528072959
# https://docs.oasis-open.org/tosca/TOSCA-Simple-Profile-YAML/v1.1/TOSCA-Simple-Profile-YAML-v1.1.html#_Toc489606742
# https://docs.oasis-open.org/tosca/TOSCA/v1.0/TOSCA-v1.0.html#_Toc356403711
TOOL=$(basename "$0")
TOSCA_META_FILE_VERSION=${TOSCA_META_FILE_VERSION:-1.1}
CSAR_VERSION=${CSAR_VERSION:-1.1}
CREATED_BY=${CREATED_BY:-$TOOL}
ZIP_LEVEL=${ZIP_LEVEL:-0}
if [ "$#" -lt 1 ]; then
cat << EOT
Simple TOSCA CSAR creation tool
Usage:
$TOOL [CSAR file path] [[root directory]]
If "CSAR file path" doesn't have a ".csar" extension, it will be automatically
appended.
If "CSAR file path" already exists, the file will be deleted.
If "root directory" is not specified, the root will be current directory.
Will automatically create and add a "TOSCA-Metadata/TOSCA.meta" file to the
archive.
You can set the "ENTRY_DEFINITIONS" environment variable to specify the path
to your service template YAML file (relative to the root). Otherwise, if
there's a single YAML file in the root it will be used automatically. If
there's more than one YAML file in the root then you *must* explicitly set
"ENTRY_DEFINITIONS".
Other supported environment variables:
TOSCA_META_FILE_VERSION
CSAR_VERSION
CREATED_BY
OTHER_DEFINITIONS
ZIP_LEVEL
EOT
exit 1
fi
# CSAR file
CSAR=$(readlink -f "$1")
if [ "${CSAR##*.}" != csar ]; then
CSAR=$CSAR.csar
fi
if [ -f "$CSAR" ]; then
echo "CSAR already exists, deleting: $CSAR"
rm --force "$CSAR"
fi
# Root directory
ROOT=$(readlink -f "${2:-.}")
if [ ! -d "$ROOT" ]; then
echo "Failed: root not a directory: $ROOT" > /dev/stderr
exit 1
fi
# Zip
pushd "$ROOT" > /dev/null
zip "-$ZIP_LEVEL" --recurse-paths "$CSAR" . > /dev/null
popd > /dev/null
# Entry-Definitions
if [ -z "$ENTRY_DEFINITIONS" ]; then
# Try to automatically set Entry-Definitions
IFS=$'\n'
for F in $(zipinfo -1 "$CSAR"); do
if [ "$(dirname "$F")" == . ]; then
EXT=${F##*.}
if [ "$EXT" == yaml ] || [ "$EXT" == yml ]; then
if [ -n "$ENTRY_DEFINITIONS" ]; then
echo 'Failed: multiple YAML files in root, you must set ENTRY_DEFINITIONS environment variable to choose one' > /dev/stderr
rm --force "$CSAR"
exit 1
fi
ENTRY_DEFINITIONS=$F
fi
fi
done
if [ -z "$ENTRY_DEFINITIONS" ]; then
echo 'Failed: no YAML files in root, you must set ENTRY_DEFINITIONS environment variable to choose one' > /dev/stderr
rm --force "$CSAR"
exit 1
fi
fi
echo "Entry-Definitions: $ENTRY_DEFINITIONS"
# Metadata
TEMP_ROOT=$(mktemp --directory)
function the_end () {
local ERR=$?
rm --recursive "$TEMP_ROOT"
if [ "$ERR" == 0 ]; then
echo "CSAR created: $CSAR"
else
echo "Failed" > /dev/stderr
rm --force "$CSAR"
exit $ERR
fi
}
trap the_end EXIT
cd "$TEMP_ROOT"
mkdir TOSCA-Metadata
cat << EOT >> TOSCA-Metadata/TOSCA.meta
TOSCA-Meta-File-Version: $TOSCA_META_FILE_VERSION
CSAR-Version: $CSAR_VERSION
Created-By: $CREATED_BY
Entry-Definitions: $ENTRY_DEFINITIONS
Other-Definitions: $OTHER_DEFINITIONS
EOT
zip "-$ZIP_LEVEL" --recurse-paths "$CSAR" . > /dev/null