Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 1 | # Copyright 2014 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """ |
| 6 | The server module contains the objects and methods used to manage servers in |
| 7 | Autotest. |
| 8 | |
| 9 | The valid actions are: |
| 10 | list: list all servers in the database |
| 11 | create: create a server |
| 12 | delete: deletes a server |
| 13 | modify: modify a server's role or status. |
| 14 | |
| 15 | The common options are: |
| 16 | --role / -r: role that's related to server actions. |
| 17 | |
| 18 | See topic_common.py for a High Level Design and Algorithm. |
| 19 | """ |
| 20 | |
| 21 | import common |
| 22 | |
| 23 | from autotest_lib.cli import action_common |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 24 | from autotest_lib.cli import skylab_utils |
Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 25 | from autotest_lib.cli import topic_common |
| 26 | from autotest_lib.client.common_lib import error |
Shuqian Zhao | db205af | 2018-02-28 15:13:03 -0800 | [diff] [blame] | 27 | from autotest_lib.client.common_lib import global_config |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 28 | from autotest_lib.client.common_lib import revision_control |
Dan Shi | b9144a4 | 2014-12-01 16:09:32 -0800 | [diff] [blame] | 29 | # The django setup is moved here as test_that uses sqlite setup. If this line |
| 30 | # is in server_manager, test_that unittest will fail. |
Dan Shi | 56f1ba7 | 2014-12-03 19:16:53 -0800 | [diff] [blame] | 31 | from autotest_lib.frontend import setup_django_environment |
Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 32 | from autotest_lib.site_utils import server_manager |
Dan Shi | 56f1ba7 | 2014-12-03 19:16:53 -0800 | [diff] [blame] | 33 | from autotest_lib.site_utils import server_manager_utils |
Ningning Xia | a043aad | 2018-04-23 15:07:09 -0700 | [diff] [blame] | 34 | from chromite.lib import gob_util |
Ningning Xia | 9c188b9 | 2018-04-27 15:34:23 -0700 | [diff] [blame] | 35 | |
| 36 | try: |
| 37 | from skylab_inventory import text_manager |
| 38 | from skylab_inventory import translation_utils |
| 39 | from skylab_inventory.lib import server as skylab_server |
| 40 | except ImportError: |
| 41 | pass |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 42 | |
| 43 | |
Shuqian Zhao | db205af | 2018-02-28 15:13:03 -0800 | [diff] [blame] | 44 | RESPECT_SKYLAB_SERVERDB = global_config.global_config.get_config_value( |
| 45 | 'SKYLAB', 'respect_skylab_serverdb', type=bool, default=False) |
| 46 | ATEST_DISABLE_MSG = ('Updating server_db via atest server command has been ' |
| 47 | 'disabled. Please use use go/cros-infra-inventory-tool ' |
| 48 | 'to update it in skylab inventory service.') |
| 49 | |
Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 50 | |
| 51 | class server(topic_common.atest): |
| 52 | """Server class |
| 53 | |
| 54 | atest server [list|create|delete|modify] <options> |
| 55 | """ |
| 56 | usage_action = '[list|create|delete|modify]' |
| 57 | topic = msg_topic = 'server' |
| 58 | msg_items = '<server>' |
| 59 | |
Xixuan Wu | 9e0cfaa | 2018-07-25 16:28:31 -0700 | [diff] [blame^] | 60 | def __init__(self, hostname_required=True, allow_multiple_hostname=False): |
Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 61 | """Add to the parser the options common to all the server actions. |
| 62 | |
| 63 | @param hostname_required: True to require the command has hostname |
| 64 | specified. Default is True. |
| 65 | """ |
| 66 | super(server, self).__init__() |
| 67 | |
| 68 | self.parser.add_option('-r', '--role', |
| 69 | help='Name of a role', |
| 70 | type='string', |
| 71 | default=None, |
| 72 | metavar='ROLE') |
Dan Shi | 56f1ba7 | 2014-12-03 19:16:53 -0800 | [diff] [blame] | 73 | self.parser.add_option('-x', '--action', |
| 74 | help=('Set to True to apply actions when role ' |
| 75 | 'or status is changed, e.g., restart ' |
Ningning Xia | 3748b5f | 2018-05-21 16:33:08 -0700 | [diff] [blame] | 76 | 'scheduler when a drone is removed. %s' % |
| 77 | skylab_utils.MSG_INVALID_IN_SKYLAB), |
Dan Shi | 56f1ba7 | 2014-12-03 19:16:53 -0800 | [diff] [blame] | 78 | action='store_true', |
| 79 | default=False, |
| 80 | metavar='ACTION') |
Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 81 | |
Allen Li | 44fbd13 | 2018-06-12 20:18:06 +0000 | [diff] [blame] | 82 | self.add_skylab_options() |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 83 | |
Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 84 | self.topic_parse_info = topic_common.item_parse_info( |
| 85 | attribute_name='hostname', use_leftover=True) |
| 86 | |
| 87 | self.hostname_required = hostname_required |
Xixuan Wu | 9e0cfaa | 2018-07-25 16:28:31 -0700 | [diff] [blame^] | 88 | self.allow_multiple_hostname = allow_multiple_hostname |
Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 89 | |
| 90 | |
| 91 | def parse(self): |
| 92 | """Parse command arguments. |
| 93 | """ |
| 94 | role_info = topic_common.item_parse_info(attribute_name='role') |
| 95 | kwargs = {} |
| 96 | if self.hostname_required: |
| 97 | kwargs['req_items'] = 'hostname' |
| 98 | (options, leftover) = super(server, self).parse([role_info], **kwargs) |
| 99 | if options.web_server: |
| 100 | self.invalid_syntax('Server actions will access server database ' |
| 101 | 'defined in your local global config. It does ' |
| 102 | 'not rely on RPC, no autotest server needs to ' |
| 103 | 'be specified.') |
| 104 | |
| 105 | # self.hostname is a list. Action on server only needs one hostname at |
| 106 | # most. |
Xixuan Wu | 9e0cfaa | 2018-07-25 16:28:31 -0700 | [diff] [blame^] | 107 | if (not self.hostname and self.hostname_required): |
| 108 | self.invalid_syntax('`server` topic requires hostname. ' |
| 109 | 'Use -h to see available options.') |
| 110 | |
| 111 | if (self.hostname_required and not self.allow_multiple_hostname and |
Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 112 | len(self.hostname) > 1): |
| 113 | self.invalid_syntax('`server` topic can only manipulate 1 server. ' |
| 114 | 'Use -h to see available options.') |
Xixuan Wu | 9e0cfaa | 2018-07-25 16:28:31 -0700 | [diff] [blame^] | 115 | |
Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 116 | if self.hostname: |
Xixuan Wu | 9e0cfaa | 2018-07-25 16:28:31 -0700 | [diff] [blame^] | 117 | if not self.allow_multiple_hostname or not self.skylab: |
| 118 | # Only support create multiple servers in skylab. |
| 119 | # Override self.hostname with the first hostname in the list. |
| 120 | self.hostname = self.hostname[0] |
| 121 | |
Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 122 | self.role = options.role |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 123 | |
| 124 | if self.skylab and self.role: |
| 125 | translation_utils.validate_server_role(self.role) |
| 126 | |
Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 127 | return (options, leftover) |
| 128 | |
| 129 | |
| 130 | def output(self, results): |
| 131 | """Display output. |
| 132 | |
| 133 | For most actions, the return is a string message, no formating needed. |
| 134 | |
| 135 | @param results: return of the execute call. |
| 136 | """ |
| 137 | print results |
| 138 | |
| 139 | |
| 140 | class server_help(server): |
| 141 | """Just here to get the atest logic working. Usage is set by its parent. |
| 142 | """ |
| 143 | pass |
| 144 | |
| 145 | |
| 146 | class server_list(action_common.atest_list, server): |
| 147 | """atest server list [--role <role>]""" |
| 148 | |
| 149 | def __init__(self): |
| 150 | """Initializer. |
| 151 | """ |
| 152 | super(server_list, self).__init__(hostname_required=False) |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 153 | |
Allen Li | 44fbd13 | 2018-06-12 20:18:06 +0000 | [diff] [blame] | 154 | self.parser.add_option('-t', '--table', |
| 155 | help=('List details of all servers in a table, ' |
| 156 | 'e.g., \tHostname | Status | Roles | ' |
| 157 | 'note\t\tserver1 | primary | scheduler | ' |
| 158 | 'lab. %s' % |
| 159 | skylab_utils.MSG_INVALID_IN_SKYLAB), |
| 160 | action='store_true', |
| 161 | default=False) |
Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 162 | self.parser.add_option('-s', '--status', |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 163 | help='Only show servers with given status.', |
Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 164 | type='string', |
| 165 | default=None, |
| 166 | metavar='STATUS') |
Allen Li | 44fbd13 | 2018-06-12 20:18:06 +0000 | [diff] [blame] | 167 | self.parser.add_option('-u', '--summary', |
| 168 | help=('Show the summary of roles and status ' |
| 169 | 'only, e.g.,\tscheduler: server1(primary) ' |
| 170 | 'server2(backup)\t\tdrone: server3(primary' |
| 171 | ') server4(backup). %s' % |
| 172 | skylab_utils.MSG_INVALID_IN_SKYLAB), |
| 173 | action='store_true', |
| 174 | default=False) |
Allen Li | ca17e7c | 2016-10-27 15:37:17 -0700 | [diff] [blame] | 175 | self.parser.add_option('--json', |
Ningning Xia | 32b2668 | 2018-05-23 14:25:18 -0700 | [diff] [blame] | 176 | help=('Format output as JSON.'), |
Allen Li | ca17e7c | 2016-10-27 15:37:17 -0700 | [diff] [blame] | 177 | action='store_true', |
| 178 | default=False) |
Aviv Keshet | e1729bb | 2017-05-31 13:27:09 -0700 | [diff] [blame] | 179 | self.parser.add_option('-N', '--hostnames-only', |
Ningning Xia | 3748b5f | 2018-05-21 16:33:08 -0700 | [diff] [blame] | 180 | help=('Only return hostnames.'), |
Aviv Keshet | e1729bb | 2017-05-31 13:27:09 -0700 | [diff] [blame] | 181 | action='store_true', |
| 182 | default=False) |
Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 183 | |
| 184 | |
| 185 | def parse(self): |
| 186 | """Parse command arguments. |
| 187 | """ |
| 188 | (options, leftover) = super(server_list, self).parse() |
Allen Li | ca17e7c | 2016-10-27 15:37:17 -0700 | [diff] [blame] | 189 | self.json = options.json |
Allen Li | 44fbd13 | 2018-06-12 20:18:06 +0000 | [diff] [blame] | 190 | self.table = options.table |
Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 191 | self.status = options.status |
Allen Li | 44fbd13 | 2018-06-12 20:18:06 +0000 | [diff] [blame] | 192 | self.summary = options.summary |
Aviv Keshet | e1729bb | 2017-05-31 13:27:09 -0700 | [diff] [blame] | 193 | self.namesonly = options.hostnames_only |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 194 | |
Allen Li | 44fbd13 | 2018-06-12 20:18:06 +0000 | [diff] [blame] | 195 | # TODO(nxia): support all formats for skylab inventory. |
| 196 | if (self.skylab and (self.table or self.summary)): |
| 197 | self.invalid_syntax('The format (table|summary)' |
| 198 | ' is not supported with --skylab.') |
| 199 | |
| 200 | if sum([self.table, self.summary, self.json, self.namesonly]) > 1: |
Aviv Keshet | e1729bb | 2017-05-31 13:27:09 -0700 | [diff] [blame] | 201 | self.invalid_syntax('May only specify up to 1 output-format flag.') |
Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 202 | return (options, leftover) |
| 203 | |
| 204 | |
Ningning Xia | d23cf2f | 2018-06-04 17:50:35 -0700 | [diff] [blame] | 205 | def execute_skylab(self): |
| 206 | """Execute 'atest server list --skylab' |
| 207 | |
| 208 | @return: A list of servers matched the given hostname and role. |
| 209 | """ |
| 210 | inventory_repo = skylab_utils.InventoryRepo( |
| 211 | self.inventory_repo_dir) |
| 212 | inventory_repo.initialize() |
| 213 | infrastructure = text_manager.load_infrastructure( |
| 214 | inventory_repo.get_data_dir()) |
| 215 | |
| 216 | return skylab_server.get_servers( |
| 217 | infrastructure, |
| 218 | self.environment, |
| 219 | hostname=self.hostname, |
| 220 | role=self.role, |
| 221 | status=self.status) |
| 222 | |
| 223 | |
Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 224 | def execute(self): |
| 225 | """Execute the command. |
| 226 | |
| 227 | @return: A list of servers matched given hostname and role. |
| 228 | """ |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 229 | if self.skylab: |
| 230 | try: |
Ningning Xia | d23cf2f | 2018-06-04 17:50:35 -0700 | [diff] [blame] | 231 | return self.execute_skylab() |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 232 | except (skylab_server.SkylabServerActionError, |
Ningning Xia | d23cf2f | 2018-06-04 17:50:35 -0700 | [diff] [blame] | 233 | revision_control.GitError, |
| 234 | skylab_utils.InventoryRepoDirNotClean) as e: |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 235 | self.failure(e, what_failed='Failed to list servers from skylab' |
| 236 | ' inventory.', item=self.hostname, fatal=True) |
| 237 | else: |
| 238 | try: |
| 239 | return server_manager_utils.get_servers( |
| 240 | hostname=self.hostname, |
| 241 | role=self.role, |
| 242 | status=self.status) |
| 243 | except (server_manager_utils.ServerActionError, |
| 244 | error.InvalidDataError) as e: |
| 245 | self.failure(e, what_failed='Failed to find servers', |
| 246 | item=self.hostname, fatal=True) |
Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 247 | |
| 248 | |
| 249 | def output(self, results): |
| 250 | """Display output. |
| 251 | |
| 252 | @param results: return of the execute call, a list of server object that |
| 253 | contains server information. |
| 254 | """ |
Allen Li | 90a84ea | 2016-10-27 15:07:42 -0700 | [diff] [blame] | 255 | if results: |
Allen Li | ca17e7c | 2016-10-27 15:37:17 -0700 | [diff] [blame] | 256 | if self.json: |
Ningning Xia | 32b2668 | 2018-05-23 14:25:18 -0700 | [diff] [blame] | 257 | if self.skylab: |
| 258 | formatter = skylab_server.format_servers_json |
| 259 | else: |
| 260 | formatter = server_manager_utils.format_servers_json |
Allen Li | 44fbd13 | 2018-06-12 20:18:06 +0000 | [diff] [blame] | 261 | elif self.table: |
| 262 | formatter = server_manager_utils.format_servers_table |
| 263 | elif self.summary: |
| 264 | formatter = server_manager_utils.format_servers_summary |
Aviv Keshet | e1729bb | 2017-05-31 13:27:09 -0700 | [diff] [blame] | 265 | elif self.namesonly: |
| 266 | formatter = server_manager_utils.format_servers_nameonly |
Allen Li | 90a84ea | 2016-10-27 15:07:42 -0700 | [diff] [blame] | 267 | else: |
| 268 | formatter = server_manager_utils.format_servers |
| 269 | print formatter(results) |
| 270 | else: |
Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 271 | self.failure('No server is found.', |
| 272 | what_failed='Failed to find servers', |
| 273 | item=self.hostname, fatal=True) |
Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 274 | |
| 275 | |
| 276 | class server_create(server): |
| 277 | """atest server create hostname --role <role> --note <note> |
| 278 | """ |
| 279 | |
| 280 | def __init__(self): |
| 281 | """Initializer. |
| 282 | """ |
Xixuan Wu | 9e0cfaa | 2018-07-25 16:28:31 -0700 | [diff] [blame^] | 283 | super(server_create, self).__init__(allow_multiple_hostname=True) |
Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 284 | self.parser.add_option('-n', '--note', |
| 285 | help='note of the server', |
| 286 | type='string', |
| 287 | default=None, |
| 288 | metavar='NOTE') |
| 289 | |
| 290 | |
| 291 | def parse(self): |
| 292 | """Parse command arguments. |
| 293 | """ |
| 294 | (options, leftover) = super(server_create, self).parse() |
| 295 | self.note = options.note |
| 296 | |
| 297 | if not self.role: |
| 298 | self.invalid_syntax('--role is required to create a server.') |
| 299 | |
| 300 | return (options, leftover) |
| 301 | |
| 302 | |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 303 | def execute_skylab(self): |
| 304 | """Execute the command for skylab inventory changes.""" |
| 305 | inventory_repo = skylab_utils.InventoryRepo( |
| 306 | self.inventory_repo_dir) |
| 307 | inventory_repo.initialize() |
| 308 | data_dir = inventory_repo.get_data_dir() |
Ningning Xia | e871405 | 2018-04-30 18:58:54 -0700 | [diff] [blame] | 309 | infrastructure = text_manager.load_infrastructure(data_dir) |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 310 | |
Xixuan Wu | 9e0cfaa | 2018-07-25 16:28:31 -0700 | [diff] [blame^] | 311 | new_servers = [] |
| 312 | for hostname in self.hostname: |
| 313 | new_servers.append(skylab_server.create( |
| 314 | infrastructure, |
| 315 | hostname, |
| 316 | self.environment, |
| 317 | role=self.role, |
| 318 | note=self.note)) |
Ningning Xia | e871405 | 2018-04-30 18:58:54 -0700 | [diff] [blame] | 319 | text_manager.dump_infrastructure(data_dir, infrastructure) |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 320 | |
| 321 | message = skylab_utils.construct_commit_message( |
| 322 | 'Add new server: %s' % self.hostname) |
Ningning Xia | a043aad | 2018-04-23 15:07:09 -0700 | [diff] [blame] | 323 | self.change_number = inventory_repo.upload_change( |
| 324 | message, draft=self.draft, dryrun=self.dryrun, |
| 325 | submit=self.submit) |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 326 | |
Xixuan Wu | 9e0cfaa | 2018-07-25 16:28:31 -0700 | [diff] [blame^] | 327 | return new_servers |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 328 | |
| 329 | |
Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 330 | def execute(self): |
| 331 | """Execute the command. |
| 332 | |
| 333 | @return: A Server object if it is created successfully. |
| 334 | """ |
Shuqian Zhao | db205af | 2018-02-28 15:13:03 -0800 | [diff] [blame] | 335 | if RESPECT_SKYLAB_SERVERDB: |
| 336 | self.failure(ATEST_DISABLE_MSG, |
| 337 | what_failed='Failed to create server', |
| 338 | item=self.hostname, fatal=True) |
| 339 | |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 340 | if self.skylab: |
| 341 | try: |
| 342 | return self.execute_skylab() |
| 343 | except (skylab_server.SkylabServerActionError, |
Ningning Xia | a043aad | 2018-04-23 15:07:09 -0700 | [diff] [blame] | 344 | revision_control.GitError, |
Ningning Xia | d23cf2f | 2018-06-04 17:50:35 -0700 | [diff] [blame] | 345 | gob_util.GOBError, |
| 346 | skylab_utils.InventoryRepoDirNotClean) as e: |
Ningning Xia | e871405 | 2018-04-30 18:58:54 -0700 | [diff] [blame] | 347 | self.failure(e, what_failed='Failed to create server in skylab ' |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 348 | 'inventory.', item=self.hostname, fatal=True) |
| 349 | else: |
| 350 | try: |
| 351 | return server_manager.create( |
| 352 | hostname=self.hostname, |
| 353 | role=self.role, |
| 354 | note=self.note) |
| 355 | except (server_manager_utils.ServerActionError, |
| 356 | error.InvalidDataError) as e: |
| 357 | self.failure(e, what_failed='Failed to create server', |
| 358 | item=self.hostname, fatal=True) |
Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 359 | |
| 360 | |
| 361 | def output(self, results): |
| 362 | """Display output. |
| 363 | |
| 364 | @param results: return of the execute call, a server object that |
| 365 | contains server information. |
| 366 | """ |
| 367 | if results: |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 368 | print 'Server %s is added.\n' % self.hostname |
Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 369 | print results |
| 370 | |
Ningning Xia | a043aad | 2018-04-23 15:07:09 -0700 | [diff] [blame] | 371 | if self.skylab and not self.dryrun and not self.submit: |
Ningning Xia | ef35cb5 | 2018-05-04 17:58:20 -0700 | [diff] [blame] | 372 | print skylab_utils.get_cl_message(self.change_number) |
Ningning Xia | a043aad | 2018-04-23 15:07:09 -0700 | [diff] [blame] | 373 | |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 374 | |
Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 375 | |
| 376 | class server_delete(server): |
| 377 | """atest server delete hostname""" |
| 378 | |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 379 | def execute_skylab(self): |
| 380 | """Execute the command for skylab inventory changes.""" |
| 381 | inventory_repo = skylab_utils.InventoryRepo( |
| 382 | self.inventory_repo_dir) |
| 383 | inventory_repo.initialize() |
| 384 | data_dir = inventory_repo.get_data_dir() |
Ningning Xia | e871405 | 2018-04-30 18:58:54 -0700 | [diff] [blame] | 385 | infrastructure = text_manager.load_infrastructure(data_dir) |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 386 | |
Ningning Xia | e871405 | 2018-04-30 18:58:54 -0700 | [diff] [blame] | 387 | skylab_server.delete(infrastructure, self.hostname, self.environment) |
| 388 | text_manager.dump_infrastructure(data_dir, infrastructure) |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 389 | |
| 390 | message = skylab_utils.construct_commit_message( |
| 391 | 'Delete server: %s' % self.hostname) |
Ningning Xia | a043aad | 2018-04-23 15:07:09 -0700 | [diff] [blame] | 392 | self.change_number = inventory_repo.upload_change( |
| 393 | message, draft=self.draft, dryrun=self.dryrun, |
| 394 | submit=self.submit) |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 395 | |
| 396 | |
Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 397 | def execute(self): |
| 398 | """Execute the command. |
| 399 | |
| 400 | @return: True if server is deleted successfully. |
| 401 | """ |
Shuqian Zhao | db205af | 2018-02-28 15:13:03 -0800 | [diff] [blame] | 402 | if RESPECT_SKYLAB_SERVERDB: |
| 403 | self.failure(ATEST_DISABLE_MSG, |
| 404 | what_failed='Failed to delete server', |
| 405 | item=self.hostname, fatal=True) |
| 406 | |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 407 | if self.skylab: |
| 408 | try: |
| 409 | self.execute_skylab() |
| 410 | return True |
| 411 | except (skylab_server.SkylabServerActionError, |
Ningning Xia | a043aad | 2018-04-23 15:07:09 -0700 | [diff] [blame] | 412 | revision_control.GitError, |
Ningning Xia | d23cf2f | 2018-06-04 17:50:35 -0700 | [diff] [blame] | 413 | gob_util.GOBError, |
| 414 | skylab_utils.InventoryRepoDirNotClean) as e: |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 415 | self.failure(e, what_failed='Failed to delete server from ' |
| 416 | 'skylab inventory.', item=self.hostname, |
| 417 | fatal=True) |
| 418 | else: |
| 419 | try: |
| 420 | server_manager.delete(hostname=self.hostname) |
| 421 | return True |
| 422 | except (server_manager_utils.ServerActionError, |
| 423 | error.InvalidDataError) as e: |
| 424 | self.failure(e, what_failed='Failed to delete server', |
| 425 | item=self.hostname, fatal=True) |
Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 426 | |
| 427 | |
| 428 | def output(self, results): |
| 429 | """Display output. |
| 430 | |
| 431 | @param results: return of the execute call. |
| 432 | """ |
| 433 | if results: |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 434 | print ('Server %s is deleted.\n' % |
Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 435 | self.hostname) |
| 436 | |
Ningning Xia | a043aad | 2018-04-23 15:07:09 -0700 | [diff] [blame] | 437 | if self.skylab and not self.dryrun and not self.submit: |
Ningning Xia | ef35cb5 | 2018-05-04 17:58:20 -0700 | [diff] [blame] | 438 | print skylab_utils.get_cl_message(self.change_number) |
Ningning Xia | a043aad | 2018-04-23 15:07:09 -0700 | [diff] [blame] | 439 | |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 440 | |
Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 441 | |
| 442 | class server_modify(server): |
| 443 | """atest server modify hostname |
| 444 | |
| 445 | modify action can only change one input at a time. Available inputs are: |
| 446 | --status: Status of the server. |
| 447 | --note: Note of the server. |
| 448 | --role: New role to be added to the server. |
| 449 | --delete_role: Existing role to be deleted from the server. |
| 450 | """ |
| 451 | |
| 452 | def __init__(self): |
| 453 | """Initializer. |
| 454 | """ |
| 455 | super(server_modify, self).__init__() |
| 456 | self.parser.add_option('-s', '--status', |
| 457 | help='Status of the server', |
| 458 | type='string', |
| 459 | metavar='STATUS') |
| 460 | self.parser.add_option('-n', '--note', |
| 461 | help='Note of the server', |
| 462 | type='string', |
| 463 | default=None, |
| 464 | metavar='NOTE') |
| 465 | self.parser.add_option('-d', '--delete', |
| 466 | help=('Set to True to delete given role.'), |
| 467 | action='store_true', |
| 468 | default=False, |
| 469 | metavar='DELETE') |
| 470 | self.parser.add_option('-a', '--attribute', |
| 471 | help='Name of the attribute of the server', |
| 472 | type='string', |
| 473 | default=None, |
| 474 | metavar='ATTRIBUTE') |
| 475 | self.parser.add_option('-e', '--value', |
| 476 | help='Value for the attribute of the server', |
| 477 | type='string', |
| 478 | default=None, |
| 479 | metavar='VALUE') |
| 480 | |
| 481 | |
| 482 | def parse(self): |
| 483 | """Parse command arguments. |
| 484 | """ |
| 485 | (options, leftover) = super(server_modify, self).parse() |
| 486 | self.status = options.status |
| 487 | self.note = options.note |
| 488 | self.delete = options.delete |
| 489 | self.attribute = options.attribute |
| 490 | self.value = options.value |
Dan Shi | 56f1ba7 | 2014-12-03 19:16:53 -0800 | [diff] [blame] | 491 | self.action = options.action |
Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 492 | |
| 493 | # modify supports various options. However, it's safer to limit one |
| 494 | # option at a time so no complicated role-dependent logic is needed |
| 495 | # to handle scenario that both role and status are changed. |
| 496 | # self.parser is optparse, which does not have function in argparse like |
| 497 | # add_mutually_exclusive_group. That's why the count is used here. |
| 498 | flags = [self.status is not None, self.role is not None, |
| 499 | self.attribute is not None, self.note is not None] |
| 500 | if flags.count(True) != 1: |
| 501 | msg = ('Action modify only support one option at a time. You can ' |
| 502 | 'try one of following 5 options:\n' |
| 503 | '1. --status: Change server\'s status.\n' |
| 504 | '2. --note: Change server\'s note.\n' |
| 505 | '3. --role with optional -d: Add/delete role from server.\n' |
| 506 | '4. --attribute --value: Set/change the value of a ' |
| 507 | 'server\'s attribute.\n' |
| 508 | '5. --attribute -d: Delete the attribute from the ' |
| 509 | 'server.\n' |
| 510 | '\nUse option -h to see a complete list of options.') |
| 511 | self.invalid_syntax(msg) |
| 512 | if (self.status != None or self.note != None) and self.delete: |
| 513 | self.invalid_syntax('--delete does not apply to status or note.') |
| 514 | if self.attribute != None and not self.delete and self.value == None: |
| 515 | self.invalid_syntax('--attribute must be used with option --value ' |
| 516 | 'or --delete.') |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 517 | |
| 518 | # TODO(nxia): crbug.com/832964 support --action with --skylab |
| 519 | if self.skylab and self.action: |
| 520 | self.invalid_syntax('--action is currently not supported with' |
| 521 | ' --skylab.') |
| 522 | |
Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 523 | return (options, leftover) |
| 524 | |
| 525 | |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 526 | def execute_skylab(self): |
| 527 | """Execute the command for skylab inventory changes.""" |
| 528 | inventory_repo = skylab_utils.InventoryRepo( |
| 529 | self.inventory_repo_dir) |
| 530 | inventory_repo.initialize() |
| 531 | data_dir = inventory_repo.get_data_dir() |
Ningning Xia | e871405 | 2018-04-30 18:58:54 -0700 | [diff] [blame] | 532 | infrastructure = text_manager.load_infrastructure(data_dir) |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 533 | |
| 534 | target_server = skylab_server.modify( |
| 535 | infrastructure, |
| 536 | self.hostname, |
Ningning Xia | e871405 | 2018-04-30 18:58:54 -0700 | [diff] [blame] | 537 | self.environment, |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 538 | role=self.role, |
| 539 | status=self.status, |
| 540 | delete_role=self.delete, |
| 541 | note=self.note, |
| 542 | attribute=self.attribute, |
| 543 | value=self.value, |
| 544 | delete_attribute=self.delete) |
Ningning Xia | e871405 | 2018-04-30 18:58:54 -0700 | [diff] [blame] | 545 | text_manager.dump_infrastructure(data_dir, infrastructure) |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 546 | |
| 547 | status = inventory_repo.git_repo.status() |
| 548 | if not status: |
| 549 | print('Nothing is changed for server %s.' % self.hostname) |
| 550 | return |
| 551 | |
| 552 | message = skylab_utils.construct_commit_message( |
| 553 | 'Modify server: %s' % self.hostname) |
Ningning Xia | a043aad | 2018-04-23 15:07:09 -0700 | [diff] [blame] | 554 | self.change_number = inventory_repo.upload_change( |
| 555 | message, draft=self.draft, dryrun=self.dryrun, |
| 556 | submit=self.submit) |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 557 | |
| 558 | return target_server |
| 559 | |
| 560 | |
Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 561 | def execute(self): |
| 562 | """Execute the command. |
| 563 | |
| 564 | @return: The updated server object if it is modified successfully. |
| 565 | """ |
Shuqian Zhao | db205af | 2018-02-28 15:13:03 -0800 | [diff] [blame] | 566 | if RESPECT_SKYLAB_SERVERDB: |
| 567 | self.failure(ATEST_DISABLE_MSG, |
| 568 | what_failed='Failed to modify server', |
| 569 | item=self.hostname, fatal=True) |
| 570 | |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 571 | if self.skylab: |
| 572 | try: |
| 573 | return self.execute_skylab() |
| 574 | except (skylab_server.SkylabServerActionError, |
Ningning Xia | a043aad | 2018-04-23 15:07:09 -0700 | [diff] [blame] | 575 | revision_control.GitError, |
Ningning Xia | d23cf2f | 2018-06-04 17:50:35 -0700 | [diff] [blame] | 576 | gob_util.GOBError, |
| 577 | skylab_utils.InventoryRepoDirNotClean) as e: |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 578 | self.failure(e, what_failed='Failed to modify server in skylab' |
| 579 | ' inventory.', item=self.hostname, fatal=True) |
| 580 | else: |
| 581 | try: |
| 582 | return server_manager.modify( |
| 583 | hostname=self.hostname, role=self.role, |
| 584 | status=self.status, delete=self.delete, |
| 585 | note=self.note, attribute=self.attribute, |
| 586 | value=self.value, action=self.action) |
| 587 | except (server_manager_utils.ServerActionError, |
| 588 | error.InvalidDataError) as e: |
| 589 | self.failure(e, what_failed='Failed to modify server', |
| 590 | item=self.hostname, fatal=True) |
Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 591 | |
| 592 | |
| 593 | def output(self, results): |
| 594 | """Display output. |
| 595 | |
| 596 | @param results: return of the execute call, which is the updated server |
| 597 | object. |
| 598 | """ |
| 599 | if results: |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 600 | print 'Server %s is modified.\n' % self.hostname |
Dan Shi | 784df0c | 2014-11-26 10:11:15 -0800 | [diff] [blame] | 601 | print results |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 602 | |
Ningning Xia | a043aad | 2018-04-23 15:07:09 -0700 | [diff] [blame] | 603 | if self.skylab and not self.dryrun and not self.submit: |
Ningning Xia | ef35cb5 | 2018-05-04 17:58:20 -0700 | [diff] [blame] | 604 | print skylab_utils.get_cl_message(self.change_number) |