summaryrefslogtreecommitdiff
path: root/kbas/__main__.py
blob: 534b5819ac278dfae3e4144575554c322464d703 (plain)
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
#!/usr/bin/env python
# Copyright (C) 2015-2016  Codethink Limited
#
# 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; version 2 of the License.
#
# 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 <http://www.gnu.org/licenses/>.
#
# =*= License: GPL-2 =*=

import logging
import os
import glob
import shutil
from time import strftime, gmtime
from datetime import datetime
import tempfile
import yaml
from bottle import Bottle, request, response, template, static_file
from subprocess import call

from ybd import app, cache

bottle = Bottle()


class KeyedBinaryArtifactServer(object):

    ''' Generic artifact cache server

        Configuration can be found in the associated kbas.conf file.'''

    def __init__(self):
        app.load_configs([
            os.path.join(os.getcwd(), 'kbas.conf'),
            os.path.join(os.path.dirname(__file__), 'config', 'kbas.conf')])
        app.config['start-time'] = datetime.now()
        app.config['last-upload'] = datetime.now()

        try:
            import cherrypy
            server = 'cherrypy'
        except:
            server = 'wsgiref'

        # for development:
        if app.config.get('mode') == 'development':
            bottle.run(server=server, host=app.config['host'],
                       port=app.config['port'], debug=True, reloader=True)
        else:
            bottle.run(server=server, host=app.config['host'],
                       port=app.config['port'], reloader=True)

    @bottle.get('/static/<filename>')
    def send_static(filename):
        current_dir = os.getcwd()
        static_dir = os.path.join(current_dir, 'kbas/public')
        return static_file(filename, root=static_dir)

    @bottle.get('/<name>')
    @bottle.get('/artifacts/<name>')
    def list(name=""):
        names = glob.glob(os.path.join(app.config['artifact-dir'],
                          '*' + name + '*'))
        try:
            content = [[strftime('%y-%m-%d', gmtime(os.path.getctime(x))),
                       cache.check(os.path.basename(x)),
                       os.path.basename(x)] for x in names]
        except:
            content = [['--------',
                       cache.check(os.path.basename(x)),
                       os.path.basename(x)] for x in names]

        return template('kbas',
                        title='Available Artifacts:',
                        content=reversed(sorted(content)),
                        css='/static/style.css')

    @bottle.get('/1.0/artifacts')
    def get_morph_artifact():
        f = request.query.filename
        path = os.path.join(app.config['artifact-dir'], f)
        if os.path.exists(path):
            call(['touch', path])
        return static_file(f, root=app.config['artifact-dir'], download=True)

    @bottle.get('/get/<cache_id>')
    def get_artifact(cache_id):
        f = os.path.join(cache_id, cache_id)
        path = os.path.join(app.config['artifact-dir'], f)
        if os.path.exists(path):
            call(['touch', os.path.dirname(path)])
        return static_file(f, root=app.config['artifact-dir'], download=True)

    @bottle.get('/')
    @bottle.get('/status')
    def status():
        stat = os.statvfs(app.config['artifact-dir'])
        free = stat.f_frsize * stat.f_bavail / 1000000000
        artifacts = len(os.listdir(app.config['artifact-dir']))
        started = app.config['start-time'].strftime('%y-%m-%d %H:%M:%S')
        last_upload = app.config['last-upload'].strftime('%y-%m-%d %H:%M:%S')
        content = [['Started:', started]]
        content += [['Last upload:', last_upload]]
        if app.config.get('last-reject'):
            content += [['Last reject:', app.config['last-reject']]]
        content += [['Space:', str(free) + 'GB']]
        content += [['Artifacts:', str(artifacts)]]
        return template('kbas',
                        title='KBAS status',
                        content=content,
                        css='/static/style.css')

    @bottle.post('/upload')
    def post_artifact():
        if app.config['password'] is 'insecure' or \
                request.forms.get('password') != app.config['password']:
            print 'Upload attempt: password fail'
            app.config['last-reject'] = \
                datetime.now().strftime('%y-%m-%d %H:%M:%S')
            response.status = 401  # unauthorized
            return
        cache_id = request.forms.get('filename')
        if os.path.isdir(os.path.join(app.config['artifact-dir'], cache_id)):
            if cache.check(cache_id) == request.forms.get('checksum', 'XYZ'):
                response.status = 777  # this is the same binary we have
                return
            response.status = 405  # method not allowed, this artifact exists
            return

        tempfile.tempdir = app.config['artifact-dir']
        tmpdir = tempfile.mkdtemp()
        try:
            upload = request.files.get('file')
            artifact = os.path.join(tmpdir, cache_id)
            upload.save(artifact)
            if call(['tar', 'tf', artifact]):
                app.log(this, 'ERROR: not a valid tarfile:', artifact)
                raise
            checksum = cache.md5(artifact)
            with open(artifact + '.md5', "a") as f:
                f.write(checksum)
            shutil.move(tmpdir, os.path.join(app.config['artifact-dir'],
                                             cache_id))
            response.status = 201  # success!
            app.config['last-upload'] = datetime.now()
            return
        except:
            # something went wrong, clean up
            try:
                shutil.rmtree(tmpdir)
            except:
                pass
            response.status = 999

        return


KeyedBinaryArtifactServer().__init__()