start.py 4.54 KB
Newer Older
1
# Copyright (c) 2016, Daniele Venzano
2 3 4 5 6 7 8 9 10 11 12 13 14 15
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

16 17
"""Main points of entry for the Zoe web interface."""

18 19 20
import os
import subprocess

21 22
from zoe_api.web.request_handler import ZoeWebRequestHandler
from zoe_api.auth.base import BaseAuthenticator
23

24 25
import zoe_lib.config

26

27
class RootWeb(ZoeWebRequestHandler):
28
    """Handler class"""
29

30 31
    def get(self):
        """Home page without authentication."""
32
        self.redirect(self.reverse_url("home_user"))
33 34


35
class LoginWeb(ZoeWebRequestHandler):
36 37 38 39
    """The login web page."""

    def get(self):
        """Login page."""
Daniele Venzano's avatar
Daniele Venzano committed
40
        self.render('login.jinja2')
41 42 43 44 45

    def post(self):
        """Try to authenticate."""
        username = self.get_argument("username", "")
        password = self.get_argument("password", "")
46
        user = BaseAuthenticator().full_auth(username, password)
47
        if user is None:
48
            self.redirect(self.get_argument("next", self.reverse_url("login")))
49
            return
50 51

        if not self.get_secure_cookie('zoe'):
52
            cookie_val = user.username
53
            self.set_secure_cookie('zoe', cookie_val)
54
        self.redirect(self.get_argument("next", self.reverse_url("home_user")))
55 56


57
class LogoutWeb(ZoeWebRequestHandler):
58 59 60 61 62
    """The logout web page."""

    def get(self):
        """Login page."""
        self.clear_cookie('zoe')
63
        self.redirect(self.get_argument("next", self.reverse_url("login")))
64 65


66
class HomeWeb(ZoeWebRequestHandler):
67
    """Handler class"""
68

69 70
    def get(self):
        """Home page with authentication."""
71
        if self.current_user is None:
72
            return
73

74
        filters = {
75
            "user_id": self.current_user.id,
76 77
            "limit": 5
        }
78
        last_executions = self.api_endpoint.execution_list(self.current_user, **filters)
79 80

        filters = {
81
            "user_id": self.current_user.id,
82 83
            "status": "running"
        }
84
        last_running_executions = self.api_endpoint.execution_list(self.current_user, **filters)
85 86

        filters = {
87
            "user_id": self.current_user.id,
88 89
            "status": "submitted"
        }
90
        last_running_executions += self.api_endpoint.execution_list(self.current_user, **filters)
91 92

        filters = {
93
            "user_id": self.current_user.id,
94
            "status": "queued"
95
        }
96
        last_running_executions += self.api_endpoint.execution_list(self.current_user, **filters)
97

98
        filters = {
99
            "user_id": self.current_user.id,
100
            "status": "starting"
101
        }
102
        last_running_executions += self.api_endpoint.execution_list(self.current_user, **filters)
103

104
        running_reservations = [e.total_reservations for e in last_running_executions if e.total_reservations is not None]
105 106
        total_memory = sum([r.memory.min for r in running_reservations])
        total_cores = sum([r.cores.min for r in running_reservations])
107

108 109 110 111 112 113 114 115 116 117 118 119 120 121
        if zoe_lib.config.get_conf().enable_cephfs_quotas:
            try:
                disk_quota = subprocess.check_output(['sudo', '/usr/bin/getfattr', '-n', 'ceph.quota.max_bytes', os.path.join(zoe_lib.config.get_conf().workspace_base_path, zoe_lib.config.get_conf().workspace_deployment_path, self.current_user.username)])
            except subprocess.CalledProcessError:
                disk_quota = -1
                disk_usage = -1
            else:
                disk_quota = int(disk_quota.decode('utf-8').split('=')[1].lstrip('"').strip().rstrip('"'))
                disk_usage = os.stat(os.path.join(zoe_lib.config.get_conf().workspace_base_path, zoe_lib.config.get_conf().workspace_deployment_path, self.current_user.username)).st_size

        else:
            disk_quota = -1
            disk_usage = -1

122
        template_vars = {
123 124 125
            "total_memory": total_memory,
            "total_cores": total_cores,
            'last_executions': sorted(last_executions, key=lambda e: e.id),
126 127 128
            'running_executions': sorted(last_running_executions, key=lambda e: e.id),
            'disk_quota': disk_quota,
            'disk_usage': disk_usage
129
        }
Daniele Venzano's avatar
Daniele Venzano committed
130
        self.render('home_user.jinja2', **template_vars)