Skip to content
This repository has been archived by the owner on May 13, 2022. It is now read-only.

Bunch of fixes #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,22 @@
# To build: sudo docker build [--no-cache=true] [--pull=true] --rm=true -t gobot .
# To run e.g: sudo docker run --name=gobot -d gobot -e "JID=myjid@chat" -e "JPASSWD=pwd" -e "JROOM=room" -e "JNICK=nick" -e "GODOMAIN=domain" -e "GOSTAGES=stage,names"


FROM ubuntu:14.04
FROM ubuntu:16.04

MAINTAINER Eugene Venter

# install required packages
RUN apt-get update && apt-get -y install git python python-dev python-pip


RUN useradd -ms /bin/bash iamgobot

USER iamgobot
WORKDIR /home/iamgobot
RUN apt-get update && \
apt-get -y install python python-dev python-pip && \
apt-get clean && \
rm -rf /tmp/* /var/tmp/* /var/lib/apt/lists/*

RUN git clone http://github.com/eugeneventer/gobot.git
RUN git config --global user.name gobot
RUN git config --global user.email gobothasnoemail
RUN useradd -m -s /bin/bash iamgobot

USER root
RUN pip install -r gobot/requirements.txt
ADD requirements.txt /tmp/
RUN pip install -r /tmp/requirements.txt && rm -f /tmp/requirements.txt
ADD gobot.py start_gobot.sh taglines.txt /gobot/

USER iamgobot
ENTRYPOINT ["/bin/bash", "/home/iamgobot/gobot/start_gobot.sh"]
WORKDIR /gobot
CMD ["/bin/sh", "/gobot/start_gobot.sh"]
118 changes: 61 additions & 57 deletions gobot.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/usr/bin/env python

import os
import sys
import logging
import getpass
from optparse import OptionParser
import argparse
import json
import random

Expand All @@ -14,7 +15,7 @@

# ensure utf8 encoding
if sys.version_info < (3, 0):
reload(sys)
reload(sys) # noqa
sys.setdefaultencoding('utf8')
else:
raw_input = input
Expand All @@ -31,20 +32,14 @@ def __init__(self, jabberid, password, room, nick, godomain, stages):
self.stages = stages

self.add_event_handler("session_start", self.start)

self.add_event_handler("groupchat_message", self.bot_message)

self.add_event_handler('gocd_listen', self.gocd_listen, threaded=True)

self.add_event_handler('disconnected', self.gocd_listen_stop)

def start(self, event):
self.get_roster()
self.send_presence()
self.plugin['xep_0045'].joinMUC(self.room,
self.nick,
wait=True)

self.plugin['xep_0045'].joinMUC(self.room, self.nick, wait=True)
self.event('gocd_listen')

def bot_message(self, msg):
Expand All @@ -56,7 +51,8 @@ def bot_message(self, msg):
tagfile = open('taglines.txt')
tagline = next(tagfile)
for num, aline in enumerate(tagfile):
if random.randrange(num + 2): continue
if random.randrange(num + 2):
continue
tagline = aline
tagfile.close()

Expand All @@ -66,23 +62,28 @@ def bot_message(self, msg):

def gocd_listen(self, event):
failedpipes = []

def gocd_message(ws, message):
msg = json.loads(message)
pipename = msg['pipeline']['name']
stage = msg['pipeline']['stage']
if stage['name'] in self.stages:
golink = 'https://%s/go/tab/pipeline/history/%s' % (self.godomain, pipename)
golink = 'https://{domain}/go/tab/pipeline/history/{pipe}'.format(
domain=self.godomain, pipe=pipename)
if stage['state'] == 'Passed' and pipename in failedpipes:
failedpipes.remove(pipename)
self.send_message(mto=self.room,
mbody="%s (%s) fixed :) - %s" % (pipename, stage['name'], golink),
mtype='groupchat')
self.send_message(
mto=self.room,
mbody="{pipe} ({stage}) fixed :) - {link}".format(
pipe=pipename, stage=stage['name'], link=golink),
mtype='groupchat')
elif stage['state'] == 'Failed' and pipename not in failedpipes:
failedpipes.append(pipename)
self.send_message(mto=self.room,
mbody='%s (%s) broken :( - %s' % (pipename, stage['name'], golink),
mtype='groupchat')

self.send_message(
mto=self.room,
mbody='{pipe} ({stage}) broken :( - {link}'.format(
pipe=pipename, stage=stage['name'], link=golink),
mtype='groupchat')

def gocd_error(ws, error):
logging.error("GOCD ERROR!!!")
Expand All @@ -93,20 +94,20 @@ def gocd_close(ws):

websocket.enableTrace(True)

self.ws = websocket.WebSocketApp("ws://%s:8887/" % self.godomain,
on_message = gocd_message,
on_error = gocd_error,
on_close = gocd_close)
self.ws = websocket.WebSocketApp("ws://{domain}:8887/".format(domain=self.godomain),
on_message=gocd_message,
on_error=gocd_error,
on_close=gocd_close)

sleepsecs = 60
while (1):
try:
self.ws.run_forever()
logging.error("Trying websocket reconnect in %s seconds" % sleepsecs)
logging.error("Trying websocket reconnect in {} seconds".format(sleepsecs))
time.sleep(sleepsecs)
except:
logging.error("Unexpected error:", sys.exc_info()[0])
logging.error("Trying websocket reconnect in %s seconds" % sleepsecs)
logging.error("Trying websocket reconnect in {} seconds".format(sleepsecs))
time.sleep(sleepsecs)

def gocd_listen_stop(self, event):
Expand All @@ -115,43 +116,46 @@ def gocd_listen_stop(self, event):

if __name__ == '__main__':
# Setup the command line arguments.
optp = OptionParser()

optp.add_option('-q', '--quiet', help='set logging to ERROR',
action='store_const', dest='loglevel',
const=logging.ERROR, default=logging.INFO)
optp.add_option("-j", "--jabberid", dest="jabberid",
help="Jabber ID")
optp.add_option("-p", "--password", dest="password",
help="password")
optp.add_option("-n", "--nick", dest="nick",
help="Nickname")
optp.add_option("-r", "--room", dest="room",
help="Room to join")
optp.add_option("-g", "--godomain", dest="godomain",
help="GoCD domain to connect to")
optp.add_option("-s", "--stages", dest="stages",
help="comma-seperated list of stage names to report on")

opts, args = optp.parse_args()
argp = argparse.ArgumentParser(description="GoCD bot")

argp.add_argument('-q', '--quiet', help='set logging to ERROR',
action='store_const', dest='loglevel',
const=logging.ERROR, default=logging.INFO)
argp.add_argument("-j", "--jabberid", dest="jabberid",
help="Jabber ID")
argp.add_argument("-p", "--password", dest="password",
help="password (insecure, use env variable GOBOT_PASSWORD instead)")
argp.add_argument("-n", "--nick", dest="nick",
help="Nickname")
argp.add_argument("-r", "--room", dest="room",
help="Room to join")
argp.add_argument("-g", "--godomain", dest="godomain",
help="GoCD domain to connect to")
argp.add_argument("-s", "--stages", dest="stages",
help="comma-seperated list of stage names to report on")

args = argp.parse_args()

# Setup logging.
logging.basicConfig(level=opts.loglevel,
logging.basicConfig(level=args.loglevel,
format='%(levelname)-8s %(message)s')

if opts.jabberid is None:
opts.jabberid = raw_input("Username: ")
if opts.password is None:
opts.password = getpass.getpass("Password: ")
if opts.nick is None:
opts.nick = raw_input("Nickname: ")
if opts.room is None:
opts.room = raw_input("Room: ")

xmpp = GoBot(opts.jabberid, opts.password, opts.room, opts.nick, opts.godomain, opts.stages.split(','))
xmpp.register_plugin('xep_0030') # Service Discovery
xmpp.register_plugin('xep_0045') # Multi-User Chat
xmpp.register_plugin('xep_0199') # XMPP Ping
if args.jabberid is None:
args.jabberid = raw_input("Username: ")
if args.password is None:
if os.environ.get('GOBOT_PASSWORD'):
args.password = os.environ.get('GOBOT_PASSWORD')
else:
args.password = getpass.getpass("Password: ")
if args.nick is None:
args.nick = raw_input("Nickname: ")
if args.room is None:
args.room = raw_input("Room: ")

xmpp = GoBot(args.jabberid, args.password, args.room, args.nick, args.godomain, args.stages.split(','))
xmpp.register_plugin('xep_0030') # Service Discovery
xmpp.register_plugin('xep_0045') # Multi-User Chat
xmpp.register_plugin('xep_0199') # XMPP Ping

if xmpp.connect():
xmpp.process(block=True)
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pyasn1
pyasn1-modules
sleekxmpp
dnspython
websocket-client
10 changes: 6 additions & 4 deletions start_gobot.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash
#!/bin/sh

# first, ensure git repo is up to date
cd /home/iamgobot/gobot && git pull
python gobot.py -j $JID -p $JPASSWD -r $JROOM -n $JNICK -g $GODOMAIN -s $GOSTAGES
if [ -z "${GOBOT_PASSWORD}" ]; then
GOBOT_PASSWORD="${JPASSWD}"; export GOBOT_PASSWORD
fi

exec python /gobot/gobot.py -j "${JID}" -r "${JROOM}" -n "${JNICK}" -g "${GODOMAIN}" -s "${GOSTAGES}"