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