def remove_client(self, client, reason=None): """ This method removes the client from the server in a higher level, it creates the appropriate messages, logs the time stamp and such. In future mysql will also log sesion time here. :param client: the client object that is being removed from the server :return: nothing, literally nothing """ total_session_time = datetime.datetime.now() - client.connection_time ColorPrint.print_message("Warning", "remove_client", "kicking the client " + str(client.username) + " Reason " + str(reason)) self.logger.warning("Kicking client " + str(client.username) + " Total Session Time " + str(total_session_time) + " Reason " + str(reason)) # cleaning up the sockets and such client_conn = client.socket_connection try: self.all_clients.pop(client.username) self.all_connections.remove(client_conn) self.comm_layer.close_connection(client_conn) except Exception as e: # Probably client is already removed self.logger.error("[remove client] remove_client error " + str(e)) self.logger.error("[remove client] " + str(traceback.format_exc())) client_disconnected_message = Message("server", "server", "event", "Client Disconnected " + str(client) + " Reason " + str(reason)) self.inbox_queue.put(client_disconnected_message)
def message_routing(self): """ This method handles all the queues, it routes the incoming massages to appropriate queue's according to the message type :return: """ while self.status: if self.inbox_queue.not_empty: new_block = self.inbox_queue.get() # print("DEBUG " + str(new_block)) self.logger.debug("[message_routing] Routing message " + str(new_block)) if new_block.type == MessageType.event: self.UI_queue.put(new_block) elif new_block.type == MessageType.utility: # self.handle_utility(new_block) self.message_handler.handle_message(new_block) elif new_block.type == MessageType.communication: self.handle_comms(new_block) else: ColorPrint.print_message("Warning", str(new_block.sender), str(new_block.payload)) self.logger.warning( "[Message Router] invalid message type " + str(new_block.sender) + " " + str(new_block.payload)) print("Message routing shutting down")
def handle_command(self, cmd, check_rc=True, get_output=False): """ Executes command :param cmd: command string to be executed :return: rc, stdout, stderr """ ColorPrint.info("Running command: {0}, Please Wait".format(cmd)) stdout_flag = None if get_output: stdout_flag = subprocess.PIPE p = subprocess.Popen(cmd, stdout=stdout_flag, stderr=subprocess.STDOUT, shell=True) (out, err) = p.communicate() p_status = p.wait() if check_rc: if p_status != 0: ColorPrint.err( "[handle_command] failed executing: {0}".format(cmd)) ColorPrint.err(str(err)) else: ColorPrint.info( "[handle_command] succeeded executing: {0}".format(cmd)) if self.abort_on_error and p_status != 0: ColorPrint.err( "EnvironmentBuilder: Execution aborted due to error[s]") exit(1) return p_status, out, err
def getsection(self, section_name): try: return self.config.items(section_name) except: ColorPrint.err( "Config file {0} or section not found".format(ENVBUILDER_CONF)) exit(1)
def getboolean(self, section, param_name): try: return self.config.getboolean(section, param_name) except: ColorPrint.err( "Config file {0} or section not found".format(ENVBUILDER_CONF)) exit(1)
def print_release_branch_per_repository(current_release): """git remote prune origin""" base_dir = SncConfig().getstring('git_repo', 'base_dir') list_of_repos = SncConfig().getlist('git_repo', 'repo') list_of_messages = {} brunches_d = {} for repository in list_of_repos: path_to_repository = base_dir + os.sep + current_release + os.sep + repository if os.path.exists(path_to_repository + os.sep + '.git'): cmd_get_branch = 'cd {0};git rev-parse --abbrev-ref HEAD'.format( path_to_repository) status, current_brunch, error = EnvironmentBuilder.handle_command( cmd_get_branch, True, True, False) current_brunch = current_brunch.rstrip() current_message = "Release: [{0}] Repository: [{1}], Branch: [{2}]".rstrip( ).format(current_release, repository, current_brunch) list_of_messages[current_message] = current_brunch if current_brunch in brunches_d: brunches_d[current_brunch] += 1 else: brunches_d[current_brunch] = 0 if brunches_d.values(): max_brunch = max(brunches_d.values()) for message, branch in list_of_messages.iteritems(): if brunches_d[branch] < max_brunch: ColorPrint.err(message) else: ColorPrint.info(message)
def run_git_unstash(self, repo_path): if os.path.exists(repo_path): ColorPrint.blue_highlight( "Unstashing the repository [{0}]".format(repo_path)) self.handle_command('cd {0};git stash pop'.format(repo_path)) else: ColorPrint.warn( "The repository path [{0}] is not available".format(repo_path))
def print_list_avalable_versions(): base_dir = SncConfig().getstring('git_repo', 'base_dir') ColorPrint.blue_highlight("***** Avalable versions ****:") for dir in os.listdir(base_dir): if os.path.isdir(base_dir + os.sep + dir) and not dir.startswith('.'): if EnvironmentBuilder.is_release_direcrory(dir): ColorPrint.info('[' + dir + ']')
def getlist(self, section, param_name): try: cfg_list = self.config.get(section, param_name) return cfg_list.split(",") except: ColorPrint.err( "Config file {0} or section not found".format(ENVBUILDER_CONF)) exit(1)
def run_git_stash(self, repo_path): if os.path.exists(repo_path): ColorPrint.blue_highlight( "Stashing the repository [{0}]".format(repo_path)) self.run_command_and_collect_errors( 'cd {0};git stash'.format(repo_path)) else: ColorPrint.warn( "The repository path [{0}] is not available".format(repo_path))
def _git_custom(self, git_command, repo): ColorPrint.blue_highlight( "Running custom git command on repository [{0}]".format(repo)) repo_path = self.path_to_workspace + os.sep + repo if os.path.exists(repo_path): self.run_command_and_collect_errors('cd {0};git {1}'.format( repo_path, git_command)) else: ColorPrint.warn( "The repository path [{0}] is not available".format(repo))
def load_plugins(self): for file_path in glob.iglob(r'{0}/*.json'.format(self.plugins_dir)): with open(file_path) as json_file: plugin = json.load(json_file) if plugin['active']: ColorPrint.info("Loading plugin {0}".format( plugin['name'])) self.plugins[plugin['flag']] = plugin return self.plugins
def copy_local_env(self, new_release_name): path_to_new_release = self.base_dir + os.sep + new_release_name copy_cmdb = 'cp -rp ' + self.path_to_workspace + ' ' + path_to_new_release ColorPrint.blue_highlight("Copying environment [{0}] to [{1}] ".format( self.release, new_release_name)) if os.path.exists(self.path_to_workspace): self.run_command_and_collect_errors(copy_cmdb) else: ColorPrint.err("Can't copy due to invalid path: [{0}] ".format( self.path_to_workspace))
def mvn_build(self): project_per_repo = self.config.getsection('projects') for repo_name, projects in project_per_repo: ColorPrint.blue_highlight( "Starting mvn install for repository {0}".format(repo_name)) for project_name in projects.split(','): project_path = self.path_to_workspace + os.sep + repo_name + os.sep + project_name java_env = 'source ~/.bash_profile' cmd = java_env + ';cd {0};mvn clean install -DskipTests'.format( project_path) self.handle_command(cmd)
def import_projects(self): project_per_repo = self.config.getsection('projects') for repo_name, projects in project_per_repo: ColorPrint.blue_highlight( "Importing {0} repository projects".format(repo_name)) for project_name in projects.split(','): project_path = self.path_to_workspace + os.sep + repo_name + os.sep + project_name if os.path.exists(project_path): java_env = 'source ~/.bash_profile' cmd = java_env + ';{2} -nosplash -data "{0}" -application org.eclipse.cdt.managedbuilder.core.headlessbuild -import {1}'.format( self.path_to_workspace, project_path, self.eclipse) self.handle_command(cmd)
def _is_branch_up_to_date(self, repo_path): ColorPrint.blue_highlight( "Checking repository status [{0}]".format(repo_path)) self.handle_command('cd {0};git remote update'.format(repo_path)) if self._is_ready_to_pull(repo_path): return True else: repo = os.path.basename(os.path.normpath(repo_path)) if repo in self.repo_status and self.repo_status[repo]: return True else: return False
def __init__(self, notification_provider, email_recipient, telegram_chat_id): ColorPrint.info("Init: NotificationManager") self.config = SncConfig() if notification_provider and email_recipient and telegram_chat_id: self.provider = notification_provider self.recipient = email_recipient self.chat_id = telegram_chat_id self.notify = True else: self.provider = self.config.getstring('notification', 'notification_provider') self.recipient = self.config.getstring('notification', 'notification_email_recipient') self.chat_id = self.config.getstring('notification', 'notification_telegram_chat_id') self.notify = self.config.getboolean('notification', 'notify')
def run_git_pull(self): now = time.time() list_of_repos = self.config.getlist('git_repo', 'repo') if (self.parallel_run): pool = Pool(len(list_of_repos)) pool.map(self._git_pull, list_of_repos) else: for repo in list_of_repos: self._git_pull(repo) later = time.time() difference = int(later - now) ColorPrint.blue_highlight( "Pull operation for release [{0}] took [{1}] seconds".format( self.release, difference))
def show_my_commits(self, show_sha, since_days): list_of_repos = self.config.getlist('git_repo', 'repo') for repo in list_of_repos: current_repo_path = self.path_to_workspace + os.sep + repo if os.path.exists(current_repo_path): if since_days is None: commit_since_days = self.config.getint( 'git_repo', 'commit_since_days') else: commit_since_days = int(since_days) since_date = datetime.now() - timedelta(days=commit_since_days) show_commit = '' if not show_sha: show_commit = '\|commit ' cmd_commits = 'cd ' + current_repo_path + ';git log --author="$(git config user.name)" --since "{0} {1} {2}"|grep -v "Author:\|Date:{3}"'.\ format(since_date.strftime("%B"), since_date.day, since_date.year, show_commit) commits_output = EnvironmentBuilder.handle_command( cmd_commits, False, True, self.print_cmd_output, self.print_cmd) p_status, output, err = commits_output if p_status == 0 and not (output.rstrip('\n').isspace()): output = os.linesep.join( ['\t' + s.strip() for s in output.splitlines() if s]) ColorPrint.blue_highlight( "Commits for repository [{0}]".format(repo.upper())) ColorPrint.info(output) unpushed_commits = self.get_unpushed_commits(current_repo_path) if unpushed_commits and not unpushed_commits.rstrip( '\n').isspace(): ColorPrint.err("\tUnpushed commits!!!") ColorPrint.warn(unpushed_commits)
def _git_pull(self, repo): ColorPrint.blue_highlight("Pulling the repository [{0}]".format(repo)) repo_path = self.path_to_workspace + os.sep + repo if os.path.exists(repo_path): if self._is_branch_up_to_date(repo_path): if repo in self.repo_status and self.repo_status[repo]: ColorPrint.blue_highlight( 'Your repository [{0}] is up-to-date, skipping [git pull]' .format(repo)) else: self.handle_command('cd {0};git pull'.format(repo_path)) else: self.run_git_stash(repo_path) if self._is_ready_to_pull(repo_path): if repo in self.repo_status and self.repo_status[repo]: ColorPrint.blue_highlight( 'Your repository [{0}] is up-to-date, skipping [git pull]' .format(repo)) else: self.handle_command( 'cd {0};git pull'.format(repo_path)) self.run_git_unstash(repo_path) else: ColorPrint.warn( "The repository path [{0}] is not available".format(repo))
def ui(self): while self.client_controller.status: try: print("Please input command [info, tools]") user_input = input() if user_input == "info": self.ui_info() elif user_input == "tools": self.ui_tools() except EOFError as e: ColorPrint.print_message("Error", "UI", "Exception occurred " + str(e)) print("UI Terminating")
def switch_track(self, track_name): now = time.time() list_of_repos = self.config.getlist('git_repo', 'repo') if (self.parallel_run): pool = Pool(len(list_of_repos)) pool.map(self._switch_repo, zip(list_of_repos, repeat(track_name))) else: for repo in list_of_repos: self._switch_repo([repo, track_name]) later = time.time() difference = int(later - now) ColorPrint.blue_highlight( "Switch operation for release [{0}] took [{1}] seconds".format( self.release, difference))
def __init__(self): self.config = RawConfigParser(allow_no_value=False) try: if ENVBUILDER_CONF in os.environ: self.config_file_path = os.environ[ENVBUILDER_CONF] if len(str(self.config_file_path).strip()) > 0 and ( ENVBUILDER_CONF in self.config_file_path): self.config.read(self.config_file_path) else: self.config.read(ENVBUILDER_CONF) else: self.config.read(ENVBUILDER_CONF) except: ColorPrint.err("Config file {0} not found".format(ENVBUILDER_CONF)) exit(1)
def _switch_repo(self, args): repo, track_name = args ColorPrint.blue_highlight( "Trying to switch the repository to [{0}]".format(track_name)) if os.path.exists(self.path_to_workspace + os.sep + repo): p_status, out, err = self.handle_command( 'cd {0};git rev-parse --abbrev-ref HEAD'.format( self.path_to_workspace + os.sep + repo)) if out == track_name: ColorPrint.warn( "The current repository already switched to [{0}], skipping" .format(track_name)) else: self.handle_command( 'cd {0};git fetch && git checkout {1}'.format( self.path_to_workspace + os.sep + repo, track_name))
def _is_ready_to_pull(self, repo_path): ColorPrint.blue_highlight( "Checking repository status [{0}]".format(repo_path)) p_status, cmd_out, err = self.handle_command( 'cd {0};git status -uno'.format(repo_path), True, True) ColorPrint.info(cmd_out) repo = os.path.basename(os.path.normpath(repo_path)) if 'Your branch is up-to-date' in str(cmd_out): self.repo_status[repo] = True else: self.repo_status[repo] = False if 'nothing to commit' in str(cmd_out): return True else: return False
def print_list_avalable_versions(current_release): base_dir = SncConfig().getstring('git_repo', 'base_dir') if current_release is not None: ColorPrint.blue_highlight('================' + current_release.upper() + '================') EnvironmentBuilder.print_release_branch_per_repository( current_release) exit(0) for dir in os.listdir(base_dir): if os.path.isdir(base_dir + os.sep + dir) and not dir.startswith('.'): if EnvironmentBuilder.is_release_direcrory(dir): ColorPrint.blue_highlight('================' + dir.upper() + '================') EnvironmentBuilder.print_release_branch_per_repository(dir)
def clone_env(self): now = time.time() if not os.path.exists(self.path_to_workspace): os.makedirs(self.path_to_workspace) list_of_repos = self.config.getlist('git_repo', 'repo') if self.parallel_run: pool = Pool(len(list_of_repos)) pool.map(self._clone_env, list_of_repos) else: for repo in list_of_repos: self._clone_env(repo) later = time.time() difference = int(later - now) log_message = "Clone operation for release [{0}] took [{1}] seconds".format( self.release, difference) ColorPrint.blue_highlight(log_message) self.notif_mgr.send_notification(True, 'clone_env', log_message)
def clone_env(self, user, password): now = time.time() if not os.path.exists(self.path_to_workspace): os.makedirs(self.path_to_workspace) list_of_repos = self.config.getlist('git_repo', 'repo') if (self.parallel_run): pool = Pool(len(list_of_repos)) pool.map(self._clone_env, zip(list_of_repos, repeat(user), repeat(password))) else: for repo in list_of_repos: self._clone_env([repo, user, password]) later = time.time() difference = int(later - now) ColorPrint.blue_highlight( "Clone operation for release [{0}] took [{1}] seconds".format( self.release, difference))
def run_git_pull(self): if not os.path.exists(self.path_to_workspace): ColorPrint.err("Invalid release name: [{0}]".format(self.release)) exit(1) now = time.time() list_of_repos = self.config.getlist('git_repo', 'repo') if self.parallel_run: pool = Pool(len(list_of_repos)) pool.map(self._git_pull, list_of_repos) else: for repo in list_of_repos: self._git_pull(repo) later = time.time() difference = int(later - now) log_message = "Pull operation for release [{0}] took [{1}] seconds".format( self.release, difference) ColorPrint.blue_highlight(log_message) self.notif_mgr.send_notification(True, 'git pull', log_message)
def ui_read_messages(self): print("Listing messages") print(self.server_controller.UI_queue.not_empty) # TODO fix blocking here while not self.server_controller.UI_queue.empty(): # UI_queue.get(block=True) #Blocks till a message appears! new_block = self.server_controller.UI_queue.get() # username, type_of_event, message = new_block if new_block.type is "event": ColorPrint.print_message( "Event", str(new_block.sender), new_block.payload + " -Message Created " + str(new_block.date)) elif new_block.type is "message": ColorPrint.print_message("Message", str(new_block.sender), new_block.payload)