buildbot/client.py

125 lines
5.2 KiB
Python
Raw Normal View History

2019-04-05 00:21:12 +08:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
2019-09-08 17:07:47 +08:00
# client.py: Automatic management tool for an arch repo.
2019-04-05 00:21:12 +08:00
# This file is part of Buildbot by JerryXiao
import logging
2019-09-06 13:05:07 +08:00
import os
2019-04-05 00:21:12 +08:00
from multiprocessing.connection import Client
from time import sleep
2019-04-10 20:36:43 +08:00
from config import REPOD_BIND_ADDRESS, REPOD_BIND_PASSWD, \
2019-09-06 11:45:12 +08:00
MASTER_BIND_ADDRESS, MASTER_BIND_PASSWD, \
2019-09-08 17:07:47 +08:00
CONSOLE_LOGFILE, MAIN_LOGFILE
2019-04-10 20:36:43 +08:00
2019-04-05 00:21:12 +08:00
from utils import print_exc_plus
2019-04-09 15:43:17 +08:00
logger = logging.getLogger(f'buildbot.{__name__}')
2019-04-05 00:21:12 +08:00
2019-09-06 13:05:07 +08:00
abspath=os.path.abspath(__file__)
abspath=os.path.dirname(abspath)
os.chdir(abspath)
2019-04-10 20:36:43 +08:00
def run(funcname, args=list(), kwargs=dict(), retries=0, server=(REPOD_BIND_ADDRESS, REPOD_BIND_PASSWD)):
2019-04-05 00:21:12 +08:00
try:
logger.info('client: %s %s %s',funcname, args, kwargs)
2019-04-10 20:36:43 +08:00
(addr, authkey) = server
with Client(addr, authkey=authkey) as conn:
2019-04-05 00:21:12 +08:00
conn.send([funcname, args, kwargs])
return conn.recv()
except ConnectionRefusedError:
if retries <= 10:
logger.info("Server refused, retry after 60s")
sleep(60)
2019-04-10 20:36:43 +08:00
return run(funcname, args=args, kwargs=kwargs, retries=retries+1)
2019-04-05 00:21:12 +08:00
else:
logger.error("Server refused")
return False
except EOFError:
logger.error('Internal server error')
return False
except Exception:
print_exc_plus()
if __name__ == '__main__':
import argparse
2019-04-10 20:36:43 +08:00
from utils import configure_logger
configure_logger(logger)
2019-09-07 13:00:01 +08:00
def print_log(debug=False):
if debug:
2019-09-08 17:07:47 +08:00
os.system(f'tail -n 43 -f \"{MAIN_LOGFILE}\"')
2019-09-07 13:00:01 +08:00
else:
os.system(f'tail -n 43 -f \"{CONSOLE_LOGFILE}\"')
2019-04-10 20:36:43 +08:00
try:
actions = {
'info': 'show buildbot info',
'update': '[--overwrite] update pushed files to the repo',
2019-09-08 14:06:30 +08:00
'clean': '[dir / all] checkout pkgbuilds in packages',
'rebuild': '[dir1 dir2 --clean] rebuild packages',
'log': '[--debug] print log',
'upload': '[dir1 dir2 --overwrite] force upload packages',
'getup': 'check for updates now'
}
parser = argparse.ArgumentParser(description='Client for buildbot',
formatter_class=argparse.RawTextHelpFormatter)
__action_help = "\n".join([f"{a}:\t{actions[a]}" for a in actions])
parser.add_argument('action', nargs='*', help=f'Choose which action to invoke:\n\n{__action_help}')
parser.add_argument('--overwrite', nargs='?', default='False', help='overwrite existing files')
parser.add_argument('--debug', nargs='?', default='False', help='print debug logs')
parser.add_argument('--clean', nargs='?', default='True', help='clean build packages')
2019-04-10 20:36:43 +08:00
args = parser.parse_args()
action = args.action
for switch in ('overwrite', 'debug', 'clean'):
s = getattr(args, switch)
if str(s).lower() in ('false', 'no', 'n', '0'):
setattr(args, switch, False)
else:
setattr(args, switch, True)
if not (action and len(action) >= 1 and action[0] in actions):
parser.print_help()
parser.exit(status=1)
if action[0] == 'info':
2019-04-10 20:51:34 +08:00
server=(MASTER_BIND_ADDRESS, MASTER_BIND_PASSWD)
logger.info(run('info', server=server))
elif action[0] == 'getup':
server=(MASTER_BIND_ADDRESS, MASTER_BIND_PASSWD)
logger.info(run('getup', server=server))
elif action[0] == 'update':
2019-04-10 20:36:43 +08:00
server=(REPOD_BIND_ADDRESS, REPOD_BIND_PASSWD)
2019-09-08 14:06:30 +08:00
logger.info(run('update', kwargs={'overwrite': args.overwrite}, server=server))
elif action[0] == 'clean':
if len(action) <= 1:
print('Error: Need package name')
parser.print_help()
parser.exit(status=1)
2019-04-10 20:36:43 +08:00
server=(MASTER_BIND_ADDRESS, MASTER_BIND_PASSWD)
if 'all' in action[1:]:
logger.info(run('clean_all', server=server))
else:
for p in action[1:]:
logger.info(run('clean', args=(p,), server=server))
elif action[0] == 'rebuild':
if len(action) <= 1:
print('Error: Need package name')
parser.print_help()
parser.exit(status=1)
2019-04-10 20:36:43 +08:00
server=(MASTER_BIND_ADDRESS, MASTER_BIND_PASSWD)
for p in action[1:]:
logger.info(run('rebuild_package', args=(p,), kwargs={'clean': args.clean}, server=server))
elif action[0] == 'upload':
if len(action) <= 1:
print('Error: Need package name')
parser.print_help()
parser.exit(status=1)
2019-04-10 20:36:43 +08:00
server=(MASTER_BIND_ADDRESS, MASTER_BIND_PASSWD)
for p in action[1:]:
2019-09-06 15:24:48 +08:00
logger.info(run('force_upload', args=(p,), kwargs={'overwrite': args.overwrite}, server=server))
elif action[0] == 'log':
2019-04-11 16:32:39 +08:00
logger.info('printing logs')
2019-09-07 13:00:01 +08:00
print_log(debug=args.debug)
2019-04-10 20:36:43 +08:00
else:
parser.error("Please choose an action")
except Exception:
print_exc_plus()
parser.exit(status=1)