From 2b78e1d561012311aa90a4f00109f3ba3eb1635e Mon Sep 17 00:00:00 2001 From: Tom Warnock Date: Tue, 6 Aug 2019 16:31:15 -0400 Subject: [PATCH 1/3] Fixed update_blueprint to actually work --- cloudendure/cloudendure.py | 71 ++++++++++++++++++++++---------------- 1 file changed, 42 insertions(+), 29 deletions(-) diff --git a/cloudendure/cloudendure.py b/cloudendure/cloudendure.py index 55103bed3..32fddec83 100644 --- a/cloudendure/cloudendure.py +++ b/cloudendure/cloudendure.py @@ -184,8 +184,7 @@ def update_blueprint( self, project_name: str = "", launch_type: str = "test", - dry_run: bool = False, - machine_list=None, + dry_run: bool = False ) -> bool: """Update the blueprint associated with the specified machines.""" print("Updating the CloudEndure Blueprints...") @@ -199,38 +198,52 @@ def update_blueprint( if not project_id: return False + machine_list = {} + machines_response = self.api.api_call(f"projects/{project_id}/machines") + for machine in json.loads(machines_response.text).get("items", []): + source_props: Dict[str, Any] = machine.get("sourceProperties", {}) + machine_id: str = machine.get("id") + machine_name: str = source_props.get("name") + if machine_name in _machines or machine_name.upper() in _machines: + machine_list[machine_id] = machine_name + + if not machine_list: + print ("No Machines Found!") + return False + try: blueprints_response = self.api.api_call(f"projects/{project_id}/blueprints") for blueprint in json.loads(blueprints_response.text).get("items", []): _machine_id = blueprint.get("machineId") - _machine_name = machine_list[_machine_id] + _machine_name = machine_list.get(_machine_id) + if not _machine_name: + continue + _blueprint_id = blueprint.get("id", "") _endpoint = f"projects/{project_id}/blueprints/{_blueprint_id}" + # Handle disk blueprints since we don't want provisioned IOPS $$$$ + for disk in blueprint["disks"]: + blueprint["disks"] = [ + {"type": "SSD", "name": disk.get("name", "")} + ] - for _machine in _machines: - if _machine_name == _machine: - # Handle disk blueprints since we don't want provisioned IOPS $$$$ - for disk in blueprint["disks"]: - blueprint["disks"] = [ - {"type": "SSD", "name": disk.get("name", "")} - ] - - # Update machine tags - blueprint["tags"] = { - "CloneStatus": CLONE_STATUS, - "MigrationWave": MIGRATION_WAVE, - } + # Update machine tags + blueprint["tags"] = [ + { "key" : "CloneStatus", "value" : CLONE_STATUS }, + { "key" : "MigrationWave", "value" : MIGRATION_WAVE } + ] - result = self.api.api_call( - _endpoint, method="patch", data=json.dumps(blueprint) - ) + result = self.api.api_call( + _endpoint, method="patch", data=json.dumps(blueprint) + ) - if result.status_code != 200: - print( - "Blueprint update failure encountered for machine:", - f"({_machine_name}) - fix blueprint settings!", - ) - print("Blueprint for machine: " + _machine_name + " updated!") + if result.status_code != 200: + print( + "Blueprint update failure encountered for machine:", + f"({_machine_name}) - {result.status_code} fix blueprint settings!", + ) + else: + print("Blueprint for machine: " + _machine_name + " updated!") except Exception as e: print(f"Updating blueprint task failed! {e}") return False @@ -588,7 +601,7 @@ def copy_image(self, image_id: str, kms_id: str): print(new_image) return new_image["ImageId"] - def split_image(self, image_id: str): + def split_image(self, image_id: str, root_name: str = "root_image"): """Split the image into a root drive only AMI and a collection of snapshots.""" print("Loading EC2 client for region: ", AWS_REGION) _ec2_res = boto3.resource("ec2", AWS_REGION) @@ -614,11 +627,11 @@ def split_image(self, image_id: str): # create a new AMI with only the root response = _ec2_res.register_image( - Architecture="x86_64", + Architecture=image.architecture, BlockDeviceMappings=[root_drive], - Name="test_split", + Name=root_name, RootDeviceName=image.root_device_name, - VirtualizationType="hvm", + VirtualizationType=image.virtualization_type, ) # return the AMI From 595e600baaad1dbb083af17178b2573c61de0e51 Mon Sep 17 00:00:00 2001 From: Tom Warnock Date: Tue, 6 Aug 2019 16:35:16 -0400 Subject: [PATCH 2/3] Removed launch_type. Dry run works. --- cloudendure/cloudendure.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cloudendure/cloudendure.py b/cloudendure/cloudendure.py index 32fddec83..291e123d5 100644 --- a/cloudendure/cloudendure.py +++ b/cloudendure/cloudendure.py @@ -183,7 +183,6 @@ def check( def update_blueprint( self, project_name: str = "", - launch_type: str = "test", dry_run: bool = False ) -> bool: """Update the blueprint associated with the specified machines.""" @@ -232,6 +231,10 @@ def update_blueprint( { "key" : "CloneStatus", "value" : CLONE_STATUS }, { "key" : "MigrationWave", "value" : MIGRATION_WAVE } ] + + if dry_run: + print("This is a dry run! Not launching any machines!") + return True result = self.api.api_call( _endpoint, method="patch", data=json.dumps(blueprint) From 7f8772b6e8902447aec32342debfba13a6016d69 Mon Sep 17 00:00:00 2001 From: Mark Beacom Date: Tue, 6 Aug 2019 17:18:55 -0400 Subject: [PATCH 3/3] make lint --- cloudendure/cloudendure.py | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/cloudendure/cloudendure.py b/cloudendure/cloudendure.py index 291e123d5..681a23d94 100644 --- a/cloudendure/cloudendure.py +++ b/cloudendure/cloudendure.py @@ -180,11 +180,7 @@ def check( ) return True - def update_blueprint( - self, - project_name: str = "", - dry_run: bool = False - ) -> bool: + def update_blueprint(self, project_name: str = "", dry_run: bool = False) -> bool: """Update the blueprint associated with the specified machines.""" print("Updating the CloudEndure Blueprints...") @@ -207,7 +203,7 @@ def update_blueprint( machine_list[machine_id] = machine_name if not machine_list: - print ("No Machines Found!") + print("No Machines Found!") return False try: @@ -222,16 +218,14 @@ def update_blueprint( _endpoint = f"projects/{project_id}/blueprints/{_blueprint_id}" # Handle disk blueprints since we don't want provisioned IOPS $$$$ for disk in blueprint["disks"]: - blueprint["disks"] = [ - {"type": "SSD", "name": disk.get("name", "")} - ] + blueprint["disks"] = [{"type": "SSD", "name": disk.get("name", "")}] # Update machine tags blueprint["tags"] = [ - { "key" : "CloneStatus", "value" : CLONE_STATUS }, - { "key" : "MigrationWave", "value" : MIGRATION_WAVE } + {"key": "CloneStatus", "value": CLONE_STATUS}, + {"key": "MigrationWave", "value": MIGRATION_WAVE}, ] - + if dry_run: print("This is a dry run! Not launching any machines!") return True