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_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_temporary_directory_as_context_manager(): from burlap.files import is_dir from burlap.require.files import temporary_directory with temporary_directory() as path: assert is_dir(path) with cd(path): run('touch foo') assert not is_dir(path)
def test_temporary_directory_as_function(): from burlap.files import is_dir from burlap.require.files import temporary_directory path1 = temporary_directory() path2 = temporary_directory() assert is_dir(path1) assert is_dir(path2) assert path1 != path2 run('rmdir %s' % quote(path1)) run('rmdir %s' % quote(path2))
def directory(path, use_sudo=False, owner='', group='', mode=''): """ Require a directory to exist. :: from burlap import require require.directory('/tmp/mydir', owner='alice', use_sudo=True) .. note:: This function can be accessed directly from the ``burlap.require`` module for convenience. """ func = use_sudo and run_as_root or run if not is_dir(path): func('mkdir -p "%(path)s"' % locals()) # Ensure correct owner if (owner and _owner(path, use_sudo) != owner) or \ (group and _group(path, use_sudo) != group): func('chown %(owner)s:%(group)s "%(path)s"' % locals()) # Ensure correct mode if mode and _mode(path, use_sudo) != mode: func('chmod %(mode)s "%(path)s"' % locals())
def install_from_oracle_site(version=DEFAULT_VERSION): """ Download tarball from Oracle site and install JDK. :: import burlap # Install Oracle JDK burlap.oracle_jdk.install_from_oracle_site() """ prefix = '/opt' release, build = version.split('-') major, update = release.split('u') if len(update) == 1: update = '0' + update arch = _required_jdk_arch() self_extracting_archive = (major == '6') extension = 'bin' if self_extracting_archive else 'tar.gz' filename = 'jdk-%(release)s-linux-%(arch)s.%(extension)s' % locals() download_path = posixpath.join('/tmp', filename) url = 'http://download.oracle.com/otn-pub/java/jdk/%(version)s/%(filename)s' % locals() _download(url, download_path) # Prepare install dir install_dir = 'jdk1.%(major)s.0_%(update)s' % locals() with cd(prefix): if is_dir(install_dir): run_as_root('rm -rf %s' % quote(install_dir)) # Extract if self_extracting_archive: run('chmod u+x %s' % quote(download_path)) with cd('/tmp'): run_as_root('rm -rf %s' % quote(install_dir)) run_as_root('./%s' % filename) run_as_root('mv %s %s' % (quote(install_dir), quote(prefix))) else: with cd(prefix): run_as_root('tar xzvf %s' % quote(download_path)) # Set up link link_path = posixpath.join(prefix, 'jdk') if is_link(link_path): run_as_root('rm -f %s' % quote(link_path)) run_as_root('ln -s %s %s' % (quote(install_dir), quote(link_path))) # Remove archive run('rm -f %s' % quote(download_path)) _create_profile_d_file(prefix)
def test_create_user(): from burlap.user import create, exists try: create('user1', create_home=False) assert exists('user1') assert not is_dir('/home/user1') finally: sudo_or_dryrun('userdel -r user1')
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 test_directory_creation(): from burlap.require import directory try: directory('testdir') assert is_dir('testdir') assert owner('testdir') == env.user finally: run('rmdir testdir')
def test_git_require_remote_url_and_path(): """ Test working_copy() with remote URL and path """ from burlap.require.git import working_copy try: working_copy(REMOTE_URL, path='wc') assert is_dir('wc') assert is_dir('wc/.git') with cd('wc'): 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' finally: run('rm -rf wc')
def test_git_require_branch(): """ Test checkout of a branch """ from burlap.require.git import working_copy try: working_copy(REMOTE_URL, path='wc', branch='master') assert is_dir('wc') assert is_dir('wc/.git') with cd('wc'): 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' finally: run('rm -rf wc')
def test_git_require_remote_url(): """ Test with remote URL only """ from burlap.require.git import working_copy try: working_copy(REMOTE_URL) assert is_dir('burlap') assert is_dir('burlap/.git') with cd('burlap'): 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' finally: run('rm -rf burlap')
def test_require_virtualenv(): """ Test Python virtualenv creation """ from burlap.require.python import virtualenv try: virtualenv('/tmp/venv') assert is_dir('/tmp/venv') assert is_file('/tmp/venv/bin/python') finally: run('rm -rf /tmp/venv')
def install_from_source(path=DEFAULT_INSTALLATION_PATH, version=DEFAULT_VERSION, mirror=DEFAULT_MIRROR, overwrite=False): """ Install Tomcat from source. :: import burlap # Install Tomcat burlap.tomcat.install_from_source(version='6.0.36') """ from burlap.require import file as require_file from burlap.require.files import directory as require_directory # Tokenize version into parts version_tokens = version.split('.') version_major = version_tokens[0] # Parse the filename and folder file_name = 'apache-tomcat-%s.tar.gz' % version folder_name = 'apache-tomcat-%s' % version # Build the distribution in /tmp with cd('/tmp'): # Make sure we have the tarball downloaded. if not is_file(os.path.join('/tmp/', file_name)): # Otherwise, download the tarball based on our mirror and version. tomcat_url = '%s/dist/tomcat/tomcat-%s/v%s/bin/%s' % (mirror, version_major, version, file_name) # Ensure the file has been downloaded require_file(url=tomcat_url) # Extract the file run('tar -xzf %s' % file_name) # Handle possibility of existing path if is_dir(path): if overwrite is False: # Raise exception as we don't want to overwrite raise OSError("Path %s already exists and overwrite not set." % path) else: # Otherwise, backup the tomcat path backup_installation_path = path + ".backup" if is_dir(backup_installation_path): run_as_root("rm -rf %s" % backup_installation_path) run_as_root("mv %s %s" % (path, backup_installation_path)) """ After all that, let's ensure we have the installation path setup properly and place the install. """ require_directory(path, mode='755', use_sudo=True) run_as_root('mv %s/* %s' % (folder_name, path)) # Now cleanup temp. run("rm -rf %s*" % file_name) # Finally, configure and start Tomcat configure_tomcat(path, overwrite=overwrite) start_tomcat()
def install_from_source(path=DEFAULT_INSTALLATION_PATH, version=DEFAULT_VERSION, mirror=DEFAULT_MIRROR, overwrite=False): """ Install Tomcat from source. :: import burlap # Install Tomcat burlap.tomcat.install_from_source(version='6.0.36') """ from burlap.require import file as require_file from burlap.require.files import directory as require_directory # Tokenize version into parts version_tokens = version.split('.') version_major = version_tokens[0] # Parse the filename and folder file_name = 'apache-tomcat-%s.tar.gz' % version folder_name = 'apache-tomcat-%s' % version # Build the distribution in /tmp with cd('/tmp'): # Make sure we have the tarball downloaded. if not is_file(os.path.join('/tmp/', file_name)): # Otherwise, download the tarball based on our mirror and version. tomcat_url = '%s/dist/tomcat/tomcat-%s/v%s/bin/%s' % ( mirror, version_major, version, file_name) # Ensure the file has been downloaded require_file(url=tomcat_url) # Extract the file run('tar -xzf %s' % file_name) # Handle possibility of existing path if is_dir(path): if overwrite is False: # Raise exception as we don't want to overwrite raise OSError("Path %s already exists and overwrite not set." % path) else: # Otherwise, backup the tomcat path backup_installation_path = path + ".backup" if is_dir(backup_installation_path): run_as_root("rm -rf %s" % backup_installation_path) run_as_root("mv %s %s" % (path, backup_installation_path)) """ After all that, let's ensure we have the installation path setup properly and place the install. """ require_directory(path, mode='755', use_sudo=True) run_as_root('mv %s/* %s' % (folder_name, path)) # Now cleanup temp. run("rm -rf %s*" % file_name) # Finally, configure and start Tomcat configure_tomcat(path, overwrite=overwrite) start_tomcat()
def working_copy(remote_url, path=None, branch="master", update=True, use_sudo=False, user=None): """ Require a working copy of the repository from the ``remote_url``. The ``path`` is optional, and defaults to the last segment of the remote repository URL, without its ``.git`` suffix. If the ``path`` does not exist, this will clone the remote repository and check out the specified branch. If the ``path`` exists and ``update`` is ``True``, it will fetch changes from the remote repository, check out the specified branch, then merge the remote changes into the working copy. If the ``path`` exists and ``update`` is ``False``, it will only check out the specified branch, without fetching remote changesets. :param remote_url: URL of the remote repository (e.g. https://github.com/ronnix/burlap.git). The given URL will be the ``origin`` remote of the working copy. :type remote_url: str :param path: Absolute or relative path of the working copy on the filesystem. If this directory doesn't exist yet, a new working copy is created through ``git clone``. If the directory does exist *and* ``update == True``, a ``git fetch`` is issued. If ``path is None`` the ``git clone`` is issued in the current working directory and the directory name of the working copy is created by ``git``. :type path: str :param branch: Branch or tag to check out. If the given value is a tag name, update must be ``False`` or consecutive calls will fail. :type branch: str :param update: Whether or not to fetch and merge remote changesets. :type update: bool :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 """ command() if path is None: path = remote_url.split('/')[-1] if path.endswith('.git'): path = path[:-4] if is_dir(path, use_sudo=use_sudo): # always fetch changesets from remote and checkout branch / tag git.fetch(path=path, use_sudo=use_sudo, user=user) git.checkout(path=path, branch=branch, use_sudo=use_sudo, user=user) if update: # only 'merge' if update is True git.pull(path=path, use_sudo=use_sudo, user=user) elif not is_dir(path, use_sudo=use_sudo): git.clone(remote_url, path=path, use_sudo=use_sudo, user=user) git.checkout(path=path, branch=branch, use_sudo=use_sudo, user=user) else: raise ValueError("Invalid combination of parameters.")
def check_for_openvz_kernel(): if not is_dir('/proc/vz'): pytest.skip("Kernel does not support OpenVZ")
def working_copy(remote_url, path=None, branch="default", update=True, use_sudo=False, user=None): """ Require a working copy of the repository from the ``remote_url``. The ``path`` is optional, and defaults to the last segment of the remote repository URL. If the ``path`` does not exist, this will clone the remote repository and check out the specified branch. If the ``path`` exists and ``update`` is ``True``, it will pull changes from the remote repository, check out the specified branch, then update the working copy. If the ``path`` exists and ``update`` is ``False``, it will only check out the specified branch, without pulling remote changesets. :param remote_url: URL of the remote repository :type remote_url: str :param path: Absolute or relative path of the working copy on the filesystem. If this directory doesn't exist yet, a new working copy is created through ``hg clone``. If the directory does exist *and* ``update == True``, a ``hg pull && hg up`` is issued. If ``path is None`` the ``hg clone`` is issued in the current working directory and the directory name of the working copy is created by ``hg``. :type path: str :param branch: Branch or tag to check out. If the given value is a tag name, update must be ``False`` or consecutive calls will fail. :type branch: str :param update: Whether or not to pull and update remote changesets. :type update: bool :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 """ command() if path is None: path = remote_url.split('/')[-1] if is_dir(path, use_sudo=use_sudo): mercurial.pull(path, use_sudo=use_sudo, user=user) if update: mercurial.update(path=path, branch=branch, use_sudo=use_sudo, user=user) elif not is_dir(path, use_sudo=use_sudo): mercurial.clone(remote_url, path=path, use_sudo=use_sudo, user=user) mercurial.update(path=path, branch=branch, use_sudo=use_sudo, user=user) else: raise ValueError("Invalid combination of parameters.")