def test_batch_update_pools(self, **kwargs):
        client = self.create_sharedkey_client(**kwargs)
        # Test Create Paas Pool
        test_paas_pool = models.PoolAddParameter(
            id=self.get_resource_name('batch_paas_'),
            vm_size='small',
            cloud_service_configuration=models.CloudServiceConfiguration(
                os_family='5'
            ),
            start_task=models.StartTask(
                command_line="cmd.exe /c \"echo hello world\"",
                resource_files=[models.ResourceFile('https://blobsource.com', 'filename.txt')],
                environment_settings=[models.EnvironmentSetting('ENV_VAR', 'env_value')],
                user_identity=models.UserIdentity(
                    auto_user=models.AutoUserSpecification(
                        elevation_level=models.ElevationLevel.admin
                    )
                )
            )
        )
        response = client.pool.add(test_paas_pool)
        self.assertIsNone(response)

        # Test Upgrade Pool OS
        self.assertBatchError(
            "PoolVersionEqualsUpgradeVersion",
            client.pool.upgrade_os,
            test_paas_pool.id,
            "*"
        )

        # Test Update Pool Parameters
        params = models.PoolUpdatePropertiesParameter([], [], [models.MetadataItem('foo', 'bar')])
        response = client.pool.update_properties(test_paas_pool.id, params)
        self.assertIsNone(response)

        # Test Patch Pool Parameters
        params = models.PoolPatchParameter(metadata=[models.MetadataItem('foo2', 'bar2')])
        response = client.pool.patch(test_paas_pool.id, params)
        self.assertIsNone(response)

        # Test Pool Exists
        response = client.pool.exists(test_paas_pool.id)
        self.assertTrue(response)

        # Test Get Pool
        pool = client.pool.get(test_paas_pool.id)
        self.assertIsInstance(pool, models.CloudPool)
        self.assertEqual(pool.id, test_paas_pool.id)
        self.assertEqual(pool.state, models.PoolState.active)
        self.assertEqual(pool.allocation_state, models.AllocationState.steady)
        self.assertEqual(pool.cloud_service_configuration.os_family, '5')
        self.assertEqual(pool.vm_size, 'small')
        self.assertIsNone(pool.start_task)
        self.assertEqual(pool.metadata[0].name, 'foo2')
        self.assertEqual(pool.metadata[0].value, 'bar2')

        # Test Get Pool with OData Clauses
        options = models.PoolGetOptions(select='id,state', expand='stats')
        pool = client.pool.get(test_paas_pool.id, options)
        self.assertIsInstance(pool, models.CloudPool)
        self.assertEqual(pool.id, test_paas_pool.id)
        self.assertEqual(pool.state, models.PoolState.active)
        self.assertIsNone(pool.allocation_state)
        self.assertIsNone(pool.vm_size)

        # Test Delete Pool
        response = client.pool.delete(test_paas_pool.id)
        self.assertIsNone(response)
Ejemplo n.º 2
0
def _construct_batch_monitoring_list(
        batch_client: azure.batch.BatchServiceClient,
        poolid: str) -> List[Dict]:
    """Construct the batch pool monitoring list
    :param batch_client: batch client
    :param poolid: pool id
    """
    logger.debug('querying batch pool: {}'.format(poolid))
    # first retrieve node exporter and cadvisor ports
    try:
        pool = batch_client.pool.get(
            poolid,
            pool_get_options=batchmodels.PoolGetOptions(
                select='id,state,startTask', ),
        )
    except batchmodels.BatchErrorException as e:
        logger.error(e.message)
        return None
    if pool.state == batchmodels.PoolState.deleting:
        logger.debug('pool {} is being deleted, ignoring'.format(pool.id))
        return None
    ne_port = None
    ca_port = None
    for es in pool.start_task.environment_settings:
        if es.name == 'PROM_NODE_EXPORTER_PORT':
            ne_port = int(es.value)
        elif es.name == 'PROM_CADVISOR_PORT':
            ca_port = int(es.value)
        if ne_port is not None and ca_port is not None:
            break
    logger.debug('pool {} state={} ne_port={} ca_port={}'.format(
        pool.id, pool.state, ne_port, ca_port))
    # get node list
    nodelist = []
    try:
        nodes = batch_client.compute_node.list(
            poolid,
            compute_node_list_options=batchmodels.ComputeNodeListOptions(
                select='id,state,ipAddress', ),
        )
        for node in nodes:
            logger.debug('compute node {} state={} ipaddress={}'.format(
                node.id, node.state.value, node.ip_address))
            if node.state in _VALID_NODE_STATES:
                nodelist.append(node)
    except batchmodels.BatchErrorException as e:
        logger.error(e.message)
        return None
    if is_none_or_empty(nodelist):
        logger.info('no viable nodes found in pool: {}'.format(poolid))
        return None
    logger.info('monitoring {} nodes for pool: {}'.format(
        len(nodelist), poolid))
    # construct prometheus targets
    targets = []
    if ne_port is not None:
        targets.append({
            'targets':
            ['{}:{}'.format(x.ip_address, ne_port) for x in nodelist],
            'labels': {
                'env': _MONITOR_BATCHPOOL_PK,
                'collector': 'NodeExporter',
                'job': '{}'.format(poolid)
            }
        })
    if ca_port is not None:
        targets.append({
            'targets':
            ['{}:{}'.format(x.ip_address, ca_port) for x in nodelist],
            'labels': {
                'env': _MONITOR_BATCHPOOL_PK,
                'collector': 'cAdvisor',
                'job': '{}'.format(poolid)
            }
        })
    return targets