Beispiel #1
0
def input(job):
    from zeroos.orchestrator.sal.Node import Node
    from zeroos.orchestrator.configuration import get_configuration, get_jwt_token

    args = job.model.args
    ip = args.get('redisAddr')
    node = Node(ip, args.get('redisPort'), get_jwt_token(job.service.aysrepo))

    config = get_configuration(job.service.aysrepo)
    version = node.client.info.version()
    core0_version = config.get('0-core-version')
    core0_revision = config.get('0-core-revision')

    if (core0_version and core0_version != version['branch']) or \
            (core0_revision and core0_revision != version['revision']):
        raise RuntimeError("Node with IP {} has a wrong version. Found version {}@{} and expected version {}@{} ".format(ip, version['branch'], version['revision'], core0_version, core0_revision))
    def connector(self, remote, auth):
        """
        remote: remote address of the node
        auth: password (jwt token usualy) nfor client
        """
        print("[+] contacting zero-os server: %s" % remote)
        while True:
            try:
                node = Node(remote, password=auth)
                node.client.timeout = 180
                break

            except RuntimeError as e:
                print(
                    "[-] cannot connect server (make sure the server is reachable), retrying"
                )
                time.sleep(1)
                pass

        self.node = node

        return node
Beispiel #3
0
def try_authorize(job, logger, netid, member, zerotier):
    import time
    from zeroos.orchestrator.sal.Node import Node
    from zeroos.orchestrator.configuration import get_jwt_token

    service = job.service
    job.context['token'] = get_jwt_token(service.aysrepo)

    if not member['online'] or member['config']['authorized']:
        return

    # authorized new member
    logger.info("authorize new member {}".format(member['nodeId']))
    member['config']['authorized'] = True
    zerotier.network.updateMember(member, member['nodeId'], netid)

    # get assigned ip of this member
    resp = zerotier.network.getMember(member['nodeId'], netid)
    member = resp.json()
    while len(member['config']['ipAssignments']) <= 0:
        time.sleep(1)
        resp = zerotier.network.getMember(member['nodeId'], netid)
        member = resp.json()
    zerotier_ip = member['config']['ipAssignments'][0]

    # do hardwarechecks
    for prod in service.producers.get('hardwarecheck', []):
        hwcheck_job=prod.getJob('check', args={'ipaddr': zerotier_ip,
                                               'node_id': member['nodeId'],
                                               'jwt': get_jwt_token(service.aysrepo)})
        j.tools.async.wrappers.sync(hwcheck_job.execute())

    # test if we can connect to the new member
    node = Node(zerotier_ip, password=get_jwt_token(service.aysrepo))
    node.client.testConnectionAttempts = 0
    node.client.timeout = 10
    for attempt in range(5):
        try:
            logger.info("connection to g8os with IP: {}".format(zerotier_ip))
            node.client.ping()
            break
        except:
            continue
    else:
        raise RuntimeError("can't connect, unauthorize member IP: {}".format(zerotier_ip))

    # connection succeeded, set the hostname of the node to zerotier member
    member['name'] = node.name
    member['description'] = node.client.info.os().get('hostname', '')
    zerotier.network.updateMember(member, member['nodeId'], netid)

    # create node.zero-os service
    name = node.name
    try:
        nodeservice = service.aysrepo.serviceGet(role='node', instance=name)
        logger.info("service for node {} already exists, updating model".format(name))
        # mac sure the service has the correct ip in his model.
        # it could happend that a node get a new ip after a reboot
        nodeservice.model.data.redisAddr = zerotier_ip
        nodeservice.model.data.status = 'running'
        # after reboot we also wonna call install
        j.tools.async.wrappers.sync(nodeservice.executeAction('install', context=job.context))
    except j.exceptions.NotFound:
        # create and install the node.zero-os service
        if service.model.data.wipedisks:
            node.wipedisks()

        node_actor = service.aysrepo.actorGet('node.zero-os')
        networks = [n.name for n in service.producers.get('network', [])]

        node_args = {
            'id': name,
            'status': 'running',
            'networks': networks,
            'hostname': node.client.info.os()['hostname'],
            'redisAddr': zerotier_ip,
        }
        logger.info("create node.zero-os service {}".format(name))
        nodeservice = node_actor.serviceCreate(instance=name, args=node_args)
        try:

            logger.info("install node.zero-os service {}".format(name))
            j.tools.async.wrappers.sync(nodeservice.executeAction('install', context=job.context))
        except:
            j.tools.async.wrappers.sync(nodeservice.delete())
            raise

    # do ERP registrations
    for prod in service.producers.get('erp_registration', []):
        erp_job=prod.getJob('register', args={'node_id': member['nodeId']})
        j.tools.async.wrappers.sync(erp_job.execute())