def publish_build_ouput(container=None, bld_obj=None, upd_repo=False, is_iso=False): """ :param container: :param bld_obj: :param upd_repo: :param is_iso: :return: """ if not container or not bld_obj: logger.error('Unable to publish build output. (Container is None)') return # proc = subprocess.Popen(['docker', 'logs', '--follow', container], stdout=subprocess.PIPE) # output = iter(proc.stdout.readline, '') output = doc.logs(container=container, stream=True) nodup = set() content = [] for line in output: # time.sleep(.10) if not line or line == '' or 'makepkg]# PS1="' in line: continue line = line.rstrip() end = line[25:] if end not in nodup or (end in nodup and 'UTF-8' in end): nodup.add(end) # line = re.sub(r'(?<=[\w\d])(( \')|(\' )(?=[\w\d]+))|(\'\n)', ' ', line) line = line.replace("'", '') line = line.replace('"', '') line = '[%s]: %s' % (datetime.datetime.now().strftime("%m/%d/%Y %I:%M%p"), line) if len(line) > 150: line = truncate_middle(line, 150) content.append(line) db.publish('build-output', line) db.set('build_log_last_line', line) result_ready = bld_obj.completed != bld_obj.failed if not result_ready: while not result_ready: result_ready = bld_obj.completed != bld_obj.failed time.sleep(3) failed = bld_obj.failed if upd_repo or failed: db.publish('build-output', 'ENDOFLOG') log = bld_obj.log existing = True if len(log) < 1 and not failed and not is_iso: existing = False for line in content: log.rpush(line) if existing: log_content = '\n '.join(log) pretty = highlight(log_content, BashLexer(), HtmlFormatter(style='monokai', linenos='inline', prestyles="background:#272822;color:#fff;", encoding='utf-8')) bld_obj.log_str = pretty
def publish_build_ouput(container=None, bld_obj=None, upd_repo=False): if not container or not bld_obj: logger.error('Unable to publish build output. (Container is None)') return # proc = subprocess.Popen(['docker', 'logs', '--follow', container], stdout=subprocess.PIPE) # output = iter(proc.stdout.readline, '') output = doc.logs(container, stream=True) nodup = set() content = [] for line in output: time.sleep(.10) if not line or line == '' or "Antergos Automated Build Server" in line or "--passphrase" in line \ or 'makepkg]# PS1="' in line: continue line = line.rstrip() # if db.get('isoBuilding') == "True": # line = line[15:] end = line[25:] if end not in nodup: nodup.add(end) line = re.sub('(?<=[\w\d]) \'(?=[\w\d]+)', ' ', line) # if line[-1:] == "'" or line[-1:] == '"': # line = line[:-1] line = re.sub('(?<=[\w\d])\' (?=[\w\d]+)', ' ', line) # bad_date = re.search(r"\d{4}-\d{2}-[\d\w:\.]+Z{1}", line) # if bad_date: # line = line.replace(bad_date.group(0), datetime.datetime.now().strftime("%m/%d/%Y %I:%M%p")) line = '[%s]: %s' % (datetime.datetime.now().strftime("%m/%d/%Y %I:%M%p"), line) if len(line) > 150: line = truncate_middle(line, 120) content.append(line) db.publish('build-output', line) db.set('build_log_last_line', line) if upd_repo: db.publish('build-output', 'ENDOFLOG') # content = '\n '.join(content) log = bld_obj.log() existing = True if not log or len(log) < 1: existing = False log.rpush(content) if existing: log_content = '\n '.join(log) pretty = highlight(log_content, BashLexer(), HtmlFormatter(style='monokai', linenos='inline', prestyles="background:#272822;color:#fff;", encoding='utf-8')) bld_obj.log_str = pretty
def build_pkgs(pkg_info=None): """ :param last: :param pkg_info: :return: """ if pkg_info is None: return False # Create our tmp directories result = '/tmp/result' cache = '/var/tmp/pkg_cache' for d in [result, cache, '/var/tmp/32build', '/var/tmp/32bit']: if os.path.exists(d) and 'pkg_cache' not in d: shutil.rmtree(d) os.makedirs(d, 0o777) elif os.path.exists(d) and 'pkg_cache' in d: logger.info('@@-build_pkg.py-@@ 476 | Cleaning package cache....') status.current_status = 'Cleaning package cache.' for pcache in os.listdir(d): pcache = os.path.join(d, pcache) if not os.path.isdir(pcache): logger.error('@@-build_pkg.py-@@ 479 | pcache is not a directory') continue for pfile in os.listdir(pcache): pname = re.search('^([a-z]|[0-9]|-|_)+(?=-\d|r|v)', pfile) if not pname or pname == '': continue pname = pname.group(0) pfile = os.path.join(pcache, pfile) dtime = time.time() if os.stat(pfile).st_mtime < (dtime - (7 * 86400)) or status.all_packages.ismember(pname): remove(pfile) else: os.makedirs(d, 0o777) pkglist1 = ['1'] in_dir_last = len([name for name in os.listdir(result)]) db.set('pkg_count', in_dir_last) for i in range(len(pkglist1)): pkg = pkg_info.name if pkg and pkg is not None and pkg != '': pkgbuild_dir = pkg_info.build_path pkg_deps = pkg_info.depends or [] pkg_deps_str = ' '.join(pkg_deps) if pkg_deps else '' bld_obj = process_and_save_build_metadata(pkg_obj=pkg_info) build_id = bld_obj.bnum if pkg_info is not None and pkg_info.autosum == "True": build_env = ['_AUTOSUMS=True'] else: build_env = ['_AUTOSUMS=False'] if '/cinnamon/' in pkg_info.path: build_env.append('_ALEXPKG=True') else: build_env.append('_ALEXPKG=False') hconfig = docker_utils.create_pkgs_host_config(cache, pkgbuild_dir, result) try: container = doc.create_container("antergos/makepkg", command="/makepkg/build.sh " + pkg_deps_str, volumes=['/var/cache/pacman', '/makepkg', '/antergos', '/pkg', '/root/.gnupg', '/staging', '/32bit', '/32build', '/result'], environment=build_env, cpuset='0-3', name=pkg, host_config=hconfig) if container.get('Warnings') and container.get('Warnings') != '': logger.error(container.get('Warnings')) except Exception as err: logger.error('Create container failed. Error Msg: %s' % err) bld_obj.failed = True continue bld_obj.container = container.get('Id') status.container = bld_obj.container try: doc.start(container.get('Id')) cont = bld_obj.container stream_process = Process(target=publish_build_ouput, kwargs=dict(container=cont, bld_obj=bld_obj)) stream_process.start() result = doc.wait(cont) if result != 0: bld_obj.failed = True logger.error('[CONTAINER EXIT CODE] Container %s exited. Return code was %s' % (pkg, result)) else: logger.info('[CONTAINER EXIT CODE] Container %s exited. Return code was %s' % (pkg, result)) bld_obj.completed = True stream_process.join() except Exception as err: logger.error('Start container failed. Error Msg: %s' % err) bld_obj.failed = True bld_obj.completed = False continue repo_updated = False if bld_obj.completed: signed = sign_pkgs.sign_packages(bld_obj.pkgname) if signed: db.publish('build-output', 'Updating staging repo database..') repo_updated = update_main_repo(rev_result='staging', bld_obj=bld_obj, ) if repo_updated: tlmsg = 'Build <a href="/build/%s">%s</a> for <strong>%s</strong> was successful.' % ( build_id, build_id, pkg) Timeline(msg=tlmsg, tl_type=4) completed = status.completed completed.rpush(bld_obj.bnum) bld_obj.review_status = 'pending' else: tlmsg = 'Build <a href="/build/%s">%s</a> for <strong>%s</strong> failed.' % (build_id, build_id, pkg) Timeline(msg=tlmsg, tl_type=5) bld_obj.failed = True bld_obj.completed = False failed = status.failed failed.rpush(build_id) bld_obj.end_str = datetime.datetime.now().strftime("%m/%d/%Y %I:%M%p") if not bld_obj.failed: db.set('antbs:misc:cache_buster:flag', True) return True return False
def build_iso(): iso_arch = ['x86_64', 'i686'] in_dir_last = len([name for name in os.listdir('/srv/antergos.info/repo/iso/testing')]) if in_dir_last is None: in_dir_last = "0" db.set('pkg_count_iso', in_dir_last) is_minimal = db.get('isoMinimal') if is_minimal == 'True': iso_name = 'antergos-iso-minimal-' else: iso_name = 'antergos-iso-' for arch in iso_arch: if db.exists('iso:one:arch') and arch == 'x86_64': continue pkgobj = package.get_pkg_object(iso_name + arch) failed = False db.incr('build_number') dt = datetime.datetime.now().strftime("%m/%d/%Y %I:%M%p") build_id = db.get('build_number') pkgobj.save_to_db('builds', build_id, 'list') this_log = 'build_log:%s' % build_id db.set('%s:start' % this_log, dt) db.set('building_num', build_id) db.hset('now_building', 'build_id', build_id) db.hset('now_building', 'key', this_log) db.hset('now_building', 'pkg', pkgobj.name) db.set(this_log, True) db.set('building_start', dt) logger.info('Building %s' % pkgobj.name) db.set('building', 'Building: %s' % pkgobj.name) db.lrem('queue', 0, pkgobj.name) db.set('%s:pkg' % this_log, pkgobj.name) db.set('%s:version' % this_log, pkgobj.version) flag = '/srv/antergos.info/repo/iso/testing/.ISO32' minimal = '/srv/antergos.info/repo/iso/testing/.MINIMAL' if arch is 'i686': if not os.path.exists(flag): open(flag, 'a').close() else: if os.path.exists(flag): os.remove(flag) if is_minimal == "True": out_dir = '/out' if not os.path.exists(minimal): open(minimal, 'a').close() else: out_dir = '/out' if os.path.exists(minimal): os.remove(minimal) # Get and compile translations for updater script # TODO: Move this into its own method. trans_dir = "/opt/antergos-iso-translations/" trans_files_dir = os.path.join(trans_dir, "translations/antergos.cnchi_updaterpot") dest_dir = '/srv/antergos.info/repo/iso/testing/trans' if not os.path.exists(dest_dir): os.mkdir(dest_dir) try: subprocess.check_call(['tx', 'pull', '-a', '-r', 'antergos.cnchi_updaterpot', '--minimum-perc=50'], cwd=trans_dir) for r, d, f in os.walk(trans_files_dir): for tfile in f: logger.info('tfile is %s' % tfile) logger.info('tfile cut is %s' % tfile[:-2]) mofile = tfile[:-2] + 'mo' logger.info('mofile is %s' % mofile) subprocess.check_call(['msgfmt', '-v', tfile, '-o', mofile], cwd=trans_files_dir) os.rename(os.path.join(trans_files_dir, mofile), os.path.join(dest_dir, mofile)) except subprocess.CalledProcessError as err: logger.error(err.output) except Exception as err: logger.error(err) nm = iso_name + arch # Initiate communication with docker daemon run_docker_clean(nm) hconfig = create_host_config(privileged=True, cap_add=['ALL'], binds={ '/opt/archlinux-mkarchiso': { 'bind': '/start', 'ro': False }, '/run/dbus': { 'bind': '/var/run/dbus', 'ro': False }, '/srv/antergos.info/repo/iso/testing': { 'bind': out_dir, 'ro': False }}, restart_policy={ "MaximumRetryCount": 2, "Name": "on-failure"}) try: iso_container = doc.create_container("antergos/mkarchiso", command='/start/run.sh', tty=True, name=nm, host_config=hconfig, cpuset='0-3') db.set('container', iso_container.get('Id')) except Exception as err: logger.error("Cant connect to Docker daemon. Error msg: %s", err) failed = True break try: doc.start(iso_container, privileged=True, cap_add=['ALL'], binds={ '/opt/archlinux-mkarchiso': { 'bind': '/start', 'ro': False }, '/run/dbus': { 'bind': '/var/run/dbus', 'ro': False }, '/srv/antergos.info/repo/iso/testing': { 'bind': out_dir, 'ro': False }, }) cont = db.get('container') stream_process = Process(target=publish_build_ouput, args=(cont, this_log)) stream_process.start() result = doc.wait(cont) result2 = None if result is not 0: doc.restart(cont) stream_process2 = Process(target=publish_build_ouput, args=(cont, this_log)) stream_process2.start() result2 = doc.wait(cont) if result2 is not 0: # failed = True # db.set('build_failed', "True") logger.error('[CONTAINER EXIT CODE] Container %s exited. Return code was %s' % (nm, result)) if result is 0 or (result2 and result2 is 0): logger.info('[CONTAINER EXIT CODE] Container %s exited. Return code was %s' % (nm, result)) db.set('build_failed', "False") except Exception as err: logger.error("Cant start container. Error msg: %s", err) break db.publish('build-output', 'ENDOFLOG') db.set('%s:end' % this_log, datetime.datetime.now().strftime("%m/%d/%Y %I:%M%p")) in_dir = len([name for name in os.listdir('/srv/antergos.info/repo/iso/testing')]) last_count = int(db.get('pkg_count_iso')) if in_dir > last_count: db.incr('pkg_count_iso', (in_dir - last_count)) db.rpush('completed', build_id) db.set('%s:result' % this_log, 'completed') # db.set('%s:review_stat' % this_log, '1') else: logger.error('%s not found after container exit.' % iso_name + arch) failed = True db.set('%s:result' % this_log, 'failed') db.rpush('failed', build_id) remove('/opt/archlinux-mkarchiso/antergos-iso') doc.remove_container(cont, v=True)
def build_pkgs(last=False, pkg_info=None): if pkg_info is None: return False # Create our tmp directories result = os.path.join("/tmp", "result") cache = os.path.join("/var/tmp", "pkg_cache") for d in [result, cache]: if os.path.exists(d) and 'result' in d: shutil.rmtree(d) os.mkdir(d, 0o777) elif os.path.exists(d) and 'pkg_cache' in d: logger.info('@@-build_pkg.py-@@ 476 | Cleaning package cache....') db.set('building', 'Cleaning package cache.') for pcache in os.listdir(d): pcache = os.path.join(d, pcache) if not os.path.isdir(pcache): logger.error('@@-build_pkg.py-@@ 479 | pcache is not a directory') continue for pfile in os.listdir(pcache): pname = re.search('^([a-z]|[0-9]|-|_)+(?=-\d|r|v)', pfile) if not pname or pname == '': continue pname = pname.group(0) pfile = os.path.join(pcache, pfile) dtime = time.time() if os.stat(pfile).st_mtime < (dtime - 7 * 86400) or status.all_packages().ismember(pname): remove(pfile) else: os.mkdir(d, 0o777) dirs = ['/var/tmp/32build', '/var/tmp/32bit'] for d in dirs: if os.path.exists(d): shutil.rmtree(d) os.mkdir(d, 0o777) # pkglist = db.lrange('queue', 0, -1) pkglist1 = ['1'] in_dir_last = len([name for name in os.listdir(result)]) db.set('pkg_count', in_dir_last) for i in range(len(pkglist1)): pkg = pkg_info.name if pkg and pkg is not None and pkg != '': pkgbuild_dir = pkg_info.build_path status.current_status = 'Building %s with makepkg' % pkg bld_obj = build_obj.get_build_object(pkg_obj=pkg_info) bld_obj.failed = False bld_obj.completed = False bld_obj.version_str = pkg_info.version_str bld_obj.start_str = datetime.datetime.now().strftime("%m/%d/%Y %I:%M%p") status.building_num = bld_obj.bnum status.building_start = bld_obj.start_str build_id = bld_obj.bnum tlmsg = 'Build <a href="/build/%s">%s</a> for <strong>%s</strong> started.' % (build_id, build_id, pkg) Timeline(msg=tlmsg, tl_type=3) pbuilds = pkg_info.builds() pbuilds.append(build_id) bld_obj.pkgname = pkg pkg_deps = pkg_info.depends() or [] pkg_deps_str = ' '.join(pkg_deps) run_docker_clean(pkg) if pkg_info is not None and pkg_info.autosum == "True": build_env = ['_AUTOSUMS=True'] else: build_env = ['_AUTOSUMS=False'] if '/cinnamon/' in pkg_info.path: build_env.append('_ALEXPKG=True') else: build_env.append('_ALEXPKG=False') hconfig = docker_utils.create_pkgs_host_config(cache, pkgbuild_dir, result) try: container = doc.create_container("antergos/makepkg", command="/makepkg/build.sh " + pkg_deps_str, volumes=['/var/cache/pacman', '/makepkg', '/repo', '/pkg', '/root/.gnupg', '/staging', '/32bit', '/32build', '/result'], environment=build_env, cpuset='0-3', name=pkg, host_config=hconfig) if container.get('Warnings') and container.get('Warnings') != '': logger.error(container.get('Warnings')) except Exception as err: logger.error('Create container failed. Error Msg: %s' % err) bld_obj.failed = True bld_obj.completed = False continue bld_obj.container = container.get('Id') try: doc.start(container.get('Id')) cont = bld_obj.container stream_process = Process(target=publish_build_ouput, args=(cont, bld_obj)) stream_process.start() result = doc.wait(cont) if result is not 0: bld_obj.failed = True bld_obj.completed = False logger.error('[CONTAINER EXIT CODE] Container %s exited. Return code was %s' % (pkg, result)) else: logger.info('[CONTAINER EXIT CODE] Container %s exited. Return code was %s' % (pkg, result)) bld_obj.failed = False bld_obj.completed = True except Exception as err: logger.error('Start container failed. Error Msg: %s' % err) bld_obj.failed = True bld_obj.completed = False continue # db.publish('build-ouput', 'ENDOFLOG') # stream = doc.logs(container, stdout=True, stderr=True, timestamps=True) # log_stream = stream.split('\n') # db_filter_and_add(log_stream, this_log) # in_dir = len([name for name in os.listdir(result)]) # last_count = int(db.get('pkg_count')) # logger.info('last count is %s %s' % (last_count, type(last_count))) # logger.info('in_dir is %s %s' % (in_dir, type(in_dir))) pkgs2sign = None if not bld_obj.failed: db.publish('build-output', 'Signing package..') pkgs2sign = glob.glob( '/srv/antergos.info/repo/iso/testing/uefi/antergos-staging/x86_64/%s-***.xz' % pkg) pkgs2sign32 = glob.glob( '/srv/antergos.info/repo/iso/testing/uefi/antergos-staging/i686/%s-***.xz' % pkg) pkgs2sign = pkgs2sign + pkgs2sign32 logger.info('[PKGS TO SIGN] %s' % pkgs2sign) if pkgs2sign is not None and pkgs2sign != []: try_sign = sign_pkgs.batch_sign(pkgs2sign) else: try_sign = False if try_sign: db.publish('build-output', 'Signature created successfully for %s' % pkg) logger.info('[SIGN PKG] Signature created successfully for %s' % pkg) db.publish('build-output', 'Updating staging repo database..') update_main_repo(pkg, 'staging', bld_obj) else: bld_obj.failed = True bld_obj.completed = False if not bld_obj.failed: db.publish('build-output', 'Build completed successfully!') tlmsg = 'Build <a href="/build/%s">%s</a> for <strong>%s</strong> completed.' % ( build_id, build_id, pkg) Timeline(msg=tlmsg, tl_type=4) # db.incr('pkg_count', (in_dir - last_count)) completed = status.completed() completed.rpush(build_id) bld_obj.review_stat = 'pending' else: tlmsg = 'Build <a href="/build/%s">%s</a> for <strong>%s</strong> failed.' % (build_id, build_id, pkg) Timeline(msg=tlmsg, tl_type=5) if pkgs2sign is not None: for p in pkgs2sign: remove(p) remove(p + '.sig') failed = status.failed() failed.rpush(build_id) bld_obj.end_str = datetime.datetime.now().strftime("%m/%d/%Y %I:%M%p") status.container = '' status.building_num = '' status.building_start = '' if not bld_obj.failed: db.set('antbs:misc:cache_buster:flag', True) return True return False