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