def migrateData(): # start with creating table setupDatabase() # read the old data f = open("/home/master-nafsdm/data/domains.txt") oldDomainsRaw = f.read() f.close() domainsTable = [] # put it into a table for domain in oldDomainsRaw.split("\n"): if len(domain.split()) == 5: domainVars = domain.split() if domainVars[4] == "dnssec.yes": domainsList = [ domainVars[0], domainVars[1], domainVars[2], domainVars[3], "y" ] domainsTable.append(domainsList) elif domainVars[4] == "dnssec.no": domainsList = [ domainVars[0], domainVars[1], domainVars[2], domainVars[3], "n" ] domainsTable.append(domainsList) # open up sql connection connection = sqlite3.connect("/home/master-nafsdm/data/domains.sql") cursor = connection.cursor() # format the info from the table above into a sql command for addDomain in domainsTable: format_str = """INSERT INTO domain (id, domain, masterIP, comment, assignedNodes, dnssec) VALUES (NULL, "{domain}", "{masterIP}", "{comment}", "{assignedNodes}", "{dnssec}");""" sql_command = format_str.format(domain=addDomain[0], masterIP=addDomain[1], comment=addDomain[2], assignedNodes=addDomain[3], dnssec=addDomain[4]) cursor.execute(sql_command) # close connection connection.commit() connection.close() log("Migration complete. Deleting old domains.txt file.") os.remove("/home/master-nafsdm/data/domains.txt") log("Done.")
def setupSSH(): #generatedPassword = generatePassword() try: output = subprocess.check_output([ "ssh-keygen", "-b", "4096", "-t", "rsa", "-f", "/home/master-nafsdm/.ssh/nafsdm_rsa", "-q", "-C", "nafsdm_master", "-N", "" ]) output = subprocess.check_output([ "cp", "/home/master-nafsdm/.ssh/nafsdm_rsa.pub", "/home/master-nafsdm/.ssh/authorized_keys" ]) except Exception, e: log("FATAL: Some error ocurred during SSH key generation.")
def setupDatabase(): # setup DB connection connection = sqlite3.connect("/home/master-nafsdm/data/domains.sql") cursor = connection.cursor() # command to setup the table create_table_cmd = """ CREATE TABLE domain ( id INTEGER PRIMARY KEY, domain VARCHAR(255), masterIP VARCHAR(15), comment TEXT, assignedNodes TEXT, dnssec CHAR(1));""" # execute the command cursor.execute(create_table_cmd) # close connection connection.commit() connection.close() log("Database & table created!")
def checkUpdate(): normalUpdate = False doICUpdate = False if devStatus == True: log("Developer mode enabled, skipping version checking.") else: if devIcMode: log("Development commit update mode.") response = requests.get( "https://api.github.com/repos/mrkakisen/nafsdm/branches/development" ) if (response.status_code == requests.codes.ok): data = response.json() latestCommit = data["commit"]["sha"][0:7] if version.split("-")[0] == latestCommit: log("You're using the latest development commit!") else: doICUpdate = True log("A new update is available (dev commit - my version: " + version + " - latest version: " + latestCommit + "-dev)") else: log("FATAL: Couldn't connect to GitHub! Quitting...") exit(1) else: log("Checking if a new version is available..") r = requests.get( "https://raw.githubusercontent.com/MrKaKisen/nafsdm/" + github_branch + "/version.txt") # check if we got a good code, requests has builtin codes which are OK if (r.status_code == requests.codes.ok): if (r.text.split("\n")[0] == version): log("You're running the latest version, " + version + "!") normalUpdate = False else: log("NOTICE: There is a new version available! New version: " + r.text.split("\n")[0]) normalUpdate = True if normalUpdate == True or doICUpdate == True: if (os.path.exists("/home/master-nafsdm/tempUpgrade")): log("WARN: folder already exists?") else: os.makedirs("/home/master-nafsdm/pythondaemon/tempUpgrade") # shortcut to make the shit importable f = open( "/home/master-nafsdm/pythondaemon/tempUpgrade/__init__.py", "w") f.write(" ") f.close() url = ("https://raw.githubusercontent.com/MrKaKisen/nafsdm/" + github_branch + "/scripts/upgradeMaster.sh") r = requests.get(url) if (r.status_code == requests.codes.ok): f = open( "/home/master-nafsdm/pythondaemon/tempUpgrade/temp_upgrade.sh", "w") f.write(r.content) f.close() import subprocess outputNull = subprocess.check_output([ "chmod", "+x", "/home/master-nafsdm/pythondaemon/tempUpgrade/temp_upgrade.sh" ]) url = ("https://raw.githubusercontent.com/MrKaKisen/nafsdm/" + github_branch + "/scripts/upgradeMaster.py") r = requests.get(url) if (r.status_code == requests.codes.ok): f = open( "/home/master-nafsdm/pythondaemon/tempUpgrade/temp_upgrade.py", "w") f.write(r.content) f.close() import subprocess outputNull = subprocess.check_output([ "chmod", "+x", "/home/master-nafsdm/pythondaemon/tempUpgrade/temp_upgrade.py" ]) from tempUpgrade.temp_upgrade import initUpgrade if doICUpdate: upgradeStatus = initUpgrade(github_branch, True) else: upgradeStatus = initUpgrade(github_branch, False) if upgradeStatus == "exception": log("FATAL: An error occured during upgrade. The script probably failed mid-through (that would break your installation). Please retry or run the script manually." ) exit(1) elif upgradeStatus == "unsupported": log("WARNING: You're running an unsupported version - nafsdm will not be able to upgrade." ) log("WARNING: Consider using developer mode to skip version checking." ) elif upgradeStatus == "unknownException": log("Unknown exception occured during upgrade.") exit(1) else: f = open("/home/master-nafsdm/upgradeLog.log", "w") f.write(upgradeStatus) f.close() log("Upgrade completed. Make sure no additional adjustments are required for this particular upgrade." ) log("Upgrade log is available at /home/master-nafsdm/upgradeLog.log" ) exit(0) else: log("FATAL: Failed to establish connection to GitHub.") exit(1) else: log("FATAL: Failed to establish connection to GitHub.") exit(1)
# nafsdm # __main__ # masterd startup file # (c) Vilhelm Prytz 2018 # https://github.com/mrkakisen/nafsdm # imports import os.path from setup import setupSSH, migrateData, setupDatabase from daemonlog import log from version import version from versionCheck import checkUpdate log("*******************************************************") log("master-nafsdm - running version " + version) log("*******************************************************") log("Performing pre-start checks..") # pre checks if not os.path.isfile("/home/master-nafsdm/.ssh/nafsdm_rsa"): log("SSH directory not found. Performing first time setup!") setupSSH() # check for upgrade script if os.path.isfile( "/home/master-nafsdm/pythondaemon/tempUpgrade/temp_upgrade.sh"): log("Upgrade script found. Please change the data file and remove upgrade script!" ) exit(1)
def setupSSH(): #generatedPassword = generatePassword() try: output = subprocess.check_output([ "ssh-keygen", "-b", "4096", "-t", "rsa", "-f", "/home/master-nafsdm/.ssh/nafsdm_rsa", "-q", "-C", "nafsdm_master", "-N", "" ]) output = subprocess.check_output([ "cp", "/home/master-nafsdm/.ssh/nafsdm_rsa.pub", "/home/master-nafsdm/.ssh/authorized_keys" ]) except Exception, e: log("FATAL: Some error ocurred during SSH key generation.") log("To continue, please copy /home/master-nafsdm/.ssh/nafsdm_rsa to all slaves /home/slave-nafsdm/.ssh/master_key" ) # SQL prepare def setupDatabase(): # setup DB connection connection = sqlite3.connect("/home/master-nafsdm/data/domains.sql") cursor = connection.cursor() # command to setup the table create_table_cmd = """ CREATE TABLE domain ( id INTEGER PRIMARY KEY, domain VARCHAR(255), masterIP VARCHAR(15), comment TEXT,