~codersquid/charms/precise/python-django/fix-test-branches

« back to all changes in this revision

Viewing changes to fabfile.py

Patrick Hetu 2014-05-02 fix a wrong configuration variable name
Patrick Hetu 2014-04-30 [merge] merge with trunk
Patrick Hetu 2014-04-30 add configurable amqp vhost
Patrick Hetu 2014-04-30 merge george-edison55's readme corrections
Patrick Hetu 2014-04-29 awk to the rescue: Re-enable http pip requirements support
Patrick Hetu 2014-04-28 drop http pip requirements as I can get it to work with ansible
Patrick Hetu 2014-04-25 add rabbitmq support
Patrick Hetu 2014-04-24 fix single python-path for fabfile.py
Patrick Hetu 2014-04-23 don't make python-path starts with a colon
Patrick Hetu 2014-04-17 add a new pip_extra_args option
Patrick Hetu 2014-04-17 fix settings path in the fabfile
Patrick Hetu 2014-04-17 forgot mirror urls in the playbook
Patrick Hetu 2014-04-17 release version 6
Patrick Hetu 2014-04-17 no need for |bool when checking variables
Patrick Hetu 2014-04-17 add the pip_freeze task in the fabfile
Patrick Hetu 2014-04-17 fix fabfile for latest version of juju-core
Patrick Hetu 2014-04-16 fix configuration reading for Django task
Patrick Hetu 2014-04-16 fix mercurial branch in playbook
Patrick Hetu 2014-04-16 merge changes from Ubuntu's ci-services and add my fixes
Patrick Hetu 2014-04-16 fix configuration injection for python3
Patrick Hetu 2014-04-16 fix mongodb settings
Patrick Hetu 2014-04-16 add redis support
Patrick Hetu 2014-04-16 fix ansible playbooks
Patrick Hetu 2014-04-16 add redis support
Patrick Hetu 2014-04-09 merge and adapt python-django_ci-services branch
Patrick Hetu 2013-11-26 FIX 1226303 : Minor formatting problem in README's markdown
Patrick Hetu 2013-11-26 python3 compatiblity
Patrick Hetu 2013-11-26 use ansible for handling repos and pip
Patrick Hetu 2013-11-18 wsgi configurations that works for gunicorn and uwsgi
Patrick Hetu 2013-11-17 convert the code to use charmhelpers
Patrick Hetu 2013-09-03 mysql don't support autocommit yet
Patrick Hetu 2013-08-07 increment version number
Patrick Hetu 2013-08-07 puts pip's source in a safe place and accept remote requirements files
Patrick Hetu 2013-08-05 add support for a mysql server relation

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
import yaml
8
8
 
9
9
from fabric.api import env, run, sudo, task, put
10
 
from fabric.context_managers import cd
 
10
from fabric.context_managers import cd, shell_env
11
11
from fabric.contrib import files
12
12
 
13
13
 
24
24
def _config_get(service_name):
25
25
    yaml_conf = Popen(['juju', 'get', service_name], stdout=PIPE)
26
26
    conf = yaml.safe_load(yaml_conf.stdout)
27
 
    orig_conf = yaml.safe_load(open('config.yaml', 'r'))['options']
28
 
    return {k: (v['value'] if v['value'] is not None else orig_conf[k]['default']) for k,v in conf['settings'].iteritems()}
 
27
    return {k: (v['value'] if 'value' in v else v['default']) for k,v in conf['settings'].iteritems()}
 
28
 
29
29
 
30
30
def _find_django_admin_cmd():
31
31
    for cmd in ['django-admin.py', 'django-admin']:
39
39
# Initialisation
40
40
env.user = 'ubuntu'
41
41
 
42
 
d = yaml.safe_load(Popen(['juju','status'],stdout=PIPE).stdout)
 
42
d = yaml.safe_load(Popen(['juju', 'status'], stdout=PIPE).stdout)
43
43
 
44
44
services = d.get("services", {})
45
45
if services is None:
59
59
            env.roledefs.setdefault(service[0], []).append(unit[1]['public-address'])
60
60
            env.roledefs.setdefault(unit[0], []).append(unit[1]['public-address'])
61
61
 
 
62
if not env.roles:
 
63
    print "You must select a charm or a unit with the -R option to perform a task"
 
64
    print "Charms: %s" % [role for role in env.roledefs.keys() if not '/' in role]
 
65
    print "Units: %s" % [host for host in env.roledefs.keys() if '/' in host]
 
66
    print
 
67
else:
 
68
    env.service_name = env.roles[0].split('/')[0]
 
69
    env.sanitized_service_name = _sanitize(env.service_name)
 
70
    env.conf = _config_get(env.service_name)
 
71
    if not env.conf['django_settings']:
 
72
        django_settings_modules = '.'.join([env.sanitized_service_name, 'settings'])
 
73
    else:
 
74
        django_settings_modules = env.conf['django_settings']
62
75
 
63
 
env.service_name = env.roles[0].split('/')[0]
64
 
env.sanitized_service_name = _sanitize(env.service_name)
65
 
env.conf = _config_get(env.service_name)
66
 
env.project_dir = os.path.join(env.conf['install_root'], env.sanitized_service_name)
67
 
env.django_settings_modules = '.'.join([env.sanitized_service_name, env.conf['django_settings']])
 
76
    env.project_dir = os.path.join(env.conf['install_root'], env.sanitized_service_name)
 
77
    env.site_path = os.path.join(env.project_dir, env.conf['application_path'])
 
78
    if env.conf['python_path']:
 
79
        env.python_path = ':'.join([env.conf['install_root'], env.conf['python_path'].replace(',', ':')])
 
80
    else:
 
81
        env.python_path = env.conf['install_root']
68
82
 
69
83
 
70
84
# Debian
75
89
    """
76
90
    sudo('apt-get install -y %s' % packages)
77
91
 
 
92
 
78
93
@task
79
94
def apt_update():
80
95
    """
82
97
    """
83
98
    sudo('apt-get update')
84
99
 
 
100
 
85
101
@task
86
102
def apt_dist_upgrade():
87
103
    """
89
105
    """
90
106
    sudo('apt-get dist-upgrade -y')
91
107
 
 
108
 
92
109
@task
93
110
def apt_install_r():
94
111
    """
98
115
        for req_file in env.conf['requirements_apt_files'].split(','):
99
116
            sudo("apt-get install -y $(cat %s | tr '\\n' ' '" % req_file)
100
117
 
101
 
# Python
 
118
 
 
119
# Pip
102
120
@task
103
121
def pip_install(packages):
104
122
    """
106
124
    """
107
125
    sudo("pip install %s" % packages)
108
126
 
 
127
 
109
128
@task
110
129
def pip_install_r():
111
130
    """
115
134
        for req_file in env.conf['requirements_pip_files'].split(','):
116
135
            sudo("pip install -r %s" % req_file)
117
136
 
 
137
 
 
138
@task
 
139
def pip_freeze():
 
140
    """
 
141
    List installed python packages
 
142
    """
 
143
    sudo("pip freeze")
 
144
 
 
145
 
118
146
# Users
119
147
@task
120
148
def adduser(username):
123
151
    """
124
152
    sudo('adduser %s --disabled-password --gecos ""' % username)
125
153
 
126
 
@task
127
 
def ssh_add_key(pub_key_file, username=None):
128
 
    """
129
 
    Add a public SSH key to the authorized_keys file on the remote machine.
130
 
    """
131
 
    with open(os.path.normpath(pub_key_file), 'rt') as f:
132
 
        ssh_key = f.read()
133
 
 
134
 
    if username is None:
135
 
        run('mkdir -p .ssh')
136
 
        files.append('.ssh/authorized_keys', ssh_key)
137
 
    else:
138
 
        run('mkdir -p /home/%s/.ssh' % username)
139
 
        files.append('/home/%s/.ssh/authorized_keys' % username, ssh_key)
140
 
        run('chown -R %s:%s /home/%s/.ssh' % (username, username, username))
141
 
 
142
154
 
143
155
# VCS
144
156
 
175
187
def manage(command):
176
188
    """ Runs management commands."""
177
189
    django_admin_cmd = _find_django_admin_cmd()
178
 
    sudo('%s %s --pythonpath=%s --settings=%s' % \
179
 
      (django_admin_cmd, command, env.conf['install_root'], env.django_settings_modules), \
180
 
         user=env.conf['wsgi_user'])
 
190
 
 
191
    with cd(env.site_path):
 
192
        with shell_env(PYTHONPATH=':'.join([env.project_dir, env.python_path])):
 
193
            sudo('%s %s --settings=%s' %
 
194
                (django_admin_cmd, command, django_settings_modules),
 
195
                 user=env.conf['wsgi_user'])
 
196
 
181
197
 
182
198
@task
183
199
def load_fixture(fixture_path):
187
203
    manage('loaddata %s' % os.path.join('/tmp/', fixture_file))
188
204
    run('rm %s' % os.path.join('/tmp/', fixture_file))
189
205
 
 
206
 
190
207
# Utils
191
208
@task
192
209
def delete_pyc():
193
210
    """ Deletes *.pyc files from project source dir """
194
 
 
195
 
    with env.project_dir:
196
 
        run("find . -name '*.pyc' -delete")
197
 
 
 
211
    with cd(env.project_dir):
 
212
        sudo("find . -name '*.pyc' -delete", user="www-data")
 
213
        reload()