def setup_name_and_ports(): cfg = psite.get_cfg() val = psite.get_option("external_name") if val is not None: cfg['external_name'] = val cfg['plain_port'] = 80 cfg['ssl_port'] = 443 cfg['port_base'] = 8000 else: nat_info = re.split("\\s+", psite.slurp_file("/etc/apache2/NAT_INFO")) if len(nat_info) >= 2: cfg['nat_name'] = nat_info[0] cfg['port_base'] = int(nat_info[1]) else: cfg['nat_name'] = "localhost" cfg['port_base'] = 8000 cfg['external_name'] = cfg['nat_name'] if 'plain_port' not in cfg: cfg['plain_port'] = get_free_port() if 'wss_port' not in cfg: port = psite.get_option("wss_port") if port is None: port = get_free_port() cfg['wss_port'] = port
def setup_daemon(): cfg = psite.get_cfg() dprog = psite.get_option("daemon") if dprog is None: return dname = re.sub(r'[.].*$', '', dprog) service_name = "{}-{}.service".format(cfg['siteid'], dname) unit = "" unit += "[Unit]\n" unit += "Description={}\n".format(dprog) unit += "\n" unit += "[Service]\n" unit += "User=root\n" unit += "Type=simple\n" unit += "ExecStart={}/{} run\n".format(cfg['src_dir'], dprog) unit += "WorkingDirectory={}\n".format(cfg['src_dir']) unit += "\n" unit += "[Install]\n" unit += "WantedBy=multi-user.target\n" with open("TMP.service", "w") as outf: outf.write(unit) uname = "/etc/systemd/system/{}".format(service_name) old = psite.slurp_file(uname) if old != unit: print( "sudo sh -c 'cp TMP.service {} && systemctl daemon-reload'".format( uname))
def setup_tunnel(): cfg = psite.get_cfg() tunnel_host = psite.get_option("tunnel_host") tunnel_sshd_port = psite.get_option("tunnel_sshd_port") tunnel_port = psite.get_option("tunnel_port") if tunnel_host is None or tunnel_sshd_port is None or tunnel_port is None: return service_name = "{}-tunnel.service".format(cfg['siteid']) keyfile = "tunnel-key" if not os.path.isfile(keyfile): print("you must obtain", keyfile, "to set up the ssh tunnel") return tun = "" tun += "/usr/bin/ssh" tun += " -NTC" tun += " -o ServerAliveInterval=60" tun += " -o ExitOnForwardFailure=yes" tun += " -o StrictHostKeyChecking=no" tun += " -R {}:localhost:22".format(tunnel_port) tun += " -i {}".format(keyfile) tun += " -p {}".format(tunnel_sshd_port) tun += " tunnel@{}".format(tunnel_host) unit = "" unit += "[Unit]\n" unit += "Description={} ssh tunnel\n".format(cfg['siteid']) unit += "StartLimitIntervalSec=0\n" unit += "\n" unit += "[Service]\n" unit += "User={}\n".format(getpass.getuser()) unit += "Type=simple\n" unit += "ExecStart={}\n".format(tun) unit += "WorkingDirectory={}\n".format(cfg['src_dir']) unit += "Restart=always\n" unit += "RestartSec=30\n" unit += "\n" unit += "[Install]\n" unit += "WantedBy=multi-user.target\n" with open("TMP.tunnel", "w") as outf: outf.write(unit) uname = "/etc/systemd/system/{}".format(service_name) old = psite.slurp_file(uname) if old != unit: print(("sudo sh -c 'cp TMP.tunnel {} &&" " systemctl daemon-reload'").format(uname))
def setup_apache(): cfg = psite.get_cfg() if psite.get_option("flat", 0) == 0: cfg['document_root'] = "{}/static".format(cfg['src_dir']) else: cfg['document_root'] = cfg['src_dir'] conf = make_apache_conf() with open("TMP.conf", "w") as outf: outf.write(conf) av_name = "/etc/apache2/sites-available/{}.conf".format(cfg['siteid']) old = psite.slurp_file(av_name) if old != conf: print( "sudo sh -c 'cp TMP.conf {}; apache2ctl graceful'".format(av_name)) en_name = "/etc/apache2/sites-enabled/{}.conf".format(cfg['siteid']) if not os.path.exists(en_name): print("sudo a2ensite {}".format(cfg['siteid']))
def get_db(): global db if db is not None: return db cfg = psite.get_cfg() db = dict(db=cfg["db"]) if cfg["db"] == "sqlite3": import sqlite3 filename = "{}/{}.db".format(cfg['aux_dir'], cfg['dbname']) db['conn'] = sqlite3.connect(filename) make_writable_for_server(filename) db['cursor'] = db['conn'].cursor() db['table_exists'] = sqlite3_table_exists db['column_exists'] = sqlite3_column_exists db['commit'] = sqlite3_commit return db elif cfg["db"] == "postgres": # apt-get install python3-psycopg2 import psycopg2 dsn = "postgresql://apache@/{}".format(cfg['dbname']) try: db['conn'] = psycopg2.connect(dsn) except(psycopg2.OperationalError): print("can't connect to database, maybe do:") print("createdb -O apache {}".format(cfg['dbname'])) raise sys.exit(1) db['cursor'] = db['conn'].cursor() db['table_exists'] = postgres_table_exists db['column_exists'] = postgres_column_exists db['commit'] = postgres_commit return db elif cfg["db"] == "mysql": # apt-get install python3-mysqldb import MySQLdb try: params = {} params['db'] = cfg['dbname'] if psite.get_option("db_host") is not None: params['host'] = psite.get_option("db_host") params['user'] = psite.get_option("db_user") file = "{}/psite_db_passwd".format(cfg['aux_dir']) pw = psite.slurp_file(file).strip() params['password'] = pw else: # get unix_socket name: mysqladmin variables | grep sock params['unix_socket'] = '/var/run/mysqld/mysqld.sock' db['conn'] = MySQLdb.connect(**params) except(MySQLdb.OperationalError): print("") print("*******") print("can't connect to database, maybe do:") print("mysql -Nrse 'create database `{}`'".format(cfg['dbname'])) print("*******") print("") print("") raise sys.exit(1) db['cursor'] = db['conn'].cursor() db['table_exists'] = mysql_table_exists db['column_exists'] = mysql_column_exists db['commit'] = mysql_commit return db else: print("get_db failed") sys.exit(1)