Add Pythonizing wrappers for the Python-bindings.
[util-vserver.git] / python / libvserver.py
1 #!/usr/bin/python -tt
2
3 # $Id$
4 # Copyright (C) 2008 Daniel Hokka Zakrisson
5
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.
10
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.
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  02110-1301, USA.
19
20 # vim:set ts=4 sw=4 expandtab:
21
22 import _libvserver
23
24 class struct:
25     _default_ = 0
26     def __init__(self, *args, **kwargs):
27         l = len(args)
28         if l > len(self._fields_):
29             raise KeyError, "%s has only %d fields" % (self.__class__,
30                                                        len(self._fields_))
31         for i in range(0, l):
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__,
36                                                                i)
37             self.__dict__[i] = kwargs[i]
38     def __totuple(self):
39         return tuple(self.__dict__.get(f, self._default_) for f in self._fields_)
40     def __iter__(self):
41         return self.__totuple().__iter__()
42     def __repr__(self):
43         return repr(self.__dict__)
44     def __addsub(self, other, negator):
45         for i in vars(other):
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)
53
54 get_vci = _libvserver.vc_get_vci
55
56 class struct_ctx_flags(struct):
57     _fields_ = ["flagword", "mask"]
58
59 def ctx_create(xid, flags=None):
60     if flags is None:
61         flags = (0, 0)
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)
67
68 class struct_ctx_stat(struct):
69     _fields_ = ["usecnt", "tasks"]
70 def ctx_stat(xid):
71     return struct_ctx_stat(*_libvserver.vc_ctx_stat(xid))
72
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"]
77 def virt_stat(xid):
78     return struct_virt_stat(*_libvserver.vc_virt_stat(xid))
79
80 ctx_kill = _libvserver.vc_ctx_kill
81 def get_cflags(xid):
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)
87
88 class struct_ctx_caps(struct):
89     _fields_ = ["bcaps", "bmask", "ccaps", "cmask"]
90 def get_ccaps(xid):
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)
96
97 class struct_vx_info(struct):
98     _fields_ = ["xid", "initpid"]
99 def get_vx_info(xid):
100     return struct_vx_info(*_libvserver.vc_get_vx_info(xid))
101
102 get_task_xid = _libvserver.vc_get_task_xid
103 wait_exit = _libvserver.vc_wait_exit
104
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))
109
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)
119
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))
124
125 reset_minmax = _libvserver.vc_reset_minmax
126
127 get_task_nid = _libvserver.vc_get_task_nid
128 net_create = _libvserver.vc_net_create
129 net_migrate = _libvserver.vc_net_migrate
130
131 class struct_net_addr(struct):
132     _fields_ = ["vna_type", "vna_flags", "vna_prefix", "vna_parent", "ip1",
133                 "ip2", "mask"]
134
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)
139
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)
144
145 class struct_net_flags(struct):
146     _fields_ = ["flagword", "mask"]
147 def get_nflags(nid):
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)
153
154 class struct_net_caps(struct):
155     _fields_ = ["ncaps", "cmask"]
156 def get_ncaps(nid):
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)
162
163 def _vc_set_iattr(f, obj, tag, flags, mask):
164     if tag is None:
165         tag = 0
166     else:
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)
175
176 def vhi_type(type):
177     if isinstance(type, int):
178         return type
179     else:
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))
185
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
190
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",
197                 "reserved"]
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))
205
206 get_task_tag = _libvserver.vc_get_task_tag
207 tag_create = _libvserver.vc_tag_create
208 tag_migrate = _libvserver.vc_tag_migrate
209
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__:
216             self.set_mask = 0
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))
227
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):
232     if cpu_id == -1:
233         return None
234     else:
235         return struct_sched_info(*_libvserver.vc_sched_info(xid, cpu_id,
236                                                                bucket_id))
237
238 set_mapping = _libvserver.vc_set_mapping
239 unset_mapping = _libvserver.vc_unset_mapping
240
241 get_badness = _libvserver.vc_get_badness
242 set_badness = _libvserver.vc_set_badness
243
244 get_insecurebcaps = _libvserver.vc_get_insecurebcaps
245 get_insecureccaps = _libvserver.vc_get_insecureccaps
246
247 isSupported = _libvserver.vc_isSupported
248 isSupportedString = _libvserver.vc_isSupportedString
249
250 getXIDType = _libvserver.vc_getXIDType
251
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)
258
259 # XXX: bcap, ccap, cflag, nflag, ncap could all use the same code here.
260 def text2bcap(text):
261     ret = _libvserver.vc_text2bcap(text)
262     if ret == 0:
263         raise ValueError, "%s is not a valid bcap" % text
264     return ret
265 lobcap2text = _libvserver.vc_lobcap2text
266 def bcap2list(bcaps):
267     list = []
268     while True:
269         bcaps, text = _libvserver.vc_lobcap2text(bcaps)
270         if text is None:
271             break
272         list.append(text)
273     return ",".join(list)
274 def list2bcap(list):
275     bcaps, bmask = _libvserver.vc_list2bcap(list)
276     return struct_ctx_caps(bcaps=bcaps, bmask=bmask)
277
278 def text2ccap(text):
279     ret = _libvserver.vc_text2ccap(text)
280     if ret == 0:
281         raise ValueError, "%s is not a valid ccap" % text
282     return ret
283 loccap2text = _libvserver.vc_loccap2text
284 def ccap2list(ccaps):
285     list = []
286     while True:
287         ccaps, text = _libvserver.vc_loccap2text(ccaps)
288         if text is None:
289             break
290         list.append(text)
291     return ",".join(list)
292 def list2ccap(list):
293     ccaps, cmask = _libvserver.vc_list2ccap(list)
294     return struct_ctx_caps(ccaps=ccaps, cmask=cmask)
295
296 def text2cflag(text):
297     ret = _libvserver.vc_text2cflag(text)
298     if ret == 0:
299         raise ValueError, "%s is not a valid cflag" % text
300     return ret
301 locflag2text = _libvserver.vc_locflag2text
302 def cflag2list(cflags):
303     list = []
304     while True:
305         cflags, text = _libvserver.vc_locflag2text(cflags)
306         if text is None:
307             break
308         list.append(text)
309     return ",".join(list)
310 def list2cflag(list):
311     return struct_ctx_flags(*_libvserver.vc_list2cflag(list))
312
313 def text2nflag(text):
314     ret = _libvserver.vc_text2nflag(text)
315     if ret == 0:
316         raise ValueError, "%s is not a valid nflag" % text
317     return ret
318 lonflag2text = _libvserver.vc_lonflag2text
319 def nflag2list(nflags):
320     list = []
321     while True:
322         nflags, text = _libvserver.vc_lonflag2text(nflags)
323         if text is None:
324             break
325         list.append(text)
326     return ",".join(list)
327 def list2nflag(list):
328     return struct_net_flags(*_libvserver.vc_list2nflag(list))
329
330 def text2ncap(text):
331     ret = _libvserver.vc_text2ncap(text)
332     if ret == 0:
333         raise ValueError, "%s is not a valid ncap" % text
334     return ret
335 loncap2text = _libvserver.vc_loncap2text
336 def ncap2list(ncaps):
337     list = []
338     while True:
339         ncaps, text = _libvserver.vc_loncap2text(ncaps)
340         if text is None:
341             break
342         list.append(text)
343     return ",".join(list)
344 def list2ncap(list):
345     return struct_net_caps(*_libvserver.vc_list2ncap(list))
346