• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python
2# Copyright Google, Martin J. Bligh <mbligh@google.com>, Jan 2009
3import os, sys
4import common
5from autotest_lib.server import frontend
6
7try:
8    old = frontend.AFE(web_server='http://' + sys.argv[1])
9    new = frontend.AFE(web_server='http://' + sys.argv[2])
10
11    hostname = sys.argv[3]
12    print 'Migrating %s ...' % hostname
13
14    old_host = old.get_hosts(hostname=hostname)[0]
15    print old_host
16except Exception:
17    print "Usage: atest_migrate_host <old_server> <new_server> <hostname>"
18    raise
19    sys.exit(1)
20
21
22# Create host
23
24new_host = new.create_host(hostname=hostname, locked=True)
25
26# Deal with labels
27old_host_labels = old_host.get_labels()
28for label in old_host_labels:
29    # Create any non-existant labels
30    if not new.get_labels(name=label.name):
31        print label
32        new_label = new.create_label(name=label.name,
33                                     platform=label.platform,
34                                     only_if_needed=label.only_if_needed)
35    # Add any missing labels to the machine
36    if not [l for l in new_host.get_labels() if l.name == label.name]:
37        new_host.add_labels([label.name])
38
39# Deal with ACLs
40old_host_acls = [a for a in old_host.get_acls() if a.name != 'Everyone']
41new_users = [user.login for user in new.get_users()]
42
43for acl in old_host_acls:
44    # Create any non-existant ACLs
45    new_acls = new.get_acls(name=acl.name)
46    if new_acls:
47        new_acl = new_acls[0]
48    else:
49        new_acl = new.create_acl(name=acl.name, description=acl.description)
50    # Add any users to the ACL that we can
51    for user in acl.users:
52        if user in new_users:
53            new_acl.add_users([user])
54        else:
55            print 'Skipping absent user %s' % user
56    # Add any missing ACLs to the machine
57    if not [a for a in new_host.get_acls() if a.name == acl.name]:
58        new_host.add_acl(acl.name)
59
60# Enable the new host
61if not old_host.locked:
62    new_host.modify(locked=False)
63
64# Delete host from old server
65old_host.delete()
66