| mbligh | 67f0893 | 2009-01-21 19:27:28 +0000 | [diff] [blame] | 1 | #!/usr/bin/python | 
 | 2 | # Copyright Google, Martin J. Bligh <mbligh@google.com>, Jan 2009 | 
 | 3 | import os, sys | 
 | 4 | import common | 
 | 5 | from autotest_lib.server import frontend | 
 | 6 |  | 
 | 7 | try: | 
 | 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 | 
 | 16 | except Exception: | 
 | 17 |     print "Usage: atest_migrate_host <old_server> <new_server> <hostname>" | 
 | 18 |     raise | 
 | 19 |     sys.exit(1) | 
 | 20 |  | 
 | 21 |  | 
 | 22 | # Create host | 
 | 23 |  | 
| mbligh | 6463c4b | 2009-01-30 00:33:37 +0000 | [diff] [blame^] | 24 | new_host = new.create_host(hostname=hostname, locked=True) | 
| mbligh | 67f0893 | 2009-01-21 19:27:28 +0000 | [diff] [blame] | 25 |  | 
 | 26 | # Deal with labels | 
 | 27 | old_host_labels = old_host.get_labels() | 
 | 28 | for 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 | 
 | 40 | old_host_acls = [a for a in old_host.get_acls() if a.name != 'Everyone'] | 
 | 41 | new_users = [user.login for user in new.get_users()] | 
 | 42 |  | 
 | 43 | for acl in old_host_acls: | 
 | 44 |     # Create any non-existant ACLs | 
| mbligh | 6463c4b | 2009-01-30 00:33:37 +0000 | [diff] [blame^] | 45 |     new_acls = new.get_acls(name=acl.name) | 
 | 46 |     if new_acls: | 
 | 47 |         new_acl = new_acls[0] | 
 | 48 |     else: | 
| mbligh | 67f0893 | 2009-01-21 19:27:28 +0000 | [diff] [blame] | 49 |         new_acl = new.create_acl(name=acl.name, description=acl.description) | 
| mbligh | 6463c4b | 2009-01-30 00:33:37 +0000 | [diff] [blame^] | 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 | 
| mbligh | 67f0893 | 2009-01-21 19:27:28 +0000 | [diff] [blame] | 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 |  | 
| mbligh | 6463c4b | 2009-01-30 00:33:37 +0000 | [diff] [blame^] | 60 | # Enable the new host | 
 | 61 | if not old_host.locked: | 
 | 62 |     new_host.modify(locked=False) | 
 | 63 |  | 
| mbligh | 67f0893 | 2009-01-21 19:27:28 +0000 | [diff] [blame] | 64 | # Delete host from old server | 
 | 65 | old_host.delete() |