2008-07-10 15:24:48 +04:00
#!/usr/bin/env ruby
2009-01-19 21:05:46 +03:00
# -------------------------------------------------------------------------- #
2018-01-02 20:27:37 +03:00
# Copyright 2002-2018, OpenNebula Project, OpenNebula Systems #
2009-01-19 21:05:46 +03:00
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
#--------------------------------------------------------------------------- #
2008-07-10 15:24:48 +04:00
require 'pp'
2008-07-22 20:15:38 +04:00
require 'digest/md5'
2010-07-17 03:58:24 +04:00
require 'fileutils'
2008-07-14 16:56:37 +04:00
###########################################
# Code to test compilation/linkning flags #
###########################################
TestCodeServer="""
2008-07-10 15:24:48 +04:00
#include <xmlrpc-c/base.hpp>
#include <xmlrpc-c/registry.hpp>
int main(int argc, char *argv[])
{
xmlrpc_c::registry RequestManagerRegistry;
return 0;
2008-07-14 16:56:37 +04:00
}"""
TestCodeClient=<<-EOT
2008-07-10 15:24:48 +04:00
#include <cstdlib>
#include <string>
#include <iostream>
#include <xmlrpc-c/girerr.hpp>
#include <xmlrpc-c/base.hpp>
#include <xmlrpc-c/client_simple.hpp>
#include <map>
using namespace std;
int
main(int argc, char **) {
if (argc-1 > 0) {
cerr << "This program has no arguments" << endl;
exit(1);
}
try {
xmlrpc_env env;
string const serverUrl("http://localhost:8080/RPC2");
string const methodAllocate("one.vmallocate");
xmlrpc_c::clientSimple myClient;
xmlrpc_c::value resultSUBMIT;
xmlrpc_env_init(&env);
myClient.call(serverUrl, methodAllocate, "ss",
&resultSUBMIT,"SESSION-GOLA&4H9109KVFSG",
"MEMORY=345 CPU=4 DISK=[FILE=\\"img\\",TYPE=cd]"
"DISK=[FILE=\\"../f\\"]");
xmlrpc_c::value_array resultArray = xmlrpc_c::value_array(resultSUBMIT);
vector<xmlrpc_c::value> const paramArrayValue(resultArray.vectorValueValue());
//check posible Errors:
2011-04-22 02:39:17 +04:00
xmlrpc_c::value firstvalue;
firstvalue = static_cast<xmlrpc_c::value>(paramArrayValue[0]);
xmlrpc_c::value_boolean status = static_cast<xmlrpc_c::value_boolean>(firstvalue);
2008-07-10 15:24:48 +04:00
2011-04-22 02:39:17 +04:00
xmlrpc_c::value secondvalue;
secondvalue = static_cast<xmlrpc_c::value>(paramArrayValue[1]);
xmlrpc_c::value_string valueS = static_cast<xmlrpc_c::value_string>(secondvalue);
2008-07-10 15:24:48 +04:00
2011-04-22 02:39:17 +04:00
if(static_cast<bool>(status)) {
2008-07-10 15:24:48 +04:00
//Success, returns the id assigned to the VM:
2011-04-22 02:39:17 +04:00
cout << "vmid returned: " << static_cast<string>(valueS) << endl;
2008-07-10 15:24:48 +04:00
return 0;
}
else{ //Failure:
2011-04-22 02:39:17 +04:00
string error_value=static_cast<string>(valueS);
2008-07-10 15:24:48 +04:00
if (error_value.find("Error inserting",0)!=string::npos ) cout << "Error inserting VM in the database" << endl;
else if (error_value.find("Error parsing",0)!=string::npos ) cout << "Error parsing VM template" << endl;
2011-04-22 02:39:17 +04:00
else cout << "Unknown error " << static_cast<string>(valueS) << endl;
2008-07-10 15:24:48 +04:00
};
} catch (girerr::error const error) {
cerr << "Client threw error: " << error.what() << endl;
//"Client threw error:"
return 20;
} catch (std::exception const e) {
cerr << "Client threw unexpected error." << endl;
//Unexpected error:
return 999;
}
return 0;
}
EOT
2008-07-14 16:56:37 +04:00
TestCode={
:client => TestCodeClient,
:server => TestCodeServer
}
2008-07-10 15:24:48 +04:00
2008-07-14 16:56:37 +04:00
# Executes a command and discards stderr
2008-09-30 20:07:51 +04:00
def exec_command(command)
2008-07-10 15:24:48 +04:00
lambda {
2008-07-14 16:56:37 +04:00
#STDERR.puts text
2008-09-30 20:07:51 +04:00
text=`#{command} 2>/dev/null`
text.gsub!("\n", " ")
2008-07-10 15:24:48 +04:00
if $?!=0
2008-09-30 20:07:51 +04:00
STDERR.puts " Error calling #{command}"
2008-07-10 15:24:48 +04:00
nil
else
text
end
}
end
2008-07-14 16:56:37 +04:00
# Adds fixed flags and libraries
2008-07-10 15:24:48 +04:00
def flags_and_libs_array(flags, libs)
lambda {
libs_text=libs.collect {|lib| "-l"+lib }.join(' ')
text=flags+" "+libs_text
2008-07-14 16:56:37 +04:00
#STDERR.puts text
2008-07-10 15:24:48 +04:00
text
}
end
2008-07-14 16:56:37 +04:00
# Array with flags/libs to test
2008-07-10 15:24:48 +04:00
Configs=[
2008-09-11 21:41:02 +04:00
# Configuration using xmlrpc-c-config
{
:description => "xmlrpc-c-config",
2008-09-30 20:07:51 +04:00
:client => exec_command("xmlrpc-c-config c++2 client --libs --cflags"),
:server => exec_command("xmlrpc-c-config c++2 abyss-server --libs --cflags")
2008-09-11 21:41:02 +04:00
},
2013-10-17 19:04:23 +04:00
# Configuration for fedora
{
:description => "pkg-config",
:client => exec_command("pkg-config xmlrpc_client++ xmlrpc++ --libs"),
:server => exec_command("pkg-config xmlrpc_server_abyss++ --static --libs")
},
2008-09-11 21:41:02 +04:00
# Debian lenny
{
:description => "mixed hardcoded libraries and xmlrpc-c-config (debian lenny)",
:client => flags_and_libs_array("-I/usr/include -L/usr/lib", [
'xmlrpc_client', 'xmlrpc_client', 'xmlrpc', 'xmlrpc_util',
'xmlrpc_xmlparse', 'xmlrpc_xmltok', 'xmlrpc_client++', 'xmlrpc++'
]),
2008-09-30 20:07:51 +04:00
:server => exec_command("xmlrpc-c-config c++2 abyss-server --libs --cflags"),
2008-09-11 21:41:02 +04:00
},
2008-07-14 16:56:37 +04:00
# Configuration for Mac OS X and Debian
2008-07-10 15:24:48 +04:00
{
2008-09-11 21:41:02 +04:00
:description => "hardcoded libraries for Mac OS X (installed using port)",
2008-07-10 15:24:48 +04:00
:server => flags_and_libs_array("-I/opt/local/include -L/opt/local/lib", [
2008-07-14 16:56:37 +04:00
'wwwxml', 'xmltok', 'xmlparse', 'wwwzip', 'wwwinit', 'wwwapp',
'wwwtelnet', 'wwwhtml', 'wwwnews', 'wwwhttp', 'wwwmime', 'wwwgopher',
'wwwftp', 'wwwfile', 'wwwdir', 'wwwcache', 'wwwstream', 'wwwmux',
'wwwtrans', 'wwwcore', 'wwwutils', 'md5', 'dl', 'z', 'pthread',
'xmlrpc_client++', 'xmlrpc_client', 'xmlrpc++', 'xmlrpc',
'xmlrpc_util', 'xmlrpc_xmlparse', 'xmlrpc_xmltok',
'xmlrpc_server_abyss++', 'xmlrpc_server++', 'xmlrpc_server_abyss',
'xmlrpc_server', 'xmlrpc_abyss',
2008-07-10 15:24:48 +04:00
]),
:client => flags_and_libs_array("-I/opt/local/include -L/opt/local/lib", [
2008-07-14 16:56:37 +04:00
'curl', 'xmlrpc_client++', 'xmlrpc_client', 'xmlrpc++', 'xmlrpc',
'xmlrpc_util', 'xmlrpc_xmlparse', 'xmlrpc_xmltok', 'wwwxml',
'xmltok', 'xmlparse', 'wwwzip', 'wwwinit', 'wwwapp', 'wwwtelnet',
'wwwhtml', 'wwwnews', 'wwwhttp', 'wwwmime', 'wwwgopher', 'wwwftp',
'wwwfile', 'wwwdir', 'wwwcache', 'wwwstream', 'wwwmux', 'wwwtrans',
'wwwcore', 'wwwutils', 'm', 'md5'
2008-07-10 15:24:48 +04:00
])
2008-07-14 16:56:37 +04:00
},
# Example adding more custom libraries
# {
# :client => exec_command("xmlrpc-c-config client c++ --cflags --libs"),
# :server => exec_command("xmlrpc-c-config abyss-server c++ --cflags --libs | tr '\\n' ' ' ; echo -lxmlrpc_server++")
# },
2008-07-10 15:24:48 +04:00
]
2008-07-14 16:56:37 +04:00
# Compiles test code with given arguments
2008-07-10 15:24:48 +04:00
def compile(file, args)
2008-09-30 20:07:51 +04:00
logfile="#{file}.log"
2011-12-20 14:34:11 +04:00
command="g++ #{file} -o #{file}.out #{ENV['LDFLAGS']} -pthread #{args} 1>>#{logfile} 2>&1"
2008-09-30 20:07:51 +04:00
2008-09-30 20:07:52 +04:00
open(logfile, "a") {|f|
f.write(command+"\n")
2008-09-30 20:07:51 +04:00
}
2008-09-11 21:41:02 +04:00
STDERR.puts command
STDERR.puts ""
2008-07-10 15:24:48 +04:00
out=system(command)
end
2008-07-14 16:56:37 +04:00
def gen_test_file(kind)
dir='.xmlrpc_test'
hash=Digest::MD5.hexdigest(Time.now.to_s+kind.to_s)[0..5]
fname="xmlrpc_test."+hash+".cc"
#fname="xmlrpc_test.cc"
full_path=dir+"/"+fname
2010-07-17 03:58:24 +04:00
FileUtils::mkdir_p(dir)
2008-07-14 16:56:37 +04:00
f=open(full_path, "w")
2008-07-10 15:24:48 +04:00
f.write(TestCode[kind])
f.close
2008-07-14 16:56:37 +04:00
full_path
end
# Tests a configuration and writes build parameters if it is successful
def test_config(kind, config)
2008-09-11 21:41:02 +04:00
STDERR.puts "Testing recipe: "+config[:description].to_s
2008-07-10 15:24:48 +04:00
args=config[kind.to_sym].call
return nil if !args
args.strip!
2008-07-14 16:56:37 +04:00
name=gen_test_file(kind)
2008-07-10 15:24:48 +04:00
exit_code=compile(name, args)
2008-09-11 21:44:24 +04:00
#File.delete(name)
2008-08-19 17:52:54 +04:00
File.delete(name+".out") if File.exists?(name+".out")
2008-07-10 15:24:48 +04:00
return nil if !exit_code
puts args
true
end
2008-07-14 16:56:37 +04:00
# Test each of possible configurations and exits when it finds one working
2008-07-10 15:24:48 +04:00
def search_config(kind)
Configs.each {|config|
found=test_config(kind,config)
exit(0) if found
}
exit(1)
end
2008-07-14 16:56:37 +04:00
if !%w{client server}.include?ARGV[0]
puts "You need to specify client or server"
exit -1
end
2008-07-10 15:24:48 +04:00
search_config(ARGV[0].to_sym)