File: server.py

package info (click to toggle)
python-bumps 0.7.11-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 10,264 kB
  • sloc: python: 22,226; ansic: 4,973; cpp: 4,849; xml: 493; makefile: 163; perl: 108; sh: 101
file content (322 lines) | stat: -rw-r--r-- 10,255 bytes parent folder | download | duplicates (3)
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
# TODO: Add /jobs/<id>/data.zip to fetch all files at once in a zip file format
# TODO: Store completed work in /path/to/store/<id>.zip

import os
import logging
import json
import pickle
import flask
from flask import redirect, url_for, flash
from flask import send_from_directory
from werkzeug.utils import  secure_filename

from . import store

app = flask.Flask(__name__)

# ==== File upload specialization ===
# By uploading files into a temporary file provided by store, we
# can then move the files directly into place on the store rather
# than copy them.  This gives us reduced storage, reduced memory
# and reduced CPU.
class Request(flask.Request):
    # Upload directly into temporary files.
    def _get_file_stream(self, total_content_length, content_type,
                         filename=None, content_length=None):
        #print "returning named temporary file for",filename
        return store.tempfile()
app.request_class = Request


# ==== Format download specialization ===
def _format_response(response, format='json', template=None):
    """
    Return response as a particular format.
    """
    #print "response",response
    if format == 'html':
        if template is None: flask.abort(400)
        return flask.render_template(template, **response)
    elif format == 'json':
        return flask.jsonify(**dict((str(k),v) for k,v in response.items()))
    elif format == 'pickle':
        return pickle.dumps(response)
    else:
        flask.abort(400) # Bad request


@app.route('/jobs.<format>', methods=['GET'])
def list_jobs(format='json'):
    """
    GET /jobs.<format>

    Return a list of all job ids.
    """
    response = dict(jobs=SCHEDULER.jobs())
    return _format_response(response, format, template='list_jobs.html')

@app.route('/jobs/<any(u"pending",u"active",u"error",u"complete"):status>.<format>',
           methods=['GET'])
def filter_jobs(status, format='json'):
    """
    GET /jobs/<pending|active|error|complete>.<format>

    Return all jobs with a particular status.
    """
    response = dict(jobs=SCHEDULER.jobs(status=str(status).upper()))
    return _format_response(response, format, template='list_jobs.html')

@app.route('/jobs.<format>', methods=['POST'])
def create_job(format='json'):
    """
    POST /jobs.<format>

    Schedule a new job, return the job record.

    The POST data should contain::

        {
        notify: "<user@email or @twitterid>",
        service: "<name of service>",
        version: "<service version>",
        name: "<request name>",
        data: "<service data>",
        ...
        }

    The response contains::

        {
        id: <job id>,
        job: <job details>
        }

    Job details is simply a copy of the original request.

    """
    request = flask.request.json #@UndefinedVariable in request proxy
    if request is None: flask.abort(415) # Unsupported media
    id = SCHEDULER.submit(request, origin=flask.request.remote_addr) #@UndefinedVariable in request proxy
    flash('Job %s scheduled' % id)
    response = {'id': id, 'job': SCHEDULER.info(id)}
    #return redirect(url_for('show_job', id=id, format=format))
    return _format_response(response, format=format, template='show_job.html')

@app.route('/jobs/<int:id>.<format>', methods=['GET'])
def show_job(id, format='json'):
    """
    GET /jobs/<id>.<format>

    Get job record by id.

    The response contains::

        {
        id: <job id>,
        job: <job details>
        }

    Job details is simply a copy of the original request.
    """
    response = {'id': id, 'job': SCHEDULER.info(id)}
    return _format_response(response, format=format, template='show_job.html')

@app.route('/jobs/<int:id>/results.<format>', methods=['GET'])
def get_results(id, format='json'):
    """
    GET /jobs/<id>/results.<format>

    Get job results by id.

    Returns::

        {
        id: <job id>
        status: 'PENDING|ACTIVE|COMPLETE|ERROR|UNKNOWN',
        result: <job value>     (absent if status != COMPLETE)
        trace: <error trace>    (absent if status != ERROR)
        }
    """
    response = SCHEDULER.results(id)
    response['id'] = id
    #print "returning response",response
    return _format_response(response, format=format)

@app.route('/jobs/<int:id>/status.<format>', methods=['GET'])
def get_status(id, format='json'):
    """
    GET /jobs/<id>/status.<format>

    Get job status by id.

    Returns::

        {
        id: <job id>,
        status: 'PENDING|ACTIVE|COMPLETE|ERROR|UNKNOWN'
        }
    """
    response = { 'status': SCHEDULER.status(id) }
    response['id'] = id
    return _format_response(response, format=format)


@app.route('/jobs/<int:id>.<format>', methods=['DELETE'])
def delete_job(id, format='json'):
    """
    DELETE /jobs/<id>.<format>

    Deletes a job, returning the list of remaining jobs as <format>
    """
    SCHEDULER.delete(id)
    flash('Job %s deleted' % id)
    response = dict(jobs=SCHEDULER.jobs())
    return _format_response(response, format=format, template="list_jobs.html")
    #return redirect(url_for('list_jobs', id=id, format=format))

