[~] Refactor

This commit is contained in:
Siarhei Siniak 2024-05-19 13:11:52 +03:00
parent e2bbae5b99
commit 7b21f92287

@ -1056,6 +1056,12 @@ def http_server(argv):
action='store_true', action='store_true',
default=False, default=False,
) )
parser.add_option(
'--force',
dest='force',
action='store_true',
default=False,
)
parser.add_option( parser.add_option(
'--token', '--token',
dest='token', dest='token',
@ -1068,6 +1074,12 @@ def http_server(argv):
type='int', type='int',
default=80, default=80,
) )
parser.add_option(
'--no_docker',
dest='docker',
action='store_true',
default=None,
)
parser.add_option( parser.add_option(
'-H', '--header', '-H', '--header',
dest='response_headers', dest='response_headers',
@ -1101,7 +1113,19 @@ def http_server(argv):
raise RuntimeError('invalid ip address %s' % options.host) raise RuntimeError('invalid ip address %s' % options.host)
if options.docker is None:
options.docker = True
index_section = 'autoindex on;' 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: if options.public:
location_section = 'location / {%s}' % index_section location_section = 'location / {%s}' % index_section
else: else:
@ -1133,34 +1157,62 @@ def http_server(argv):
'deny all;' 'deny all;'
'}' '}'
'location /%s/ {' 'location /%s/ {'
'alias /app/;' 'alias %s/;'
'%s' '%s'
'%s' '%s'
'}' '}'
) % ( ) % (
path, path,
APP_DIR,
'\n'.join([ '\n'.join([
'add_header %s;' % o 'add_header %s;' % o
for o in options.response_headers for o in options.response_headers
]), ]),
index_section index_section
) )
subprocess.check_call(
r''' if options.docker:
sudo docker run \ subprocess.check_call(
-p %s:%d:80 \ r'''
-u root \ sudo docker run \
-it --entrypoint=/bin/bash \ -p %s:%d:80 \
-v $PWD:/app:ro \ -u root \
--log-driver none \ -it --entrypoint=/bin/bash \
nginx:latest \ -v $PWD:%s:ro \
-c 'echo "server{listen 80; charset UTF-8; root /app; %s}" > /etc/nginx/conf.d/default.conf; nginx -g "daemon off;"' --log-driver none \
''' % ( nginx:latest \
options.host, -c 'echo "server{listen 80; charset UTF-8; root /app; %s}" > /etc/nginx/conf.d/default.conf; nginx -g "daemon off;"'
options.port, ''' % (
location_section, options.host,
), options.port,
shell=True) 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): def pass_ssh_osx(argv):
assert isinstance(argv, list) and all([isinstance(o, str) for o in argv]) assert isinstance(argv, list) and all([isinstance(o, str) for o in argv])