static gboolean db_node_find (nodePtr node, gpointer id) { GSList *iter; if (g_str_equal (node->id, (gchar *)id)) return TRUE; iter = node->children; while (iter) { if (db_node_find ((nodePtr)iter->data, id)) return TRUE; iter = g_slist_next (iter); } return FALSE; }
/* node register and authtication */ static int __node_register_auth(NodeInfo * nf, int ent_id) { if (nf->host_tag <= 0) { logerror(_("error in %s(%d)\n"), __func__, __LINE__); return -1; } int ret = -1; DBNodeRegInfo db_nf; bzero(&db_nf, sizeof(DBNodeRegInfo)); int found = db_node_find(DB_NODE_FIND_BY_ID, &nf->host_tag, &db_nf); if (found == 1) { logdebug(_("tagged node found in db(%d %s %d %d)\n"), db_nf.id, db_nf.ip, db_nf.status, db_nf.enabled); if (!ly_entity_is_authenticated(ent_id)) { logwarn(_("authentication is required for node(%d, %s)\n"), nf->host_tag, nf->host_ip); goto out; } if (strcmp(nf->host_ip, db_nf.ip) != 0) logwarn(_("tagged node ip changed from %s to %s\n"), db_nf.ip, nf->host_ip); nf->cpu_vlimit = db_nf.cpu_vlimit; nf->mem_vlimit = db_nf.mem_vlimit; ly_entity_enable(ent_id, db_nf.id, db_nf.enabled); ret = LY_S_REGISTERING_DONE_SUCCESS; } else if (found == 0) { logwarn(_("invalid tag for node(%d, %s), re-registration needed\n"), nf->host_tag, nf->host_ip); ret = LY_S_REGISTERING_REINIT; goto out; } else { logerror(_("error in %s(%d)\n"), __func__, __LINE__); goto out; } out: db_node_reginfo_free(&db_nf); return ret; }
void db_node_cleanup (nodePtr root) { sqlite3_stmt *stmt; debug0 (DEBUG_DB, "Cleaning node ids..."); /* Fetch all node ids */ stmt = db_get_statement ("nodeIdListStmt"); while (sqlite3_step (stmt) == SQLITE_ROW) { /* Drop node ids not in feed list anymore */ const gchar *id = sqlite3_column_text (stmt, 0); if (!db_node_find (root, (gpointer)id)) { db_subscription_remove (id); /* in case it is a subscription */ db_node_remove (id); /* in case it is a folder */ } } sqlite3_finalize (stmt); }
/* process node register request */ static int __node_xml_register(xmlDoc * doc, xmlNode * node, int ent_id) { if (ly_entity_is_registered(ent_id)) { logwarn(_("received node register request again, ignored\n")); return -1; } LYNodeData * nd = ly_entity_data(ent_id); if (nd == NULL ) { logerror(_("error in %s(%d)\n"), __func__, __LINE__); return -1; } NodeInfo * nf = &nd->node; /* Create xpath evaluation context */ xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc); if (xpathCtx == NULL) { logerror(_("unable to create new XPath context %s, %d\n"), __func__, __LINE__); return -1; } int ret = -1; char *str; str = xml_xpath_text_from_ctx(xpathCtx, "/" LYXML_ROOT "/request/parameters/status"); if (str == NULL) goto xml_err; nf->status = atoi(str); free(str); str = xml_xpath_text_from_ctx(xpathCtx, "/" LYXML_ROOT "/request/parameters/hypervisor"); if (str == NULL) goto xml_err; nf->hypervisor = atoi(str); free(str); str = xml_xpath_text_from_ctx(xpathCtx, "/" LYXML_ROOT "/request/parameters/host/tag"); int tag = -1; if (str) { tag = atoi(str); /* NULL str is allowed for new node */ free(str); } str = xml_xpath_text_from_ctx(xpathCtx, "/" LYXML_ROOT "/request/parameters/host/name"); if (str == NULL) goto xml_err; nf->host_name = str; str = xml_xpath_text_from_ctx(xpathCtx, "/" LYXML_ROOT "/request/parameters/host/ip"); if (str == NULL) goto xml_err; nf->host_ip = str; str = xml_xpath_text_from_ctx(xpathCtx, "/" LYXML_ROOT "/request/parameters/cpu/arch"); if (str == NULL) goto xml_err; nf->cpu_arch = atoi(str); free(str); str = xml_xpath_text_from_ctx(xpathCtx, "/" LYXML_ROOT "/request/parameters/cpu/model"); if (str == NULL) goto xml_err; nf->cpu_model = str; str = xml_xpath_text_from_ctx(xpathCtx, "/" LYXML_ROOT "/request/parameters/cpu/mhz"); if (str == NULL) goto xml_err; nf->cpu_mhz = atoi(str); free(str); str = xml_xpath_text_from_ctx(xpathCtx, "/" LYXML_ROOT "/request/parameters/cpu/max"); if (str == NULL) goto xml_err; nf->cpu_max = atoi(str); nf->cpu_vlimit = NODE_SCHEDULE_CPU_LIMIT(nf->cpu_max); free(str); str = xml_xpath_text_from_ctx(xpathCtx, "/" LYXML_ROOT "/request/parameters/cpu/commit"); if (str == NULL) goto xml_err; nf->cpu_commit = atoi(str); free(str); str = xml_xpath_text_from_ctx(xpathCtx, "/" LYXML_ROOT "/request/parameters/memory/total"); if (str == NULL) goto xml_err; nf->mem_max = atoi(str); nf->mem_vlimit = NODE_SCHEDULE_MEM_LIMIT(nf->mem_max); free(str); str = xml_xpath_text_from_ctx(xpathCtx, "/" LYXML_ROOT "/request/parameters/memory/free"); if (str == NULL) goto xml_err; nf->mem_free = atoi(str); free(str); str = xml_xpath_text_from_ctx(xpathCtx, "/" LYXML_ROOT "/request/parameters/memory/commit"); if (str == NULL) goto xml_err; nf->mem_commit = atoi(str); free(str); str = xml_xpath_text_from_ctx(xpathCtx, "/" LYXML_ROOT "/request/parameters/storage/total"); if (str == NULL) goto xml_err; nf->storage_total = atoi(str); free(str); str = xml_xpath_text_from_ctx(xpathCtx, "/" LYXML_ROOT "/request/parameters/storage/free"); if (str == NULL) goto xml_err; nf->storage_free = atoi(str); free(str); str = xml_xpath_text_from_ctx(xpathCtx, "/" LYXML_ROOT "/request/parameters/load/average"); if (str == NULL) goto xml_err; nf->load_average = atoi(str); free(str); if (nf->status >= NODE_STATUS_REGISTERED) { logwarn(_("node(%d, %s) tries to register from wrong status(%d)\n"), nf->host_tag, nf->host_ip, nf->status); ly_entity_release(ent_id); ret = 0; /* no need to continue node registration */ goto done; } if (ly_entity_node_active(nf->host_ip)) { logwarn(_("duplicate node ip received. something is wrong...\n")); ly_entity_release(ent_id); ret = 0; /* no need to continue node registration */ goto done; } if (tag > 0) { /* check authentication result */ if (nf->host_tag > 0 && tag != nf->host_tag) { logerror(_("node tag changed, %d -> %d. something is wrong.\n"), nf->host_tag, tag); goto done; } nf->host_tag = tag; ret = __node_register_auth(nf, ent_id); if (ret == LY_S_REGISTERING_DONE_SUCCESS) { AuthConfig * ac = ly_entity_auth(ent_id); if (db_node_update_secret(DB_NODE_FIND_BY_ID, &tag, ac->secret) < 0 || db_node_update_status(DB_NODE_FIND_BY_ID, &tag, NODE_STATUS_REGISTERED) < 0) { logerror(_("error in %s(%d)\n"), __func__, __LINE__); ret = -1; goto done; } loginfo(_("node(tag:%d) registered\n"), tag); ly_entity_update(ent_id, tag, LY_ENTITY_FLAG_STATUS_REGISTERED); } goto done; } /* new node */ DBNodeRegInfo db_nf; bzero(&db_nf, sizeof(DBNodeRegInfo)); ret = db_node_find(DB_NODE_FIND_BY_IP, nf->host_ip, &db_nf); if (ret < 0 || ret > 1) { logerror(_("error in %s(%d)\n"), __func__, __LINE__); ret = -1; goto done; } if (ret == 0 || db_nf.secret) { /* new node */ logdebug(_("new node\n")); if (nf->status != NODE_STATUS_UNINITIALIZED) { logwarn(_("node(%d, %s) tries to register from unexpected status(%d)\n"), nf->host_tag, nf->host_ip, nf->status); nf->status = NODE_STATUS_UNINITIALIZED; } if (db_nf.secret) { logwarn(_("new node takes ip(%s) used by tagged node\n"), nf->host_ip); db_node_reginfo_free(&db_nf); bzero(&db_nf, sizeof(DBNodeRegInfo)); } ret = db_node_insert(nf); if (ret < 0) { logerror(_("error in %s(%d)\n"), __func__, __LINE__); goto new_done; } db_nf.id = ret; loginfo(_("new node %s added in db(%d)\n"), nf->host_ip, ret); /* enable node if node is control server */ if (ly_is_clc_ip(nf->host_ip)) { if (db_node_enable(ret, 1) != 0 || db_node_find(DB_NODE_FIND_BY_ID, &ret, &db_nf) != 1) { logerror(_("error in %s(%d)\n"), __func__, __LINE__); ret = -1; goto new_done; } } } else logdebug(_("untagged node for ip(%s) found in db\n"), nf->host_ip); ly_entity_update(ent_id, db_nf.id, LY_ENTITY_FLAG_STATUS_ONLINE); if (db_nf.enabled) { AuthConfig * ac = ly_entity_auth(ent_id); ret = -1; if (ac->secret) { logerror(_("error in %s(%d)\n"), __func__, __LINE__); goto new_done; } ac->secret = lyauth_secret(); if (ac->secret == NULL) { logerror(_("error in %s(%d)\n"), __func__, __LINE__); goto new_done; } nf->host_tag = db_nf.id; ret = LY_S_REGISTERING_CONFIG; } else { logdebug(_("register request done, node %s not enabled\n"), nf->host_ip); ret = LY_S_REGISTERING_INIT; } nf->status = NODE_STATUS_ONLINE; if (db_node_update(DB_NODE_FIND_BY_ID, &db_nf.id, nf) < 0) { logerror(_("error in %s(%d)\n"), __func__, __LINE__); ret = -1; } new_done: db_node_reginfo_free(&db_nf); goto done; xml_err: logerror(_("invalid node xml register request\n")); done: xmlXPathFreeContext(xpathCtx); logdebug(_("end of %s, node status %d\n"), __func__, nf->status); return ret; }