Skip to content

Instantly share code, notes, and snippets.

@Ragnarokkr
Created July 11, 2018 16:18
Show Gist options
  • Select an option

  • Save Ragnarokkr/ecb5b9e0d5ccba90b88da5677bd56b5b to your computer and use it in GitHub Desktop.

Select an option

Save Ragnarokkr/ecb5b9e0d5ccba90b88da5677bd56b5b to your computer and use it in GitHub Desktop.
Simple apache-like website manager for nginx web server.
#! /usr/bin/env python3
"""nginx-site-manager.py
Simple apache-like website manager for nginx web server.
@author Marco Trulla <marco.trulla@gmail.com>
@version 0.1
@license MIT
"""
import os
import argparse
# Common paths used by nginx web server
NGINX_PATH = '/etc/nginx'
NGINX_SITES_AVAILABLE = NGINX_PATH + '/sites-available'
NGINX_SITES_ENABLED = NGINX_PATH + '/sites-enabled'
# Program messages
OPT_HELP = 'Simple nginx websites config manager'
OPT_HELP_ENABLE = 'website(s) to enable'
OPT_HELP_DISABLE = 'website(s) to disable'
OPT_HELP_FORCE = 'force to perform the specified action'
OPT_HELP_LIST = 'list all available websites and their current status'
OPT_HELP_DRYRUN = 'simulate the action without make changes to the system'
ERR_NO_WEBSITE = 'It is required to specify which website(s) to {}'
ERR_ACTION = 'Unable to {} the website "{}": {}'
MSG_ENABLE = 'enable'
MSG_DISABLE = 'disable'
MSG_ENABLED = 'enabled'
MSG_DISABLED = 'disabled'
MSG_ALREADY_ENABLED = 'already enabled'
MSG_ALREADY_DISABLED = 'already disabled'
MSG_AVAIL_WEBSITES = 'Available website(s):\n====================='
MSG_INFO = 'Please, reload/restart the nginx service in order to make effective every changes.'
def options():
"""
Configure command-line options and arguments
"""
parser = argparse.ArgumentParser(description=OPT_HELP)
parser.add_argument('--version', action='version', version='%(prog)s v0.1')
parser.add_argument('--dry-run', action='store_true', help=OPT_HELP_DRYRUN)
parser.add_argument('-d', '--disable', nargs='+', default='', help=OPT_HELP_DISABLE)
parser.add_argument('-e', '--enable', nargs='+', default='', help=OPT_HELP_ENABLE)
parser.add_argument('-f', '--force', action='store_true', help=OPT_HELP_FORCE)
parser.add_argument('-l', '--list', action='store_true', help=OPT_HELP_LIST)
return parser.parse_args()
def enableWebsite(website, force, dryrun):
"""
Enables a website.
Parameters:
-----------
website: str
website's config filename to enable
force: bool
force the enabling operation
dryrun: bool
simulate the enabling operation for testing
"""
src = '{}/{}'.format(NGINX_SITES_AVAILABLE, website)
dest = '{}/{}'.format(NGINX_SITES_ENABLED, website)
isAvailable = os.path.exists(src)
isEnabled = os.path.exists(dest)
try:
if isAvailable and (not isEnabled or force):
if not dryrun:
os.symlink(src, dest)
print('{} -> {}'.format(website, MSG_ENABLED))
else:
print('{} -> {}'.format(website, MSG_ALREADY_ENABLED))
except IOError as e:
print(ERR_ACTION.format(MSG_ENABLE, website, e.strerror))
exit(1)
def disableWebsite(website, force, dryrun):
"""
Disable a website.
Parameters:
-----------
website: str
website's config filename to disable
force: bool
force the disabling operation
dryrun: bool
simulate the disabling operation for testing
"""
dest = '{}/{}'.format(NGINX_SITES_ENABLED, website)
isEnabled = os.path.exists(dest)
try:
if isEnabled or force:
if not dryrun:
os.unlink(dest)
print('{} -> {}'.format(website, MSG_DISABLED))
else:
print('{} -> {}'.format(website, MSG_ALREADY_DISABLED))
except IOError as e:
print(ERR_ACTION.format(MSG_DISABLE, website, e.strerror))
exit(1)
def listWebsites():
"""
List the available website's config files and their current status
"""
print(MSG_AVAIL_WEBSITES)
for entry in os.scandir(NGINX_SITES_AVAILABLE):
if not entry.name.startswith('.') and entry.is_file():
dest = '{}/{}'.format(NGINX_SITES_ENABLED, entry.name)
print('{} ({})'.format(entry.name, MSG_ENABLED if os.path.exists(dest) else MSG_DISABLED))
def main():
args = options()
if args.list:
print('\n')
listWebsites()
print('\n')
exit(0)
if args.enable:
print('\n')
if type(args.enable) == str:
enableWebsite(args.enable, args.force, args.dry_run)
print(MSG_INFO + '\n')
exit(0)
elif type(args.enable) == list:
for website in args.enable:
enableWebsite(website, args.force, args.dry_run)
print(MSG_INFO + '\n')
exit(0)
else:
print(ERR_NO_WEBSITE.format(MSG_ENABLE))
print('\n')
exit(1)
if args.disable:
print('\n')
if type(args.disable) == str:
disableWebsite(args.disable, args.force, args.dry_run)
print(MSG_INFO + '\n')
exit(0)
elif type(args.disable) == list:
for website in args.disable:
disableWebsite(website, args.force, args.dry_run)
print(MSG_INFO + '\n')
exit(0)
else:
print(ERR_NO_WEBSITE.format(MSG_DISABLE))
print('\n')
exit(1)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment