예제 #1
0
    def handle_add(self, hostname, device_type, worker_name,
                   description, dictionary, pipeline, public, online):
        try:
            dt = DeviceType.objects.get(name=device_type)
        except DeviceType.DoesNotExist:
            self.stderr.write("Unable to find device-type '%s'" % device_type)
            sys.exit(1)
        try:
            worker = Worker.objects.get(hostname=worker_name)
        except Worker.DoesNotExist:
            self.stderr.write("Unable to find worker '%s'" % worker_name)
            sys.exit(1)

        status = Device.IDLE if online else Device.OFFLINE
        Device.objects.create(hostname=hostname, device_type=dt,
                              description=description, worker_host=worker,
                              is_pipeline=pipeline, status=status,
                              is_public=public)

        if dictionary is not None:
            data = jinja2_to_devicedictionary(dictionary.read())
            if data is None:
                self.stderr.write("Invalid device dictionary")
                sys.exit(1)
            element = DeviceDictionary(hostname=hostname)
            element.hostname = hostname
            element.parameters = data
            element.save()
예제 #2
0
    def handle(self, *args, **options):
        """
        Accept options via lava-server manage which provides access
        to the database.
        """
        hostname = options['hostname']
        if hostname is None:
            self.stderr.write("Please specify a hostname")
            sys.exit(2)
        if options['import'] is not None:
            data = parse_template(options['import'])
            element = DeviceDictionary.get(hostname)
            if element is None:
                self.stdout.write("Adding new device dictionary for %s" %
                                  hostname)
                element = DeviceDictionary(hostname=hostname)
                element.hostname = hostname
            element.parameters = data
            element.save()
            self.stdout.write("Device dictionary updated for %s" % hostname)
        elif options['export'] is not None or options['review'] is not None:
            element = DeviceDictionary.get(hostname)
            data = None
            if element is None:
                self.stderr.write(
                    "Unable to export - no dictionary found for '%s'" %
                    hostname)
                sys.exit(2)
            else:
                data = devicedictionary_to_jinja2(
                    element.parameters, element.parameters['extends'])
            if options['review'] is None:
                self.stdout.write(data)
            else:
                string_loader = jinja2.DictLoader({'%s.yaml' % hostname: data})
                type_loader = jinja2.FileSystemLoader(
                    [os.path.join(options['path'], 'device-types')])
                env = jinja2.Environment(loader=jinja2.ChoiceLoader(
                    [string_loader, type_loader]),
                                         trim_blocks=True)
                template = env.get_template("%s.yaml" % hostname)
                device_configuration = template.render()

                # validate against the device schema
                try:
                    validate_device(yaml.load(device_configuration))
                except (yaml.YAMLError, SubmissionException) as exc:
                    self.stderr.write("Invalid template: %s" % exc)

                self.stdout.write(device_configuration)
        else:
            self.stderr.write(
                "Please specify one of --import, --export or --review")
            sys.exit(1)
예제 #3
0
    def handle(self, *args, **options):
        """
        Accept options via lava-server manage which provides access
        to the database.
        """
        hostname = options['hostname']
        if hostname is None:
            self.stderr.write("Please specify a hostname")
            sys.exit(2)
        if options['import']:
            data = parse_template(options['import'])
            element = DeviceDictionary.get(hostname)
            if element is None:
                self.stdout.write("Adding new device dictionary for %s" %
                                  hostname)
                element = DeviceDictionary(hostname=hostname)
                element.hostname = hostname
            element.parameters = data
            element.save()
            self.stdout.write("Device dictionary updated for %s" % hostname)
        elif options['export'] or options['review']:
            element = DeviceDictionary.get(hostname)
            if element is None:
                self.stderr.write(
                    "Unable to export - no dictionary found for '%s'" %
                    hostname)
                sys.exit(2)
            else:
                data = devicedictionary_to_jinja2(
                    element.parameters, element.parameters['extends'])
            if not options['review']:
                self.stdout.write(data)
            else:
                template = prepare_jinja_template(hostname,
                                                  data,
                                                  system_path=False,
                                                  path=options['path'])
                device_configuration = template.render()

                # validate against the device schema
                try:
                    validate_device(yaml.load(device_configuration))
                except (yaml.YAMLError, SubmissionException) as exc:
                    self.stderr.write("Invalid template: %s" % exc)

                self.stdout.write(device_configuration)
        else:
            self.stderr.write(
                "Please specify one of --import, --export or --review")
            sys.exit(1)
예제 #4
0
    def handle_set(self, options):
        """ Set device properties """
        hostname = options["hostname"]
        try:
            device = Device.objects.get(hostname=hostname)
        except Device.DoesNotExist:
            self.stderr.write("Unable to find device '%s'" % hostname)
            sys.exit(1)

        status = options["status"]
        if status is not None:
            device.status = self.device_status[status]

        health = options["health"]
        if health is not None:
            device.health_status = self.health_status[health]

        description = options["description"]
        if description is not None:
            device.description = description

        worker_name = options["worker"]
        if worker_name is not None:
            try:
                worker = Worker.objects.get(hostname=worker_name)
                device.worker_host = worker
            except Worker.DoesNotExist:
                self.stderr.write("Unable to find worker '%s'" % worker_name)
                sys.exit(1)

        public = options["public"]
        if public is not None:
            device.is_public = public

        dictionary = options["dictionary"]
        if dictionary is not None:
            data = jinja2_to_devicedictionary(dictionary.read())
            if data is None:
                self.stderr.write("Invalid device dictionary")
                sys.exit(1)
            element = DeviceDictionary.get(hostname)
            if element is None:
                element = DeviceDictionary(hostname=hostname)
                element.hostname = hostname
            element.parameters = data
            element.save()

        # Save the modifications
        device.save()
예제 #5
0
    def handle(self, *args, **options):
        """
        Accept options via lava-server manage which provides access
        to the database.
        """
        hostname = options['hostname']
        if hostname is None:
            self.stderr.write("Please specify a hostname")
            sys.exit(2)
        if options['import'] is not None:
            data = parse_template(options['import'])
            element = DeviceDictionary.get(hostname)
            if element is None:
                self.stdout.write("Adding new device dictionary for %s" %
                                  hostname)
                element = DeviceDictionary(hostname=hostname)
                element.hostname = hostname
            element.parameters = data
            element.save()
            self.stdout.write("Device dictionary updated for %s" % hostname)
        elif options['export'] is not None or options['review'] is not None:
            element = DeviceDictionary.get(hostname)
            data = None
            if element is None:
                self.stderr.write("Unable to export - no dictionary found for '%s'" %
                                  hostname)
                sys.exit(2)
            else:
                data = devicedictionary_to_jinja2(
                    element.parameters,
                    element.parameters['extends']
                )
            if options['review'] is None:
                self.stdout.write(data)
            else:
                template = prepare_jinja_template(hostname, data, system_path=False, path=options['path'])
                device_configuration = template.render()

                # validate against the device schema
                try:
                    validate_device(yaml.load(device_configuration))
                except (yaml.YAMLError, SubmissionException) as exc:
                    self.stderr.write("Invalid template: %s" % exc)

                self.stdout.write(device_configuration)
        else:
            self.stderr.write("Please specify one of --import, --export or --review")
            sys.exit(1)
 def handle(self, *args, **options):
     """
     Accept options via lava-server manage which provides access
     to the database.
     """
     hostname = options['hostname']
     if hostname is None:
         self.stderr.write("Please specify a hostname")
         sys.exit(2)
     if options['import'] is not None:
         data = parse_template(options['import'])
         element = DeviceDictionary.get(hostname)
         if element is None:
             self.stdout.write("Adding new device dictionary for %s" %
                               hostname)
             element = DeviceDictionary(hostname=hostname)
             element.hostname = hostname
         element.parameters = data
         element.save()
         self.stdout.write("Device dictionary updated for %s" % hostname)
     elif options['export'] is not None or options['review'] is not None:
         element = DeviceDictionary.get(hostname)
         data = None
         if element is None:
             self.stderr.write("Unable to export - no dictionary found for '%s'" %
                               hostname)
             sys.exit(2)
         else:
             data = devicedictionary_to_jinja2(
                 element.parameters,
                 element.parameters['extends']
             )
         if options['review'] is None:
             self.stdout.write(data)
         else:
             string_loader = jinja2.DictLoader({'%s.yaml' % hostname: data})
             type_loader = jinja2.FileSystemLoader([
                 os.path.join(options['path'], 'device-types')])
             env = jinja2.Environment(
                 loader=jinja2.ChoiceLoader([string_loader, type_loader]),
                 trim_blocks=True)
             template = env.get_template("%s.yaml" % hostname)
             device_configuration = template.render()
             self.stdout.write(device_configuration)
     else:
         self.stderr.write("Please specify one of --import, --export or --review")
         sys.exit(1)
