Created
November 22, 2015 02:58
-
-
Save samehmohamed88/48631c00c0bbb79385c4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
This is a script to create instances and set thier auto-scaling for eTech-Campus' | |
live OpsWorks Stack. The Stack is created using the CloudFormation template | |
is this directory. | |
Once the Stack and its single layer are created, | |
""" | |
import boto3 | |
def get_base_instance_dict(): | |
return dict( | |
InstanceType='m3.medium', | |
Os='Amazon Linux 2015.09', | |
AvailabilityZone='us-west-2a', | |
VirtualizationType='paravirtual', | |
Architecture='x86_64', | |
RootDeviceType='instance-store', | |
InstallUpdatesOnBoot=True, | |
EbsOptimized=False, | |
AgentVersion='INHERIT' | |
) | |
def get_instance_dict(autoscale, stackId, layerId, hostname, subnetId): | |
instance_dict = dict( | |
StackId=stackId, | |
LayerIds=[layerId], | |
Hostname=hostname, | |
SubnetId=subnetId, | |
) | |
if autoscale: | |
instance_dict.update(dict(AutoScalingType=autscale)) | |
instance_dict.update(get_base_instance_dict()) | |
return instance_dict | |
def get_existing_loadbased(client, layerId): | |
resp = client.describe_load_based_auto_scaling(LayerIds=[layerId]) | |
return resp['LoadBasedAutoScalingConfigurations'][0] | |
def get_existing_instances(client, layerId): | |
instances = client.describe_instances(LayerId=layerId)['Instances'] | |
return instances | |
def get_existing_timebase(client, layerId): | |
instances = get_existing_instances(client, layerId) | |
instanceIds = [x['InstanceId'] for x in instances] | |
resp = client.describe_time_based_auto_scaling(InstanceIds=instanceIds) | |
config = [x['AutoScalingSchedule'] for x in resp['TimeBasedAutoScalingConfigurations']] | |
return instances, config | |
def get_existing_config(client): | |
layerId = '5ff0e881-88ec-491a-ba14-dd825c5dd651' | |
loadbased_config = get_existing_loadbased(client, layerId) | |
instances, timebased_config = get_existing_timebase(client, layerId) | |
return instances, loadbased_config, timebased_config | |
def chunk(lst, size): | |
return [lst[i:i+size] for i in range(0, len(lst), size)] | |
def create_volume(opsworks, stackId): | |
client = boto3.client('ec2', region_name='us-west-2') | |
volId = client.create_volume( | |
Size=20, | |
AvailabilityZone='us-west-2a', | |
VolumeType='standard' | |
)['VolumeId'] | |
volId = opsworks.register_volume( | |
Ec2VolumeId=volId, | |
StackId=stackId | |
)['VolumeId'] | |
opsworks.update_volume( | |
VolumeId=volId, | |
MountPoint='/opt/codedeploy-agent' | |
) | |
return volId | |
def create_instance(autoscale, client, stackId, layerId, hostname, subnetId): | |
volId = create_volume(client, stackId) | |
instId = client.create_instance( | |
**get_instance_dict(autoscale, stackId, layerId, hostname, subnetId) | |
)['InstanceId'] | |
client.assign_volume( | |
VolumeId=volId, | |
InstanceId=instId | |
) | |
client.start_instance(InstanceId=instId) | |
return instId | |
def create_load_based_auto_scaling(client, layerId, config): | |
client.set_load_based_auto_scaling( | |
LayerId=layerId, | |
Enable=True, | |
UpScaling=config['UpScaling'], | |
DownScaling=config['DownScaling'] | |
) | |
def create_time_based_auto_scaling(client, config, instances): | |
for i in range(len(config)): | |
_config = config[i] | |
instId = instances[0] | |
client.set_time_based_auto_scaling( | |
InstanceId=instId, | |
AutoScalingSchedule=_config | |
) | |
def get_auto_scaling_count(instances): | |
_list = [x.get('AutoScalingType') for x in instances] | |
timers = _list.count('timer') | |
loaders = _list.count('load') | |
twentyfour = len(instances) - timers - loaders | |
return twentyfour, timers, loaders | |
def create_instances(client, instances, layerId, loadbased_config, timebased_config): | |
new_instances = [] | |
stackId = '2ec347fc-6459-4ae1-b331-aab57a7ec16f' | |
subnets = ['subnet-54c4b731', 'subnet-c2c757b5', 'subnet-2e348177'] | |
allday, timers, loaders = get_auto_scaling_count(instances) | |
count = len(instances) | |
size = len(instances)/len(subnets) | |
num = len(instances)/size | |
_list = chunk(range(count), size) | |
for i in range(num): | |
for j in _list[i]: | |
hostname = 'converge-live-be{}'.format(j) | |
if j < allday: | |
new_instances.append(create_instance(None, client, stackId, layerId, hostname, subnets[i])) | |
elif j < allday + timers: | |
new_instances.append(create_instance('timer', client, stackId, layerId, hostname, subnets[i])) | |
else: | |
new_instances.append(create_instance('load', client, stackId, layerId, hostname, subnets[i])) | |
return new_instances | |
def main(): | |
layerId = 'a5e334ac-557a-4b01-9d18-e5ced7c1ac27' | |
client = boto3.client('opsworks', region_name='us-east-1') | |
old_instances, loadbased_config, timebased_config = get_existing_config(client) | |
instances = create_instances(client, old_instances, layerId, loadbased_config, timebased_config) | |
create_load_based_auto_scaling(client, layerId, loadbased_config) | |
create_time_based_auto_scaling(client, timebased_config, instances) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment