/
vboxweb.py
executable file
·146 lines (127 loc) · 4.73 KB
/
vboxweb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python
# ***** BEGIN LICENSE BLOCK *****
#
# The MIT License
#
# Copyright (c) 2009 Josh Wright
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ***** END LICENSE BLOCK *****
import os, sys, pickle
USAGE = """
VBoxWeb - A web-based frontend for Sun's VirtualBox
Usage:
-h, --help
Print this usage list and exit
-p, --port [port number]
Set the port number VBoxWeb should listen on
--vbox-path [path]
The path to VBoxPython.so (i.e. /usr/lib/virtualbox/)
-s, --save-settings
Save any settings modified with command line arguments
"""
try:
import cherrypy
major_version = int(cherrypy.__version__[0])
if major_version < 3:
raise ImportError
except ImportError:
print """
VBoxWeb requires CherryPy (version 3.0 or higher).
You can download the latest version of CherryPy
from http://www.cherrypy.org/
"""
sys.exit()
from content import Root, VM, HardDisk
DEFAULT_SETTINGS = {'username': 'vboxweb', 'password': 'vboxweb', 'port': 8080, 'vbox_python_path': '/usr/lib/virtualbox'}
def main(argv):
if os.path.exists('config.pkl'):
f = open('config.pkl', 'r')
vboxweb_config = pickle.load(f)
f.close()
for key in DEFAULT_SETTINGS:
if key not in vboxweb_config:
vboxweb_config[key] = DEFAULT_SETTINGS[key]
f = open('config.pkl', 'w')
pickle.dump(vboxweb_config, f, 1)
f.close()
else:
vboxweb_config = DEFAULT_SETTINGS
f = open('config.pkl', 'w')
pickle.dump(vboxweb_config, f, 1)
f.close()
port = vboxweb_config['port']
vbox_python_path = vboxweb_config['vbox_python_path']
save_settings = False
if len(argv) > 1:
i = iter(argv)
executable = i.next()
for arg in i:
if arg in ('-p', '--port'):
port = i.next()
elif arg == '--vbox-path':
vbox_python_path = i.next()
elif arg in ('-s', '--save-settings'):
save_settings = True
elif arg in ('-h', '--help'):
print USAGE
sys.exit(0)
else:
print "\nUnknown command: %s" % arg
print USAGE
sys.exit(1)
if save_settings:
vboxweb_config['port'] = port
vboxweb_config['vbox_python_path'] = vbox_python_path
f = open('config.pkl', 'w')
pickle.dump(vboxweb_config, f, 1)
f.close()
sys.path.append(vbox_python_path)
import xpcom.vboxxpcom
import xpcom
import xpcom.components
vbox = xpcom.components.classes["@virtualbox.org/VirtualBox;1"].createInstance()
class LocalManager:
def getSessionObject(self, vbox):
return xpcom.components.classes["@virtualbox.org/Session;1"].createInstance()
cherrypy.config.update({
'server.socket_port': port,
'cherrypy.server.socket_host': '0.0.0.0',
'tools.encode.on': True, 'tools.encode.encoding': 'utf-8',
'tools.decode.on': True,
'tools.trailing_slash.on': True,
'tools.staticdir.root': os.path.abspath(os.path.dirname(__file__)),
})
root = Root(LocalManager(), vbox)
root.vm = VM(LocalManager(), vbox)
root.hard_disk = HardDisk(LocalManager(), vbox)
cherrypy.quickstart(root, '/', {
'/': {
'tools.digest_auth.on': True,
'tools.digest_auth.realm': 'Some site',
'tools.digest_auth.users': {vboxweb_config['username']: vboxweb_config['password']}
},
'/media': {
'tools.staticdir.on': True,
'tools.staticdir.dir': 'media'
}
})
if __name__ == '__main__':
main(sys.argv)