1 | import os |
---|
2 | import bcrypt |
---|
3 | import tempfile |
---|
4 | import ConfigParser |
---|
5 | import shutil |
---|
6 | import lliurex.net |
---|
7 | |
---|
8 | from jinja2 import Environment |
---|
9 | from jinja2.loaders import FileSystemLoader |
---|
10 | from jinja2 import Template |
---|
11 | |
---|
12 | |
---|
13 | class LliurexOwncloud: |
---|
14 | |
---|
15 | BASE_DIR="/usr/share/lliurex-owncloud/" |
---|
16 | OWNCLOUD_BASE_DIR=BASE_DIR+"llx-data/" |
---|
17 | HTACCESS=OWNCLOUD_BASE_DIR+".htaccess" |
---|
18 | ADMIN_DATA=OWNCLOUD_BASE_DIR+"data/" |
---|
19 | CONFIG_DATA=OWNCLOUD_BASE_DIR+"config/" |
---|
20 | TEMPLATE_DIR=OWNCLOUD_BASE_DIR+"" |
---|
21 | SQL_TEMPLATE="owncloud.sql" |
---|
22 | EASY_SITE=OWNCLOUD_BASE_DIR+"owncloud.json" |
---|
23 | #CNAME="cname-owncloud" |
---|
24 | |
---|
25 | OWNCLOUD_DATA_DIR="/var/www/owncloud/data/" |
---|
26 | OWNCLOUD_CONFIG_DIR="/var/www/owncloud/config/" |
---|
27 | EASY_SITES_DIR="/var/www/srv/links/" |
---|
28 | |
---|
29 | |
---|
30 | ''' |
---|
31 | # TESTING # |
---|
32 | |
---|
33 | TEMPLATE_DIR="/home/netadmin/" |
---|
34 | BASE_DIR="/home/netadmin/workspace/lliurex-owncloud/install-files"+BASE_DIR |
---|
35 | OWNCLOUD_BASE_DIR=BASE_DIR+"llx-data/" |
---|
36 | HTACCESS=OWNCLOUD_BASE_DIR+".htaccess" |
---|
37 | EASY_SITE=OWNCLOUD_BASE_DIR+"owncloud.json" |
---|
38 | |
---|
39 | # TESTING # |
---|
40 | ''' |
---|
41 | |
---|
42 | def __init__(self): |
---|
43 | |
---|
44 | self.template=None |
---|
45 | self.template_vars=["DB_USER","DB_PWD","DB_NAME","ADMIN_USER","ADMIN_PWD"] |
---|
46 | |
---|
47 | #def init |
---|
48 | |
---|
49 | def parse_template(self,template_path): |
---|
50 | |
---|
51 | print("* Parsing template...") |
---|
52 | |
---|
53 | config = ConfigParser.ConfigParser() |
---|
54 | config.optionxform=str |
---|
55 | config.read(template_path) |
---|
56 | |
---|
57 | self.template={} |
---|
58 | try: |
---|
59 | |
---|
60 | self.template["DB_USER"]=config.get("owncloud","DB_USER") |
---|
61 | self.template["DB_PWD"]=config.get("owncloud","DB_PWD") |
---|
62 | self.template["DB_NAME"]=config.get("owncloud","DB_NAME") |
---|
63 | self.template["ADMIN_USER"]=config.get("owncloud","ADMIN_USER") |
---|
64 | self.template["ADMIN_PWD"]=config.get("owncloud","ADMIN_PWD") |
---|
65 | |
---|
66 | self.load_template(self.template) |
---|
67 | return [True,""] |
---|
68 | |
---|
69 | except Exception as e: |
---|
70 | msg="[!] Error: %s"%(str(e)) |
---|
71 | print("[!] Error:",e) |
---|
72 | return [False,e] |
---|
73 | |
---|
74 | |
---|
75 | |
---|
76 | |
---|
77 | #def parse_template |
---|
78 | |
---|
79 | |
---|
80 | def load_template(self,template): |
---|
81 | |
---|
82 | print("* Loading template...") |
---|
83 | |
---|
84 | if not type({})==type(template): |
---|
85 | return [False,""] |
---|
86 | |
---|
87 | for var in self.template_vars: |
---|
88 | if var not in template: |
---|
89 | return [False,""] |
---|
90 | |
---|
91 | self.template=template |
---|
92 | |
---|
93 | try: |
---|
94 | |
---|
95 | self.template["LDAP_BASE_USER_TREE"]="ou=People,"+objects["VariablesManager"].get_variable("LDAP_BASE_DN") |
---|
96 | self.template["LDAP_BASE_GROUP_TREE"]="ou=Groups,"+objects["VariablesManager"].get_variable("LDAP_BASE_DN") |
---|
97 | self.template["SRV_IP"]=objects["VariablesManager"].get_variable("SRV_IP") |
---|
98 | self.template["INTERNAL_DOMAIN"]=objects["VariablesManager"].get_variable("INTERNAL_DOMAIN") |
---|
99 | self.template["EXTERNAL_IP"]=lliurex.net.get_ip(objects["VariablesManager"].get_variable("EXTERNAL_INTERFACE")) |
---|
100 | |
---|
101 | except: |
---|
102 | import xmlrpclib as x |
---|
103 | |
---|
104 | c=x.ServerProxy("https://server:9779") |
---|
105 | self.template["LDAP_BASE_USER_TREE"]="ou=People,"+c.get_variable("","VariablesManager","LDAP_BASE_DN") |
---|
106 | self.template["LDAP_BASE_GROUP_TREE"]="ou=Groups,"+c.get_variable("","VariablesManager","LDAP_BASE_DN") |
---|
107 | self.template["SRV_IP"]=c.get_variable("","VariablesManager","SRV_IP") |
---|
108 | self.template["INTERNAL_DOMAIN"]=c.get_variable("","VariablesManager","INTERNAL_DOMAIN") |
---|
109 | self.template["EXTERNAL_IP"]=lliurex.net.get_ip(c.get_variable("","VariablesManager","EXTERNAL_INTERFACE")) |
---|
110 | |
---|
111 | self.template["ADMIN_PWD"]=self.create_password_bhash(self.template["ADMIN_PWD"]) |
---|
112 | return [True,""] |
---|
113 | |
---|
114 | #def load_template |
---|
115 | |
---|
116 | |
---|
117 | def mysql_service_init(self): |
---|
118 | |
---|
119 | print("* Initializing mysql root passwd (if needed) ...") |
---|
120 | os.system("sudo mysql_root_passwd -i") |
---|
121 | return [True,""] |
---|
122 | |
---|
123 | #def mysql_service_init |
---|
124 | |
---|
125 | |
---|
126 | def create_db(self): |
---|
127 | |
---|
128 | print("* Creating database...") |
---|
129 | |
---|
130 | if self.template==None: |
---|
131 | return [False,""] |
---|
132 | |
---|
133 | cmd='mysql -u%s -p%s -e "drop database IF EXISTS %s"'%(self.template["DB_USER"],self.template["DB_PWD"],self.template["DB_NAME"]) |
---|
134 | os.system(cmd) |
---|
135 | |
---|
136 | cmd='mysql -u%s -p%s -e "create database %s"'%(self.template["DB_USER"],self.template["DB_PWD"],self.template["DB_NAME"]) |
---|
137 | os.system(cmd) |
---|
138 | |
---|
139 | file_path=self.process_sql_template() |
---|
140 | if file_path==None: |
---|
141 | return [False,"Error processing sql template"] |
---|
142 | cmd="mysql -u %s -p%s %s < %s"%(self.template["DB_USER"],self.template["DB_PWD"],self.template["DB_NAME"],file_path) |
---|
143 | os.system(cmd) |
---|
144 | os.remove(file_path) |
---|
145 | |
---|
146 | return [True,""] |
---|
147 | |
---|
148 | #def init_sql |
---|
149 | |
---|
150 | def create_db_user(self): |
---|
151 | |
---|
152 | print("* Creating mysql user ...") |
---|
153 | |
---|
154 | db_pass=self.template["DB_PWD"] |
---|
155 | db_user=self.template["DB_USER"] |
---|
156 | db_name=self.template["DB_NAME"] |
---|
157 | cmd='mysql -uroot -p$(mysql_root_passwd -g) -e "GRANT ALL PRIVILEGES ON %s.* TO \'%s\'@localhost IDENTIFIED BY \'%s\'"'%(db_name, db_user,db_pass) |
---|
158 | ret=os.system(cmd) |
---|
159 | |
---|
160 | return [True,ret] |
---|
161 | |
---|
162 | #def generate_user |
---|
163 | |
---|
164 | |
---|
165 | def create_password_bhash(self,password): |
---|
166 | |
---|
167 | print("* Generating admin password...") |
---|
168 | |
---|
169 | salt=bcrypt.gensalt(10) |
---|
170 | return bcrypt.hashpw(password,bcrypt.gensalt(10)) |
---|
171 | |
---|
172 | #def create_password_bhash |
---|
173 | |
---|
174 | |
---|
175 | def process_sql_template(self): |
---|
176 | |
---|
177 | print("* Procesing SQL template...") |
---|
178 | try: |
---|
179 | template_dir=LliurexOwncloud.TEMPLATE_DIR |
---|
180 | sql_template_file=LliurexOwncloud.SQL_TEMPLATE |
---|
181 | |
---|
182 | tpl_env = Environment(loader=FileSystemLoader(template_dir)) |
---|
183 | sql_template = tpl_env.get_template(sql_template_file) |
---|
184 | content = sql_template.render(self.template).encode('UTF-8') |
---|
185 | |
---|
186 | tmp_file=tempfile.mktemp() |
---|
187 | f=open(tmp_file,"w") |
---|
188 | f.write(content) |
---|
189 | f.close() |
---|
190 | |
---|
191 | return tmp_file |
---|
192 | except: |
---|
193 | return None |
---|
194 | |
---|
195 | |
---|
196 | #def process_sql_template |
---|
197 | |
---|
198 | def clean_old_files(self): |
---|
199 | |
---|
200 | print("* Cleaning old OwnCloud data...") |
---|
201 | |
---|
202 | for dir in [LliurexOwncloud.OWNCLOUD_DATA_DIR,LliurexOwncloud.OWNCLOUD_CONFIG_DIR]: |
---|
203 | if os.path.exists(dir): |
---|
204 | os.system("rm -rf %s"%dir) |
---|
205 | |
---|
206 | return [True,""] |
---|
207 | |
---|
208 | #def clean_old_files |
---|
209 | |
---|
210 | def copy_new_files(self): |
---|
211 | |
---|
212 | print("* Copying new OwnCloud data...") |
---|
213 | |
---|
214 | cmd="cp -r %s/* %s"%(LliurexOwncloud.OWNCLOUD_BASE_DIR,"/var/www/owncloud/") |
---|
215 | os.system(cmd) |
---|
216 | |
---|
217 | for dir in [LliurexOwncloud.OWNCLOUD_DATA_DIR,LliurexOwncloud.OWNCLOUD_CONFIG_DIR]: |
---|
218 | if os.path.exists(dir): |
---|
219 | os.system("chown -R www-data:www-data %s"%dir) |
---|
220 | |
---|
221 | os.system("chmod 775 %s"%LliurexOwncloud.OWNCLOUD_CONFIG_DIR) |
---|
222 | os.system("chmod 770 %s"%LliurexOwncloud.OWNCLOUD_DATA_DIR) |
---|
223 | |
---|
224 | os.system("mv %s/ADMIN_USER %s/%s"%(LliurexOwncloud.OWNCLOUD_DATA_DIR,LliurexOwncloud.OWNCLOUD_DATA_DIR,self.template["ADMIN_USER"])) |
---|
225 | |
---|
226 | cmd="cp %s %s"%(LliurexOwncloud.HTACCESS,"/var/www/owncloud/") |
---|
227 | os.system(cmd) |
---|
228 | |
---|
229 | return [True,""] |
---|
230 | |
---|
231 | #def copy_new_files |
---|
232 | |
---|
233 | def process_config_file(self): |
---|
234 | |
---|
235 | print("* Procesing config template...") |
---|
236 | |
---|
237 | template_dir=LliurexOwncloud.OWNCLOUD_CONFIG_DIR |
---|
238 | template_file="config.php" |
---|
239 | |
---|
240 | tpl_env = Environment(loader=FileSystemLoader(template_dir)) |
---|
241 | template = tpl_env.get_template(template_file) |
---|
242 | content = template.render(self.template).encode('UTF-8') |
---|
243 | |
---|
244 | f=open(template_dir+template_file,"w") |
---|
245 | f.write(content) |
---|
246 | f.close() |
---|
247 | |
---|
248 | return [True,""] |
---|
249 | |
---|
250 | #def process_config_file |
---|
251 | |
---|
252 | def enable_easy_site(self): |
---|
253 | |
---|
254 | print("* Enabling easy site...") |
---|
255 | |
---|
256 | if os.path.exists(LliurexOwncloud.EASY_SITES_DIR): |
---|
257 | shutil.copy(LliurexOwncloud.EASY_SITE,LliurexOwncloud.EASY_SITES_DIR) |
---|
258 | |
---|
259 | return [True,""] |
---|
260 | |
---|
261 | #def enable_easy_site |
---|
262 | |
---|
263 | def enable_cname(self): |
---|
264 | |
---|
265 | ''' |
---|
266 | template_dir=LliurexOwncloud.OWNCLOUD_BASE_DIR |
---|
267 | tpl_env = Environment(loader=FileSystemLoader(template_dir)) |
---|
268 | template = tpl_env.get_template(LliurexOwncloud.CNAME) |
---|
269 | |
---|
270 | content = template.render(self.template).encode('UTF-8') |
---|
271 | f=open("/var/lib/dnsmasq/config/cname-owncloud","w") |
---|
272 | f.write(content) |
---|
273 | f.close() |
---|
274 | |
---|
275 | ''' |
---|
276 | |
---|
277 | f=open("/etc/n4d/key","r") |
---|
278 | magic_key=f.readline().strip("\n") |
---|
279 | f.close() |
---|
280 | import xmlrpclib as x |
---|
281 | c=x.ServerProxy("https://server:9779") |
---|
282 | result = c.set_internal_dns_entry(magic_key,"Dnsmasq","owncloud") |
---|
283 | if result['status'] == True: |
---|
284 | os.system("service dnsmasq restart") |
---|
285 | return [True,""] |
---|
286 | else: |
---|
287 | return [False,result['msg']] |
---|
288 | |
---|
289 | |
---|
290 | #def enable_cname |
---|
291 | |
---|
292 | def enable_apache_conf(self): |
---|
293 | |
---|
294 | os.system("a2enmod ldap") |
---|
295 | os.system("a2enmod ssl") |
---|
296 | os.system("a2enmod rewrite") |
---|
297 | os.system("a2enmod headers") |
---|
298 | os.system("a2ensite default-ssl.conf") |
---|
299 | os.system("service apache2 restart") |
---|
300 | os.system("a2ensite owncloud.conf") |
---|
301 | os.system("a2enconf owncloud") |
---|
302 | os.system("service apache2 reload") |
---|
303 | |
---|
304 | #def enable_apache_mods |
---|
305 | |
---|
306 | |
---|
307 | def initialize_owncloud(self,template): |
---|
308 | |
---|
309 | try: |
---|
310 | |
---|
311 | status,ret=self.load_template(template) |
---|
312 | if not status: |
---|
313 | return [False,ret +"1"] |
---|
314 | |
---|
315 | status,ret=self.mysql_service_init() |
---|
316 | if not status: |
---|
317 | return [False,ret+"3"] |
---|
318 | |
---|
319 | status,ret=self.create_db_user() |
---|
320 | if not status: |
---|
321 | return [False,ret+"4"] |
---|
322 | |
---|
323 | status,ret=self.create_db() |
---|
324 | if not status: |
---|
325 | return [False,ret+"5"] |
---|
326 | |
---|
327 | status,ret=self.clean_old_files() |
---|
328 | if not status: |
---|
329 | return [False,ret+"6"] |
---|
330 | |
---|
331 | status,ret=self.copy_new_files() |
---|
332 | if not status: |
---|
333 | return [False,ret+"7"] |
---|
334 | |
---|
335 | status,ret=self.process_config_file() |
---|
336 | if not status: |
---|
337 | return [False,ret+"8"] |
---|
338 | |
---|
339 | status,ret=self.enable_easy_site() |
---|
340 | if not status: |
---|
341 | return [False,ret+"9"] |
---|
342 | |
---|
343 | status,ret=self.enable_cname() |
---|
344 | if not status: |
---|
345 | return [False,ret+"10"] |
---|
346 | |
---|
347 | self.enable_apache_conf() |
---|
348 | |
---|
349 | return [True,"SUCCESS"] |
---|
350 | |
---|
351 | except Exception as e: |
---|
352 | |
---|
353 | return [False,str(e)+" ?"] |
---|
354 | |
---|
355 | #def initlializ_owncloud |
---|
356 | |
---|
357 | |
---|
358 | #class LliurexOwncloud |
---|
359 | |
---|
360 | if __name__=="__main__": |
---|
361 | |
---|
362 | lo=LliurexOwncloud() |
---|
363 | lo.parse_template("/home/netadmin/owncloud.ini") |
---|
364 | lo.mysql_service_init() |
---|
365 | lo.create_db_user() |
---|
366 | lo.create_db() |
---|
367 | lo.clean_old_files() |
---|
368 | lo.copy_new_files() |
---|
369 | lo.process_config_file() |
---|
370 | lo.enable_easy_site() |
---|
371 | lo.enable_apache_conf() |
---|
372 | |
---|
373 | |
---|
374 | |
---|
375 | |
---|