[~] 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 subprocess
import threading
import io import io
import json import json
import sys import sys
@ -8,22 +9,28 @@ import traceback
import time import time
with io.open(sys.argv[1], 'r') as f: def forward(config_dir, config):
config = json.load(f)
server_address = config['server_address'] server_address = config['server_address']
username = config['username'] username = config['username']
target_address = config['target_address'] target_address = config['target_address']
blank_endpoint = config['blank_endpoint'] blank_endpoint = config['blank_endpoint']
target_ports = config['target_ports'] 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"'
ports = dict( ports = dict(
target=r''' target=r'''
ssh \ %s -i %s \
%s@%s \ %s@%s \
%s \ %s \
-v -N; -N;
''' % ( ''' % (
ssh_command,
ssh_key,
username, username,
server_address, server_address,
' '.join([ ' '.join([
@ -36,11 +43,13 @@ ports = dict(
]), ]),
), ),
blank=r''' blank=r'''
ssh \ %s -i %s \
%s@%s \ %s@%s \
%s \ %s \
-v -N; -N;
''' % ( ''' % (
ssh_command,
ssh_key,
username, username,
server_address, server_address,
' '.join([ ' '.join([
@ -53,12 +62,12 @@ ports = dict(
) )
) )
app_name = config['app_name']
has_server = lambda : subprocess.call([ has_server = lambda : subprocess.call([
'ping','-c', '1', target_address, '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') notify('started')
@ -91,3 +100,23 @@ while True:
finally: finally:
p.terminate() p.terminate()
notify('stopped') 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: build:
context: . context: .
dockerfile: ./docker/blank-app/Dockerfile dockerfile: ./docker/blank-app/Dockerfile
ports:
- 80:80
volumes: volumes:
- .:/app:rw - .:/app:rw
restart: always 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 \
'