17c8ea99fecf140093f2fe3dcd0ab6b5bf65b1bbembligh#!/usr/bin/python
243da086dd7f20afdf9a67a70122c84e14a090c4bmbligh
343da086dd7f20afdf9a67a70122c84e14a090c4bmbligh# change_protection_level.py "No protection" machine1 machine2 machine3
443da086dd7f20afdf9a67a70122c84e14a090c4bmbligh
543da086dd7f20afdf9a67a70122c84e14a090c4bmblighimport sys, optparse, traceback, pwd, os
643da086dd7f20afdf9a67a70122c84e14a090c4bmblighimport common
743da086dd7f20afdf9a67a70122c84e14a090c4bmblighfrom autotest_lib.cli import rpc, host
843da086dd7f20afdf9a67a70122c84e14a090c4bmbligh
943da086dd7f20afdf9a67a70122c84e14a090c4bmblighusage = 'usage: %prog [options] new_protection_level machine1 machine2 ...'
1043da086dd7f20afdf9a67a70122c84e14a090c4bmblighparser = optparse.OptionParser(usage=usage)
1143da086dd7f20afdf9a67a70122c84e14a090c4bmblighparser.add_option('-w', '--web',
1243da086dd7f20afdf9a67a70122c84e14a090c4bmbligh                  help='Autotest server to use (i.e. "autotest")')
1343da086dd7f20afdf9a67a70122c84e14a090c4bmbligh
1443da086dd7f20afdf9a67a70122c84e14a090c4bmblighoptions, leftover_args = parser.parse_args()
1543da086dd7f20afdf9a67a70122c84e14a090c4bmblighassert len(leftover_args) > 1, 'Must pass protection level and hosts'
1643da086dd7f20afdf9a67a70122c84e14a090c4bmblighprotection_level = leftover_args[0]
1743da086dd7f20afdf9a67a70122c84e14a090c4bmbligh
1843da086dd7f20afdf9a67a70122c84e14a090c4bmblighuser = pwd.getpwuid(os.getuid())[0]
1943da086dd7f20afdf9a67a70122c84e14a090c4bmblighautotest_host = rpc.get_autotest_server(options.web)
20347aaf4b6830f64709ba76217c2db702a7c1a98dScott Zawalskiafe_proxy = rpc.afe_comm(autotest_host, '/afe/server/noauth/rpc/')
2143da086dd7f20afdf9a67a70122c84e14a090c4bmbligh
2243da086dd7f20afdf9a67a70122c84e14a090c4bmblighhosts = afe_proxy.run('get_hosts', hostname__in=leftover_args[1:])
2343da086dd7f20afdf9a67a70122c84e14a090c4bmblighfor host in hosts:
241ef218db76c473c28627377d8f50d6e6c6743289mbligh    try:
251ef218db76c473c28627377d8f50d6e6c6743289mbligh        afe_proxy.run('modify_host', host['id'], protection=protection_level)
261ef218db76c473c28627377d8f50d6e6c6743289mbligh    except Exception, exc:
271ef218db76c473c28627377d8f50d6e6c6743289mbligh        print 'For host %s:', host['hostname']
281ef218db76c473c28627377d8f50d6e6c6743289mbligh        traceback.print_exc()
291ef218db76c473c28627377d8f50d6e6c6743289mbligh    else:
301ef218db76c473c28627377d8f50d6e6c6743289mbligh        print 'Host %s succeeded' % host['hostname']
3143da086dd7f20afdf9a67a70122c84e14a090c4bmbligh
3243da086dd7f20afdf9a67a70122c84e14a090c4bmblighprint 'Invalid hosts:'
3343da086dd7f20afdf9a67a70122c84e14a090c4bmblighprint ','.join(set(leftover_args[1:]) - set(host['hostname'] for host in hosts))
34