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 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 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 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 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 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 print_execution_error_summary(self): if not os.path.exists("errors.txt"): exit(0) with open('errors.txt', 'r') as error_file: all_errors = error_file.read() if all_errors: ColorPrint.blue_highlight("Fix the following errors and run again") ColorPrint.err('\n' + all_errors) else: ColorPrint.blue_highlight("Execution complited without errors")
def _git_pull(self, repo): ColorPrint.blue_highlight("Pulling the repository [{0}]".format(repo)) repo_path = self.path_to_workspace + os.sep + repo is_git_pull_ran = False if os.path.exists(repo_path): current_branch = self.get_branch_name(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: p_status, output, error = self.run_command_and_collect_errors( 'cd {0};git pull origin {1}'.format( repo_path, current_branch)) is_git_pull_ran = True 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: p_status, output, error = self.run_command_and_collect_errors( 'cd {0};git pull origin {1}'.format( repo_path, current_branch)) is_git_pull_ran = True self.run_git_unstash(repo_path) else: ColorPrint.warn( "The repository path [{0}] is not available".format(repo)) if is_git_pull_ran and p_status == 0: if 'up to date' in output or 'Successfully rebased and updated' or 'Fast-forward' in output: ColorPrint.blue_highlight( "Pull for repository {0} finished successfully".format( repo)) else: current_error = "Your repository {0} is broken, try to run 'git gc --prune=now' and 'git remote prune origin' to fix it".format( repo) ColorPrint.err(current_error) filename = 'errors.txt' if os.path.exists(filename): append_write = 'a' # append if already exists else: append_write = 'w' # make a new file if not error_file = open(filename, append_write) error_file.write(current_error + '\n') error_file.close()
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 mvn_clean(self): if not os.path.exists(self.path_to_workspace): ColorPrint.err("Invalid release name: [{0}]".format(self.release)) exit(1) project_per_repo = self.config.getsection('projects') for repo_name, projects in project_per_repo: ColorPrint.blue_highlight( "Starting mvn clean 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'.format(project_path) self.run_command_and_collect_errors(cmd) log_message = "Maven clean operation for release completed".format( self.release) ColorPrint.blue_highlight(log_message) self.notif_mgr.send_notification(True, 'Maven Clean', log_message)
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 switch_track(self, track_name): 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._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) log_message = "Switch operation for release [{0}] took [{1}] seconds".format( self.release, difference) ColorPrint.blue_highlight(log_message) self.notif_mgr.send_notification(True, "Switch branch", log_message)
def send_notification(self, status, subject, message): ColorPrint.info("Send message {0}".format(message)) list_of_providers = self.provider.split(",") if len(list_of_providers) == 0 or not self.notify: ColorPrint.err("Notification providers list is empty or notification disabled.") else: for provider in list_of_providers: if status is True: subject += " Successful" else: subject += ' Failed' if provider == "telegram": ColorPrint.info("Send telegram message") telegram = TelegramSender(None) telegram.send_message(self.chat_id, subject + '\n\n' + message) if provider == "email": ColorPrint.info("Send email message") email_sender = EmailSender(None, None, None, None) email_sender.send_message(self.recipient, subject, message, status)
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 create_mid_config(self, port='0'): current_port = int(port) if not (current_port > 0 and current_port < 65536): current_port = self.instance_port path_to_work_config = self.path_to_workspace + os.sep + 'mid/mid/work/config.xml' path_to_orig_config = self.path_to_workspace + os.sep + 'mid/mid/config.xml' path_to_key_store = self.path_to_workspace + os.sep + 'mid/mid/keystore/agent_keystore.jks' if not os.path.exists(path_to_work_config): ColorPrint.blue_highlight( "Configuring the local mid server with instance port [{0}]". format(current_port)) tree = ET.parse(path_to_orig_config) root = tree.getroot() for parameter in root.findall('parameter'): parameter_name = parameter.get('name') if parameter_name == 'url': parameter.set( 'value', '{0}:{1}/'.format(self.instance_host, current_port)) if parameter_name == 'mid.instance.username': parameter.set('value', self.instance_user) if parameter_name == 'mid.instance.password': parameter.set('value', self.instance_password) if parameter_name == 'name': parameter.set('value', 'eclipse01') tree.write(path_to_work_config) if os.path.exists(path_to_key_store): ColorPrint.info( "Found keystore file, deleting it to prevent crash on mid start [{0}]" .format(path_to_key_store)) os.remove(path_to_key_store) ColorPrint.blue_highlight("Mid server is ready to start") else: ColorPrint.err( "Configuration file for mid server already exist in [{0}] directory" .format(path_to_work_config))
def handle_command(cmd, check_rc=True, get_output=True, print_output=False, print_cmd=False, background=False): """ Executes command :param cmd: command string to be executed :return: rc, stdout, stderr """ stdout_flag = None if get_output: stdout_flag = subprocess.PIPE if print_cmd: ColorPrint.info("[handle_command] running {0}".format(cmd)) 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) + ' ' + str(out)) else: if print_output: ColorPrint.info( "[handle_command] Command output: {0} ".format( str(out))) abort_on_error = SncConfig().getboolean('envbuilder', 'abort_on_error') if abort_on_error and p_status != 0: ColorPrint.err( "EnvironmentBuilder: Execution aborted due to error[s]") exit(1) return p_status, out, err
plugin = plugins[flag] if plugin['active'] is True: parser.add_argument('-{0}'.format(plugin['flag']), help='{0}'.format(plugin['description']), action="store_true") args = parser.parse_args() # print str(args) if not args.release: ColorPrint.info( "The -r [release] option is not provided, using default [{0}] from envbuilder.conf" .format(default_release)) if not default_release: ColorPrint.err( '\n' + "The [release] parameter is not defined under [enbuilder] section in enbuilder.conf" ) args.release = default_release if args.status and args.release: EnvironmentBuilder.print_list_avalable_versions(args.release) exit(0) if args.status: EnvironmentBuilder.print_list_avalable_versions(None) exit(0) if args.mvn and args.release: builder = EnvironmentBuilder(args.release) builder.mvn_build() builder.print_execution_error_summary()