#!/usr/bin/env python
import subprocess
import pipes
import json
import sys
import os
import re
CODENAME='sid'
REVIEW_CMD=['apt', 'full-upgrade']
UPGRADE_CMD=['apt', 'full-upgrade', '-y']
def make_remote_command(cmd):
return ' '.join([pipes.quote(token) for token in cmd])
def get_candidate_hosts():
remote_command = make_remote_command(REVIEW_CMD)
command = ['salt', '--output=json', '--static', '-G', 'lsb_distrib_codename:%s' % CODENAME, 'cmd.run', remote_command]
try:
raw_output = subprocess.check_output(command)
except subprocess.CalledProcessError as cpe:
# So what, punk?
raw_output = cpe.output
json_output = json.loads(raw_output)
return json_output
def get_hosts_to_actually_upgrade():
hosts_to_actually_upgrade = []
json_output = get_candidate_hosts()
sorted_hosts = json_output.keys()
sorted_hosts.sort()
for host in sorted_hosts:
print host + ':'
lines = json_output[host].split('\n')
for line in lines[:-1]:
if line:
rem = re.search('apt does not have a stable CLI interface', line)
if not rem:
print ' ' + line
sys.stdout.write(' Do you want to continue? [y/N] ')
reply = raw_input().lower()
if reply == 'y':
hosts_to_actually_upgrade.append(host)
return hosts_to_actually_upgrade
def upgrade(hosts):
if hosts:
remote_command = make_remote_command(UPGRADE_CMD)
command = ['salt', '-L', ','.join(hosts), '-b', '1', 'cmd.run', remote_command, 'env={"DEBIAN_FRONTEND": "noninteractive"}']
print command
os.execvp('salt', command)
def main():
hosts_to_actually_upgrade = get_hosts_to_actually_upgrade()
if hosts_to_actually_upgrade:
print ''
print 'The following hosts are about to be upgraded:'
for host in hosts_to_actually_upgrade:
print ' - %s' % host
sys.stdout.write('Go? [y/N] ')
reply = raw_input().lower()
if reply == 'y':
upgrade(hosts_to_actually_upgrade)
if __name__ == '__main__':
main()