1#!/usr/bin/python
2
3# change_protection_level.py "No protection" machine1 machine2 machine3
4
5import sys, optparse, traceback, pwd, os
6import common
7from autotest_lib.cli import rpc, host
8
9usage = 'usage: %prog [options] new_protection_level machine1 machine2 ...'
10parser = optparse.OptionParser(usage=usage)
11parser.add_option('-w', '--web',
12                  help='Autotest server to use (i.e. "autotest")')
13
14options, leftover_args = parser.parse_args()
15assert len(leftover_args) > 1, 'Must pass protection level and hosts'
16protection_level = leftover_args[0]
17
18user = pwd.getpwuid(os.getuid())[0]
19autotest_host = rpc.get_autotest_server(options.web)
20afe_proxy = rpc.afe_comm(autotest_host, '/afe/server/noauth/rpc/')
21
22hosts = afe_proxy.run('get_hosts', hostname__in=leftover_args[1:])
23for host in hosts:
24    try:
25        afe_proxy.run('modify_host', host['id'], protection=protection_level)
26    except Exception, exc:
27        print 'For host %s:', host['hostname']
28        traceback.print_exc()
29    else:
30        print 'Host %s succeeded' % host['hostname']
31
32print 'Invalid hosts:'
33print ','.join(set(leftover_args[1:]) - set(host['hostname'] for host in hosts))
34