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@13
|
40 |
from vmmanager.vmmanager import VMManager
|
om@13
|
41 |
|
om@13
|
42 |
# local
|
om@13
|
43 |
from environment import Environment
|
om@13
|
44 |
|
om@13
|
45 |
|
om@13
|
46 |
# ------------------------------------------------------------
|
om@13
|
47 |
# const
|
om@13
|
48 |
|
om@13
|
49 |
__version__ = "0.1"
|
om@13
|
50 |
|
om@13
|
51 |
|
om@13
|
52 |
"""All the URLs we know mapping to class handler"""
|
om@13
|
53 |
opensecurity_urls = (
|
om@17
|
54 |
'/device_change', 'os_device_change', # http://localhost:8080/device_change GET
|
om@17
|
55 |
'/sdvms', 'os_sdvms', # http://localhost:8080/sdvms GET, PUT, DELETE
|
om@17
|
56 |
'/vms', 'os_vms', # http://localhost:8080/vms GET
|
om@17
|
57 |
'/vms/(.*)', 'os_vm', # http://localhost:8080/vms/[VMNAME] GET
|
om@17
|
58 |
'/', 'os_root' # http://localhost:8080/ GET
|
om@13
|
59 |
)
|
om@13
|
60 |
|
om@13
|
61 |
|
om@13
|
62 |
# ------------------------------------------------------------
|
om@13
|
63 |
# vars
|
om@13
|
64 |
|
om@13
|
65 |
# Global VMManager instance
|
om@13
|
66 |
gvm_mgr = VMManager()
|
om@13
|
67 |
|
om@13
|
68 |
|
om@13
|
69 |
# ------------------------------------------------------------
|
om@13
|
70 |
# code
|
om@13
|
71 |
|
om@13
|
72 |
|
om@13
|
73 |
class os_device_change:
|
om@13
|
74 |
"""OpenSecurity '/device_change' handler"""
|
om@13
|
75 |
|
om@13
|
76 |
def GET(self):
|
om@17
|
77 |
gvm_mgr.handleDeviceChange()
|
om@13
|
78 |
return "os_device_change"
|
om@13
|
79 |
|
om@13
|
80 |
|
om@13
|
81 |
class os_sdvms:
|
om@13
|
82 |
"""OpenSecurity '/sdvms' handler"""
|
om@13
|
83 |
|
om@13
|
84 |
def GET(self):
|
om@17
|
85 |
"""get the list of SDVMs"""
|
om@13
|
86 |
return gvm_mgr.listSDVM()
|
om@13
|
87 |
|
om@17
|
88 |
def PUT(self):
|
om@17
|
89 |
"""create a new SDVM"""
|
om@17
|
90 |
print("os_sdvms:PUT - 1")
|
om@17
|
91 |
# pick the vm-name
|
om@17
|
92 |
args = web.input()
|
om@17
|
93 |
if not "name" in args:
|
om@17
|
94 |
raise web.badrequest()
|
om@17
|
95 |
print("os_sdvms:PUT - 2")
|
om@17
|
96 |
|
om@17
|
97 |
return gvm_mgr.createVM(args.name)
|
om@17
|
98 |
|
om@17
|
99 |
def DELETE(self):
|
om@17
|
100 |
"""remove a new SDVM"""
|
om@17
|
101 |
# pick the vm-name
|
om@17
|
102 |
args = web.input()
|
om@17
|
103 |
if not "name" in args:
|
om@17
|
104 |
raise web.badrequest()
|
om@17
|
105 |
|
om@17
|
106 |
return gvm_mgr.removeVM(args.name)
|
om@13
|
107 |
|
om@13
|
108 |
class os_vm:
|
om@17
|
109 |
"""OpenSecurity '/vms/[VM]' handler"""
|
om@13
|
110 |
|
om@13
|
111 |
def GET(self, name):
|
om@13
|
112 |
return gvm_mgr.getVMInfo(name)
|
om@13
|
113 |
|
om@13
|
114 |
|
om@13
|
115 |
class os_vms:
|
om@13
|
116 |
"""OpenSecurity '/vms' handler"""
|
om@13
|
117 |
|
om@13
|
118 |
def GET(self):
|
om@13
|
119 |
return gvm_mgr.listVM()
|
om@13
|
120 |
|
om@13
|
121 |
|
om@13
|
122 |
class os_root:
|
om@13
|
123 |
"""OpenSecurity '/' handler"""
|
om@13
|
124 |
|
om@13
|
125 |
def GET(self):
|
om@13
|
126 |
res = "'os_server': { "
|
om@13
|
127 |
res += "'version': '" + __version__ + "', "
|
om@13
|
128 |
res += "'machine_folder': '" + gvm_mgr.getDefaultMachineFolder() + "' "
|
om@13
|
129 |
res += "}"
|
om@13
|
130 |
return res
|
om@13
|
131 |
|
om@13
|
132 |
|
om@13
|
133 |
# start
|
om@13
|
134 |
if __name__ == "__main__":
|
om@13
|
135 |
server = web.application(opensecurity_urls, globals())
|
om@13
|
136 |
server.run()
|
om@13
|
137 |
|