Aller au contenu. | Aller à la navigation

Outils personnels

Navigation

Vous êtes ici : Accueil / Wiki / makebuild

makebuild

makebuild.py — text/python-source, 11 kB (11 803 bytes)

Contenu du fichier

""" script de creation du buildout pour la prod ageliaco
developer : Serge Renfer
2009-12-03
**Introduction**
"""

import csv
import os, stat
import string



class vhost:
    def __init__(self, host,plone,domain,https=False):
        self.host = host
        self.plone = plone
        self.domain = domain
        self.https = https
        
class inst:
    def __init__(self, nom,port, domain, index):
        self.nom = nom
        self.port = port
        self.domain = domain
        self.index = index
        self.hosts = {}
    def add_host(self, host,plone, domain):
        https = False
        if 'https' in host:
            hset = host.split('/')
            https = True
            host = hset[-1]
        if host not in self.hosts:
            self.hosts[host] = vhost(host,plone,domain,https)
            
class awstat_host:
    def __init__(self, host):
        self.host = host
            
class awstat_domain:
    def __init__(self,domain,buildout_path):
        self.domain = domain
        self.log = "%s/var/log/%s-access.log"%(buildout_path,domain)
        self.hosts = {}
    def add_host(self, host):
        if host not in self.hosts:
            self.hosts[host] = awstat_host(host)

