server.py 1.74 KB
Newer Older
Fardale's avatar
Fardale committed
1 2
#!/usr/bin/python3

Fardale's avatar
Fardale committed
3
import common
Fardale's avatar
Fardale committed
4 5
import http.server
import subprocess
Fardale's avatar
Fardale committed
6
import sys,os,pwd,grp
Fardale's avatar
Fardale committed
7
import argparse
Fardale's avatar
Fardale committed
8 9

def ssh(host, command):
Fardale's avatar
Fardale committed
10 11
    cossh = subprocess.Popen(["ssh", "%s" % host, command], shell=False, stdout=subprocess.PIPE)
    print(cossh.stdout.readlines())
Fardale's avatar
Fardale committed
12 13


Fardale's avatar
Fardale committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
def run():
    PORT = 8080
    server_address = ("", PORT)

    server = http.server.HTTPServer
    handler = http.server.SimpleHTTPRequestHandler
    print("Serveur actif sur le port :", PORT)

    httpd = server(server_address, handler)
    while True:
        httpd.handle_request()
        ssh("01.dptinfo.ens-cachan.fr", "cd projet_genielog/the_dungeon_project/; git pull")


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("-fg", help="run the server in foreground", action="store_true")
    args = parser.parse_args()
    if args.fg:
        run()
        exit()
    # do the UNIX double-fork magic, see Stevens' "Advanced
    # Programming in the UNIX Environment" for details (ISBN 0201563177)
    try:
        pid = os.fork()
        if pid > 0:
            # exit first parent
            sys.exit(0)
Fardale's avatar
Fardale committed
42
    except OSError as e:
Fardale's avatar
Fardale committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
        print("fork #1 failed: %d (%s)" % (e.errno, e.strerror),file=sys.stderr)
        sys.exit(1)

    # decouple from parent environment
    os.chdir("/")   #don't prevent unmounting....
    os.setsid()
    os.umask(0)

    # do second fork
    try:
        pid = os.fork()
        if pid > 0:
            # exit from second parent, print eventual PID before
            #print "Daemon PID %d" % pid
            open(common.PIDFILE, 'w').write("%d" % pid)
            sys.exit(0)
Fardale's avatar
Fardale committed
59
    except OSError as e:
Fardale's avatar
Fardale committed
60 61
        print("fork #2 failed: %d (%s)" % (e.errno, e.strerror),file=sys.stderr)
        sys.exit(1)
Fardale's avatar
Fardale committed
62

Fardale's avatar
Fardale committed
63 64
    #main loop
    run()