1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | from clint import arguments |
---|
4 | from clint.textui import puts, indent, colored |
---|
5 | import lliurex.lliurexup |
---|
6 | import os |
---|
7 | import sys |
---|
8 | import commands |
---|
9 | import datetime |
---|
10 | |
---|
11 | class LliurexUpCli(object): |
---|
12 | def __init__(self): |
---|
13 | |
---|
14 | self.lliurexcore = lliurex.lliurexup.LliurexUpCore() |
---|
15 | log_msg="------------------------------------------\n"+"LLIUREX-UP-CLI STARTING AT: " + datetime.datetime.today().strftime("%d/%m/%y %H:%M:%S") +"\n------------------------------------------" |
---|
16 | self.log(log_msg) |
---|
17 | self.checkInitialFlavour() |
---|
18 | |
---|
19 | def checkInitialFlavour(self): |
---|
20 | |
---|
21 | self.targetMetapackage=self.lliurexcore.checkInitialFlavour() |
---|
22 | log_msg="Initial check metapackage. Metapackage to install: " + str(self.targetMetapackage) |
---|
23 | self.log(log_msg) |
---|
24 | log_msg="Get initial flavours: " + str(self.lliurexcore.previuosFlavours) |
---|
25 | self.log(log_msg) |
---|
26 | |
---|
27 | |
---|
28 | def initActionsScript(self): |
---|
29 | |
---|
30 | command=self.lliurexcore.initActionsScript(self.initActionsArg) |
---|
31 | |
---|
32 | try: |
---|
33 | os.system(command) |
---|
34 | log_msg="Exec Init-Actions" |
---|
35 | self.log(log_msg) |
---|
36 | except Exception as e: |
---|
37 | log_msg="Exec Init-Actions.Error: " +str(e) |
---|
38 | self.log(log_msg) |
---|
39 | print e |
---|
40 | |
---|
41 | def checkLliurexUp(self): |
---|
42 | |
---|
43 | is_lliurexup_updated=self.lliurexcore.isLliurexUpIsUpdated() |
---|
44 | |
---|
45 | |
---|
46 | if not is_lliurexup_updated: |
---|
47 | print (" [Lliurex-up]: Updating Lliurex-up") |
---|
48 | is_lliurexup_installed=self.lliurexcore.installLliurexUp() |
---|
49 | log_msg="Installing lliurex-up. Returncode: " + str(is_lliurexup_installed['returncode']) + ". Error: " + str(is_lliurexup_installed['stderrs']) |
---|
50 | self.log(log_msg) |
---|
51 | print (" [Lliurex-up]: Lliurex-up is now udpate and will be reboot now" ) |
---|
52 | time.sleep(3) |
---|
53 | os.execv("lliurex-upgrade",sys.argv) |
---|
54 | |
---|
55 | else: |
---|
56 | log_msg="Checking lliurex-up. Is lliurex-up updated: "+ str(is_lliurexup_updated) |
---|
57 | self.log(log_msg) |
---|
58 | print (" [Lliurex-up]: Lliurex-up is updated.Nothing to do") |
---|
59 | |
---|
60 | |
---|
61 | def checkMirror(self): |
---|
62 | |
---|
63 | is_mirror_updated=self.lliurexcore.lliurexMirrorIsUpdated() |
---|
64 | |
---|
65 | if is_mirror_updated !=None: |
---|
66 | is_mirror_running=self.lliurexcore.lliurexMirrorIsRunning() |
---|
67 | |
---|
68 | if is_mirror_updated['action']=='update': |
---|
69 | log_msg="Checking mirror. Is mirror update: False" |
---|
70 | self.log(log_msg) |
---|
71 | if not is_mirror_running: |
---|
72 | response=raw_input(' [LLiurex-up]: Do you want update mirror (yes/no): ').lower() |
---|
73 | if response.startswith('y'): |
---|
74 | log_msg="Update lliurex-mirror: Yes" |
---|
75 | self.log(log_msg) |
---|
76 | print(" [Lliurex-up]: Updating mirror. Wait a moment please") |
---|
77 | command='lliurex-mirror update llx16' |
---|
78 | os.system(command) |
---|
79 | else: |
---|
80 | log_msg="Update lliurex-mirror: No" |
---|
81 | self.log(log_msg) |
---|
82 | print(" [Lliurex-up]: Mirror update.Nothing to do") |
---|
83 | else: |
---|
84 | log_msg="Checking mirror. Is mirror update: None" |
---|
85 | self.log(log_msg) |
---|
86 | print(" [Lliurex-up]: Nothing to do with mirror") |
---|
87 | |
---|
88 | |
---|
89 | def checkingInitialFlavourToInstall(self): |
---|
90 | |
---|
91 | |
---|
92 | if self.targetMetapackage == None: |
---|
93 | # self.flavourToInstall=self.lliurexcore.requiresInstallFlavour() |
---|
94 | # if self.flavourToInstall != None: |
---|
95 | # log_msg="Requires Install this Flavour: " + str(self.flavourToInstall) |
---|
96 | # self.log(log_msg) |
---|
97 | # print " [Lliurex-up]: Installation of flavour is required: " +str(self.flavourToInstall) |
---|
98 | # is_flavour_installed=self.lliurexcore.installInitialFlavour(self.flavourToInstall) |
---|
99 | # returncode=is_flavour_installed['returncode'] |
---|
100 | # error=is_flavour_installed['stderrs'] |
---|
101 | # print " [Lliurex-up]: Flavour is now installed: " + str(returncode) + " Error: " + str(error) |
---|
102 | # log_msg="Install initial flavour:" + flavourToInstall + ": Returncode: " + str(returncode) + " Error: " + str(error) |
---|
103 | # self.log(log_msg) |
---|
104 | # else: |
---|
105 | print " [Lliurex-up]: Installation of metapackage is not required" |
---|
106 | |
---|
107 | else: |
---|
108 | print " [Lliurex-up]: Installation of metapackage is required: " + str(self.targetMetapackage) |
---|
109 | is_flavour_installed=self.lliurexcore.installInitialFlavour(self.targetMetapackage) |
---|
110 | self.returncode_initflavour=is_flavour_installed['returncode'] |
---|
111 | error=is_flavour_installed['stderrs'] |
---|
112 | log_msg="Install initial flavour:" + self.targetMetapackage + ": Returncode: " + str(self.returncode_initflavour) + " Error: " + str(error) |
---|
113 | self.log(log_msg) |
---|
114 | print " [Lliurex-up]: Metapackage is now installed: " + str(returncode) + " Error: " + str(error) |
---|
115 | |
---|
116 | |
---|
117 | def checkingIncorrectFlavours(self): |
---|
118 | |
---|
119 | incorrectFlavours=self.lliurexcore.checkIncorrectFlavours() |
---|
120 | log_msg="Checking incorrect metapackages. Others metapackages detected: " + str(incorrectFlavours) |
---|
121 | self.log(log_msg) |
---|
122 | |
---|
123 | return incorrectFlavours |
---|
124 | |
---|
125 | |
---|
126 | def checkPreviousUpgrade(self): |
---|
127 | |
---|
128 | error=False |
---|
129 | if self.returncode_initflavour!=0: |
---|
130 | error=True |
---|
131 | |
---|
132 | else: |
---|
133 | if self.version_update["candidate"]!=None: |
---|
134 | if self.version_update["installed"]!=self.version_update["candidate"]: |
---|
135 | error=True |
---|
136 | else: |
---|
137 | if self.version_update["installed"]!=self.version_available["candidate"]: |
---|
138 | error=True |
---|
139 | |
---|
140 | return error |
---|
141 | |
---|
142 | def preActionsScript(self): |
---|
143 | |
---|
144 | command=self.lliurexcore.preActionsScript() |
---|
145 | try: |
---|
146 | os.system(command) |
---|
147 | log_msg="Exec Pre-Actions" |
---|
148 | self.log(log_msg) |
---|
149 | |
---|
150 | except Exception as e: |
---|
151 | log_msg="Exec Pre-Actions. Error " +str(e) |
---|
152 | self.log(log_msg) |
---|
153 | print e |
---|
154 | |
---|
155 | def distUpgrade(self): |
---|
156 | |
---|
157 | command=self.lliurexcore.distUpgradeProcess() |
---|
158 | try: |
---|
159 | os.system(command) |
---|
160 | log_msg="Exec Dist-uggrade" |
---|
161 | self.log(log_msg) |
---|
162 | except Exception as e: |
---|
163 | log_msg="Exec Dist-uggrade.Error : " +str(e) |
---|
164 | self.log(log_msg) |
---|
165 | print e |
---|
166 | |
---|
167 | def checkingFinalFlavourToInstall(self): |
---|
168 | |
---|
169 | #self.flavourToInstall=self.lliurexcore.checkFinalFlavour() |
---|
170 | self.flavourToInstall=self.lliurexcore.checkFlavour() |
---|
171 | |
---|
172 | log_msg="Final check metapackage. Metapackage to install: " + str(self.flavourToInstall) |
---|
173 | self.log(log_msg) |
---|
174 | |
---|
175 | if self.flavourToInstall!=None: |
---|
176 | print (" [Lliurex-up]: Install of metapackage is required: " + str(self.flavourToInstall)) |
---|
177 | |
---|
178 | command=self.lliurexcore.installFinalFlavour(self.flavourToInstall) |
---|
179 | try: |
---|
180 | os.system(command) |
---|
181 | |
---|
182 | except Exception as e: |
---|
183 | print e |
---|
184 | else: |
---|
185 | print (" [Lliurex-up]: metapackage is correct. Nothing to do") |
---|
186 | |
---|
187 | |
---|
188 | def postActionsScript(self): |
---|
189 | |
---|
190 | command=self.lliurexcore.postActionsScript()+' 2>&1 1>/dev/null | tee ' + self.lliurexcore.errorpostaction_token |
---|
191 | try: |
---|
192 | os.system(command) |
---|
193 | log_msg="Exec Post-Actions" |
---|
194 | self.log(log_msg) |
---|
195 | except Exception as e: |
---|
196 | print e |
---|
197 | |
---|
198 | def checkFinalUpgrade(self): |
---|
199 | |
---|
200 | print (" [Lliurex-up]: Checking Dist-upgrade ") |
---|
201 | error=self.lliurexcore.checkErrorDistUpgrade() |
---|
202 | |
---|
203 | if error: |
---|
204 | print(" [Lliurex-up]: The updated process is endend with errors") |
---|
205 | log_msg="Dist-upgrade process ending with errors" |
---|
206 | |
---|
207 | else: |
---|
208 | print(" [Lliurex-up]: The system is now update") |
---|
209 | log_msg="Dist-upgrade process ending OK" |
---|
210 | |
---|
211 | self.log(log_msg) |
---|
212 | |
---|
213 | def log(self,msg): |
---|
214 | |
---|
215 | log_file="/var/log/lliurex-up.log" |
---|
216 | f=open(log_file,"a+") |
---|
217 | f.write(msg + '\n') |
---|
218 | f.close() |
---|
219 | |
---|
220 | def main(self,mode): |
---|
221 | |
---|
222 | if mode=="sai": |
---|
223 | self.initActionsArg="initActionsSai" |
---|
224 | |
---|
225 | else: |
---|
226 | mode="nomal" |
---|
227 | self.initActionsArg="initActions" |
---|
228 | |
---|
229 | log_msg="Mode of execution: " + str(mode) |
---|
230 | self.log(log_msg) |
---|
231 | |
---|
232 | print(" [Lliurex-up]: Checking system") |
---|
233 | self.initActionsScript() |
---|
234 | print(" [Lliurex-up]: Checking Lliurex-up version") |
---|
235 | self.checkLliurexUp() |
---|
236 | print(" [Lliurex-up]: Checking if mirror is updated") |
---|
237 | self.checkMirror() |
---|
238 | print(" [Lliurex-up]: Checking LliurexVersion from local repository") |
---|
239 | self.version_update=self.lliurexcore.getLliurexVersionLocal() |
---|
240 | log_msg="Get LliurexVersion installed: " + str(self.version_update["installed"]) |
---|
241 | self.log(log_msg) |
---|
242 | log_msg="Get LliurexVersion candidate from Local repository: " + str(self.version_update["candidate"]) |
---|
243 | self.log(log_msg) |
---|
244 | |
---|
245 | print(" [Lliurex-up]: Checking LliurexVersion from lliurex.net") |
---|
246 | self.version_available=self.lliurexcore.getLliurexVersionLliurexNet() |
---|
247 | log_msg="Get LliurexVersion candidate from Lliurex Net: " + str(self.version_available["candidate"]) |
---|
248 | self.log(log_msg) |
---|
249 | |
---|
250 | print(" [Lliurex-up]: Checking if installation of flavour is required") |
---|
251 | self.checkingInitialFlavourToInstall() |
---|
252 | |
---|
253 | print(" [Lliurex-up]: Checking for new updates") |
---|
254 | self.packages=self.lliurexcore.getPackagesToUpdate() |
---|
255 | |
---|
256 | if not self.checkingIncorrectFlavours(): |
---|
257 | if len(self.packages)>0: |
---|
258 | print(" [Lliurex-up]:List of packages to update") |
---|
259 | log_msg="Number of packages: "+ str(len(self.packages)) |
---|
260 | self.log(log_msg) |
---|
261 | |
---|
262 | for item in self.packages: |
---|
263 | print item + ": " + self.packages[item]["candidate"] |
---|
264 | |
---|
265 | response=raw_input(' [LLiurex-up]: Do you want to udpate the system(yes/no)): ').lower() |
---|
266 | if response.startswith('y'): |
---|
267 | print(" [Lliurex-up]: Preparing system to update") |
---|
268 | self.preActionsScript() |
---|
269 | print(" [Lliurex-up]: Downloading and installing packages") |
---|
270 | self.distUpgrade() |
---|
271 | print(" [Lliurex-up]: Ending the update") |
---|
272 | self.postActionsScript() |
---|
273 | print(" [Lliurex-up]: Checking Final Flavour") |
---|
274 | self.checkingFinalFlavourToInstall() |
---|
275 | self.checkFinalUpgrade() |
---|
276 | self.lliurexcore.cleanEnvironment() |
---|
277 | |
---|
278 | else: |
---|
279 | log_msg="Cancel the update" |
---|
280 | self.log(log_msg) |
---|
281 | print(" [Lliurex-up]: Cancel the update") |
---|
282 | self.lliurexcore.cleanEnvironment() |
---|
283 | return 0 |
---|
284 | else: |
---|
285 | if not self.checkPreviousUpgrade(): |
---|
286 | print(" [Lliurex-up]: Your systems is updated. Nothing to do") |
---|
287 | return 0 |
---|
288 | else: |
---|
289 | print(" [Lliurex-up]: Updated abort. An error occurred checking new updates") |
---|
290 | return 1 |
---|
291 | else: |
---|
292 | print("[Lliurex-up]: Updated abort for incorrect flavours detected in new update") |
---|
293 | return 1 |
---|
294 | |
---|
295 | def usage(): |
---|
296 | puts("Usage") |
---|
297 | with indent(4): |
---|
298 | puts("lliurex-upgrade [FLAGS...]") |
---|
299 | puts("Flags") |
---|
300 | with indent(4): |
---|
301 | puts("-h --help: Show help") |
---|
302 | puts("-s --sai: Update the system without checking sources list") |
---|
303 | |
---|
304 | sys.exit(1) |
---|
305 | |
---|
306 | |
---|
307 | |
---|
308 | if __name__ == '__main__': |
---|
309 | if os.geteuid() != 0: |
---|
310 | print "You need be root!" |
---|
311 | sys.exit(1) |
---|
312 | mode=None |
---|
313 | args=arguments.Args().copy |
---|
314 | |
---|
315 | if args.contains(["-h", "--help"]): |
---|
316 | usage() |
---|
317 | |
---|
318 | if args.contains(["-s", "--sai"]): |
---|
319 | mode="sai" |
---|
320 | else: |
---|
321 | if len(args)>0: |
---|
322 | usage() |
---|
323 | |
---|
324 | lliurexupcli = LliurexUpCli() |
---|
325 | sys.exit(lliurexupcli.main(mode)) |
---|