def setup_sphinx(): """Install sphinx from pip. We prefer packages from pip as ubuntu packages are usually old. To build the Documentation we also need to check and update the subjacent docutils installation""" fastprint(blue('Setting up Sphinx\n')) if is_installed('docutils-common'): sudo('apt-get remove docutils-common') if is_installed('docutils-doc'): sudo('apt-get remove docutils-doc') if is_installed('python-docutils'): sudo('apt-get remove python-docutils') sudo('pip install --upgrade docutils==0.10') sudo('pip install sphinx') fastprint(green('Setting up Sphinx completed\n'))
def server(version=None, password=None): """ Require a MySQL server to be installed and running. Example:: from fabtools import require require.mysql.server(password='******') """ if version: pkg_name = 'mysql-server-%s' % version else: pkg_name = 'mysql-server' if not is_installed(pkg_name): if password is None: password = prompt_password() with settings(hide('running')): preseed_package('mysql-server', { 'mysql-server/root_password': ('password', password), 'mysql-server/root_password_again': ('password', password), }) package(pkg_name) started('mysql')
def server(version=None, password=None): """ Require a MySQL server to be installed and running. Example:: from fabtools import require require.mysql.server(password='******') """ if version: pkg_name = "mysql-server-%s" % version else: pkg_name = "mysql-server" if not is_installed(pkg_name): if password is None: password = prompt_password() with settings(hide("running")): preseed_package( "mysql-server", { "mysql-server/root_password": ("password", password), "mysql-server/root_password_again": ("password", password), }, ) package(pkg_name) started("mysql")
def install_dependencies(): # install vim utils.deb.install('vim') # install required packages by plugins print(green('Installing plugins dependencies.')) # ctags, better grep, python flake, C/C++ omnicompletion plugins = ['exuberant-ctags', 'ack-grep', 'pyflakes', 'clang'] for plugin in plugins: utils.deb.install(plugin) # install pip if is not available utils.deb.install('python-pip') # update pip through pip py_install('pip', use_sudo=True, upgrade=True) # TODO Check if the command python get-pip.py is better # install and upgrade setup tools (that replaced distribute) py_install('setuptools', use_sudo=True, upgrade=True) # python flake+pep8 if not py_is_installed('flake8'): py_install('flake8', use_sudo=True) # js linters if is_installed('nodejs'): cmd = 'sudo -H npm -g install jscs jshint' run(cmd) else: print(red('npm not installed. Re-run this task after installing npm'))
def server(mailname): """ Require a Postfix email server. This makes sure that Postfix is installed and started. :: from fabtools import require # Handle incoming email for our domain require.postfix.server('example.com') """ # Ensure the package is installed if not is_installed('postfix'): preseed_package( 'postfix', { 'postfix/main_mailer_type': ('select', 'Internet Site'), 'postfix/mailname': ('string', mailname), 'postfix/destinations': ('string', '%s, localhost.localdomain, localhost ' % mailname), }) install('postfix') # Ensure the service is started started('postfix')
def site(server_name, template_contents=None, template_source=None, enabled=True, check_config=True, **kwargs): """ Require an nginx site. You must provide a template for the site configuration, either as a string (*template_contents*) or as the path to a local template file (*template_source*). :: from fabtools import require CONFIG_TPL = ''' server { listen %(port)d; server_name %(server_name)s %(server_alias)s; root %(docroot)s; access_log /var/log/nginx/%(server_name)s.log; }''' require.nginx.site('example.com', template_contents=CONFIG_TPL, port=80, server_alias='www.example.com', docroot='/var/www/mysite', ) .. seealso:: :py:func:`fabtools.require.files.template_file` """ if not is_installed('nginx-common'): # nginx-common is always installed if Nginx exists server() config_filename = '/etc/nginx/sites-available/%s.conf' % server_name context = { 'port': 80, } context.update(kwargs) context['server_name'] = server_name template_file(config_filename, template_contents, template_source, context, use_sudo=True) link_filename = '/etc/nginx/sites-enabled/%s.conf' % server_name if enabled: if not is_link(link_filename): run_as_root("ln -s %(config_filename)s %(link_filename)s" % locals()) # Make sure we don't break the config if check_config: with settings(hide('running', 'warnings'), warn_only=True): if run_as_root('nginx -t').failed: run_as_root("rm %(link_filename)s" % locals()) message = red("Error in %(server_name)s nginx site config (disabling for safety)" % locals()) abort(message) else: if is_link(link_filename): run_as_root("rm %(link_filename)s" % locals()) reload_service('nginx')
def server(version=None, password=None): """ Require a MySQL server to be installed and running. Example:: from fabtools import require require.mysql.server(password='******') """ if version: pkg_name = 'mysql-server-%s' % version else: pkg_name = 'mysql-server' if not is_installed(pkg_name): if password is None: password = prompt('Please enter password for MySQL user "root":' % user) with settings(hide('running')): preseed_package( 'mysql-server', { 'mysql-server/root_password': ('password', password), 'mysql-server/root_password_again': ('password', password), }) package(pkg_name) started('mysql')
def install_dependencies(): # install vim utils.deb.install('vim') # install required packages by plugins print(green('Installing plugins dependencies.')) # ctags, better grep, python flake, C/C++ omnicompletion plugins = ['exuberant-ctags', 'ack-grep', 'pyflakes', 'clang'] for plugin in plugins: utils.deb.install(plugin) # install pip if is not available utils.deb.install('python-pip') # update pip through pip py_install('pip', use_sudo=True, upgrade=True) # TODO Check if the command python get-pip.py is better # install and upgrade setup tools (that replaced distribute) py_install('setuptools', use_sudo=True, upgrade=True) # python flake+pep8 if not py_is_installed('flake8'): py_install('flake8', use_sudo=True) # js linter if is_installed('nodejs'): cmd = 'sudo -H npm -g install jscs' run(cmd) else: print(red('npm not installed. Re-run this task after installing npm'))
def server(mailname): """ Require a Postfix email server. This makes sure that Postfix is installed and started. :: from fabtools import require # Handle incoming email for our domain require.postfix.server('example.com') """ # Ensure the package is installed if not is_installed('postfix'): preseed_package('postfix', { 'postfix/main_mailer_type': ('select', 'Internet Site'), 'postfix/mailname': ('string', mailname), 'postfix/destinations': ('string', '%s, localhost.localdomain, localhost ' % mailname), }) install('postfix') # Ensure the service is started started('postfix')
def require_mysql(app): # pragma: no cover if not deb.is_installed('mariadb-server'): require.deb.packages(['mariadb-server', 'mariadb-client', 'php-mysql']) require.mysql.user(app.name, app.name) require.mysql.database(app.name, owner=app.name) if confirm('Recreate database?', default=False): upload_sqldump(app)
def remove_old_install(): """ remove old packages from navitia """ if deb.is_installed('navitia-stat-persistor'): execute(stop_stat_persistor) deb.uninstall('navitia-stat-persistor', purge=True) files.remove('/srv/stat_persistor/alembic.ini') files.remove('/srv/stat_persistor/stat_persistor.json') else: print yellow('No old installation detected')
def package(pkg_name, update=False): """ Require a deb package to be installed. Example:: from fabtools import require require.deb.package('foo') """ if not is_installed(pkg_name): install(pkg_name, update)
def nopackage(pkg_name): """ Require a deb package to be uninstalled. Example:: from fabtools import require require.deb.nopackage('apache2') """ if is_installed(pkg_name): uninstall(pkg_name)
def install(package, upgrade=False): """ Helper method to install a deb package. If the package is already installed and the parameter 'upgrade' is True, then it will be upgraded if possible. """ if not is_installed(package): deb_install(package) if upgrade: cmd = 'apt-get install --only-upgrade {}'.format(package) sudo(cmd)
def server(mailname): """ I can haz postfix email server """ # Ensure the package is installed if not is_installed('postfix'): preseed_package('postfix', { 'postfix/main_mailer_type': ('select', 'Internet Site'), 'postfix/mailname': ('string', mailname), 'postfix/destinations': ('string', '%s, localhost.localdomain, localhost ' % mailname), }) install('postfix') # Ensure the service is started started('postfix')
def server(mailname): """ Require a Postfix email server """ # Ensure the package is installed if not is_installed('postfix'): preseed_package('postfix', { 'postfix/main_mailer_type': ('select', 'Internet Site'), 'postfix/mailname': ('string', mailname), 'postfix/destinations': ('string', '%s, localhost.localdomain, localhost ' % mailname), }) install('postfix') # Ensure the service is started started('postfix')
def setup_docker(force=False): """Setup docker on the target host. :param force: Whether to continue with installation even if docker already appears to be installed. Defaults to False. :type force: bool """ fastprint(yellow('Setting up docker on host: %s\n' % env.host)) if is_installed('lxc-docker'): fastprint(green( 'This system already appears to have docker installed on it\n')) else: version = run('uname -r') if '3.2' in version: # LTS 3.2 version is too old so we install a backported one # see http://docs.docker.io/en/latest/installation/ubuntulinux/ # #ubuntu-precise-12-04-lts-64-bit fastprint(red('Upgrading kernel to 3.8!\n')) response = prompt('Do you wish to continue? y/n :') if response != 'y': fastprint(red('Docker install aborted by user.\n')) return fastprint(blue('Ok upgrading kernel.')) require_packages([ 'linux-image-generic-lts-raring', 'linux-headers-generic-lts-raring']) fastprint(red('\nWe need to reboot the system now!\n')) response = prompt('Do you wish to continue? y/n :') if response is not None: reboot() else: require_package('linux-image-extra-%s' % version) require_ppa('ppa:dotcloud/lxc-docker') apt_get_update() require_packages([ 'software-properties-common', 'lxc-docker']) # Ensure ufw forwards traffic. # http://docs.docker.io/en/latest/installation/ubuntulinux/#ufw sed( '/etc/default/ufw', 'DEFAULT_FORWARD_POLICY="DROP"', 'DEFAULT_FORWARD_POLICY="ACCEPT"', use_sudo=False) setup_docker_image() setup_docker_user()
def nopackages(pkg_list): """ Require several deb packages to be uninstalled. Example:: from fabtools import require require.deb.nopackages([ 'perl', 'php5', 'ruby', ]) """ pkg_list = [pkg for pkg in pkg_list if is_installed(pkg)] if pkg_list: uninstall(pkg_list)
def package(pkg_name, update=False, version=None): """ Require a deb package to be installed. Example:: from fabtools import require # Require a package require.deb.package('foo') # Require a specific version require.deb.package('firefox', version='11.0+build1-0ubuntu4') """ if not is_installed(pkg_name): install(pkg_name, update=update, version=version)
def packages(pkg_list, update=False): """ Require several deb packages to be installed. Example:: from fabtools import require require.deb.packages([ 'foo', 'bar', 'baz', ]) """ pkg_list = [pkg for pkg in pkg_list if not is_installed(pkg)] if pkg_list: install(pkg_list, update)
def server(version='5.1', password=None): """ Require a MySQL server """ if not is_installed("mysql-server-%s" % version): if password is None: password = prompt_password() with settings(hide('running')): preseed_package('mysql-server', { 'mysql-server/root_password': ('password', password), 'mysql-server/root_password_again': ('password', password), }) package('mysql-server-%s' % version) started('mysql')
def setup_docker(force=False): """Setup docker on the target host. :param force: Whether to continue with installation even if docker already appears to be installed. Defaults to False. :type force: bool """ fastprint(yellow('Setting up docker on host: %s\n' % env.host)) if is_installed('lxc-docker'): fastprint( green( 'This system already appears to have docker installed on it\n') ) else: version = run('uname -r') if '3.2' in version: # LTS 3.2 version is too old so we install a backported one # see http://docs.docker.io/en/latest/installation/ubuntulinux/ # #ubuntu-precise-12-04-lts-64-bit fastprint(red('Upgrading kernel to 3.8!\n')) response = prompt('Do you wish to continue? y/n :') if response != 'y': fastprint(red('Docker install aborted by user.\n')) return fastprint(blue('Ok upgrading kernel.')) require_packages([ 'linux-image-generic-lts-raring', 'linux-headers-generic-lts-raring' ]) fastprint(red('\nWe need to reboot the system now!\n')) response = prompt('Do you wish to continue? y/n :') if response is not None: reboot() else: require_package('linux-image-extra-%s' % version) require_ppa('ppa:dotcloud/lxc-docker') apt_get_update() require_packages(['software-properties-common', 'lxc-docker']) # Ensure ufw forwards traffic. # http://docs.docker.io/en/latest/installation/ubuntulinux/#ufw sed('/etc/default/ufw', 'DEFAULT_FORWARD_POLICY="DROP"', 'DEFAULT_FORWARD_POLICY="ACCEPT"', use_sudo=False) setup_docker_image() setup_docker_user()
def server(version='5.1', password=None): """ Require a MySQL server """ if not is_installed("mysql-server-%s" % version): if password is None: password = prompt_password() with settings(hide('running')): preseed_package( 'mysql-server', { 'mysql-server/root_password': ('password', password), 'mysql-server/root_password_again': ('password', password), }) package('mysql-server-%s' % version) started('mysql')
def install_postfix(self): # def install_postfix(mailname): """ Require a Postfix email server. This makes sure that Postfix is installed and started. :: from fabtools import require # Handle incoming email for our domain require.postfix.server('example.com') """ self.get_env() self.copy_mailgate_file() # Ensure the package is installed if not is_installed('postfix'): preseed_package( 'postfix', { 'postfix/main_mailer_type': ('select', 'Internet Site'), 'postfix/mailname': ('string', self.postfix_hostname), 'postfix/destinations': ( 'string', '%s, localhost.localdomain, localhost ' % (self.postfix_hostname), ) }) install('postfix') # Update postfix conf custom_sudo("postconf -e 'virtual_alias_domains = regexp:%s'" % (self.virtual_domains_regex_path)) custom_sudo("postconf -e 'virtual_alias_maps = hash:%s'" % (self.virtual_alias_path)) # Restart postfix custom_sudo('service postfix restart') # Ensure the service is started started('postfix')
def query(query, use_sudo=True, **kwargs): """ Run a MySQL query. """ family = distrib_family() if family == 'debian': from fabtools.deb import install, is_installed elif family == 'redhat': from fabtools.rpm import install, is_installed else: raise UnsupportedFamily(supported=['debian', 'redhat']) func = use_sudo and run_as_root or run user = kwargs.get('mysql_user') or env.get('mysql_user') password = kwargs.get('mysql_password') or env.get('mysql_password') func_mysql = 'mysql' mysql_host = kwargs.get('mysql_host') or env.get('mysql_host') options = [ '--batch', '--raw', '--skip-column-names', ] if user: options.append('--user=%s' % quote(user)) if password: if not is_installed('sshpass'): install('sshpass') func_mysql = 'sshpass -p %(password)s mysql' % { 'password': quote(password) } options.append('--password') if mysql_host: options.append('--host=%s' % quote(mysql_host)) options = ' '.join(options) return func('%(cmd)s %(options)s --execute=%(query)s' % { 'cmd': func_mysql, 'options': options, 'query': quote(query), })
def set_js_linters(): """Set standard JS linters on syntastic.""" if is_installed('nodejs'): cmd = 'sudo -H npm -g install jscs jshint' run(cmd) else: print(red('npm not installed. Re-run this task after installing npm')) # patterns before = '^let g:syntastic_javascript_checkers.*$' after = "let g:syntastic_javascript_checkers = ['jscs', 'jshint']" print(green('Setting jscs and jshint as default linters on vim.')) sed('.vim/vimrc', before, after) print(green('Uploading configuration files')) config_path = 'conventions/.jscsrc' put(config_path) config_path = 'conventions/.jshintrc' put(config_path)
def query(query, use_sudo=True, **kwargs): """ Run a MySQL query. """ family = distrib_family() if family == 'debian': from fabtools.deb import install, is_installed elif family == 'redhat': from fabtools.rpm import install, is_installed else: raise UnsupportedFamily(supported=['debian', 'redhat']) func = use_sudo and run_as_root or run user = kwargs.get('mysql_user') or env.get('mysql_user') password = kwargs.get('mysql_password') or env.get('mysql_password') func_mysql = 'mysql' mysql_host = kwargs.get('mysql_host') or env.get('mysql_host') options = [ '--batch', '--raw', '--skip-column-names', ] if user: options.append('--user=%s' % quote(user)) if password: if not is_installed('sshpass'): install('sshpass') func_mysql = 'sshpass -p %(password)s mysql' % {'password': password} options.append('--password') options.append('--password=%s' % quote(password)) if mysql_host: options.append('--host=%s' % quote(mysql_host)) options = ' '.join(options) return func('%(cmd)s %(options)s --execute=%(query)s' % { 'cmd': func_mysql, 'options': options, 'query': quote(query), })
def install_postfix(self): # def install_postfix(mailname): """ Require a Postfix email server. This makes sure that Postfix is installed and started. :: from fabtools import require # Handle incoming email for our domain require.postfix.server('example.com') """ self.get_env() self.copy_mailgate_file() # Ensure the package is installed if not is_installed('postfix'): preseed_package('postfix', { 'postfix/main_mailer_type': ('select', 'Internet Site'), 'postfix/mailname': ('string', self.postfix_hostname), 'postfix/destinations': ( 'string', '%s, localhost.localdomain, localhost ' % ( self.postfix_hostname),) }) install('postfix') # Update postfix conf custom_sudo("postconf -e 'virtual_alias_domains = regexp:%s'" % ( self.virtual_domains_regex_path)) custom_sudo("postconf -e 'virtual_alias_maps = hash:%s'" % ( self.virtual_alias_path)) # Restart postfix custom_sudo('service postfix restart') # Ensure the service is started started('postfix')
def _server_debian(version, password): from fabtools.deb import is_installed, preseed_package from fabtools.require.deb import package as require_deb_package if version: pkg_name = 'mysql-server-%s' % version else: pkg_name = 'mysql-server' if not is_installed(pkg_name): if password is None: password = prompt('Please enter password for MySQL user "root":') with settings(hide('running')): preseed_package('mysql-server', { 'mysql-server/root_password': ('password', password), 'mysql-server/root_password_again': ('password', password), }) require_deb_package(pkg_name) started('mysql')
def openerp(): '''This task's goal is to install OpenERP 7 on a freshly installed Ubuntu 12.04 Precise Pangolin machine. It installs PostgreSQL and sets up a base. Then, it installs OpenERP package from the editor.''' ## Because all vagrant users does not live in the USA require.file( '/etc/apt/sources.list', contents=SOURCES_LIST_CONTENT, use_sudo=True ) ## Add OpenERP's editor debian packages repository require.deb.source( 'openerp', 'http://nightly.openerp.com/7.0/nightly/deb/', './' ) ## Installs and configure our PostgreSQL server require.postgres.server() require.postgres.user( 'openerp', password='******', createdb=True, createrole=True, login=True, connection_limit=20 ) require.postgres.database('openerp', 'openerp') ## OpenERP repository provides not signed packages, we can't use ## require.deb.package as it does not permit to force the installation ## of unsigned packages #require.deb.package('openerp') if not deb.is_installed('openerp'): deb.install('openerp', options=['--force-yes'])
def _server_debian(version, password): from fabtools.deb import is_installed, preseed_package from fabtools.require.deb import package as require_deb_package if version: pkg_name = 'mysql-server-%s' % version else: pkg_name = 'mysql-server' if not is_installed(pkg_name): if password is None: password = prompt('Please enter password for MySQL user "root":') with settings(hide('running')): preseed_package( 'mysql-server', { 'mysql-server/root_password': ('password', password), 'mysql-server/root_password_again': ('password', password), }) require_deb_package(pkg_name) started('mysql')
def install(package): """ Helper method to install a deb package if is not already installed. """ if not is_installed(package): deb_install(package)
def is_installed(pkg_name): deb.is_installed(pkg_name)
def install(): """ Installs Nix""" # Install the Nix package if not deb.is_installed('nix'): execute('nix.install_deps') # obtain the LSB codename codename = utils.deb.get_release_info().codename url = NIX_BUILDS_URL.format(**NIX_BUILDS[codename]) tmp_dir = mkdtemp() with cd(tmp_dir): print(green('Downloading Nix 1.11.2')) run("wget '{}'".format(url)) print(green('Installing Nix')) sudo('dpkg --unpack *.deb') deb.install('nix') rmtree(tmp_dir) # Create Nix build user accounts grp = 'nixbld' if not group.exists(grp): group.create(grp) for n in range(10): usr = "******".format(n) if not user.exists(usr): user.create(usr, comment="Nix build user {}".format(n), group=grp, extra_groups=[grp], system=True, shell='/bin/false') sudo('mkdir -p /etc/nix') sudo('mkdir -p /nix/store') sudo('chown root.nixbld /nix/store') sudo('chmod 1775 /nix/store') sudo('mkdir -p -m 1777 /nix/var/nix/gcroots/per-user') sudo('mkdir -p -m 1777 /nix/var/nix/profiles/per-user') # Configure nix-daemon init_path = '/etc/init.d/' upload_template(os.path.join(TEMPLATES_FOLDER, 'nix-daemon'), init_path, backup=False, mode=0o755, use_sudo=True) sudo('update-rc.d nix-daemon defaults') green('Starting nix-daemon') sudo('/etc/init.d/nix-daemon start') # Setup profile nix_profile = '/etc/profile.d/nix.sh' append(nix_profile, PROFILE_SUFFIX, use_sudo=True) sudo('service nix-daemon restart') # Setup Nix for current user execute('nix.user_setup') green('Done. Remember to log out and back in before using Nix.')
def site(server_name, template_contents=None, template_source=None, enabled=True, check_config=True, **kwargs): """ Require an nginx site. You must provide a template for the site configuration, either as a string (*template_contents*) or as the path to a local template file (*template_source*). :: from fabtools import require CONFIG_TPL = ''' server { listen %(port)d; server_name %(server_name)s %(server_alias)s; root %(docroot)s; access_log /var/log/nginx/%(server_name)s.log; }''' require.nginx.site('example.com', template_contents=CONFIG_TPL, port=80, server_alias='www.example.com', docroot='/var/www/mysite', ) .. seealso:: :py:func:`fabtools.require.files.template_file` """ if not is_installed('nginx-common'): # nginx-common is always installed if nginx exists server() config_filename = '/etc/nginx/sites-available/%s.conf' % server_name context = { 'port': 80, } context.update(kwargs) context['server_name'] = server_name template_file(config_filename, template_contents, template_source, context, use_sudo=True) link_filename = '/etc/nginx/sites-enabled/%s.conf' % server_name if enabled: if not is_link(link_filename): run_as_root("ln -s %(config_filename)s %(link_filename)s" % locals()) # Make sure we don't break the config if check_config: with settings(hide('running', 'warnings'), warn_only=True): if run_as_root('nginx -t').failed: run_as_root("rm %(link_filename)s" % locals()) message = red( "Error in %(server_name)s nginx site config (disabling for safety)" % locals()) abort(message) else: if is_link(link_filename): run_as_root("rm %(link_filename)s" % locals()) reload_service('nginx')