<s> import ConfigParser <EOL> import os <EOL> import random <EOL> import re <EOL> import string <EOL> from getpass import getpass <EOL> from argyle import postgres , nginx , system <EOL> from argyle . supervisor import supervisor_command , upload_supervisor_app_conf <EOL> from argyle . system import service_command <EOL> from fabric import utils <EOL> from fabric . api import cd , env , get , hide , local , put , require , run , settings , sudo , task <EOL> from fabric . contrib import files , console <EOL> PROJECT_ROOT = os . path . dirname ( __file__ ) <EOL> CONF_ROOT = os . path . join ( PROJECT_ROOT , '<STR_LIT>' ) <EOL> SERVER_ROLES = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> env . project = '<STR_LIT>' <EOL> env . project_user = '<STR_LIT>' <EOL> env . repo = u'<STR_LIT>' <EOL> env . shell = '<STR_LIT>' <EOL> env . disable_known_hosts = True <EOL> env . ssh_port = <NUM_LIT> <EOL> env . forward_agent = True <EOL> env . password_names = [ '<STR_LIT>' ] <EOL> env . ARGYLE_TEMPLATE_DIRS = ( <EOL> os . path . join ( CONF_ROOT , '<STR_LIT>' ) <EOL> ) <EOL> @ task <EOL> def vagrant ( ) : <EOL> env . environment = '<STR_LIT>' <EOL> env . vagrant = True <EOL> env . hosts = [ '<STR_LIT>' , ] <EOL> env . branch = '<STR_LIT>' <EOL> env . server_name = '<STR_LIT>' <EOL> setup_path ( ) <EOL> @ task <EOL> def staging ( ) : <EOL> env . environment = '<STR_LIT>' <EOL> env . vagrant = False <EOL> env . hosts = [ '<STR_LIT>' , ] <EOL> env . branch = '<STR_LIT>' <EOL> env . server_name = '<STR_LIT>' <EOL> env . port = <NUM_LIT> <EOL> setup_path ( ) <EOL> @ task <EOL> def production ( ) : <EOL> env . environment = '<STR_LIT>' <EOL> env . vagrant = False <EOL> env . hosts = [ '<STR_LIT>' , ] <EOL> env . branch = '<STR_LIT>' <EOL> env . server_name = '<STR_LIT>' <EOL> env . ssh_port = <NUM_LIT> <EOL> setup_path ( ) <EOL> def setup_path ( ) : <EOL> env . home = '<STR_LIT>' % env <EOL> env . root = os . path . join ( env . home , '<STR_LIT>' , env . environment ) <EOL> env . code_root = os . path . join ( env . root , env . project ) <EOL> env . project_root = os . path . join ( env . code_root , env . project ) <EOL> env . virtualenv_root = os . path . join ( env . root , '<STR_LIT>' ) <EOL> env . log_dir = os . path . join ( env . root , '<STR_LIT>' ) <EOL> env . db = '<STR_LIT>' % ( env . project , env . environment ) <EOL> env . vhost = '<STR_LIT>' % ( env . project , env . environment ) <EOL> env . settings = '<STR_LIT>' % env <EOL> @ task <EOL> def create_users ( ) : <EOL> """<STR_LIT>""" <EOL> ssh_dir = u"<STR_LIT>" % env . project_user <EOL> system . create_user ( env . project_user , groups = [ '<STR_LIT>' , '<STR_LIT>' , ] ) <EOL> sudo ( '<STR_LIT>' % ssh_dir ) <EOL> user_dir = os . path . join ( CONF_ROOT , "<STR_LIT>" ) <EOL> for username in os . listdir ( user_dir ) : <EOL> key_file = os . path . normpath ( os . path . join ( user_dir , username ) ) <EOL> system . create_user ( username , groups = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , ] , key_file = key_file ) <EOL> with open ( key_file , '<STR_LIT>' ) as f : <EOL> ssh_key = f . read ( ) <EOL> files . append ( '<STR_LIT>' % ssh_dir , ssh_key , use_sudo = True ) <EOL> sudo ( '<STR_LIT>' % ( env . project_user , env . project_user , ssh_dir ) ) <EOL> @ task <EOL> def configure_ssh ( ) : <EOL> """<STR_LIT>""" <EOL> ssh_config = u'<STR_LIT>' <EOL> files . sed ( ssh_config , u"<STR_LIT>" , u"<STR_LIT>" % env . ssh_port , use_sudo = True ) <EOL> files . sed ( ssh_config , u"<STR_LIT>" , u"<STR_LIT>" , use_sudo = True ) <EOL> files . append ( ssh_config , u"<STR_LIT>" , use_sudo = True ) <EOL> files . append ( ssh_config , u"<STR_LIT>" , use_sudo = True ) <EOL> service_command ( u'<STR_LIT>' , u'<STR_LIT>' ) <EOL> @ task <EOL> def install_packages ( * roles ) : <EOL> """<STR_LIT>""" <EOL> config_file = os . path . join ( CONF_ROOT , u'<STR_LIT>' ) <EOL> config = ConfigParser . SafeConfigParser ( ) <EOL> config . read ( config_file ) <EOL> for role in roles : <EOL> if config . has_section ( role ) : <EOL> if config . has_option ( role , '<STR_LIT>' ) : <EOL> for ppa in config . get ( role , '<STR_LIT>' ) . split ( '<STR_LIT:U+0020>' ) : <EOL> system . add_ppa ( ppa , update = False ) <EOL> if config . has_option ( role , '<STR_LIT>' ) : <EOL> for section in config . get ( role , '<STR_LIT>' ) . split ( '<STR_LIT:U+0020>' ) : <EOL> source = config . get ( section , '<STR_LIT:source>' ) <EOL> key = config . get ( section , '<STR_LIT:key>' ) <EOL> system . add_apt_source ( source = source , key = key , update = False ) <EOL> sudo ( u"<STR_LIT>" ) <EOL> sudo ( u"<STR_LIT>" % config . get ( role , '<STR_LIT>' ) ) <EOL> sudo ( u"<STR_LIT>" ) <EOL> @ task <EOL> def setup_server ( * roles ) : <EOL> """<STR_LIT>""" <EOL> require ( '<STR_LIT>' ) <EOL> sudo ( '<STR_LIT>' ) <EOL> roles = list ( roles ) <EOL> if roles == [ '<STR_LIT:all>' , ] : <EOL> roles = SERVER_ROLES <EOL> if '<STR_LIT>' not in roles : <EOL> roles . insert ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> install_packages ( * roles ) <EOL> if '<STR_LIT>' in roles : <EOL> if console . confirm ( u"<STR_LIT>" , default = False ) : <EOL> pg_version = postgres . detect_version ( ) <EOL> sudo ( '<STR_LIT>' % pg_version , user = '<STR_LIT>' ) <EOL> sudo ( '<STR_LIT>' % pg_version , <EOL> user = '<STR_LIT>' ) <EOL> postgres . create_db_user ( username = env . project_user ) <EOL> postgres . create_db ( name = env . db , owner = env . project_user ) <EOL> if '<STR_LIT>' in roles : <EOL> project_run ( '<STR_LIT>' % env ) <EOL> project_run ( '<STR_LIT>' % env ) <EOL> with settings ( user = env . project_user ) : <EOL> run ( '<STR_LIT>' % env ) <EOL> with cd ( env . code_root ) : <EOL> run ( '<STR_LIT>' % env ) <EOL> with settings ( hide ( '<STR_LIT>' ) , warn_only = True ) : <EOL> test_for_pip = run ( '<STR_LIT>' ) <EOL> if not test_for_pip : <EOL> sudo ( "<STR_LIT>" ) <EOL> with settings ( hide ( '<STR_LIT>' ) , warn_only = True ) : <EOL> test_for_virtualenv = run ( '<STR_LIT>' ) <EOL> if not test_for_virtualenv : <EOL> sudo ( "<STR_LIT>" ) <EOL> project_run ( '<STR_LIT>' % env . virtualenv_root ) <EOL> path_file = os . path . join ( env . virtualenv_root , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> files . append ( path_file , env . code_root , use_sudo = True ) <EOL> sudo ( '<STR_LIT>' % ( env . project_user , env . project_user , path_file ) ) <EOL> sudo ( '<STR_LIT>' ) <EOL> update_requirements ( ) <EOL> upload_supervisor_app_conf ( app_name = u'<STR_LIT>' ) <EOL> upload_supervisor_app_conf ( app_name = u'<STR_LIT>' ) <EOL> supervisor_command ( '<STR_LIT>' ) <EOL> supervisor_command ( '<STR_LIT>' % env ) <EOL> if '<STR_LIT>' in roles : <EOL> nginx . remove_default_site ( ) <EOL> nginx . upload_nginx_site_conf ( site_name = u'<STR_LIT>' % env ) <EOL> def project_run ( cmd ) : <EOL> """<STR_LIT>""" <EOL> sudo ( cmd , user = env . project_user ) <EOL> def _random_password ( length = <NUM_LIT:8> , chars = string . letters + string . digits ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' . join ( [ random . choice ( chars ) for i in range ( length ) ] ) <EOL> def _load_passwords ( names , length = <NUM_LIT:20> , generate = False ) : <EOL> """<STR_LIT>""" <EOL> for name in names : <EOL> filename = '<STR_LIT>' . join ( [ env . home , name ] ) <EOL> if generate : <EOL> passwd = _random_password ( length = length ) <EOL> sudo ( '<STR_LIT>' % filename , user = env . project_user ) <EOL> sudo ( '<STR_LIT>' % filename , user = env . project_user ) <EOL> with hide ( '<STR_LIT>' ) : <EOL> sudo ( '<STR_LIT>' % ( passwd , filename ) , user = env . project_user ) <EOL> if env . host_string and files . exists ( filename ) : <EOL> with hide ( '<STR_LIT>' ) : <EOL> passwd = sudo ( '<STR_LIT>' % filename ) . strip ( ) <EOL> else : <EOL> passwd = getpass ( '<STR_LIT>' % name ) <EOL> setattr ( env , name , passwd ) <EOL> @ task <EOL> def update_service_confs ( ) : <EOL> """<STR_LIT>""" <EOL> require ( '<STR_LIT>' ) <EOL> if not env . vagrant : <EOL> upload_newrelic_conf ( ) <EOL> upload_supervisor_app_conf ( app_name = u'<STR_LIT>' ) <EOL> upload_supervisor_app_conf ( app_name = u'<STR_LIT>' ) <EOL> nginx . upload_nginx_site_conf ( site_name = u'<STR_LIT>' % env ) <EOL> supervisor_command ( '<STR_LIT>' ) <EOL> @ task <EOL> def upload_newrelic_conf ( ) : <EOL> """<STR_LIT>""" <EOL> require ( '<STR_LIT>' ) <EOL> _load_passwords ( env . password_names ) <EOL> template = os . path . join ( CONF_ROOT , '<STR_LIT>' , '<STR_LIT>' ) <EOL> destination = os . path . join ( env . root , '<STR_LIT>' % env ) <EOL> files . upload_template ( template , destination , context = env , use_sudo = True ) <EOL> sudo ( '<STR_LIT>' % ( env . project_user , env . project_user , destination ) ) <EOL> @ task <EOL> def update_requirements ( ) : <EOL> """<STR_LIT>""" <EOL> require ( '<STR_LIT>' ) <EOL> project_run ( u'<STR_LIT>' % { <EOL> '<STR_LIT>' : env . virtualenv_root , <EOL> '<STR_LIT>' : os . path . join ( env . code_root , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : env . home , <EOL> } ) <EOL> @ task <EOL> def manage_run ( command ) : <EOL> """<STR_LIT>""" <EOL> require ( '<STR_LIT>' ) <EOL> manage_base = u"<STR_LIT>" % env <EOL> if '<STR_LIT>' not in command : <EOL> command = u"<STR_LIT>" % ( command , env . settings ) <EOL> project_run ( u'<STR_LIT>' % ( manage_base , command ) ) <EOL> @ task <EOL> def manage_shell ( ) : <EOL> """<STR_LIT>""" <EOL> manage_run ( "<STR_LIT>" ) <EOL> @ task <EOL> def syncdb ( ) : <EOL> """<STR_LIT>""" <EOL> manage_run ( '<STR_LIT>' ) <EOL> manage_run ( '<STR_LIT>' ) <EOL> @ task <EOL> def collectstatic ( ) : <EOL> """<STR_LIT>""" <EOL> manage_run ( '<STR_LIT>' ) <EOL> manage_run ( '<STR_LIT>' ) <EOL> def match_changes ( changes , match ) : <EOL> pattern = re . compile ( match ) <EOL> return pattern . search ( changes ) is not None <EOL> @ task <EOL> def deploy ( branch = None , full = False ) : <EOL> """<STR_LIT>""" <EOL> require ( '<STR_LIT>' ) <EOL> if branch is not None : <EOL> env . branch = branch <EOL> requirements = False if not full else True <EOL> migrations = False if not full else True <EOL> with cd ( env . code_root ) : <EOL> with settings ( user = env . project_user ) : <EOL> run ( '<STR_LIT>' ) <EOL> changes = run ( "<STR_LIT>" % env ) <EOL> requirements = match_changes ( changes , r"<STR_LIT>" ) <EOL> migrations = match_changes ( changes , r"<STR_LIT>" ) <EOL> if requirements or migrations : <EOL> supervisor_command ( '<STR_LIT>' % env ) <EOL> with settings ( user = env . project_user ) : <EOL> run ( "<STR_LIT>" % env ) <EOL> if requirements : <EOL> update_requirements ( ) <EOL> syncdb ( ) <EOL> elif migrations : <EOL> syncdb ( ) <EOL> collectstatic ( ) <EOL> supervisor_command ( '<STR_LIT>' % env ) <EOL> @ task <EOL> def upload_secrets ( secrets_filepath ) : <EOL> """<STR_LIT>""" <EOL> require ( '<STR_LIT>' ) <EOL> destination_file = os . path . join ( env . root , '<STR_LIT>' ) <EOL> put ( secrets_filepath , destination_file , use_sudo = True ) <EOL> sudo ( '<STR_LIT>' % ( env . project_user , env . project_user , destination_file ) ) <EOL> @ task <EOL> def get_db_dump ( clean = True ) : <EOL> """<STR_LIT>""" <EOL> require ( '<STR_LIT>' ) <EOL> dump_file = '<STR_LIT>' % env <EOL> temp_file = os . path . join ( env . home , dump_file ) <EOL> flags = '<STR_LIT>' <EOL> if clean : <EOL> flags += '<STR_LIT:c>' <EOL> sudo ( '<STR_LIT>' % ( flags , env . db , temp_file ) , user = env . project_user ) <EOL> get ( temp_file , dump_file ) <EOL> @ task <EOL> def load_db_dump ( dump_file ) : <EOL> """<STR_LIT>""" <EOL> require ( '<STR_LIT>' ) <EOL> temp_file = os . path . join ( env . home , '<STR_LIT>' % env ) <EOL> put ( dump_file , temp_file , use_sudo = True ) <EOL> sudo ( '<STR_LIT>' % ( env . db , temp_file ) , user = env . project_user ) <EOL> @ task <EOL> def reset_local_media ( ) : <EOL> """<STR_LIT>""" <EOL> require ( '<STR_LIT>' , provided_by = ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> media = os . path . join ( env . code_root , '<STR_LIT>' ) <EOL> local ( "<STR_LIT>" % <EOL> ( env . ssh_port , env . user , env . hosts [ <NUM_LIT:0> ] , media , PROJECT_ROOT ) ) <EOL> @ task <EOL> def reset_local_db ( ) : <EOL> """<STR_LIT>""" <EOL> require ( '<STR_LIT>' , provided_by = ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> question = '<STR_LIT>' '<STR_LIT>' % env <EOL> if not console . confirm ( question , default = False ) : <EOL> utils . abort ( '<STR_LIT>' ) <EOL> if env . environment == '<STR_LIT>' : <EOL> from raspberryio . settings . staging import DATABASES as remote <EOL> else : <EOL> from raspberryio . settings . production import DATABASES as remote <EOL> from raspberryio . settings . local import DATABASES as loc <EOL> local_db = loc [ '<STR_LIT:default>' ] [ '<STR_LIT>' ] <EOL> remote_db = remote [ '<STR_LIT:default>' ] [ '<STR_LIT>' ] <EOL> with settings ( warn_only = True ) : <EOL> local ( '<STR_LIT>' % local_db ) <EOL> local ( '<STR_LIT>' % local_db ) <EOL> host = '<STR_LIT>' % ( env . project_user , env . hosts [ <NUM_LIT:0> ] ) <EOL> local ( '<STR_LIT>' % ( env . ssh_port , host , remote_db , local_db ) ) </s>
