Commits

Denis Khabarov committed 16311d7

new file: ngxensite

Comments (0)

Files changed (1)

+#!/usr/bin/env python
+#-*- coding: utf8 -*-
+"""
+##########################################################################
+ ngxensite - script for quick virtual host enabling
+
+ Copyright © 2009-2012 Denis 'Saymon21' Khabarov
+ E-Mail: <saymon@hub21.ru>
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License version 3
+ as published by the Free Software Foundation.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program.  If not, see <http://www.gnu.org/licenses/>.
+##########################################################################
+"""
+import os, sys, subprocess 
+
+NGINX_CONFIG_DIR="/usr/local/etc/nginx"
+NGINX_BIN="/usr/local/sbin/nginx"
+
+def test_config():
+	shell=("%s -t -c %s/nginx.conf" %(NGINX_BIN, NGINX_CONFIG_DIR))
+	child = subprocess.Popen(shell, shell = True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=None)
+	streamdata = child.communicate()
+	return child.returncode, "".join(map(str, streamdata))
+
+def main():
+	if len(sys.argv) < 2:
+		print('Usage: %s site' % sys.argv[0])
+		sys.exit(1)
+	hostname=sys.argv[1]
+	source="%s/sites-available/%s.conf" % (NGINX_CONFIG_DIR, hostname)
+	link_name="%s/sites-enabled/%s" %(NGINX_CONFIG_DIR, hostname)
+	if os.path.isfile(source) == False:
+		print("Config file '%s' doesn't exists!" % source)
+		sys.exit(1)
+	if os.path.islink(link_name):
+		print("Virtual host '%s' already running!" % hostname)
+		sys.exit(1)
+	try:
+		os.symlink(source, link_name)
+	except OSError as errstr:
+		print("Error when activating the virtual host '%s': %s" %(hostname,errstr))
+		sys.exit(1)
+
+	code, ngxres = test_config()
+	if code == 1:
+		print('nginx config-test error:\n'+ngxres)
+		sys.exit(1)
+	elif code == 0:
+		print("Virtual host '%s' has been activated successfully!\nRun '/etc/init.d/nginx reload' to enable changes" % (hostname))
+		sys.exit(0)
+	
+
+if __name__ == "__main__":
+	main()