X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=fabfile.py;h=ef39c4ae159ee516793ca83a523ec6159741419a;hb=1e9fdbf37e6c258927e53ba48702aed04ebe983d;hp=b54cd52e03d9c4a99b790a3acbfaf7b112af2493;hpb=6787f2bbd0252cc5cc2a921c39b842e826e60ac4;p=oweals%2Fkarmaworld.git diff --git a/fabfile.py b/fabfile.py index b54cd52..ef39c4a 100644 --- a/fabfile.py +++ b/fabfile.py @@ -9,78 +9,51 @@ from fabric.api import cd, env, lcd, prefix, run, sudo, task, local, settings from fabric.contrib import files ######### GLOBAL -env.user = 'vagrant' -env.group = 'vagrant' +env.group = 'www-data' env.proj_repo = 'git@github.com:FinalsClub/karmaworld.git' -env.repo_root = '~/karmaworld' +env.repo_root = '~/karmaworld' # transient setting for VMs only env.proj_root = '/var/www/karmaworld' -env.branch = 'prod' +env.branch = 'prod' # only used for supervisor conf two lines below. cleanup? env.code_root = env.proj_root -env.env_root = env.proj_root env.supervisor_conf = '{0}/confs/{1}/supervisord.conf'.format(env.code_root, env.branch) +env.usde_csv = '{0}/confs/acceditation.csv'.format(env.code_root) -######## Define host(s) -def here(): - """ - Connection information for the local machine - """ - def _custom_local(command): - prefixed_command = '/bin/bash -l -i -c "%s"' % command - return local(prefixed_command) - - # This is required for the same reason as above - env.proj_root = '/var/www/karmaworld' - env.cd = lcd - env.reqs = 'reqs/dev.txt' - env.confs = 'confs/beta' - env.branch = 'beta' - - - -####### Define production host -@task -def prod(): - """ - Connection Information for production machine - """ - - env.user = 'djkarma' - env.hosts = ['karmanotes.org'] - env.proj_root = '/var/www/karmaworld' - env.reqs = 'reqs/prod.txt' - env.confs = 'confs/prod/' - env.branch = 'beta' - env.gunicorn_addr = '127.0.0.1:8000' - -####### Define beta host -@task -def beta(): - """ - Connection Information for beta machine - """ - - env.user = 'djkarma' - env.hosts = ['beta.karmanotes.org'] - env.proj_root = '/var/www/karmaworld' - env.reqs = 'reqs/prod.txt' - env.confs = 'confs/prod/' - env.branch = 'beta' +env.use_ssh_config = True -######## Run Commands in Virutal Environment +######## Run Commands in Virtual Environment def virtenv_path(): """ - Builds the virtualenv path. - """ - # not much here now, but maybe useful later. - return env.env_root + Find and memoize the virtualenv for use internally. + """ + default_venv = env.proj_root + '/venv/bin/activate' + + # Return environment root if its been memoized + if 'env_root' in env and env['env_root']: + return env['env_root'] + + # Not memoized. Try to find a single unique virtual environment. + outp = run("find -L {0} -path '*/bin/activate' | grep -v '/local/'".format(env.proj_root)) + if not len(outp) or len(outp.splitlines()) != 1: + # Cannot find any virtualenv or found multiple virtualenvs. + if len(outp) and default_venv not in outp: + # Multiple venvs and the default is not present. + raise Exception('Cannot determine the appropriate virtualenv.') + # If there are no virtualenvs, then use the default (this will create + # one if being called by make_virtualenv, otherwise it will cause an + # error). + # If there are multiple virtualenvs and the default is in their midst, + # use the default. + outp = default_venv + # Pop off the /bin/activate from /venv/bin/activate + outp = os.path.sep.join(outp.split(os.path.sep)[:-2]) + env['env_root'] = outp + return outp def virtenv_exec(command): """ Execute command in Virtualenv """ - - path = virtenv_path() - with prefix('source {0}/bin/activate'.format(path)): + with prefix('source {0}/bin/activate'.format(virtenv_path())): run(command) ######## Sync database @@ -99,7 +72,7 @@ def collect_static(): Collect static files (if AWS config. present, push to S3) """ - virtenv_exec('%s/manage.py collectstatic --noinput' % env.code_root ) + virtenv_exec('{0}/manage.py collectstatic --noinput'.format(env.code_root)) ####### Run Dev Server @task @@ -108,7 +81,7 @@ def dev_server(): Runs the built-in django webserver """ - virtenv_exec('%s/manage.py runserver' % env.code_root) + virtenv_exec('{0}/manage.py runserver'.format(env.code_root)) ####### Create Virtual Environment @@ -146,10 +119,9 @@ def stop_supervisord(): @task def restart_supervisord(): """ - Restarts supervisord + Restarts supervisord, also making sure to load in new config data. """ - stop_supervisord() - start_supervisord() + virtenv_exec('supervisorctl -c {0} update; supervisorctl -c {0} restart all'.format(env.supervisor_conf)) def supervisorctl(action, process): @@ -212,12 +184,12 @@ def restart_gunicorn(): ####### Update Requirements @task def update_reqs(): - virtenv_exec('pip install -r {0}/reqs/{1}.txt'.format(env.repo_root, env.branch)) + virtenv_exec('pip install -r {0}/reqs/prod.txt'.format(env.code_root)) ####### Pull new code @task def update_code(): - virtenv_exec('cd %s; git pull' % env.proj_root ) + virtenv_exec('cd {0}; git pull'.format(env.code_root)) def backup(): """ @@ -243,7 +215,7 @@ def file_setup(): # touch a file and change ownership if needed if 'log' in option and not files.exists(filepath): sudo('touch {0}'.format(filepath)) - sudo('chown {0}:{1} {2}'.format(env.user, env.group, filepath)) + sudo('chown {0}:{1} {2}'.format(env.local_user, env.group, filepath)) @task def check_secrets(): @@ -252,7 +224,7 @@ def check_secrets(): """ secrets_path = env.code_root + '/karmaworld/secret' - secrets_files = ('filepicker.py', 'static_s3.py', 'db_settings.py', 'drive.py', 'client_secrets.json') + secrets_files = ('filepicker.py', 'static_s3.py', 'db_settings.py', 'drive.py', 'client_secrets.json', 'drive.p12') errors = [] for sfile in secrets_files: @@ -262,6 +234,53 @@ def check_secrets(): if errors: raise Exception('\n'.join(errors)) +@task +def fetch_usde(): + """ + Download USDE accreditation school CSV. + """ + virtenv_exec('{0}/manage.py fetch_usde_csv {1}'.format(env.code_root, env.usde_csv)) + +@task +def import_usde(): + """ + Import accreditation school CSV into the database and scrub it. + """ + virtenv_exec('{0}/manage.py import_usde_csv {1}'.format(env.code_root, env.usde_csv)) + virtenv_exec('{0}/manage.py sanitize_usde_schools'.format(env.code_root)) + +@task +def install_pdf2htmlEX(): + """ + # Some things we need: + sudo apt-get install cmake libpng-dev libjpeg-dev libgtk2.0-dev pkg-config libfontconfig1-dev autoconf libtool + + # Ubuntu 12.04 comes with a version of poppler that is too + # old, so compile our own + wget http://poppler.freedesktop.org/poppler-0.24.4.tar.xz + tar xf poppler-0.24.4.tar.gz + ./configure --prefix=/usr --enable-xpdf-headers + make + sudo make install + + # Ubuntu 12.04 comes with a version of fontforge that is too + # old, so compile our own + git clone https://github.com/fontforge/fontforge.git + ./autogen.sh + ./configure --prefix=/usr + make + sudo make install + + # Compile pdf2htmlEX + wget https://github.com/coolwanglu/pdf2htmlEX/archive/v0.10.tar.gz + tar xf x0.10.tar.gz + cd pdf2htmlEX + cmake . + make + sudo make install + """ + print "not implemented yet!" + @task def first_deploy(): """ @@ -273,6 +292,9 @@ def first_deploy(): check_secrets() update_reqs() syncdb() + collect_static() + fetch_usde() + import_usde() start_supervisord()