-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
341 lines (280 loc) · 14.1 KB
/
app.py
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# This file is part of RESTerville, a Workflow Automation toolkit.
# Copyright (C) 2024 GEOACE
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# You can contact the developer via email or using the contact form provided at https://geoace.net
from flask import Flask, render_template, request, Response, jsonify, stream_with_context
import subprocess
import logging
import os
import sys
import json
from json import dumps
import traceback
# Configure logging to output to stdout immediately
logging.basicConfig(
level=logging.DEBUG, # Adjust the log level as needed
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[logging.StreamHandler(sys.stdout)]
)
# Ensure real-time flushing
class FlushHandler(logging.StreamHandler):
def emit(self, record):
super().emit(record)
self.flush()
# Apply the flush handler to the root logger
logging.getLogger().handlers = [FlushHandler(sys.stdout)]
API_KEY = os.getenv('API_KEY')
if not API_KEY:
logging.error('API_KEY not set in environment variables')
sys.exit(1)
app = Flask(__name__)
def validate_api_key():
""" Validate that the API key in the request arguments matches the expected API key. """
api_key = request.args.get('api_key') if request.method == 'GET' else request.form.get('api_key')
print(f"Received API key: {api_key}") # Debug print
if api_key != API_KEY:
return False
return True
@app.route('/')
def home():
return render_template('index.html')
@app.route('/agol2pg', methods=['GET', 'POST'])
def run_pg_script():
try:
# Fetch parameters based on the request method
if request.method == 'GET':
if not validate_api_key():
return Response("Invalid API key", status=403)
service_name = request.args.get('service')
url = request.args.get('url')
table = request.args.get('table')
schema = request.args.get('schema', 'public')
source_epsg = request.args.get('source_epsg')
target_epsg = request.args.get('target_epsg')
oid = request.args.get('oid')
batch = request.args.get('batch', '1000') # Fetch 'batch' parameter, default to '1000'
save_attachments = dumps(request.args.get('save_attachments', "false")) # Default to 'false'
bucket = request.args.get('bucket', os.getenv('BUCKET'))
elif request.method == 'POST':
if not validate_api_key():
return Response("Invalid API key", status=403)
service_name = request.form.get('service')
url = request.form.get('url')
table = request.form.get('table')
schema = request.form.get('schema', 'public')
source_epsg = request.form.get('source_epsg')
target_epsg = request.form.get('target_epsg')
oid = request.form.get('oid')
batch = request.form.get('batch', '1000') # Fetch 'batch' parameter, default to '1000'
save_attachments = dumps(request.args.get('save_attachments', "false")) # Default to 'false'
bucket = request.args.get('bucket', os.getenv('BUCKET'))
print(f"Received parameters: service_name={service_name}, url={url}, table={table}, schema={schema}, batch={batch}", "save_attachments={save_attachments}", "bucket={bucket}") # Debug print
if not service_name or not url or not table:
return Response('Missing required parameters (service, url, table)', status=400)
if save_attachments == "true" and not bucket:
return Response('Missing required parameter (bucket) for saving attachments', status=400)
command = ['python3', 'lib/agol_to_pg.py', service_name, url, table, '--schema', schema, '--batch', batch]
if source_epsg:
command += ['--source_epsg', source_epsg]
if target_epsg:
command += ['--target_epsg', target_epsg]
if oid:
command += ['--oid', oid]
if save_attachments:
command += ['--save_attachments', save_attachments]
command += ['--bucket', bucket]
print(f"Running command: {' '.join(command)}") # Debug print
def generate():
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
while True:
output = process.stdout.readline()
if output:
yield f"data:{output}\n\n"
err = process.stderr.readline()
if err:
yield f"data:{err}\n\n"
if output == '' and process.poll() is not None:
break
return Response(stream_with_context(generate()), mimetype='text/event-stream')
except Exception as e:
print(f"An error occurred: {e}")
print(traceback.format_exc())
return Response(f"An internal error occurred: {str(e)}", status=500)
@app.route('/pg2agol', methods=['GET', 'POST'])
def run_pg_to_agol_script():
try:
# Fetch parameters based on the request method
if request.method == 'GET':
if not validate_api_key():
return Response("Invalid API key", status=403)
service_name = request.args.get('service')
url = request.args.get('url')
table = request.args.get('table')
schema = request.args.get('schema', 'public')
batch = request.args.get('batch', '100') # Set default batch size to 100
truncate = request.args.get('truncate', 'no') # Default option for truncation
target_epsg = request.args.get('target_epsg', '3857') # Default EPSG code
geom = request.args.get('geom')
ignore = request.args.get('ignore')
portal_url = request.args.get('portal_url')
elif request.method == 'POST':
if not validate_api_key():
return Response("Invalid API key", status=403)
service_name = request.form.get('service')
url = request.form.get('url')
table = request.form.get('table')
schema = request.form.get('schema', 'public')
batch = request.form.get('batch', '100') # Set default batch size to 100
truncate = request.form.get('truncate', 'no') # Default option for truncation
target_epsg = request.form.get('target_epsg', '3857') # Default EPSG code
geom = request.form.get('geom')
ignore = request.form.get('ignore')
portal_url = request.form.get('portal_url')
if not service_name or not url or not table:
return Response('Missing required parameters (service, url, table)', status=400)
# Construct the command line arguments
command = ['python3', 'lib/pg_to_agol.py', service_name, url, table, '--schema', schema, '--batch', batch, '--truncate', truncate, '--target_epsg', target_epsg]
# Optional parameters with command line handling
if geom:
command.extend(['--geom', geom])
if ignore:
command.extend(['--ignore', ignore])
if portal_url:
command.extend(['--portal_url', portal_url])
print(f"Running command: {' '.join(command)}") # Debug print
def generate():
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
while True:
output = process.stdout.readline()
if output:
yield f"data:{output}\n\n"
err = process.stderr.readline()
if err:
yield f"data:{err}\n\n"
if output == '' and process.poll() is not None:
break
return Response(stream_with_context(generate()), mimetype='text/event-stream')
except Exception as e:
print(f"An error occurred: {e}")
print(traceback.format_exc())
return Response(f"An internal error occurred: {str(e)}", status=500)
@app.route('/backup', methods=['GET', 'POST'])
def backup():
try:
# Fetch parameters based on the request method
if request.method == 'GET':
if not validate_api_key():
return Response("Invalid API key", status=403)
remove_archives = request.args.get('remove_archives', 'no')
duration = request.args.get('duration')
bucket = request.args.get('bucket', os.getenv('BUCKET'))
usernames = request.args.get('usernames')
elif request.method == 'POST':
if not validate_api_key():
return Response("Invalid API key", status=403)
remove_archives = request.form.get('remove_archives', 'no')
duration = request.form.get('duration')
bucket = request.form.get('bucket', os.getenv('BUCKET'))
usernames = request.form.get('usernames')
# Ensure the required usernames parameter is provided
if not usernames:
return Response("The 'usernames' parameter is required", status=400)
# Build the backup command with appropriate arguments
command = ['python3', 'lib/backup.py', '--remove_archives', remove_archives, '--usernames', usernames]
if remove_archives == 'yes' and duration:
command += ['--duration', duration]
if bucket:
command += ['--bucket_name', bucket]
print(f"Running command: {' '.join(command)}") # Debug print
def generate():
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, env=os.environ.copy())
while True:
output = process.stdout.readline()
if output:
yield f"data:{output}\n\n"
err = process.stderr.readline()
if err:
yield f"data:{err}\n\n"
if output == '' and process.poll() is not None:
break
return Response(stream_with_context(generate()), mimetype='text/event-stream')
except Exception as e:
return Response(f"An error occurred: {str(e)}", status=500)
@app.route('/pg_function', methods=['GET', 'POST'])
def pg_function():
try:
# Fetch parameters based on the request method
if request.method == 'GET':
if not validate_api_key():
return Response("Invalid API key", status=403)
service_name = request.args.get('service', 'default_service') # Default service if not provided
function_name = request.args.get('function')
schema = request.args.get('schema', 'public') # Default schema if not provided
elif request.method == 'POST':
if not validate_api_key():
return Response("Invalid API key", status=403)
service_name = request.form.get('service', 'default_service') # Default service if not provided
function_name = request.form.get('function')
schema = request.form.get('schema', 'public') # Default schema if not provided
# Ensure the required function_name parameter is provided
if not function_name:
return Response("Function parameter is required", status=400)
# Call the external script and pass the service name, function name, and schema
command = ['python3', 'lib/pg_function.py', service_name, function_name, schema]
print(f"Running command: {' '.join(command)}") # Debug print
def generate():
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
while True:
output = process.stdout.readline()
if output:
yield f"data:{output}\n\n"
err = process.stderr.readline()
if err:
yield f"data:{err}\n\n"
if output == '' and process.poll() is not None:
break
return Response(stream_with_context(generate()), mimetype='text/event-stream')
except Exception as e:
print(f"An error occurred: {e}")
print(traceback.format_exc())
return Response(f"An internal error occurred: {str(e)}", status=500)
@app.route('/pg_service', methods=['GET', 'POST'])
def pg_service():
try:
# Validate API key based on request method
if request.method == 'GET':
if not validate_api_key():
return Response("Invalid API key", status=403)
elif request.method == 'POST':
if not validate_api_key():
return Response("Invalid API key", status=403)
# Path to the `get_services.py` script
script_path = "lib/get_services.py" # Adjust this path as needed
print(f"Running script: {script_path}") # Debug print
def generate():
process = subprocess.Popen(['python3', script_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
while True:
output = process.stdout.readline()
if output:
yield f"data:{output}\n\n"
err = process.stderr.readline()
if err:
yield f"data:{err}\n\n"
if output == '' and process.poll() is not None:
break
return Response(stream_with_context(generate()), mimetype='text/event-stream')
except Exception as e:
print(f"An error occurred: {e}")
print(traceback.format_exc())
return Response(f"An internal error occurred: {str(e)}", status=500)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)