Example #1
0
    def _validate_external_reference(self, tpl_file, resource_file,
                                     raise_exc=True):
        """Verify that the external resource exists

        If resource_file is a URL verify that the URL is valid.
        If resource_file is a relative path verify that the path is valid
        considering base folder (self.temp_dir) and tpl_file.
        Note that in a CSAR resource_file cannot be an absolute path.
        """
        if UrlUtils.validate_url(resource_file):
            msg = (_('The resource at "%s" cannot be accessed.') %
                   resource_file)
            try:
                if UrlUtils.url_accessible(resource_file):
                    return
                else:
                    ExceptionCollector.appendException(
                        URLException(what=msg))
                    self.error_caught = True
            except Exception:
                ExceptionCollector.appendException(
                    URLException(what=msg))
                self.error_caught = True

        if os.path.isfile(os.path.join(self.temp_dir,
                                       os.path.dirname(tpl_file),
                                       resource_file)):
            return

        if raise_exc:
            ExceptionCollector.appendException(
                ValueError(_('The resource "%s" does not exist.')
                           % resource_file))
            self.error_caught = True
Example #2
0
 def main(self, args):
     # TODO(spzala): set self.deploy based on passed args once support for
     # --deploy argument is enabled.
     self.deploy = False
     self._validate(args)
     path = args[0].split('--template-file=')[1]
     # e.g. --template_file=translator/tests/data/tosca_helloworld.yaml
     template_type = args[1].split('--template-type=')[1]
     # e.g. --template_type=tosca
     if not template_type:
         msg = _("Template type is needed. For example, 'tosca'")
         #log.error(msg)
         raise ValueError(msg)
     elif template_type not in self.SUPPORTED_TYPES:
         msg = _("%(value)s is not a valid template type.") % {
             'value': template_type
         }
         #log.error(msg)
         raise ValueError(msg)
     parsed_params = {}
     validate_only = None
     output_file = None
     if len(args) > 2:
         parameters = None
         for arg in args:
             if "--validate-only=" in arg:
                 validate_only = arg
             if "--parameters=" in arg:
                 parameters = arg
             if "--output-file=" in arg:
                 output = arg
                 output_file = output.split('--output-file=')[1]
         if parameters:
             parsed_params = self._parse_parameters(parameters)
     a_file = os.path.isfile(path)
     a_url = UrlUtils.validate_url(path) if not a_file else False
     if a_file or a_url:
         run_only_validation = False
         if validate_only:
             value = validate_only.split('-validate-only=')[1].lower()
             if template_type == 'tosca' and value == 'true':
                 run_only_validation = True
         if run_only_validation:
             ToscaTemplate(path, parsed_params, a_file)
         else:
             #log.info(
             #    _('Checked whether template path is a file or url path.'))
             heat_tpl = self._translate(template_type, path, parsed_params,
                                        a_file)
             if heat_tpl:
                 self._write_output(heat_tpl, output_file)
     else:
         msg = _("The path %(path)s is not a valid file or URL.") % {
             'path': path
         }
         #log.error(msg)
         raise ValueError(msg)
Example #3
0
 def main(self, args):
     # TODO(spzala): set self.deploy based on passed args once support for
     # --deploy argument is enabled.
     self.deploy = False
     self._validate(args)
     path = args[0].split("--template-file=")[1]
     # e.g. --template_file=translator/tests/data/tosca_helloworld.yaml
     template_type = args[1].split("--template-type=")[1]
     # e.g. --template_type=tosca
     if not template_type:
         msg = _("Template type is needed. For example, 'tosca'")
         # log.error(msg)
         raise ValueError(msg)
     elif template_type not in self.SUPPORTED_TYPES:
         msg = _("%(value)s is not a valid template type.") % {"value": template_type}
         # log.error(msg)
         raise ValueError(msg)
     parsed_params = {}
     validate_only = None
     output_file = None
     if len(args) > 2:
         parameters = None
         for arg in args:
             if "--validate-only=" in arg:
                 validate_only = arg
             if "--parameters=" in arg:
                 parameters = arg
             if "--output-file=" in arg:
                 output = arg
                 output_file = output.split("--output-file=")[1]
         if parameters:
             parsed_params = self._parse_parameters(parameters)
     a_file = os.path.isfile(path)
     a_url = UrlUtils.validate_url(path) if not a_file else False
     if a_file or a_url:
         run_only_validation = False
         if validate_only:
             value = validate_only.split("-validate-only=")[1].lower()
             if template_type == "tosca" and value == "true":
                 run_only_validation = True
         if run_only_validation:
             ToscaTemplate(path, parsed_params, a_file)
         else:
             # log.info(
             #    _('Checked whether template path is a file or url path.'))
             heat_tpl = self._translate(template_type, path, parsed_params, a_file)
             if heat_tpl:
                 self._write_output(heat_tpl, output_file)
     else:
         msg = _("The path %(path)s is not a valid file or URL.") % {"path": path}
         # log.error(msg)
         raise ValueError(msg)
Example #4
0
    def _validate_external_reference(self, base_dir, tpl_file, resource_file, raise_exc=True):
        """Verify that the external resource exists

        If resource_file is a URL verify that the URL is valid.
        If resource_file is a relative path verify that the path is valid
        considering base_dir and tpl_file.
        Note that in a CSAR resource_file cannot be an absolute path.
        """
        if UrlUtils.validate_url(resource_file):
            msg = _("The resource at %s cannot be accessed") % resource_file
            try:
                if UrlUtils.url_accessible(resource_file):
                    return
                else:
                    raise URLException(what=msg)
            except Exception:
                raise URLException(what=msg)

        if os.path.isfile(os.path.join(base_dir, os.path.dirname(tpl_file), resource_file)):
            return

        if raise_exc:
            raise ValueError(_("The resource %s does not exist.") % resource_file)
Example #5
0
    def validate(self):
        """Validate the provided CSAR file."""

        self.is_validated = True

        # validate that the file or URL exists
        missing_err_msg = (_('"%s" does not exist.') % self.path)
        if self.a_file:
            if not os.path.isfile(self.path):
                ExceptionCollector.appendException(
                    ValidationError(message=missing_err_msg))
                return False
            else:
                self.csar = self.path
        else:  # a URL
            if not UrlUtils.validate_url(self.path):
                ExceptionCollector.appendException(
                    ValidationError(message=missing_err_msg))
                return False
            else:
                response = requests.get(self.path)
                self.csar = BytesIO(response.content)

        # validate that it is a valid zip file
        if not zipfile.is_zipfile(self.csar):
            err_msg = (_('"%s" is not a valid zip file.') % self.path)
            ExceptionCollector.appendException(
                ValidationError(message=err_msg))
            return False

        # validate that it contains the metadata file in the correct location
        self.zfile = zipfile.ZipFile(self.csar, 'r')
        filelist = self.zfile.namelist()
        if TOSCA_META in filelist:
            self.is_tosca_metadata = True
            # validate that 'Entry-Definitions' property exists in TOSCA.meta
            is_validated = self._validate_tosca_meta(filelist)
        else:
            self.is_tosca_metadata = False
            is_validated = self._validate_root_level_yaml(filelist)

        if is_validated:
            # validate that external references and imports in the main
            # template actually exist and are accessible
            main_tpl = self._read_template_yaml(self.main_template_file_name)
            self._validate_external_references(main_tpl)
        return not ExceptionCollector.exceptionsCaught()
