Skip to content

Instantly share code, notes, and snippets.

@justquick
Created October 11, 2010 18:58
Show Gist options
  • Save justquick/621031 to your computer and use it in GitHub Desktop.
Save justquick/621031 to your computer and use it in GitHub Desktop.
Ubuntu update manager for Fabric. Checks for updates, installs them and reboots if needed across multiple servers
"""
Ubuntu update manager for Fabric
Checks for updates, installs them and reboots if needed across multiple servers
Create a "hosts" file alongside this fabfile and put your hosts in there one per line
Updating package list::
fab update
Check for updates::
fab check
Install updates and reboot (if necessary)::
fab upgrade
"""
from fabric.api import sudo, run, env
from fabric.colors import red
from fabric.contrib.files import exists
from fabric.contrib.console import confirm
for host in open('hosts').readlines():
host = host.strip()
if host.startswith('#'):
continue
env.hosts.append(host)
def update():
"""
Updates the package list
"""
sudo('apt-get update -qq')
def check():
"""
Displays package updates needed
"""
run("""apt-get dist-upgrade -s | python -c "import sys; [sys.stderr.write(l) for l in sys.stdin.readlines()[7:] if not (l.startswith('Inst') or l.startswith('Conf'))]" """)
def upgrade():
"""
Upgrades the system, updating packages and reboots if needed
"""
sudo('apt-get -y upgrade')
if exists('/var/run/reboot-required') and confirm('Needs reboot, do it now?'):
print(red('Rebooting now', True))
sudo('reboot')
@zed
Copy link

zed commented Mar 25, 2014

A file object is already an iterator over lines in Python. You don't need .readlines() call

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment