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