-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh_client_web.py
More file actions
123 lines (95 loc) · 3.93 KB
/
Copy pathssh_client_web.py
File metadata and controls
123 lines (95 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import os
import paramiko
from dotenv import load_dotenv
import time
import json
import argparse
import logging
import sys
from flask import Flask, jsonify
# from fastapi import FastAPI
# from starlette.middleware.cors import CORSMiddleware
import datetime, time
import warnings
from ssh_client import utils, work
warnings.filterwarnings("ignore")
''' pip install python-dotenv'''
load_dotenv() # will search for .env file in local folder and load variables
# Configure basic logging to console with INFO level and a custom format
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
stream=sys.stdout)
app = Flask(__name__)
# app = FastAPI(
# title="FastAPI Basic Docker with k8s Service",
# description="FastAPI Basic Docker with k8s Service",
# version="0.0.1",
# # terms_of_service="http://example.com/terms/",
# )
# app.add_middleware(
# CORSMiddleware,
# allow_origins=["*"],
# allow_credentials=True,
# allow_methods=["*"],
# allow_headers=["*"],
# )
@app.route("/ssh/<string:env>/<string:service>/<string:cmd>", methods=["GET"])
def service(env, service, cmd):
"""
Execute the services via ssh
Sample Request: http://localhost:8000/ssh/new-dev/spark_cluster/start
Args:
env(string): The name of env
service(string) : The name of service
cmd (string) : The name of command
Returns:
json: message
Return Sample {"message":"Succeed to execute 'start' commands. The Spark_cluster is already started.","result":"Service Spark_cluster PID as 17513","status":200}
"""
try:
logging.info(f"ENV : {env}, Service : {service}, CMD = {cmd}")
''' load ssh_config.json'''
ssh_config = utils.load_json_config("./ssh_config.json", env)
if ssh_config is None:
return {
"status" : 500,
"message" : "{}".format("Please ensure the env name")
}
logging.info(json.dumps(ssh_config, indent=2))
# ''' call to perform the ssh commands'''
response = work(ssh_config, service, cmd)
logging.info(f"response : {json.dumps(response, indent=2)}\n")
return jsonify(response), 200, {'Content-Type': 'application/json'}
except Exception as e:
logging.error(e)
return jsonify(response), 500, {'Content-Type': 'application/json'}
@app.route('/')
def service_main():
# return render_template('./index.html', host_name=socket.gethostname().split(".")[0], linked_port=port, service_host=env_name)
return {
"app" : "standalone-ssh-service.py",
"started_time" : datetime.datetime.now()
}
if __name__ == '__main__':
"""
Running this service to check the status of tools and send alerts
python ./ssh-client-web.py --port 8000 or ./ssh_client_web.sh start
http://localhost:8000/ssh/test/kibana/start
"""
parser = argparse.ArgumentParser(description="Running this service to check the status of tools and send alerts using this script")
parser.add_argument('-port', '--port', dest='port', default=8000, help='port')
args = parser.parse_args()
if args.port:
_port = args.port
try:
''' Expose this app to acesss'''
''' Flask at first run: Do not use the development server in a production environment '''
''' For deploying an application to production, one option is to use Waitress, a production WSGI server. '''
# app.run(host="0.0.0.0", port=int(port)-4000)
from waitress import serve
serve(app, host="0.0.0.0", port=_port)
logging.info(f"# Flask App's Port : {_port}")
except Exception as e:
logging.error(f"An error occurred: {e}")
finally:
logging.info("** Job is being performed..")