def install_sudo(): """Install sudo, if it's not present""" import vars vars = vars.Vars() with settings(warn_only=True): if run("which sudo").failed: run(vars.os.package_install_cmd % "sudo")
def install(package, force=False): """Install a package from the repositories""" import vars vars = vars.Vars() if not is_installed(package) or force: sudo(vars.os.package_refresh_cmd) sudo(vars.os.package_install_cmd % package)
def add_to_path(path): """Add a new directory to PATH for the default shell""" from fabric.contrib.files import append import vars vars = vars.Vars() for file in [ vars.os.default_shell_config, vars.os.default_loginshell_config ]: append(file, "export PATH=$PATH:" + path, use_sudo=True)
def is_installed(package): """Check if package is installed""" import vars vars = vars.Vars() # This will work with "package.<extension> and "package" package_name = os.path.splitext(package)[0] with quiet(), settings(warn_only=True): if sudo(vars.os.package_installed_cmd + " " + package_name).failed: return False else: return True
def migrate_node(proxy_url=None): """Migrate node from Puppet 3.x to 4.x""" import package, puppet, vars vars = vars.Vars() package.remove("puppet facter") sudo("rm -f /etc/apt/sources.list.d/puppetlabs.list") if exists("/var/lib/puppet"): sudo("mv /var/lib/puppet /var/lib/puppet.old.3") if exists("/etc/puppet"): sudo("mv /etc/puppet /etc/puppet.old.3") puppet.setup_agent4(proxy_url=proxy_url) if vars.osfamily == 'Debian': package.autoremove() puppet.resolve_aptitude_conflicts()
def download_and_install(url, package_name=None, proxy_url=None): """Download a package from URL and install it. Use package_name to manually define the name of the installed package and to prevent unnecessary reinstalls.""" import vars vars = vars.Vars() with cd("/tmp"): parsed = urlparse(url) package_file = re.split("/", parsed.path)[1] if not package_name: package_name = package_file.rpartition(".")[0] if not exists(package_file): # wget is not universally available out of the box if proxy_url: cmd = "curl -Os -x " + proxy_url + " " + url else: cmd = "curl -Os " + url run(cmd) if not is_installed(package_name): sudo(vars.os.package_local_install_cmd % package_file)
def install_puppetlabs_release_package(pc, proxy_url=None): """Install Puppetlabs apt repo release package""" import package, vars vars = vars.Vars() os = vars.lsbdistcodename if vars.osfamily == "Debian": package.download_and_install( "https://apt.puppetlabs.com/puppetlabs-release-pc" + pc + "-" + os + ".deb", "puppetlabs-release-pc" + pc, proxy_url=proxy_url) elif vars.osfamily == "RedHat": if vars.operatingsystem in ["RedHat", "CentOS", "Scientific"]: oscode = "el" elif vars.operatingsystem == "Fedora": oscode = "fedora" url = "https://yum.puppetlabs.com/puppetlabs-release-pc" + pc + "-" + oscode + "-" + vars.operatingsystemmajrelease + ".noarch.rpm" package.download_and_install(url, "puppetlabs-release-pc" + pc, proxy_url=proxy_url)
def stop(name): """Stop a service""" import vars vars = vars.Vars() sudo(vars.os.service_stop_cmd % name)
def status(name): """View status of a service""" import vars vars = vars.Vars() sudo(vars.os.service_status_cmd % name)
def restart(name): """Restart a service""" import vars vars = vars.Vars() sudo(vars.os.service_restart_cmd % name)
def autoremove(): """Remove obsolete packages, such as unused kernel images""" import vars vars = vars.Vars() with settings(hide("user", "stdout")): sudo(vars.os.package_autoremove_cmd)
def remove(package): """Remove a package that may or may not be installed""" import vars vars = vars.Vars() sudo(vars.os.package_remove_cmd % package)
def simple(): """Install latest (security) updates""" import vars vars = vars.Vars() sudo(vars.os.package_refresh_cmd) sudo(vars.os.package_upgrade_cmd)
def install(): """Install Git""" import package, vars vars = vars.Vars() package.install(vars.os.git_package_name)