def test_delete_image(client, valid_headers_admin, valid_image_kernel): r = client.delete('/api/v1/image/%d' % valid_image_kernel.id, headers=valid_headers_admin) assert r.status_code == 204 assert len(Image.all()) == 0
def create_images_admin(): logger.info("Image uploaded by %s" % g.user.username) form = validations.CreateImageForm(CombinedMultiDict((request.files, request.form))) logger.info("known good %s" % form.known_good.data) logger.info("public %s" % form.public.data) if form.validate(): f = form.image.data random_suffix = binascii.hexlify(os.urandom(4)).decode('utf-8') filename = "%s.%s" % (secure_filename(f.filename), random_suffix) directory = os.path.join(app.config['TFTP_ROOT'], secure_filename(g.user.username)) if not os.path.exists(directory): os.makedirs(directory) path = os.path.join(secure_filename(g.user.username), filename) logger.info("known good %s" % form.known_good.data) new_image = Image(description=form.description.data, filename=path, file_type=form.file_type.data, known_good=form.known_good.data, user_id=g.user.id, public=form.public.data) db.session.add(new_image) try: db.session.commit() f.save(os.path.join(app.config['TFTP_ROOT'], path)) flash('File uploaded successfully', 'success') except IntegrityError as e: db.session.rollback() flash("IntegrityError, try again.", 'info') else: flash_form_errors(form) return redirect(url_for('.get_images_admin'))
def images_get(): show_all = True if request.args.get('show_all', 'false').lower() == 'true' else False if show_all: images = Image.all_visible(g.user) else: images = Image.query.filter(Image.user_id == g.user.id).all() return jsonify([serialize_image(i) for i in images]), 200
def valid_image_bootloader_no_user(db, valid_arch): image = Image(filename='bootloader', description='bootloader', file_type='bootloader', user_id=None, arch_id=valid_arch.id, known_good=False, public=True) db.session.add(image) db.session.commit() db.session.refresh(image) return image
def valid_image_initrd(db, user_admin, valid_arch): image = Image(filename='%s/initrd' % user_admin.username, description='initrd', file_type='Initrd', user_id=user_admin.id, arch_id=valid_arch.id, known_good=False, public=True) db.session.add(image) db.session.commit() db.session.refresh(image) return image
def populate_choices(form, g, machine): bmcs = BMC.query.all() if g.user.admin else [] images = Image.all_visible(g.user) preseeds = Preseed.all_visible(g.user) form.bmc_id.choices = [("", "(None)")] + \ [(bmc.id, "%s - %s - %s" % (bmc.name, bmc.ip, bmc.bmc_type)) for bmc in bmcs] form.kernel_id.choices = [("", "(None)")] + \ [(i.id, "%s - %s" % (i.description, i.filename)) for i in images if i.file_type == "Kernel"] form.initrd_id.choices = [("", "(None)")] + \ [(i.id, "%s - %s" % (i.description, i.filename)) for i in images if i.file_type == "Initrd"] form.preseed_id.choices = [("", "(None)")] + \ [(p.id, "%s - %s%s" % (p.description, p.filename, " (known good)" if p.known_good else "")) for p in preseeds] form.assignee.user_id.choices = [("", "(None)")] + \ [(u.id, u.username) for u in User.query.all()]
def image_create(): data = json.loads(request.form.get('q')) # data = request.get_json(force=True) data = image_schema.validate(data) if 'file' not in request.files: raise InvalidUsage('multipart `file` is required') if 'type' not in data: raise InvalidUsage('type is required') if 'arch' not in data: raise InvalidUsage('arch is required') arch = Arch.query.filter_by(name=data['arch']).one_or_none() if not arch: raise InvalidUsage('no such architecture') f = request.files['file'] random_suffix = binascii.hexlify(os.urandom(4)).decode('utf-8') filename = "%s.%s" % (secure_filename(f.filename), random_suffix) path = os.path.join(secure_filename(g.user.username), filename) directory = os.path.join(app.config['TFTP_ROOT'], secure_filename(g.user.username)) if not os.path.exists(directory): os.makedirs(directory) image = Image(filename=path, user_id=g.user.id, description=data.get('description', None), file_type=data.get('type'), arch_id=arch.id, public=data.get('public', False), known_good=data.get('known_good', False)) db.session.add(image) db.session.commit() f.save(os.path.join(app.config['TFTP_ROOT'], path)) db.session.refresh(image) return jsonify(serialize_image(image)), 201
def get_images_admin(): form = validations.CreateImageForm() return render_template("admin-images.html", form=form, images=Image.all_visible(g.user))
Optional('known_good'): bool, Optional('public'): bool, }, ignore_extra_keys=True) change_preseed_schema = Schema({ Optional('name'): And(str, lambda s: validators.length(s, min=2, max=256)), Optional('type'): And(str, lambda s: s in Preseed.list_types()), Optional('description'): Or(None, And(str, lambda s: validators.length(s, min=0, max=256))), Optional('content'): Or(None, And(str, lambda s: validators.length(s, min=0, max=2 * 1024 * 1024))), Optional('known_good'): bool, Optional('public'): bool, }, ignore_extra_keys=True) image_schema = Schema({ 'type': And(str, lambda s: s in Image.list_types()), Optional('description'): Or(None, And(str, lambda s: validators.length(s, min=0, max=256))), Optional('arch'): Or(None, And(str, lambda s: validators.length(s, min=0, max=256))), Optional('known_good'): bool, Optional('public'): bool, }, ignore_extra_keys=True) change_image_schema = Schema({ Optional('type'): And(str, lambda s: s in Image.list_types()), Optional('description'): Or(None, And(str, lambda s: validators.length(s, min=0, max=256))), Optional('arch'): Or(None, And(str, lambda s: validators.length(s, min=0, max=256))), Optional('known_good'): bool, Optional('public'): bool, }, ignore_extra_keys=True)
def upgrade(): bind = op.get_bind() # ### commands auto generated by Alembic - please adjust! ### op.create_table('arch', sa.Column('id', sa.Integer(), nullable=False), sa.Column('name', sa.String(), nullable=False), sa.Column('description', sa.String(), nullable=True), sa.PrimaryKeyConstraint('id'), sa.UniqueConstraint('name')) op.create_table( 'subarch', sa.Column('id', sa.Integer(), nullable=False), sa.Column('name', sa.String(), nullable=False), sa.Column('description', sa.String(), nullable=True), sa.Column('arch_id', sa.Integer(), nullable=False), sa.Column('bootloader_id', sa.Integer(), nullable=True), sa.ForeignKeyConstraint(['arch_id'], ['arch.id'], ondelete='CASCADE'), sa.ForeignKeyConstraint( ['bootloader_id'], ['image.id'], ), sa.PrimaryKeyConstraint('id')) op.create_index('subarch_arch_name_uniq', 'subarch', ['arch_id', 'name'], unique=True) op.add_column('image', sa.Column('arch_id', sa.Integer(), nullable=True)) op.create_foreign_key(None, 'image', 'arch', ['arch_id'], ['id']) op.add_column('machine', sa.Column('arch_id', sa.Integer(), nullable=True)) op.create_foreign_key(None, 'machine', 'arch', ['arch_id'], ['id']) op.add_column('machine', sa.Column('subarch_id', sa.Integer(), nullable=True)) op.create_foreign_key(None, 'machine', 'subarch', ['subarch_id'], ['id']) # ### end Alembic commands ### session = Session(bind=bind) arch = Arch(name='default') session.add(arch) session.commit() image = None bootfile = current_app.config.get('DHCP_DEFAULT_BOOTFILE') if bootfile and bootfile != '': session = Session(bind=bind) image = Image(filename=bootfile, description='Default bootloader', file_type='bootloader', arch_id=arch.id, user_id=None, known_good=True, public=True) session.add(image) session.commit() session = Session(bind=bind) subarch = Subarch(name='default', arch_id=arch.id, bootloader_id=image.id if image else None) session.add(subarch) session.commit() session = Session(bind=bind) s = sa.sql.text( 'UPDATE machine SET arch_id=:arch_id WHERE arch_id IS NULL') session.execute(s, {'arch_id': arch.id}) s = sa.sql.text( 'UPDATE machine SET subarch_id=:subarch_id WHERE subarch_id IS NULL') session.execute(s, {'subarch_id': subarch.id}) s = sa.sql.text('UPDATE image SET arch_id=:arch_id WHERE arch_id IS NULL') session.execute(s, {'arch_id': arch.id}) session.commit() op.alter_column('machine', 'arch_id', nullable=False) op.alter_column('image', 'arch_id', nullable=False)