OpenSecurity/bin/opensecurityd.py
author om
Fri, 06 Dec 2013 14:24:42 +0100
changeset 22 ff138e89aa4d
parent 18 d7d7b8dee78e
child 27 9732d799391f
permissions -rw-r--r--
changed some tests and fixed vmmanger path lookup
om@13
     1
#!/bin/env python
om@13
     2
# -*- coding: utf-8 -*-
om@13
     3
om@13
     4
# ------------------------------------------------------------
om@13
     5
# opensecurityd
om@13
     6
# 
om@13
     7
# the opensecurityd as RESTful server
om@13
     8
#
om@13
     9
# Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
om@13
    10
#
om@13
    11
# Copyright (C) 2013 AIT Austrian Institute of Technology
om@13
    12
# AIT Austrian Institute of Technology GmbH
om@13
    13
# Donau-City-Strasse 1 | 1220 Vienna | Austria
om@13
    14
# http://www.ait.ac.at
om@13
    15
#
om@13
    16
# This program is free software; you can redistribute it and/or
om@13
    17
# modify it under the terms of the GNU General Public License
om@13
    18
# as published by the Free Software Foundation version 2.
om@13
    19
# 
om@13
    20
# This program is distributed in the hope that it will be useful,
om@13
    21
# but WITHOUT ANY WARRANTY; without even the implied warranty of
om@13
    22
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
om@13
    23
# GNU General Public License for more details.
om@13
    24
# 
om@13
    25
# You should have received a copy of the GNU General Public License
om@13
    26
# along with this program; if not, write to the Free Software
om@13
    27
# Foundation, Inc., 51 Franklin Street, Fifth Floor, 
om@13
    28
# Boston, MA  02110-1301, USA.
om@13
    29
# ------------------------------------------------------------
om@13
    30
om@13
    31
om@13
    32
# ------------------------------------------------------------
om@13
    33
# imports
om@13
    34
om@13
    35
import os
om@13
    36
import os.path
om@13
    37
import subprocess
om@13
    38
import sys
om@13
    39
import web
om@22
    40
om@22
    41
from vmmanager import VMManager
om@13
    42
om@13
    43
# local
om@13
    44
from environment import Environment
om@13
    45
om@13
    46
om@13
    47
# ------------------------------------------------------------
om@13
    48
# const
om@13
    49
om@13
    50
__version__ = "0.1"
om@13
    51
om@13
    52
om@13
    53
"""All the URLs we know mapping to class handler"""
om@13
    54
opensecurity_urls = (
om@17
    55
    '/device_change',           'os_device_change',     # http://localhost:8080/device_change   GET
om@22
    56
    '/sdvms',                   'os_sdvms',             # http://localhost:8080/sdvms           GET, PUT
om@22
    57
    '/sdvms/(.*)',              'os_sdvm',              # http://localhost:8080/sdvms           GET, DELETE
om@17
    58
    '/vms',                     'os_vms',               # http://localhost:8080/vms             GET
om@17
    59
    '/vms/(.*)',                'os_vm',                # http://localhost:8080/vms/[VMNAME]    GET
om@17
    60
    '/',                        'os_root'               # http://localhost:8080/                GET
om@13
    61
)
om@13
    62
om@13
    63
om@13
    64
# ------------------------------------------------------------
om@13
    65
# vars
om@13
    66
om@13
    67
# Global VMManager instance
om@13
    68
gvm_mgr = VMManager()
om@13
    69
om@13
    70
om@13
    71
# ------------------------------------------------------------
om@13
    72
# code
om@13
    73
om@13
    74
om@13
    75
class os_device_change:
om@13
    76
    """OpenSecurity '/device_change' handler"""
om@13
    77
    
om@13
    78
    def GET(self):
om@17
    79
        gvm_mgr.handleDeviceChange()
om@13
    80
        return "os_device_change"
om@13
    81
om@13
    82
om@22
    83
class os_sdvm:
om@22
    84
    """OpenSecurity '/sdvms/[VM]' handler"""
om@22
    85
    
om@22
    86
    def GET(self, name):
om@22
    87
        return gvm_mgr.getVMInfo(name)
om@22
    88
            
om@22
    89
om@22
    90
    def DELETE(self, name):
om@22
    91
        return gvm_mgr.removeVM(name)
om@22
    92
            
om@22
    93
om@13
    94
class os_sdvms:
om@13
    95
    """OpenSecurity '/sdvms' handler"""
om@13
    96
    
om@13
    97
    def GET(self):
om@17
    98
        """get the list of SDVMs"""
om@13
    99
        return gvm_mgr.listSDVM() 
om@13
   100
            
om@17
   101
    def PUT(self):
om@17
   102
        """create a new SDVM"""
om@22
   103
om@17
   104
        # pick the vm-name
om@17
   105
        args = web.input()
om@17
   106
        if not "name" in args:
om@17
   107
            raise web.badrequest()
om@17
   108
            
om@22
   109
        return gvm_mgr.createVM(args.name)
om@22
   110
            
om@13
   111
class os_vm:
om@17
   112
    """OpenSecurity '/vms/[VM]' handler"""
om@13
   113
    
om@13
   114
    def GET(self, name):
om@13
   115
        return gvm_mgr.getVMInfo(name)
om@13
   116
            
om@13
   117
om@13
   118
class os_vms:
om@13
   119
    """OpenSecurity '/vms' handler"""
om@13
   120
    
om@13
   121
    def GET(self):
om@13
   122
        return gvm_mgr.listVM() 
om@13
   123
            
om@13
   124
om@13
   125
class os_root:
om@13
   126
    """OpenSecurity '/' handler"""
om@13
   127
    
om@13
   128
    def GET(self):
om@13
   129
        res = "'os_server': { "
om@13
   130
        res += "'version': '" + __version__ + "', "
om@13
   131
        res += "'machine_folder': '" + gvm_mgr.getDefaultMachineFolder() + "' "
om@13
   132
        res += "}"
om@13
   133
        return res
om@13
   134
om@13
   135
om@13
   136
# start
om@13
   137
if __name__ == "__main__":
om@13
   138
    server = web.application(opensecurity_urls, globals())
om@13
   139
    server.run()
om@13
   140