aboutsummaryrefslogtreecommitdiff
path: root/copy-tags
blob: 9f8beb9afac94cc6c559282a9b32e0688fcdc9be (plain)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#! /bin/sh
# Copy tags between two EC2 objects.
# Copyright (C) 2012-2014 Sergey Poznyakoff
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

help() {
	cat <<EOT
usage: $0 [OPTIONS] SRC DST
copies tags between from EC2 object SRC to DST

Options:
  -n, --dry-run               do not actually copy tags to DST, show them
                              instead

AWS options:
  -O, --access-key=STRING     set access key to use
  -W, --secret-key=STRING     set secret key to use
  --access-file=NAME          set access file
  -c, --config-file=FILE      use FILE instead of the default configuration
  --region=NAME               set AWS region (availability zone)
  --ssl                       use SSL (HTTPS) connection
  -M, --map=MAPNAME           use this map instead of the default one (implies
                              --translate)
  -x, --translate             translate resource IDs

Filtering and editing options:
  --exclude=REGEXP            exclude tag with names matching REGEXP
  --exclude-value=REGEXP      exclude tags with values matching REGEXP
  --sed=EXPR                  translate keys and values using EXPR
  --filter=FILE               use AWK program FILE to filter tags

The filtering options are applied in the same order as listed above.

Selecting object type:

  -a       SRC and DST are AMIs
  -i       SRC and DST are instances
  -s       SRC and DST are snapshots
  -v       SRC and DST are volumes

Alternatively, both SRC and DST may be specified using the following
notation:
  -r, --resource-id=MAP:ID
where ID is the resource identifier and MAP specifies the map to use
for translation.

Informational options:
  -h, --help                  display this help summary


Send bug reports to <gray@gnu.org>
EOT
}

obj=
prog=
filter=
dry_run=
sexp=
while [ $# -ne 0 ]
do
	case $1 in
	ECLAT_OPTIONS=*)
		eval $1
		shift
		;;
	-n|--dry-run)
		dry_run=1
		shift
		;;
	--exclude=*)
		arg=$(expr "$1" : '--exclude=\(.*\)')
		prog="$prog /^${arg}=/ { next }"
		shift
		;;
	--exclude-value=*)
		arg=$(expr "$1" : '--exclude-value=\(.*\)')
		prog="$prog /=$arg/ { next }"
		shift
		;;
	--filter=*)
		filter=$(expr "$1" : '--filter=\(.*\)')
		shift
		;;
	--sed=*)
		sexp=$(expr "$1" : '--sed=\(.*\)')
		shift
		;;
	-[aisv])
		obj=$1; shift;;
	-h|--help)
		help
		exit 0;;
	-[MOWac]*)
		opt=$(expr "$1" : '-\(.\).*')
	        arg=$(expr "$1" : '-.\(.*\)')
		if [ -z "$arg" ]; then
			shift
			if [ $# -eq 0 ]; then
				echo >&2 "$0: option -$opt requres argument"
				exit 1
			fi
			ECLAT_OPTIONS="$ECLAT_OPTIONS -$opt$1"
		else
			ECLAT_OPTIONS="$ECLAT_OPTIONS $1"
		fi
		shift
		;;
	-x|--access-key=*|--secret-key=*|--access-file=*|--config-file=*|--region=*|--ssl|--map=*)
		ECLAT_OPTIONS="$ECLAT_OPTIONS $1"
		shift
		;;
        -r*|--resource-id=*)
		break;;		
	--)     shift; break;;
	-*)	echo >&2 "$0: unrecognized option $1"
		exit 1
		;;
	*)	break;;
	esac
done	

if [ $# != 2 ]; then
	exec 1>&2
	help
	exit 1
fi

case $1 in
-r*|--resource-id*)
	case $2 in
	-r*|--resource-id*)
		if [ -n "$obj" ]; then
			echo >&2 "$0: conflicting object types: $obj and $1"
			exit 1
		fi
		;;
	*)	echo >&2 "$0: either none or both arguments must have -r prefix"
		exit 1
		;;
	esac
	;;
*)	if [ -z "$obj" ]; then
		obj=-r
	fi
	;;	
esac

source=$1
dest=$2

export ECLAT_OPTIONS

gettags() {
	eclat -e 'if (.DescribeTagsResponse)
		    for (var in .DescribeTagsResponse.tagSet.item)
		      print(var.key,"=",var.value,"\n");' \
	      describe-tags $obj $1
}

puttags() {
	if [ -n "$dry_run" ]; then
		cat -
	else
		eclat create-tags $obj $1 -T -
	fi
}

pipeline="|"
if [ -n "$prog" ]; then
	prog="$prog { print }"
	awk "$prog" < /dev/null || exit 1
        pipeline="\"$pipeline\"|awk \"$prog\"|"
fi	

if [ -n "$sexp" ]; then
	pipeline="${pipeline}sed -e '$sexp'|"
fi

if [ -n "$filter" ]; then
	awk -f $filter < /dev/null || exit 1
	pipeline="\"$pipeline\"awk -f '$filter'|"
fi

#echo $pipeline
#test -n "$dry_run" && set -x

eval gettags $source $pipeline puttags $dest

# EOF

Return to:

Send suggestions and report system problems to the System administrator.