def django_media_pull(project_path, local_media_path, remote_media_path): """ Overwrite local media files with remote ones """ with cd(project_path), cd(remote_media_path): run('tar -czf /tmp/media.tar.gz *') get('/tmp/media.tar.gz', '/tmp/media.tar.gz') local("mkdir -p %s" % (local_media_path)) local("rm -rf %s/*" % (local_media_path)) local("tar -xf /tmp/media.tar.gz -C %s" % (local_media_path))
def django_media_push(project_path, local_media_path, remote_media_path): with lcd(local_media_path): local('tar -czf /tmp/media.tar.gz *') put('/tmp/media.tar.gz', '/tmp/media.tar.gz') with cd(project_path): run("mkdir -p %s" % (remote_media_path)) with cd(remote_media_path): run("rm -rf *") run("tar -xf /tmp/media.tar.gz")
def install_python27(): print white('--- install python2.7 ---', bold=True) #if not file_exists('/usr/local/bin/python2.7'): with cd('~/'): run('curl -O https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz') run('tar zxvf Python-2.7.9.tgz') with cd('/home/{}/Python-2.7.9'.format(run('whoami'))): run('./configure') run('make') sudo('make install') with cd('~/'): run('rm Python-2.7.9.tgz') run('rm -rf Python-2.7.9')
def set_symlinks(): print white('--- set symlinks ---', bold=True) with cd('~/'): dotfiles = ''' zshrc zshenv tmux.conf vimrc vim gitignore gitconfig gitattributes '''.split() map(lambda _: run('ln -sf dotfiles/_{0} .{0}'.format(_)), dotfiles)
def set_symlinks(src='dotfiles'): print white('--- set symlinks ---', bold=True) with cd('~/'): dotfiles = ''' zshrc zshenv tmux.conf vimrc vim gitignore gitconfig gitattributes mackup.cfg '''.split() map(lambda _: run('ln -sf {0}/_{1} .{1}'.format(src, _)), dotfiles)
def compilemessages(): """ Run python manage.py compilemessages """ puts(red('###############################')) puts(red('### Compilemessages')) puts(red('###############################')) with cuisine.cd(env.project_path): cuisine.run('python manage.py compilemessages')
def bower(): """ Run bower install from bower.json""" puts(red('###############################')) puts(red('### Bower install')) puts(red('###############################')) with cuisine.cd(env.project_path): cuisine.run('bower install')
def debugsqlshell(): """ Run python manage.py debugsqlshell """ puts(red('###############################')) puts(red('### Debugsqlshell')) puts(red('###############################')) with cuisine.cd(env.project_path): cuisine.run('python manage.py debugsqlshell')
def pip(): """ Run pip install --user -r """ puts(red('###############################')) puts(red('### Pip')) puts(red('###############################')) with cuisine.cd(env.project_path): cuisine.run('pip install --user -r requirements.txt')
def runserver(): """ Run python manage.py runserver 0.0.0.0:8000 """ puts(red('###############################')) puts(red('### Runserver')) puts(red('###############################')) with cuisine.cd(env.project_path): cuisine.run('python manage.py runserver 0.0.0.0:8000')
def createsuperuser(): """ Run python manage.py makemigrations""" puts(red('###############################')) puts(red('### Createsuperuser')) puts(red('###############################')) with cuisine.cd(env.project_path): cuisine.run('python manage.py createsuperuser')
def hello(): with cs.cd('/usr/java') : status = cs.run('java && echo OK ; true') with cs.mode_sudo() : if status.endswith('OK') : cs.run('rpm -Uvh jre-7u7-linux-i586.rpm') else : cs.run('rpm -ivh jre-7u7-linux-i586.rpm') cs.run(green('echo "Hello World"'))
def virtualenv_ensure(project_path, venv_path='.venv', packages_file='requirements.txt', restart=False): package_ensure('python-dev python-pip python-virtualenv') with cd(project_path): dir_ensure('downloads') if restart: run('rm -rf %s' % venv_path) if not dir_exists(venv_path): run('virtualenv --no-site-packages --distribute %s' % venv_path) with virtualenv(project_path, venv_path): run('pip install --download-cache downloads -r ' + packages_file)
def gunicorn_supervisor_ensure(project_path, project_name, template, key_env): with mode_sudo(), cd(project_path): config = '/etc/supervisor/conf.d/%s.conf' % project_name package_ensure('supervisor') python_package_ensure('setproctitle') run("cp %s %s" % (template, config)) file_update(config, lambda x: text_template(x,key_env)) run("supervisorctl reread") run("supervisorctl update") run("supervisorctl restart %s" % (project_name))
def startapp(app_name): """ Run python manage.py startapp name """ puts(red('###############################')) puts(red('### Startapp')) puts(red('###############################')) with cuisine.cd(env.project_path): path = 'applications/%s' % app_name cuisine.run('mkdir %s' % path) cuisine.run('python manage.py startapp %s %s' % (app_name, path))
def nginx_ensure(project_path, name, template, key_env): with mode_sudo(), cd(project_path): package_ensure('nginx') run("cp %s /etc/nginx/sites-available/%s" % (template, name)) file_update('/etc/nginx/sites-available/%s' % name, lambda x: text_template(x,key_env)) if not file_exists("/etc/nginx/sites-enabled/%s" % name): run("ln -s -t /etc/nginx/sites-enabled /etc/nginx/sites-available/%s " % ( name)) file_unlink('/etc/nginx/sites-enabled/default') run("service nginx restart")
def setup_dokuwiki(): if not cuisine.file_exists('/tmp/dokuwiki-stable.tgz'): cuisine.cd("/tmp") cuisine.run( " wget -c http://download.dokuwiki.org/src/dokuwiki/dokuwiki-stable.tgz") if not cuisine.dir_exists('/tmp/dokuwiki-20*'): cuisine.cd( "/tmp" ) cuisine.run("tar xvzf dokuwiki-stable.tgz"); ## Improve for update purpose #if not cuisine.dir_exists( www_folder + "/dokuwiki" ): if True: cuisine.log_message("Installing") with cuisine.mode_sudo(): cuisine.dir_ensure( www_folder + "/dokuwiki" ) cuisine.run("cp -rv /tmp/dokuwiki-20*/* " + www_folder + "/dokuwiki" ) for _ in ( 'data', 'conf' , 'bin' , 'inc'): cuisine.file_write( www_folder + "/dokuwiki" + "/" + _ + "/.htaccess" , cuisine.file_local_read("./htaccess_template") , owner=http_user , group=http_user) cuisine.dir_ensure( www_folder + "/dokuwiki", owner=http_user , group=http_user , recursive=True ) print "now visit <IP>/dokuwiki/install.php"
def _list_dir(self, dir_file, append_dir_file=True): directory_path = os.path.join(self.directory, dir_file) with cd(directory_path): ls_str = c_run("/bin/bash -c 'ls | awk \"{ print $1 \"}'").strip() if not ls_str: return [] ls_files = ls_str.split("\n") if append_dir_file: return [os.path.join(dir_file, line.strip()) for line in ls_files] else: return [line.strip() for line in ls_files]
def migrate(app=None): """ Run python manage.py migrate """ puts(red('###############################')) puts(red('### Migrate')) puts(red('###############################')) with cuisine.cd(env.project_path): if app is not None: cuisine.run('python manage.py migrate %s' % app) else: cuisine.run('python manage.py migrate')
def makemigrations(app=None): """ Run python manage.py makemigrations""" puts(red('###############################')) puts(red('### Makemigrations')) puts(red('###############################')) with cuisine.cd(env.project_path): if app is not None: cuisine.run('python manage.py makemigrations %s' % app) else: cuisine.run('python manage.py makemigrations')
def makemessages(lang=None): """ Run python manage.py makemessages """ puts(red('###############################')) puts(red('### Makemessages')) puts(red('###############################')) with cuisine.cd(env.project_path): if lang is not None: cuisine.run('python manage.py makemessages -l %s' % lang) else: cuisine.run('python manage.py makemessages -a')
def django_database_push(project_path, db_name, db_username, db_password, config_template_path, config_path): """ Recreate and load local database to remote host""" local("python manage.py dumpdata > /tmp/db.json") with cd(project_path): put('/tmp/db.json', '/tmp/db.json') if postgresql_database_check(db_name): postgresql_database_drop(db_name) django_config_ensure(project_path, config_template_path, config_path) postgresql_ensure(db_name, db_username, project_path, db_password) django_database_ensure(project_path) with virtualenv(project_path): run("python manage.py loaddata /tmp/db.json")
def tar_gz_install(url, option=""): archive = os.path.basename(url) package = archive.replace(".tar.gz", "") cuisine.run("wget {}".format(url), pty=False) cuisine.run("tar zxf {}".format(archive)) directory = cuisine.run("""echo $(tar tvf {} | head -1 | egrep -o "[^ ]+/$")""".format(archive)) with cuisine.cd(directory): cuisine.run("{} make".format(option)) with cuisine.mode_sudo(): cuisine.run("paco -lp {} \" {} make install\"".format(package, option)) cuisine.run("rm -rf {} {}".format(archive, directory))
def dotfiles(): dotfiles = [ [".pryrc", "~/.pryrc"], [".tigrc", "~/.tigrc"], [".zshrc", "~/.zshrc"], [".vimrc", "~/.vimrc"], [".vimrc.yaml", "~/.vimrc.yaml"], [".rbenv/default-gems", "~/.rbenv/default-gems"], ] git_clone("https://github.com/kazufusa/dotfiles", "~/dotfiles") with cuisine.cd("~"): for target, sym in dotfiles: cuisine.run("ln -sf ~/dotfiles/{} {}".format(target, sym))
def django_database_push(project_path, db_name, db_username, db_password, config_template_path, config_path): """ Recreate and load local database to remote host""" local("python manage.py dumpdata > /tmp/db.json") with cd(project_path): put('/tmp/db.json', '/tmp/db.json') if postgresql_database_check(db_name): postgresql_database_drop(db_name) django_config_ensure(project_path, config_template_path, config_path) postgresql_ensure(db_name, db_username, project_path, db_password ) django_database_ensure(project_path ) with virtualenv(project_path): run("python manage.py loaddata /tmp/db.json")
def setup() : cent_setup() if not exists('~/dotfiles') : cs.run('git clone https://github.com/yokotanaohiko/dotfiles.git') with cs.mode_sudo() : cs.run('rm -r .vim') cs.run('rm .vim*') cs.run('rm .zshrc*') with cs.cd('~/dotfiles') : cs.run('./setup.sh ; true') cs.run('vim -c NeoBundleInstall -c q')
def virtualenv(environment): '''Define the virtual environment to use. This function is useful for fabric.api.run commands, because all run invocation will be called within the virtual environemnt. Example: with virtualenv(VENV_SCRAPYD): run('pip install scrapyd') run('pip install simplejson') The parameter environment is the name of the virtual environment followed by VENV_PREFIX ''' venv_path = VENV_PREFIX + os.sep + environment venv_activate = 'source %s%sbin%sactivate' % (venv_path, os.sep, os.sep) with cuisine.cd(venv_path): with prefix(venv_activate): yield
def git_ensure(repo_path, commit=None): '''seed a remote git repository''' package_ensure('git') if not dir_exists(repo_path): run("mkdir -p %s" % (repo_path)) if commit is None: # if no commit is specified we will push HEAD commit = local('git rev-parse HEAD', capture=True) # if local('git status --porcelain', capture=True) != '': # abort( # 'Working copy is dirty. This check can be overridden by\n' # 'importing gitric.api.allow_dirty and adding allow_dirty to your ' # 'call.') with cd(repo_path): # initialize the remote repository (idempotent) run('git init') if run('git rev-parse --verify -q HEAD', warn_only=True) == commit: puts('Remote already on commit %s' % commit) else: # silence git complaints about pushes coming in on the current branch # the pushes only seed the immutable object store and do not modify the # working copy run('git config receive.denyCurrentBranch ignore') # a target doesn't need to keep track of which branch it is on so we always # push to its "master" with settings(warn_only=True): push = local( 'git push git+ssh://%s@%s:%s%s %s:refs/heads/master' % ( env.user, env.host, env.port, repo_path, commit)) if push.failed: abort( '%s is a non-fast-forward\n' 'push. The seed will abort so you don\'t lose information. ' % commit) # checkout a sha1 on a remote git repo run('git reset --hard %s' % (commit))
def setup(os_name="") : if os_name : if os_name == "centos" : print("centos setup") cent_setup() if os_name == "ubuntu" : print("ubuntu setup") ubuntu_setup() if not exists('~/dotfiles') : cs.run('git clone https://github.com/yokotanaohiko/dotfiles.git') with cs.mode_sudo() : cs.run('rm -r .vim') cs.run('rm .vim*') cs.run('rm .zshrc*') with cs.cd('~/dotfiles') : cs.run('./setup.sh ; true') cs.run('vim -c NeoBundleInstall -c q')
def django_config_ensure(project_path, template, config, key_env): with cd(project_path): run("cp %s %s" % (template, config)) file_update(config, lambda x: text_template(x, key_env))
def bootstrap(): """ Bootstrap vagrant enviroment """ puts(red('###############################')) puts(red('### Setup host')) puts(red('###############################')) puts(green('-> Add backports')) cuisine.sudo( 'echo "deb http://ftp.us.debian.org/debian wheezy-backports main" >> /etc/apt/sources.list' ) puts(green('-> Update repositories')) cuisine.package_update() puts(green('-> Installing gettext')) cuisine.package_ensure("gettext") puts(green('-> Installing curl')) cuisine.package_ensure("curl") puts(green('-> Installing git')) cuisine.package_ensure("git") puts(green('-> Installing nano')) cuisine.package_ensure("nano") puts(green('-> Installing build-essential')) cuisine.package_ensure("build-essential") puts(green('-> Installing libxml2-dev')) cuisine.package_ensure("libxml2-dev") puts(green('-> Installing libjpeg8-dev')) cuisine.package_ensure("libjpeg8-dev") puts(green('-> Installing libpng12-dev')) cuisine.package_ensure("libpng12-dev") puts(green('-> Installing python')) cuisine.package_ensure("python") puts(green('-> Installing python-dev')) cuisine.package_ensure("python-dev") puts(green('-> Installing python-pip')) cuisine.package_ensure("python-pip") puts(green('-> Installing postgres')) if not cuisine.dir_exists('/etc/postgresql'): cuisine.sudo( 'echo "deb http://apt.postgresql.org/pub/repos/apt/ wheezy-pgdg main" > /etc/apt/sources.list.d/pgdg.list' ) cuisine.sudo( 'wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add -' ) cuisine.package_update() cuisine.package_ensure('postgresql') cuisine.package_ensure('postgresql-server-dev-9.3') puts(green('-> Configuring postgres user login')) old_srt = "local all all peer" new_srt = "local all all trust" cuisine.sudo('sed -i "s/%s/%s/g" /etc/postgresql/9.3/main/pg_hba.conf' % (old_srt, new_srt)) cuisine.sudo('/etc/init.d/postgresql restart') puts(green('-> Creating database')) db_name = DATABASES['default']['NAME'] db_user = DATABASES['default']['USER'] db_pass = DATABASES['default']['PASSWORD'] puts(green('-> Creating postgres username')) cuisine.sudo('psql -c "CREATE USER %s WITH PASSWORD \'%s\';"' % (db_user, db_pass), user='******') puts(green('-> Creating postgres database')) cuisine.sudo('psql -c "CREATE DATABASE %s;"' % db_name, user='******') puts(green('-> Installing requirements for django')) with cuisine.cd(env.project_path): cuisine.run( 'pip install --user https://www.djangoproject.com/download/1.7c2/tarball/' ) cuisine.run('pip install --user -r requirements.txt') puts(green('-> Installing nodejs')) cuisine.package_ensure('nodejs-legacy') cuisine.sudo('curl https://www.npmjs.org/install.sh | sh ') puts(green('-> Installing yuglify')) cuisine.sudo('npm -g install yuglify') puts(green('-> Installing bower')) cuisine.sudo('npm -g install bower') puts(green('-> Creating directories')) cuisine.dir_ensure(env.project_path + 'assets/components') cuisine.dir_ensure(env.project_path + 'assets/images') cuisine.dir_ensure(env.project_path + 'assets/stylesheets') cuisine.dir_ensure(env.project_path + 'assets/scripts') cuisine.dir_ensure(env.project_path + 'locale/') puts(red('###############################')) puts(red('### Host setup completed')) puts(red('###############################'))
def django_log_ensure(project_path): with cd(project_path): dir_ensure('logs')
def django_disable_debug_mode(project_path, config): with cd(project_path): file_update(config, lambda x: re.sub('DEBUG = \w*', 'DEBUG = False', x) )
def git_is_origin(path, uri): with cd(path): return run('git config --get remote.origin.url').endswith(uri)
def virtualenv(path, venv_path='.venv'): with cd(path), prefix('source %s/bin/activate' % venv_path): yield
def django_disable_debug_mode(project_path, config): with cd(project_path): file_update(config, lambda x: re.sub('DEBUG = \w*', 'DEBUG = False', x))
def django_config_ensure(path, template, config): with cd(path): run("cp %s %s" % (template, config)) file_update(config, lambda x: text_template(x,env))
def virtualenv(): venv_activate = 'source %s%sbin%sactivate' % (VENV_PATH, os.sep, os.sep) with cuisine.cd(VENV_PATH): with prefix(venv_activate): yield