Example #6
0
def load_yaml(path, a_file=True):
    f = None

    try:
        if a_file:
            f = codecs.open(path, encoding='utf-8', errors='strict')
            s = f.read()
        else:

            s = UrlUtils.get_url(path).content

    except requests.exceptions.Timeout as e:
        msg = (_('Timeout reaching server "%(path)s": Reason is %(reason)s.') %
               {
                   'path': path,
                   'reason': e
               })
        ExceptionCollector.appendException(URLException(what=msg))
        return

    except requests.exceptions.ConnectionError as e:
        msg = (_('Error reaching server "%(path)s": Reason is %(reason)s.') % {
            'path': path,
            'reason': e
        })
        ExceptionCollector.appendException(URLException(what=msg))
        return

    except requests.exceptions.HTTPError as e:
        msg = (_('Request error "%(path)s": Reason is %(reason)s.') % {
            'path': path,
            'reason': e
        })
        ExceptionCollector.appendException(URLException(what=msg))
        return

    except Exception as e:
        raise
    finally:
        if f:
            f.close()
    return yaml.load(s, Loader=yaml_loader)
Example #7
0
def main():
    if len(sys.argv) < 3:
        msg = _("The program requires minimum two arguments. "
                "Please refer to the usage documentation.")
        raise ValueError(msg)
    if "--template-file=" not in sys.argv[1]:
        msg = _("The program expects --template-file as first argument. "
                "Please refer to the usage documentation.")
        raise ValueError(msg)
    if "--template-type=" not in sys.argv[2]:
        msg = _("The program expects --template-type as second argument. "
                "Please refer to the usage documentation.")
        raise ValueError(msg)
    path = sys.argv[1].split('--template-file=')[1]
    # e.g. --template_file=translator/tests/data/tosca_helloworld.yaml
    template_type = sys.argv[2].split('--template-type=')[1]
    # e.g. --template_type=tosca
    supported_types = ['tosca']
    if not template_type:
        raise ValueError(_("Template type is needed. For example, 'tosca'"))
    elif template_type not in supported_types:
        raise ValueError(_("%(value)s is not a valid template type.")
                         % {'value': template_type})
    parsed_params = {}
    if len(sys.argv) > 3:
        parsed_params = parse_parameters(sys.argv[3])

    a_file = os.path.isfile(path)
    a_url = UrlUtils.validate_url(path) if not a_file else False
    if a_file or a_url:
        heat_tpl = orchetration(path, parsed_params, a_file)
        #if heat_tpl:
        #    write_output(heat_tpl)
    else:
        raise ValueError(_("The path %(path)s is not a valid file or URL.") %
                         {'path': path})
Example #8
0
def main():
    if len(sys.argv) < 3:
        msg = _("The program requires minimum two arguments. "
                "Please refer to the usage documentation.")
        raise ValueError(msg)
    if "--template-file=" not in sys.argv[1]:
        msg = _("The program expects --template-file as first argument. "
                "Please refer to the usage documentation.")
        raise ValueError(msg)
    if "--template-type=" not in sys.argv[2]:
        msg = _("The program expects --template-type as second argument. "
                "Please refer to the usage documentation.")
        raise ValueError(msg)
    path = sys.argv[1].split('--template-file=')[1]
    # e.g. --template_file=translator/tests/data/tosca_helloworld.yaml
    template_type = sys.argv[2].split('--template-type=')[1]
    # e.g. --template_type=tosca
    supported_types = ['tosca']
    if not template_type:
        raise ValueError(_("Template type is needed. For example, 'tosca'"))
    elif template_type not in supported_types:
        raise ValueError(_("%(value)s is not a valid template type.")
                         % {'value': template_type})
    parsed_params = {}
    if len(sys.argv) > 3:
        parsed_params = parse_parameters(sys.argv[3])

    a_file = os.path.isfile(path)
    a_url = UrlUtils.validate_url(path) if not a_file else False
    if a_file or a_url:
        heat_tpl = translate(template_type, path, parsed_params, a_file)
        if heat_tpl:
            write_output(heat_tpl)
    else:
        raise ValueError(_("The path %(path)s is not a valid file or URL.") %
                         {'path': path})
