-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
80 lines (69 loc) · 2.48 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python
import jinja2
from config import *
import logging
import os
def get_template(template_name):
try:
template_file = open("%s/%s" % (TEMPLATE_DIR, template_name))
template = jinja2.Template(template_file.read())
return template
except IOError:
raise jinja2.TemplateNotFound
def write_config(dest_file_path, ready_config):
try:
dest_file = open(dest_file_path, 'w')
dest_file.write(ready_config)
dest_file.close()
except IOError, exc:
logging.error("I/O Error. %s" % exc)
raise
logging.debug("Config written to file %s" % dest_file_path)
def new_vhost(config_template, params, httpd="nginx"):
"""
Main Vhost-add procedure.
"""
if httpd=="apache":
available_dir = APACHE_CONFIG_DIR
else:
available_dir = NGINX_CONFIG_DIR
config = get_template(config_template)
ready_config = config.render(params)
logging.debug("Template %s with parameters %s rendered." % (
config_template,
params
)
)
logging.debug("Writing config into new file")
dest_file_path = "%s/%s.conf" % (available_dir, params['domain'])
write_config(dest_file_path, ready_config)
def enable_vhost(domain_name, httpd="nginx"):
if httpd=="apache":
enabled_dir = APACHE_ENABLED_DIR
available_dir = APACHE_CONFIG_DIR
else:
enabled_dir = NGINX_ENABLED_DIR
available_dir = NGINX_CONFIG_DIR
logging.debug("Enabling vhost %s" % domain_name)
enabled_path = "%s/%s.conf" % (enabled_dir, domain_name)
available_path = "%s/%s.conf" % (available_dir, domain_name)
os.symlink(available_path, enabled_path)
logging.debug("Symlink done")
def disable_vhost(domain_name, delete=False, httpd="nginx"):
if httpd=="apache":
enabled_dir = APACHE_ENABLED_DIR
available_dir = APACHE_CONFIG_DIR
else:
enabled_dir = NGINX_ENABLED_DIR
available_dir = NGINX_CONFIG_DIR
logging.debug("Disabling vhost %s on %s" % (domain_name, httpd))
enabled_path = "%s/%s.conf" % (enabled_dir, domain_name)
os.unlink(enabled_path)
logging.debug("Symlink deleted")
if delete:
logging.debug("Deleting vhost %s on %s" % (domain_name, httpd))
available_path = "%s/%s.conf" % (available_dir, domain_name)
os.unlink(available_path)
logging.debug("Config deleted")
def delete_vhost(domain_name, httpd="nginx"):
disable_vhost(domain_name, delete=True, httpd=httpd)