Created
May 29, 2016 13:40
-
-
Save smbambling/e37e21b7468738d14e53083b3a39bfb1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Requirements: | |
# - request module ( sudo yum install python-requests ) | |
# - slacker ( sudo pip install slacker) | |
# * https://github.com/os/slacker | |
# Suppress Deprecation Warning....because...python 2.6 is old | |
import warnings | |
def fxn(): | |
warnings.warn("deprecated", DeprecationWarning) | |
with warnings.catch_warnings(): | |
warnings.simplefilter("ignore") | |
fxn() | |
import argparse | |
import logging | |
import json | |
import sys | |
import requests | |
from slacker import Slacker | |
import re | |
from requests.packages.urllib3.exceptions import InsecureRequestWarning | |
requests.packages.urllib3.disable_warnings(InsecureRequestWarning) | |
def slack(environment, error): | |
room = '<YOUR SLACK #CHANNEL' | |
username = 'Code-Manager Deployer' | |
icon_url = 'https://cloud.githubusercontent.com/assets/91011/6860448/96c424a0-d46b-11e4-9131-31a711893171.png' | |
attachment = [ | |
{ | |
"color": "warning", | |
"pretext": ":warning: Code-Manager Deployment: *failed*", | |
"title": "Code-Manager deployment FAILED for environment: %s" % (environment), | |
"text": "%s" % (error), | |
"mrkdwn_in": [ | |
"text", | |
"pretext" | |
] | |
} | |
] | |
# This uses Bamblings API token, not sure what a better option is | |
slack = Slacker('<YOUR SLACK TOKEN') | |
slack.chat.post_message(room, '', username=username, icon_url=icon_url, attachments=attachment) | |
def trigger_api(branch): | |
# Set the URL/Endpoint for Code-Manger Trigger | |
url = "https://<YOUR HOST NAME>:8170/code-manager/v1/deploys" | |
token = "ADD YOUR TOKEN HERE | |
# Deploy Pushed Branch | |
data = '{"environments": ["%s"], "wait": true}' % branch | |
headers = {'Content-Type': 'application/json', 'X-Authentication': '%s' % token} | |
try: | |
r = requests.post(url, headers=headers, data=data, verify=False, timeout=(20, 30)) | |
except requests.exceptions.RequestException as e: | |
print e | |
sys.exit(1) | |
if r.status_code == 200: | |
values = json.loads(r.text) | |
for index in values: | |
environment = index['environment'] | |
status = index['status'] | |
if status == 'complete': | |
print '\nCode-Manger deployment SUCCESSFUL for environment: %s \n' % environment | |
continue | |
else: | |
error = index['error']['msg'] | |
error = '\n'.join(re.findall(r"\bERROR\b.*", error)) | |
print '\nCode-Manager deployment FAILED for environment: %s \n' % environment | |
print error | |
slack(environment, error) | |
sys.exit(1) | |
else: | |
print 'API Request Failure' | |
print 'Status Code: ', r.status_code | |
print 'Return Reason: ', r.text | |
sys.exit(1) | |
# Gather our code in a main() function | |
def main(args, loglevel): | |
logging.basicConfig(format="%(levelname)s: %(message)s", level=loglevel) | |
for line in sys.stdin: | |
(oldrev, newrev, refname) = line.split() | |
branch = refname.split('/')[2] | |
if newrev == "0000000000000000000000000000000000000000": | |
print "\nCode-Manager deploying production to purge environment: %s \n" % branch | |
branch = 'production' | |
# Run trigger_api def | |
trigger_api(branch) | |
# Standard boilerplate to call the main() function to begin | |
# the program. | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser( | |
description="Triggers Puppet Enterprise Code-Manager Environment Deployment", | |
epilog="" | |
) | |
loglevel_group = parser.add_mutually_exclusive_group(required=False) | |
loglevel_group.add_argument( | |
"-v", | |
"--verbose", | |
help="increase output verbosity", | |
action="count", | |
default=0) | |
loglevel_group.add_argument( | |
"-q", | |
"--quiet", | |
help="decrease output verbosity", | |
action="count", | |
default=0) | |
args = parser.parse_args() | |
# Setup logging | |
# script -vv -> DEBUG | |
# script -v -> INFO | |
# script -> WARNING | |
# script -q -> ERROR | |
# script -qq -> CRITICAL | |
# script -qqq -> no logging at all | |
loglevel = logging.WARNING + 10*args.quiet - 10*args.verbose | |
# Set 'max'/'min' levels for logging | |
if loglevel > 50: | |
loglevel = 50 | |
elif loglevel < 10: | |
loglevel = 10 | |
main(args, loglevel) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Throwing exception about malformed data.
I need to massage into payload to json.
Thanks for work