# Path: constants/exceptions.py
# class loginFailedException(Exception):
# class loginBannedException(Exception):
# class tokenNotFoundException(Exception):
# class channelNoPermissionsException(Exception):
# class channelUnknownException(Exception):
# class channelModeratedException(Exception):
# class noAdminException(Exception):
# class commandSyntaxException(Exception):
# class banchoConfigErrorException(Exception):
# class banchoMaintenanceException(Exception):
# class moderatedPMException(Exception):
# class userNotFoundException(Exception):
# class alreadyConnectedException(Exception):
# class stopSpectating(Exception):
# class matchWrongPasswordException(Exception):
# class matchNotFoundException(Exception):
# class matchJoinErrorException(Exception):
# class matchCreateError(Exception):
# class banchoRestartingException(Exception):
# class apiException(Exception):
# class invalidArgumentsException(Exception):
# class messageTooLongWarnException(Exception):
# class messageTooLongException(Exception):
# class userSilencedException(Exception):
# class need2FAException(Exception):
# class userRestrictedException(Exception):
# class haxException(Exception):
# class forceUpdateException(Exception):
# class loginLockedException(Exception):
# class unknownStreamException(Exception):
# class userTournamentException(Exception):
# class userAlreadyInChannelException(Exception):
# class userNotInChannelException(Exception):
# class missingReportInfoException(Exception):
# class invalidUserException(Exception):
# class wrongChannelException(Exception):
# class periodicLoopException(Exception):
# 
# Path: helpers/systemHelper.py
# def dispose():
# def runningUnderUnix():
# def scheduleShutdown(sendRestartTime, restart, message = "", delay=20):
# def restartServer():
# def shutdownServer():
# def getSystemInfo():
# 
# Path: objects/glob.py
# 		VERSION = f.read().strip()
# 	VERSION = "Unknown"
# DATADOG_PREFIX = "peppy"


import json
import tornado.web
import tornado.gen
from common.sentry import sentry
from common.log import logUtils as log
from common.web import requestsManager
from constants import exceptions
from helpers import systemHelper
from objects import glob




class handler(requestsManager.asyncRequestHandler):
	@tornado.web.asynchronous
	@tornado.gen.engine
	@sentry.captureTornado
	def asyncGet(self):
		statusCode = 400
		data = {"message": "unknown error"}
		try:
			# Check arguments
			if not requestsManager.checkArguments(self.request.arguments, ["k"]):
				raise exceptions.invalidArgumentsException()

			# Check ci key
			key = self.get_argument("k")
			if key is None or key != glob.conf.config["server"]["cikey"]:
				raise exceptions.invalidArgumentsException()

			log.info("Ci event triggered!!")

# Path: constants/exceptions.py
# class loginFailedException(Exception):
# class loginBannedException(Exception):
# class tokenNotFoundException(Exception):
# class channelNoPermissionsException(Exception):
# class channelUnknownException(Exception):
# class channelModeratedException(Exception):
# class noAdminException(Exception):
# class commandSyntaxException(Exception):
# class banchoConfigErrorException(Exception):
# class banchoMaintenanceException(Exception):
# class moderatedPMException(Exception):
# class userNotFoundException(Exception):
# class alreadyConnectedException(Exception):
# class stopSpectating(Exception):
# class matchWrongPasswordException(Exception):
# class matchNotFoundException(Exception):
# class matchJoinErrorException(Exception):
# class matchCreateError(Exception):
# class banchoRestartingException(Exception):
# class apiException(Exception):
# class invalidArgumentsException(Exception):
# class messageTooLongWarnException(Exception):
# class messageTooLongException(Exception):
# class userSilencedException(Exception):
# class need2FAException(Exception):
# class userRestrictedException(Exception):
# class haxException(Exception):
# class forceUpdateException(Exception):
# class loginLockedException(Exception):
# class unknownStreamException(Exception):
# class userTournamentException(Exception):
# class userAlreadyInChannelException(Exception):
# class userNotInChannelException(Exception):
# class missingReportInfoException(Exception):
# class invalidUserException(Exception):
# class wrongChannelException(Exception):
# class periodicLoopException(Exception):
# 
# Path: helpers/systemHelper.py
# def dispose():
# def runningUnderUnix():
# def scheduleShutdown(sendRestartTime, restart, message = "", delay=20):
# def restartServer():
# def shutdownServer():
# def getSystemInfo():
# 
# Path: objects/glob.py
# 		VERSION = f.read().strip()
# 	VERSION = "Unknown"
# DATADOG_PREFIX = "peppy"


# Path: handlers/ciTriggerHandler.py
import json
import tornado.web
import tornado.gen
from common.sentry import sentry
from common.log import logUtils as log
from common.web import requestsManager
from constants import exceptions
from helpers import systemHelper
from objects import glob





class handler(requestsManager.asyncRequestHandler):
	@tornado.web.asynchronous
	@tornado.gen.engine
	@sentry.captureTornado
	def asyncGet(self):
		statusCode = 400
		data = {"message": "unknown error"}
		try:
			# Check arguments
			if not requestsManager.checkArguments(self.request.arguments, ["k"]):
				raise exceptions.invalidArgumentsException()

			# Check ci key
			key = self.get_argument("k")
			if key is None or key != glob.conf.config["server"]["cikey"]:
				raise exceptions.invalidArgumentsException()

			log.info("Ci event triggered!!")

			systemHelper.scheduleShutdown(5, False, "A new Bancho update is available and the server will be restarted in 5 seconds. Thank you for your patience.")