<commit_before>#!/usr/bin/python

#
# Copyright 2014 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.
# A copy of the License is located at
#
#  http://aws.amazon.com/apache2.0
#
# or in the "license" file accompanying this file. This file 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.
#

"""
examples:

easy_deploy.py deploy --application=myapp rolling --stack-name=teststack --layer-name=apiserver --comment="Rolling deployment to all apiservers" --timeout=300

easy_deploy.py deploy --application=myapp instances --stack-name=teststack --hosts=host1,host2 --comment="Deploy to host1 and host2"

easy_deploy.py --profile=dev deploy --application=myapp all --stack-name=teststack --layer-name=appserver --comment="Deploy to all servers" --custom_json="{ \"deploy\" : { \"foobar\" : { \"scm\": { \"revision\" : \"5ed93d9976e65e3826d376e2fa724babdb448365\" } } } }"

easy_deploy.py update --no-allow-reboot rolling --stack-name=teststack --layer-name=apiserver --comment="Rolling patch to all apiservers

easy_deploy.py update --allow-reboot --amazon-linux-release=2014.09 instances --stack-name=teststack --hosts=host1,host2 --comment="Updating host1 and host2 to latest Amazon Linux"

easy_deploy.py update --allow-reboot all --stack-name=teststack --layer-name=appserver --comment="Applying kernel patches to all servers"
"""


import click
import os
import sys
import time
import json
import arrow
import botocore.session


class Operation(object):
    def __init__(self, context):
        self.session = botocore.session.get_session()
        self.service_endpoints = {
            'opsworks': context.obj['OPSWORKS_REGION'],
            'elb': context.obj['ELB_REGION']
        }
        self.clients = {}
        self.stack_name = None
        self.layer_name = None
        self.deploy_timeout = None
        self._stack_id = None
        self._layer_id = None

        self.pre_deployment_hooks = []
        self.post_deployment_hooks = []

    def init(self, stack_name, timeout=None, layer_name=None):
        self.stack_name = stack_name
        self.layer_name = layer_name
        self.deploy_timeout = timeout

    @property
    def stack_id(self):
        if self._stack_id is None:
            stacks = self._make_api_call('opsworks', 'describe_stacks')['Stacks']
            for stack in stacks:
                stack_id = stack['StackId']
                if self.stack_name == stack['Name'].lower():
                    self._stack_id = stack_id
                    break
            else:
                log("Stack {0} not found.  Aborting".format(self.stack_name))
                sys.exit(1)
        return self._stack_id

    @property
    def layer_id(self):
        if self._layer_id is None:
            layers = self._make_api_call('opsworks', 'describe_layers', StackId=self.stack_id)['Layers']
            for each_layer in layers:
                layer_id = each_layer['LayerId']
                if self.layer_name == each_layer['Name'].lower():
                    self._layer_id = layer_id
                    break
            else:
                log("No Layer found with name {0} in stack {1}.  Aborting".format(self.layer_name, self.stack_name))
                sys.exit(1)
        return self._layer_id

    def layer_at_once(self, comment, custom_json, exclude_hosts=None):
        all_instances = self._make_api_call('opsworks', 'describe_instances', LayerId=self.layer_id)

        if exclude_hosts is None:
            exclude_hosts = []

        deployment_instance_ids = []
        for each in all_instances['Instances']:
            if each['Status'] == 'online' and each['Hostname'] not in exclude_hosts:
                deployment_instance_ids.append(each['InstanceId'])
        self._deploy_to(instance_ids=deployment_instance_ids, name="{0} instances".format(self.layer_name), comment=comment, custom_json=custom_json)

    def layer_rolling(self, comment, custom_json):
        load_balancer_names = self._get_opsworks_elb_names()

        if len(load_balancer_names) > 0:
            log("{0} instances are registered to {1} elbs".format(self.layer_name, ", ".join(load_balancer_names)))
            self.pre_deployment_hooks.append(self._remove_instance_from_elb)
            self.post_deployment_hooks.append(self._add_instance_to_elb)

        all_instances = self._make_api_call('opsworks', 'describe_instances', LayerId=self.layer_id)
        for each in all_instances['Instances']:
            if each['Status'] != 'online':
                continue

            hostname = each['Hostname']
            instance_id = each['InstanceId']
            ec2_instance_id = each['Ec2InstanceId']

            self._deploy_to(instance_ids=[instance_id], name=hostname, comment=comment, custom_json=custom_json, load_balancer_names=load_balancer_names, ec2_instance_id=ec2_instance_id)

    def instances_at_once(self, host_names, comment, custom_json):
        all_instances = self._make_api_call('opsworks', 'describe_instances', StackId=self.stack_id)

        deployment_instance_ids = []
        for each in all_instances['Instances']:
            if each['Status'] == 'online' and each['Hostname'] in host_names:
                deployment_instance_ids.append(each['InstanceId'])

        self._deploy_to(instance_ids=deployment_instance_ids, name=", ".join(host_names), comment=comment, custom_json=custom_json)

    def post_elb_registration(self, hostname, load_balancer_names):
        timeout = 0
        describe_result = self._make_api_call('elb', 'describe_load_balancers', LoadBalancerNames=load_balancer_names)
        for load_balancer_desc in describe_result['LoadBalancerDescriptions']:
            healthy_threshold = load_balancer_desc['HealthCheck']['HealthyThreshold']
            interval = load_balancer_desc['HealthCheck']['Interval']

            instance_healthy_wait = (healthy_threshold * interval)
            timeout = max(timeout, instance_healthy_wait)

        log("Added {0} to ELB {1}.  Sleeping for {2} seconds for it to be online".format(hostname, ','.join(load_balancer_names), timeout))<commit_msg>wait 2 more HealthyThresholds for instance to show up as healthy in ELB<commit_after>#!/usr/bin/python

