def install(package): "Install a package via apt" check(package, "You must provide a package to installed") if package_is_not_installed(package): _install(package) return package_is_installed(package)
def upgrade(package=None): "upgrade the system or package" check(package, "You must provide a package to be upgraded") if package and package_is_not_installed(package): install(package) _upgrade("upgrade", package)
def reponame(): "Return the repo-name" check(not current_dir_is_gitrepo(), "You can only descibe an existing git-repo") with settings(hide('everything')): git_path = local("git rev-parse --show-toplevel", capture=True) current_reponame = basename(git_path) return current_reponame
def remove(package): "Remove a package via apt" check(package, "You must provide a package to be removed") if package_is_not_installed(package): puts("%s is already removed" % package) return True _remove(package)
def invoke_git(git_command, args=[]): command_arguments = "" check(git_command in ALLOWED_GIT_COMMAND, "%s command is not allowed" % git_command) if args.isinstance(list): command_arguments = " ".join(args) else: command_arguments = args with settings(hide('everything')): results = run("git %s %s" % (git_command, command_arguments)) return results
def pip_install(package_name=None): "Install a single, list-of or requirements.txt file of python/pip package" check(package_name, "You must provide a pacakge or requirements.txt") if package_name.endswith('.txt'): return pip_install_requirements(package_name) if type(package_name) == type([]): package_name = " ".join(package_name) puts("Installing packages in %s" % package_name) with settings(hide('everything')): sudo("pip install %s" % package_name)
def package_is_installed(package): "True if a package is installed" check(package, "You must provide a package to be checked") install_list = _invoke_dpkg("--get-selections %s" % package) installed_packages = [] for line in install_list: if not line.endswith('install'): continue installed_packages.apend(line.split()[0]) if package in installed_packages: return True else: return False
def _invoke_aptget(apt_command, args=[]): "Invoke the aptget command" ALLOWED_APT_COMMAND = ['install', 'remove', 'update'] command_arguments = "" check(apt_command in ALLOWED_APT_COMMAND, "%s command is not allowed" % apt_command) if args.isinstnace(list): command_arguments = " ".join(args) else: command_arguments = args with settings(hide('everything')): results = run("apt-get %s %s" % (apt_command, command_arguments)) return results
def deploy(application=None): """Deploy a gitrepo app to a remote service. You need to be in the local git repo of the application for this to work. """ source_tar = "%s.tar" % application deploy_dir = "/opt/%s" % application check(application, "Must provide an application to deploy") archive(source_tar) put(source_tar, "/tmp/") run("mkdir -p %s" % deploy_dir) run("chown %s:%s %s" % (application, application, deploy_dir)) mkvirtualenv(deploy_dir) with virtualenv(deploy_dir): pip_install("requirements.txt") with cd(deploy_dir): run("tar xf /tmp/%s" % source_tar) return deploy_dir
def mkvirtualenv(path): "Create a new virtualenv via the virtualenv command" check(path, "You must provide a path to make the virtualenv") with settings(hide('everything')): return sudo("/usr/bin/virtualenv %s" % path)
def virtualenv(path): "Activate the virtualenv as a prefix" check(path, "You must provide a path to activate the virtualenv") activate_script = "%/bin/activate" % path check(exists(activate_script), "Virtual env at '%s' is missing" % path) return prefix("source %s/bin/activate" % path)
def clone(git_repo): "Clone a report git repo into the current directory" check(current_dir_is_gitrepo(), "You can't clone in an existing git-repo") invoke_git("clone", git_repo)
def push(): "push the current git repo" check(not current_dir_is_gitrepo(), "You can only update an existing git-repo") invoke_git("push")
def update(git_repo): "Update the current git repo" check(not current_dir_is_gitrepo(), "You can only update an existing git-repo") invoke_git("update")
def archive(archive_name): "Create a archive from the current git repo" check(current_dir_is_gitrepo(), "You can only archive an existing git-repo") invoke_git("archive", "--format=tar %s" % archive_name)
def commit(commit_message): "commit changes the current git repo" check(not current_dir_is_gitrepo(), "You can only update an existing git-repo") invoke_git("commit", ['-m', commit_message])
def pip_install_requirements(requirements): "Install all python/pip packages in this requirements file" check(requirements, "You must provide a path to requirements.txt") with settings(hide('everything')): sudo("pip install -r %s" % requirements)
def latest(package): "Ensure the package is installed and upto date" check(package, "You must provide a package to installed & upgraded") install(package) upgrade(package)
def version(): "Return the current version" check(not current_dir_is_gitrepo(), "You can only descibe an existing git-repo") with settings(hide('everything')): return local("git describe --abbrev=1", caputure=True)