start.py 3.5 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
from zoe_api.web.request_handler import ZoeWebRequestHandler
from zoe_api.auth.base import BaseAuthenticator
20

21

22
class RootWeb(ZoeWebRequestHandler):
23
    """Handler class"""
24

25 26
    def get(self):
        """Home page without authentication."""
27
        self.redirect("/user")
28 29


30
class LoginWeb(ZoeWebRequestHandler):
31 32 33 34
    """The login web page."""

    def get(self):
        """Login page."""
Daniele Venzano's avatar
Daniele Venzano committed
35
        self.render('login.jinja2')
36 37 38 39 40

    def post(self):
        """Try to authenticate."""
        username = self.get_argument("username", "")
        password = self.get_argument("password", "")
41
        user = BaseAuthenticator().full_auth(username, password)
42 43 44
        if user is None:
            self.redirect(self.get_argument("next", u"/login"))
            return
45 46

        if not self.get_secure_cookie('zoe'):
47
            cookie_val = user.username
48 49 50 51
            self.set_secure_cookie('zoe', cookie_val)
        self.redirect(self.get_argument("next", u"/user"))


52
class LogoutWeb(ZoeWebRequestHandler):
53 54 55 56 57 58 59 60
    """The logout web page."""

    def get(self):
        """Login page."""
        self.clear_cookie('zoe')
        self.redirect(self.get_argument("next", u"/login"))


61
class HomeWeb(ZoeWebRequestHandler):
62
    """Handler class"""
63

64 65
    def get(self):
        """Home page with authentication."""
66
        if self.current_user is None:
67
            return
68

Daniele Venzano's avatar
Daniele Venzano committed
69
        filters = {
70
            "user_id": self.current_user.id,
71 72
            "limit": 5
        }
73
        last_executions = self.api_endpoint.execution_list(self.current_user, **filters)
74 75

        filters = {
76
            "user_id": self.current_user.id,
77 78
            "status": "running"
        }
79
        last_running_executions = self.api_endpoint.execution_list(self.current_user, **filters)
80 81

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

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

93
        filters = {
94
            "user_id": self.current_user.id,
95
            "status": "starting"
Daniele Venzano's avatar
Daniele Venzano committed
96
        }
97
        last_running_executions += self.api_endpoint.execution_list(self.current_user, **filters)
98 99

        running_reservations = [e.total_reservations for e in last_running_executions]
100 101
        total_memory = sum([r.memory.min for r in running_reservations])
        total_cores = sum([r.cores.min for r in running_reservations])
102 103

        template_vars = {
104 105 106 107
            "total_memory": total_memory,
            "total_cores": total_cores,
            'last_executions': sorted(last_executions, key=lambda e: e.id),
            'running_executions': sorted(last_running_executions, key=lambda e: e.id)
108
        }
Daniele Venzano's avatar
Daniele Venzano committed
109
        self.render('home_user.jinja2', **template_vars)