#
# Copyright 2014 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.
# A copy of the License is located at
#
#  http://aws.amazon.com/apache2.0
#
# or in the "license" file accompanying this file. This file 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.
#

"""
examples:

easy_deploy.py deploy --application=myapp rolling --stack-name=teststack --layer-name=apiserver --comment="Rolling deployment to all apiservers" --timeout=300

easy_deploy.py deploy --application=myapp instances --stack-name=teststack --hosts=host1,host2 --comment="Deploy to host1 and host2"

easy_deploy.py --profile=dev deploy --application=myapp all --stack-name=teststack --layer-name=appserver --comment="Deploy to all servers" --custom_json="{ \"deploy\" : { \"foobar\" : { \"scm\": { \"revision\" : \"5ed93d9976e65e3826d376e2fa724babdb448365\" } } } }"

easy_deploy.py update --no-allow-reboot rolling --stack-name=teststack --layer-name=apiserver --comment="Rolling patch to all apiservers

easy_deploy.py update --allow-reboot --amazon-linux-release=2014.09 instances --stack-name=teststack --hosts=host1,host2 --comment="Updating host1 and host2 to latest Amazon Linux"

easy_deploy.py update --allow-reboot all --stack-name=teststack --layer-name=appserver --comment="Applying kernel patches to all servers"
"""


import click
import os
import sys
import time
import json
import arrow
import botocore.session


