示例#1
0
 def __init__(self, *args, **kwargs):
     base_class.__init__(self, *args, **kwargs)
     for field_name in field_names:
         path = get_file_field_base_path(model_class, field_name)
         field = model_class._meta.get_field_by_name(field_name)[0]
         choices = []
         for name in os.listdir(path):
             if extensions:
                 for extension in extensions:
                     if name.endswith(extension):
                         choices.append((name, name))
                         break
             else:
                 choices.append((name, name))
         if field.blank:
             choices = (("empty", "---------"),) + tuple(choices)
             self.fields[field_name].required = False
         self.fields[field_name].widget = forms.widgets.Select(choices=choices)
示例#2
0
 def __init__(self, *args, **kwargs):
     base_class.__init__(self, *args, **kwargs)
     for field_name in field_names:
         # FIXME path is invalid on dynamic upload_to + settings override
         path = get_file_field_base_path(model_class, field_name)
         field = model_class._meta.get_field_by_name(field_name)[0]
         choices = []
         for name in os.listdir(path):
             # get file path relative to MEDIA_ROOT
             file_path = os.path.join(path, name)
             file_path = os.path.relpath(file_path, settings.MEDIA_ROOT)
             if extensions:
                 for extension in extensions:
                     if name.endswith(extension):
                         choices.append((file_path, name))
                         break
             else:
                 choices.append((file_path, name))
         if field.blank:
             choices = (("empty", "---------"),) + tuple(choices)
             self.fields[field_name].required = False
         self.fields[field_name].widget = forms.widgets.Select(choices=choices)
示例#3
0
 def __init__(self, *args, **kwargs):
     base_class.__init__(self, *args, **kwargs)
     for field_name in field_names:
         #FIXME path is invalid on dynamic upload_to + settings override
         path = get_file_field_base_path(model_class, field_name)
         field = model_class._meta.get_field_by_name(field_name)[0]
         choices = []
         for name in os.listdir(path):
             # get file path relative to MEDIA_ROOT
             file_path = os.path.join(path, name)
             file_path = os.path.relpath(file_path, settings.MEDIA_ROOT)
             if extensions:
                 for extension in extensions:
                     if name.endswith(extension):
                         choices.append((file_path, name))
                         break
             else:
                 choices.append((file_path, name))
         if field.blank:
             choices = (('empty', '---------'), ) + tuple(choices)
             self.fields[field_name].required = False
         self.fields[field_name].widget = forms.widgets.Select(
             choices=choices)
示例#4
0
def build(build_id, *args, **kwargs):
    """ Builds a firmware image for build.node, excluding exclude files """
    # Avoid circular imports
    from firmware.models import Build, Config
    
    exclude = kwargs.get('exclude', [])
    
    # retrieve the existing build instance, used for user feedback
    update_state(build, 1, 4, 'Build started')
    build_obj = Build.objects.get(pk=build_id)
    build_obj.task_id = build.request.id
    build_obj.save()
    
    config = Config.objects.get()
    node = build_obj.node
    image = Image(build_obj.base_image.path)
    
    try:
        # Build the image
        image.prepare()
        
        update_state(build, 5, 14, 'Unpackaging base image')
        image.gunzip()
        
        update_state(build, 15, 29, 'Preparing image file system')
        image.mount()
        
        # handle registry api #245: put cert content into firmware image
        build_file = build_obj.files.get(path='/etc/confine/registry-server.crt')
        image.create_file(build_file)
        
        files = config.eval_files(node, exclude=exclude, image=image)
        total = len(files)
        for num, build_file in enumerate(files):
            current = 30 + num/total*25
            next = min(30 + (num+1)/total*25, 59)
            update_state(build, current, next, 'Generating %s file' % build_file.name)
            image.create_file(build_file)
            build_file.build = build_obj
            build_file.save()
        
        # generate binary versions of uhttpd key and certificate #410
        from .models import BuildFile # avoid circular imports
        for pem_path in ['/etc/uhttpd.crt.pem', '/etc/uhttpd.key.pem']:
            try:
                pem_file = build_obj.files.get(path=pem_path)
            except BuildFile.DoesNotExist:
                pass
            else:
                der_content = pem2der_string(pem_file.content)
                der_path = pem_path.rstrip('.pem')
                der_file = BuildFile(path=der_path, content=der_content, config=pem_file.config)
                image.create_file(der_file)
        
        plugins = config.plugins.active()
        total = len(plugins)
        for num, plugin in enumerate(plugins):
            plugin.instance.pre_umount(image, build_obj, *args, **kwargs)
        
        update_state(build, 60, 74, 'Unmounting image file system')
        image.umount()
        
        # Post umount
        for num, plugin in enumerate(plugins):
            # FIXME this progress is not correct
            current = 75 + num/total*25
            next = min(75 + (num+1)/total*25, 80)
            instance = plugin.instance
            update_state(build, current, next, instance.post_umount.__doc__)
            instance.post_umount(image, build_obj, *args, **kwargs)
        
        update_state(build, 80, 94, 'Compressing image')
        image.gzip()
        
        update_state(build, 95, 99, 'Cleaning up')
        
        # Image name
        image_name = config.get_image_name(node, build_obj)
        for plugin in config.plugins.active():
            image_name = plugin.instance.update_image_name(image_name, **kwargs)
        base_path = get_file_field_base_path(Build, 'image')
        dest_path = os.path.join(base_path, image_name)
        image.move(dest_path)
    finally:
        image.clean()
    
    build_obj.image = dest_path
    build_obj.save()
    return { 'progress': 100, 'description': 'Redirecting', 'result': dest_path }
示例#5
0
def build(build_id, *args, **kwargs):
    """ Builds a firmware image for build.node, excluding exclude files """
    # Avoid circular imports
    from firmware.models import Build, Config

    exclude = kwargs.get('exclude', [])

    # retrieve the existing build instance, used for user feedback
    update_state(build, 1, 4, 'Build started')
    build_obj = Build.objects.get(pk=build_id)
    build_obj.task_id = build.request.id
    build_obj.save()

    config = Config.objects.get()
    node = build_obj.node
    image = Image(build_obj.base_image.path)

    try:
        # Build the image
        image.prepare()

        update_state(build, 5, 14, 'Unpackaging base image')
        image.gunzip()

        update_state(build, 15, 29, 'Preparing image file system')
        image.mount()

        # handle registry api #245: put cert content into firmware image
        build_file = build_obj.files.get(
            path='/etc/confine/registry-server.crt')
        image.create_file(build_file)

        files = config.eval_files(node, exclude=exclude, image=image)
        total = len(files)
        for num, build_file in enumerate(files):
            current = 30 + num / total * 25
            next = min(30 + (num + 1) / total * 25, 59)
            update_state(build, current, next,
                         'Generating %s file' % build_file.name)
            image.create_file(build_file)
            build_file.build = build_obj
            build_file.save()

        # generate binary versions of uhttpd key and certificate #410
        from .models import BuildFile  # avoid circular imports
        for pem_path in ['/etc/uhttpd.crt.pem', '/etc/uhttpd.key.pem']:
            try:
                pem_file = build_obj.files.get(path=pem_path)
            except BuildFile.DoesNotExist:
                pass
            else:
                der_content = pem2der_string(pem_file.content)
                der_path = pem_path.rstrip('.pem')
                der_file = BuildFile(path=der_path,
                                     content=der_content,
                                     config=pem_file.config)
                image.create_file(der_file)

        plugins = config.plugins.active()
        total = len(plugins)
        for num, plugin in enumerate(plugins):
            plugin.instance.pre_umount(image, build_obj, *args, **kwargs)

        update_state(build, 60, 74, 'Unmounting image file system')
        image.umount()

        # Post umount
        for num, plugin in enumerate(plugins):
            # FIXME this progress is not correct
            current = 75 + num / total * 25
            next = min(75 + (num + 1) / total * 25, 80)
            instance = plugin.instance
            update_state(build, current, next, instance.post_umount.__doc__)
            instance.post_umount(image, build_obj, *args, **kwargs)

        update_state(build, 80, 94, 'Compressing image')
        image.gzip()

        update_state(build, 95, 99, 'Cleaning up')

        # Image name
        image_name = config.get_image_name(node, build_obj)
        for plugin in config.plugins.active():
            image_name = plugin.instance.update_image_name(
                image_name, **kwargs)
        base_path = get_file_field_base_path(Build, 'image')
        dest_path = os.path.join(base_path, image_name)
        image.move(dest_path)
    finally:
        image.clean()

    build_obj.image = dest_path
    build_obj.save()
    return {'progress': 100, 'description': 'Redirecting', 'result': dest_path}