-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.py
110 lines (99 loc) · 4.12 KB
/
create.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
import boto3
import json
import yaml
import botocore
from botocore.exceptions import ClientError
class CreateManager:
def __init__(self):
self.s3_client = boto3.client('s3')
self.ec2_client = boto3.client('ec2')
self.iam_client = boto3.client('iam')
def create_instance(self, args):
if args.get('file'):
with open(args['file'], 'r') as f:
data = yaml.safe_load(f)
instances = data.get('instances')
if instances:
for instance in instances:
self.create_instance(instance)
else:
print("No instance specifications found in the YAML file.")
else:
instance_name = args.get('instance_name') or 'default-name'
response = self.ec2_client.run_instances(
ImageId=args['image_id'],
InstanceType=args['instance_type'],
MinCount=1,
MaxCount=1,
TagSpecifications=[
{
'ResourceType': 'instance',
'Tags': [
{
'Key': 'Name',
'Value': instance_name or ''
},
]
},
]
)
print(f"Created instance with id {args['image_id']} and name: {args['instance_name']}")
def create_bucket(self, args):
bucket_name = args.get('bucket_name')
region = args.get('region')
if bucket_name and region:
location_constraint = self.get_location_constraint(region)
try:
if location_constraint:
response = self.s3_client.create_bucket(
Bucket=bucket_name,
CreateBucketConfiguration={
'LocationConstraint': location_constraint
}
)
else:
response = self.s3_client.create_bucket(Bucket=bucket_name)
print(f"Created bucket {bucket_name} in region {region}")
except botocore.exceptions.ClientError as e:
error_code = e.response['Error']['Code']
error_message = e.response['Error']['Message']
if error_code == 'BucketAlreadyExists':
print(f"Bucket {bucket_name} already exists.")
else:
print(f"Error occurred while creating the bucket: {error_message}")
except Exception as e:
print(f"Error occurred while creating the bucket: {str(e)}")
else:
print("Please provide both 'bucket_name' and 'region' arguments.")
def create_iam_user(self, user_data):
user_name = user_data.get('user_name')
try:
self.iam_client.create_user(UserName=user_name)
print(f"Created IAM user: {user_name}")
except ClientError as e:
if e.response['Error']['Code'] == 'EntityAlreadyExists':
print(f"IAM user {user_name} already exists.")
else:
print(f"Error creating IAM user {user_name}: {str(e)}")
def create_iam_role(self, role_name, assume_role_policy):
try:
response = self.iam_client.create_role(
RoleName=role_name,
AssumeRolePolicyDocument=json.dumps(assume_role_policy)
)
print(f"Created IAM role: {role_name}")
except Exception as e:
print(f"Error occurred while creating IAM role: {str(e)}")
def create_iam_policy(self, args):
with open(args['policy_document'], 'r') as f:
policy_document = f.read()
response = self.iam_client.create_policy(
PolicyName=args['policy_name'],
PolicyDocument=policy_document
)
print(f"Created IAM policy: {args['policy_name']}")
def get_location_constraint(self, region):
if region == 'us-east-1':
return ''
else:
return region