add createimage subcommand
authorZhang Qiang <qiang.z.zhang@intel.com>
Wed, 17 Apr 2013 02:37:02 +0000 (10:37 +0800)
committerZhang Qiang <qiang.z.zhang@intel.com>
Tue, 23 Apr 2013 09:52:39 +0000 (17:52 +0800)
Input:
    ks file specified by --ks-file. Or,
    ks directory, configured in gbs.conf

Output:
    image created by mic, the output dir is configured in gbs.conf
    with image_dir key.

createimage subcommand also support creating special profile image
with -P/--profile.

Change-Id: If5651c19f0a15730bbce2868e44308fb9a000e9f

data/gbs.sh
gitbuildsys/cmd_createimage.py [new file with mode: 0644]
tools/gbs

index de916db..6b3abd3 100644 (file)
@@ -76,7 +76,7 @@ __gbs_find_on_cmdline ()
 __gbs ()
 {
     subcommands="
-        build remotebuild submit import export changelog chroot clone pull
+        build createimage remotebuild submit import export changelog chroot clone pull
     "
     common_opts="--upstream-tag= --upstream-branch= --squash-patches-until=
         --packaging-dir= --no-patch-export"
@@ -86,6 +86,10 @@ __gbs ()
         --skip-conf-repos --profile= --noinit --keep-packs
         --clean-repos --define --baselibs
     "
+    cr_opts="
+        --profile= --tmpfs --ks-file
+    "
+
     rb_opts="
         --base-obsprj= --target-obsprj= --spec= --commit= --include-all
         --status --buildlog --profile= --arch= --repository=
@@ -102,7 +106,7 @@ __gbs ()
         --source-rpm --include-all --commit= --spec= --outdir=
     "
     ch_opts="--message= --since= --packaging-dir="
-    cr_opts="--root"
+    chr_opts="--root"
     lbex_opts="--no-configure --exclude-from-file= --exclude= --binary-list= --threads=\
         --incremental --overwrite --clean-once --debug --deps --rdeps $lb_opts"
     cl_opts="--upstream-branch= --all --depth="
@@ -123,6 +127,9 @@ __gbs ()
             build,--*)
                 __gbscomp "$lb_opts $lbex_opts $common_opts"
                 ;;
+            createimage,--*)
+                __gbscomp "$cr_opts"
+                ;;
             remotebuild,--*)
                 __gbscomp "$rb_opts $common_opts"
                 ;;
@@ -139,7 +146,7 @@ __gbs ()
                 __gbscomp "$ch_opts"
                 ;;
             chroot,--*)
-                __gbscomp "$cr_opts"
+                __gbscomp "$chr_opts"
                 ;;
             clone,--*)
                 __gbscomp "$cl_opts"
diff --git a/gitbuildsys/cmd_createimage.py b/gitbuildsys/cmd_createimage.py
new file mode 100644 (file)
index 0000000..ffb69de
--- /dev/null
@@ -0,0 +1,68 @@
+#!/usr/bin/python -tt
+# vim: ai ts=4 sts=4 et sw=4
+#
+# Copyright (c) 2013 Intel, Inc.
+#
+# This program is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the Free
+# Software Foundation; version 2 of the License
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+# for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc., 59
+# Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+"""Implementation of subcmd: createimage
+"""
+
+import os
+import glob
+
+from gitbuildsys.errors import GbsError
+from gitbuildsys.cmd_build import get_profile
+from gitbuildsys.conf import configmgr
+from gitbuildsys.log import LOGGER as log
+
+def createimage(args, ks_file, outdir):
+    '''create image using mic'''
+    extra_mic_opts = ['--outdir=%s' % outdir]
+    if args.tmpfs:
+        extra_mic_opts += ['--tmpfs']
+    extra_mic_opts += ['--record-pkgs=name']
+    mic_cmd = 'sudo mic create auto %s %s' % (ks_file, ' '.join(extra_mic_opts))
+    log.debug(mic_cmd)
+    os.system(mic_cmd)
+
+def main(args):
+    '''main entrance for createimage'''
+    profile = get_profile(args)
+    if profile.image_dir:
+        image_dir = profile.image_dir
+    else:
+        image_dir = configmgr.get('image_dir', 'general')
+    image_dir = os.path.expanduser(image_dir)
+
+    if args.ks_file:
+        if not os.path.exists(args.ks_file):
+            raise GbsError('specified ks file: not exists' % args.ks_file)
+        log.info('creating image for ks file: %s' % args.ks_file)
+        createimage(args, args.ks_file, image_dir)
+    else:
+        if profile.ks_dir:
+            ks_dir = profile.ks_dir
+        else:
+            ks_dir = configmgr.get('ks_dir', 'general')
+
+        ks_dir = os.path.expanduser(ks_dir)
+        ks_list = glob.glob(os.path.join(ks_dir, '*.ks'))
+        if not ks_list:
+            raise GbsError('no avaliable ks file found in ks dir:%s' % ks_dir)
+
+        log.debug('avaliable ks files are:\n %s '% ' '.join(ks_list))
+        for ks_file in ks_list:
+            log.info('creating image for ks file: %s' % ks_file)
+            createimage(args, ks_file, image_dir)
index 404b3b9..e81d3c3 100755 (executable)
--- a/tools/gbs
+++ b/tools/gbs
@@ -262,6 +262,23 @@ def build_parser(parser):
     return parser
 
 @subparser
+def createimage_parser(parser):
+    """create image using ks file """
+
+    parser.add_argument('-K', '--ks-file',
+                        help='ks file to be used')
+    parser.add_argument('-P', '--profile',
+                        help='profile to be used for building, can be given '
+                             'without the "profile." prefix')
+    parser.add_argument('--tmpfs', action='store_true',
+                        help='use tmpfs to accelerate creating image, but '\
+                             'plesae make sure you have enough free memory '\
+                             'for whole image')
+    parser.set_defaults(alias="cr")
+    return parser
+
+
+@subparser
 def remotebuild_parser(parser):
     """remote build package
     Examples: