#!/usr/bin/python2.7
#==============================================================================
# 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 import util
from cfnbootstrap.cfn_client import CloudFormationClient
from optparse import OptionParser
import cfnbootstrap
import os
import sys
parser = OptionParser()
parser.add_option_group(util.get_cred_options(parser))
parser.add_option_group(util.get_proxy_options(parser))
parser.add_option("-s", "--stack", help="A CloudFormation stack",
type="string", dest="stack_name", default=os.environ.get('STACK_NAME'))
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'))
parser.add_option("-u", "--url", help="The CloudFormation service URL. The endpoint URL must match the region option. Use of this parameter is discouraged.",
type="string", dest="endpoint")
parser.add_option("", "--region", help="The CloudFormation region. Default: us-east-1.",
type="string", dest="region", default="us-east-1")
parser.add_option("-v", "--verbose", help="Enables verbose logging",
action="store_true", dest="verbose")
(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.stack_name, "StackName")
fail_on_unspecified(options.command_name, "CommandName")
fail_on_unspecified(options.invocation_id, "InvocationId")
fail_on_unspecified(options.listener_id, "ListenerId")
cfnbootstrap.configureLogging("DEBUG" if options.verbose else "INFO")
creds = util.get_creds_or_die(options)
url = CloudFormationClient.endpointForRegion(options.region)
if options.endpoint:
url = options.endpoint
client = CloudFormationClient(creds, url=url, region=options.region, proxyinfo=util.get_proxyinfo(options))
try:
leader = client.elect_command_leader(options.stack_name,
options.command_name,
options.invocation_id,
options.listener_id)
sys.exit(0 if leader == options.listener_id else 5)
except IOError, e:
if e.strerror:
print >> sys.stderr, e.strerror
else:
print >> sys.stderr, "Unknown error electing command leader"
sys.exit(1)
@KyuuKazami