-
Notifications
You must be signed in to change notification settings - Fork 16
/
copy-tags.sh
executable file
·65 lines (60 loc) · 2.1 KB
/
copy-tags.sh
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
# Copyright 2013 Orange Software S.L. (OSOCO)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#!/bin/bash
# Copy the tags from a given object to another one. Note that Amazon CFN tags will be tail
# truncated using ':' as the separator character
# Depends on:
# - aws-common.sh
# - ec2-api-tools
# Author: [email protected] - OSOCO
COMMON_SCRIPT_PATH="`dirname $0`/aws-common.sh"
if [ -f "$COMMON_SCRIPT_PATH" ] ; then
source $COMMON_SCRIPT_PATH
else
echo "aws-common.sh not found in $COMMON_SCRIPT_PATH... Exiting now" >&2
exit 1
fi
USAGE_DESCRIPTION="Usage: `basename $0` -o OBJECT_WITH_TAGS -n OBJECT_TO_COPY_TAGS $EC2_PARAMS_DESC"
function parse_params
{
while getopts ":n:o:$EC2_PARAMS_OPTS" opt; do
case $opt in
o)
OBJECT_WITH_TAGS="$OPTARG"
;;
n)
OBJECT_TO_COPY_TAGS="$OPTARG"
;;
\?)
print_error "Unknown option -$OPTARG"
usage "$USAGE_DESCRIPTION"
;;
*)
parse_common_ec2_param "$opt" "$OPTARG"
;;
esac
done
}
parse_params $@
check_given_mandatory_params OBJECT_WITH_TAGS OBJECT_TO_COPY_TAGS
TAG_CMD="ec2-describe-tags -F resource-id=\"$OBJECT_WITH_TAGS\" | awk '{print \$4\"=\"\$5}'"
execute "TAGS_TO_COPY" "$TAG_CMD"
IFS=$'\n'
for tag in $TAGS_TO_COPY ; do
TAG_KEY="$(echo $tag | cut -d'=' -f1 | awk -F':' '{print $NF}' | sed 's/-/\\-/g')"
TAG_VALUE="$(echo $tag | cut -d'=' -f2 | sed 's/-/\\-/g')"
TAGS_STRING="$TAGS_STRING --tag $TAG_KEY=$TAG_VALUE"
done
TAG_CMD="ec2-create-tags $TAGS_STRING $OBJECT_TO_COPY_TAGS"
execute TAG_OUTPUT "$TAG_CMD"