class Operation(object):
    def __init__(self, context):
        self.session = botocore.session.get_session()
        self.service_endpoints = {
            'opsworks': context.obj['OPSWORKS_REGION'],
            'elb': context.obj['ELB_REGION']
        }
        self.clients = {}
        self.stack_name = None
        self.layer_name = None
        self.deploy_timeout = None
        self._stack_id = None
        self._layer_id = None

        self.pre_deployment_hooks = []
        self.post_deployment_hooks = []

    def init(self, stack_name, timeout=None, layer_name=None):
        self.stack_name = stack_name
        self.layer_name = layer_name
        self.deploy_timeout = timeout

    @property
    def stack_id(self):
        if self._stack_id is None:
            stacks = self._make_api_call('opsworks', 'describe_stacks')['Stacks']
            for stack in stacks:
                stack_id = stack['StackId']
                if self.stack_name == stack['Name'].lower():
                    self._stack_id = stack_id
                    break
            else:
                log("Stack {0} not found.  Aborting".format(self.stack_name))
                sys.exit(1)
        return self._stack_id

    @property
    def layer_id(self):
        if self._layer_id is None:
            layers = self._make_api_call('opsworks', 'describe_layers', StackId=self.stack_id)['Layers']
            for each_layer in layers:
                layer_id = each_layer['LayerId']
                if self.layer_name == each_layer['Name'].lower():
                    self._layer_id = layer_id
                    break
            else:
                log("No Layer found with name {0} in stack {1}.  Aborting".format(self.layer_name, self.stack_name))
                sys.exit(1)
        return self._layer_id

    def layer_at_once(self, comment, custom_json, exclude_hosts=None):
        all_instances = self._make_api_call('opsworks', 'describe_instances', LayerId=self.layer_id)

        if exclude_hosts is None:
            exclude_hosts = []

        deployment_instance_ids = []
        for each in all_instances['Instances']:
            if each['Status'] == 'online' and each['Hostname'] not in exclude_hosts:
                deployment_instance_ids.append(each['InstanceId'])
        self._deploy_to(instance_ids=deployment_instance_ids, name="{0} instances".format(self.layer_name), comment=comment, custom_json=custom_json)

    def layer_rolling(self, comment, custom_json):
        load_balancer_names = self._get_opsworks_elb_names()

        if len(load_balancer_names) > 0:
            log("{0} instances are registered to {1} elbs".format(self.layer_name, ", ".join(load_balancer_names)))
            self.pre_deployment_hooks.append(self._remove_instance_from_elb)
            self.post_deployment_hooks.append(self._add_instance_to_elb)

        all_instances = self._make_api_call('opsworks', 'describe_instances', LayerId=self.layer_id)
        for each in all_instances['Instances']:
            if each['Status'] != 'online':
                continue

            hostname = each['Hostname']
            instance_id = each['InstanceId']
            ec2_instance_id = each['Ec2InstanceId']

            self._deploy_to(instance_ids=[instance_id], name=hostname, comment=comment, custom_json=custom_json, load_balancer_names=load_balancer_names, ec2_instance_id=ec2_instance_id)

    def instances_at_once(self, host_names, comment, custom_json):
        all_instances = self._make_api_call('opsworks', 'describe_instances', StackId=self.stack_id)

        deployment_instance_ids = []
        for each in all_instances['Instances']:
            if each['Status'] == 'online' and each['Hostname'] in host_names:
                deployment_instance_ids.append(each['InstanceId'])

        self._deploy_to(instance_ids=deployment_instance_ids, name=", ".join(host_names), comment=comment, custom_json=custom_json)

    def post_elb_registration(self, hostname, load_balancer_names):
        timeout = 0
        describe_result = self._make_api_call('elb', 'describe_load_balancers', LoadBalancerNames=load_balancer_names)
        for load_balancer_desc in describe_result['LoadBalancerDescriptions']:
            healthy_threshold = load_balancer_desc['HealthCheck']['HealthyThreshold']
            # give the healthy_threshold 2 more intervals to be sure we just don't time out too soon
            healthy_threshold += 2
            interval = load_balancer_desc['HealthCheck']['Interval']

            instance_healthy_wait = (healthy_threshold * interval)
            timeout = max(timeout, instance_healthy_wait)

        log("Added {0} to ELB {1}.  Sleeping for {2} seconds for it to be online".format(hostname, ','.join(load_balancer_names), timeout))
{'fraction_non_alphanumeric': 0.10732984293193717, 'fraction_numerical': 0.01070218663381583, 'mean_word_length': 3.083307136120717, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7641373', 'n_tokens_mistral': 3959, 'n_tokens_neox': 3772, 'n_words': 877}