Exemple #1
0
    def check_dependencies(self, module):
        """
        Check if all the dependencies (pip packages) are satisfied for
        a certain module, and can install them if the user approves

        Args:
            module (Module): a module object (no manager class must be loaded
                             through load_manager_class method of this object)

        Returns:
            bool: the result of the check
        """
        required_packages = getattr(module, "dependencies")
        installed_packages = subprocess.check_output(['pip3', 'list', '--format=json'], stderr=subprocess.STDOUT).decode()
        missing_packages_names = []  # package names displayed to the user
        missing_packages_commands = []  # packages names/urls passed to pip3
        for required_package in required_packages:
            # Format: pip-package-name:package-url|package-git (the latter is optional)
            #
            # Symbolic: pip package name (the one you see with "pip3 list")
            # Package URL (optional): package url from where pip3 will download the library
            # Package Git (optional): package git url in the form git+git://github.com/my_user/my_project.git(@branch)
            #
            # e.g. libcloud:apache-libcloud
            required_package_data = required_package.split(":", 1)
            if required_package_data[0] not in installed_packages:
                missing_packages_names.append(required_package_data[0])
                if len(required_package_data) == 2:
                    missing_packages_commands.append(required_package_data[1])
                else:
                    missing_packages_commands.append(required_package_data[0])
        if len(missing_packages_names) > 0:
            packages_list = ""
            for missing_package_name in missing_packages_names:
                packages_list += "- " + missing_package_name + "\n"
            choice = SimpleTUI.yn_dialog("Missing packages", "The following packages are required by the " + module.platform_name + " module:\n" +
                                         "\n" + packages_list + "\nIf these packages are not installed, this module won't be loaded.\n" +
                                         "Do you want to install them through pip?", warning=True)
            if choice:
                SimpleTUI.msg_dialog("Library installer", "Installing the required libraries, this can take a bit...",
                                     SimpleTUI.DIALOG_INFO, pause_on_exit=False, clear_on_exit=False)
                if self.install_libraries(missing_packages_commands):
                    SimpleTUI.msg_dialog("Library installer", "All the packages for " + module.platform_name + "\n" +
                                         "were successfully installed!\n\n" + packages_list, SimpleTUI.DIALOG_SUCCESS)
                    return True
                else:
                    SimpleTUI.msg_dialog("Library installer", "There was an error while installing the missing packages\n" +
                                         "for " + module.platform_name + ".\n"
                                         "Please check logs" + sep + "installer.log in the main directory for details.", SimpleTUI.DIALOG_ERROR)
                    return False
            return False
        return True
Exemple #2
0
 def _platform_extra_menu(self):
     """
     Print the extra Functions Menu (specific for each platform)
     """
     while (True):
         menu_header = self.platform_name + " Extra Commands"
         menu_subheader = [
             "Region: \033[1;94m" + self._platform_get_region() + "\033[0m"
         ]
         menu_items = [
             "Promote ephimeral IP to static", "Demote a static IP",
             "List instances for all the regions",
             "List volumes for all the regions",
             "List floating ips for all the regions",
             "Back to the Main Menu"
         ]
         choice = SimpleTUI.print_menu(menu_header, menu_items,
                                       menu_subheader)
         if choice == 1:  # Promote IP
             self.promote_ephimeral_ip()
         elif choice == 2:  # Demote IP
             answer = SimpleTUI.yn_dialog(
                 "Demotion status",
                 "You can demote a static IP easily deleting it through \"Manage floating IPs\" > \"Release a reserved Floating IP\".\n"
                 +
                 "NOTE: the static IP won't be removed from the associated instance until the latter is stopped/rebooted/deleted.\n"
                 +
                 "For more infos about Ephimeral/Static IPs on GCP, please visit https://cloud.google.com/compute/docs/ip-addresses/.\n"
                 +
                 "Would you like to start the \"Release a reserved Floating IP\" wizard now?",
                 SimpleTUI.DIALOG_INFO)
             if answer:
                 self.release_floating_ip()
         elif choice == 3:  # List all the instances (Global)
             SimpleTUI.list_dialog("Instances available (Global view)",
                                   list_printer=self.print_global_instances)
         elif choice == 4:  # List all the volumes (Global)
             SimpleTUI.list_dialog("Volumes available (Global view)",
                                   list_printer=self.print_global_volumes)
         elif choice == 5:  # List all the floating ips (Global)
             SimpleTUI.list_dialog(
                 "Floating IPs available (Global view)",
                 list_printer=self.print_global_floating_ips)
         elif choice == 6:
             break
         else:
             SimpleTUI.msg_dialog("Error", "Unimplemented functionality",
                                  SimpleTUI.DIALOG_ERROR)
Exemple #3
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