def makebuildout (project_path, cvs_file, awstats_conf_path, index_path="/var/www"):
    # generate variables in buildout and awstats conf file if it doesn't exist
    
    ### variables pour les differents fichiers de sortie  ###
    #########################################################
    
    """
    format du fichier csv : nom,port,domain,host,plone
    
    
    fichiers generes :
	instances.cfg      => une section [host-parameters] par host different ex:
		
        [host-parameters]
        port = 12001
        host = www.sismondi.ch
        plone = /sismondi
        name = instance1
	
	varsetting.cfg      => en partant de init-varsetting.cfg remplacer les variables:
	                                                            vh_targets
	    une ligne par host :     host:host-parameters (vhs_targets si host est https)

	                                                            instances_targets
	    une ligne par instance : instance:host-parameters (en prenant un des host de cette instance)

	                                                            supervisor
	    une ligne par instance : 
	        ex: 
	            20 _instance_ ${buildout:directory}/parts/_instance_/bin/runzope true ${users:zope}
	            
	                                                            logrotate
	    une ligne par instance : 
	        ex:
	            _instance_-logrotate:_instance_-parameters
	                                                            rotate_log
	    une ligne par instance : 
	        ex:
	            _instance_-logrotate:log
	                                                            eventlistener
	    une ligne par instance : 
	        ex:
     Instance1-HttpOk TICK_60 ${buildout:bin-directory}/httpok [-m serge.renfer@gmail.com -p instance1 http://localhost:12001]
	                                                            
	main.cfg        => config nginx => en partant de init-main.cfg remplacer les variables:
	                                                            vh_nginx
	    une ligne par host :     host:nginx

	varnish.cfg    => config varnish => en partant de init-varnish.cfg remplacer les variables:
	                                                            vh_varnish
	    une ligne par host :     host:varnish
	"""
	
    # pour generer un fichier main.cfg a partir de init-main.cfg
    vh_nginx = '' 
    
    # pour generer un fichier varnish.cfg a partir de init-varnish.cfg
    vh_varnish = ''
    
    # pour generer un fichier varsetting.cfg a partir de init-varsetting.cfg
    vh_targets = ''
    vhs_targets = ''
    instances_targets = ''
    backup_targets = ''
    cron_targets = ''
    copy_targets = ''
    supervisor = ''
    eventlistener = ''
    logrotate = ''
    rotate_log = ''
    stat_chown = "\nchown -R www-data /var/lib/awstats/"
    supervisor_conf = "\t20 %s ${buildout:directory}/bin/%s [console] true ${users:zope}\n"
    # pour generer un fichier instances.cfg
    inst_struct = "[%s-parameters]\nport = %s\ndomain = %s\nname = %s\nindex = %s\n"
    vhost_struct = "[%s-parameters]\nport = %s\ndomain = %s\nhost = %s\nplone = %s\nserverport = %s\nssl = %s\n"
    inst_cfg = ''
    event_conf = "\t%s-HttpOk TICK_60 ${buildout:bin-directory}/httpok [-m serge.renfer@gmail.com -p %s http://localhost:%s]\n"
    statlink = "<a href='http://stats.ageliaco.org/awstats/awstats.pl?config=%s'>%s</a>"
    cron_line = "perl /home/admin/zope/awstats/wwwroot/cgi-bin/awstats.pl -config=%s -update >/dev/null"
    update_stat = ""
    #########################################################
    
    ### lecture du fichier csv
    
    
    ### iteration sur les champs                        #####
    ### avec generation des variables necessaires       #####
    #########################################################
    
    aReader = csv.reader(open(cvs_file))
    instances = {}
    domains = {}
    stats = {}
    for index,row in enumerate(aReader):
        # print ' : '.join(row)
        nom,port,domain,host,plone = row
        # print "nom = %s, port = %s, domain = %s, host = %s, plone =%s"%(nom,port,domain,host,plone) 
        if nom not in instances:
            instance = inst(nom,port,domain,index)
            instance.add_host(host,plone,domain)
            instances[nom] =instance
        else:
            instance = instances[nom]
            instance.add_host(host,plone,domain)
        if domain not in domains:
            dom = awstat_domain(domain,project_path)
            domains[domain] = dom
        else:
            dom = domains[domain]
        dom.add_host(host)
            
    for instance in instances.values():
        # print "%s, nom= %s, domain = %s"%(instance.port, instance.nom, instance.domain)
        # creation des variables qui sont au niveau de l'instance
        # soit : structure_instance instances_targets supervisor logrotate rotate_log
        #
        # instances.cfg : structure_instance 
        inst_cfg += inst_struct%(instance.nom,instance.port,instance.domain,instance.nom, instance.index)
        # varsetting.cfg : instances_targets : instance:host-parameters
        instances_targets += "\t%s:%s-parameters\n"%(instance.nom,instance.nom)
        
        # varsetting.cfg : backup_targets : instance:host-parameters
        backup_targets += "\tbackup-%s:%s-parameters\n"%(instance.nom,instance.nom)
        
        # varsetting.cfg : cron_targets : instance:host-parameters
        cron_targets += "\tcron-%s:%s-parameters\n"%(instance.nom,instance.nom)
        
        # varsetting.cfg : copy_targets : instance:host-parameters
        copy_targets += "\tcopy-%s:%s-parameters\n"%(instance.nom,instance.nom)
        
        # varsetting.cfg : supervisor
        supervisor += supervisor_conf%(instance.nom,instance.nom)
        
        # varsetting.cfg : eventlistener
        eventlistener += event_conf%(instance.nom,instance.nom, instance.port)
        
        # varsetting.cfg : logrotate
        logrotate += "\t%s-logrotate:%s-parameters\n"%(instance.nom,instance.nom)
        
        # varsetting.cfg : rotate_log
        rotate_log += "\t%s-logrotate:log\n"%instance.nom
        
        for host in instance.hosts.values():
            # print "\t%s => %s sur le plone : %s"%(host.domain,host.host,host.plone)
            # creation des variables qui sont au niveau du host
            # soit : structure_vhost vh_targets vh_nginx vh_varnish
            #
            # instances.cfg : structure_vhost 
            if host.https:
                inst_cfg += vhost_struct%(host.host,instance.port,host.domain,host.host,host.plone,'443','${buildout:ssl}')
            else:
                inst_cfg += vhost_struct%(host.host,instance.port,host.domain,host.host,host.plone,'80','')
            
            # varsetting.cfg : vh_targets : instance:host-parameters
            vh_targets += "\t%s:%s-parameters\n"%(host.host,host.host)
            
            # main.cfg : vh_nginx
            vh_nginx += "\t${%s:nginx}\n"%host.host
            
            # varnish.cfg : vh_varnish
            vh_varnish += "\t${%s:varnish}\n"%host.host
         
    # print inst_cfg
    # print 'vh_nginx = ', vh_nginx
    # print 'vh_varnish = ', vh_varnish
    f = open(project_path + '/instances.cfg','w')
    f.write(inst_cfg)
    f.close()
    
    main = open(project_path + '/init-main.cfg').read()
    main = main.replace('$$$vh_nginx',vh_nginx)
    f = open(project_path + '/main.cfg','w')
    f.write(main)
    f.close()
    
    varnish = open(project_path + '/init-varnish.cfg').read()
    varnish = varnish.replace('$$$vh_varnish',vh_varnish)
    f = open(project_path + '/varnish.cfg','w')
    f.write(varnish)
    f.close()
    
    varsetting = open(project_path + '/init-varsetting.cfg').read()
    varsetting = varsetting.replace('$$$vh_targets',vh_targets)
    varsetting = varsetting.replace('$$$instances_targets',instances_targets)
    varsetting = varsetting.replace('$$$backup_targets',backup_targets)
    varsetting = varsetting.replace('$$$cron_targets',cron_targets)
    varsetting = varsetting.replace('$$$copy_targets',copy_targets)
    varsetting = varsetting.replace('$$$supervisor',supervisor)
    varsetting = varsetting.replace('$$$eventlistener',eventlistener)
    varsetting = varsetting.replace('$$$logrotate',logrotate)
    varsetting = varsetting.replace('$$$rotate_log',rotate_log)
    f = open(project_path + '/varsetting.cfg','w')
    f.write(varsetting)
    f.close()
    
    # generation des fichiers de configuration des statistiques
    for domain in domains.values():
        hosts = "%s "%domain.domain
        for host in domain.hosts.values():
            hosts = hosts + "%s "%host.host
        awstat_conf_text = open(awstats_conf_path+'/init-awstats.conf').read()
        awstat_conf_text = awstat_conf_text.replace('$$$domain',domain.domain)
        awstat_conf_text = awstat_conf_text.replace('$$$hosts',hosts)
        awstat_conf_text = awstat_conf_text.replace('$$$logfile',domain.log)
        f = open(awstats_conf_path+ '/awstats.' + domain.domain+ '.conf','w')
        f.write(awstat_conf_text)
        f.close()
        
        # setting index for links to stats
        stats[domain.domain] = statlink % (domain.domain,domain.domain)
        aLine = cron_line % domain.domain
        update_stat = update_stat + aLine + "\n"
        
    html_stat = "<ul>\n"
    for domainlink in stats.values():
        html_stat += "<li> " + domainlink + "</li>\n"
    html_stat += "</ul>\n"
    index_text = open(index_path+'/init-index.html').read()
    index_text = index_text.replace('$$$stats',html_stat)
    f = open(index_path + '/%s-%s.html'%(project_path.split('/')[-2],project_path.split('/')[-1]), 'w')
    f.write(index_text)
    f.close()
    
    # file for cron job
    f = open(project_path + "/updatestats.sh", 'w')
    update_stat += stat_chown
    f.write(update_stat)
    f.close()
    os.chmod(project_path + "/updatestats.sh", stat.S_IRWXU)
    
    # generation du fichier pour le packing des bases
    # creation d'un fichier contenant les commandes a effectuer
    # ce fichier est a mettre en cron
    packcmd = '/usr/bin/wget  --http-user=admin --http-passwd=zopeadmin08 --post-data=days:float=7 -O /dev/null http://localhost:%s/Control_Panel/Database/main/manage_pack\n'
    packset = '#!/bin/sh\n'
    
    for instance in instances.values():
        packset += packcmd % instance.port
        
    f = open(project_path + '/packing','w')
    f.write(packset)
    f.close()
    os.chmod(project_path + "/packing", stat.S_IRWXU)