예제 #7
0
    def import_device_dictionary(self, hostname, jinja_str):
        """
        Name
        ----
        `import_device_dictionary` (`device_hostname`, `jinja_string`)

        Description
        -----------
        [superuser only]
        Import or update the device dictionary key value store for a
        pipeline device.
        This action will be logged.

        Arguments
        ---------
        `device_hostname`: string
            Device hostname to update.
        `jinja_str`: string
            Jinja2 settings to store in the DeviceDictionary

        Return value
        ------------
        This function returns an XML-RPC binary data of output file.
        """
        self._authenticate()
        if not self.user.is_superuser:
            raise xmlrpclib.Fault(
                403,
                "User '%s' is not superuser." % self.user.username
            )
        try:
            device = Device.objects.get(hostname=hostname)
        except DeviceType.DoesNotExist:
            raise xmlrpclib.Fault(
                404, "Device '%s' was not found." % hostname
            )
        try:
            device_data = jinja2_to_devicedictionary(jinja_str)
        except (ValueError, KeyError, TypeError):
            raise xmlrpclib.Fault(
                400, "Unable to parse specified jinja string"
            )
        if not device_data or 'extends' not in device_data:
            raise xmlrpclib.Fault(
                400, "Invalid device dictionary content - %s - not updating." % jinja_str
            )
        try:
            template = prepare_jinja_template(hostname, jinja_str, system_path=True)
        except (jinja2.TemplateError, yaml.YAMLError, IOError) as exc:
            raise xmlrpclib.Fault(
                400, "Template error: %s" % exc
            )
        if not template:
            raise xmlrpclib.Fault(400, "Empty template")
        element = DeviceDictionary.get(hostname)
        msg = ''
        if element is None:
            msg = "Adding new device dictionary for %s\n" % hostname
            element = DeviceDictionary(hostname=hostname)
            element.hostname = hostname
        element.parameters = device_data
        element.save()
        msg += "Device dictionary updated for %s\n" % hostname
        device.log_admin_entry(self.user, msg)
        return msg
예제 #8
0
파일: api.py 프로젝트: pevik/lava-server
    def import_device_dictionary(self, hostname, jinja_str):
        """
        Name
        ----
        `import_device_dictionary` (`device_hostname`, `jinja_string`)

        Description
        -----------
        [superuser only]
        Import or update the device dictionary key value store for a
        pipeline device.

        Arguments
        ---------
        `device_hostname`: string
            Device hostname to update.
        `jinja_str`: string
            Jinja2 settings to store in the DeviceDictionary

        Return value
        ------------
        This function returns an XML-RPC binary data of output file.
        """
        self._authenticate()
        if not self.user.is_superuser:
            raise xmlrpclib.Fault(
                403,
                "User '%s' is not superuser." % self.user.username
            )
        try:
            Device.objects.get(hostname=hostname)
        except DeviceType.DoesNotExist:
            raise xmlrpclib.Fault(
                404, "Device '%s' was not found." % hostname
            )
        try:
            device_data = jinja2_to_devicedictionary(jinja_str)
        except (ValueError, KeyError, TypeError):
            raise xmlrpclib.Fault(
                400, "Unable to parse specified jinja string"
            )
        if not device_data or 'extends' not in device_data:
            raise xmlrpclib.Fault(
                400, "Invalid device dictionary content - %s - not updating." % jinja_str
            )
        try:
            template = prepare_jinja_template(hostname, jinja_str, system_path=True)
        except (jinja2.TemplateError, yaml.YAMLError, IOError) as exc:
            raise xmlrpclib.Fault(
                400, "Template error: %s" % exc
            )
        if not template:
            raise xmlrpclib.Fault(400, "Empty template")
        element = DeviceDictionary.get(hostname)
        msg = ''
        if element is None:
            msg = "Adding new device dictionary for %s\n" % hostname
            element = DeviceDictionary(hostname=hostname)
            element.hostname = hostname
        element.parameters = device_data
        element.save()
        msg += "Device dictionary updated for %s\n" % hostname
        return msg