-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaws.py
executable file
·110 lines (92 loc) · 3.46 KB
/
aws.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
"""
File: aws.py
Author: Rafal Marguzewicz (PGS Software)
Email: [email protected]
Gitlab:
Description: Coore super class for class services AWS
ami_filter = [
{"Name": "platform", 'Values': ["windows"]},
{"Name": "virtualization-type", 'Values': ["hvm"]},
{"Name": "image-type", 'Values': ["machine"]},
{"Name": "architecture", 'Values': ["x86_64"]},
{"Name": "state", 'Values': ["available"]},
{'Name': 'tag:stop', 'Values': ['false'] },
{"Name": "is-public", 'Values': ['false']},
{'Name': 'instance-state-name', 'Values': ['running'] },
]
ami_owners = ['amazon']
"""
from typing import TypeVar, Union
import logging
from os import environ
StrNone = Union[str, None]
AnyStr = TypeVar('AnyStr', bytes, str)
log = logging.getLogger(__name__)
log.addHandler(logging.StreamHandler())
log.setLevel(logging.INFO)
class Aws(object):
"""Command line app to manage AWS PGS Sandox """
f = {
'running': {'Name': 'instance-state-name', 'Values': ['running']},
"dont_stop": {'Name': 'tag:stop', 'Values': ['false']},
"without_owner": {'Name': 'tag:Owner', 'Values': ['*']}
}
filters: list = []
# defaul region where you work
region_name = 'ap-southeast-1'
regions = [
'sa-east-1',
'ca-central-1',
'eu-west-3', 'eu-west-2', 'eu-west-1', 'eu-central-1',
'ap-south-1', 'ap-northeast-2', 'ap-northeast-1', 'ap-southeast-1', 'ap-southeast-2',
'us-east-1', 'us-east-2', 'us-west-1', 'us-west-2'
]
# region to tests https://ap-southeast-1.console.aws.amazon.com/ec2/v2/home?region=ap-southeast-1
if environ.get('TESTMODE'):
regions = [
'ap-southeast-1',
]
print('TESTMODE on')
def __init__(self, params={}):
"""Preconfiguring filters """
# from file settings (maybe in future)
# from command line
if params is not None:
self.params = params
if self.params.region_name:
self.region_name = self.params.region_name
self.regions = [self.region_name]
else:
self.params.region_name = self.region_name
if params.running:
self.filters.append(self.f['running'])
if params.without_owner:
self.filters.append(self.f['without_owner'])
if params.tags:
self.filters.append(self.f['dont_stop'])
def set_filter(self, **kwargs):
"""factory filters """
filters = []
options = {'running': False, 'dont_stop': False}
options.update(kwargs)
if options['running']:
filters.append(self.f['running'])
if options['dont_stop']:
filters.append(self.f['dont_stop'])
return filters
def have_tag(self, dictionary: dict, tag_key: str) -> StrNone:
"""Search owner by tag value
:instance: ec2.Instance
:tag_key: str Tag Key ex. owner, stop
:returns: StrNone Onwer name
"""
tags = (tag_key.capitalize(), tag_key.lower())
if dictionary is not None:
dict_with_owner_key = [tag for tag in dictionary if tag["Key"] in tags]
if dict_with_owner_key:
return dict_with_owner_key[0]['Value']
return None
def have_tag_owner(self, tags: dict):
return self.have_tag(tags, 'owner')
def have_tag_stop(self, tags: dict):
return self.have_tag(tags, 'stop')