Last active
November 17, 2015 15:22
-
-
Save Loknar/1ce422ab28c0ce21cb97 to your computer and use it in GitHub Desktop.
This file contains 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/python | |
# -*- coding: utf-8 -*- | |
import sys | |
import traceback | |
from fabric.api import task, env, run, settings | |
from fabric.utils import abort | |
from fabric.colors import blue, green, yellow, red | |
from my_slackclient import slack_msg | |
################################# | |
# Global variables and settings # | |
################################# | |
HOST = 'elo' | |
PORT = 22 | |
AND = ' && ' | |
env.user = 'mrbluesky' | |
######### | |
# Tasks # | |
######### | |
@task | |
def fail_test1(host=HOST, port=PORT): | |
''' | |
fabric failure example | |
sends slack message on failure | |
''' | |
notify('fail test', msg_type='info') | |
try: | |
with settings(host_string = '%s:%s' % (host, port)): | |
run('''python -c "raise Exception('foobar')"''') | |
except: # catch all failures, no matter the type | |
exception_type, value, tb_msg = sys.exc_info() | |
traceback_msg = traceback.format_exc() | |
message = 'something went wrong, got exception type %s.\n' % exception_type.__name__ | |
message += 'Runtime output on `elo` in `/var/log/fail_test.log` might provide more info.\n' | |
message += 'If not then perhaps the fabric exception traceback has some info:\n' | |
message += '```\n%s\n```' % traceback_msg | |
notify(message, msg_type='error') | |
abort('sent slack message, now aborting for good ...') | |
notify('script ran successfully', msg_type='success') | |
@task | |
def fail_test2(host=HOST, port=PORT): | |
''' | |
another fabric failure example | |
sends slack message on failure | |
''' | |
ran_successfully = False | |
notify('fail test', msg_type='info') | |
try: | |
with settings(host_string = '%s:%s' % (host, port)): | |
run('''python -c "raise Exception('foobar')"''') | |
ran_successfully = True | |
finally: # catch all failures, no matter the type | |
if ran_successfully: | |
notify('script ran successfully', msg_type='success') | |
else: | |
message = 'something went wrong.\n' | |
message += 'Runtime output on `elo` in `/var/log/fail_test.log` might provide more info.\n' | |
notify(message, msg_type='error') | |
######### | |
# Utils # | |
######### | |
def notify(msg, msg_type='info', colorless=False): | |
''' | |
Prints message, also sends slack message to maintainer | |
msg_type is in msg_types, defaults to info if provided is not supported | |
''' | |
msg_types = ['info','success','warning','error'] | |
if not msg_type in msg_types: | |
msg_type = 'info' | |
message = '%s: %s' % (msg_type.upper(), msg) | |
print '\n' | |
if msg_type == msg_types[0]: | |
if colorless: | |
print message | |
else: | |
print blue(message) | |
message = ':information_source: %s' % message | |
elif msg_type == msg_types[1]: | |
if colorless: | |
print message | |
else: | |
print green(message) | |
message = ':white_check_mark: %s' % message | |
elif msg_type == msg_types[2]: | |
if colorless: | |
print message | |
else: | |
print yellow(message) | |
message = ':warning: %s' % message | |
elif msg_type == msg_types[3]: | |
if colorless: | |
print message | |
else: | |
print red(message) | |
message = ':bangbang: %s' % message | |
print '\n' | |
slack_msg(message) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment