def pull(path, use_sudo=False, user=None): """ Pull changes from the default remote repository. :param path: Path of the working copy directory. This directory must exist and be a Mercurial working copy with a default remote to pull from. :type path: str :param use_sudo: If ``True`` execute ``hg`` with :func:`fabric.operations.sudo`, else with :func:`fabric.operations.run`. :type use_sudo: bool :param user: If ``use_sudo is True``, run :func:`fabric.operations.sudo` with the given user. If ``use_sudo is False`` this parameter has no effect. :type user: str """ if not path: raise ValueError("Path to the working copy is needed to pull from a " "remote repository.") cmd = 'hg pull' with cd(path): if use_sudo and user is None: run_as_root(cmd) elif use_sudo: sudo(cmd, user=user) else: run(cmd)
def clone(remote_url, path=None, use_sudo=False, user=None): """ Clone a remote Git repository into a new directory. :param remote_url: URL of the remote repository to clone. :type remote_url: str :param path: Path of the working copy directory. Must not exist yet. :type path: str :param use_sudo: If ``True`` execute ``git`` with :func:`fabric.operations.sudo`, else with :func:`fabric.operations.run`. :type use_sudo: bool :param user: If ``use_sudo is True``, run :func:`fabric.operations.sudo` with the given user. If ``use_sudo is False`` this parameter has no effect. :type user: str """ cmd = 'git clone --quiet %s' % remote_url if path is not None: cmd = cmd + ' %s' % path if use_sudo and user is None: run_as_root(cmd) elif use_sudo: sudo(cmd, user=user) else: run(cmd)
def update(path, branch="default", use_sudo=False, user=None, force=False): """ Merge changes to a working copy and/or switch branches. :param path: Path of the working copy directory. This directory must exist and be a Mercurial working copy. :type path: str :param use_sudo: If ``True`` execute ``hg`` with :func:`fabric.operations.sudo`, else with :func:`fabric.operations.run`. :type use_sudo: bool :param user: If ``use_sudo is True``, run :func:`fabric.operations.sudo` with the given user. If ``use_sudo is False`` this parameter has no effect. :type user: str """ cmd = "hg up %s" % branch with cd(path): if use_sudo and user is None: run_as_root(cmd) elif use_sudo: sudo(cmd, user=user) else: run(cmd)
def locales(names): """ Require the list of locales to be available. Raises UnsupportedLocales if some of the required locales are not supported. """ family = distrib_family() if family == 'debian': command = 'dpkg-reconfigure --frontend=noninteractive locales' if distrib_id() == 'Ubuntu': config_file = '/var/lib/locales/supported.d/local' if not is_file(config_file): run_as_root('touch %s' % config_file) else: config_file = '/etc/locale.gen' _locales_generic(names, config_file=config_file, command=command) elif family in ['arch', 'gentoo']: _locales_generic(names, config_file='/etc/locale.gen', command='locale-gen') elif distrib_family() == 'redhat': _locales_redhat(names) else: raise UnsupportedFamily( supported=['debian', 'arch', 'gentoo', 'redhat'])
def repolist(status='', media=None): """ Get the list of ``yum`` repositories. Returns enabled repositories by default. Extra *status* may be passed to list disabled repositories if necessary. Media and debug repositories are kept disabled, except if you pass *media*. :: import burlap # Install a package that may be included in disabled repositories burlap.rpm.install('vim', burlap.rpm.repolist('disabled')) """ manager = MANAGER with settings(hide('running', 'stdout')): if media: repos = run_as_root( "%(manager)s repolist %(status)s | sed '$d' | sed -n '/repo id/,$p'" % locals()) else: repos = run_as_root( "%(manager)s repolist %(status)s | sed '/Media\\|Debug/d' | sed '$d' | sed -n '/repo id/,$p'" % locals()) return [line.split(' ')[0] for line in repos.splitlines()[1:]]
def install(packages, update=False, options=None): """ Install one or more packages. If *update* is ``True``, the package definitions will be updated first, using :py:func:`~burlap.opkg.update_index`. Extra *options* may be passed to ``opkg`` if necessary. Example:: import burlap # Update index, then install a single package burlap.opkg.install('build-essential', update=True) # Install multiple packages burlap.opkg.install([ 'mc', 'htop', ]) """ manager = MANAGER if update: update_index() if options is None: options = [] if not isinstance(packages, basestring): packages = " ".join(packages) options.append("--verbosity=0") options = " ".join(options) cmd = '%(manager)s install %(options)s %(packages)s' % locals() run_as_root(cmd, pty=False)
def test_add_apt_key_with_key_id_from_specific_key_server(): from burlap.deb import add_apt_key try: add_apt_key(keyid='7BD9BF62', keyserver='keyserver.ubuntu.com') run_as_root('apt-key finger | grep -q 7BD9BF62') finally: run_as_root('apt-key del 7BD9BF62', quiet=True)
def groupinstall(group, options=None): """ Install a group of packages. You can use ``yum grouplist`` to get the list of groups. Extra *options* may be passed to ``yum`` if necessary like (e.g. ``['--nogpgcheck', '--exclude=package']``). :: import burlap # Install development packages burlap.rpm.groupinstall('Development tools') """ manager = MANAGER if options is None: options = [] elif isinstance(options, str): options = [options] options = " ".join(options) run_as_root('%(manager)s %(options)s groupinstall "%(group)s"' % locals(), pty=False)
def set_hostname(hostname, persist=True): """ Set the hostname. """ run_as_root('hostname %s' % hostname) if persist: run_as_root('echo %s >/etc/hostname' % hostname)
def test_git_require_sudo_user(gituser): """ Test working_copy() with sudo as a user """ from burlap.require.git import working_copy username, groupname = gituser with cd('/tmp'): try: working_copy(REMOTE_URL, path='wc_nobody', use_sudo=True, user=username) assert is_dir('wc_nobody') assert is_dir('wc_nobody/.git') with cd('wc_nobody'): remotes = sudo('git remote -v', user=username) assert remotes == \ 'origin\thttps://github.com/chrisspen/burlap.git (fetch)\r\n' \ 'origin\thttps://github.com/chrisspen/burlap.git (push)' assert _current_branch() == 'master' assert owner('wc_nobody') == username assert group('wc_nobody') == groupname finally: run_as_root('rm -rf wc_nobody')
def test_add_apt_key_without_key_id_from_url(): from burlap.deb import add_apt_key try: add_apt_key(url='http://repo.varnish-cache.org/debian/GPG-key.txt') run_as_root('apt-key finger | grep -q C4DEFFEB') finally: run_as_root('apt-key del C4DEFFEB', quiet=True)
def test_git_require_sudo(): """ Test working_copy() with sudo """ from burlap.require.git import working_copy try: working_copy(REMOTE_URL, path='wc_root', use_sudo=True) assert is_dir('wc_root') assert is_dir('wc_root/.git') with cd('wc_root'): remotes = run('git remote -v') assert remotes == \ 'origin\thttps://github.com/chrisspen/burlap.git (fetch)\r\n' \ 'origin\thttps://github.com/chrisspen/burlap.git (push)' assert _current_branch() == 'master' assert owner('wc_root') == 'root' assert group('wc_root') == 'root' finally: run_as_root('rm -rf wc_root')
def test_require_deb_key_from_specific_keyserver(): from burlap.require.deb import key as require_key try: require_key(keyid='7BD9BF62', keyserver='keyserver.ubuntu.com') run_as_root('apt-key finger | grep -q 7BD9BF62') finally: run_as_root('apt-key del 7BD9BF62', quiet=True)
def install_pip(python_cmd='python', use_sudo=True): """ Install the latest version of `pip`_, using the given Python interpreter. :: import burlap if not burlap.python.is_pip_installed(): burlap.python.install_pip() .. note:: pip is automatically installed inside a virtualenv, so there is no need to install it yourself in this case. .. _pip: http://www.pip-installer.org/ """ with cd('/tmp'): download(GET_PIP_URL) command = '%(python_cmd)s get-pip.py' % locals() if use_sudo: run_as_root(command, pty=False) else: run(command, pty=False) run('rm -f get-pip.py')
def upgrade(): """ Upgrade all packages. """ manager = MANAGER cmd = 'upgrade' run_as_root("%(manager)s %(cmd)s" % locals(), pty=False)
def install(packages, update=False, options=None): """ Install one or more Arch Linux packages. If *update* is ``True``, the package definitions will be updated first, using :py:func:`~burlap.arch.update_index`. Extra *options* may be passed to ``pacman`` if necessary. Example:: import burlap # Update index, then install a single package burlap.arch.install('mongodb', update=True) # Install multiple packages burlap.arch.install([ 'mongodb', 'python-pymongo', ]) """ manager = pkg_manager() if update: update_index() if options is None: options = [] if not isinstance(packages, basestring): packages = " ".join(packages) options = " ".join(options) cmd = '%(manager)s -S %(options)s %(packages)s' % locals() run_as_root(cmd, pty=False)
def clone(self, remote_url, path=None, use_sudo=False, user=None): """ Clone a remote Git repository into a new directory. :param remote_url: URL of the remote repository to clone. :type remote_url: str :param path: Path of the working copy directory. Must not exist yet. :type path: str :param use_sudo: If ``True`` execute ``git`` with :func:`fabric.operations.sudo`, else with :func:`fabric.operations.run`. :type use_sudo: bool :param user: If ``use_sudo is True``, run :func:`fabric.operations.sudo` with the given user. If ``use_sudo is False`` this parameter has no effect. :type user: str """ cmd = 'git clone --quiet %s' % remote_url if path is not None: cmd = cmd + ' %s' % path if use_sudo and user is None: run_as_root(cmd) elif use_sudo: sudo(cmd, user=user) else: run(cmd)
def ismounted(device): """ Check if partition is mounted Example:: from burlap.disk import ismounted if ismounted('/dev/sda1'): print ("disk sda1 is mounted") """ # Check filesystem with settings(hide('running', 'stdout')): res = run_as_root('mount') for line in res.splitlines(): fields = line.split() if fields[0] == device: return True # Check swap with settings(hide('running', 'stdout')): res = run_as_root('swapon -s') for line in res.splitlines(): fields = line.split() if fields[0] == device: return True return False
def test_require_deb_key_from_url(): from burlap.require.deb import key as require_key try: require_key(keyid='C4DEFFEB', url='http://repo.varnish-cache.org/debian/GPG-key.txt') run_as_root('apt-key finger | grep -q C4DEFFEB') finally: run_as_root('apt-key del C4DEFFEB', quiet=True)
def _server_redhat(version, password): from burlap.require.rpm import package as require_rpm_package require_rpm_package('mysql-server') run_as_root('chkconfig --levels 235 mysqld on') run_as_root('service mysqld start') _require_root_password(password)
def test_require_deb_key_from_file(): from burlap.require.deb import key as require_key try: run('wget http://repo.varnish-cache.org/debian/GPG-key.txt -O /tmp/tmp.burlap.test.key') require_key(keyid='C4DEFFEB', filename='/tmp/tmp.burlap.test.key') run_as_root('apt-key finger | grep -q C4DEFFEB') finally: run_as_root('apt-key del C4DEFFEB', quiet=True)
def test_add_apt_key_without_key_id_from_file(): from burlap.deb import add_apt_key try: run('wget http://repo.varnish-cache.org/debian/GPG-key.txt -O /tmp/tmp.burlap.test.key') add_apt_key(filename='/tmp/tmp.burlap.test.key') run_as_root('apt-key finger | grep -q C4DEFFEB') finally: run_as_root('apt-key del C4DEFFEB', quiet=True)
def upgrade(full=False): """ Upgrade all packages. """ manager = MANAGER cmds = {'pkgin': {False: 'uprade', True: 'full-upgrade'}} cmd = cmds[manager][full] run_as_root("%(manager)s -y %(cmd)s" % locals())
def update_config(): """ Reread and update supervisor job configurations. Less heavy-handed than a full reload, as it doesn't restart the backend supervisor process and all managed processes. """ run_as_root("supervisorctl update")
def test_add_apt_key_without_key_id_from_file(): from burlap.deb import add_apt_key try: run('wget http://repo.varnish-cache.org/debian/GPG-key.txt -O /tmp/tmp.burlap.test.key' ) add_apt_key(filename='/tmp/tmp.burlap.test.key') run_as_root('apt-key finger | grep -q C4DEFFEB') finally: run_as_root('apt-key del C4DEFFEB', quiet=True)
def test_require_deb_key_from_file(): from burlap.require.deb import key as require_key try: run('wget http://repo.varnish-cache.org/debian/GPG-key.txt -O /tmp/tmp.burlap.test.key' ) require_key(keyid='C4DEFFEB', filename='/tmp/tmp.burlap.test.key') run_as_root('apt-key finger | grep -q C4DEFFEB') finally: run_as_root('apt-key del C4DEFFEB', quiet=True)
def upgrade(kernel=False): """ Upgrade all packages, including obsoletes. Exclude *kernel* upgrades by default. """ manager = MANAGER cmds = {'yum -y --color=never': {False: '--exclude=kernel* upgrade', True: 'upgrade'}} cmd = cmds[manager][kernel] run_as_root("%(manager)s %(cmd)s" % locals())
def update_package(package, local=False, npm='npm'): """ Update a Node.js package. If *local* is ``True``, the package will be updated locally. """ if local: run('%(npm)s update -l %(package)s' % locals()) else: run_as_root('HOME=/root %(npm)s update -g %(package)s' % locals())
def upgrade(safe=True): """ Upgrade all packages. """ manager = MANAGER if safe: cmd = 'upgrade' else: cmd = 'dist-upgrade' run_as_root("%(manager)s --assume-yes %(cmd)s" % locals(), pty=False)
def update(kernel=False): """ Upgrade all packages, skip obsoletes if ``obsoletes=0`` in ``yum.conf``. Exclude *kernel* upgrades by default. """ manager = MANAGER cmds = {'yum -y --color=never': {False: '--exclude=kernel* update', True: 'update'}} cmd = cmds[manager][kernel] run_as_root("%(manager)s %(cmd)s" % locals())
def gituser(): from burlap.require import user username = '******' groupname = 'gitgroup' user(username, group=groupname) yield username, groupname run_as_root('userdel -r %s' % username)
def update_index(quiet=True): """ Update pacman package definitions. """ manager = pkg_manager() if quiet: with settings(hide('running', 'stdout', 'stderr', 'warnings'), warn_only=True): run_as_root("%(manager)s -Sy" % locals()) else: run_as_root("%(manager)s -Sy" % locals())
def test_initial_owner_requirement(users): from burlap.require import directory try: directory('testdir', owner='testuser', use_sudo=True) assert is_dir('testdir') assert owner('testdir') == 'testuser' finally: run_as_root('rmdir testdir')
def repository(name): """ Require a repository. Aimed for 3rd party repositories. *Name* currently only supports EPEL and RPMforge. Example:: from burlap import require # RPMforge packages for CentOS 6 require.rpm.repository('rpmforge') """ name = name.lower() epel_url = 'http://download.fedoraproject.org/pub/epel' rpmforge_url = 'http://packages.sw.be/rpmforge-release/rpmforge-release' rpmforge_version = '0.5.2-2' arch = get_arch() try: release = int(str(distrib_release())) except ValueError: release = int(float(str(distrib_release()))) if release == 6: epel_version = '6-8' elif release == 5: epel_version = '5-4' if name == 'rpmforge' and arch == 'i386': arch = 'i686' supported = { 'rpmforge': { '%(arch)s' % locals(): { '6': '%(rpmforge_url)s-%(rpmforge_version)s.el6.rf.i686.rpm' % locals(), '5': '%(rpmforge_url)s-%(rpmforge_version)s.el5.rf.x86_64.rpm' % locals(), }, }, 'epel': { '%(arch)s' % locals(): { '6': '%(epel_url)s/6/%(arch)s/epel-release-%(epel_version)s.noarch.rpm' % locals(), '5': '%(epel_url)s/5/%(arch)s/epel-release-%(epel_version)s.noarch.rpm' % locals(), } }, } keys = { 'rpmforge': 'http://apt.sw.be/RPM-GPG-KEY.dag.txt', 'epel': '%(epel_url)s/RPM-GPG-KEY-EPEL-%(release)s' % locals(), } repo = supported[name][str(arch)][str(release)] key = keys[name] with settings(hide('warnings'), warn_only=True): run_as_root('rpm --import %(key)s' % locals()) run_as_root('rpm -Uh %(repo)s' % locals())
def update_index(quiet=True): """ Update Portage package definitions. """ manager = MANAGER if quiet: with settings(hide('running', 'stdout', 'stderr', 'warnings'), warn_only=True): run_as_root("%(manager)s --sync" % locals()) else: run_as_root("%(manager)s --sync" % locals())
def modify(name, comment=None, home=None, move_current_home=False, group=None, extra_groups=None, login_name=None, password=None, shell=None, uid=None, ssh_public_keys=None, non_unique=False): """ Modify an existing user. *ssh_public_keys* can be a (local) filename or a list of (local) filenames of public keys that should be added to the user's SSH authorized keys (see :py:func:`burlap.user.add_ssh_public_keys`). Example:: import burlap if burlap.user.exists('alice'): burlap.user.modify('alice', shell='/bin/sh') """ args = [] if comment: args.append('-c %s' % quote(comment)) if home: args.append('-d %s' % quote(home)) if move_current_home: args.append('-m') if group: args.append('-g %s' % quote(group)) if extra_groups: groups = ','.join(quote(group) for group in extra_groups) args.append('-G %s' % groups) if login_name: args.append('-l %s' % quote(login_name)) if password: crypted_password = _crypt_password(password) args.append('-p %s' % quote(crypted_password)) if shell: args.append('-s %s' % quote(shell)) if uid: args.append('-u %s' % quote(uid)) if non_unique: args.append('-o') if args: args.append(name) args = ' '.join(args) run_as_root('usermod %s' % args) if ssh_public_keys: if isinstance(ssh_public_keys, basestring): ssh_public_keys = [ssh_public_keys] add_ssh_public_keys(name, ssh_public_keys)
def groupupdate(group, options=None): """ Update an existing software group, skip obsoletes if ``obsoletes=1`` in ``yum.conf``. Extra *options* may be passed to ``yum`` if necessary. """ manager = MANAGER if options is None: options = [] elif isinstance(options, str): options = [options] options = " ".join(options) run_as_root('%(manager)s %(options)s groupupdate "%(group)s"' % locals())
def set_sysctl(key, value): """ Set a kernel parameter. Example:: import burlap # Protect from SYN flooding attack burlap.system.set_sysctl('net.ipv4.tcp_syncookies', 1) """ run_as_root('/sbin/sysctl -n -e -w %(key)s=%(value)s' % locals())
def uninstall(packages, options=None): """ Remove one or more Arch Linux packages. Extra *options* may be passed to ``pacman`` if necessary. """ manager = pkg_manager() if options is None: options = [] if not isinstance(packages, basestring): packages = " ".join(packages) options = " ".join(options) cmd = '%(manager)s -R %(options)s %(packages)s' % locals() run_as_root(cmd, pty=False)
def groupuninstall(group, options=None): """ Remove an existing software group. Extra *options* may be passed to ``yum`` if necessary. """ manager = MANAGER if options is None: options = [] elif isinstance(options, str): options = [options] options = " ".join(options) run_as_root('%(manager)s %(options)s groupremove "%(group)s"' % locals())
def test_file_changes_ownership(): from burlap.files import owner from burlap.require.files import file as require_file try: run('touch foo') assert owner('foo') == env.user require_file('foo', use_sudo=True) assert owner('foo') == 'root' finally: run_as_root('rm -f foo')
def _install_from_scratch(python_cmd, use_sudo): """ Install setuptools from scratch using installer """ with cd("/tmp"): download(EZ_SETUP_URL) command = '%(python_cmd)s ez_setup.py' % locals() if use_sudo: run_as_root(command) else: run(command) run('rm -f ez_setup.py')