import repository from arizona
[raven.git] / apps / ravenpublish / container.py
1 from sfa.trust.certificate import Keypair
2 from sfa.trust.credential import Credential
3 from errorparser import ErrorParser
4 import hmac
5 import os
6 import shutil
7 import sys
8
9 CONTAINER_VERSION=4
10
11 def commalist(x):
12    items = x.split(",")
13    result = []
14    for item in items:
15       item = item.strip()
16       if item:
17          result.append(item)
18    return result
19
20 class container():
21     def __init__(self):
22         self.name = None
23         self.id = None
24         self.slices = []
25
26         self.cred = None
27         self.privateKey = None
28         self.privateKeyName = None
29         self.credName = "None" # user can specify "None" to not have a cred
30
31         self.noinstall = []
32
33         self.rootdir = "."
34
35         self.packagedir = "packages"
36         self.userdir = "users"
37         self.uploaddir = "upload"
38         self.tempestdir = "tempest"
39         self.templatedir = "template"
40         self.tpfiledir = "tpfiles"
41         self.configdir = "config"
42         self.cachedir = "cache"
43         self.builderdir = "build"
44         self.kongdir = "kong"
45
46         self.manage_packages = True
47         self.upgrade_stork = True
48         self.upgrade_owld = True
49         self.upgrade_kong = True
50
51         self.ravenconfig_rpm_name_version = ""
52
53         self.version = CONTAINER_VERSION
54
55     def set_dir(self, s):
56         self.rootdir = s
57
58     def get_configName(self):
59         return os.path.join(self.rootdir, "raven.conf")
60
61     def set_name(self, s):
62         self.name = s
63
64     def get_name(self):
65         return self.name
66
67     def set_privateKeyName(self, s):
68         self.privateKeyName = s
69
70     def get_privateKeyName(self):
71         return self.privateKeyName
72
73     def set_credName(self, s):
74         self.credName = s
75
76     def get_credName(self):
77         return self.credName
78
79     def get_rootdir(self):
80         return self.rootdir
81
82     def get_userdir(self):
83         return os.path.join(self.rootdir, self.userdir)
84
85     def get_packagedir(self):
86         return os.path.join(self.rootdir, self.packagedir)
87
88     def get_uploaddir(self):
89         return os.path.join(self.rootdir, self.uploaddir)
90
91     def get_tempestdir(self):
92         return os.path.join(self.rootdir, self.tempestdir)
93
94     def get_tempestgroupsdir(self):
95         return os.path.join(self.get_tempestdir(), "groups.d")
96
97     def get_tempestactionsdir(self):
98         return os.path.join(self.get_tempestdir(), "actions.d")
99
100     def get_templatedir(self):
101         return os.path.join(self.rootdir, self.templatedir)
102
103     def get_tpfiledir(self):
104         return os.path.join(self.rootdir, self.tpfiledir)
105
106     def get_configdir(self):
107         return os.path.join(self.rootdir, self.configdir)
108
109     def get_cachedir(self):
110         return os.path.join(self.rootdir, self.cachedir)
111
112     def get_builderdir(self):
113         return os.path.join(self.rootdir, self.builderdir)
114
115     def get_kongdir(self):
116         return os.path.join(self.rootdir, self.kongdir)
117
118     def get_slices(self):
119         return self.slices
120
121     def set_slices(self, s):
122         self.slices = s
123
124     def get_noinstall(self):
125         return self.noinstall
126
127     def set_noinstall(self, s):
128         self.noinstall = s
129
130     def set_ravenconfig_rpm(self, name_version):
131         self.ravenconfig_rpm_name_version = name_version
132
133     def get_ravenconfig_rpm(self):
134         return self.ravenconfig_rpm_name_version
135
136     def get_privateKey(self):
137         if not self.privateKey:
138             if not os.path.exists(self.privateKeyName):
139                 print >> sys.stderr, "Error: Private key", self.privateKeyName, "does not exist"
140                 sys.exit(-1)
141
142             self.privateKey = Keypair(filename = self.privateKeyName)
143
144         return self.privateKey
145
146     def get_cred(self):
147         if not self.cred:
148             if (not self.credName) or (self.credName == "None"):
149                 print >> sys.stderr, "Error: no geni credential is configured"
150                 sys.exit(-1)
151             if not os.path.exists(self.credName):
152                 print >> sys.stderr, "Error: cred", self.credName, "does not exist"
153                 sys.exit(-1)
154             self.cred = Credential(filename = self.credName)
155
156         return self.cred
157
158     def get_id(self):
159         if (not self.id):
160             print >> sys.stderr, "Error: Problem with experiment id. rerun 'raven config'"
161             sys.exit(-1)
162         return self.id
163
164     def check_update_id(self):
165         """ returns True if self.id is valid """
166         name_part = self.name[:31]
167         if (self.id!=None) and (self.id.startswith(name_part)):
168             # no need to recompute it, we have a valid id
169             return True
170
171         if (not self.privateKeyName) or (not os.path.exists(self.privateKeyName)):
172             # we need a private key. can't do anything.
173             return False
174
175         key = open(self.privateKeyName, "r").read()
176
177         hm = hmac.new(key, name_part).hexdigest()
178
179         # maximum of 64 bytes long
180         self.id = name_part + "_" + hm
181         return True
182
183     def makedirs(self):
184         if not os.path.exists(self.rootdir):
185             os.makedirs(self.rootdir)
186         if not os.path.exists(self.get_packagedir()):
187             os.makedirs(self.get_packagedir())
188         if not os.path.exists(self.get_userdir()):
189             os.makedirs(self.get_userdir())
190         if not os.path.exists(self.get_uploaddir()):
191             os.makedirs(self.get_uploaddir())
192         if not os.path.exists(self.get_tempestdir()):
193             os.makedirs(self.get_tempestdir())
194         if not os.path.exists(self.get_tempestactionsdir()):
195             os.makedirs(self.get_tempestactionsdir())
196         if not os.path.exists(self.get_tempestgroupsdir()):
197             os.makedirs(self.get_tempestgroupsdir())
198         if not os.path.exists(self.get_templatedir()):
199             os.makedirs(self.get_templatedir())
200         if not os.path.exists(self.get_tpfiledir()):
201             os.makedirs(self.get_tpfiledir())
202         if not os.path.exists(self.get_configdir()):
203             os.makedirs(self.get_configdir())
204         if not os.path.exists(self.get_cachedir()):
205             os.makedirs(self.get_cachedir())
206         if not os.path.exists(self.get_builderdir()):
207             os.makedirs(self.get_builderdir())
208         if not os.path.exists(self.get_kongdir()):
209             os.makedirs(self.get_kongdir())
210
211     def copy_initial_files(self):
212        shutil.copyfile("/usr/local/raven/etc/stork.conf.template",
213                        os.path.join(self.get_templatedir(), "stork.conf.template"))
214
215        shutil.copyfile("/usr/local/raven/etc/stork.publickey",
216                        os.path.join(self.get_userdir(), "10.stork.publickey"))
217
218        shutil.copyfile("/usr/local/raven/etc/fedora8.publickey",
219                        os.path.join(self.get_userdir(), "20.fedora8.publickey"))
220
221        shutil.copyfile("/usr/local/raven/etc/stork.packages.pacman",
222                        os.path.join(self.get_tempestactionsdir(), "stork.packages.pacman"))
223
224        shutil.copyfile("/usr/local/raven/etc/owl.packages.pacman",
225                        os.path.join(self.get_tempestactionsdir(), "owl.packages.pacman"))
226
227        shutil.copyfile("/usr/local/raven/etc/kong.packages.pacman",
228                        os.path.join(self.get_tempestactionsdir(), "kong.packages.pacman"))
229
230        for fn in ["prepare", "start", "stop", "abort", "kong-server.conf",
231                   "prepared", "complete", "opt_out"]:
232            shutil.copyfile("/usr/local/raven/etc/" + fn,
233                            os.path.join(self.get_kongdir(), fn))
234
235
236     def exists(self):
237         return os.path.exists(self.get_configName())
238
239     def load(self):
240         self.read_config(self.get_configName())
241
242     def read_config(self, fn):
243         parser = ErrorParser()
244         parser.Read(fn, True)
245         self.name = parser.Get("experiment", "name")
246         self.id = parser.GetOpt("experiment", "id", "")
247         self.slices = commalist(parser.GetOpt("experiment", "slices", ""))
248         self.privateKeyName = parser.Get("identity", "privatekeyname")
249         self.credName = parser.GetOpt("identity", "credname", "None")
250         self.packagedir = parser.Get("dir", "packagedir")
251         self.userdir = parser.Get("dir", "userdir")
252         self.uploaddir = parser.Get("dir", "uploaddir")
253         self.tempestdir = parser.Get("dir", "tempestdir")
254         self.configdir = parser.Get("dir", "configdir")
255         #self.kongdir = parser.Get("dir", "kong")
256         self.manage_packages = parser.GetOptBool("manage_packages", "manage_packages", True)
257         self.upgrade_stork = parser.GetOptBool("manage_packages", "upgrade_stork", True)
258         self.upgrade_owld = parser.GetOptBool("manage_packages", "upgrade_owld", True)
259         self.upgrade_kong = parser.GetOptBool("manage_packages", "upgrade_kong", True)
260         self.noinstall = commalist(parser.GetOpt("packagerules", "noinstall", ""))
261         self.version = parser.GetOptInt("container", "version", 0)
262         self.ravenconfig_rpm_name_version = parser.GetOpt("kong_package_check", "ravenconfig_rpm", "")
263
264     def save(self):
265         parser = ErrorParser()
266         parser.add_section("experiment")
267         parser.add_section("identity")
268         parser.add_section("dir")
269         parser.add_section("packagerules")
270         parser.add_section("manage_packages")
271         parser.add_section("container")
272         parser.add_section("kong_package_check")
273         parser.set("experiment", "name", self.name)
274         parser.set("experiment", "id", self.id)
275         parser.set("experiment", "slices", ",".join(self.slices))
276         parser.set("identity", "privatekeyname", self.privateKeyName)
277         parser.set("identity", "credname", self.credName)
278         parser.set("dir", "packagedir", self.packagedir)
279         parser.set("dir", "userdir", self.userdir)
280         parser.set("dir", "uploaddir", self.uploaddir)
281         parser.set("dir", "tempestdir", self.tempestdir)
282         parser.set("dir", "configdir", self.configdir)
283         parser.set("dir", "builderdir", self.builderdir)
284         parser.set("dir", "kong", self.kongdir)
285         parser.set("packagerules", "noinstall", ",".join(self.noinstall))
286         parser.SetBool("manage_packages", "manage_packages", self.manage_packages)
287         parser.SetBool("manage_packages", "upgrade_stork", self.upgrade_stork)
288         parser.SetBool("manage_packages", "upgrade_owld", self.upgrade_owld)
289         parser.SetBool("manage_packages", "upgrade_kong", self.upgrade_kong)
290         parser.SetInt("container", "version", self.version)
291         parser.set("kong_package_check", "ravenconfig_rpm", self.ravenconfig_rpm_name_version)
292         parser.write(open(self.get_configName(), "w"))
293
294