Example #9
0
    def validate(self):
        """Validate the provided CSAR file."""

        self.is_validated = True

        # validate that the file or URL exists
        missing_err_msg = (_('"%s" does not exist.') % self.path)
        if self.a_file:
            if not os.path.isfile(self.path):
                ExceptionCollector.appendException(
                    ValidationError(message=missing_err_msg))
                return False
            else:
                self.csar = self.path
        else:  # a URL
            if not UrlUtils.validate_url(self.path):
                ExceptionCollector.appendException(
                    ValidationError(message=missing_err_msg))
                return False
            else:
                response = requests.get(self.path)
                self.csar = BytesIO(response.content)

        # validate that it is a valid zip file
        if not zipfile.is_zipfile(self.csar):
            err_msg = (_('"%s" is not a valid zip file.') % self.path)
            ExceptionCollector.appendException(
                ValidationError(message=err_msg))
            return False

        # validate that it contains the metadata file in the correct location
        self.zfile = zipfile.ZipFile(self.csar, 'r')
        filelist = self.zfile.namelist()
        if 'TOSCA-Metadata/TOSCA.meta' not in filelist:
            err_msg = (_('"%s" is not a valid CSAR as it does not contain the '
                         'required file "TOSCA.meta" in the folder '
                         '"TOSCA-Metadata".') % self.path)
            ExceptionCollector.appendException(
                ValidationError(message=err_msg))
            return False

        # validate that 'Entry-Definitions' property exists in TOSCA.meta
        data = self.zfile.read('TOSCA-Metadata/TOSCA.meta')
        invalid_yaml_err_msg = (_('The file "TOSCA-Metadata/TOSCA.meta" in '
                                  'the CSAR "%s" does not contain valid YAML '
                                  'content.') % self.path)
        try:
            meta = yaml.load(data)
            if type(meta) is dict:
                self.metadata = meta
            else:
                ExceptionCollector.appendException(
                    ValidationError(message=invalid_yaml_err_msg))
                return False
        except yaml.YAMLError:
            ExceptionCollector.appendException(
                ValidationError(message=invalid_yaml_err_msg))
            return False

        if 'Entry-Definitions' not in self.metadata:
            err_msg = (_('The CSAR "%s" is missing the required metadata '
                         '"Entry-Definitions" in '
                         '"TOSCA-Metadata/TOSCA.meta".')
                       % self.path)
            ExceptionCollector.appendException(
                ValidationError(message=err_msg))
            return False

        # validate that 'Entry-Definitions' metadata value points to an
        # existing file in the CSAR
        entry = self.metadata.get('Entry-Definitions')
        if entry and entry not in filelist:
            err_msg = (_('The "Entry-Definitions" file defined in the '
                         'CSAR "%s" does not exist.') % self.path)
            ExceptionCollector.appendException(
                ValidationError(message=err_msg))
            return False

        # validate that external references in the main template actually
        # exist and are accessible
        self._validate_external_references()
        return not self.error_caught
Example #10
0
    def main(self, argv):

        parser = self.get_parser(argv)
        (args, args_list) = parser.parse_known_args(argv)

        template_file = args.template_file
        template_type = args.template_type
        output_file = args.output_file
        validate_only = args.validate_only
        deploy = args.deploy
        stack_name = args.stack_name

        parsed_params = {}
        if args.parameters:
            parsed_params = self._parse_parameters(args.parameters)

        a_file = os.path.isfile(template_file)
        a_url = UrlUtils.validate_url(template_file) if not a_file else False
        if a_file or a_url:
            if validate_only:
                ToscaTemplate(template_file, parsed_params, a_file)
                msg = (_('The input "%(template_file)s" successfully passed '
                         'validation.') % {
                             'template_file': template_file
                         })
                print(msg)
            else:
                if keystone_client_avail:
                    try:
                        keystone_auth = (
                            loading.load_auth_from_argparse_arguments(args))
                        keystone_session = (
                            loading.load_session_from_argparse_arguments(
                                args, auth=keystone_auth))
                        images.SESSION = keystone_session
                        flavors.SESSION = keystone_session
                    except Exception:
                        keystone_session = None

                translator = self._get_translator(template_type, template_file,
                                                  parsed_params, a_file,
                                                  deploy)

                if translator and deploy:
                    if not keystone_client_avail or not heat_client_avail:
                        raise RuntimeError(
                            _('Could not find Heat or Keystone'
                              'client to deploy, aborting '))
                    if not keystone_session:
                        raise RuntimeError(
                            _('Impossible to login with '
                              'Keystone to deploy on Heat, '
                              'please check your credentials'))

                    file_name = os.path.basename(
                        os.path.splitext(template_file)[0])
                    self.deploy_on_heat(keystone_session, keystone_auth,
                                        translator, stack_name, file_name,
                                        parsed_params)

                self._write_output(translator, output_file)
        else:
            msg = (_('The path %(template_file)s is not a valid '
                     'file or URL.') % {
                         'template_file': template_file
                     })

            log.error(msg)
            raise ValueError(msg)
