Exemple #1
0
    def _platform_attach_volume(self, volume, instance):
        """
        Attach a volume using the Google Cloud Platform API
        Some specific steps are performed here:
            - Device name (the name assigned to this device visible through the VM OS)
            - Mount mode (read only or read write)
            - Disk interface (SCSI or NVME)

        Args:
            volume (<volume>): The volume to attach
            instance (<instance>): The instance where the volume is to be attached

        Returns:
            bool: True if the volume is attached successfully, False otherwise
        """
        # Ask for mount ro/rw
        ro_rw = SimpleTUI.input_dialog(
            "Mount mode",
            question="Specify a mount mode (READ_WRITE, READ_ONLY)",
            return_type=str,
            regex="^(READ_WRITE|READ_ONLY)$")
        if ro_rw is None:
            return
        # Ask for disk interface
        interface = SimpleTUI.input_dialog(
            "Disk interface",
            question="Specify disk interface (SCSI, NVME)",
            return_type=str,
            regex="^(SCSI|NVME)$")
        return self.gcp_client.attach_volume(node=instance,
                                             volume=volume,
                                             device=volume.name,
                                             ex_boot=False,
                                             ex_auto_delete=False,
                                             ex_interface=interface)
Exemple #2
0
    def ask_for_data(self,
                     section_name,
                     param_name,
                     return_type=None,
                     regex=None):
        """
        Ask for user input if a parameter is not defined

        Args:
            param_name (str): the name of the missing parameter
            return_type (<return_type>, optional): the return type assigned to user input
            regex (str): the regular expression the input must follow

        Returns:
            <return_type>: user input of type <return_type>
        """
        return SimpleTUI.input_dialog(
            "Missing value",
            question=
            "A parameter required by this module has not been defined in settings.cfg!\n"
            "Please, define a value to assign to \"" + param_name + "\" (\"" +
            section_name + "\" section)",
            return_type=return_type,
            regex=regex,
            pause_on_exit=False,
            cannot_quit=True)
Exemple #3
0
    def _platform_create_volume(self, volume_name, volume_size):
        """
        Create a new volume using the Google Cloud Platform API
        Some specific steps are performed here:
            - Volume type (pd-standard or pd-ssd)

        Args:
            volume_name (str): Volume name
            volume_size (int): Volume size in GB

        Returns:
            bool: True if the volume is successfully created, False otherwise
        """
        # Volume type
        volume_type = SimpleTUI.input_dialog(
            "Volume type",
            question="Specify the volume type (pd-standard, pd-ssd)",
            return_type=str,
            regex="^(pd-standard|pd-ssd)$")
        if volume_type is None:
            return
        # Volume creation
        return self.gcp_client.create_volume(size=volume_size,
                                             name=volume_name,
                                             ex_disk_type=volume_type)
Exemple #4
0
    def _platform_create_volume(self, volume_name, volume_size):
        """
        Create a new volume using the Amazon Web Services API
        Some specific steps are performed here:
            - Volume type (standard or io1)
            - IOPS (only if io1 is selected)
            - Zone selection (required)

        Args:
            volume_name (str): Volume name
            volume_size (int): Volume size in GB

        Returns:
            bool: True if the volume is successfully created, False otherwise
        """
        # Volume type
        volume_type = SimpleTUI.input_dialog(
            "Volume type",
            question="Specify the volume type (standard, io1)",
            return_type=str,
            regex="^(standard|io1)$")
        if volume_type is None:
            return
        # IOPS
        iops = None
        if volume_type == "io1":
            iops = SimpleTUI.input_dialog(
                "IOPS limit",
                question=
                "Specify the number of IOPS (I/O operations per second) the volume has to support",
                return_type=int)
            if iops is None:
                return
        # Zone selection
        zone_index = SimpleTUI.list_dialog(
            "Zones available",
            self.print_all_availability_zones,
            question="Select a zone where the volume will be created")
        if zone_index is None:
            return
        zone = self.avail_zones[zone_index - 1]
        # Volume creation
        return self.ec2_client.create_volume(name=volume_name,
                                             size=volume_size,
                                             location=zone,
                                             ex_volume_type=volume_type,
                                             ex_iops=iops)
