def pack(self, path):
        """
        Creates TOSCA based Shell package
        :return:
        """
        shell_package = ShellPackage(path)
        shell_name = shell_package.get_shell_name()
        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)
            self._create_driver('', os.curdir, shell_name)

            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 in node_type['artifacts'].values():
                        self._copy_artifact(artifact['file'], package_path)

            zip_path = self._zip_package(package_path, '', shell_name)

            click.echo(u'Shell package was successfully created: ' + zip_path)
    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_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 test_get_shell_name_should_be_capitalized(self):
        # Arrange
        shell_package = ShellPackage('work/folders/nut-shell')

        # Act
        shell_name = shell_package.get_shell_name()

        # Assert
        self.assertEqual(shell_name, 'NutShell')
    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)
    def install(self, path):
        shell_package = ShellPackage(path)
        shell_filename = shell_package.get_shell_name() + '.zip'
        package_full_path = os.path.join(path, 'dist', shell_filename)

        cloudshell_config = self.cloudshell_config_reader.read()

        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)

        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)