1 | import subprocess |
---|
2 | import threading |
---|
3 | import datetime |
---|
4 | import os |
---|
5 | |
---|
6 | class LliurexMirrorCore: |
---|
7 | |
---|
8 | |
---|
9 | LOCK_FILE="/net/mirror/%s/db/lockfile" |
---|
10 | |
---|
11 | def __init__(self): |
---|
12 | |
---|
13 | self.update_thread=threading.Thread() |
---|
14 | self.percentage=("Got 0%",None) |
---|
15 | self.distro="llx1505" |
---|
16 | |
---|
17 | # init |
---|
18 | |
---|
19 | def startup(self,options): |
---|
20 | |
---|
21 | self.n4d_vars=objects["VariablesManager"] |
---|
22 | self.variable=objects["VariablesManager"].get_variable("LLIUREXMIRROR") |
---|
23 | |
---|
24 | |
---|
25 | if self.variable==None: |
---|
26 | try: |
---|
27 | self.n4d_vars.add_variable("LLIUREXMIRROR",{"internal":{}},"","Lliurex Mirror info variable","lliurex-mirror-core") |
---|
28 | except Exception as e: |
---|
29 | pass |
---|
30 | |
---|
31 | if type(self.variable)!=type({}): |
---|
32 | self.variable={} |
---|
33 | self.variable["internal"]={} |
---|
34 | |
---|
35 | #def init |
---|
36 | |
---|
37 | |
---|
38 | |
---|
39 | |
---|
40 | def update(self,distro=None): |
---|
41 | |
---|
42 | if distro==None: |
---|
43 | distro=self.distro |
---|
44 | |
---|
45 | lock_file=LliurexMirrorCore.LOCK_FILE%distro |
---|
46 | |
---|
47 | |
---|
48 | |
---|
49 | if self.update_thread.is_alive(): |
---|
50 | return [False,"Lliurex-mirror (n4d instance) is running"] |
---|
51 | |
---|
52 | if os.path.exists(lock_file): |
---|
53 | return [False,"Lock file exists"] |
---|
54 | |
---|
55 | self.percentage=("Got 0%",None) |
---|
56 | self.update_thread=threading.Thread(target=self._update,args=(distro,)) |
---|
57 | self.update_thread.daemon=True |
---|
58 | self.update_thread.start() |
---|
59 | |
---|
60 | return [True,"Running"] |
---|
61 | |
---|
62 | #def update |
---|
63 | |
---|
64 | def is_alive(self): |
---|
65 | return self.update_thread.is_alive() |
---|
66 | #def is_alive |
---|
67 | |
---|
68 | def _update(self,distro): |
---|
69 | |
---|
70 | |
---|
71 | p=subprocess.Popen(["/usr/share/n4d/binary-plugins/lliurex_mirror " + distro + " update"],shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) |
---|
72 | |
---|
73 | |
---|
74 | try: |
---|
75 | objects["ZCenterVariables"].add_pulsating_color("lliurexmirror") |
---|
76 | except: |
---|
77 | pass |
---|
78 | |
---|
79 | while True: |
---|
80 | line1=p.stdout.readline().strip("\n") |
---|
81 | if line1!="": |
---|
82 | if "Got " in line1: |
---|
83 | self.percentage=(line1,None) |
---|
84 | else: |
---|
85 | break |
---|
86 | |
---|
87 | self.percentage=(self.percentage[0],p.poll()) |
---|
88 | self.set_mirror_info(distro) |
---|
89 | |
---|
90 | try: |
---|
91 | objects["ZCenterVariables"].remove_pulsating_color("lliurexmirror") |
---|
92 | except: |
---|
93 | pass |
---|
94 | |
---|
95 | |
---|
96 | #def _update |
---|
97 | |
---|
98 | def get_progress(self,full=False): |
---|
99 | |
---|
100 | if full: |
---|
101 | return self.percentage |
---|
102 | else: |
---|
103 | return self.percentage[0] |
---|
104 | |
---|
105 | #def get_progress |
---|
106 | |
---|
107 | def get_rc_status(self): |
---|
108 | |
---|
109 | return self.percentage[1] |
---|
110 | |
---|
111 | #def get_rc_status |
---|
112 | |
---|
113 | |
---|
114 | def get_size(self,start_path = '.'): |
---|
115 | |
---|
116 | total_size = 0 |
---|
117 | try: |
---|
118 | for dirpath, dirnames, filenames in os.walk(start_path): |
---|
119 | for f in filenames: |
---|
120 | fp = os.path.join(dirpath, f) |
---|
121 | total_size += os.path.getsize(fp) |
---|
122 | |
---|
123 | total_size/=1024*1024*1024.0 |
---|
124 | return total_size |
---|
125 | except: |
---|
126 | return 0 |
---|
127 | |
---|
128 | #def get_size(start_path = '.'): |
---|
129 | |
---|
130 | |
---|
131 | |
---|
132 | def set_mirror_info(self,distro=None): |
---|
133 | |
---|
134 | if distro!=None: |
---|
135 | distro=distro |
---|
136 | else: |
---|
137 | distro=self.distro |
---|
138 | |
---|
139 | |
---|
140 | #self.n4d_vars.set_variable("ZEROCENTERINTERNAL",self.internal_variable) |
---|
141 | |
---|
142 | MIRROR_DATE=datetime.date.today().strftime("%d/%m/%Y") |
---|
143 | MIRROR_SIZE=self.get_size("/net/mirror/"+distro) |
---|
144 | |
---|
145 | self.variable["internal"]["last_mirror_date"]=MIRROR_DATE |
---|
146 | self.variable["internal"]["mirror_size"]=str(MIRROR_SIZE) |
---|
147 | |
---|
148 | print self.n4d_vars.set_variable("LLIUREXMIRROR",self.variable) |
---|
149 | |
---|
150 | #set_custom_text(self,app,text): |
---|
151 | txt="Updated on: " + str(MIRROR_DATE) |
---|
152 | txt+=" # Size: %.2fGB"%MIRROR_SIZE |
---|
153 | try: |
---|
154 | objects["ZCenterVariables"].set_custom_text("lliurexmirror",txt) |
---|
155 | abstract=open('/var/log/lliurex/lliurex-mirror.log','w') |
---|
156 | abstract.write(txt+"\n") |
---|
157 | abstract.close() |
---|
158 | except Exception as e: |
---|
159 | pass |
---|
160 | |
---|
161 | |
---|
162 | |
---|
163 | #def set_mirror_info(self): |
---|
164 | |
---|
165 | |
---|