Ejemplo n.º 1
0
def _update_virtualenv(source_folder):
    virtualenv_folder = source_folder + '/..'
    if not exists(virtualenv_folder + '/bin/pip'):  #1
        run('virtualenv --python=python2.7 %s' % (virtualenv_folder, ))
    sudo('%s/bin/pip install -r %s/requirements.txt' % (  #2
        virtualenv_folder, source_folder))
    run('source %s/bin/activate' % (virtualenv_folder))
Ejemplo n.º 2
0
def deploy_test():
    _get_latest_source(source_folder)
    _update_virtualenv(test_source_folder)
    # _update_settings(test_source_folder, 'settings_test.py')
    run('source /srv/www/live/foodtrade-env/bin/activate && cd /srv/www/live/foodtrade-env/foodtrade && sudo python manage.py collectstatic --noinput'
        )
    sudo('sudo supervisorctl restart all')
Ejemplo n.º 3
0
def deploy():
    # _create_directory_structure_if_necessary(HOST_FOLDER)
    _get_latest_source(source_folder)
    _update_settings(source_folder)
    run('cd /srv/www/meroanswer/source/ && source ../virtualenv/bin/activate && python manage.py collectstatic'
        )
    sudo('reload %s' % (HOST_FOLDER))
Ejemplo n.º 4
0
def config_celery(config_dir):
    config_instance = Config(config_dir)

    celery_configs = config_instance.config_path('celery', 'systemd')
    remote_conf_path = '/etc/systemd/system/'

    upload_template('celeryd',
                    os.path.join(
                        remote_conf_path,
                        '{}.service'.format(config_instance.celeryd_name)),
                    template_dir=celery_configs,
                    context=config_instance.context,
                    use_sudo=True,
                    use_jinja=True)
    sudo('systemctl reenable {}.service'.format(config_instance.celeryd_name))

    upload_template('celerybeat',
                    os.path.join(
                        remote_conf_path,
                        '{}.service'.format(config_instance.celerybeat_name)),
                    template_dir=celery_configs,
                    context=config_instance.context,
                    use_sudo=True,
                    use_jinja=True)
    sudo('systemctl reenable {}.service'.format(
        config_instance.celerybeat_name))
Ejemplo n.º 5
0
def _nginx_config(source_folder, site_name):
    run('cd %s && '
        'sed "s/SITENAME/%s/g" '
        'deploy_tools/nginx.template.conf '
        '| '
        'sudo tee /etc/nginx/sites-available/%s' % (source_folder,site_name, site_name))

    if not exists('/etc/nginx/sites-enabled/%s' % site_name):
        sudo('ln -s /etc/nginx/sites-available/%s /etc/nginx/sites-enabled/%s' % (site_name, site_name))
Ejemplo n.º 6
0
def _run_upstart():
    upstart_file = "/etc/init/%s.conf" % (HOST_FOLDER)
    sudo('cp %s/deploy_tools/gunicon-upstart.template.conf %s' %
         (source_folder, upstart_file))

    sed(upstart_file, "SITENAME", HOST_FOLDER, use_sudo=True)  #1
    sed(upstart_file, "SITES_FOLDER", SITES_FOLDER, use_sudo=True)  #1
    sed(upstart_file, "PROJECT_NAME", PROJECT_NAME, use_sudo=True)  #1
    sed(upstart_file, "DOMAIN_NAME", DOMAIN_NAME, use_sudo=True)  #1
    sudo('rm %s.bak' % (upstart_file))
Ejemplo n.º 7
0
 def build_server_file(self):
     with cd(self.sys_deploy_path):
         rm_all_except()
         # 5.4 解压
         sudo('tar -zxf ' + self.name + '.tar.gz --strip-components 1',
              quiet=True)  # 解压完了是web下面的东西
         sudo('rm ' + self.name + '.tar.gz')
     t_mkdir(self.sys_deploy_path + 'logs')
     t_mkdir(self.sys_deploy_path + 'tmp')
     t_chmod(self.sys_deploy_path + 'logs', '777', '-R')
     t_chmod(self.sys_deploy_path + 'tmp', '777', '-R')
