-
Notifications
You must be signed in to change notification settings - Fork 2
/
migrate.py
139 lines (124 loc) · 4.46 KB
/
migrate.py
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"""
File: migrate.py
Author: Rafal Marguzewicz (PGS Software)
Email: rmarguzewicz@pgs-soft.com
Github:
Description: God to AWS
"""
import boto3
from aws import Aws
from uuid import uuid1
class Migrate(Aws):
uuid = 'z' + str(uuid1())[:8]
""" List method:
all - list instances with possibly filters and can stop or terminate instances
clean - imporve rule : all instance without tag owner - terminate, running without tag stop - stop
0 : pending
16 : running
32 : shutting-down
48 : terminated
64 : stopping
80 : stopped
"""
def __init__(self, params={}):
"""TODO: to be defined1. """
super().__init__(params)
def all(self):
self.ec2()
self.rds()
self.elb()
self.auto_scaling_group()
self.ebs()
def client(self, client_name):
return boto3.client(client_name, region_name=self.region_name)
def availability_zones(self) -> list:
client = self.client('ec2')
response = client.describe_availability_zones()
return [i['ZoneName'] for i in response['AvailabilityZones']]
def ec2(self) -> boto3:
"""Print all instances
:returns: list
"""
print(self.params.region_name)
ec2 = boto3.resource('ec2', region_name=self.region_name)
return ec2.create_instances(
ImageId='ami-0c5199d385b432989',
InstanceType='t2.micro',
MinCount=1,
MaxCount=1,
TagSpecifications=[
{
'ResourceType': 'instance',
'Tags': [
{'Key': 'name', 'Value': 'rmarguzewicz'},
]
},
]
)
def ebs(self):
ec2 = boto3.resource('ec2', region_name=self.region_name)
zones = self.availability_zones()
print(zones)
return ec2.create_volume(
AvailabilityZone=zones[0],
Encrypted=False,
Size=20,
# SnapshotId='string',
VolumeType='standard', # |'io1'|'gp2'|'sc1'|'st1',
DryRun=False,
)
# 'ResourceType': 'customer-gateway'|'dedicated-host'|'dhcp-options'|'image'|'instance'|'internet-gateway'|'network-acl'|'network-interface'|'reserved-instances'|'route-table'|'snapshot'|'spot-instances-request'|'subnet'|'security-group'|'volume'|'vpc'|'vpn-connection'|'vpn-gateway',
def rds(self):
client = boto3.client('rds', region_name=self.region_name)
return client.create_db_instance(
DBName='DBName',
DBInstanceIdentifier=self.uuid,
AllocatedStorage=20,
DBInstanceClass='db.t2.micro',
Engine='mariadb',
MasterUsername='root',
MasterUserPassword='123456789asdfsdfsfjpoiewur',
Tags=[
{
'Key': 'string',
'Value': 'string'
},
],
)
def elb(self):
client = boto3.client('elb', region_name=self.region_name)
return client.create_load_balancer(
LoadBalancerName=self.uuid,
Listeners=[
{
'Protocol': 'HTTP',
'LoadBalancerPort': 80,
'InstancePort': 80,
},
],
AvailabilityZones=['ap-southeast-1a', ],
# SecurityGroups=[ 'string', ],
# Scheme='string',
# Tags=[ { 'Key': 'string', 'Value': 'string' }, ]
)
def auto_scaling_group(self):
client = boto3.client('autoscaling', region_name=self.region_name)
return client.create_auto_scaling_group(
AutoScalingGroupName=self.uuid,
LaunchConfigurationName='launch-configuration-to-autoscaling-rm',
MinSize=2,
MaxSize=2,
AvailabilityZones=['ap-southeast-1a', ],
# LoadBalancerNames=[ 'string', ],
# TargetGroupARNs=[ 'string', ],
# HealthCheckType='string',
# HealthCheckGracePeriod=123,
# PlacementGroup='string',
# VPCZoneIdentifier='string',
# TerminationPolicies=[ 'string', ],
# NewInstancesProtectedFromScaleIn=True|False,
Tags=[
{'Key': 'string', 'Value': 'string', 'PropagateAtLaunch': True},
],
# ServiceLinkedRoleARN='string'
)