<s> """<STR_LIT>""" <EOL> import sys <EOL> import os <EOL> import time <EOL> if sys . version_info < ( <NUM_LIT:2> , <NUM_LIT:6> , <NUM_LIT:0> ) : <EOL> print ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> elif sys . version_info >= ( <NUM_LIT:3> , <NUM_LIT:0> , <NUM_LIT:0> ) : <EOL> sys . stderr . write ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> sys . path . insert ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> sys . path . insert ( <NUM_LIT:0> , os . getcwd ( ) ) <EOL> import shutil <EOL> import optparse <EOL> import requests <EOL> import subprocess <EOL> try : <EOL> import instances_creator_conf as icc <EOL> import psycopg2 <EOL> except ImportError : <EOL> sys . stderr . write ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:3> ) <EOL> os . environ [ '<STR_LIT>' ] = icc . DB_PASSWORD <EOL> class AskbotInstance ( ) : <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> if not os . environ [ '<STR_LIT>' ] == '<STR_LIT:root>' : <EOL> self . abort ( '<STR_LIT>' ) <EOL> sys . path . insert ( <NUM_LIT:0> , icc . DEFAULT_INSTANCE_DIR ) <EOL> def _populate_file ( self , original_file , values ) : <EOL> """<STR_LIT>""" <EOL> f = open ( original_file , '<STR_LIT:r>' ) <EOL> file_content = f . read ( ) <EOL> f . close ( ) <EOL> populated_settings = file_content . format ( ** values ) <EOL> f = open ( original_file , '<STR_LIT:w>' ) <EOL> f . write ( populated_settings ) <EOL> f . close ( ) <EOL> def create_instance ( self , instance_name , instance_db_name ) : <EOL> """<STR_LIT>""" <EOL> INSTANCE_DIR = os . path . join ( icc . DEFAULT_INSTANCE_DIR , instance_name ) <EOL> try : <EOL> shutil . copytree ( icc . SKEL_DIR , INSTANCE_DIR ) <EOL> os . chdir ( INSTANCE_DIR ) <EOL> template = os . path . join ( INSTANCE_DIR , '<STR_LIT>' ) <EOL> os . symlink ( <EOL> os . path . join ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> os . path . join ( INSTANCE_DIR , '<STR_LIT>' ) ) <EOL> values = { <EOL> '<STR_LIT>' : instance_name , <EOL> '<STR_LIT>' : instance_db_name , <EOL> '<STR_LIT>' : icc . DB_HOST , <EOL> '<STR_LIT>' : icc . BASE_URL <EOL> } <EOL> self . _populate_file ( template , values ) <EOL> print ( '<STR_LIT>' . format ( instance_name ) ) <EOL> except : <EOL> self . abort ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def create_db ( self , instance_db_name ) : <EOL> """<STR_LIT>""" <EOL> createdb = subprocess . Popen ( ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % ( instance_db_name , <EOL> icc . DB_USER ) , shell = True ) <EOL> createdb . wait ( ) <EOL> try : <EOL> psycopg2 . connect ( <EOL> database = instance_db_name , <EOL> user = icc . DB_USER , <EOL> password = icc . DB_PASSWORD , <EOL> host = icc . DB_HOST <EOL> ) <EOL> print ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( instance_db_name ) ) <EOL> except : <EOL> self . abort ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def syncdb_and_migrate ( self , instance_name ) : <EOL> """<STR_LIT>""" <EOL> working_dir = os . path . join ( icc . DEFAULT_INSTANCE_DIR , instance_name ) <EOL> os . chdir ( working_dir ) <EOL> syncdb = subprocess . Popen ( ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , shell = True ) <EOL> syncdb . wait ( ) <EOL> def collect_static ( seld , instance_name ) : <EOL> """<STR_LIT>""" <EOL> working_dir = os . path . join ( icc . DEFAULT_INSTANCE_DIR , instance_name ) <EOL> os . chdir ( working_dir ) <EOL> collectstatic = subprocess . Popen ( ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> shell = True ) <EOL> collectstatic . wait ( ) <EOL> def add_instance_to_supervisor ( self , instance_name ) : <EOL> """<STR_LIT>""" <EOL> INSTANCE_DIR = os . path . join ( icc . DEFAULT_INSTANCE_DIR , instance_name ) <EOL> try : <EOL> template = os . path . join ( INSTANCE_DIR , '<STR_LIT>' ) <EOL> values = { <EOL> '<STR_LIT>' : instance_name , <EOL> '<STR_LIT>' : INSTANCE_DIR <EOL> } <EOL> self . _populate_file ( template , values ) <EOL> os . symlink ( template , os . path . join ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' % instance_name ) ) <EOL> print ( '<STR_LIT>' ) <EOL> except : <EOL> self . abort ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def add_instance_to_nginx ( self , instance_name ) : <EOL> """<STR_LIT>""" <EOL> INSTANCE_DIR = os . path . join ( icc . DEFAULT_INSTANCE_DIR , instance_name ) <EOL> try : <EOL> template = os . path . join ( INSTANCE_DIR , '<STR_LIT>' ) <EOL> values = { '<STR_LIT>' : instance_name } <EOL> self . _populate_file ( template , values ) <EOL> template = os . path . join ( INSTANCE_DIR , '<STR_LIT>' ) <EOL> values = { '<STR_LIT>' : instance_name } <EOL> self . _populate_file ( template , values ) <EOL> print ( '<STR_LIT>' ) <EOL> except : <EOL> self . abort ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def restart_server ( self ) : <EOL> supervisord = subprocess . Popen ( ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , shell = True ) <EOL> supervisord . wait ( ) <EOL> nginx = subprocess . Popen ( ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , shell = True ) <EOL> nginx . wait ( ) <EOL> time . sleep ( <NUM_LIT:5> ) <EOL> def update_entries_metadata ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> update_metadata = subprocess . Popen ( ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> shell = True ) <EOL> update_metadata . wait ( ) <EOL> url = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % icc . META_REFRESH_KEY <EOL> requests . get ( url ) <EOL> except : <EOL> self . abort ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def disable_instance ( self , instance_name ) : <EOL> """<STR_LIT>""" <EOL> INSTANCE_DIR = os . path . join ( icc . DEFAULT_INSTANCE_DIR , instance_name ) <EOL> try : <EOL> if not os . path . isdir ( icc . DEFAULT_DISABLED_INSTANCES_DIR ) : <EOL> os . makedirs ( icc . DEFAULT_DISABLED_INSTANCES_DIR ) <EOL> shutil . copy ( INSTANCE_DIR , icc . DEFAULT_DISABLED_INSTANCES_DIR ) <EOL> shutil . rmtree ( INSTANCE_DIR ) <EOL> print ( '<STR_LIT>' . format ( instance_name ) ) <EOL> except : <EOL> self . abort ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def destroy_instance ( self , instance_name ) : <EOL> """<STR_LIT>""" <EOL> INSTANCE_DIR = os . path . join ( icc . DEFAULT_INSTANCE_DIR , instance_name ) <EOL> sys . path . insert ( <NUM_LIT:0> , INSTANCE_DIR ) <EOL> try : <EOL> import instance_settings <EOL> except : <EOL> self . abort ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> try : <EOL> instance_db_name = instance_settings . DATABASE_NAME <EOL> dropdb = subprocess . Popen ( '<STR_LIT>' % <EOL> instance_db_name , shell = True ) <EOL> dropdb . wait ( ) <EOL> shutil . rmtree ( INSTANCE_DIR ) <EOL> os . remove ( os . path . join ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' % instance_name ) ) <EOL> print ( '<STR_LIT>' . format ( instance_name ) ) <EOL> except : <EOL> self . abort ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def abort ( self , msg , status = <NUM_LIT:1> ) : <EOL> sys . stderr . write ( msg ) <EOL> sys . exit ( status ) <EOL> parser = optparse . OptionParser ( <EOL> description = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> version = '<STR_LIT>' <EOL> ) <EOL> parser . add_option ( <EOL> '<STR_LIT:-c>' , <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> action = '<STR_LIT:store>' , <EOL> nargs = <NUM_LIT:2> <EOL> ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> dest = '<STR_LIT>' <EOL> ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> dest = '<STR_LIT>' <EOL> ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> dest = '<STR_LIT>' <EOL> ) <EOL> ( opts , args ) = parser . parse_args ( ) <EOL> inst = AskbotInstance ( ) <EOL> if opts . instance_data : <EOL> INSTANCE_NAME = opts . instance_data [ <NUM_LIT:0> ] <EOL> INSTANCE_DB_NAME = opts . instance_data [ <NUM_LIT:1> ] <EOL> inst . create_instance ( INSTANCE_NAME , INSTANCE_DB_NAME ) <EOL> inst . add_instance_to_supervisor ( INSTANCE_NAME ) <EOL> inst . add_instance_to_nginx ( INSTANCE_NAME ) <EOL> inst . create_db ( INSTANCE_DB_NAME ) <EOL> inst . syncdb_and_migrate ( INSTANCE_NAME ) <EOL> inst . collect_static ( INSTANCE_NAME ) <EOL> inst . restart_server ( ) <EOL> if not opts . no_metadata : <EOL> inst . update_entries_metadata ( ) <EOL> elif opts . disable_instance_name : <EOL> INSTANCE_NAME = opts . disable_instance_name <EOL> inst . disable_instance ( INSTANCE_NAME ) <EOL> elif opts . destroy_instance_name : <EOL> INSTANCE_NAME = opts . destroy_instance_name <EOL> inst . destroy_instance ( INSTANCE_NAME ) </s>