Example #11
0
    def main(self, args):
        self.deploy = False
        self._validate(args)
        path = args[0].split('--template-file=')[1]
        # e.g. --template_file=translator/tests/data/tosca_helloworld.yaml
        template_type = args[1].split('--template-type=')[1]
        # e.g. --template_type=tosca
        if not template_type:
            msg = _("Template type is needed. For example, 'tosca'")
            log.error(msg)
            raise ValueError(msg)
        elif template_type not in self.SUPPORTED_TYPES:
            msg = _("%(value)s is not a valid template type.") % {
                'value': template_type}
            log.error(msg)
            raise ValueError(msg)
        parsed_params = {}
        validate_only = None
        output_file = None
        if len(args) > 2:
            parameters = None
            for arg in args:
                if "--validate-only=" in arg:
                    validate_only = arg
                if "--parameters=" in arg:
                    parameters = arg
                if "--output-file=" in arg:
                    output = arg
                    output_file = output.split('--output-file=')[1]
                if "--deploy" in arg:
                    self.deploy = True
            if parameters:
                parsed_params = self._parse_parameters(parameters)
        a_file = os.path.isfile(path)
        a_url = UrlUtils.validate_url(path) if not a_file else False
        if a_file or a_url:
            run_only_validation = False
            if validate_only:
                value = validate_only.split('-validate-only=')[1].lower()
                if template_type == 'tosca' and value == 'true':
                    run_only_validation = True
            if run_only_validation:
                ToscaTemplate(path, parsed_params, a_file)
                msg = (_('The input "%(path)s" successfully passed '
                         'validation.') % {'path': path})
                print(msg)
            else:
                log.info(
                    _('Checked whether template path is a file or url path.'))
                heat_tpl = self._translate(template_type, path, parsed_params,
                                           a_file)
                if heat_tpl:
                    if utils.check_for_env_variables() and self.deploy:
                        try:
                            heatclient(heat_tpl, parsed_params)
                        except Exception:
                            log.error(_("Unable to launch the heat stack"))

                    self._write_output(heat_tpl, output_file)
        else:
            msg = _("The path %(path)s is not a valid file or URL.") % {
                'path': path}
            log.error(msg)
            raise ValueError(msg)
