blob: b444c490e2a28879b78de29ee4b2ffac01950dfc [file] [log] [blame]
import getpass
import smtplib
import subprocess
import sys
import common # pylint: disable-msg=W0611
from autotest_lib.site_utils.lib import infra
print 'Pulling servers...'
SCHEDULERS = infra.sam_servers()
DRONES = infra.drone_servers()
EXTRAS = infra.extra_servers()
TEST_INSTANCE = infra.test_instance()
# List of deployment actions that can be requested, in the order
# in which they'll be executed.
deploy_actions = [
('sync', ALL_SERVERS.union(EXTRAS), 'cd /usr/local/autotest ; '
'repo sync'),
('build_externals', ALL_SERVERS, 'cd /usr/local/autotest ; '
('test_importer', SCHEDULERS, 'cd /usr/local/autotest ; '
('migrate', SCHEDULERS, 'cd /usr/local/autotest ; '
'./database/ sync'),
('afe', SCHEDULERS, 'cd /usr/local/autotest ; '
'./utils/ -c autotest.AfeClient'),
('tko', SCHEDULERS, 'cd /usr/local/autotest ; '
'./utils/ -c autotest.TkoClient'),
('apache', SCHEDULERS, 'sudo service apache2 restart'),
('scheduler', SCHEDULERS, 'sudo service scheduler restart'),
('host_scheduler', SCHEDULERS, 'sudo service host-scheduler restart'),
('suite_scheduler', {'cautotest'},
'sudo service suite-scheduler restart'),
('gs_offloader', DRONES, 'sudo service gs_offloader restart ; '
'sudo service gs_offloader_s restart'),
('stats_poller', {'chromeos-mcp'}, 'sudo service stats-poller restart'),
print 'Updating checkout...'
subprocess.check_call('git fetch', shell=True)
start = subprocess.check_output('git ls-remote cros prod',
end = subprocess.check_output('git ls-remote cros master',
changes = subprocess.check_output('git log %s..%s --oneline' % (start, end),
deploys = subprocess.check_output(
'git log %s..%s | grep DEPLOY= | '
'sed "s/DEPLOY=//" | tr "\n-" ",_"' %
(start, end), shell=True).split(',')
deploys = set([x.strip() for x in deploys if x])
# Always sync.
deploys = deploys.union(set(sys.argv[1:]))
valid_deploys = set([deploy[0] for deploy in deploy_actions])
unknowns = deploys - valid_deploys
if unknowns:
raise Exception('Unknown deploy actions: %s' % list(unknowns))
if not TESTING:
print 'Running cbf test...'
infra.execute_command(TEST_INSTANCE, '~/update_autotest')
print infra.execute_command(TEST_INSTANCE, 'cd /usr/local/autotest ;'
if not TESTING:
print 'Pushing prod...'
subprocess.check_call('git rebase cros/master prod', shell=True)
subprocess.check_call('git push cros prod:prod', shell=True)
print 'Deploying...'
actions = []
deploy_order = [deploy for deploy in deploy_actions
if deploy[0] in deploys]
for deploy, servers, action in deploy_order:
for server in servers:
print '%s $ %s' % (server, action)
if not TESTING:
infra.execute_command(server, action)
actions.append((server, deploy))
user = getpass.getuser()
usermail = '' % user
EMAIL = """\
Subject: push to prod %(start)s..%(end)s
From: %(user)s <%(usermail)s>
To: ChromeOS Lab Infrastructure <>
""" % dict(start=start[:7], end=end[:7],
changes=changes, user=user, usermail=usermail,
actions='\n'.join(['%s %s' % (x,y) for x, y in actions]))
print EMAIL
smtp = smtplib.SMTP('localhost')
if not TESTING:
smtp.sendmail(usermail, [''], EMAIL)