Add 'mbs-manager import_module'.

This commit is contained in:
Jan Kaluza
2018-03-09 10:47:08 +01:00
committed by mprahl
parent 1d46b332b3
commit 6fc8c646de
5 changed files with 72 additions and 18 deletions

View File

@@ -34,7 +34,7 @@ from module_build_service import app, conf, db, create_app
from module_build_service import models
from module_build_service.utils import (
submit_module_build_from_yaml,
load_local_builds,
load_local_builds, load_mmd, import_mmd
)
import module_build_service.messaging
import module_build_service.scheduler.consumer
@@ -90,6 +90,15 @@ def cleardb():
models.ComponentBuild.query.delete()
@console_script_help
@manager.command
def import_module(mmd_file):
""" Imports the module from mmd_file
"""
mmd = load_mmd(mmd_file, is_file=True)
import_mmd(db.session, mmd)
@manager.option('--stream', action='store', dest="stream")
@manager.option('--file', action='store', dest="yaml_file")
@manager.option('--skiptests', action='store_true', dest="skiptests")

View File

@@ -1796,3 +1796,60 @@ def cors_header(allow='*'):
return rv
return wrapper
return decorator
def import_mmd(session, mmd):
"""
Imports new module build defined by `mmd` to MBS database using `session`.
If it already exists, it is updated.
The ModuleBuild.koji_tag is set according to xmd['mbs]['koji_tag'].
The ModuleBuild.state is set to "ready".
The ModuleBuild.rebuild_strategy is set to "all".
The ModuleBuild.owner is set to "mbs_import".
TODO: The "context" is not stored directly in database. We only store
build_context and runtime_context and compute context, but when importing
the module, we have no idea what build_context or runtime_context is - we only
know the resulting "context", but there is no way to store it into do DB.
By now, we just ignore mmd.get_context() and use default 00000000 context instead.
"""
mmd.set_context("00000000")
name = mmd.get_name()
stream = mmd.get_stream()
version = str(mmd.get_version())
context = mmd.get_context()
# NSVC is used for logging purpose later.
nsvc = ":".join([name, stream, version, context])
# Get the koji_tag.
xmd = mmd.get_xmd()
if "mbs" in xmd.keys() and "koji_tag" in xmd["mbs"].keys():
koji_tag = xmd["mbs"]["koji_tag"]
else:
log.warn("'koji_tag' is not set in xmd['mbs'] for module %s", nsvc)
koji_tag = ""
# Get the ModuleBuild from DB.
build = models.ModuleBuild.get_build_from_nsvc(
session, name, stream, version, context)
if build:
log.info("Updating existing module build %s.", nsvc)
else:
build = models.ModuleBuild()
build.name = name
build.stream = stream
build.version = version
build.koji_tag = koji_tag
build.state = models.BUILD_STATES['ready']
build.modulemd = mmd.dumps()
build.owner = "mbs_import"
build.rebuild_strategy = 'all'
build.time_submitted = datetime.utcnow()
build.time_modified = datetime.utcnow()
build.time_completed = datetime.utcnow()
session.add(build)
session.commit()
log.info("Module %s imported", nsvc)