Example #12
0
    def validate(self):
        """Validate the provided CSAR file."""

        self.is_validated = True

        # validate that the file exists
        if self.a_file and not os.path.isfile(self.csar_file):
            err_msg = _("The file %s does not exist.") % self.csar_file
            raise ValidationError(message=err_msg)

        if not self.a_file:  # a URL
            if not UrlUtils.validate_url(self.csar_file):
                err_msg = _("The URL %s does not exist.") % self.csar_file
                raise ValidationError(message=err_msg)
            else:
                response = requests.get(self.csar_file)
                self.csar_file = BytesIO(response.content)

        # validate that it is a valid zip file
        if not zipfile.is_zipfile(self.csar_file):
            err_msg = _("The file %s is not a valid zip file.") % self.csar_file
            raise ValidationError(message=err_msg)

        # validate that it contains the metadata file in the correct location
        self.zfile = zipfile.ZipFile(self.csar_file, "r")
        filelist = self.zfile.namelist()
        if "TOSCA-Metadata/TOSCA.meta" not in filelist:
            err_msg = (
                _(
                    "The file %s is not a valid CSAR as it does not "
                    'contain the required file "TOSCA.meta" in the '
                    'folder "TOSCA-Metadata".'
                )
                % self.csar_file
            )
            raise ValidationError(message=err_msg)

        # validate that 'Entry-Definitions' property exists in TOSCA.meta
        data = self.zfile.read("TOSCA-Metadata/TOSCA.meta")
        invalid_yaml_err_msg = (
            _('The file "TOSCA-Metadata/TOSCA.meta" in %s ' "does not contain valid YAML content.") % self.csar_file
        )
        try:
            meta = yaml.load(data)
            if type(meta) is not dict:
                raise ValidationError(message=invalid_yaml_err_msg)
            self.metadata = meta
        except yaml.YAMLError:
            raise ValidationError(message=invalid_yaml_err_msg)

        if "Entry-Definitions" not in self.metadata:
            err_msg = (
                _(
                    'The CSAR file "%s" is missing the required metadata '
                    '"Entry-Definitions" in "TOSCA-Metadata/TOSCA.meta".'
                )
                % self.csar_file
            )
            raise ValidationError(message=err_msg)

        # validate that 'Entry-Definitions' metadata value points to an
        # existing file in the CSAR
        entry = self.metadata["Entry-Definitions"]
        if entry not in filelist:
            err_msg = _('The "Entry-Definitions" file defined in the CSAR ' '"%s" does not exist.') % self.csar_file
            raise ValidationError(message=err_msg)

        # validate that external references in the main template actually exist
        # and are accessible
        self._validate_external_references()
Example #13
0
    def main(self, argv):

        parser = self.get_parser(argv)
        (args, args_list) = parser.parse_known_args(argv)

        template_file = args.template_file
        template_type = args.template_type
        output_file = args.output_file
        validate_only = args.validate_only
        deploy = args.deploy
        stack_name = args.stack_name

        parsed_params = {}
        if args.parameters:
            parsed_params = self._parse_parameters(args.parameters)

        a_file = os.path.isfile(template_file)
        a_url = UrlUtils.validate_url(template_file) if not a_file else False
        if a_file or a_url:
            if validate_only:
                ToscaTemplate(template_file, parsed_params, a_file)
                msg = (_('The input "%(template_file)s" successfully passed '
                         'validation.') % {'template_file': template_file})
                print(msg)
            else:
                if keystone_client_avail:
                    try:
                        keystone_auth = (
                            loading.load_auth_from_argparse_arguments(args)
                        )
                        keystone_session = (
                            loading.load_session_from_argparse_arguments(
                                args,
                                auth=keystone_auth
                            )
                        )
                        images.SESSION = keystone_session
                        flavors.SESSION = keystone_session
                    except Exception:
                        keystone_session = None

                translator = self._get_translator(template_type,
                                                  template_file,
                                                  parsed_params, a_file,
                                                  deploy)

                if translator and deploy:
                    if not keystone_client_avail or not heat_client_avail:
                        raise RuntimeError(_('Could not find Heat or Keystone'
                                             'client to deploy, aborting '))
                    if not keystone_session:
                        raise RuntimeError(_('Impossible to login with '
                                             'Keystone to deploy on Heat, '
                                             'please check your credentials'))

                    file_name = os.path.basename(
                        os.path.splitext(template_file)[0])
                    self.deploy_on_heat(keystone_session, keystone_auth,
                                        translator, stack_name, file_name,
                                        parsed_params)

                self._write_output(translator, output_file)
        else:
            msg = (_('The path %(template_file)s is not a valid '
                     'file or URL.') % {'template_file': template_file})

            log.error(msg)
            raise ValueError(msg)