@app.route('/jobs/nextjob.<format>', methods=['POST'])
def fetch_work(format='json'):
    # TODO: verify signature
    request = flask.request.json #@UndefinedVariable in request proxy
    if request is None: flask.abort(415) # Unsupported media
    job = SCHEDULER.nextjob(queue=request['queue'])
    return _format_response(job, format=format)

@app.route('/jobs/<int:id>/postjob', methods=['POST'])
def return_work(id):
    # TODO: verify signature corresponds to flask.request.form['queue']
    # TODO: verify that work not already returned by another client
    try:
        #print "decoding <%s>"%flask.request.form['results']
        results = json.loads(flask.request.form['results']) #@UndefinedVariable in request proxy
    except:
        import traceback;
        logging.error(traceback.format_exc())
        results = {
            'status': 'ERROR',
            'error': 'No results returned from the server',
            'trace': flask.request.form['results'], #@UndefinedVariable in request proxy
        }
    _transfer_files(id)
    SCHEDULER.postjob(id, results)
    # Should be signaling code 204: No content
    return _format_response({},format="json")

@app.route('/jobs/<int:id>/data/index.<format>')
def listfiles(id, format):
    try:
        path = store.path(id)
        files = sorted(os.listdir(path))
        finfo = [(f,os.path.getsize(os.path.join(path,f)))
                 for f in files if os.path.isfile(os.path.join(path,f))]
    except:
        finfo = []
    response = { 'files': finfo }
    response['id'] = id
    return _format_response(response, format=format, template="index.html")

# TODO: don't allow putfiles without authentication
#@app.route('/jobs/<int:id>/data/', methods=['GET','PUT'])
def putfiles(id):
    if flask.request.method=='PUT': #@UndefinedVariable in request proxy
        # TODO: verify signature
        _transfer_files(id)
    return redirect(url_for('getfile',id=id,filename='index.html'))

@app.route('/jobs/<int:id>/data/<filename>')
def getfile(id, filename):
    as_attachment = filename.endswith('.htm') or filename.endswith('.html')
    if filename.endswith('.json'):
        mimetype = "application/json"
    else:
        mimetype = None

    return send_from_directory(store.path(id), filename,
                               mimetype=mimetype, as_attachment=as_attachment)

#@app.route('/jobs/<int:id>.<format>', methods=['PUT'])
#def update_job(id, format='.json'):
#    """
#    PUT /job/<id>.<format>
#
#    Updates a job using data from the job submission form.
#    """
#    book = Book(id=id, name=u"I don't know") # Your query
#    book.name = request.form['name'] # Save it
#    flash('Book %s updated!' % book.name)
#    return redirect(url_for('show_job', id=id, format=format))

#@app.route('/jobs/new.html')
#def new_job_form():
#    """
#    GET /jobs/new
#
#    Returns a job submission form.
#    """
#    return render_template('new_job.html')

#@app.route('/jobss/<int:id>/edit.html')
#def edit_job_form(id):
#    """
#    GET /books/<id>/edit
#
#    Form for editing job details
#    """
#    book = Book(id=id, name=u'Something crazy') # Your query
#    return render_template('edit_book.html', book=book)

def _transfer_files(jobid):
    logging.warn("XSS attacks possible if stored file is mimetype html")
    for file in flask.request.files.getlist('file'): #@UndefinedVariable in request proxy
        if not file: continue
        filename = secure_filename(os.path.split(file.filename)[1])
        # Because we used named temporary files that aren't deleted on
        # closing as our streaming file type, we can simply move the
        # resulting files to the store rather than copying them.
        file.stream.close()
        logging.warn("moving %s -> %s"%(file.stream.name, os.path.join(store.path(jobid),filename)))
        os.rename(file.stream.name, os.path.join(store.path(jobid),filename))


def init_scheduler(conf):
    if conf == 'slurm':
        from . import slurm
        Scheduler = slurm.Scheduler
    elif conf == 'direct':
        logging.warn("direct scheduler is not a good choice!")
        try: os.nice(19)
        except: pass
        from . import simplequeue
        Scheduler = simplequeue.Scheduler
    elif conf == 'dispatch':
        from . import dispatcher
        Scheduler = dispatcher.Scheduler
    else:
        raise ValueError("unknown scheduler %s"%conf)
    return Scheduler()

def serve():
    app.run(host='0.0.0.0')

def fullpath(p): return os.path.abspath(os.path.expanduser(p))
def configure(jobstore=None, jobkey=None, jobdb=None, scheduler=None):
    global SCHEDULER, app

    if jobstore:
        store.ROOT = fullpath(jobstore)
    if jobkey:
        app.config['SECRET_KEY'] = open(fullpath(jobkey)).read()
    if jobdb:
        from . import db
        db.DB_URI = jobdb

    SCHEDULER = init_scheduler(scheduler)

if __name__ == '__main__':
    configure(jobstore='/tmp/server/%s',
              jobdb='sqlite:///tmp/jobqueue.db',
              jobkey='~/.bumps/key',
              scheduler='dispatch',
              )
    app.config['DEBUG'] = True
    serve()