Exemple #5
0
 def promote_ephimeral_ip(self):
     """
     Promote an Ephimeral IP to a Static one
     For more infos about Ephimeral/Static IPs on GCP, please visit
     https://cloud.google.com/compute/docs/ip-addresses/
     """
     # Select an instance
     floating_ip = None
     while (True):
         instance_index = SimpleTUI.list_dialog(
             "Instances available",
             self.print_all_instances,
             question=
             "Select the instance which floating IP has to be promoted to \"static\""
         )
         if instance_index is None:
             return
         instance = self.instances[instance_index - 1]
         # Check if the instance has an IP assigned (e.g. no IP is assigned while stopped)
         if len(instance.public_ips) == 0 or None in instance.public_ips:
             SimpleTUI.msg_dialog(
                 "Promotion status",
                 "This instance has no available floating IPs to promote!",
                 SimpleTUI.DIALOG_ERROR)
         # Check if the instance has already a static IP assigned
         elif self._is_instance_floating_ip_static(instance):
             SimpleTUI.msg_dialog(
                 "Promotion status",
                 "This instance floating IP is already promoted to \"static\"!",
                 SimpleTUI.DIALOG_ERROR)
         # Continue the ephimeral to static conversion
         else:
             floating_ip = instance.public_ips[0]
             break
     # Specify address name
     address_name = SimpleTUI.input_dialog(
         "Static Floating IP Name",
         question="Specify a name for the new Static Floating IP",
         return_type=str,
         regex="^[a-zA-Z0-9-]+$")
     if address_name is None:
         return
     if self._promote_ephimeral_ip_to_static(floating_ip, address_name):
         SimpleTUI.msg_dialog("Static Floating IP Promotion",
                              "Floating IP promoted!",
                              SimpleTUI.DIALOG_SUCCESS)
     else:
         SimpleTUI.msg_dialog(
             "Static Floating IP Promotion",
             "There was an error while promoting this Floating IP!",
             SimpleTUI.DIALOG_ERROR)
Exemple #6
0
 def _platform_reserve_floating_ip(self):
     """
     Reserve a floating IP using the Google Cloud Platform API
     """
     address_name = SimpleTUI.input_dialog(
         "Floating IP Name",
         question="Specify a name for the new Floating IP",
         return_type=str,
         regex="^[a-zA-Z0-9-]+$")
     if address_name is None:
         return
     if self.gcp_client.ex_create_address(name=address_name):
         return True
     return False
Exemple #7
0
    def _platform_attach_volume(self, volume, instance):
        """
        Attach a volume using the Amazon Web Services API
        Some specific steps are performed here:
            - Exposure point (e.g. /dev/sdb)

        Args:
            volume (<volume>): The volume to attach
            instance (<instance>): The instance where the volume is to be attached

        Returns:
            bool: True if the volume is attached successfully, False otherwise
        """
        # Ask for exposure point (the GNU/Linux device where this volume will
        # be available)
        exposure_point = SimpleTUI.input_dialog(
            "Exposure point",
            question="Specify where the device is exposed, e.g. ‘/dev/sdb’",
            return_type=str,
            regex="^(/[^/ ]*)+/?$")
        if exposure_point is None:
            return
        return self.ec2_client.attach_volume(instance, volume, exposure_point)
Exemple #8
0
    def _platform_associate_floating_ip(self, floating_ip, instance):
        """
        Associate a floating IP to an instance using the Google Cloud Platform API
        Some specific steps are performed here:
            - NIC (Network interface controller) selection
            - Access config name

        Args:
            floating_ip (GCEAddress): The floating IP to attach
            instance (Node): The instance where the floating IP is to be assigned

        Returns:
            bool: True if the floating IP is successfully associated, False otherwise
        """
        # Set an instance, as required by print_all_nics()
        self.current_instance = instance
        nic_index = SimpleTUI.list_dialog(
            "NICs available",
            self.print_all_nics,
            question="Select the VM NIC to assign this IP")
        if nic_index is None:
            return
        nic = instance.extra["networkInterfaces"][
            nic_index - 1]  # serve nome per rimuovere
        # Check if there's already an active Access Configuration and ask the user for confirm
        remove_old_access_config = False
        if self._nic_has_access_config(nic):
            choice = SimpleTUI.yn_dialog(
                "Access Configuration Overwrite",
                "Warning: there's already an access configuration associated to this NIC.\n"
                +
                "Do you really want to continue (the current access configuration will be overwritten)?",
                warning=True)
            if not choice:
                return
            remove_old_access_config = True
        # Access Configuration name
        access_config_name = SimpleTUI.input_dialog(
            "Access configuration",
            question="Specify an access configuration name",
            return_type=str,
            regex="^[a-zA-Z0-9-]+$")
        if access_config_name is None:
            return
        # Remove the old access configuration if it's already existing
        if remove_old_access_config:
            SimpleTUI.msg_dialog("Access Configuration Overwrite",
                                 "Removing old access configuration...",
                                 SimpleTUI.DIALOG_INFO,
                                 pause_on_exit=False,
                                 clear_on_exit=False)
            if not self._delete_access_config(instance, nic):
                SimpleTUI.msg_dialog(
                    "Access Configuration Overwrite",
                    "There was an error while removing the current access configuration!",
                    SimpleTUI.DIALOG_ERROR)
                return
        # Associate the Access Configuration to the NIC
        if self.gcp_client.ex_add_access_config(node=instance,
                                                name=access_config_name,
                                                nic=nic,
                                                nat_ip=floating_ip.address,
                                                config_type="ONE_TO_ONE_NAT"):
            return True
        return False