Ejemplo n.º 8
0
def deploy_staging():
    # _create_directory_structure_if_necessary(HOST_FOLDER)
    _get_latest_source(source_folder)
    _update_virtualenv(source_folder)
    settings_server_file = 'settings_stagingserver.py'
    # run('cd %s && git reset --hard && git clean -f -d && git checkout master && git pull && git pull origin master' % (source_folder))
    _update_virtualenv(source_folder)
    settings_path = source_folder + '/' + PROJECT_NAME + '/settings.py'
    settings_server_path = source_folder + '/' + PROJECT_NAME + '/' + settings_server_file
    run("sudo touch %s" % (settings_path))
    run("sudo rm %s" % (settings_path))
    run('sudo cp %s %s' % (settings_server_path, settings_path))
    run("sudo " + source_folder + '/../bin/python ' + source_folder +
        '/manage.py collectstatic --noinput')
    sudo('sudo supervisorctl restart all')
Ejemplo n.º 9
0
def _run_nginx():

    sites_available = '/etc/nginx/sites-available/%s' % (HOST_FOLDER)
    sites_enabled = '/etc/nginx/sites-enabled/%s' % (HOST_FOLDER)
    sudo('cp %s/deploy_tools/nginx.template.conf %s' %
         (source_folder, sites_available))
    sed(sites_available, "SITENAME", DOMAIN_NAME, use_sudo=True)  #1
    sed(sites_available, "SITES_FOLDER", SITES_FOLDER, use_sudo=True)  #1
    sed(sites_available, "HOST_FOLDER", HOST_FOLDER, use_sudo=True)  #1
    sudo("rm %s.bak" % (sites_available))

    sudo("ln -s %s %s" % (sites_available, sites_enabled))
    run('mkdir -p %s/%s/logs' % (SITES_FOLDER, HOST_FOLDER))
    run('cd %s/%s && touch nginx-access.log' % (SITES_FOLDER, HOST_FOLDER))
    run('cd %s/%s && touch nginx-error.log' % (SITES_FOLDER, HOST_FOLDER))
    sudo('service nginx reload')
    sudo('service nginx restart')
Ejemplo n.º 10
0
def _generate_secret_key(source_folder, secret_key_file):

    settings_file = '{0}/{1}/staging.py'.format(source_folder, SETTINGS_FOLDER)

    if exists(secret_key_file):

        get(local_path='/tmp/secret_key.txt', remote_path=secret_key_file)
        tmp_key_path = '/tmp/secret_key.txt'

        with open(tmp_key_path, 'r') as key_file:
            data = key_file.read().replace('\n', '')

        # tmp_key_file = data

        if data is not '':
            append(settings_file, '\nSECRET_KEY = "{0}"'.format(data))

        sudo('rm -rf /tmp/secret_key.txt')

    else:

        print(
            "[localhost] print: Remote key file does not exist. Making one now."
        )
        chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
        generated_key = ''.join(
            [random.SystemRandom().choice(chars) for _ in range(50)])

        append(settings_file, '\nSECRET_KEY = "{0}"'.format(generated_key))

        secret_key_file = os.path.join('/tmp/', 'secret_key.txt')

        with open(secret_key_file, 'r+') as text_file:
            # TODO -- this can be run with Python 2.7 print statement.
            text_file.write('{0}'.format(generated_key))

            # TODO -- the below requires `from __future__ import print_function` to work.
            # print('{0}'.format(generated_key), file=text_file)

            put(use_sudo=True,
                local_path=text_file,
                remote_path='/etc/prv/{0}/secret_key.txt'.format(PROJECT))
            sudo('rm -rf /tmp/secret_key.txt')
Ejemplo n.º 11
0
def _generate_secret_key(source_folder, secret_key_file):

    settings_file = '{0}/{1}/staging.py'.format(source_folder, SETTINGS_FOLDER)

    if exists(secret_key_file):

        get(local_path='/tmp/secret_key.txt', remote_path=secret_key_file)
        tmp_key_path = '/tmp/secret_key.txt'

        with open(tmp_key_path, 'r') as key_file:
            data = key_file.read().replace('\n', '')

        # tmp_key_file = data

        if data is not '':
            append(settings_file, '\nSECRET_KEY = "{0}"'.format(data))

        sudo('rm -rf /tmp/secret_key.txt')

    else:

        print("[localhost] print: Remote key file does not exist. Making one now.")
        chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
        generated_key = ''.join([random.SystemRandom().choice(chars) for _ in range(50)])

        append(settings_file, '\nSECRET_KEY = "{0}"'.format(generated_key))

        secret_key_file = os.path.join('/tmp/', 'secret_key.txt')

        with open(secret_key_file, 'r+') as text_file:
            # TODO -- this can be run with Python 2.7 print statement.
            text_file.write('{0}'.format(generated_key))

            # TODO -- the below requires `from __future__ import print_function` to work.
            # print('{0}'.format(generated_key), file=text_file)

            put(use_sudo=True, local_path=text_file, remote_path='/etc/prv/{0}/secret_key.txt'.format(PROJECT))
            sudo('rm -rf /tmp/secret_key.txt')
Ejemplo n.º 12
0
def update_latest_code():
    settings_server_file = 'settings_server.py'
    if push_type == 'staging':
        settings_server_file = 'settings_staging_server.py'
    run('cd %s && git reset --hard && git clean -f -d && git checkout master && git pull && git pull origin master'
        % (source_folder))
    _update_virtualenv(source_folder)
    settings_path = source_folder + '/' + PROJECT_NAME + '/settings.py'
    settings_server_path = source_folder + '/' + PROJECT_NAME + '/' + settings_server_file
    run("touch %s" % (settings_path))
    run("rm %s" % (settings_path))
    run('cp %s %s' % (settings_server_path, settings_path))
    # run ("cd %s && python manage.py collectstatic"%(source_folder))
    _update_static_files(source_folder)
    _update_database(source_folder)
    # _run_gunicorn()
    sudo("service nginx reload")
    sudo("service nginx restart")
    sudo("reload %s" % (HOST_FOLDER))
Ejemplo n.º 13
0
def _restart_service(host):
    sudo('systemctl daemon-reload')
    sudo(f'systemctl restart gunicorn-{host}.service')
Ejemplo n.º 14
0
def _restart_servers(http_server, uwsgi_server):

    sudo('restart {0} && restart {1}'.format(http_server, uwsgi_server))
Ejemplo n.º 15
0
def reboot():
    sudo("reboot")
Ejemplo n.º 16
0
def _start_services(site_name):
    sudo('service nginx reload')
    with settings(warn_only=True):
        sudo('stop gunicorn-%s' % site_name)
    sudo('start gunicorn-%s' % site_name)
Ejemplo n.º 17
0
def _install_dependencies():
    sudo('apt-get install -y nginx git python3 python3-pip')
Ejemplo n.º 18
0
def _install_package_from_apt():
    sudo('apt-get install git')
    sudo('apt-get install python-pip')
    sudo('pip install virtualenv')
    sudo('apt-get install nginx')
    sudo('apt-get install python-dev')
Ejemplo n.º 19
0
def _install_virtualenv():
    sudo('pip3 install virtualenv')
Ejemplo n.º 20
0
def install_rabbitmq(config_dir):
    config_instance = Config(config_dir)

    sudo('echo "deb http://www.rabbitmq.com/debian/ testing main" |'
         ' tee  /etc/apt/sources.list.d/rabbitmq.list > /dev/null')
    with cd('/tmp'):
        run('wget -O rabbitmq-release-signing-key.asc'
            ' http://www.rabbitmq.com/rabbitmq-release-signing-key.asc')
        sudo('apt-key add rabbitmq-release-signing-key.asc')
    sudo('apt-get update')
    sudo('apt-get -y install rabbitmq-server')
    sudo('service rabbitmq-server start')

    systemd_override_dir = '/etc/systemd/system/rabbitmq-server.service.d'
    sudo('mkdir -p {}'.format(systemd_override_dir))
    systemd_override = os.path.join(systemd_override_dir, 'override.conf')
    upload_template('override',
                    systemd_override,
                    template_dir=config_instance.config_path('rabbitmq'),
                    context=config_instance.context,
                    use_sudo=True,
                    use_jinja=True)
    sudo('systemctl daemon-reload')
Ejemplo n.º 21
0
def _restart_servers(http_server, uwsgi_server):

    sudo('restart {0} && restart {1}'.format(http_server, uwsgi_server))