|
41 | 41 | create_disk_version_from_scratch, |
42 | 42 | is_azure_job_not_complete, |
43 | 43 | is_sas_present, |
| 44 | + list_all_targets, |
44 | 45 | logdiff, |
45 | 46 | seek_disk_version, |
46 | 47 | set_new_sas_disk_version, |
@@ -258,10 +259,7 @@ def get_product(self, product_id: str, first_target: str = "preview") -> Product |
258 | 259 | Returns: |
259 | 260 | Product: the requested product |
260 | 261 | """ |
261 | | - targets = [first_target] |
262 | | - for tgt in ["preview", "draft", "live"]: |
263 | | - if tgt not in targets: |
264 | | - targets.append(tgt) |
| 262 | + targets = list_all_targets(start_with=first_target) |
265 | 263 |
|
266 | 264 | for t in targets: |
267 | 265 | log.info("Requesting the product ID \"%s\" with state \"%s\".", product_id, t) |
@@ -377,20 +375,24 @@ def get_plan_by_name(self, product: Product, plan_name: str) -> PlanSummary: |
377 | 375 | self._raise_error(NotFoundError, f"No such plan with name \"{plan_name}\"") |
378 | 376 |
|
379 | 377 | def get_product_plan_by_name( |
380 | | - self, product_name: str, plan_name: str |
| 378 | + self, |
| 379 | + product_name: str, |
| 380 | + plan_name: str, |
| 381 | + first_target: str = "preview", |
381 | 382 | ) -> Tuple[Product, PlanSummary, str]: |
382 | 383 | """Return a tuple with the desired Product and Plan after iterating over all targets. |
383 | 384 |
|
384 | 385 | Args: |
385 | 386 | product_name (str): The name of the product to search for |
386 | 387 | plan_name (str): The name of the plan to search for |
387 | | -
|
| 388 | + first_target (str, optional) |
| 389 | + The first target to lookup into. Defaults to ``preview``. |
388 | 390 | Returns: |
389 | 391 | Tuple[Product, PlanSummary, str]: The Product, PlanSummary and target when fonud |
390 | 392 | Raises: |
391 | 393 | NotFoundError whenever all targets are exhausted and no information was found |
392 | 394 | """ |
393 | | - targets = ["preview", "draft", "live"] |
| 395 | + targets = list_all_targets(start_with=first_target) |
394 | 396 |
|
395 | 397 | for tgt in targets: |
396 | 398 | try: |
@@ -664,71 +666,93 @@ def publish(self, metadata: AzurePublishingMetadata) -> None: |
664 | 666 | # "product-name/plan-name" |
665 | 667 | product_name = metadata.destination.split("/")[0] |
666 | 668 | plan_name = metadata.destination.split("/")[-1] |
667 | | - product, plan, tgt = self.get_product_plan_by_name(product_name, plan_name) |
| 669 | + disk_version = None |
| 670 | + sas_found = False |
668 | 671 | log.info( |
669 | | - "Preparing to associate the image \"%s\" with the plan \"%s\" from product \"%s\" on \"%s\"", # noqa: E501 |
| 672 | + "Preparing to associate the image \"%s\" with the plan \"%s\" from product \"%s\"", |
670 | 673 | metadata.image_path, |
671 | 674 | plan_name, |
672 | 675 | product_name, |
673 | | - tgt, |
674 | 676 | ) |
675 | 677 |
|
676 | | - # 2. Retrieve the VM Technical configuration for the given plan |
677 | | - log.info("Retrieving the technical config for \"%s\" on \"%s\".", metadata.destination, tgt) |
678 | | - tech_config = self.get_plan_tech_config(product, plan) |
679 | | - |
680 | | - # 3. Prepare the Disk Version |
| 678 | + # 2. Prepare the Disk Version |
681 | 679 | log.info("Creating the VMImageResource with SAS for image: \"%s\"", metadata.image_path) |
682 | 680 | sas = OSDiskURI(uri=metadata.image_path) |
683 | 681 | source = VMImageSource(source_type="sasUri", os_disk=sas.to_json(), data_disks=[]) |
684 | 682 |
|
| 683 | + # 3. Set the new Disk Version into the product/plan if required |
| 684 | + # |
685 | 685 | # Note: If `overwrite` is True it means we can set this VM image as the only one in the |
686 | 686 | # plan's technical config and discard all other VM images which may've been present. |
687 | | - disk_version = None # just to make mypy happy |
688 | 687 | if metadata.overwrite is True: |
| 688 | + product, plan, tgt = self.get_product_plan_by_name(product_name, plan_name) |
689 | 689 | log.warning( |
690 | 690 | "Overwriting the plan \"%s\" on \"%s\" with the given image: \"%s\".", |
691 | 691 | plan_name, |
692 | 692 | tgt, |
693 | 693 | metadata.image_path, |
694 | 694 | ) |
| 695 | + tech_config = self.get_plan_tech_config(product, plan) |
695 | 696 | disk_version = create_disk_version_from_scratch(metadata, source) |
696 | 697 | tech_config.disk_versions = [disk_version] |
| 698 | + else: |
| 699 | + # Otherwise we need to check whether SAS isn't already present |
| 700 | + # in any of the targets "preview", "live" or "draft" and if not attach and publish it. |
| 701 | + for initial_target in list_all_targets(start_with="preview"): |
697 | 702 |
|
698 | | - # We just want to append a new image if the SAS is not already present. |
699 | | - elif not is_sas_present(tech_config, metadata.image_path, metadata.check_base_sas_only): |
700 | | - # Here we can have the metadata.disk_version set or empty. |
701 | | - # When set we want to get the existing disk_version which matches its value. |
702 | | - log.info( |
703 | | - "Scanning the disk versions from \"%s\" on \"%s\" for the image \"%s\"", |
704 | | - metadata.destination, |
705 | | - tgt, |
706 | | - metadata.image_path, |
707 | | - ) |
708 | | - disk_version = seek_disk_version(tech_config, metadata.disk_version) |
709 | | - |
710 | | - # Check the images of the selected DiskVersion if it exists |
711 | | - if disk_version: |
| 703 | + # 3.1 Retrieve the VM Technical configuration for the given plan |
| 704 | + product, plan, tgt = self.get_product_plan_by_name( |
| 705 | + product_name, plan_name, first_target=initial_target |
| 706 | + ) |
| 707 | + log.info( |
| 708 | + "Retrieving the technical config for \"%s\" on \"%s\".", |
| 709 | + metadata.destination, |
| 710 | + tgt, |
| 711 | + ) |
| 712 | + tech_config = self.get_plan_tech_config(product, plan) |
| 713 | + |
| 714 | + if is_sas_present(tech_config, metadata.image_path, metadata.check_base_sas_only): |
| 715 | + log.info( |
| 716 | + "The destination \"%s\" on \"%s\" already contains the SAS URI: \"%s\".", |
| 717 | + metadata.destination, |
| 718 | + tgt, |
| 719 | + metadata.image_path, |
| 720 | + ) |
| 721 | + # We don't want to seek for SAS anymore as it was already found |
| 722 | + sas_found = True |
| 723 | + break |
| 724 | + else: |
| 725 | + # Seek SAS URI until it reaches the last target |
| 726 | + continue |
| 727 | + |
| 728 | + if not sas_found: |
| 729 | + # At this point there's no SAS URI in any target so we can safely add it |
| 730 | + |
| 731 | + # Here we can have the metadata.disk_version set or empty. |
| 732 | + # When set we want to get the existing disk_version which matches its value. |
712 | 733 | log.info( |
713 | | - "DiskVersion \"%s\" exists in \"%s\" on \"%s\" for the image \"%s\".", |
714 | | - disk_version.version_number, |
| 734 | + "Scanning the disk versions from \"%s\" on \"%s\" for the image \"%s\"", |
715 | 735 | metadata.destination, |
716 | 736 | tgt, |
717 | 737 | metadata.image_path, |
718 | 738 | ) |
719 | | - disk_version = set_new_sas_disk_version(disk_version, metadata, source) |
720 | | - |
721 | | - else: # The disk version doesn't exist, we need to create one from scratch |
722 | | - log.info("The DiskVersion doesn't exist, creating one from scratch.") |
723 | | - disk_version = create_disk_version_from_scratch(metadata, source) |
724 | | - tech_config.disk_versions.append(disk_version) |
725 | | - else: |
726 | | - log.info( |
727 | | - "The destination \"%s\" on \"%s\" already contains the SAS URI: \"%s\".", |
728 | | - metadata.destination, |
729 | | - tgt, |
730 | | - metadata.image_path, |
731 | | - ) |
| 739 | + disk_version = seek_disk_version(tech_config, metadata.disk_version) |
| 740 | + |
| 741 | + # Check the images of the selected DiskVersion if it exists |
| 742 | + if disk_version: |
| 743 | + log.info( |
| 744 | + "DiskVersion \"%s\" exists in \"%s\" on \"%s\" for the image \"%s\".", |
| 745 | + disk_version.version_number, |
| 746 | + metadata.destination, |
| 747 | + tgt, |
| 748 | + metadata.image_path, |
| 749 | + ) |
| 750 | + disk_version = set_new_sas_disk_version(disk_version, metadata, source) |
| 751 | + |
| 752 | + else: # The disk version doesn't exist, we need to create one from scratch |
| 753 | + log.info("The DiskVersion doesn't exist, creating one from scratch.") |
| 754 | + disk_version = create_disk_version_from_scratch(metadata, source) |
| 755 | + tech_config.disk_versions.append(disk_version) |
732 | 756 |
|
733 | 757 | # 4. With the updated disk_version we should adjust the SKUs and submit the changes |
734 | 758 | if disk_version: |
|
0 commit comments