blob: 6773304399f1945d9a1087167227708045fbaadc [file] [log] [blame]
Mike Frysingerd03e6b52019-08-03 12:49:01 -04001#!/usr/bin/python2
mbligh67f08932009-01-21 19:27:28 +00002# 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
mbligh6463c4b2009-01-30 00:33:37 +000024new_host = new.create_host(hostname=hostname, locked=True)
mbligh67f08932009-01-21 19:27:28 +000025
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,
MK Ryu9c5fbbe2015-02-11 15:46:22 -080033 platform=label.platform,
34 only_if_needed=label.only_if_needed)
mbligh67f08932009-01-21 19:27:28 +000035 # 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
mbligh6463c4b2009-01-30 00:33:37 +000045 new_acls = new.get_acls(name=acl.name)
46 if new_acls:
47 new_acl = new_acls[0]
48 else:
mbligh67f08932009-01-21 19:27:28 +000049 new_acl = new.create_acl(name=acl.name, description=acl.description)
mbligh6463c4b2009-01-30 00:33:37 +000050 # 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
mbligh67f08932009-01-21 19:27:28 +000056 # 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
mbligh6463c4b2009-01-30 00:33:37 +000060# Enable the new host
61if not old_host.locked:
62 new_host.modify(locked=False)
63
mbligh67f08932009-01-21 19:27:28 +000064# Delete host from old server
65old_host.delete()