def send_upstream(yaml_file, event): """ Creates a gerrit.commentAdded event. Lets the commentAdded object decide whether or not sending a proposed change to upstream is necessary. @param yaml_file - String yaml file name @param event - String event that should contain json """ try: _config = config.load_config(yaml_file) start = time.time() logger.info("send upstream starting...") downstream = gerrit.Remote(_config['gerrit']) upstream = gerrit.Remote(_config['upstream']) event_obj = gerrit.CommentAdded(event, _config) event_obj.send_upstream(downstream, upstream) duration = time.time() - start msg = "send upstream run finished in %s seconds." % duration logger.info(msg) print msg except Exception as e: logging.exception("Error occurred:") raise e
def service(yaml_file): """ Initializes a downstream event listener, an upstream event listener, and a threadpool to handle events from both. Also sets up a schdule if things need to be delayed. Runs in infinite loop until killed. Each loop iteration consists of checking the schedule, checking downstream, then checking upstream. Sleep if no action taken. @param yaml_file - String location to configuration """ # Get configuraion _config = config.load_config(yaml_file) numthreads = int(_config['daemon']['numthreads']) sleep = int(_config['daemon']['sleep']) # Register the signal handler to kill threads signal.signal(signal.SIGINT, thread.stop_threads) signal.signal(signal.SIGTERM, thread.stop_threads) schedule = list() pool = thread.WorkerPool(numthreads) downstream_remote = gerrit.Remote(_config['gerrit']) downstream = downstream_remote.SSHStream() downstream.start() upstream_remote = gerrit.Remote(_config['upstream']) upstream = upstream_remote.SSHStream() upstream.start() while True: downstream_active = False upstream_active = False # Check schedule and add events to event pool if len(schedule) > 0 and time.time() > schedule[0][0]: t, func, args, kwargs = schedule.pop(0) pool.add_task(func, *args, **kwargs) continue # Check for new events downstream_active = pull_downstream(_config, downstream, pool, schedule, yaml_file) upstream_active = pull_upstream(_config, upstream, pool, schedule, yaml_file) if downstream_active: logger.debug("Downstream is active") if upstream_active: logger.debug("Upstream is active") logger.debug("Schedule len: %s" % len(schedule)) # Sleep if no events recieved. if not downstream_active and not upstream_active: time.sleep(sleep) continue
def sync_projects(_config, specific=None): """ Syncs projects described in _config. Projects that are to be synced have a source repo. Syncing is the process of pushing those changes to downstream. Optionally, a specific project can be named and only that project will be synced. @param _config - Dictionary @param specific - String name of a specific project. """ remote = gerrit.Remote(_config['gerrit']) # Convert project dictionaries to Project objects projects = [gerrit.Project(p) for p in _config.get('projects', [])] # If a specific project is provided, filter out other projects if specific: projects = [p for p in projects if p.name == specific] if not projects: msg = "Project %s: Not in configuration" % specific logger.error(msg) print msg for p in projects: try: p.ensure(remote, _config) print "" except: logger.exception("Unable to sync project") traceback.print_exc()
def sync_users(_config): """ Ensures users desribed by _config are present. Will create them if they DO NOT exist but will leave them alone if they DO exist. @param _config - Dictionary """ remote = gerrit.Remote(_config['gerrit']) for user_data in _config.get('users', []): try: user = gerrit.User(user_data) user.present(remote) print "" except: logger.exception("Unable to sync user") traceback.print_exc()
def sync_groups(_config): """ Ensures groups listed described by _config are present. Will create them if they DO NOT exist but will leave them alone if they DO exist. @param _config - Dictionary """ remote = gerrit.Remote(_config['gerrit']) for group_data in _config.get('groups', []): try: group = gerrit.Group(group_data) group.present(remote) print "" except: logger.exception("Unable to sync group") traceback.print_exc()