4 # Copyright (C) 2008 Daniel Hokka Zakrisson
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
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 02110-1301, USA.
20 # vim:set ts=4 sw=4 expandtab:
26 def __init__(self, *args, **kwargs):
28 if l > len(self._fields_):
29 raise KeyError, "%s has only %d fields" % (self.__class__,
32 self.__dict__[self._fields_[i]] = args[i]
33 for i in kwargs.iterkeys():
34 if i not in self._fields_:
35 raise KeyError, "%s has no such field '%s'" % (self.__class__,
37 self.__dict__[i] = kwargs[i]
39 return tuple(self.__dict__.get(f, self._default_) for f in self._fields_)
41 return self.__totuple().__iter__()
43 return repr(self.__dict__)
44 def __addsub(self, other, negator):
46 if i in self._fields_:
47 self.__dict__[i] = (self.__dict__.get(i, self._default_) +
48 (negator * getattr(other, i)))
49 def __add__(self, other):
50 self.__addsub(other, 1)
51 def __sub__(self, other):
52 self.__addsub(other, -1)
54 get_vci = _libvserver.vc_get_vci
56 class struct_ctx_flags(struct):
57 _fields_ = ["flagword", "mask"]
59 def ctx_create(xid, flags=None):
62 elif not isinstance(flags, struct_ctx_flags):
63 raise TypeError, "flags must be of type struct_ctx_flags"
64 return _libvserver.vc_ctx_create(xid, *flags)
65 def ctx_migrate(xid, flags=0L):
66 return _libvserver.vc_ctx_migrate(xid, flags)
68 class struct_ctx_stat(struct):
69 _fields_ = ["usecnt", "tasks"]
71 return struct_ctx_stat(*_libvserver.vc_ctx_stat(xid))
73 class struct_virt_stat(struct):
74 _fields_ = ["offset", "uptime", "nr_threads", "nr_running",
75 "nr_uninterruptible", "nr_onhold", "nr_forks",
76 "load0", "load1", "load2"]
78 return struct_virt_stat(*_libvserver.vc_virt_stat(xid))
80 ctx_kill = _libvserver.vc_ctx_kill
82 return struct_ctx_flags(*_libvserver.vc_get_cflags(xid))
83 def set_cflags(xid, flags):
84 if not isinstance(flags, struct_ctx_flags):
85 raise TypeError, "flags must be of type struct_ctx_flags"
86 _libvserver.vc_set_cflags(xid, *flags)
88 class struct_ctx_caps(struct):
89 _fields_ = ["bcaps", "bmask", "ccaps", "cmask"]
91 return struct_ctx_caps(*_libvserver.vc_get_ccaps(xid))
92 def set_ccaps(xid, caps):
93 if not isinstance(caps, struct_ctx_caps):
94 raise TypeError, "caps must be of type struct_ctx_caps"
95 _libvserver.vc_set_ccaps(xid, *caps)
97 class struct_vx_info(struct):
98 _fields_ = ["xid", "initpid"]
100 return struct_vx_info(*_libvserver.vc_get_vx_info(xid))
102 get_task_xid = _libvserver.vc_get_task_xid
103 wait_exit = _libvserver.vc_wait_exit
105 class struct_rlimit_mask(struct):
106 _fields_ = ["min", "soft", "hard"]
107 def get_rlimit_mask(xid):
108 return struct_rlimit_mask(*_libvserver.vc_get_rlimit_mask(xid))
110 class struct_rlimit(struct):
111 _fields_ = ["min", "soft", "hard"]
112 _default_ = _libvserver.VC_LIM_KEEP
113 def get_rlimit(xid, resource):
114 return struct_rlimit(*_libvserver.vc_get_rlimit(xid, resource))
115 def set_rlimit(xid, resource, limit):
116 if not isinstance(limit, struct_rlimit):
117 raise TypeError, "limit must be of type struct_rlimit"
118 _libvserver.vc_set_rlimit(xid, resource, *limit)
120 class stuct_rlimit_stat(struct):
121 _fields_ = ["hits", "value", "minimum", "maximum"]
122 def rlimit_stat(xid, resource):
123 return struct_rlimit_stat(*_libvserver.vc_rlimit_stat(xid, resource))
125 reset_minmax = _libvserver.vc_reset_minmax
127 get_task_nid = _libvserver.vc_get_task_nid
128 net_create = _libvserver.vc_net_create
129 net_migrate = _libvserver.vc_net_migrate
131 class struct_net_addr(struct):
132 _fields_ = ["vna_type", "vna_flags", "vna_prefix", "vna_parent", "ip1",
135 def net_add(nid, addr):
136 if not isinstance(addr, struct_net_addr):
137 raise TypeError, "addr must be of type struct_net_addr"
138 _libvserver.vc_net_add(nid, *addr)
140 def net_remove(nid, addr):
141 if not isinstance(addr, struct_net_addr):
142 raise TypeError, "addr must be of type struct_net_addr"
143 _libvserver.vc_net_remove(nid, *addr)
145 class struct_net_flags(struct):
146 _fields_ = ["flagword", "mask"]
148 return struct_net_flags(*_libvserver.vc_get_nflags(nid))
149 def set_nflags(nid, flags):
150 if not isinstance(flags, struct_net_flags):
151 raise TypeError, "flags must be of type struct_net_flags"
152 _libvserver.vc_set_nflags(nid, *flags)
154 class struct_net_caps(struct):
155 _fields_ = ["ncaps", "cmask"]
157 return struct_net_caps(*_libvserver.vc_get_ncaps(nid))
158 def set_ncaps(nid, caps):
159 if not isinstance(caps, struct_net_caps):
160 raise TypeError, "caps must be of type struct_net_caps"
161 _libvserver.vc_set_ncaps(nid, *caps)
163 def _vc_set_iattr(f, obj, tag, flags, mask):
167 mask |= _libvserver.VC_IATTR_XID
168 f(obj, tag, flags, mask)
169 def set_iattr(filename, tag=None, flags=0, mask=0):
170 _vc_set_iattr(_libvserver.vc_set_iattr, filename, tag, flags, mask)
171 def fset_iattr(fd, tag=None, flags=0, mask=0):
172 _vc_set_iattr(_libvserver.vc_fset_iattr, fd, tag, flags, mask)
173 get_iattr = lambda f: _libvserver.vc_get_iattr(f, -1)
174 fget_iattr = lambda f: _libvserver.vc_fget_iattr(f, -1)
177 if isinstance(type, int):
180 return _libvserver.__dict__["vcVHI_" + repr(type)]
181 def set_vhi_name(xid, type, val):
182 _libvserver.vc_set_vhi_name(xid, vhi_type(type), val)
183 def get_vhi_name(xid, type):
184 return _libvserver.vc_get_vhi_name(xid, vhi_type(type))
186 enter_namespace = _libvserver.vc_enter_namespace
187 set_namespace = _libvserver.vc_set_namespace
188 get_space_mask = _libvserver.vc_get_space_mask
189 get_space_default = _libvserver.vc_get_space_default
191 def add_dlimit(path, tag, flags=0):
192 _libvserver.vc_add_dlimit(path, tag, flags)
193 def rem_dlimit(path, tag, flags=0):
194 _libvserver.vc_rem_dlimit(path, tag, flags)
195 class struct_ctx_dlimit(struct):
196 _fields_ = ["space_used", "space_total", "inodes_used", "inodes_total",
198 _default_ = _libvserver.VC_CDLIM_KEEP
199 def set_dlimit(path, tag, limit, flags=0):
200 if not isinstance(limit, struct_ctx_dlimit):
201 raise TypeError, "limit must be of type struct_ctx_dlimit"
202 _libvserver.vc_set_dlimit(path, tag, flags, *limit)
203 def get_dlimit(path, tag, flags=0):
204 return struct_ctx_dlimit(*_libvserver.vc_get_dlimit(path, tag, flags))
206 get_task_tag = _libvserver.vc_get_task_tag
207 tag_create = _libvserver.vc_tag_create
208 tag_migrate = _libvserver.vc_tag_migrate
210 class struct_set_sched(struct):
211 _fields_ = ["set_mask", "fill_rate", "interval", "fill_rate2", "interval2",
212 "tokens", "tokens_min", "tokens_max", "priority_bias",
213 "cpu_id", "bucket_id"]
214 def fill_set_mask(self):
215 if "set_mask" not in self.__dict__:
217 for field in self.__dict__:
218 f = field.replace("priority", "prio").upper()
219 self.set_mask |= _libvserver.__dict__.get("VC_VXSM_" + f, 0)
220 def set_sched(xid, sched):
221 if not isinstance(sched, struct_set_sched):
222 raise TypeError, "sched must be of type struct_set_sched"
223 sched.fill_set_mask()
224 _libvserver.vc_set_sched(xid, *sched)
225 def get_sched(xid, cpu_id=0, bucket_id=0):
226 return struct_set_sched(*_libvserver.vc_get_sched(xid, cpu_id, bucket_id))
228 class struct_sched_info(struct):
229 _fields_ = ["cpu_id", "bucket_id", "user_msec", "sys_msec", "hold_msec",
230 "token_usec", "vavavoom"]
231 def sched_info(xid, cpu_id=-1, bucket_id=0):
235 return struct_sched_info(*_libvserver.vc_sched_info(xid, cpu_id,
238 set_mapping = _libvserver.vc_set_mapping
239 unset_mapping = _libvserver.vc_unset_mapping
241 get_badness = _libvserver.vc_get_badness
242 set_badness = _libvserver.vc_set_badness
244 get_insecurebcaps = _libvserver.vc_get_insecurebcaps
245 get_insecureccaps = _libvserver.vc_get_insecureccaps
247 isSupported = _libvserver.vc_isSupported
248 isSupportedString = _libvserver.vc_isSupportedString
250 getXIDType = _libvserver.vc_getXIDType
252 def xidopt2xid(opt, honor_static=True):
253 return _libvserver.vc_xidopt2xid(opt, honor_static)
254 def nidopt2nid(opt, honor_static=True):
255 return _libvserver.vc_nidopt2nid(opt, honor_static)
256 def tagopt2tag(opt, honor_static=True):
257 return _libvserver.vc_tagopt2tag(opt, honor_static)
259 # XXX: bcap, ccap, cflag, nflag, ncap could all use the same code here.
261 ret = _libvserver.vc_text2bcap(text)
263 raise ValueError, "%s is not a valid bcap" % text
265 lobcap2text = _libvserver.vc_lobcap2text
266 def bcap2list(bcaps):
269 bcaps, text = _libvserver.vc_lobcap2text(bcaps)
273 return ",".join(list)
275 bcaps, bmask = _libvserver.vc_list2bcap(list)
276 return struct_ctx_caps(bcaps=bcaps, bmask=bmask)
279 ret = _libvserver.vc_text2ccap(text)
281 raise ValueError, "%s is not a valid ccap" % text
283 loccap2text = _libvserver.vc_loccap2text
284 def ccap2list(ccaps):
287 ccaps, text = _libvserver.vc_loccap2text(ccaps)
291 return ",".join(list)
293 ccaps, cmask = _libvserver.vc_list2ccap(list)
294 return struct_ctx_caps(ccaps=ccaps, cmask=cmask)
296 def text2cflag(text):
297 ret = _libvserver.vc_text2cflag(text)
299 raise ValueError, "%s is not a valid cflag" % text
301 locflag2text = _libvserver.vc_locflag2text
302 def cflag2list(cflags):
305 cflags, text = _libvserver.vc_locflag2text(cflags)
309 return ",".join(list)
310 def list2cflag(list):
311 return struct_ctx_flags(*_libvserver.vc_list2cflag(list))
313 def text2nflag(text):
314 ret = _libvserver.vc_text2nflag(text)
316 raise ValueError, "%s is not a valid nflag" % text
318 lonflag2text = _libvserver.vc_lonflag2text
319 def nflag2list(nflags):
322 nflags, text = _libvserver.vc_lonflag2text(nflags)
326 return ",".join(list)
327 def list2nflag(list):
328 return struct_net_flags(*_libvserver.vc_list2nflag(list))
331 ret = _libvserver.vc_text2ncap(text)
333 raise ValueError, "%s is not a valid ncap" % text
335 loncap2text = _libvserver.vc_loncap2text
336 def ncap2list(ncaps):
339 ncaps, text = _libvserver.vc_loncap2text(ncaps)
343 return ",".join(list)
345 return struct_net_caps(*_libvserver.vc_list2ncap(list))