root / lib / hypervisor / FakeHypervisor.py @ 65a6f9b7
History | View | Annotate | Download (6.1 kB)
1 |
#
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2006, 2007, 2008 Google Inc.
|
5 |
#
|
6 |
# This program is free software; you can redistribute it and/or modify
|
7 |
# it under the terms of the GNU General Public License as published by
|
8 |
# the Free Software Foundation; either version 2 of the License, or
|
9 |
# (at your option) any later version.
|
10 |
#
|
11 |
# This program is distributed in the hope that it will be useful, but
|
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
# General Public License for more details.
|
15 |
#
|
16 |
# You should have received a copy of the GNU General Public License
|
17 |
# along with this program; if not, write to the Free Software
|
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
# 02110-1301, USA.
|
20 |
|
21 |
|
22 |
"""Fake hypervisor
|
23 |
|
24 |
"""
|
25 |
|
26 |
import os |
27 |
import os.path |
28 |
|
29 |
from ganeti import utils |
30 |
from ganeti import constants |
31 |
from ganeti import errors |
32 |
from ganeti.hypervisor import BaseHypervisor |
33 |
|
34 |
|
35 |
class FakeHypervisor(BaseHypervisor.BaseHypervisor): |
36 |
"""Fake hypervisor interface.
|
37 |
|
38 |
This can be used for testing the ganeti code without having to have
|
39 |
a real virtualisation software installed.
|
40 |
|
41 |
"""
|
42 |
_ROOT_DIR = constants.RUN_DIR + "/ganeti-fake-hypervisor"
|
43 |
|
44 |
def __init__(self): |
45 |
BaseHypervisor.__init__(self)
|
46 |
if not os.path.exists(self._ROOT_DIR): |
47 |
os.mkdir(self._ROOT_DIR)
|
48 |
|
49 |
def ListInstances(self): |
50 |
"""Get the list of running instances.
|
51 |
|
52 |
"""
|
53 |
return os.listdir(self._ROOT_DIR) |
54 |
|
55 |
def GetInstanceInfo(self, instance_name): |
56 |
"""Get instance properties.
|
57 |
|
58 |
Args:
|
59 |
instance_name: the instance name
|
60 |
|
61 |
Returns:
|
62 |
(name, id, memory, vcpus, stat, times)
|
63 |
"""
|
64 |
file_name = "%s/%s" % (self._ROOT_DIR, instance_name) |
65 |
if not os.path.exists(file_name): |
66 |
return None |
67 |
try:
|
68 |
fh = file(file_name, "r") |
69 |
try:
|
70 |
inst_id = fh.readline().strip() |
71 |
memory = fh.readline().strip() |
72 |
vcpus = fh.readline().strip() |
73 |
stat = "---b-"
|
74 |
times = "0"
|
75 |
return (instance_name, inst_id, memory, vcpus, stat, times)
|
76 |
finally:
|
77 |
fh.close() |
78 |
except IOError, err: |
79 |
raise errors.HypervisorError("Failed to list instance %s: %s" % |
80 |
(instance_name, err)) |
81 |
|
82 |
def GetAllInstancesInfo(self): |
83 |
"""Get properties of all instances.
|
84 |
|
85 |
Returns:
|
86 |
[(name, id, memory, vcpus, stat, times),...]
|
87 |
"""
|
88 |
data = [] |
89 |
for file_name in os.listdir(self._ROOT_DIR): |
90 |
try:
|
91 |
fh = file(self._ROOT_DIR+"/"+file_name, "r") |
92 |
inst_id = "-1"
|
93 |
memory = "0"
|
94 |
stat = "-----"
|
95 |
times = "-1"
|
96 |
try:
|
97 |
inst_id = fh.readline().strip() |
98 |
memory = fh.readline().strip() |
99 |
vcpus = fh.readline().strip() |
100 |
stat = "---b-"
|
101 |
times = "0"
|
102 |
finally:
|
103 |
fh.close() |
104 |
data.append((file_name, inst_id, memory, vcpus, stat, times)) |
105 |
except IOError, err: |
106 |
raise errors.HypervisorError("Failed to list instances: %s" % err) |
107 |
return data
|
108 |
|
109 |
def StartInstance(self, instance, force, extra_args): |
110 |
"""Start an instance.
|
111 |
|
112 |
For the fake hypervisor, it just creates a file in the base dir,
|
113 |
creating an exception if it already exists. We don't actually
|
114 |
handle race conditions properly, since these are *FAKE* instances.
|
115 |
|
116 |
"""
|
117 |
file_name = self._ROOT_DIR + "/%s" % instance.name |
118 |
if os.path.exists(file_name):
|
119 |
raise errors.HypervisorError("Failed to start instance %s: %s" % |
120 |
(instance.name, "already running"))
|
121 |
try:
|
122 |
fh = file(file_name, "w") |
123 |
try:
|
124 |
fh.write("0\n%d\n%d\n" % (instance.memory, instance.vcpus))
|
125 |
finally:
|
126 |
fh.close() |
127 |
except IOError, err: |
128 |
raise errors.HypervisorError("Failed to start instance %s: %s" % |
129 |
(instance.name, err)) |
130 |
|
131 |
def StopInstance(self, instance, force=False): |
132 |
"""Stop an instance.
|
133 |
|
134 |
For the fake hypervisor, this just removes the file in the base
|
135 |
dir, if it exist, otherwise we raise an exception.
|
136 |
|
137 |
"""
|
138 |
file_name = self._ROOT_DIR + "/%s" % instance.name |
139 |
if not os.path.exists(file_name): |
140 |
raise errors.HypervisorError("Failed to stop instance %s: %s" % |
141 |
(instance.name, "not running"))
|
142 |
utils.RemoveFile(file_name) |
143 |
|
144 |
def RebootInstance(self, instance): |
145 |
"""Reboot an instance.
|
146 |
|
147 |
For the fake hypervisor, this does nothing.
|
148 |
|
149 |
"""
|
150 |
return
|
151 |
|
152 |
def GetNodeInfo(self): |
153 |
"""Return information about the node.
|
154 |
|
155 |
The return value is a dict, which has to have the following items:
|
156 |
(all values in MiB)
|
157 |
- memory_total: the total memory size on the node
|
158 |
- memory_free: the available memory on the node for instances
|
159 |
- memory_dom0: the memory used by the node itself, if available
|
160 |
|
161 |
"""
|
162 |
# global ram usage from the xm info command
|
163 |
# memory : 3583
|
164 |
# free_memory : 747
|
165 |
# note: in xen 3, memory has changed to total_memory
|
166 |
try:
|
167 |
fh = file("/proc/meminfo") |
168 |
try:
|
169 |
data = fh.readlines() |
170 |
finally:
|
171 |
fh.close() |
172 |
except IOError, err: |
173 |
raise errors.HypervisorError("Failed to list node info: %s" % err) |
174 |
|
175 |
result = {} |
176 |
sum_free = 0
|
177 |
for line in data: |
178 |
splitfields = line.split(":", 1) |
179 |
|
180 |
if len(splitfields) > 1: |
181 |
key = splitfields[0].strip()
|
182 |
val = splitfields[1].strip()
|
183 |
if key == 'MemTotal': |
184 |
result['memory_total'] = int(val.split()[0])/1024 |
185 |
elif key in ('MemFree', 'Buffers', 'Cached'): |
186 |
sum_free += int(val.split()[0])/1024 |
187 |
elif key == 'Active': |
188 |
result['memory_dom0'] = int(val.split()[0])/1024 |
189 |
|
190 |
result['memory_free'] = sum_free
|
191 |
return result
|
192 |
|
193 |
@staticmethod
|
194 |
def GetShellCommandForConsole(instance): |
195 |
"""Return a command for connecting to the console of an instance.
|
196 |
|
197 |
"""
|
198 |
return "echo Console not available for fake hypervisor" |
199 |
|
200 |
def Verify(self): |
201 |
"""Verify the hypervisor.
|
202 |
|
203 |
For the fake hypervisor, it just checks the existence of the base
|
204 |
dir.
|
205 |
|
206 |
"""
|
207 |
if not os.path.exists(self._ROOT_DIR): |
208 |
return "The required directory '%s' does not exist." % self._ROOT_DIR |