1#!/usr/bin/python
2
3# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""Module used to sync and deploy infrastructure changes for the lab team.
8
9This is a helper and used by lab_deploy which bootstraps and calls this utility
10once a user has logged into the main autotest server. This can be called
11directly if already on the autotest server.
12
13Usage:
14  lab_deploy_helper.py  (sync,restart,print) (devservers, drones, scheduler)+.
15"""
16import logging
17import sys
18
19import common
20from autotest_lib.client.common_lib.cros import dev_server
21from autotest_lib.client.common_lib import global_config
22from autotest_lib.server import hosts
23
24import common_util
25
26CONFIG = global_config.global_config
27
28
29def devserver_list():
30    """Returns the list of devserver-type machines used by the infrastructure/
31    """
32    image_servers = dev_server.ImageServer.servers()
33    crash_servers = dev_server.CrashServer.servers()
34    return image_servers + crash_servers
35
36
37def autotest_scheduler_drones():
38    """Returns tuple containing the autotest scheduler and list of drones."""
39    autotest_master = CONFIG.get_config_value('scheduler', 'host', type=str,
40                                              default=None)
41    autotest_drones = CONFIG.get_config_value('scheduler', 'drones',
42                                              type=list, default=[])
43    return autotest_master, autotest_drones
44
45
46def devserver_restart(host):
47    """SSH's in to |host| and restarts the devserver instance.
48
49    This method uses puppet apply to restart the devserver instance on host.
50    """
51    logging.info('METHOD STUB called for restarting devserver on %s', host)
52    # host.run('puppet apply devserver_start')
53
54
55def devserver_sync(host):
56    """SSH's in to |host| and syncs the devserver.
57
58    This method uses puppet apply to sync the devserver instance on host.
59    """
60    logging.info('METHOD STUB called for syncing devserver on %s', host)
61    # host.run('puppet apply devserver_sync')
62
63
64def autotest_restart(host):
65    """SSH's in to |host| and restarts autotest instance.
66
67    This method uses puppet apply to restart autotest installed on the host.
68    """
69    logging.info('METHOD STUB called for restarting autotest on %s', host)
70    # host.run('puppet apply autotest_start')
71
72
73def autotest_sync(host):
74    """SSH's in to |host| and syncs autotest.
75
76    This method uses puppet apply to sync autotest.
77    """
78    logging.info('METHOD STUB called for syncing autotest on %s', host)
79    # host.run('puppet apply autotest_sync')
80
81
82def main(argv):
83    common_util.setup_logging()
84    args = common_util.parse_args(argv)
85    requested_server_set = set(args.servers)
86    devservers = devserver_list()
87    master, drones = autotest_scheduler_drones()
88
89    if args.operation == common_util.SYNC:
90        if common_util.DEVS in requested_server_set:
91            for server in devservers:
92                devserver_sync(hosts.SSHHost(server))
93
94        if common_util.DRONES in requested_server_set:
95            for server in drones:
96                autotest_sync(hosts.SSHHost(server))
97
98        if common_util.SCHEDULER in requested_server_set:
99            autotest_sync(master)
100
101    elif args.operation == common_util.RESTART:
102        if common_util.DEVS in requested_server_set:
103            for server in devservers:
104                devserver_restart(hosts.SSHHost(server))
105
106        if common_util.DRONES in requested_server_set:
107            for server in drones:
108                autotest_restart(hosts.SSHHost(server))
109
110        if common_util.SCHEDULER in requested_server_set:
111            autotest_restart(master)
112
113    elif args.operation == common_util.PRINT:
114        if common_util.DEVS in requested_server_set:
115            for server in devservers:
116                print server
117
118        if common_util.DRONES in requested_server_set:
119            for server in drones:
120                print server
121
122        if common_util.SCHEDULER in requested_server_set:
123            print master
124
125    return 0
126
127
128if __name__ == '__main__':
129    main(sys.argv[1:])
130