+ # For every resource key listed in the limit table, add in a
+ # new method with which one can get/set the resource's hard,
+ # soft, minimum limits.
+ limits = {"CPU": RLIMIT_CPU,
+ "RSS": RLIMIT_RSS,
+ "NPROC": RLIMIT_NPROC,
+ "NOFILE": RLIMIT_NOFILE,
+ "MEMLOCK": RLIMIT_MEMLOCK,
+ "AS": RLIMIT_AS,
+ "LOCKS": RLIMIT_LOCKS,
+ "SIGPENDING": RLIMIT_SIGPENDING,
+ "MSGQUEUE": RLIMIT_MSGQUEUE,
+ "NSOCK": VLIMIT_NSOCK,
+ "OPENFD": VLIMIT_OPENFD,
+ "ANON": VLIMIT_ANON,
+ "SHMEM": VLIMIT_SHMEM}
+ for meth in limits.keys():
+ resource_type = limits[meth]
+ func = lambda \
+ hard=VC_LIM_KEEP,\
+ soft=VC_LIM_KEEP,\
+ minimum=VC_LIM_KEEP:\
+ self.__set_vserver_limit(resource_type,\
+ hard, \
+ soft,\
+ minimum)
+ self.__dict__["set_%s_limit"%meth] = func
+
+ func = lambda \
+ hard=VC_LIM_KEEP,\
+ soft=VC_LIM_KEEP,\
+ minimum=VC_LIM_KEEP:\
+ self.__set_vserver_config(meth, resource_type, \
+ hard, \
+ soft,\
+ minimum)
+ self.__dict__["set_%s_config"%meth] = func
+
+ func = lambda : self.__get_vserver_limit(resource_type)
+ self.__dict__["get_%s_limit"%meth] = func
+
+ func = lambda : self.__get_vserver_config(meth,resource_type)
+ self.__dict__["get_%s_config"%meth] = func
+
+ def __set_vserver_limit(self,resource_type,hard,soft,minimum):
+ """Generic set resource limit function for vserver"""
+ if self.is_running():
+ ret = vserverimpl.setrlimit(self.ctx,resource_type,hard,soft,minimum)
+
+ def __set_vserver_config(self,meth,resource_type,hard,soft,minimum):
+ """Generic set resource limit function for vserver"""
+ resources = {}
+ if hard <> VC_LIM_KEEP:
+ resources["VS_%s_HARD"%meth] = hard
+ if soft <> VC_LIM_KEEP:
+ resources["VS_%s_SOFT"%meth] = soft
+ if minimum <> VC_LIM_KEEP:
+ resources["VS_%s_MINIMUM"%meth] = minimum
+ if len(resources)>0:
+ self.update_resources(resources)
+ self.__set_vserver_limit(resource_type,hard,soft,minimum)
+
+ def __get_vserver_limit(self,resource_type):
+ """Generic get resource configuration function for vserver"""
+ if self.is_running():
+ ret = vserverimpl.getrlimit(self.ctx,resource_type)
+ else:
+ ret = __get_vserver_config(meth,resource_type)
+ return ret
+
+ def __get_vserver_config(self,meth,resource_type):
+ """Generic get resource configuration function for vserver"""
+ hard = int(self.config.get("VS_%s_HARD"%meth,VC_LIM_KEEP))
+ soft = int(self.config.get("VS_%s_SOFT"%meth,VC_LIM_KEEP))
+ minimum = int(self.config.get("VS_%s_MINIMUM"%meth,VC_LIM_KEEP))
+ return (hard,soft,minimum)
+
+ def set_WHITELISTED_config(self,whitelisted):
+ resources = {'VS_WHITELISTED': whitelisted}
+ self.update_resources(resources)
+