def _create_jenkins_node_slots()

in services/jenkins-autoscaling/lambda_mxnet_ci/autoscaling/handler.py [0:0]


def _create_jenkins_node_slots(jenkins_server, label2num_instances):
    """
    Create a
    :param label:
    :return:
    """
    # We could use the swarm plugin to do this automatically, but we don't want to deploy credentials to our slaves.
    # Thus, we create nodes ourselves.
    jobs = []
    node_slots = defaultdict(list)
    for label, num_instances in label2num_instances.items():
        logging.info('Creating %d nodes of type %s', num_instances, label)

        if label not in _get_slave_configuration():
            logging.error('No slave configuration for %s found', label)
            continue
        configuration = _get_slave_configuration()[label]

        for _ in range(0, num_instances):
            random_part = ''.join(random.choices(string.ascii_lowercase + string.digits, k=10))
            name = '{}_{}'.format(label, random_part)
            logging.debug('Creating slave slot %s of type %s', name, label)
            # Create node slot through Jenkins API. Unfortunately, we have to write our own implementation of
            # node.Node.get_node_attributes() to support all required options. This also reimplements
            # jenkins_server.nodes.create_node due to inefficiency of underlying implementation
            node_attributes_encoded = parse.urlencode(
                _custom_get_node_attributes(name=name, node_attributes=configuration))
            url = ('{}/computer/doCreateItem?{}'.format(jenkins_server.baseurl,
                                                        node_attributes_encoded))
            data = {'json': node_attributes_encoded}
            jobs.append({
                'url': url,
                'data': data
            })

            node_slots[label].append(name)

    if jobs:
        Parallel(n_jobs=min(JENKINS_PARALLEL_CREATE_REQUESTS_LIMIT, len(jobs)), backend="threading")(
            delayed(jenkins_server.requester.post_and_confirm_status)(url=job['url'], data=job['data'],
                                                                      allow_redirects=False) for job in jobs)
    else:
        logging.debug('No jenkins node slot to create')

    return node_slots