--- /dev/null
+#!/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')