Last active
December 13, 2015 19:48
-
-
Save alejandrobernardis/4965259 to your computer and use it in GitHub Desktop.
NGINX, Add Site
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| # | |
| # Copyright (c) 2013 Asumi Kamikaze Inc. | |
| # Copyright (c) 2013 The Octopus Apps Inc. | |
| # Licensed under the Apache License, Version 2.0 (the "License") | |
| # | |
| # Author: Alejandro M. Bernardis | |
| # Email: alejandro.m.bernardis at gmail.com | |
| # Created: 2/114/13, 12:32 AM | |
| # imports | |
| try: | |
| import os, sys | |
| from optparse import OptionParser | |
| except: | |
| exit(u'Module not found.') | |
| class Config(): | |
| BASE_PATH = '' | |
| PATH_SITES_AVAILABLE = BASE_PATH + '/etc/nginx/sites-available' | |
| PATH_SITES_ENABLED = BASE_PATH + '/etc/nginx/sites-enabled' | |
| class Process(): | |
| def __init__(self, args, opts): | |
| self.pid = 0 | |
| self.error = u'success' | |
| self.args = args | |
| self.opts = opts | |
| self.site = None | |
| self.get_sites_list() | |
| def get_source(self, value): | |
| value = value.replace('.conf','') | |
| return '%s/%s.conf' % (Config.PATH_SITES_AVAILABLE, value) | |
| def get_link(self, value): | |
| value = value.replace('.conf','') | |
| return '%s/%s.conf' % (Config.PATH_SITES_ENABLED, value) | |
| def get_sites_list(self): | |
| self.sites = [] | |
| for root, dirs, files in os.walk(Config.PATH_SITES_AVAILABLE): | |
| if not len(files): | |
| exit(u'The directory is empty.') | |
| for name in files: | |
| rename = name.replace('.conf','') | |
| link = self.get_link(rename) | |
| if not os.path.islink(link): | |
| self.sites.append(rename) | |
| if not len(self.sites): | |
| exit(u'All sites are enabled.') | |
| self.sites.sort() | |
| def print_sites_list(self): | |
| print u'# Configurations:' | |
| i = 1 | |
| for site in self.sites: | |
| print i, site | |
| i += 1 | |
| print '--' | |
| def run(self): | |
| if self.opts.list: | |
| self.print_sites_list() | |
| message = u'Please, select your option --> ' | |
| action = raw_input(message.encode('utf-8')) | |
| try: | |
| action = int(action)-1 | |
| if action < 0 or action > len(self.sites): | |
| raise | |
| self.site = self.sites[action] | |
| except: | |
| exit(u'Operation Canceled.') | |
| print u'Config choice: `%s`' % self.site | |
| elif args[0] in self.sites: | |
| self.site = args[0] | |
| print u'Config found: `%s`' % args[0] | |
| else: | |
| exit(u'Site not found.') | |
| self.src = self.get_source(self.site) | |
| self.link = self.get_link(self.site) | |
| if os.path.islink(self.link): | |
| exit(u'Site `%s` already exists.' % self.site) | |
| if not self.opts.force: | |
| message = u'Are you sure to proceed? [y/n] --> ' | |
| action = raw_input(message.encode('utf-8')) | |
| if str(action) not in ['y','Y']: | |
| exit(u'Operation Canceled.') | |
| os.symlink(self.src, self.link) | |
| if os.path.islink(self.link): | |
| print u'Site `%s` was successfully added.' % self.site | |
| cmd = os.popen('service nginx reload') | |
| print cmd.readline() | |
| if __name__ == '__main__': | |
| try: | |
| parser = OptionParser() | |
| parser.add_option('-l', '--list', action='store_true', dest='list', default=False) | |
| parser.add_option('-f', '--force', action='store_true', dest='force', default=False) | |
| opts, args = parser.parse_args() | |
| if not len(args) and not opts.list: | |
| message = u'Sorry, but the "arguments" and "options" are empty.\nUsage: %s -h' | |
| exit(message % __file__) | |
| pid = Process(args, opts) | |
| pid.run() | |
| except KeyboardInterrupt: | |
| exit(u'\nAu revoir.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment