Last active 1440820878

b3DeployListen.py Raw
1from twisted.words.protocols import irc
2from twisted.internet import protocol, task, reactor
3from twisted.application import internet, service
4import subprocess
5
6nick = "Nexus"
7user = "Nexus"
8passw = "bouncer password"
9jenkins_nick = "HawkJenkins"
10jenkins_job = "Nexus"
11script_path = "/home/nexus/redeploy.sh"
12
13channel = "#DSH105" # channel script will try to join
14server = "bouncer.bounce" # irc server or bouncer
15port = 5555 # if changed, else 6667
16
17class Bot(irc.IRCClient):
18 nickname = nick
19 username = user
20 password = passw
21
22 def signedOn(self):
23 print "Signed on as %s." % self.nickname
24
25 def privmsg(self, user, channel, msg):
26 nick, _, host = user.partition("!")
27 trigger_phrase = "Project %s build" % jenkins_job
28 if nick == jenkins_nick and trigger_phrase in msg and "SUCCESS" in msg:
29 out = subprocess.check_output([script_path])
30 self.msg(channel, "Starting redeploy (triggered by %s)... " % jenkins_nick + out)
31
32class BotFactory(protocol.ClientFactory):
33 protocol = Bot
34
35 def __init__(self, channel):
36 self.channel = channel
37
38 def clientConnectionLost(self, connector, reason):
39 print "Lost connection (%s), reconnecting." % (reason,)
40 connector.connect()
41
42 def clientConnectionFailed(self, connector, reason):
43 print "Could not connect: %s" % (reason,)
44
45if __name__ == "__main__":
46 channel = channel
47 reactor.connectTCP(server, port, BotFactory(channel))
48 reactor.run()
49elif __name__ == '__builtin__':
50 application = service.Application('H365IRCBot')
51 channel = channel
52 ircService = internet.TCPClient(server, port, BotFactory(channel))
53 ircService.setServiceParent(application)
54