| 
									
										
										
										
											2019-11-18 22:39:57 +05:00
										 |  |  | import json | 
					
						
							|  |  |  | from os.path import join | 
					
						
							|  |  |  | from uuid import uuid4 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-09 00:40:05 +05:00
										 |  |  | from uncloud.common.etcd_wrapper import EtcdEntry | 
					
						
							|  |  |  | from uncloud.common.classes import SpecificEtcdEntryBase | 
					
						
							| 
									
										
										
										
											2019-11-18 22:39:57 +05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class RequestType: | 
					
						
							|  |  |  |     CreateVM = "CreateVM" | 
					
						
							|  |  |  |     ScheduleVM = "ScheduleVM" | 
					
						
							|  |  |  |     StartVM = "StartVM" | 
					
						
							|  |  |  |     StopVM = "StopVM" | 
					
						
							|  |  |  |     InitVMMigration = "InitVMMigration" | 
					
						
							|  |  |  |     TransferVM = "TransferVM" | 
					
						
							|  |  |  |     DeleteVM = "DeleteVM" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class RequestEntry(SpecificEtcdEntryBase): | 
					
						
							|  |  |  |     def __init__(self, e): | 
					
						
							| 
									
										
										
										
											2019-12-29 23:48:04 +05:00
										 |  |  |         self.destination_sock_path = None | 
					
						
							| 
									
										
										
										
											2019-12-28 15:39:11 +05:00
										 |  |  |         self.destination_host_key = None | 
					
						
							| 
									
										
										
										
											2019-11-18 22:39:57 +05:00
										 |  |  |         self.type = None  # type: str | 
					
						
							|  |  |  |         self.migration = None  # type: bool | 
					
						
							|  |  |  |         self.destination = None  # type: str | 
					
						
							|  |  |  |         self.uuid = None  # type: str | 
					
						
							|  |  |  |         self.hostname = None  # type: str | 
					
						
							|  |  |  |         super().__init__(e) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @classmethod | 
					
						
							|  |  |  |     def from_scratch(cls, request_prefix, **kwargs): | 
					
						
							| 
									
										
										
										
											2020-01-09 00:40:05 +05:00
										 |  |  |         e = EtcdEntry(meta_or_key=join(request_prefix, uuid4().hex), | 
					
						
							|  |  |  |                       value=json.dumps(kwargs).encode('utf-8'), value_in_json=True) | 
					
						
							| 
									
										
										
										
											2019-11-18 22:39:57 +05:00
										 |  |  |         return cls(e) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class RequestPool: | 
					
						
							|  |  |  |     def __init__(self, etcd_client, request_prefix): | 
					
						
							|  |  |  |         self.client = etcd_client | 
					
						
							|  |  |  |         self.prefix = request_prefix | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def put(self, obj: RequestEntry): | 
					
						
							|  |  |  |         if not obj.key.startswith(self.prefix): | 
					
						
							|  |  |  |             obj.key = join(self.prefix, obj.key) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.client.put(obj.key, obj.value, value_in_json=True) |