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