def create_monitor_instance(stackName, client, layerName='Monitor',
                        hostname=None):
    stackId = stackId_from_name(stackName)
    if stackId is None:
        print("Can't find stack %s; nothing done" % stackName)
        return False

    layerId = layerId_from_stackId_and_name(stackId, layerName)
    if layerId is None:
        print("Can't find layer %s in stack %s; nothing done" %
              (layerName, stackName))
        return False

    host_name = 'monitor'
    try:
        result = client.create_instance(

                  # Required parameters
                  StackId=stackId,
                  LayerIds=[layerId],
                  InstanceType='t2.micro',

                  # Optional parameters
                  Hostname = host_name,
                  # SshKeyName='string',
                  Architecture = 'x86_64',
                  )
        print("instance in layer %s of stack %s created" %
              (layerName, stackName))
        print(result)
    except botocore.exceptions.ClientError as e:
        print e
        return False
    return True
def create_app_layer(stackName, client):
    stackId = stackId_from_name(stackName)
    if stackId is None:
        print("Can't find stack %s; nothing done" % stackName)
        return False

    try:
        layerName = 'PHP App Servers'
        result = client.create_layer(
            # Required parameters
            StackId=stackId,
            Type = 'php-app',
            Name = layerName,
            Shortname = layerName,

            # Optional parameters
            CustomRecipes =  {
                'Setup': [   'mash::devpkgs',
                             'mash::phpcomposer',
                             'mash::cachetsetup'],
                }
            )
        layerId = layerId_from_stackId_and_name(stackId, layerName)
        client.set_load_based_auto_scaling(LayerId=layerId)
        print(result)
    except botocore.exceptions.ClientError as e:
        print e
        return False
    return True
def create_app_layer(stackName, client):
    stackId = stackId_from_name(stackName)
    if stackId is None:
        print("Can't find stack %s; nothing done" % stackName)
        return False

    try:
        layerName = 'PHP App Servers'
        result = client.create_layer(
            # Required parameters
            StackId=stackId,
            Type='php-app',
            Name=layerName,
            Shortname=layerName,

            # Optional parameters
            CustomRecipes={
                'Setup':
                ['mash::devpkgs', 'mash::phpcomposer', 'mash::cachetsetup'],
            })
        layerId = layerId_from_stackId_and_name(stackId, layerName)
        client.set_load_based_auto_scaling(LayerId=layerId)
        print(result)
    except botocore.exceptions.ClientError as e:
        print e
        return False
    return True
def create_app_instance(stackName,
                        client,
                        layerName='PHP App Servers',
                        hostname=None,
                        availabilityZone='us-east-1a'):
    global LAST_APP_NUM
    stackId = stackId_from_name(stackName)
    if stackId is None:
        print("Can't find stack %s; nothing done" % stackName)
        return False

    layerId = layerId_from_stackId_and_name(stackId, layerName)
    if layerId is None:
        print("Can't find layer %s in stack %s; nothing done" %
              (layerName, stackName))
        return False

    host_name = 'cachet-app%d' % LAST_APP_NUM
    try:
        result = client.create_instance(

            # Required parameters
            StackId=stackId,
            LayerIds=[layerId],
            InstanceType='t2.micro',

            # Optional parameters
            Hostname=host_name,
            # SshKeyName='string',
            Architecture='x86_64',
            AvailabilityZone=availabilityZone,
        )
        print("host %s created" % host_name)
        LAST_APP_NUM += 1
        instanceId = result['InstanceId']
        print(result)
    except botocore.exceptions.ClientError as e:
        print(e)
        return False
    return True
def create_app_instance(stackName, client, layerName='PHP App Servers',
                        hostname=None):
    global LAST_APP_NUM
    stackId = stackId_from_name(stackName)
    if stackId is None:
        print("Can't find stack %s; nothing done" % stackName)
        return False

    layerId = layerId_from_stackId_and_name(stackId, layerName)
    if layerId is None:
        print("Can't find layer %s in stack %s; nothing done" %
              (layerName, stackName))
        return False

    host_name = 'cachet-app%d' % LAST_APP_NUM
    try:
        result = client.create_instance(

                  # Required parameters
                  StackId=stackId,
                  LayerIds=[layerId],
                  InstanceType='t2.micro',

                  # Optional parameters
                  Hostname = host_name,
                  # SshKeyName='string',
                  Architecture = 'x86_64',
                  )
        LAST_APP_NUM += 1
        instanceId = result['InstanceId']
        print("instance %s in layer %s of stack %s created" %
              (instanceId, layerName, stackName))
        register_app_instance(instanceId, elb_client)
        print(result)
    except botocore.exceptions.ClientError as e:
        print e
        return False
    return True
If no layer name given use %s.""" %
          (os.path.basename(sys.argv[0]), stackName, layerName))
    sys.exit(1)

if len(sys.argv) >= 2: stackName = sys.argv[1]
if len(sys.argv) >= 3: layerName = sys.argv[2]
if len(sys.argv) == 4 and sys.argv[3] == 'full':
    full = True

stackId = stackId_from_name(stackName)
if stackId is None:
    print("Didn't find stack %s" % stackName)
    sys.exit(2)

layerId = layerId_from_stackId_and_name(stackId, layerName)
if layerId is None:
    print("Didn't find layer %s in stack %s" % (layerName, stackName))
    sys.exit(2)

resp = ops_client.describe_instances(LayerId=layerId)

if full:
    import pprint
    pp = pprint.PrettyPrinter(indent=4)
    pp.pprint(resp)
else:
    hosts = [instance['Hostname'] for instance in resp['Instances']]
    print(', '.join(hosts))

sys.exit(0)
if not (1 < len(sys.argv) <= 2):
    print("""usage:
%s *layerName* [*StackName*]

Delete OpsWorks layer *layerName in stack *StackName*. If no
*StackName* is given we use BotoTest
""" % os.path.basename(sys.argv[0]))
    sys.exit(1)

layerName = sys.argv[1]
stackName = 'BotoTest'
if len(sys.argv) == 3: stackName = sys.argv[2]

if stackName not in stackNames():
    print("Stack %s doesn't exists, nothing done" % stackName)
    sys.exit(2)

stackId = stackId_from_name(stackName)
if stackId is None:
    print("Can't find stack with name %s" % stackName)
    sys.exit(3)

layerId = layerId_from_stackId_and_name(stackId, layerName)
if layerId is None:
    print("Can't find layer in stack %s with name %s" % (layerName, stackName))
    sys.exit(4)

print("Layer %s of %s deleted" % (layerName, stackName))
result = ops_client.delete_layer(LayerId=layerId)
print(result)