[~] Refactor

This commit is contained in:
Siarhei Siniak 2021-11-07 01:45:18 +03:00
parent 2339d03038
commit 010a1e7f4b
3 changed files with 123 additions and 81 deletions

@ -1,4 +1,5 @@
import subprocess
import threading
import io
import json
import sys
@ -8,22 +9,28 @@ import traceback
import time
with io.open(sys.argv[1], 'r') as f:
config = json.load(f)
def forward(config_dir, config):
server_address = config['server_address']
username = config['username']
target_address = config['target_address']
blank_endpoint = config['blank_endpoint']
target_ports = config['target_ports']
app_name = config['app_name']
ssh_key = os.path.join(
config_dir,
config['ssh_key']
)
ssh_command = 'ssh -o "ExitOnForwardFailure yes" -o "StrictHostKeyChecking no"'
server_address = config['server_address']
username = config['username']
target_address = config['target_address']
blank_endpoint = config['blank_endpoint']
target_ports = config['target_ports']
ports = dict(
ports = dict(
target=r'''
ssh \
%s -i %s \
%s@%s \
%s \
-v -N;
-N;
''' % (
ssh_command,
ssh_key,
username,
server_address,
' '.join([
@ -36,11 +43,13 @@ ports = dict(
]),
),
blank=r'''
ssh \
%s -i %s \
%s@%s \
%s \
-v -N;
-N;
''' % (
ssh_command,
ssh_key,
username,
server_address,
' '.join([
@ -51,16 +60,16 @@ ports = dict(
for pair in target_ports
]),
)
)
)
app_name = config['app_name']
has_server = lambda : subprocess.call([
has_server = lambda : subprocess.call([
'ping','-c', '1', target_address,
]) == 0
], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) == 0
notify = lambda msg : subprocess.check_output(['notify-send', '-t', '5000', app_name, msg])
#notify = lambda msg : subprocess.check_output(['notify-send', '-t', '5000', app_name, msg])
notify = lambda msg: subprocess.call(['echo', app_name, msg])
while True:
while True:
notify('started')
if has_server():
t6 = ports['target']
@ -91,3 +100,23 @@ while True:
finally:
p.terminate()
notify('stopped')
config_path = sys.argv[1]
config_dir = os.path.split(
config_path
)[0]
with io.open(config_path, 'r') as f:
configs = json.load(f)
t1 = []
for config in configs:
t2 = threading.Thread(
target=forward,
args=[config_dir, config],
)
t2.start()
t1.append(t2)
for o in t1:
o.join()

@ -4,8 +4,14 @@ services:
build:
context: .
dockerfile: ./docker/blank-app/Dockerfile
ports:
- 80:80
volumes:
- .:/app:rw
restart: always
forward:
build:
context: .
dockerfile: ./docker/forward/Dockerfile
volumes:
- ./d1/forward.py:/app/d1/forward.py:ro
- ./tmp/cache/forward_data:/app/tmp/cache/forward_data:ro
restart: always

@ -0,0 +1,7 @@
FROM python:3
WORKDIR /app
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y iputils-ping
ENTRYPOINT sh -c ' \
python d1/forward.py tmp/cache/forward_data/forward.config.json \
'