示例#1
0
bool ClusterManager::ReleaseResourceOnMachine(const string& machine, const ClassAd& resource){
    string data;
    int32_t version;
    if(!ZKMasterI::Instance()->Get("/cluster" + machine + "/status", data, version)){
         return false;
    }
    if(data != "ONLINE") {
        return false;
    }
    if(!ZKMasterI::Instance()->Get("/cluster" + machine + "/resource", data, version)){
        return false;
    }

    ClassAdParser parser;
    ClassAd* classad_ptr = parser.ParseClassAd(data);

    //
    int32_t total_vcpu, total_memory, requried_vcpu, required_memory;
    if(!classad_ptr->EvaluateAttrInt("memory", total_memory) ||
       !classad_ptr->EvaluateAttrInt("vcpu", total_vcpu) ||
       !resource.EvaluateAttrInt("memory", required_memory) ||
       !resource.EvaluateAttrInt("vcpu", requried_vcpu)) {
        return false;
    }
   
    stringstream ss;
    ss << "[ vcpu = " << total_vcpu + requried_vcpu << "; memory = " << total_memory + required_memory << " ]"; 
    
    if(!ZKMasterI::Instance()->Set("/cluster" + machine + "/resource", ss.str(), version)){
        return false;
    }
    return true; 
}
示例#2
0
int main(){
    string ad_string = "[a = 1; b = \"Cardini\"]";
    ClassAd ad;
    ClassAdParser parser;

    int val;

    if(!parser.ParseClassAd(ad_string, ad, true)){
       return 1;
    }

    if(ad.EvaluateAttrInt("a", val)) {
        printf("%d\n",val);
    } else {
        printf("error\n");
        return 1;
    }
    return 0;
}
示例#3
0
static void test_user_functions(void)
{
	string name = "triple";
	string classad_text = "[ Test1 = 3 + triple(9); Test2 = 3 + double(9); "
		                  "Test3 = todays_date(); ]";
	ClassAd        *classad;
	ClassAdParser  parser;

	cout << "Testing user functions...\n";

	FunctionCall::RegisterFunction(name, triple);

#if defined(HAVE_DLOPEN) && !defined(DISABLE_DLOPEN_CHECK)
	bool opened_library;
	char path[10240];
	string libname;
	path[0] = 0;
	getcwd(path, 10239);
	libname = path;
	libname += "/";
	libname += TEST_LIBNAME; // TEST_LIBNAME is #define'd
	opened_library = FunctionCall::RegisterSharedLibraryFunctions(libname.c_str());
	if (!opened_library) {
		cout << "  Failed to open libshared.so: " << CondorErrMsg << endl;
	}
#endif

	classad = parser.ParseClassAd(classad_text);
	if (classad == NULL) {
		cout << "  Failed to parse ClassAd in test_internal_user_function().\n";
		exit(1);
	}

	int test;
	if (!classad->EvaluateAttrInt("Test1", test) || test != 30) {
		cout << "  Failed: Couldn't evaluate internal user function triple correctly.";
	} else {
		cout << "  OK: Evaluated internal user function triple (";
		cout << test << ") correctly.\n";
	}

#if defined(HAVE_DLOPEN) && !defined(DISABLE_DLOPEN_CHECK)
	if (opened_library) {
		// The library defines triple, but it shouldn't be allowed to
		// overwrite the original defintion, so we verify that the
		// result hasn't changed.
		if (!classad->EvaluateAttrInt("Test1", test) || test != 30) {
			cout << "  Failed: Couldn't evaluate internal user function triple correctly."
				 << endl;
		} else {
			cout << "  OK: Evaluated internal user function triple (";
			cout << test << ") correctly.\n";
		}

		// Then we test one function that comes from the shared library.
		if (!classad->EvaluateAttrInt("Test2", test) || test != 21) {
			cout << "  Failed: Couldn't evaluate shared user function double " 
				 << " (" << test << ") correctly." << endl;
			if (!classad->EvaluateAttrInt("Test2", test)) {
				cout << "  Couldn't even evaluate it at all." << endl;
			}
		} else {
			cout << "  OK: Evaluated shared user function double (";
			cout << test << ") correctly.\n";
		}

		// Then we test another function that comes from the shared library.
		string date;
		if (!classad->EvaluateAttrString("Test3", date)) {
			cout << "  Failed: Couldn't evaluate shared user function " 
				 << " todays_date (" << test << ") correctly." << endl;
		} else {
			cout << "  OK: Evaluated shared function todays_date: ";
			cout << date << endl;
		}
		
	}
#endif
	
	return;
}
示例#4
0
int
CondorQ::fetchQueueFromHostAndProcessV2(const char *host,
					const char *constraint,
					StringList &attrs,
					int fetch_opts,
					int match_limit,
					condor_q_process_func process_func,
					void * process_func_data,
					int connect_timeout,
					int useFastPath,
					CondorError *errstack,
					ClassAd ** psummary_ad)
{
	classad::ClassAdParser parser;
	classad::ExprTree *expr = NULL;
	parser.ParseExpression(constraint, expr);
	if (!expr) return Q_INVALID_REQUIREMENTS;

	classad::ClassAd request_ad;  // query ad to send to schedd
	ClassAd *ad = NULL;	// job ad result

	request_ad.Insert(ATTR_REQUIREMENTS, expr);

	char *projection = attrs.print_to_delimed_string("\n");
	if (projection) {
		request_ad.InsertAttr(ATTR_PROJECTION, projection);
		free(projection);
	}

	bool want_authentication = false;
	if (fetch_opts == fetch_DefaultAutoCluster) {
		request_ad.InsertAttr("QueryDefaultAutocluster", true);
		request_ad.InsertAttr("MaxReturnedJobIds", 2); // TODO: make this settable by caller of this function.
	} else if (fetch_opts == fetch_GroupBy) {
		request_ad.InsertAttr("ProjectionIsGroupBy", true);
		request_ad.InsertAttr("MaxReturnedJobIds", 2); // TODO: make this settable by caller of this function.
	} else {
		if (fetch_opts & fetch_MyJobs) {
			const char * owner = my_username();
			if (owner) { request_ad.InsertAttr("Me", owner); }
			request_ad.InsertAttr("MyJobs", owner ? "(Owner == Me)" : "true");
			want_authentication = true;
		}
		if (fetch_opts & fetch_SummaryOnly) {
			request_ad.InsertAttr("SummaryOnly", true);
		}
		if (fetch_opts & fetch_IncludeClusterAd) {
			request_ad.InsertAttr("IncludeClusterAd", true);
		}
	}

	if (match_limit >= 0) {
		request_ad.InsertAttr(ATTR_LIMIT_RESULTS, match_limit);
	}

	// determine if authentication can/will happen.  three reasons why it might not:
	// 1) security negotiation is disabled (NEVER or OPTIONAL for outgoing connections)
	// 2) Authentication is disabled (NEVER) by the client
	// 3) Authentication is disabled (NEVER) by the server.  this is actually impossible to
	//    get correct without actually querying the server but we make an educated guess by
	//    paraming the READ auth level.
	bool can_auth = true;
	char *paramer = NULL;

	paramer = SecMan::getSecSetting ("SEC_%s_NEGOTIATION", CLIENT_PERM);
	if (paramer != NULL) {
		char p = toupper(paramer[0]);
		free(paramer);
		if (p == 'N' || p == 'O') {
			// authentication will not happen since no security negotiation will occur
			can_auth = false;
		}
	}

	paramer = SecMan::getSecSetting ("SEC_%s_AUTHENTICATION", CLIENT_PERM);
	if (paramer != NULL) {
		char p = toupper(paramer[0]);
		free(paramer);
		if (p == 'N') {
			// authentication will not happen since client doesn't allow it.
			can_auth = false;
		}
	}

	// authentication will not happen since server probably doesn't allow it.
	// on the off chance that someone's config manages to trick us, leave an
	// undocumented knob as a last resort to disable our inference.
	if (param_boolean("CONDOR_Q_INFER_SCHEDD_AUTHENTICATION", true)) {
		paramer = SecMan::getSecSetting ("SEC_%s_AUTHENTICATION", READ);
		if (paramer != NULL) {
			char p = toupper(paramer[0]);
			free(paramer);
			if (p == 'N') {
				can_auth = false;
			}
		}

		paramer = SecMan::getSecSetting ("SCHEDD.SEC_%s_AUTHENTICATION", READ);
		if (paramer != NULL) {
			char p = toupper(paramer[0]);
			free(paramer);
			if (p == 'N') {
				can_auth = false;
			}
		}
	}

	if (!can_auth) {
		dprintf (D_ALWAYS, "detected that authentication will not happen.  falling back to QUERY_JOB_ADS without authentication.\n");
	}

	DCSchedd schedd(host);
	int cmd = QUERY_JOB_ADS;
	if (want_authentication && can_auth && (useFastPath > 2)) {
		cmd = QUERY_JOB_ADS_WITH_AUTH;
	}
	Sock* sock;
	if (!(sock = schedd.startCommand(cmd, Stream::reli_sock, connect_timeout, errstack))) return Q_SCHEDD_COMMUNICATION_ERROR;

	classad_shared_ptr<Sock> sock_sentry(sock);

	if (!putClassAd(sock, request_ad) || !sock->end_of_message()) return Q_SCHEDD_COMMUNICATION_ERROR;
	dprintf(D_FULLDEBUG, "Sent classad to schedd\n");

	int rval = 0;
	do {
		ad = new ClassAd();
		if ( ! getClassAd(sock, *ad) || ! sock->end_of_message()) {
			rval = Q_SCHEDD_COMMUNICATION_ERROR;
			break;
		}
		dprintf(D_FULLDEBUG, "Got classad from schedd.\n");
		long long intVal;
		if (ad->EvaluateAttrInt(ATTR_OWNER, intVal) && (intVal == 0))
		{ // Last ad.
			sock->close();
			dprintf(D_FULLDEBUG, "Ad was last one from schedd.\n");
			std::string errorMsg;
			if (ad->EvaluateAttrInt(ATTR_ERROR_CODE, intVal) && intVal && ad->EvaluateAttrString(ATTR_ERROR_STRING, errorMsg))
			{
				if (errstack) errstack->push("TOOL", intVal, errorMsg.c_str());
				rval = Q_REMOTE_ERROR;
			}
			if (psummary_ad && rval == 0) {
				std::string val;
				if (ad->LookupString(ATTR_MY_TYPE, val) && val == "Summary") {
					ad->Delete(ATTR_OWNER); // remove the bogus owner attribute
					*psummary_ad = ad; // return the final ad, because it has summary information
					ad = NULL; // so we don't delete it below.
				}
			}
			break;
		}
		// Note: According to condor_q.h, process_func() will return false if taking
		// ownership of ad, so only delete if it returns true, else set to NULL
		// so we don't delete it here.  Either way, next set ad to NULL since either
		// it has been deleted or will be deleted later by process_func().
		if (process_func(process_func_data, ad)) {
			delete ad;
		}
		ad = NULL;
	} while (true);

	// Make sure ad is not leaked no matter how we break out of the above loop.
	delete ad;

	return rval;
}