OpenSecurity/bin/opensecurityd.py
author om
Tue, 10 Dec 2013 12:16:11 +0100
changeset 30 0d5637405430
parent 27 9732d799391f
child 31 d95fe93d7a83
permissions -rw-r--r--
extended opensecurityd and added some test scripts
     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/(.*)/ip',           'os_sdvm_ip',           # http://localhost:8080/sdvms/[VMNAME]/ip       GET
    59     '/sdvms/(.*)/start',        'os_sdvm_start',        # http://localhost:8080/sdvms/[VMNAME]/start    GET
    60     '/sdvms/(.*)/stop',         'os_sdvm_stop',         # http://localhost:8080/sdvms/[VMNAME]/stop     GET
    61     '/sdvms/(.*)',              'os_sdvm',              # http://localhost:8080/sdvms/[VMNAME]          GET, DELETE
    62     '/vms',                     'os_vms',               # http://localhost:8080/vms                     GET
    63     '/vms/(.*)',                'os_vm',                # http://localhost:8080/vms/[VMNAME]            GET
    64     '/',                        'os_root'               # http://localhost:8080/                        GET
    65 )
    66 
    67 
    68 # ------------------------------------------------------------
    69 # vars
    70 
    71 # Global VMManager instance
    72 gvm_mgr = VMManager()
    73 
    74 
    75 # ------------------------------------------------------------
    76 # code
    77 
    78 
    79 class os_device_change:
    80     """OpenSecurity '/device_change' handler"""
    81     
    82     def GET(self):
    83         gvm_mgr.handleDeviceChange()
    84         return "os_device_change"
    85 
    86 class os_sdvm_started:
    87     """OpenSecurity '/sdvm_started' handler"""
    88     
    89     def GET(self):
    90         # self.request get address
    91         return "os_sdvm_started"
    92 
    93 class os_sdvm:
    94     """OpenSecurity '/sdvms/[VM]' handler"""
    95     
    96     def GET(self, name):
    97         return gvm_mgr.getVMInfo(name)
    98 
    99     def DELETE(self, name):
   100         return gvm_mgr.removeVM(name)
   101             
   102 
   103 class os_sdvm_ip:
   104     """OpenSecurity '/sdvms/[VM]/ip' handler"""
   105     
   106     def GET(self, name):
   107         return gvm_mgr.getHostOnlyIP(name)
   108             
   109 
   110 class os_sdvm_start:
   111     """OpenSecurity '/sdvms/[VM]/start' handler"""
   112     
   113     def GET(self, name):
   114         return gvm_mgr.startVM(name)
   115             
   116 
   117 class os_sdvm_stop:
   118     """OpenSecurity '/sdvms/[VM]/stop' handler"""
   119     
   120     def GET(self, name):
   121         return gvm_mgr.stopVM(name)
   122             
   123 
   124 class os_sdvms:
   125     """OpenSecurity '/sdvms' handler"""
   126     
   127     def GET(self):
   128         """get the list of SDVMs"""
   129         return gvm_mgr.listSDVM() 
   130             
   131     def PUT(self):
   132         """create a new SDVM"""
   133 
   134         # get a new vm-name
   135         name = gvm_mgr.generateSDVMName()
   136         try:
   137             gvm_mgr.createVM(name)
   138         except:
   139             raise web.internalerror()
   140             
   141         return name
   142             
   143 class os_vm:
   144     """OpenSecurity '/vms/[VM]' handler"""
   145     
   146     def GET(self, name):
   147         return gvm_mgr.getVMInfo(name)
   148             
   149 
   150 class os_vms:
   151     """OpenSecurity '/vms' handler"""
   152     
   153     def GET(self):
   154         return gvm_mgr.listVM() 
   155             
   156 
   157 class os_root:
   158     """OpenSecurity '/' handler"""
   159     
   160     def GET(self):
   161         res = "'os_server': { "
   162         res += "'version': '" + __version__ + "', "
   163         res += "'machine_folder': '" + gvm_mgr.getDefaultMachineFolder() + "' "
   164         res += "}"
   165         return res
   166 
   167 
   168 # start
   169 if __name__ == "__main__":
   170     server = web.application(opensecurity_urls, globals())
   171     server.run()
   172