Ejemplo n.º 1
0
    def test_get_shell_from_definition_with_hard_reload(self):
        # Arrange
        self.fs.CreateFile('work/nut-shell/TOSCA-Metadata/TOSCA.meta',
                           contents='TOSCA-Meta-File-Version: 1.0\n'
                           'CSAR-Version: 0.1.0\n'
                           'Created-By: Anonymous\n'
                           'Entry-Definitions: shell-definition.yaml')
        self.fs.CreateFile(
            'work/nut-shell/shell-definition.yaml',
            contents='tosca_definitions_version: tosca_simple_yaml_1_0\n'
            'metadata:\n'
            '  template_name: NutShell\n'
            '  template_author: Anonymous\n'
            '  template_version: 1.0.0\n'
            'node_types:\n'
            '  vendor.switch.NXOS:\n'
            '    derived_from: cloudshell.nodes.Switch\n'
            '    artifacts:\n'
            '      icon:\n'
            '        file: nxos.png\n'
            '        type: tosca.artifacts.File\n'
            '      driver:\n'
            '        file: NutShellDriver.zip\n'
            '        type: tosca.artifacts.File')

        shell_package = ShellPackage('work/nut-shell')
        shell_package.real_shell_name = "something"

        # Act
        shell_name = shell_package.get_name_from_definition(should_reload=True)

        # Assert
        self.assertEqual(shell_name, 'NutShell')
Ejemplo n.º 2
0
    def generate(self):
        """
        Generates Python driver by connecting to CloudShell server
        :return:
        """
        current_path = os.getcwd()
        shell_package = ShellPackage(current_path)
        if not shell_package.is_tosca():
            click.echo('Code generation supported in TOSCA based shells only', err=True)
            return

        # shell_name = shell_package.get_shell_name()
        shell_name = shell_package.get_name_from_definition()
        shell_filename = shell_name + '.zip'
        package_full_path = path.join(current_path, 'dist', shell_filename)
        destination_path = path.join(current_path, 'src')

        cloudshell_config = self.cloudshell_config_reader.read()

        click.echo('Connecting to Cloudshell server ...')

        self.driver_generator.generate_driver(cloudshell_config=cloudshell_config,
                                              destination_path=destination_path,
                                              package_full_path=package_full_path,
                                              shell_filename=shell_filename,
                                              shell_name=shell_name)
    def pack(self, path):
        """
        Creates TOSCA based Shell package
        :return:
        """

        self._remove_all_pyc(path)
        shell_package = ShellPackage(path)
        shell_name = shell_package.get_shell_name()
        shell_real_name = shell_package.get_name_from_definition()
        with TempDirContext(shell_name) as package_path:
            self._copy_tosca_meta(package_path, "")
            tosca_meta = self._read_tosca_meta(path)

            shell_definition_path = tosca_meta["Entry-Definitions"]

            self._copy_shell_definition(package_path, "",
                                        shell_definition_path)

            with open(shell_definition_path) as shell_definition_file:
                shell_definition = yaml.load(shell_definition_file)

                if "template_icon" in shell_definition["metadata"]:
                    self._copy_artifact(
                        shell_definition["metadata"]["template_icon"],
                        package_path)

                for node_type in shell_definition["node_types"].values():
                    if "artifacts" not in node_type:
                        continue

                    for artifact_name, artifact in node_type[
                            "artifacts"].iteritems():
                        if artifact_name == "driver":
                            self._create_driver(path="",
                                                package_path=os.curdir,
                                                dir_path=self.DRIVER_DIR,
                                                driver_name=os.path.basename(
                                                    artifact["file"]))
                        elif artifact_name == "deployment":
                            self._create_driver(path="",
                                                package_path=os.curdir,
                                                dir_path=self.DEPLOY_DIR,
                                                driver_name=os.path.basename(
                                                    artifact["file"]),
                                                mandatory=False)

                        self._copy_artifact(artifact["file"], package_path)

            zip_path = self._zip_package(package_path, "", shell_real_name)

            click.echo(u"Shell package was successfully created: " + zip_path)
Ejemplo n.º 4
0
    def install(self, path):
        """ Install new or Update existed Shell """

        shell_package = ShellPackage(path)
        # shell_name = shell_package.get_shell_name()
        shell_name = shell_package.get_name_from_definition()
        shell_filename = shell_name + ".zip"
        package_full_path = os.path.join(path, "dist", shell_filename)

        cloudshell_config = self.cloudshell_config_reader.read()

        if cloudshell_config.domain != self.GLOBAL_DOMAIN:
            raise click.UsageError(
                "Gen2 shells could not be installed into non Global domain.")

        cs_connection_label = "Connecting to CloudShell at {}:{}".format(
            cloudshell_config.host, cloudshell_config.port)
        with click.progressbar(length=CLOUDSHELL_MAX_RETRIES,
                               show_eta=False,
                               label=cs_connection_label) as pbar:
            try:
                client = self._open_connection_to_quali_server(
                    cloudshell_config, pbar, retry=CLOUDSHELL_MAX_RETRIES)
            finally:
                self._render_pbar_finish(pbar)

        try:
            is_official = client.get_shell(shell_name=shell_name).get(
                SHELL_IS_OFFICIAL_FLAG, False)

            if is_official:
                click.confirm(
                    text=
                    "Upgrading to a custom version of the shell will limit you "
                    "only to customized versions of this shell from now on. "
                    "You won't be able to upgrade it to an official version of the shell in the future."
                    "\nDo you wish to continue?",
                    abort=True)

        except FeatureUnavailable:
            # try to update shell first
            pass
        except ShellNotFoundException:
            # try to install shell
            pass
        except click.Abort:
            raise
        except Exception as e:
            raise FatalError(
                self._parse_installation_error(
                    "Failed to get information about installed shell", e))

        pbar_install_shell_len = 2  # amount of possible actions (update and add)
        installation_label = "Installing shell into CloudShell".ljust(
            len(cs_connection_label))
        with click.progressbar(length=pbar_install_shell_len,
                               show_eta=False,
                               label=installation_label) as pbar:
            try:
                client.update_shell(package_full_path)
            except ShellNotFoundException:
                self._increase_pbar(pbar, DEFAULT_TIME_WAIT)
                self._add_new_shell(client, package_full_path)
            except Exception as e:
                self._increase_pbar(pbar, DEFAULT_TIME_WAIT)
                raise FatalError(
                    self._parse_installation_error("Failed to update shell",
                                                   e))
            finally:
                self._render_pbar_finish(pbar)