import repository from arizona
[raven.git] / apps / gacks / gackspolicy.py
1 import os
2 import hashlib
3
4 from ravenlib.ravenconfigparser import RavenConfigParser
5
6 # since we're passing around pickled objects, lets keep track of versions
7 VERSION=1
8
9 class GacksPolicy:
10     def __init__(self, name, resource, description=""):
11         self.name = name
12         self.resource = resource
13         self.description = description
14         self.maxUnitsConcurrent = -1
15         self.maxUnitsNode = -1
16         self.maxUnitsDay = -1
17         self.maxDuration = -1
18         self.maxEndDays = -1
19         self.bucketInRate = -1
20         self.bucketMax = -1
21         self.bucketInit = -1
22         self.cost = 1
23         self.reservations = []
24
25     def dump(self):
26         print self.name
27         print "   resource:", self.resource
28         print "   description:", self.description
29         if self.maxUnitsConcurrent>=0:
30            print "   maxUnitsConcurrent:", self.maxUnitsConcurrent
31         if self.maxUnitsNode>=0:
32            print "   maxUnitsNode:", self.maxUnitsNode
33         if self.maxUnitsDay>=0:
34             print "   maxUnitsDay:", self.maxUnitsDay
35         if self.maxDuration>=0:
36             print "   maxDuration:", self.maxDuration
37         if self.maxEndDays>=0:
38             print "   maxEndDays:", self.maxEndDays
39         if self.bucketInRate>=0:
40             print "   bucketInRate:", self.bucketInRate, "bucketMax:", self.bucketMax, "bucketInit:", self.bucketInit
41         if self.cost>=0:
42             print "   cost per unit:", self.cost
43         print "   reservations:", ",".join(self.reservations)
44
45 class GacksPolicyDirectory:
46     def __init__(self, dir=None):
47         self.version = VERSION
48         self.policies = []
49         if dir:
50             self.read_dir(dir)
51
52     def read_dir(self, dir):
53         for fn in os.listdir(dir):
54             pathname = os.path.join(dir, fn)
55             if not os.path.isfile(pathname):
56                 continue
57             if pathname.endswith("~") or pathname.startswith("~"):
58                 continue
59             self.read_config(pathname)
60
61     def decode_duration(self, x):
62         multiplier = 1
63         if x.endswith("days"):
64             multiplier = 24*60*60
65             x = x.rstrip("days")
66         if x.endswith("hours"):
67             multiplier = 60*60
68             x = x.rstrip("hours")
69         x = x.strip()
70         try:
71             x = int(x) * multiplier
72         except ValueError:
73             print "didn't undestand duration", x
74             x = 0
75         return x
76
77     def read_config(self, fn):
78         parser = RavenConfigParser()
79
80         parser.update_order = True
81         parser.Read(fn, True)
82         parser.update_order = False
83
84         name = parser.GetOpt("policy", "name", None)
85         if not name:
86             return
87
88         description = parser.GetOpt("policy", "description", "")
89         resources = parser.GetOpt("policy", "resources", "").split(",")
90         resources = [x.strip() for x in resources]
91
92         for section in resources:
93             p = GacksPolicy(name=name, description=description, resource=section)
94
95             p.description = parser.GetOpt(section, "description", description)
96             p.maxUnitsConcurrent = parser.GetOptInt(section, "maxUnitsConcurrent", -1)
97             p.maxUnitsNode = parser.GetOptInt(section, "maxUnitsNode", -1)
98             p.maxUnitsDay = parser.GetOptInt(section, "maxUnitsDay", -1)
99             p.maxDuration = self.decode_duration(parser.GetOpt(section, "maxDuration", "-1"))
100             p.maxEndDays = parser.GetOptInt(section, "MaxEndDays", -1)
101             p.bucketInRate = parser.GetOptInt(section, "BucketInRate", -1)
102             p.bucketMax = parser.GetOptInt(section, "BucketMax", -1)
103             p.bucketInit = parser.GetOptInt(section, "BucketInit", -1)
104             p.cost = parser.GetOptInt(section, "cost", 1)
105
106             resList = parser.GetOpt(section, "reservations", "").split(",")
107             resList = [x.strip() for x in resList]
108             p.reservations = resList
109
110             self.policies.append(p)
111
112     def get_policy(self, name, resource):
113         for policy in self.policies:
114             if (policy.name == name) and (policy.resource == resource):
115                 return policy
116         return None
117
118     def dump(self):
119         for policy in self.policies:
120             policy.dump()
121
122     def pickled(self):
123         if not hasattr(self, "_pickled"):
124             self._pickled = cPickle.dumps(self)
125             self._hash = hashlib.sha1(self._pickled).hexdigest()
126         return self._pickled
127
128     def hash(self):
129         self.pickled() # makes sure self.__hash is computed
130         return self._hash
131
132 def test():
133     rdir = GacksPolicyDirectory(dir="./policy.d")
134     rdir.dump()
135
136 if __name__=="__main__":
137    test()
138
139
140
141
142
143
144
145
146