<s> import os . path <EOL> import re <EOL> import subprocess <EOL> import json <EOL> import itertools <EOL> import opus . lib . deployer <EOL> from opus . lib . deployer import DeploymentException <EOL> from opus . lib . conf import OpusConfig <EOL> import opus . project . deployment . tasks <EOL> from opus . project . deployment import database <EOL> from opus . lib . log import get_logger <EOL> log = get_logger ( ) <EOL> from django . conf import settings <EOL> from django . db import models <EOL> from django . core . validators import RegexValidator , ValidationError <EOL> import django . contrib . auth . models <EOL> id_re = re . compile ( r'<STR_LIT>' ) <EOL> validate_identifier = RegexValidator ( id_re , u"<STR_LIT>" , '<STR_LIT>' ) <EOL> class IdentifierField ( models . CharField ) : <EOL> default_validators = [ validate_identifier ] <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> kwargs [ '<STR_LIT:max_length>' ] = kwargs . get ( '<STR_LIT:max_length>' , <NUM_LIT> ) <EOL> models . CharField . __init__ ( self , * args , ** kwargs ) <EOL> class DeploymentInfo ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . dbengine = None <EOL> self . dbname = None <EOL> self . dbuser = "<STR_LIT>" <EOL> self . dbpassword = "<STR_LIT>" <EOL> self . dbhost = "<STR_LIT>" <EOL> self . dbport = "<STR_LIT>" <EOL> self . superusername = None <EOL> self . superemail = None <EOL> self . superpassword = None <EOL> def validate ( self ) : <EOL> if not ( self . dbengine and self . superusername and <EOL> self . superemail and self . superpassword ) : <EOL> raise ValidationError ( "<STR_LIT>" ) <EOL> class DeployedProject ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> name = IdentifierField ( unique = True ) <EOL> owner = models . ForeignKey ( django . contrib . auth . models . User ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( DeployedProject , self ) . __init__ ( * args , ** kwargs ) <EOL> self . _conf = None <EOL> @ property <EOL> def projectdir ( self ) : <EOL> return os . path . join ( settings . OPUS_BASE_DIR , self . name ) <EOL> @ property <EOL> def apache_conf ( self ) : <EOL> return os . path . join ( settings . OPUS_APACHE_CONFD , "<STR_LIT>" + self . name + "<STR_LIT>" ) <EOL> @ models . permalink <EOL> def get_absolute_url ( self ) : <EOL> return ( "<STR_LIT>" , <EOL> ( ) , <EOL> dict ( projectname = self . name ) ) <EOL> @ property <EOL> def serve_http ( self ) : <EOL> port = settings . OPUS_HTTP_PORT <EOL> if not port : <EOL> return <EOL> if port != <NUM_LIT> : <EOL> portline = "<STR_LIT::>" + str ( port ) <EOL> else : <EOL> portline = "<STR_LIT>" <EOL> return "<STR_LIT>" . format ( <EOL> self . name , settings . OPUS_APACHE_SERVERNAME_SUFFIX , <EOL> portline ) <EOL> @ property <EOL> def serve_https ( self ) : <EOL> port = settings . OPUS_HTTPS_PORT <EOL> if not port : <EOL> return <EOL> if port != <NUM_LIT> : <EOL> portline = "<STR_LIT::>" + str ( port ) <EOL> else : <EOL> portline = "<STR_LIT>" <EOL> return "<STR_LIT>" . format ( <EOL> self . name , settings . OPUS_APACHE_SERVERNAME_SUFFIX , <EOL> portline ) <EOL> def get_urls ( self ) : <EOL> """<STR_LIT>""" <EOL> urls = [ ] <EOL> http = self . serve_http <EOL> if http : <EOL> urls . append ( http ) <EOL> https = self . serve_https <EOL> if https : <EOL> urls . append ( https ) <EOL> return urls <EOL> def get_apps ( self ) : <EOL> """<STR_LIT>""" <EOL> for app in self . config [ '<STR_LIT>' ] : <EOL> if os . path . exists ( os . path . join ( self . projectdir , app ) ) : <EOL> yield app <EOL> @ property <EOL> def config ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _conf : <EOL> self . _conf = OpusConfig ( os . path . join ( self . projectdir , "<STR_LIT>" ) ) <EOL> return self . _conf <EOL> def save ( self , * args , ** kwargs ) : <EOL> if self . _conf : <EOL> self . _conf . save ( ) <EOL> wsgifile = os . path . join ( self . projectdir , "<STR_LIT>" , '<STR_LIT>' ) <EOL> if os . path . exists ( wsgifile ) : <EOL> os . utime ( wsgifile , None ) <EOL> super ( DeployedProject , self ) . save ( * args , ** kwargs ) <EOL> def is_active ( self ) : <EOL> return os . path . exists ( self . apache_conf ) <EOL> active = property ( is_active ) <EOL> def _verify_project ( self ) : <EOL> """<STR_LIT>""" <EOL> fullpath = self . projectdir <EOL> if not os . path . isdir ( fullpath ) : <EOL> return False <EOL> if os . path . exists ( os . path . join ( fullpath , "<STR_LIT>" ) ) : <EOL> return False <EOL> if not os . path . exists ( os . path . join ( fullpath , "<STR_LIT>" ) ) : <EOL> return False <EOL> if not os . path . exists ( os . path . join ( fullpath , "<STR_LIT>" ) ) : <EOL> return False <EOL> return True <EOL> def deploy ( self , info , active = True ) : <EOL> """<STR_LIT>""" <EOL> self . full_clean ( ) <EOL> if not self . _verify_project ( ) : <EOL> raise DeploymentException ( "<STR_LIT>" ) <EOL> d = opus . lib . deployer . ProjectDeployer ( self . projectdir ) <EOL> d . create_environment ( ) <EOL> d . secure_project ( settings . OPUS_SECUREOPS_COMMAND ) <EOL> d . configure_database ( info . dbengine , <EOL> info . dbname , <EOL> info . dbuser , <EOL> info . dbpassword , <EOL> info . dbhost , <EOL> info . dbport , <EOL> ) <EOL> d . set_paths ( ) <EOL> d . install_requirements ( settings . OPUS_SECUREOPS_COMMAND ) <EOL> d . sync_database ( info . superusername , <EOL> info . superemail , <EOL> info . superpassword , <EOL> settings . OPUS_SECUREOPS_COMMAND <EOL> ) <EOL> d . gen_cert ( settings . OPUS_APACHE_SERVERNAME_SUFFIX ) <EOL> d . setup_celery ( settings . OPUS_SECUREOPS_COMMAND , <EOL> pythonpath = self . _get_path_additions ( ) ) <EOL> if active : <EOL> self . activate ( d ) <EOL> self . save ( ) <EOL> def _get_path_additions ( self ) : <EOL> return "<STR_LIT>" . format ( <EOL> os . path . split ( opus . __path__ [ <NUM_LIT:0> ] ) [ <NUM_LIT:0> ] , <EOL> ) <EOL> def set_debug ( self , d ) : <EOL> """<STR_LIT>""" <EOL> self . config [ '<STR_LIT>' ] = bool ( d ) <EOL> self . config [ '<STR_LIT>' ] = bool ( d ) <EOL> if d : <EOL> self . config [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> else : <EOL> self . config [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> def activate ( self , d = None ) : <EOL> """<STR_LIT>""" <EOL> if not self . all_settings_set ( ) : <EOL> raise DeploymentException ( "<STR_LIT>" ) <EOL> if not d : <EOL> d = opus . lib . deployer . ProjectDeployer ( self . projectdir ) <EOL> path_additions = self . _get_path_additions ( ) <EOL> d . configure_apache ( settings . OPUS_APACHE_CONFD , <EOL> settings . OPUS_HTTP_PORT , <EOL> settings . OPUS_HTTPS_PORT , <EOL> settings . OPUS_APACHE_SERVERNAME_SUFFIX , <EOL> secureops = settings . OPUS_SECUREOPS_COMMAND , <EOL> pythonpath = path_additions , <EOL> ssl_crt = settings . OPUS_SSL_CRT , <EOL> ssl_key = settings . OPUS_SSL_KEY , <EOL> ssl_chain = settings . OPUS_SSL_CHAIN , <EOL> ) <EOL> opus . project . deployment . tasks . start_supervisord . delay ( self . projectdir ) <EOL> def deactivate ( self ) : <EOL> """<STR_LIT>""" <EOL> destroyer = opus . lib . deployer . ProjectUndeployer ( self . projectdir ) <EOL> destroyer . remove_apache_conf ( settings . OPUS_APACHE_CONFD , <EOL> secureops = settings . OPUS_SECUREOPS_COMMAND ) <EOL> destroyer . stop_celery ( <EOL> secureops = settings . OPUS_SECUREOPS_COMMAND ) <EOL> opus . project . deployment . tasks . kill_processes . apply_async ( <EOL> args = [ self . pk ] , <EOL> countdown = <NUM_LIT:5> ) <EOL> def destroy ( self ) : <EOL> """<STR_LIT>""" <EOL> destroyer = opus . lib . deployer . ProjectUndeployer ( self . projectdir ) <EOL> destroyer . remove_apache_conf ( settings . OPUS_APACHE_CONFD , <EOL> secureops = settings . OPUS_SECUREOPS_COMMAND ) <EOL> destroyer . stop_celery ( <EOL> secureops = settings . OPUS_SECUREOPS_COMMAND ) <EOL> destroyer . delete_celery ( <EOL> secureops = settings . OPUS_SECUREOPS_COMMAND ) <EOL> destroyer . delete_user ( <EOL> secureops = settings . OPUS_SECUREOPS_COMMAND ) <EOL> try : <EOL> if self . config [ '<STR_LIT>' ] [ '<STR_LIT:default>' ] [ '<STR_LIT>' ] . endswith ( "<STR_LIT>" ) and settings . OPUS_AUTO_POSTGRES_CONFIG : <EOL> database . delete_postgres ( self . name ) <EOL> except Exception , e : <EOL> log . warning ( "<STR_LIT>" , e ) <EOL> destroyer . remove_projectdir ( ) <EOL> if self . id is not None : <EOL> self . delete ( ) <EOL> def get_app_settings ( self ) : <EOL> """<STR_LIT>""" <EOL> app_settings = { } <EOL> for app in self . get_apps ( ) : <EOL> md_filename = os . path . join ( self . projectdir , app , "<STR_LIT>" ) <EOL> if not os . path . exists ( md_filename ) : <EOL> continue <EOL> with open ( md_filename , '<STR_LIT:r>' ) as md_file : <EOL> app_metadata = json . load ( md_file ) <EOL> usersettings = app_metadata . get ( "<STR_LIT>" , None ) <EOL> if not usersettings : <EOL> continue <EOL> u = [ ] <EOL> for s in usersettings : <EOL> if len ( s ) < <NUM_LIT:3> : <EOL> log . warning ( "<STR_LIT>" , s ) <EOL> continue <EOL> if not all ( isinstance ( x , basestring ) for x in s [ : <NUM_LIT:3> ] ) : <EOL> log . warning ( "<STR_LIT>" , s ) <EOL> continue <EOL> if s [ <NUM_LIT:2> ] not in ( "<STR_LIT:int>" , "<STR_LIT>" , "<STR_LIT:str>" , "<STR_LIT:string>" , "<STR_LIT:float>" , '<STR_LIT>' , '<STR_LIT:bool>' ) : <EOL> log . warning ( "<STR_LIT>" , s ) <EOL> continue <EOL> u . append ( s ) <EOL> if u : <EOL> app_settings [ app ] = u <EOL> return app_settings <EOL> def all_settings_set ( self ) : <EOL> """<STR_LIT>""" <EOL> app_settings = self . get_app_settings ( ) <EOL> for setting in itertools . chain . from_iterable ( app_settings . itervalues ( ) ) : <EOL> if setting [ <NUM_LIT:0> ] not in self . config : <EOL> return False <EOL> return True </s>
