Skip to content

Instantly share code, notes, and snippets.

@erayaydin
Last active September 27, 2020 22:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erayaydin/99e11985c380c42c26da to your computer and use it in GitHub Desktop.
Save erayaydin/99e11985c380c42c26da to your computer and use it in GitHub Desktop.
Virtual Host Manager for Arch Linux
#!/usr/bin/python
import getopt, sys, os, shutil
def main(argv):
mode = ''
name = ''
try:
opts, args = getopt.getopt(argv, "hm:n:", ["mod=", "name="])
except getopt.GetoptError:
print("test.py -m <mode> -n <name>")
sys.exit(2)
for opt, arg in opts:
if opt == "-h":
print("test.py -m <mode> -n <name>")
sys.exit()
elif opt in ("-m", "--mod"):
mode = arg
elif opt in ("-n", "--name"):
name = arg
if mode in ("add", "a"):
if(os.path.exists("/srv/http/{0}".format(name))):
print("Project folder already exists!")
else:
print("Creating project folder...")
os.mkdir("/srv/http/{0}".format(name))
if(os.path.exists("/srv/http/{0}/public_html".format(name))):
print("public_html folder already exists!")
else:
print("Creating public_html folder...")
os.mkdir("/srv/http/{0}/public_html".format(name))
if(os.path.exists("/etc/httpd/conf/vhosts/{0}.conf".format(name))):
print("virtual host file already exists!")
else:
print("Creating virtual host file...")
vhostfile = open("/etc/httpd/conf/vhosts/{0}.conf".format(name), "w+")
hostConf = "<VirtualHost *:80>\nServerAdmin webmaster@{0}\nDocumentRoot \"/srv/http/{0}/public_html\"\nServerName {0}\nServerAlias www.{0}\nErrorLog \"/var/log/httpd/{0}-error log\"\nCustomLog \"/var/log/httpd/{0}-access log\" common\n</VirtualHost>"
vhostfile.write(hostConf.format(name))
vhostfile.close()
print("Adding to hosts file...")
hostsfile = open("/etc/hosts", "a")
hostsFileText = "\n127.0.0.1 {0} www.{0}"
hostsfile.write(hostsFileText.format(name))
hostsfile.close()
print("Restarting httpd service...")
os.system("systemctl restart httpd")
elif mode in ("remove", "r"):
print("Removing project folder...")
shutil.rmtree("/srv/http".format(name))
if(os.path.exists("/etc/httpd/conf/vhosts/{0}.conf".format(name))):
print("Removing virtual host file...")
os.remove("/etc/httpd/conf/vhosts/{0}.conf".format(name))
else:
print("Virtual host file doesn't exists!")
elif mode in ("install", "i"):
print("Installing...")
httpdFile = open("/etc/hosts", "a")
includeOptional = "IncludeOptional conf/vhosts/*.conf"
httpdFile.write(includeOptional)
httpdFile.close()
if(not os.path.exists("/etc/httpd/conf/vhosts")):
os.mkdir("/etc/httpd/conf/vhosts")
print("DONE!")
if __name__ == "__main__":
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment