From: y0169.zhang Date: Tue, 31 Oct 2017 01:58:08 +0000 (+0800) Subject: Add subcommand(deploy) for deploy icecream X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fheads%2Fsandbox%2Ficecream;p=tools%2Fgbs.git Add subcommand(deploy) for deploy icecream Use gbs build to construct the buildroot for icecc-scheduler and icccd, and run icecc-scheduler and iceccd through chroot into buildroot Change-Id: I4a7d35bcf7055406528f3b55e3728901582e68e4 --- diff --git a/debian/gbs.install b/debian/gbs.install index 583765b..07873b2 100644 --- a/debian/gbs.install +++ b/debian/gbs.install @@ -7,6 +7,7 @@ usr/lib/python*/*packages/gitbuildsys/cmd_devel.py usr/lib/python*/*packages/gitbuildsys/cmd_import.py usr/lib/python*/*packages/gitbuildsys/cmd_pull.py usr/lib/python*/*packages/gitbuildsys/cmd_submit.py +usr/lib/python*/*packages/gitbuildsys/cmd_deploy.py usr/lib/python*/*packages/gitbuildsys/parsing.py usr/bin/* etc/bash_completion.d/* diff --git a/gitbuildsys/cmd_deploy.py b/gitbuildsys/cmd_deploy.py new file mode 100644 index 0000000..1ae1ca4 --- /dev/null +++ b/gitbuildsys/cmd_deploy.py @@ -0,0 +1,190 @@ +#!/usr/bin/python -tt +# vim: ai ts=4 sts=4 et sw=4 +# +# Copyright (c) 2012 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: deploy +""" + +import os +import errno +import subprocess +from multiprocessing import cpu_count + +from gitbuildsys.utils import Temp, Workdir +from gitbuildsys.log import LOGGER as log +from gitbuildsys.errors import GbsError + +fake_spec = '\ +Name: fake\n\ +Summary: A fake tizen package for gbs deploy\n\ +Version: 1.0\n\ +Release: 1\n\ +Group: Development/Tools\n\ +License: GPLv2\n\ +Source0: %{name}-%{version}.tbz2\n\ +\n\ +%description\n\ +A fake tizen package for gbs deploy\n\ +* download and install gbs\n\ +* use this package to test gbs build, remotebuild, export, import and so on\n\ +%prep\n\ +%setup -q\n\ +\n\ +%install\n\ +mkdir -p %{buildroot}/%{_docdir}\n\ +' + +gbs_conf = '\ +[general]\n\ +fallback_to_native = true\n\ +profile = profile.unified_standard\n\ +\n\ +[repo.base_arm]\n\ +url = http://download.tizen.org/snapshots/tizen/4.0-base/latest/repos/arm/packages/\n\ +[repo.base_arm64]\n\ +url = http://download.tizen.org/snapshots/tizen/4.0-base/latest/repos/arm64/packages/\n\ +[repo.base_ia32]\n\ +url = http://download.tizen.org/snapshots/tizen/4.0-base/latest/repos/ia32/packages/\n\ +[repo.base_x86_64]\n\ +url = http://download.tizen.org/snapshots/tizen/4.0-base/latest/repos/x86_64/packages/\n\ +\n\ +[profile.unified_standard]\n\ +repos = repo.base_arm,repo.base_arm64,repo.base_ia32,repo.base_x86_64\n\ +' + +def fake_buildroot(buildroot_name): + tmp = Temp(prefix='gbs_deploy_', + dirn='/var/tmp', + directory=True) + conf = tmp.path + '/.gbs.conf' + with open(conf, 'w') as f: + f.write(gbs_conf) + + spec = tmp.path + '/fake.spec' + with open(spec, 'w') as f: + f.write(fake_spec) + + cmds = [['mkdir', '-p', 'fake/packaging'], + ['cp', 'fake.spec', 'fake/packaging'], + ['git', 'init', 'fake'], + ['git', '--git-dir=fake/.git/', '--work-tree=fake/', 'add', '.'], + ['git', '--git-dir=fake/.git/', '--work-tree=fake/', 'commit', '-m', '"Init commit"'], + ['gbs', 'build', '-A', 'armv7l', '--clean', '--overwrite', '-B', buildroot_name, 'fake']] + with Workdir(tmp.path): + try: + for cmd in cmds: + retcode = os.system(' '.join(cmd)) + except (OSError): + raise GbsError("failed to run %s in %s" % (' '.join(cmd), tmp.path)) + +def deploy_scheduler(): + log.info('Initialize the environment for icecc scheduler, this may take several minutes...') + buildroot = os.path.expanduser('~/icecc-scheduler-buildroot/') + fake_buildroot(buildroot) + scratch = buildroot + 'local/BUILD-ROOTS/scratch.armv7l.0/' + log.info('Startup icecc scheduler ...') + cmd = ['sudo', 'chroot', scratch, '/emul/usr/sbin/icecc-scheduler', '-d', '-r', '-l', '/var/log/icecc-scheduler.log'] + ret = 0 + try: + ret = os.system(' '.join(cmd)) + except (OSError): + raise GbsError("failed to run %s in %s" % (' '.join(cmd), os.getcwd())) + + return ret + +def deploy_worker(scheduler): + log.info('Initialize the environment for icecc daemon, this may take several minutes...') + buildroot = os.path.expanduser('~/iceccd-buildroot/') + fake_buildroot(buildroot) + scratch = buildroot + 'local/BUILD-ROOTS/scratch.armv7l.0/' + icecc_dir = scratch + 'run/icecc/' + max_jobs = cpu_count()*2 + log.info('Startup icecc daemon...') + cmds = [['sudo', 'mount', '-t', 'proc', '/proc/', scratch+'proc/'], + ['sudo', 'chroot', scratch, '/emul/usr/sbin/iceccd', '-s', scheduler, '-m', str(max_jobs), '-d', '-l', '/var/log/iceccd.log'], + ['sudo', 'mkdir', '-p', '/run/icecc/'], + ['sudo', 'mount', '-o', 'bind', icecc_dir, '/run/icecc/']] + ret = 0 + try: + for cmd in cmds: + ret = os.system(' '.join(cmd)) + except (OSError): + raise GbsError("failed to run %s in %s" % (' '.join(cmd), os.getcwd())) + + return ret + +def clean_scheduler(): + log.info('Clean the icecc scheduler') + buildroot = os.path.expanduser('~/iceccd-buildroot/') + scratch = buildroot + 'local/BUILD-ROOTS/scratch.armv7l.0/' + cmd = 'ps -ef |grep icecc-scheduler |grep -v grep |awk "{print \$2}"' + ret = subprocess.check_output(cmd, shell=True) + pid = ret.strip('\n') + if pid != '': + cmd = ('sudo chroot %s kill -9 %s'% (scratch, pid)) + ret = subprocess.call(cmd, shell=True) + if ret != 0: + log.error("Execute command %s failed!" %cmd) + + return 0 + +def clean_worker(): + log.info('Clean the icecc daemon') + buildroot = os.path.expanduser('~/iceccd-buildroot/') + scratch = buildroot + 'local/BUILD-ROOTS/scratch.armv7l.0/' + cmd = 'ps -ef |grep iceccd |grep -v grep |awk "{print \$2}"' + ret = subprocess.check_output(cmd, shell=True) + pid = ret.strip('\n') + if pid != '': + cmd = ('sudo chroot %s kill -9 %s'% (scratch, pid)) + ret = subprocess.call(cmd, shell=True) + if ret != 0: + log.error("Execute command %s failed!" %cmd) + + cmd = 'sudo umount -l /run/icecc/' + subprocess.call(cmd, shell=True) + cmd = ('sudo umount -l %s/proc/' %scratch) + subprocess.call(cmd, shell=True) + + return 0 + +def main(args): + """gbs deploy entrypoint.""" + + retcode = 0 + if args.role == 'scheduler': + clean_scheduler() + if args.clean_only: + return + + retcode = deploy_scheduler() + elif args.role == 'worker': + clean_worker() + if args.clean_only: + return + + if not args.scheduler_address: + raise GbsError("must specify the address of scheduler") + + retcode = deploy_worker(args.scheduler_address) + else: + raise GbsError('Invalid role, only support scheduler and worker') + + if retcode != 0: + raise GbsError('deploy %s failed' %args.role) + else: + log.info('Done') diff --git a/packaging/gbs.spec b/packaging/gbs.spec index e1dbe83..9011661 100755 --- a/packaging/gbs.spec +++ b/packaging/gbs.spec @@ -146,6 +146,7 @@ rm -rf %{buildroot} %{python_sitelib}/gitbuildsys/cmd_import.py* %{python_sitelib}/gitbuildsys/cmd_pull.py* %{python_sitelib}/gitbuildsys/cmd_submit.py* +%{python_sitelib}/gitbuildsys/cmd_deploy.py* %{python_sitelib}/gitbuildsys/parsing.py* %{_bindir}/* %{_sysconfdir}/bash_completion.d diff --git a/tools/gbs b/tools/gbs index edc6823..d066f01 100755 --- a/tools/gbs +++ b/tools/gbs @@ -563,6 +563,24 @@ def devel_parser(parser): help='Action to take') return parser +@subparser +def deploy_parser(parser): + """deploy icecream for gbs build + Examples: + $ gbs deploy --role=scheduler + $ gbs deploy --role=worker --scheduler-address=109.123.123.123 + $ gbs deploy --role=scheduler --clean-only + """ + + parser.add_argument('-r', '--role', required=True, + help='The role to deploy, valid role:scheduler, worker') + parser.add_argument('-s', '--scheduler-address', + help='The adress of scheduler for connect. ' + 'Must specify this option if the role is worker.') + parser.add_argument('--clean-only', action='store_true', + help='Only clean the icecream, this option will not deploy icecream') + return parser + def main(argv): """Script entry point."""