diff --git a/dotfiles/.local/bin/commands b/dotfiles/.local/bin/commands index d45aae4..efaee5a 100755 --- a/dotfiles/.local/bin/commands +++ b/dotfiles/.local/bin/commands @@ -1056,6 +1056,12 @@ def http_server(argv): action='store_true', default=False, ) + parser.add_option( + '--force', + dest='force', + action='store_true', + default=False, + ) parser.add_option( '--token', dest='token', @@ -1068,6 +1074,12 @@ def http_server(argv): type='int', default=80, ) + parser.add_option( + '--no_docker', + dest='docker', + action='store_true', + default=None, + ) parser.add_option( '-H', '--header', dest='response_headers', @@ -1101,7 +1113,19 @@ def http_server(argv): raise RuntimeError('invalid ip address %s' % options.host) + if options.docker is None: + options.docker = True + index_section = 'autoindex on;' + if options.docker: + DATA_DIR = '/usr/share/nginx' + APP_DIR = '/app' + CONF_DIR = '/etc/nginx' + else: + DATA_DIR = '/opt/nginx/%s/data/' % options.token + CONF_DIR = '/opt/nginx/%s/conf/' % options.token + APP_DIR = os.path.abspath(os.path.curdir) + if options.public: location_section = 'location / {%s}' % index_section else: @@ -1133,34 +1157,62 @@ def http_server(argv): 'deny all;' '}' 'location /%s/ {' - 'alias /app/;' + 'alias %s/;' '%s' '%s' '}' ) % ( path, + APP_DIR, '\n'.join([ 'add_header %s;' % o for o in options.response_headers ]), index_section ) - subprocess.check_call( - r''' - sudo docker run \ - -p %s:%d:80 \ - -u root \ - -it --entrypoint=/bin/bash \ - -v $PWD:/app:ro \ - --log-driver none \ - nginx:latest \ - -c 'echo "server{listen 80; charset UTF-8; root /app; %s}" > /etc/nginx/conf.d/default.conf; nginx -g "daemon off;"' - ''' % ( - options.host, - options.port, - location_section, - ), - shell=True) + + if options.docker: + subprocess.check_call( + r''' + sudo docker run \ + -p %s:%d:80 \ + -u root \ + -it --entrypoint=/bin/bash \ + -v $PWD:%s:ro \ + --log-driver none \ + nginx:latest \ + -c 'echo "server{listen 80; charset UTF-8; root /app; %s}" > /etc/nginx/conf.d/default.conf; nginx -g "daemon off;"' + ''' % ( + options.host, + options.port, + APP_DIR, + location_section, + ), + shell=True + ) + else: + if os.path.exists(CONF_DIR): + assert options.force + shutil.rmtree(CONF_DIR) + + if os.path.exists(DATA_DIR): + assert options.force + shutil.rmtree(DATA_DIR) + + subprocess.check_call( + r''' + mkdir -p $CONF_DIR; + echo "server{listen 80; charset UTF-8; root $DATA_DIR; %s}" > + $CONF_DIR/default.conf; + exec nginx -c $CONF_DIR -p $DATA_DIR -g "daemon off;" + ''', + env=dict( + CONF_DIR=CONF_DIR, + DATA_DIR=DATA_DIR, + APP_DIR=APP_DIR, + ), + shell=True, + ) def pass_ssh_osx(argv): assert isinstance(argv, list) and all([isinstance(o, str) for o in argv])