def test_printVerbose(capsys): logManager.printVerbose("TEST") captured = capsys.readouterr() assert captured.out == "TEST\n" config = configManager.getConfig() configManager.set(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_VERBOSE_OPTION_, "no") logManager.printVerbose("TEST") captured = capsys.readouterr() assert captured.out == ""
def deploy(): config = configManager.getConfig() deployUrl = configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_DEPLOY_URL_OPTION) if deployUrl is None or deployUrl == sad._INIT_CONFIG_DEPLOY_URL: gaeErrorHandler.addError("Deploy Url is emprty", sad._CRITICAL_ERROR_) webhookPath = configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_WEBHOOK_PATH_OPTION) if webhookPath is None or webhookPath == sad._INIT_CONFIG_WEBHOOK_PATH: webhookPath = configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_TOKEN_OPTION_) gaeErrorHandler.handle() _createSkeleton() commandManager.runGAEDeploy() _deleteSkeleton() commandManager.runCurlCommand(deployUrl + "set_webhook") logManager.printVerbose("\nDeploying bot in Google Cloud...OK")
def _verifyGAEInstallation(): commandManager.runLsCommand(sad._GAE_PATH_, writeFile=sad._TEMP_LS_VERIFY_FILE_NAME) tempFile = open(sad._TEMP_LS_VERIFY_FILE_NAME, 'r') line = tempFile.readline() tempFile.close() commandManager.runRmCommand(sad._TEMP_LS_VERIFY_FILE_NAME) tokens = line.split(' ') tokens[0] = tokens[0].rstrip('\n') if tokens[0] != sad._GAE_PATH_: logManager.printVerbose("Google Cloud SDK is not installed") if utils.hasSupport(): gaeErrorHandler.addError( "You have to install Google Cloud SDK. Instructions: https://cloud.google.com/sdk/docs/quickstart-debian-ubuntu", sad._CRITICAL_ERROR_) else: gaeErrorHandler.addError("Google Cloud SDK is no installed", sad._CRITICAL_ERROR_) gaeErrorHandler.handle()
def deployBot(withOptions=False, testFlag=True, generateFlag=True): if not withOptions: if utils.file_Or_Directory_Exists(sad._ACTUAL_PATH, sad._OUTPUT_BOT_DIR_): if utils.file_Or_Directory_Exists(sad._OUTPUT_BOT_DIR_, sad._OUTPUT_BOT_NAME_): generateFlag = False headers = utils._getHeaders() testFlag = headers[sad._HEADER_TOKEN_FLAG] == sad._STR_TRUE_ if testFlag: if generateFlag: generateBot(testFlag) logManager.printVerbose("Running Test Bot") commandManager.runPythonCommand(sad.OUTPUT_BOT_PATH) commandManager.runRmCommand(sad._MODULES_DIR_ + sad._DF_ + sad._LINUX_ALL_TAG_ + sad._PYC_EXTENTION) else: cloudManager.configure() if generateFlag: generateBot(testFlag) cloudManager.deploy()
def changeState(testFlag): if testFlag: deployBot(withOptions=True) else: logManager.printVerbose("Changing to deploy mode...") logging.basicConfig( format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) logger = logging.getLogger(__name__) config = configManager.getConfig() updater = Updater( config.get(sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_TOKEN_OPTION_)) webhookURL = configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_DEPLOY_URL_OPTION) webhookPath = configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_WEBHOOK_PATH_OPTION) if (webhookURL[-1] == '/'): webhookURL = webhookURL[:-1] if (webhookPath[-1] == '/'): webhookPath = webhookPath[:-1] webhookURL += sad._DF_ + webhookPath logManager.printVerbose("Changing webhook to: " + webhookURL) updater.bot.setWebhook(webhookURL) logManager.printVerbose("DONE")
def _verifyCurlInstallation(): commandManager.runLsCommand(sad._CURL_PATH, writeFile=sad._TEMP_LS_VERIFY_FILE_NAME) tempFile = open(sad._TEMP_LS_VERIFY_FILE_NAME, 'r') line = tempFile.readline() tempFile.close() commandManager.runRmCommand(sad._TEMP_LS_VERIFY_FILE_NAME) tokens = line.split(' ') tokens[0] = tokens[0].rstrip('\n') if tokens[0] != sad._CURL_PATH: logManager.printVerbose("Curl is not installed") flagOption = inputManager.getYesNoAnswer( "Do you want to install Curl (Y/n): ") if not flagOption: gaeErrorHandler.addError("Curl is necessary to deploy the bot", sad._CRITICAL_ERROR_) else: if utils.hasSupport(): commandManager.runPackageManagerInstall(sad._CURL_PACKAGE) else: gaeErrorHandler.addError("Curl is necessary to deploy the bot", sad._CRITICAL_ERROR_) gaeErrorHandler.handle()
def deploy(): _crateSkeleton() commandManager.runGitAddAll() logManager.printVerbose("Commiting changes...") commandManager.runGitCommitCommand(utils.getTime() + ": Deploy bot in heroku") logManager.printVerbose("Deploying bot in Heroku...") commandManager.runGitPushCommand(sad._DEPLOY_HEROKU_OPTION, sad._GIT_MASTER) _deleteSkeleton() logManager.printVerbose("Deploying bot in Heroku...OK")
def _verifyHerokuInstallation(): commandManager.runLsCommand(sad._HEROKU_SNAP_PATH, writeFile=sad._TEMP_LS_VERIFY_FILE_NAME) tempFile = open(sad._TEMP_LS_VERIFY_FILE_NAME, 'r') line = tempFile.readline() tempFile.close() commandManager.runRmCommand(sad._TEMP_LS_VERIFY_FILE_NAME) tokens = line.split(' ') tokens[0] = tokens[0].rstrip('\n') if (tokens[0] != sad._HEROKU_SNAP_PATH): logManager.printVerbose("Snap is not installed") if (utils.hasSupport()): answer = inputManager.getYesNoAnswer( "Do you want to install snapd? (y/n):") if answer: commandManager.runPackageManagerInstall( sad._HEORKU_SNAP_PACKAGE) logManager.printVerbose("Waiting for snapd...") time.sleep(30) else: herokuErrorHandler.addError("Snap is not installed", sad._CRITICAL_ERROR_) else: herokuErrorHandler.addError("Snap is not installed", sad._CRITICAL_ERROR_) herokuErrorHandler.handle() commandManager.runSnapListCommand(sad._TEMP_SNAP_LIST_FILE_NAME) tempFile = open(sad._TEMP_SNAP_LIST_FILE_NAME, 'r') flag = False for line in tempFile: tokens = line.split(' ') if (tokens[0] == sad._DEPLOY_HEROKU_OPTION): flag = True break tempFile.close() commandManager.runRmCommand(sad._TEMP_SNAP_LIST_FILE_NAME) if not flag: logManager.printVerbose("heroku-cli is not installed") answer = inputManager.getYesNoAnswer( "Do you want to install heroku-cli? (y/n):") if answer: commandManager.runSnapInstallCommand( sad._DEPLOY_HEROKU_OPTION, sad._HEROKU_HEROKU_CLI_VERSION_) else: herokuErrorHandler.addError("heroku-cli is not installerd", sad._CRITICAL_ERROR_) herokuErrorHandler.handle()
def destroy(): config = configManager.getConfig() hosting = configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_HOSTING_OPTION_) logManager.printVerbose("WARNING: This action delete the bot in the cloud") token = inputManager.getInput("For security, paste here the bot token: ") if token == configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_TOKEN_OPTION_): logManager.printVerbose("Deleting bot in the cloud...") if hosting == sad._DEPLOY_HEROKU_OPTION: herokuManager.deleteCloudApp() elif hosting == sad._DEPLOY_GAE_OPTION: gaeManager.deleteCloudApp() else: logManager.printVerbose("Ufff, the tokens don't match")
def _createSkeleton(): appConfigFile = open(sad._GAE_APPENGINE_CONFIG_FILE_NAME_, 'w') appConfigFile.write("import os\n") appConfigFile.write("from google.appengine.ext import vendor\n") appConfigFile.write( "vendor.add(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'lib'))\n" ) appConfigFile.close() commandManager.runCpCommand(sad.OUTPUT_BOT_PATH, sad._ACTUAL_PATH) appYamlFile = open(sad._GAE_APP_YAML, 'w') runtimeFile = open(sad._CONFIG_RUNTIME_FILE_PATH_, 'r') runtime = runtimeFile.readline() runtimeFile.close() appYamlFile.write("runtime: " + runtime + "\n") appYamlFile.write("api_version: 1\n") appYamlFile.write("threadsafe: yes\n\n") appYamlFile.write("handlers:\n") appYamlFile.write("- url: .*\n") appYamlFile.write(" script: bot.app\n\n") appYamlFile.write("libraries:\n") appYamlFile.write("- name: webapp2\n") appYamlFile.write(" version: \"2.5.2\"\n") appYamlFile.write("- name: flask\n") appYamlFile.write(" version: latest\n") appYamlFile.write("- name: ssl\n") appYamlFile.write(" version: latest\n") appYamlFile.close() if _generateReq(): commandManager.runCpCommand(sad._GAE_TEMP_REQ_FILE_PATH, sad._GAE_REQ_FILE_NAME) logManager.printVerbose("We need to install the requirements") logManager.printVerbose("We need sudo privileges") logManager.printVerbose( "Command to run: sudo pip install -t lib -r requirements.txt") commandManager.runPipInstallReq() commandManager.runRmCommand(sad._GAE_TEMP_REQ_FILE_PATH) commandManager.runTouchSudoCommand(sad._GAE_LIB_DIR_NAME_ + sad._DF_ + sad._INIT_PY)
def initConfiguration(): logManager.printVerbose("Verifying configuration...") logManager.printVerbose("Verifying Google Cloud installation...") _verifyGAEInstallation() logManager.printVerbose("Verifying Curl installation...") _verifyCurlInstallation() logManager.printVerbose("Verifying Google Cloud login...") while True: if not _verifyGAELogin(): logManager.printVerbose("Can't find google account") logManager.printVerbose("Google login...") _GAELogin() else: break config = configManager.getConfig() projectName = configManager.get(config, sad._DEPLOY_GAE_OPTION, sad._CONFIG_PROJECT_NAME_OPTION_) logManager.printVerbose("Verifiying project name...") if projectName != None and projectName != sad._INIT_CONFIG_PROJECT_NAME: logManager.printVerbose("Project name found: " + projectName) else: projectName = _getNewGAEName(config) if _verifyGAEProject(projectName): logManager.printVerbose( "The project has already been created in Google Cloud") token = configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_TOKEN_OPTION_) deployUrl = sad._HTTPS_ + projectName + sad._GAE_URL_ configManager.set(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_DEPLOY_URL_OPTION, deployUrl) configManager.set(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_WEBHOOK_PATH_OPTION, token) else: logManager.printVerbose("Project hasn't been created in Google Cloud") while True: _GAECreate(projectName) if _verifyGAEProject(projectName): token = configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_TOKEN_OPTION_) deployUrl = sad._HTTPS_ + projectName + sad._GAE_URL_ configManager.set(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_DEPLOY_URL_OPTION, deployUrl) configManager.set(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_WEBHOOK_PATH_OPTION, token) break logManager.printVerbose( "The project can't created in Google Cloud. Read the errors above and chose a new Google Cloud project name" ) projectName = _getNewGAEName(config) _GAEsetProject(projectName) logManager.printVerbose("All Configurations... OK")
def _initConfiguration(projectNameFlag=True, initProjectFlag=True, gitInitFlag=True, gitHerokuFlag=-1): config = configManager.getConfig() if gitInitFlag: logManager.printVerbose("Creating git...") commandManager.runGitInitCommand() gitHerokuFlag = -1 if projectNameFlag: logManager.printVerbose("Project name doesn't found") _getNewHerokuName(config) initProjectFlag = True if initProjectFlag: logManager.printVerbose("Project hasn't been created in heroku") erroFlag = False while True: if erroFlag: logManager.printVerbose( "The project can't created in heroku. Read the errors above and chose a new heroku project name" ) _getNewHerokuName(config) projectName = configManager.get(config, sad._DEPLOY_HEROKU_OPTION, sad._CONFIG_PROJECT_NAME_OPTION_) token = configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_TOKEN_OPTION_) deployUrl = sad._HTTPS_ + projectName + sad._HEROKU_URL gitUrl = sad._HTTPS_ + sad._HEORKU_GIT_URL + projectName + sad._GIT_EXTENTION commandManager.runHerokuCreateCommand(projectName) if _verifyProject(projectName): configManager.set(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_DEPLOY_URL_OPTION, deployUrl) configManager.set(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_WEBHOOK_PATH_OPTION, token) configManager.set(config, sad._DEPLOY_HEROKU_OPTION, sad._CONFIG_GIT_OPTION_, gitUrl) gitHerokuFlag = _verifyRemoteHeroku(gitUrl) break else: erroFlag = True if gitHerokuFlag == -1: logManager.printVerbose("Adding git remote heroku...") gitUrl = configManager.get(config, sad._DEPLOY_HEROKU_OPTION, sad._CONFIG_GIT_OPTION_) commandManager.runGitAddRemoteCommand(sad._DEPLOY_HEROKU_OPTION, gitUrl) if gitHerokuFlag == 0: logManager.printVerbose("Setting git remote heroku...") gitUrl = configManager.get(config, sad._DEPLOY_HEROKU_OPTION, sad._CONFIG_GIT_OPTION_) commandManager.runGitSetRemoteUrlCommand(sad._DEPLOY_HEROKU_OPTION, gitUrl)
def initConfiguration(): logManager.printVerbose("Verifying configuration...") logManager.printVerbose("Verifying Heroku installation...") _verifyHerokuInstallation() logManager.printVerbose("Verifying Heroku login...") while True: if not _verifyHerokuLogIn(): logManager.printVerbose("Can't find heroku token") logManager.printVerbose("Heorku login...") _herokuLogIn() else: break config = configManager.getConfig() projectNameFlag = True initProjectFlag = True gitInitFlag = True gitHerokuFlag = True projectName = configManager.get(config, sad._DEPLOY_HEROKU_OPTION, sad._CONFIG_PROJECT_NAME_OPTION_) logManager.printVerbose("Verifiying project name...") if projectName != None and projectName != sad._INIT_CONFIG_PROJECT_NAME: logManager.printVerbose("Project name found: " + projectName) projectNameFlag = False logManager.printVerbose("Verifiying project in heroku...") if _verifyProject(projectName): logManager.printVerbose( "The project has already been created in heroku") initProjectFlag = False if utils.file_Or_Directory_Exists(sad._ACTUAL_PATH, sad._GIT_DIR_): logManager.printVerbose("Git has already been created") gitInitFlag = False gitHerokuFlag = _verifyRemoteHeroku( configManager.get(config, sad._DEPLOY_HEROKU_OPTION, sad._CONFIG_GIT_OPTION_)) if gitHerokuFlag > 0: logManager.printVerbose("Git has already been configured") _initConfiguration(projectNameFlag, initProjectFlag, gitInitFlag, gitHerokuFlag) logManager.printVerbose("All Configurations... OK")