Created
February 27, 2013 23:12
-
-
Save juntalis/5052720 to your computer and use it in GitHub Desktop.
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
| import os | |
| from shutil import copyfile | |
| from site import addsitedir | |
| currFolder = os.path.dirname(__file__) | |
| addsitedir(currFolder) | |
| # Some variables. Too lazy to do a proper | |
| # config file. Sorry. | |
| pathLogs = 'C:/ShellEnv/j-tree/sbin/www/apache/logs/' | |
| pathVhosts = 'C:\\ShellEnv\\j-tree\\sbin\\www\\apache\\config\\vhosts' | |
| import web | |
| urls = ( | |
| '/', 'index', | |
| '.+reboot/?', 'reboot', | |
| '.+check/?', 'check', | |
| ) | |
| class index: | |
| def debugHtml(self,strv): | |
| return '<html style="width:100%;height:100%;"><body style="width:100%;height:100%;"><pre style="font:normal 12px Consolas;width:100%;height:100%;">' + strv.replace('<','<').replace('>','>') + '</pre></body></html>' | |
| def getPPrint(self,obj): | |
| import pprint, StringIO | |
| s = StringIO.StringIO() | |
| pprint.pprint(obj, s) | |
| return s.getvalue() | |
| def setHeaders(self): | |
| web.ctx.headers.append(('Content-Type','text/html')) | |
| def openFolder(self,pFolder): | |
| if not os.path.exists(pFolder): | |
| return | |
| import subprocess | |
| p = subprocess.Popen(['explorer.exe', pFolder], | |
| stdout=subprocess.PIPE, | |
| stderr=subprocess.STDOUT) | |
| def GET(self): | |
| self.setHeaders() | |
| siteFull = open(os.path.join(currFolder, 'views', 'master.html'), 'r').read() | |
| return siteFull.replace('{{content}}', open(os.path.join(currFolder, 'views', 'main.html'), 'r').read()) | |
| def POST(self): | |
| # Set to return text/html | |
| self.setHeaders() | |
| # Get the input. | |
| dataRequest = web.input() | |
| hostDomain = dataRequest.get('host_domain') | |
| subDomain = dataRequest.get('sub_domain') | |
| pathFolder = dataRequest.get('dir_path') | |
| useTemplate = dataRequest.get('gen_template') == 'true' | |
| localLogs = dataRequest.get('local_logs') == 'true' | |
| if localLogs: | |
| pathLogs = os.path.join(pathFolder,'logs') | |
| if not os.path.exists(pathLogs): os.makedirs(pathLogs) | |
| pathLogs += '/' | |
| # Create the new virtual host. | |
| ## Write template vhost configuration to file. | |
| pathVhostNew = os.path.join(pathVhosts, '%s.conf' % subDomain) | |
| contentsVhost = str(open(os.path.join(currFolder, 'views', 'vhost_template'), 'r').read())\ | |
| .replace('{{subdomain}}',subDomain)\ | |
| .replace('{{pathLogs}}', pathLogs)\ | |
| .replace('{{folder}}',pathFolder)\ | |
| .replace('{{hostname}}',hostDomain) | |
| open(pathVhostNew, 'w').write("%s\n" % contentsVhost) | |
| if useTemplate: | |
| ## Make the folder structure | |
| assetsFolder = os.path.join(pathFolder,'assets') | |
| os.makedirs(os.path.join(assetsFolder, 'img')) | |
| os.makedirs(os.path.join(assetsFolder, 'js')) | |
| assetsFolder = os.path.join(assetsFolder, 'css') | |
| os.makedirs(assetsFolder) | |
| ## Copy files over. | |
| tCss = os.path.join(currFolder, 'views', 'css') | |
| copyfile(os.path.join(tCss,'screen.css'), os.path.join(assetsFolder, 'screen.css')) | |
| copyfile(os.path.join(tCss,'screen.css'), os.path.join(assetsFolder, 'print.css')) | |
| copyfile(os.path.join(tCss,'screen.css'), os.path.join(assetsFolder, 'ie.css')) | |
| copyfile(os.path.join(currFolder, 'views', 'index_template'), os.path.join(pathFolder, 'index.html')) | |
| # Add the new domain to hosts file. | |
| newDomain = '%s.%s' % (subDomain,hostDomain) | |
| hostsFiles = os.path.join(os.getenv('SystemRoot'),'system32','drivers','etc','hosts') | |
| fileHosts = open(hostsFiles, 'r') | |
| currHosts = "%s127.0.0.1\t%s\r\n" % (str(fileHosts.read()), newDomain) | |
| open(hostsFiles, 'w').write(currHosts) | |
| # Open the new virtual host. | |
| self.openFolder(os.path.abspath(pathFolder)) | |
| # Return output. | |
| siteFull = str(open(os.path.join(currFolder, 'views', 'master.html'), 'r').read())\ | |
| .replace('{{content}}', open(os.path.join(currFolder,'views','results.html'),'r').read()\ | |
| .replace('__hostname__', newDomain)) | |
| return siteFull | |
| class reboot: | |
| def GET(self): | |
| import subprocess | |
| batchScript = str(os.path.abspath(os.path.join(currFolder, 'restart.bat'))) | |
| p = subprocess.Popen(['cmd.exe', '/C', batchScript], | |
| stdout=subprocess.PIPE, | |
| stderr=subprocess.STDOUT) | |
| return 'OK' | |
| class check: | |
| def POST(self): | |
| import urllib | |
| newHost = web.input().get('hostname') | |
| res = urllib.urlopen(newHost).getcode() | |
| if res == 200: | |
| return 'OK' | |
| else: | |
| return 'NO' | |
| application = web.application(urls, globals()).wsgifunc() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment