/
cfn-send-cmd-result
executable file
·94 lines (81 loc) · 4.35 KB
/
cfn-send-cmd-result
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
#!/usr/bin/env python
#==============================================================================
# Copyright 2011 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# 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.
#==============================================================================
from cfnbootstrap.aws_client import Credentials
from cfnbootstrap.sqs_client import SQSClient
from cfnbootstrap import util
from optparse import OptionParser
import os
import sys
try:
import simplejson as json
except ImportError:
import json
parser = OptionParser(usage="usage: %prog [options] [Command Result Data]")
parser.add_option("", "--access-key", help="An AWS Access Key",
type="string", dest="access_key", default=os.environ.get('RESULT_ACCESS_KEY'))
parser.add_option("", "--secret-key", help="An AWS Secret Key",
type="string", dest="secret_key", default=os.environ.get('RESULT_SECRET_KEY'))
parser.add_option("", "--token", help="An AWS Session Token",
type="string", dest="security_token", default=os.environ.get('RESULT_SESSION_TOKEN'))
parser.add_option("-q", "--queue-url", help="SQS Queue URL for storing the command result",
type="string", dest="queue_url", default=os.environ.get('RESULT_QUEUE'))
parser.add_option("-d", "--dispatcher-id", help="The dispatcher ID",
type="string", dest="dispatcher_id", default=os.environ.get('DISPATCHER_ID'))
parser.add_option("-c", "--command-name", help="The command name",
type="string", dest="command_name", default=os.environ.get('CMD_NAME'))
parser.add_option("-i", "--invocation-id", help="The invocation ID",
type="string", dest="invocation_id", default=os.environ.get('INVOCATION_ID'))
parser.add_option("-l", "--listener-id", help="The listener ID",
type="string", dest="listener_id", default=os.environ.get('LISTENER_ID'))
#Optional arguments
parser.add_option("-s", "--success", help="If true, signal success; if false, signal failure. Default: true",
dest="success", action="store", type="choice", choices=["true", "false"], default="true")
parser.add_option("-e", "--exit-code", help="Derive success or failure from specified exit code. Note: This takes precedence over the success flag", dest="exit_code", type="int", action="store")
(options, args) = parser.parse_args()
def fail_on_unspecified(value, name):
if not value:
print >> sys.stderr, "Error: You must specify %s" % name
parser.print_help(sys.stderr)
sys.exit(1)
fail_on_unspecified(options.dispatcher_id, "DispatcherId")
fail_on_unspecified(options.command_name, "CommandName")
fail_on_unspecified(options.invocation_id, "InvocationId")
fail_on_unspecified(options.listener_id, "ListenerId")
fail_on_unspecified(options.queue_url, "QueueUrl")
if (not options.access_key or not options.secret_key or not options.security_token):
print >> sys.stderr, "Error: You must specify a token and an access key/secret key pair"
parser.print_help(sys.stderr)
exit(1)
if options.exit_code == None:
success = options.success == 'true'
else:
success = options.exit_code == 0
result_msg = {
'DispatcherId' : options.dispatcher_id,
'CommandName' : options.command_name,
'ListenerId' : options.listener_id,
'InvocationId' : options.invocation_id,
'Data' : ' '.join(args) if success else '',
'Message' : ' '.join(args) if not success else '',
'Status' : 'SUCCESS' if success else 'FAILURE',
}
client = SQSClient(Credentials(options.access_key, options.secret_key, options.security_token))
try:
client.send_message(options.queue_url,json.dumps(result_msg))
except Exception, e:
print >> sys.stderr, "Error: Could not send command result: " + str(e)
sys.exit(1)