def notifies_hipchat(start_msg, end_msg): """ A decorator to post a notification to hipchat at the start and end of this function. The `FOO_msg` arguments define template strings that can use the following variables as context: * `deployer` The deploying user * `deployment_name` The deploying environment. Eg. "beta" * `generation` The generational target. Eg. "live", "pending" * `git_branch` The current git branch name. * `duration` The number of wall-clock seconds taken to complete the decorated method. Note: Only available to `end_msg`. """ # Ensure we have the required configs hipchat_conf = {} for key in ['api_token', 'room']: hipchat_conf[key] = env.get('hipchat_%s' % key, None) for key, value in hipchat_conf.items(): if value is None: logger.warning( "No hipchat_%s found. Not notifying.", key, ) yield logger.warning( "No hipchat_%s found. Not notifying.", key, ) return hipchat_conf['color'] = env.get('hipchat_color', 'green') hipchat_conf['from'] = env.get('hipchat_from', 'Neckbeard') # Get our git branchname. Fallback to a SHA if detached head r = git.Repo('.') branch_name = r.commit().hexsha[:7] if not r.head.is_detached: branch_name = r.active_branch.name # Build the message context = { 'deployer': get_deployer(), 'deployment_name': env.get('_deployment_name', 'unknown'), 'generation': _get_gen_target().lower(), 'git_branch': branch_name, } message = start_msg % context _send_hipchat_msg(message, hipchat_conf) method_start = datetime.now() yield duration = datetime.now() - method_start context['duration'] = duration.seconds message = end_msg % context _send_hipchat_msg(message, hipchat_conf)
def _send_deployment_end_newrelic(): ''' API: https://rpm.newrelic.com/accounts/87516/applications/402046/deployments/instructions # noqa ''' GIT_MOST_RECENT_TWO_TAGS = 'git tag | tail -2' GIT_MOST_RECENT_COMMIT_MESSAGE = 'git log -1 --format="%s"' GIT_CURRENT_TAG = 'git describe --tags' GITHUB_COMPARE_OPERATOR = '...' NEWRELIC_API_HTTP_METHOD = 'POST' def generate_github_changelog_url(tags): return GITHUB_COMPARE_URL % GITHUB_COMPARE_OPERATOR.join(tags) if env.get('newrelic_api_token', False): logger.info('Announcing deployment to newrelic') headers = { 'x-api-key': env.newrelic_api_token, } # Description is the generation target, e.g. active, pending description = '%s ' % (_get_gen_target().lower(), ) params = { 'deployment[application_id]': env.newrelic_application_id, 'deployment[description]': description, } # Add user information to deployment user = get_deployer() if user: params['deployment[user]'] = user # Set the changelog to a github comparison URL result = local(GIT_MOST_RECENT_TWO_TAGS, capture=True) if result.return_code == 0: tags = result.split() url = generate_github_changelog_url(tags) params['deployment[changelog]'] = url # Append the most recent commit message to the description result = local(GIT_MOST_RECENT_COMMIT_MESSAGE, capture=True) if result.return_code == 0: params['deployment[description]'] += result.strip() # Set the revision to the current tag result = local(GIT_CURRENT_TAG, capture=True) if result.return_code == 0: params['deployment[revision]'] = result.strip() # Attempt to post the deployment to newrelic conn = httplib.HTTPSConnection(NEWRELIC_API_HTTP_HOST) conn.request( NEWRELIC_API_HTTP_METHOD, NEWRELIC_API_HTTP_URL, urllib.urlencode(params), headers, ) response = conn.getresponse() if response.status != 201: logger.warn('Failed to post deployment to newrelic')
def notifies_hipchat(start_msg, end_msg): """ A decorator to post a notification to hipchat at the start and end of this function. The `FOO_msg` arguments define template strings that can use the following variables as context: * `deployer` The deploying user * `deployment_name` The deploying environment. Eg. "beta" * `generation` The generational target. Eg. "live", "pending" * `git_branch` The current git branch name. * `duration` The number of wall-clock seconds taken to complete the decorated method. Note: Only available to `end_msg`. """ # Ensure we have the required configs hipchat_conf = {} for key in ["api_token", "room"]: hipchat_conf[key] = env.get("hipchat_%s" % key, None) for key, value in hipchat_conf.items(): if value is None: logger.warning("No hipchat_%s found. Not notifying.", key) yield logger.warning("No hipchat_%s found. Not notifying.", key) return hipchat_conf["color"] = env.get("hipchat_color", "green") hipchat_conf["from"] = env.get("hipchat_from", "Neckbeard") # Get our git branchname. Fallback to a SHA if detached head r = git.Repo(".") branch_name = r.commit().hexsha[:7] if not r.head.is_detached: branch_name = r.active_branch.name # Build the message context = { "deployer": get_deployer(), "deployment_name": env.get("_deployment_name", "unknown"), "generation": _get_gen_target().lower(), "git_branch": branch_name, } message = start_msg % context _send_hipchat_msg(message, hipchat_conf) method_start = datetime.now() yield duration = datetime.now() - method_start context["duration"] = duration.seconds message = end_msg % context _send_hipchat_msg(message, hipchat_conf)
def _send_deployment_end_newrelic(): """ API: https://rpm.newrelic.com/accounts/87516/applications/402046/deployments/instructions # noqa """ GIT_MOST_RECENT_TWO_TAGS = "git tag | tail -2" GIT_MOST_RECENT_COMMIT_MESSAGE = 'git log -1 --format="%s"' GIT_CURRENT_TAG = "git describe --tags" GITHUB_COMPARE_OPERATOR = "..." NEWRELIC_API_HTTP_METHOD = "POST" def generate_github_changelog_url(tags): return GITHUB_COMPARE_URL % GITHUB_COMPARE_OPERATOR.join(tags) if env.get("newrelic_api_token", False): logger.info("Announcing deployment to newrelic") headers = {"x-api-key": env.newrelic_api_token} # Description is the generation target, e.g. active, pending description = "%s " % (_get_gen_target().lower(),) params = {"deployment[application_id]": env.newrelic_application_id, "deployment[description]": description} # Add user information to deployment user = get_deployer() if user: params["deployment[user]"] = user # Set the changelog to a github comparison URL result = local(GIT_MOST_RECENT_TWO_TAGS, capture=True) if result.return_code == 0: tags = result.split() url = generate_github_changelog_url(tags) params["deployment[changelog]"] = url # Append the most recent commit message to the description result = local(GIT_MOST_RECENT_COMMIT_MESSAGE, capture=True) if result.return_code == 0: params["deployment[description]"] += result.strip() # Set the revision to the current tag result = local(GIT_CURRENT_TAG, capture=True) if result.return_code == 0: params["deployment[revision]"] = result.strip() # Attempt to post the deployment to newrelic conn = httplib.HTTPSConnection(NEWRELIC_API_HTTP_HOST) conn.request(NEWRELIC_API_HTTP_METHOD, NEWRELIC_API_HTTP_URL, urllib.urlencode(params), headers) response = conn.getresponse() if response.status != 201: logger.warn("Failed to post deployment to newrelic")