This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/cloudbot

204 lines
6.8 KiB
Plaintext
Raw Normal View History

2012-02-29 10:12:17 +01:00
#!/usr/bin/env python
# Bot Wrapper by neersighted
import sys
import os
import subprocess
2012-02-29 18:44:05 +01:00
import re
2012-02-29 20:01:31 +01:00
import json
2012-02-29 18:44:05 +01:00
2012-03-01 04:38:49 +01:00
if os.path.isfile("./config"):
try:
config = json.load(open('config'))
2012-03-01 06:01:53 +01:00
command = ":"
2012-03-01 04:38:49 +01:00
except ValueError, e:
print 'error: malformed config', e
2012-03-01 04:57:34 +01:00
else:
config = False
2012-03-01 06:01:53 +01:00
command = "python bot.py"
2012-02-29 19:38:50 +01:00
nocol = "\033[1;m"
red = "\033[1;31m"
green = "\033[1;32m"
2012-02-29 18:44:05 +01:00
usage = "usage: ./cloudbot {start|stop|restart|status}"
2012-02-29 21:11:51 +01:00
quit = "Goodbye! Thanks for using CloudBot!"
2012-02-29 10:12:17 +01:00
pwd = os.getcwd()
2012-02-29 21:11:51 +01:00
clearlog1 = ": > ./bot.log && "
clearlog2 = ": > ./bot.log"
2012-02-29 19:30:31 +01:00
2012-03-01 04:45:29 +01:00
try:
daemoncheck = subprocess.check_output("locate /usr/bin/daemon", shell=True)
except (subprocess.CalledProcessError), e:
2012-03-01 04:57:34 +01:00
daemoncheck = "False"
2012-03-01 04:45:29 +01:00
print "daemon not installed!"
exit
2012-02-29 18:44:05 +01:00
daemon = re.match(r'^/usr/bin/daemon$', daemoncheck)
2012-03-01 04:45:29 +01:00
try:
screencheck = subprocess.check_output("locate /usr/bin/screen", shell=True)
except (subprocess.CalledProcessError), e:
2012-03-01 04:57:34 +01:00
screencheck = "False"
2012-03-01 04:45:29 +01:00
print "screen not installed!"
exit
2012-02-29 19:30:31 +01:00
screen = re.match(r'^/usr/bin/screen$', screencheck)
2012-03-01 06:01:53 +01:00
backend = config.get("wrapper", {}).get("backend", "daemon")
2012-02-29 19:30:31 +01:00
try:
runningcheck = subprocess.check_output("ps ax|grep cloudbot|"\
"grep -v grep|grep -v ./cloudbot", shell=True)
running = re.match(r'^[1-9]+', runningcheck)
except (subprocess.CalledProcessError):
running = False
2012-02-29 18:44:05 +01:00
2012-02-29 19:30:31 +01:00
error1 = red + "Neither screen nor daemon is installed! "\
2012-02-29 19:38:50 +01:00
"This program cannot run! {ERROR 1}" + nocol
2012-02-29 19:30:31 +01:00
error2 = red + "Could not find bot.py! Are you in the wrong folder? "\
"(" + pwd + ") {ERROR 2}" + nocol
error3 = red + "Invalid choice, exiting! {ERROR 3}" + nocol
error4 = red + "Program killed by user! {ERROR 4}" + nocol
error5 = red + "Invalid backend in config! {ERROR 5}" + nocol
2012-02-29 20:01:31 +01:00
error6 = red + "Author error! We be derpin'! {ERROR 6}" + nocol
2012-02-29 18:44:05 +01:00
2012-03-01 05:08:26 +01:00
start = "echo " + "'" + error1 + "'"
stop = "echo " + "'" + error1 + "'"
restart = "echo " + "'" + error1 + "'"
2012-02-29 20:01:31 +01:00
pid = "echo 'Cannot get pid'"
2012-02-29 18:44:05 +01:00
if daemon:
if backend == "daemon":
2012-02-29 20:01:31 +01:00
start = "daemon -n cloudbot -O " + pwd + \
"/bot.log -r python " + pwd + "/bot.py"
2012-02-29 21:11:51 +01:00
stop = clearlog1 + "daemon -n cloudbot --stop"
restart = clearlog1 + "daemon -n cloudbot --restart"
2012-02-29 20:01:31 +01:00
pid = "pidof /usr/bin/daemon"
elif backend == "screen":
2012-02-29 20:01:31 +01:00
start = "screen -S cloudbot -dm python" + pwd +\
"/bot.py >>" + pwd + "/bot.log 2>&1"
2012-02-29 21:11:51 +01:00
stop = clearlog1 + "kill `pidof /usr/bin/screen`"
restart = clearlog1 + "daemon -n cloudbot --restart"
2012-02-29 20:01:31 +01:00
pid = "pidof /usr/bin/screen"
else:
print error5
2012-02-29 18:44:05 +01:00
elif screen:
if backend == "daemon":
2012-02-29 20:01:31 +01:00
start = "daemon -n cloudbot -O " + pwd + \
"/bot.log -r python " + pwd + "/bot.py"
2012-02-29 21:11:51 +01:00
stop = clearlog1 + "daemon -n cloudbot --stop"
2012-02-29 20:01:31 +01:00
restart = stop + " && " + start
pid = "pidof /usr/bin/daemon"
elif backend == "screen":
2012-02-29 20:01:31 +01:00
start = "screen -S cloudbot -dm python" + pwd +\
2012-02-29 18:44:05 +01:00
"/bot.py >>" + pwd + "/bot.log 2>&1"
2012-02-29 21:11:51 +01:00
stop = clearlog1 + "kill `pidof /usr/bin/screen`"
2012-02-29 20:01:31 +01:00
restart = stop + " && " + start
pid = "pidof /usr/bin/screen"
else:
print error5
2012-02-29 18:44:05 +01:00
else:
command = command
2012-02-29 18:44:05 +01:00
print " ______ __ ______ __ __ "\
" _______ .______ ______ .___________."
print " / || | / __ \ | | | | "\
"| \ | _ \ / __ \ | |"
print "| ,----'| | | | | | | | | | "\
"| .--. || |_) | | | | | `---| |----`"
print "| | | | | | | | | | | | "\
"| | | || _ < | | | | | | "
print "| `----.| `----.| `--' | | `--' | "\
"| '--' || |_) | | `--' | | | "
print " \______||_______| \______/ \______/ "\
"|_______/ |______/ \______/ |__| "
print "http://git.io/cloudbot "\
" by lukeroge"
2012-02-29 18:59:18 +01:00
try:
2012-02-29 19:30:31 +01:00
if running:
if (len(sys.argv) > 1):
if (sys.argv[1] == 'start'):
print "Bot is alread running, cannot start!"
elif (sys.argv[1] == 'stop'):
command = stop
print "Stopping... (" + backend + ")"
2012-02-29 19:30:31 +01:00
elif (sys.argv[1] == 'restart'):
command = restart
print "Restarting... (" + backend + ")"
2012-02-29 19:30:31 +01:00
elif (sys.argv[1] == 'status'):
2012-02-29 19:38:50 +01:00
command = pid
print green + "Bot is running! " + nocol
2012-02-29 21:11:51 +01:00
elif (sys.argv[1] == 'clear'):
command = clearlog2
2012-02-29 19:30:31 +01:00
else:
2012-03-01 06:01:53 +01:00
command = command
2012-02-29 19:30:31 +01:00
print usage
exit
2012-02-29 18:59:18 +01:00
else:
2012-02-29 19:30:31 +01:00
print "{1|start} {2|stop} {3|restart} {4|status} {5|exit}"
read = int(raw_input('Please choose a option: '))
if read == 1:
print "Bot is alread running, cannot start!"
elif read == 2:
command = stop
print "Stopping... (" + backend + ")"
2012-02-29 19:30:31 +01:00
elif read == 3:
command = restart
print "Restarting... (" + backend + ")"
2012-02-29 19:30:31 +01:00
elif read == 4:
2012-02-29 19:38:50 +01:00
command = pid
print green + "Bot is running! " + nocol
2012-02-29 19:30:31 +01:00
elif read == 5:
print quit
else:
print error3
2012-02-29 18:44:05 +01:00
else:
2012-02-29 19:30:31 +01:00
if (len(sys.argv) > 1):
if (sys.argv[1] == 'start'):
command = start
print "Starting... (" + backend + ")"
2012-02-29 19:30:31 +01:00
elif (sys.argv[1] == 'stop'):
print "Bot is not running, cannot stop!"
elif (sys.argv[1] == 'restart'):
print "Bot is not running, cannot restart!"
elif (sys.argv[1] == 'status'):
2012-02-29 19:38:50 +01:00
print red + "Bot is not running!" + nocol
2012-02-29 21:11:51 +01:00
elif (sys.argv[1] == 'clear'):
command = clearlog2
2012-02-29 19:30:31 +01:00
else:
2012-03-01 06:01:53 +01:00
command = command
2012-02-29 19:30:31 +01:00
print usage
exit
2012-02-29 18:59:18 +01:00
else:
2012-02-29 19:30:31 +01:00
print "{1|start} {2|stop} {3|restart} {4|status} {5|exit}"
read = int(raw_input('Please choose a option: '))
if read == 1:
command = start
print "Starting... (" + backend + ")"
2012-02-29 19:30:31 +01:00
elif read == 2:
print "Bot is not running, cannot stop!"
elif read == 3:
command = restart
elif read == 4:
print red + "Bot is not running, cannot restart!" + nocol
elif read == 5:
print quit
else:
print error3
exit
2012-02-29 20:01:31 +01:00
except (TypeError, ValueError), e:
2012-02-29 18:59:18 +01:00
print error3
exit
2012-02-29 20:01:31 +01:00
except (KeyboardInterrupt), e:
2012-02-29 18:59:18 +01:00
print error4
exit
2012-02-29 20:01:31 +01:00
except (NameError, SyntaxError), e:
print error6
2012-02-29 18:59:18 +01:00
exit
2012-02-29 10:16:21 +01:00
if os.path.isfile("./bot.py"):
command = command
2012-02-29 10:12:17 +01:00
else:
2012-02-29 18:44:05 +01:00
print error2
exit
2012-02-29 18:59:18 +01:00
2012-02-29 10:12:17 +01:00
subprocess.call(command, shell=True)
2012-02-29 18:59:18 +01:00
print quit
2012-02-29 18:44:05 +01:00
exit