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 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 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 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 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 __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 _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 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
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 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 __init__(self, plugins_dir): self.plugins_dir = plugins_dir ColorPrint.info("Init: PluginsLoader") self.plugins = dict()
pl = PluginsLoader(os.environ[ENVB_PATH] + os.sep + "plugins") plugins = pl.load_plugins() for flag in plugins: 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)