|
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(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(first_target) |
394 | 396 |
|
395 | 397 | for tgt in targets: |
396 | 398 | try: |
@@ -605,71 +607,93 @@ def publish(self, metadata: AzurePublishingMetadata) -> None: |
605 | 607 | # "product-name/plan-name" |
606 | 608 | product_name = metadata.destination.split("/")[0] |
607 | 609 | plan_name = metadata.destination.split("/")[-1] |
608 | | - product, plan, tgt = self.get_product_plan_by_name(product_name, plan_name) |
| 610 | + disk_version = None |
| 611 | + sas_found = False |
609 | 612 | log.info( |
610 | | - "Preparing to associate the image \"%s\" with the plan \"%s\" from product \"%s\" on \"%s\"", # noqa: E501 |
| 613 | + "Preparing to associate the image \"%s\" with the plan \"%s\" from product \"%s\"", |
611 | 614 | metadata.image_path, |
612 | 615 | plan_name, |
613 | 616 | product_name, |
614 | | - tgt, |
615 | 617 | ) |
616 | 618 |
|
617 | | - # 2. Retrieve the VM Technical configuration for the given plan |
618 | | - log.info("Retrieving the technical config for \"%s\" on \"%s\".", metadata.destination, tgt) |
619 | | - tech_config = self.get_plan_tech_config(product, plan) |
620 | | - |
621 | | - # 3. Prepare the Disk Version |
| 619 | + # 2. Prepare the Disk Version |
622 | 620 | log.info("Creating the VMImageResource with SAS for image: \"%s\"", metadata.image_path) |
623 | 621 | sas = OSDiskURI(uri=metadata.image_path) |
624 | 622 | source = VMImageSource(source_type="sasUri", os_disk=sas.to_json(), data_disks=[]) |
625 | 623 |
|
| 624 | + # 3. Set the new Disk Version into the product/plan if required |
| 625 | + # |
626 | 626 | # Note: If `overwrite` is True it means we can set this VM image as the only one in the |
627 | 627 | # plan's technical config and discard all other VM images which may've been present. |
628 | | - disk_version = None # just to make mypy happy |
629 | 628 | if metadata.overwrite is True: |
| 629 | + product, plan, tgt = self.get_product_plan_by_name(product_name, plan_name) |
630 | 630 | log.warning( |
631 | 631 | "Overwriting the plan \"%s\" on \"%s\" with the given image: \"%s\".", |
632 | 632 | plan_name, |
633 | 633 | tgt, |
634 | 634 | metadata.image_path, |
635 | 635 | ) |
| 636 | + tech_config = self.get_plan_tech_config(product, plan) |
636 | 637 | disk_version = create_disk_version_from_scratch(metadata, source) |
637 | 638 | tech_config.disk_versions = [disk_version] |
| 639 | + else: |
| 640 | + # Otherwise we need to check whether SAS isn't already present |
| 641 | + # in any of the targets "preview", "live" or "draft" and if not attach and publish it. |
| 642 | + for initial_target in list_all_targets(start_with="preview"): |
638 | 643 |
|
639 | | - # We just want to append a new image if the SAS is not already present. |
640 | | - elif not is_sas_present(tech_config, metadata.image_path, metadata.check_base_sas_only): |
641 | | - # Here we can have the metadata.disk_version set or empty. |
642 | | - # When set we want to get the existing disk_version which matches its value. |
643 | | - log.info( |
644 | | - "Scanning the disk versions from \"%s\" on \"%s\" for the image \"%s\"", |
645 | | - metadata.destination, |
646 | | - tgt, |
647 | | - metadata.image_path, |
648 | | - ) |
649 | | - disk_version = seek_disk_version(tech_config, metadata.disk_version) |
650 | | - |
651 | | - # Check the images of the selected DiskVersion if it exists |
652 | | - if disk_version: |
| 644 | + # 3.1 Retrieve the VM Technical configuration for the given plan |
| 645 | + product, plan, tgt = self.get_product_plan_by_name( |
| 646 | + product_name, plan_name, first_target=initial_target |
| 647 | + ) |
| 648 | + log.info( |
| 649 | + "Retrieving the technical config for \"%s\" on \"%s\".", |
| 650 | + metadata.destination, |
| 651 | + tgt, |
| 652 | + ) |
| 653 | + tech_config = self.get_plan_tech_config(product, plan) |
| 654 | + |
| 655 | + if is_sas_present(tech_config, metadata.image_path, metadata.check_base_sas_only): |
| 656 | + log.info( |
| 657 | + "The destination \"%s\" on \"%s\" already contains the SAS URI: \"%s\".", |
| 658 | + metadata.destination, |
| 659 | + tgt, |
| 660 | + metadata.image_path, |
| 661 | + ) |
| 662 | + # We don't want to seek for SAS anymore as it was already found |
| 663 | + sas_found = True |
| 664 | + break |
| 665 | + else: |
| 666 | + # Seek SAS URI until it reaches the last target |
| 667 | + continue |
| 668 | + |
| 669 | + if not sas_found: |
| 670 | + # At this point there's no SAS URI in any target so we can safely add it |
| 671 | + |
| 672 | + # Here we can have the metadata.disk_version set or empty. |
| 673 | + # When set we want to get the existing disk_version which matches its value. |
653 | 674 | log.info( |
654 | | - "DiskVersion \"%s\" exists in \"%s\" on \"%s\" for the image \"%s\".", |
655 | | - disk_version.version_number, |
| 675 | + "Scanning the disk versions from \"%s\" on \"%s\" for the image \"%s\"", |
656 | 676 | metadata.destination, |
657 | 677 | tgt, |
658 | 678 | metadata.image_path, |
659 | 679 | ) |
660 | | - disk_version = set_new_sas_disk_version(disk_version, metadata, source) |
661 | | - |
662 | | - else: # The disk version doesn't exist, we need to create one from scratch |
663 | | - log.info("The DiskVersion doesn't exist, creating one from scratch.") |
664 | | - disk_version = create_disk_version_from_scratch(metadata, source) |
665 | | - tech_config.disk_versions.append(disk_version) |
666 | | - else: |
667 | | - log.info( |
668 | | - "The destination \"%s\" on \"%s\" already contains the SAS URI: \"%s\".", |
669 | | - metadata.destination, |
670 | | - tgt, |
671 | | - metadata.image_path, |
672 | | - ) |
| 680 | + disk_version = seek_disk_version(tech_config, metadata.disk_version) |
| 681 | + |
| 682 | + # Check the images of the selected DiskVersion if it exists |
| 683 | + if disk_version: |
| 684 | + log.info( |
| 685 | + "DiskVersion \"%s\" exists in \"%s\" on \"%s\" for the image \"%s\".", |
| 686 | + disk_version.version_number, |
| 687 | + metadata.destination, |
| 688 | + tgt, |
| 689 | + metadata.image_path, |
| 690 | + ) |
| 691 | + disk_version = set_new_sas_disk_version(disk_version, metadata, source) |
| 692 | + |
| 693 | + else: # The disk version doesn't exist, we need to create one from scratch |
| 694 | + log.info("The DiskVersion doesn't exist, creating one from scratch.") |
| 695 | + disk_version = create_disk_version_from_scratch(metadata, source) |
| 696 | + tech_config.disk_versions.append(disk_version) |
673 | 697 |
|
674 | 698 | # 4. With the updated disk_version we should adjust the SKUs and submit the changes |
675 | 699 | if disk_version: |
|
0 commit comments