forked from SamsungLabs/FastFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance.py
302 lines (254 loc) · 10.7 KB
/
instance.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# Copyright 2022 Samsung Electronics Co., Ltd. All Rights Reserved
# TODO: Document API wiki
from enum import Enum
import yaml
from fastflow.autoscaler.utils import launcher_util
class InstanceType(Enum):
AWS_EC2 = "aws_ec2"
SSH = "ssh"
SELF_HOSTED_CLUSTER = "self_hosted_cluster"
def instance_option_from_yaml(yaml_path,
cluster_type,
self_hosted_cluster_option=None):
"""
Convert yaml type config to InstanceOption.
Yaml key must be equal to the keyword
argument of InstanceOption.__init__
e.g.) To set image_type="fastflow",
yaml format should be image_type: "fastflow".
check doc string about
"fastflow.autoscaler.framework.instance.AWSEC2InstanceOption"
"fastflow.autoscaler.framework.instance.SSHInstanceOption"
"fastflow.autoscaler.framework.instance.SelfHostedClusterInstanceOption"
self_hosted_cluster_option must have "self_hosted_cluster_creator",
"self_hosted_cluster_terminator", "dispatcher", "worker".
Also, "dispatcher" and "worker" must have "InstanceType"
self_hosted_cluster_option:
each specific field MUST filled
this is dictionary for your own cluster system
{
"self_hosted_cluster_creator": your_python_function,
"self_hosted_cluster_terminator": your_python_function,
"dispatcher": {
"InstanceType": dispatcher_instance_type,
also_your_needed_option: ...
},
"worker": {
"InstanceType": worker_instance_type,
also_your_needed_option: ...
},
also_your_needed_option: ...
}
:this is form about creator and terminator
self_hosted_cluster_creator form:
>>> def my_instance_creator(option_dictionary, entrypoint):
>>> option = your_option_creator(option_dictionary,
... entrypoint)
>>> instance = my_cluster_cli.create_instance(option)
>>> return instance
self_hosted_cluster_terminator form:
>>> def my_instance_terminator(instance):
>>> instance.terminate()
yaml_path: yaml path
cluster_type: cluster type, "aws_ec2" | "ssh" | "self_hosted_cluster"
self_hosted_cluster_option: self_hosted_cluster_option
:return: {cluster_type}InstanceOption
"""
with open(yaml_path) as yaml_file:
yaml_dict = yaml.load(yaml_file, Loader=yaml.FullLoader)
if cluster_type == InstanceType.AWS_EC2.value:
return AWSEC2InstanceOption(**yaml_dict)
if cluster_type == InstanceType.SSH.value:
return SSHInstanceOption(**yaml_dict)
if cluster_type == InstanceType.SELF_HOSTED_CLUSTER.value:
return SelfHostedClusterInstanceOption(
self_hosted_cluster_option=self_hosted_cluster_option,
**yaml_dict
)
raise ValueError("Unrecognized Cluster Type")
class InstanceOption:
def __init__(self, **kwargs):
if not kwargs.get("image_type") in {"fastflow", "python"}:
raise ValueError("unsupported image type")
self._option_dictionary = kwargs.copy()
def get_option_dictionary(self):
return self._option_dictionary.copy()
class AWSEC2InstanceOption(InstanceOption):
def __init__(self,
image_type="fastflow",
boto3_credential=None,
ec2_instance_option=None):
"""
image_type:
"fastflow" | "python"
Type of AMI or docker image that installed fastflow or not.
boto3_credential:
{
"region_name": your_aws_region,
"endpoint_url": your_aws_endpoint_url,
"aws_access_key_id": your_aws_access_key_id,
"aws_secret_access": your_aws_secret_access_key,
"aws_session_token": your_aws_session_token
}
ec2_instance_option:
{
"dispatcher": {
"ImageId": your_AMI_id,
"MinCount": 1, # fixed
"MaxCount": 1, # fixed
"InstanceType": your_AWS_instance_type,
"KeyName": your_key_pair_name,
"InstanceInitiatedShutdownBehavior": "terminate", # fixed
"SecurityGroupIds": [your_security_group_list]
},
"worker": {
"ImageId": your_AMI_id,
"MinCount": 1, # fixed
"InstanceType": your_AWS_instance_type,
"KeyName": your_key_pair_name,
"InstanceInitiatedShutdownBehavior": "terminate", # fixed
"SecurityGroupIds": [your_security_group_list]
}
}
"""
if not boto3_credential:
raise ValueError("detect aws_ec2, boto3_credential "
"must need")
if not ec2_instance_option:
raise ValueError("detect aws_ec2, ec2_instance_option "
"must need")
if not ec2_instance_option.get("dispatcher") \
or not ec2_instance_option.get("worker"):
raise ValueError("ec2_instance_option must "
"have dispatcher and worker")
super().__init__(image_type=image_type,
boto3_credential=boto3_credential,
ec2_instance_option=ec2_instance_option)
class SSHInstanceOption(InstanceOption):
"""
An SSH cluster is a cluster environment
that can access already running instances through ssh.
"""
def __init__(self,
dispatcher,
workers,
ssh_user_name=None,
ssh_private_key_path=None,
image_type="fastflow"):
"""
dispatcher:
dispatcher address, ex) 10.0.0.68
workers:
worker address lists, ex) [10.0.0.67, 10.0.0.66]
ssh_user_name:
ssh_user_name, if you need specifying username
ssh_private_key_path:
ssh_private_key_path, if you need specifying ssh private key
image_type:
"fastflow" | "python"
Type of AMI or docker image that installed fastflow or not.
in SSH Cluster, it mean your already running instance
have installed fastflow or not.
"""
super().__init__(image_type=image_type,
dispatcher=dispatcher,
workers=workers,
ssh_user_name=ssh_user_name,
ssh_private_key_path=ssh_private_key_path)
class SelfHostedClusterInstanceOption(InstanceOption):
def __init__(self,
image_type="fastflow",
self_hosted_cluster_option=None):
"""
image_type:
"fastflow" | "python"
Type of AMI or docker image that installed fastflow or not.
self_hosted_cluster_option:
each specific field MUST filled.
this is dictionary for your own cluster system
{
"self_hosted_cluster_creator": your_python_function,
"self_hosted_cluster_terminator": your_python_function,
"dispatcher": {
"InstanceType": dispatcher_instance_type,
also_your_needed_option: ...
},
"worker": {
"InstanceType": worker_instance_type,
also_your_needed_option: ...
},
also_your_needed_option: ...
}
self_hosted_cluster_creator MUST return instance or process
:this is form about creator and terminator
self_hosted_cluster_creator form:
>>> def my_instance_creator(option_dictionary, entrypoint):
>>> option = your_option_creator(option_dictionary,
... entrypoint)
>>> instance = my_cluster_cli.create_instance(option)
>>> return instance
self_hosted_cluster_terminator form:
>>> def my_instance_terminator(instance):
>>> instance.terminate()
"""
if not self_hosted_cluster_option:
raise ValueError("detect self_hosted_cluster, "
"self_hosted_cluster_option "
"must need")
if not self_hosted_cluster_option.get("self_hosted_cluster_creator") \
or not self_hosted_cluster_option \
.get("self_hosted_cluster_terminator") \
or not self_hosted_cluster_option.get("dispatcher") \
or not self_hosted_cluster_option.get("worker"):
raise ValueError("self_hosted_cluster_creator "
"and self_hosted_cluster_terminator "
"and dispatcher "
"and worker must need")
if not self_hosted_cluster_option \
.get("dispatcher").get("InstanceType") \
or not self_hosted_cluster_option \
.get("worker").get("InstanceType"):
raise ValueError("dispatcher and worker must have InstanceType")
super().__init__(image_type=image_type,
self_hosted_cluster_option=self_hosted_cluster_option)
class Instance:
def __init__(self,
instance,
instance_id
):
self._instance = instance
self._instance_id = instance_id
self._instance_info = None
def terminate(self):
raise NotImplementedError
def get_info(self):
return self._instance_info
def set_info(self, info):
self._instance_info = info
def get_id(self):
return str(self._instance_id)
class AWSEC2Instance(Instance):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def terminate(self):
self._instance.terminate()
class SSHInstance(Instance):
"""
SSHInstance for terminating process
"""
def __init__(self, *args, **kwargs):
self._is_running = True
super().__init__(*args, **kwargs)
def terminate(self):
if not self._is_running:
return
command = launcher_util.build_multiline_command(
launcher_util.SSH_PROCESS_TERMINATION_FORM)
self._instance.exec_command(command)
self._is_running = False
# TODO: Implement SelfHostedClusterInstance
# for autoscaling with self-hosted cluster
class SelfHostedClusterInstance(Instance):
"""
SelfHostedClusterInstance for terminating instance and process
"""