def test_rack_grouping():
  old_grouping_functions = MesosMaintenance.GROUPING_FUNCTIONS.copy()
  MesosMaintenance.GROUPING_FUNCTIONS['by_rack'] = rack_grouping

  example_host_list = [
    'west-aaa-001.example.com',
    'west-aaa-002.example.com',
    'west-xyz-002.example.com',
    'east-xyz-003.example.com',
    'east-xyz-004.example.com',
  ]

  try:
    batches = list(MesosMaintenance.iter_batches(example_host_list, 1, 'by_rack'))
    assert batches[0] == Hosts(set([
        'west-aaa-001.example.com',
        'west-aaa-002.example.com'
    ]))
    assert batches[1] == Hosts(set([
        'west-xyz-002.example.com',
        'east-xyz-003.example.com',
        'east-xyz-004.example.com',
    ]))

    batches = list(MesosMaintenance.iter_batches(example_host_list, 2, 'by_rack'))
    assert batches[0] == Hosts(set(example_host_list))

    batches = list(MesosMaintenance.iter_batches(example_host_list, 3, 'by_rack'))
    assert batches[0] == Hosts(set(example_host_list))

    with pytest.raises(ValueError):
      list(MesosMaintenance.iter_batches(example_host_list, 0))

  finally:
    MesosMaintenance.GROUPING_FUNCTIONS = old_grouping_functions
def test_default_grouping():
  example_host_list = [
    'xyz321.example.com',
    'bar337.example.com',
    'foo001.example.com',
  ]

  batches = list(MesosMaintenance.iter_batches(example_host_list, 1))
  assert batches[0] == Hosts(set(['bar337.example.com']))
  assert batches[1] == Hosts(set(['foo001.example.com']))
  assert batches[2] == Hosts(set(['xyz321.example.com']))

  batches = list(MesosMaintenance.iter_batches(example_host_list, 2))
  assert batches[0] == Hosts(set(['bar337.example.com', 'foo001.example.com']))
  assert batches[1] == Hosts(set(['xyz321.example.com']))
Example #3
0
def perform_maintenance_hosts(cluster):
    """usage: perform_maintenance cluster [--filename=filename]
                                        [--hosts=hosts]
                                        [--batch_size=num]
                                        [--post_drain_script=path]
                                        [--grouping=function]

  Asks the scheduler to remove any running tasks from the machine and remove it
  from service temporarily, perform some action on them, then return the machines
  to service.
  """
    options = app.get_options()
    drainable_hosts = parse_hosts(options)

    if options.post_drain_script:
        if not os.path.exists(options.post_drain_script):
            die("No such file: %s" % options.post_drain_script)
        cmd = os.path.abspath(options.post_drain_script)
        drained_callback = lambda host: subprocess.Popen([cmd, host])
    else:
        drained_callback = None

    MesosMaintenance(CLUSTERS[cluster], options.verbosity).perform_maintenance(
        drainable_hosts,
        batch_size=int(options.batch_size),
        callback=drained_callback,
        grouping_function=options.grouping)
Example #4
0
def end_maintenance_hosts(cluster):
    """usage: end_maintenance_hosts cluster [--filename=filename]
                                          [--hosts=hosts]
  """
    options = app.get_options()
    MesosMaintenance(CLUSTERS[cluster],
                     options.verbosity).end_maintenance(parse_hosts(options))
Example #5
0
def host_maintenance_status(cluster):
    """usage: host_maintenance_status cluster [--filename=filename]
                                            [--hosts=hosts]

  Check on the schedulers maintenance status for a list of hosts in the cluster.
  """
    options = app.get_options()
    checkable_hosts = parse_hosts(options)
    statuses = MesosMaintenance(
        CLUSTERS[cluster], options.verbosity).check_status(checkable_hosts)
    for pair in statuses:
        log.info("%s is in state: %s" % pair)
 def test_start_maintenance(self, mock_api):
   mock_api.return_value = Response(responseCode=ResponseCode.OK)
   maintenance = MesosMaintenance(DEFAULT_CLUSTER, 'quiet')
   maintenance.start_maintenance(MOCK_TEST_HOSTS)