diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/Server/Generators/Vhost.py | 96 | ||||
-rw-r--r-- | src/lib/Server/Structures/Vhost.py | 59 |
2 files changed, 155 insertions, 0 deletions
diff --git a/src/lib/Server/Generators/Vhost.py b/src/lib/Server/Generators/Vhost.py index e69de29bb..d98ee891e 100644 --- a/src/lib/Server/Generators/Vhost.py +++ b/src/lib/Server/Generators/Vhost.py @@ -0,0 +1,96 @@ +'''This module manages vhost files for bcfg2''' +__revision__ = '$Revision: 1.48 $' +#------------------------------------------- +# Script Name: vhost.py +# Script Version: 1.0 +# Date: 20 July 2005 +# Author: Scott R Behrens +# Description: opens a request file, genereates a vhost httpd.conf file, and establishes symlinks +# Revision History: +# 1.0/<20-7-2005>: orignal version +# 1.1/<20-7-2005>: now genreates multiple files based on XML document +# 1.2/<24-7-2005>: generates one file encoded in base64 +# 1.3/<02-8-2005>: almost a functional generator +#------------------------------------------- + +import sys, re, os +import base64 +from getopt import getopt, GetoptError +from socket import gethostbyname +from Bcfg2.Server.Generator import SingleXMLFileBacked +from elementtree.ElementTree import XML, ElementTree +from Bcfg2.Server.Generator import Generator, DirectoryBacked + +############################################## +# +############################################## +class VhostFile(SingleXMLFileBacked): + '''The Base file contains unstructured/independent configuration elements''' + sitesen = "/etc/apache2/sites-enabled/" + sitesav = "/etc/apache2/sites-available/" + + def __init__(self, name, fam): + self.dispatch = {} + SingleXMLFileBacked.__init__(self, name, fam) + + def Index(self): + self.store = XML(self.data) + self.http = open('/mnt/bcfg2/Vhost/default.httpd', 'r').readlines() + for server in self.store.findall("server"): + for vhost in server.findall("vhost"): + name = vhost.get('name') + self.dispatch[self.sitesav + name] = self.generateconf + self.dispatch['/etc/default/apache2'] = self.generateservice + + def generateservice(self, entry, metadata): + if [x for x in self.store.findall('server') if x.get('name') == metadata.hostname]: + entry.text='NO_START=0\n' + else: + entry.text='NO_START=1\n' + entry.attrib.update({'owner':'root', 'group':'root', 'perms':'0644'}) + + def defaultgenerate(self, entry, metadata): + if [x for x in self.store.findall('server') if x.get('name') == metadata.hostname]: + entry.set('status', 'on') + else: + entry.set('status', 'off') + + + def generateconf(self, entry, metadata): + if [server for server in self.store.findall('server') if server.get('name') == metadata.hostname]: + if [vhost for vhost in server.findall('vhost') if self.sitesav + vhost.get('name') == entry.get('name')]: + name = vhost.attrib['name'] + email = vhost.attrib['email'] + root = vhost.attrib['root'] + opt = vhost.attrib['opt'] + + if root == "Hostname": + choice = name + elif root == "Ip": + choice = gethostbyname(name) + + config = "" + for line in self.http: + line = line.replace("XXemailXX", email) + line = line.replace("XXdomainXX", name) + line = line.replace("XXchoiceXX", choice) + config+=line + entry.text = base64.encodestring(config) + entry.attrib['encoding'] = 'base64' + entry.attrib.update({'owner':'root', 'group':'root', 'perms':'0644'}) + +class Vhost(Generator): + '''This Generates the sites enabled stuff for things''' + + __name__ = 'Vhost' + __version__ = '$Id: s.Vhost.py 1.48 05/05/13 13:13:57-05:00 behrens@mcs.anl.gov $' + __author__ = 'bcfg-dev@mcs.anl.gov' + + + def __init__(self, core, datastore): + + Generator.__init__(self, core, datastore) + self.Vhost = VhostFile("%s/Vhost/Vhost.xml"%(datastore), self.core.fam) + self.repository = DirectoryBacked(self.data, self.core.fam) + self.__provides__['ConfigFile'] = self.Vhost.dispatch + self.__provides__['Service'] = {'apache2': self.Vhost.defaultgenerate} diff --git a/src/lib/Server/Structures/Vhost.py b/src/lib/Server/Structures/Vhost.py index e69de29bb..a7ef791df 100644 --- a/src/lib/Server/Structures/Vhost.py +++ b/src/lib/Server/Structures/Vhost.py @@ -0,0 +1,59 @@ +#!/usr/bin/python +#------------------------------------------- +# Script Name: vhost.py +# Script Version: 1.0 +# Date: 20 July 2005 +# Author: Scott R Behrens +# Description: opens a request file, genereates a vhost httpd.conf file, and establishes symlinks +# Revision History: +# 1.0/<20-7-2005>: orignal version +# 1.1/<20-7-2005>: now genreates multiple files based on XML document +# 1.2/<20-14-2005>: generates one file encoded in base64 +#------------------------------------------- + +import sys, re, os +from getopt import getopt, GetoptError +from elementtree.ElementTree import XML, Element +from Bcfg2.Server.Generator import SingleXMLFileBacked +from Bcfg2.Server.Structure import Structure + + +# Global Variables for paths of apache +sitesen = "/etc/apache2/sites-enabled/" +sitesav = "/etc/apache2/sites-available/" + +class VhostFile(SingleXMLFileBacked): + '''The Base file contains unstructured/independent configuration elements''' + + def Index(self): + self.meta = XML(self.data) + + def Construct(self, metadata): + self.output = Element("Bundle", name='apache-vhost', version='2.0') + self.serverlist = [] + for server in self.meta.findall("server"): + self.serverlist.append(server.attrib['name']) + if server.attrib['name'] in metadata.hostname: + for vhost in server.findall("vhost"): + name = vhost.get('name') + email = vhost.get('email') + root = vhost.get('root') + opt = vhost.get('opt') + self.output.append(XML("<SymLink name=\'" + sitesen + name + "\' to=\'" + sitesav + name + "\'/>")) + self.output.append(XML("<ConfigFile name=\'/etc/apache2/sites-available/" + name + "\' encode=\'base64\'/>")) + self.output.append(XML("<ConfigFile name=\'/etc/default/apache2\'/>")) + if [software for software in self.meta.findall('Software') if metadata.hostname in self.serverlist]: + for child in software.getchildren(): + self.output.append(child) + return [self.output] + +class Vhost(Structure): + '''This Structure is good for the pile of independent configs needed for most actual systems''' + __name__ = 'Vhost' + __version__ = '$Id: s.Vhost.py 1.15 04/12/03 10:23:33-06:00 desai@topaz.mcs.anl.gov $' + + '''Vhost creates independent clauses based on client metadata''' + def __init__(self, core, datastore): + Structure.__init__(self, core, datastore) + self.Vhost = VhostFile("%s/Vhost/Vhost.xml"%(datastore), self.core.fam) + self.Construct = self.Vhost.Construct |