def install(): packages([ 'build-essential', 'php5', 'php5-mysql', ], update=False) mysql(password="******") apache() stopped('apache2') site_disabled('default') site( 'mylampvm.dev', template_source=full_path + '/templates/apache2/vhost.tpl', port=80, server_name='mylampvm.dev', document_root='/srv', ) template_file(template_source=full_path + '/templates/apache2/envvars.tpl', path='/etc/apache2/envvars', context={ 'apache_run_user': '******', 'apache_run_group': 'vagrant', }, owner='root', group='root', use_sudo=True) directory('/var/lock/apache2', True, 'vagrant', 'vagrant') # site_enabled('mylampvm.dev') restarted('apache2')
def install_dependencies(): deb.uptodate_index(max_age={'hour': 1}) deb.packages([ 'python-software-properties', 'python-dev', 'build-essential', 'nginx', 'libxslt1-dev', 'supervisor', 'git', 'tig', 'vim', 'multitail', 'curl', 'tmux', 'htop', 'ack-grep', 'libmysqlclient-dev', 'mysql-server', 'mysql-client', 'python-mysqldb', 'libjpeg-dev', 'libtiff-dev', 'zlib1g-dev', 'python-virtualenv', ])
def update_system_dependencies(): deb.uptodate_index(max_age={'hour': 1}) deb.packages([ 'python-software-properties', 'build-essential', 'python-dev', 'python-pip', 'python-virtualenv', 'postgresql', 'postgresql-server-dev-9.3', 'nginx', 'libxml2', 'libxml2-dev', 'libxslt1-dev', 'libxslt-dev', 'supervisor', 'libjpeg-dev', 'libtiff-dev', 'zlib1g-dev', 'git', # not required but nice to have 'tig', 'tmux', 'htop', 'ack-grep', ])
def install(): packages([ 'build-essential', 'php5', 'php5-mysql', ], update=False) mysql(password="******") apache() stopped('apache2') site_disabled('default') site( 'mylampvm.dev', template_source=full_path + '/templates/apache2/vhost.tpl', port=80, server_name='mylampvm.dev', document_root='/srv', ) template_file( template_source = full_path + '/templates/apache2/envvars.tpl', path='/etc/apache2/envvars', context = { 'apache_run_user': '******', 'apache_run_group': 'vagrant', }, owner = 'root', group = 'root', use_sudo=True ) directory('/var/lock/apache2', True, 'vagrant', 'vagrant') # site_enabled('mylampvm.dev') restarted('apache2')
def create(venv=None, pyver=None): deb.packages(['libjpeg8', 'libjpeg8-dev', 'libfreetype6', 'libfreetype6-dev', 'zlib1g-dev']) validate = "^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$" if not env.get("new_user"): if env.no_input_mode: env.new_user = env.new_user_default else: prompt("User: "******"new_user", env.get('new_user_default', ''), validate=validate) if not fabtools.user.exists(env.new_user): prompt("Password for {0}: ".format(env.new_user), "new_passwd", '', validate=validate) env.home_dir = '{0}/{1}'.format(env.home_user, env.new_user) fabtools.user.create(env.new_user, home=env.home_dir, group=env.new_user, create_home=True, system=False, shell='/bin/bash', create_group=True, password=env.new_passwd) if not env.get("project"): if env.no_input_mode: env.project = env.project_default else: prompt("Project name: ", "project", env.get('project_default', '')) if not env.get("repository"): if env.no_input_mode: env.repository = env.repository_default else: prompt("Deploy from: ", "repository", env.get('repository_default', '')) if not env.get("domain"): if env.no_input_mode: abort("Need set env.domain !") else: prompt("Project DNS url: ", "domain", env.get('domain_default', ''), validate=validate) else: if not re.findall(validate, env.domain): abort("Invalid env.domain !") require('repository', 'domain', 'project') if venv is not None: env.venv = venv else: prompt("Virtual ENV: ", "venv", env.get('venv_default', ''), validate=validate) if pyver is not None: env.pyver = pyver else: prompt("Python VERSION: ", "pyver", env.get('pyver_default', ''), validate=validate) env.virt = '{0}/{1}/{2}'.format(env.home_user, env.new_user, env.virt_home) pyver_dir = '{0}{1}/{2}-{3}'.format(env.python_dir, env.pyver, env.new_user, env.venv) files.directory(env.virt, mode='750', owner=env.new_user, group=env.new_user, use_sudo=True) if not is_dir(posixpath.join(env.virt, env.venv)): with prefix('pythonbrew use {0}'.format(env.pyver)): if not is_dir(pyver_dir): sudo('pythonbrew venv create {0}-{1}'.format(env.new_user, env.venv)) sudo('ln -s /usr/lib/x86_64-linux-gnu/libjpeg.so {0}/lib/'.format(pyver_dir)) sudo('ln -s /usr/lib/x86_64-linux-gnu/libfreetype.so {0}/lib/'.format(pyver_dir)) sudo('ln -s /usr/lib/x86_64-linux-gnu/libz.so {0}/lib/'.format(pyver_dir)) sudo("find {2}/venvs/ -type d -iname '{1}-{0}' -print0 | xargs -I{3} -0 chown -R --preserve-root --no-dereference {1}:pythonbrew '{3}'".format(env.venv, env.new_user, '${PYTHONBREW_ROOT}', '{}')) sudo('ln -s {0} {1}/'.format(pyver_dir, env.virt)) sudo('chown -R {0}:{0} {1}'.format(env.new_user, env.virt))
def distribute(): """ Require distribute """ deb.packages([ 'curl', 'python-dev', ]) if not is_distribute_installed(): install_distribute()
def install_php_dependencies(): deb.packages([ "php5", "php5-fpm", "php-pear", "php5-common", "php5-mcrypt", "php5-mysql", "php5-cli", "php5-gd", ])
def distribute(): """ Require `distribute`_ to be installed. .. _distribute: http://packages.python.org/distribute/ """ deb.packages([ 'curl', 'python-dev', ]) if not is_distribute_installed(): install_distribute()
def update_dependencies(): deb.uptodate_index(max_age={'hour': 1}) deb.packages([ 'python-software-properties', 'python-dev', 'build-essential', 'python-pip', 'nginx', 'supervisor', 'postgresql-9.3', 'postgresql-client-9.3', 'postgresql-server-dev-9.3', 'git', 'tig', 'vim-nox', 'curl', 'tmux', 'htop', 'ack-grep', ]) sudo('pip install -U pip') sudo('pip install -U virtualenv') sudo('pip install -U setproctitle')
def samba_install(): 'Install samba' deb.packages(['samba'])
def push(): """ This function for create django site project work flow on remote server. Django site source cloning from remote git repository. NOTE: This function may be used in other fab file. For this need setup global `env` dict. **`env` settings** env.user - deploy user name (use for ssh) env.password - deploy user password (use for ssh) env.hosts - list deploy hosts (use for ssh) env.domain - django site domain (DNS) use for: - nginx settings - uWSGI start user - project dir name env.repository - remote git repository url, use for git clone site source env.no_input_mode - in this variable True use no input deploy mode. If no_input_mode==True using follow strategy: Abort if env.domain (env.repository) value not set or invalid. And using default confirm() value if needed. """ # cwd => ./deploy env.lcwd = os.path.abspath(os.path.dirname(__file__)) require('no_input_mode') #env.no_input_mode = False if env.no_input_mode: def confirm_local(question, default=True): puts(question) puts("Use no_input_mode [default: {0}]".format("Y" if default else "N")) return default confirm = confirm_local else: confirm = confirm_global validate = "^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$" if not env.get("domain"): if env.no_input_mode: abort("Need set env.domain !") else: prompt("Project DNS url: ", "domain", env.get('domain_default', ''), validate=validate) else: if not re.findall(validate, env.domain): abort("Invalid env.domain !") if not env.get("repository"): if env.no_input_mode: env.repository = env.repository_default else: prompt("Deploy from: ", "repository", env.get('repository_default', '')) require('repository', 'domain') puts("Deploy site: {0} \nFrom: {1}".format(env.domain, env.repository)) DOMAIN_WITHOUT_DOT = env.domain.replace('.', '_') env.project_user = DOMAIN_WITHOUT_DOT env.project_group = DOMAIN_WITHOUT_DOT env.project_dir_name = DOMAIN_WITHOUT_DOT env.root = posixpath.join(PROJECTS_ROOT, env.project_dir_name) env.debug = True deb.packages(['git']) files.directory(PROJECTS_ROOT, use_sudo=True, owner='root', group='root', mode='755') with cd(PROJECTS_ROOT): # pip cache files.directory('.pip.cache', use_sudo=True, owner='deploy', group='deploy', mode='755') pip_cache_dir = posixpath.join(PROJECTS_ROOT, '.pip.cache') # proj dir create if is_dir(env.project_dir_name) and confirm("proj dir exist! abort ?", default=False): return files.directory(env.project_dir_name, use_sudo=True, owner='root', group='root', mode='755') # proj user create if not fabtools.user.exists(env.project_user): fabtools.user.create(env.project_user, home=env.root, group=env.project_group, create_home=False, system=True, shell='/bin/false', create_group=True) # proj infrastructure with cd(env.project_dir_name): # proj source if not is_dir('src') or confirm("proj src exist! [rm all and re clone / git pull]?", default=False): files.directory('src', use_sudo=True, owner='deploy', group='deploy', mode='755') with cd('src'): sudo('rm -Rf .??* *') sudo('git clone {repository:s} .'.format(env), user='******') else: with cd('src'): sudo('git pull', user='******') # proj virtual env if not is_dir('.virtualenvs') or confirm("proj venv dir exist! [rm all and recreate / repeat install]?", default=False): files.directory('.virtualenvs', use_sudo=True, owner='deploy', group='deploy', mode='755') with cd('.virtualenvs'): sudo('rm -Rf .??* *') python.virtualenv('.virtualenvs', use_sudo=True, user='******', clear=True) with fabtools.python.virtualenv('.virtualenvs'): python.install_requirements('src/requirements.txt', use_mirrors=False, use_sudo=True, user='******', download_cache=pip_cache_dir) # ------------------- # # WEB SERVER SETTINGS # # ------------------- # # I`m use nginx <-> uWSGI <-> Django nginx.server() deb.packages(['uwsgi', 'uwsgi-plugin-python']) # proj conf! if not is_dir('conf') or confirm("proj conf dir exist! [backup and update? / skip]", default=False): files.directory('conf', use_sudo=True, owner='root', group='root', mode='755') with cd('conf'): local_conf_templates = os.path.join(os.path.dirname(__file__), 'template', 'conf') uwsgi_conf = os.path.join(local_conf_templates, 'uwsgi.ini') nginx_conf = os.path.join(local_conf_templates, 'nginx.conf') sudo("rm -Rf *.back") sudo("ls -d *{.conf,.ini} | sed 's/.*$/mv -fu \"&\" \"\\0.back\"/' | sh") files.template_file('uwsgi.ini', template_source=uwsgi_conf, context=env, use_sudo=True, owner='root', group='root', mode='644') files.file('reload', use_sudo=True, owner='root', group='root') sudo('ln -sf $(pwd)/uwsgi.ini /etc/uwsgi/apps-enabled/' + env.project_dir_name + '.ini') files.template_file('nginx.conf', template_source=nginx_conf, context=env, use_sudo=True, owner='root', group='root', mode='644') sudo('ln -sf $(pwd)/nginx.conf /etc/nginx/sites-enabled/' + env.project_dir_name) sudo('service nginx restart') sudo('service uwsgi restart')
def sane_install(): 'Install sane' deb.packages(['libsane', 'sane-utils', 'hplip'])
def dovecot_install(): "Install dovecot" deb.packages(["dovecot-imapd"])
def python_install(): 'Install Python' deb.packages(['python2.7'])
def nfs_install(): 'Install NFS' deb.packages(['nfs-kernel-server'])
def postfix_install(): 'Install postfix and friends' deb.packages(['postfix', 'postgrey', 'postfix-policyd-spf-python'])