00001 #include <string>
00002 #include <iostream>
00003 #include <memory>
00004 #include <cassert>
00005 #include <dirent.h>
00006 #include "otsdaq-core/ConfigurationInterface/ConfigurationManagerRW.h"
00007 #include "otsdaq-core/ConfigurationInterface/ConfigurationInterface.h"
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 using namespace ots;
00018
00019
00020 void FlattenActiveConfigurationGroups(int argc, char* argv[])
00021 {
00022 std::cout << "=================================================\n";
00023 std::cout << "=================================================\n";
00024 std::cout << "=================================================\n";
00025 std::cout << __COUT_HDR_FL__ << "\nFlattening Active Configuration Groups!" << std::endl;
00026
00027 std::cout << "\n\nusage: Two arguments:\n\t pathToSwapIn <flatVersion> <pathToSwapIn (optional)> \n\n" <<
00028 "\t Default values: flatVersion = 0, pathToSwapIn = \"\" \n\n" <<
00029 std::endl;
00030
00031 std::cout << "\n\nNote: you can optionally just swap databases (and not modify their contents at all)" <<
00032 " by providing an invalid flatVersion of -1.\n\n" <<
00033 std::endl;
00034
00035 std::cout << "\n\nNote: This assumes artdaq db file type interface. " <<
00036 "The current database/ will be moved to database_<linuxtime>/ " <<
00037 "and if a pathToSwapIn is specified it will be copied to database/ " <<
00038 "before saving the currently active groups.\n\n" <<
00039 std::endl;
00040
00041 std::cout << "argc = " << argc << std::endl;
00042 for(int i = 0; i < argc; i++)
00043 std::cout << "argv[" << i << "] = " << argv[i] << std::endl;
00044
00045 if(argc < 2)
00046 {
00047 std::cout << "Must provide at least one parameter.";
00048 return;
00049 }
00050
00051
00052 std::string flatVersionStr = argv[1];
00053 if(flatVersionStr.find('h') != std::string::npos)
00054 {
00055 std::cout << "Recognized parameter 1. as a 'help' option. Usage was printed. Exiting." << std::endl;
00056 return;
00057 }
00058
00059 int flatVersion = 0;
00060 std::string pathToSwapIn = "";
00061 if(argc >= 2)
00062 sscanf(argv[1],"%d",&flatVersion);
00063 if(argc >= 3)
00064 pathToSwapIn = argv[2];
00065
00066 std::cout << __COUT_HDR_FL__ << "flatVersion = " << flatVersion << std::endl;
00067 std::cout << __COUT_HDR_FL__ << "pathToSwapIn = " << pathToSwapIn << std::endl;
00068
00069
00070
00071
00072
00073
00074
00075 setenv("CONFIGURATION_TYPE","File",1);
00076 setenv("CONFIGURATION_DATA_PATH",(std::string(getenv("USER_DATA")) + "/ConfigurationDataExamples").c_str(),1);
00077 setenv("CONFIGURATION_INFO_PATH",(std::string(getenv("USER_DATA")) + "/ConfigurationInfo").c_str(),1);
00079
00080
00081 setenv("SERVICE_DATA_PATH",(std::string(getenv("USER_DATA")) + "/ServiceData").c_str(),1);
00082
00083
00084 setenv("XDAQ_CONFIGURATION_DATA_PATH",(std::string(getenv("USER_DATA")) + "/XDAQConfigurations").c_str(),1);
00085 setenv("XDAQ_CONFIGURATION_XML","otsConfigurationNoRU_CMake",1);
00087
00088
00089
00090
00091
00092 ConfigurationManagerRW cfgMgrInst("flatten_admin");
00093 ConfigurationManagerRW *cfgMgr = &cfgMgrInst;
00094
00095 std::cout << __COUT_HDR_FL__ << "\n\n\nLoading activeGroupsMap..." << std::endl;
00096
00097
00098 std::map<std::string, std::pair<std::string, ConfigurationGroupKey> > activeGroupsMap =
00099 cfgMgr->getActiveConfigurationGroups();
00100
00101 std::map<std::string, std::map<std::string, ConfigurationVersion> > activeGroupMembersMap;
00102 for(auto &activeGroupPair: activeGroupsMap)
00103 {
00104 if(activeGroupPair.second.second.isInvalid())
00105 {
00106 std::cout << __COUT_HDR_FL__ << "Skipping invalid " <<
00107 activeGroupPair.first << std::endl;
00108 continue;
00109 }
00110
00111 std::cout << __COUT_HDR_FL__ << "Loading members for " <<
00112 activeGroupPair.first << "\t" <<
00113 activeGroupPair.second.first << "(" << activeGroupPair.second.second << ")" <<
00114 std::endl;
00115
00116 activeGroupMembersMap[activeGroupPair.second.first] =
00117 cfgMgr->getConfigurationInterface()->getConfigurationGroupMembers(
00118 ConfigurationGroupKey::getFullGroupString(
00119 activeGroupPair.second.first,activeGroupPair.second.second));
00120 }
00121
00122
00123
00124
00125 std::string currentDir = getenv("ARTDAQ_DATABASE_URI");
00126
00127 if(currentDir.find("filesystemdb://") != 0)
00128 throw std::runtime_error("filesystemdb:// was not found in $ARTDAQ_DATABASE_URI!");
00129
00130 currentDir = currentDir.substr(std::string("filesystemdb://").length());
00131 while(currentDir.length() && currentDir[currentDir.length()-1] == '/')
00132 currentDir = currentDir.substr(0,currentDir.length()-1);
00133 std::string moveToDir = currentDir + "_" + std::to_string(time(0));
00134
00135 std::cout << __COUT_HDR_FL__ << "Moving current directory: \t" << currentDir << std::endl;
00136 std::cout << __COUT_HDR_FL__ << "\t... to: \t\t" << moveToDir << std::endl;
00137
00138 if(argc < 2)
00139 throw std::runtime_error("Aborting move! Must at least give version argument to flatten to!");
00140
00141 rename(currentDir.c_str(),moveToDir.c_str());
00142 FILE *fp = fopen((moveToDir + "/README_otsdaq_flatten.txt").c_str(),"a");
00143 if(!fp)
00144 std::cout << __COUT_HDR_FL__ << "\tError opening README file!" << std::endl;
00145 else
00146 {
00147 time_t rawtime;
00148 struct tm * timeinfo;
00149 char buffer [200];
00150
00151 time (&rawtime);
00152 timeinfo = localtime (&rawtime);
00153 strftime (buffer,200,"%b %d, %Y %I:%M%p %Z",timeinfo);
00154
00155 fprintf(fp,"This database was moved from...\n\t %s \nto...\n\t %s \nat this time \n\t %lu \t %s\n\n\n",
00156 currentDir.c_str(),moveToDir.c_str(),time(0),buffer);
00157
00158 fclose(fp);
00159 }
00160
00161 if(pathToSwapIn != "")
00162 {
00163 DIR *dp;
00164 if((dp = opendir(pathToSwapIn.c_str())) == 0)
00165 {
00166 std::cout << __COUT_HDR_FL__<< "ERROR:(" << errno << "). Can't open directory: " << pathToSwapIn << std::endl;
00167 exit(0);
00168 }
00169 closedir(dp);
00170
00171 std::cout << __COUT_HDR_FL__ << "Swapping in directory: \t" << pathToSwapIn << std::endl;
00172 std::cout << __COUT_HDR_FL__ << "\t.. to: \t\t" << currentDir << std::endl;
00173
00174 rename(pathToSwapIn.c_str(),currentDir.c_str());
00175 FILE *fp = fopen((currentDir + "/README_otsdaq_flatten.txt").c_str(),"a");
00176 if(!fp)
00177 std::cout << __COUT_HDR_FL__ << "\tError opening README file!" << std::endl;
00178 else
00179 {
00180 time_t rawtime;
00181 struct tm * timeinfo;
00182 char buffer [200];
00183
00184 time (&rawtime);
00185 timeinfo = localtime (&rawtime);
00186 strftime (buffer,200,"%b %d, %Y %I:%M:%S%p %Z",timeinfo);
00187
00188 fprintf(fp,"This database was moved from...\t %s \t to...\t %s at this time \t %lu \t %s\n\n",
00189 pathToSwapIn.c_str(),currentDir.c_str(),time(0),buffer);
00190 fclose(fp);
00191 }
00192 }
00193
00194
00195
00196
00197
00198 ConfigurationInterface* theInterface_ = ConfigurationInterface::getInstance(false);
00199 ConfigurationView* cfgView;
00200 ConfigurationBase* config;
00201
00202 std::map<std::string, ConfigurationVersion> activeMap = cfgMgr->getActiveVersions();
00203
00204
00205
00206
00207
00208 const std::string groupAliasesName = "GroupAliasesConfiguration";
00209 const std::string versionAliasesName = "VersionAliasesConfiguration";
00210
00211
00212
00213 if(ConfigurationVersion(flatVersion).isInvalid() ||
00214 ConfigurationVersion(flatVersion).isTemporaryVersion())
00215 {
00216 std::cout << __COUT_HDR_FL__ << "\n\nflatVersion " << ConfigurationVersion(flatVersion) <<
00217 " is an invalid or temporary version. Skipping to end!" << std::endl;
00218 goto CLEAN_UP;
00219 }
00220
00221
00222
00223 if(activeMap.find(groupAliasesName) != activeMap.end())
00224 {
00225 std::cout << __COUT_HDR_FL__ << "\n\nModifying " << groupAliasesName << std::endl;
00226 config = cfgMgr->getConfigurationByName(groupAliasesName);
00227 cfgView = config->getViewP();
00228
00229 unsigned int col1 = cfgView->findCol("GroupName");
00230 unsigned int col2 = cfgView->findCol("GroupKey");
00231
00232
00233 for(unsigned int row = 0; row<cfgView->getNumberOfRows(); ++row )
00234 for(auto &activeGroupPair: activeGroupsMap)
00235 if(activeGroupPair.second.second.isInvalid()) continue;
00236 else if(cfgView->getDataView()[row][col1] == activeGroupPair.second.first &&
00237 cfgView->getDataView()[row][col2] == activeGroupPair.second.second.toString())
00238 {
00239
00240 std::cout << __COUT_HDR_FL__ <<
00241 "Changing row " << row << " for " <<
00242 cfgView->getDataView()[row][col1] << " key=" <<
00243 cfgView->getDataView()[row][col2] << " to DEFAULT=" <<
00244 ConfigurationGroupKey(ConfigurationGroupKey::getDefaultKey()) << std::endl;
00245 cfgView->setValue(
00246 ConfigurationGroupKey(ConfigurationGroupKey::getDefaultKey()).toString(),
00247 row,col2);
00248 break;
00249 }
00250
00251 }
00252
00253
00254 if(activeMap.find(versionAliasesName) != activeMap.end())
00255 {
00256 std::cout << __COUT_HDR_FL__ << "\n\nModifying " << versionAliasesName << std::endl;
00257 config = cfgMgr->getConfigurationByName(versionAliasesName);
00258 cfgView = config->getViewP();
00259 unsigned int col1 = cfgView->findCol("ConfigurationName");
00260 unsigned int col2 = cfgView->findCol("Version");
00261
00262
00263 for(unsigned int row = 0; row<cfgView->getNumberOfRows(); ++row )
00264 for(auto &activePair:activeMap)
00265 if(cfgView->getDataView()[row][col1] == activePair.first &&
00266 cfgView->getDataView()[row][col2] == activePair.second.toString())
00267 {
00268
00269 std::cout << __COUT_HDR_FL__ << "Changing row " << row << " for " <<
00270 cfgView->getDataView()[row][col1] << " version=" <<
00271 cfgView->getDataView()[row][col2] << " to flatVersion=" <<
00272 ConfigurationVersion(flatVersion) << std::endl;
00273 cfgView->setValue(ConfigurationVersion(flatVersion).toString(),row,col2);
00274 break;
00275 }
00276 }
00277
00278 std::cout << __COUT_HDR_FL__ << "\n\nChanging versions... " << std::endl;
00279
00280 for(auto &activePair:activeMap)
00281 {
00282 std::cout << __COUT_HDR_FL__ << activePair.first << ":v" << activePair.second << std::endl;
00283
00284 config = cfgMgr->getConfigurationByName(activePair.first);
00285 cfgView = config->getViewP();
00286 cfgView->setVersion(ConfigurationVersion(flatVersion));
00287 theInterface_->saveActiveVersion(config);
00288 }
00289
00290
00291
00292
00293 for(auto &activeGroupMembersPair: activeGroupMembersMap)
00294 {
00295 std::cout << __COUT_HDR_FL__ << "Group " << activeGroupMembersPair.first << std::endl;
00296
00297 for(auto &groupMemberPair: activeGroupMembersPair.second)
00298 {
00299 std::cout << __COUT_HDR_FL__ << "\t from...\t" <<
00300 groupMemberPair.first << ":v" << groupMemberPair.second << std::endl;
00301 groupMemberPair.second = flatVersion;
00302 }
00303
00304 for(auto &groupMemberPair: activeGroupMembersPair.second)
00305 {
00306 std::cout << __COUT_HDR_FL__ << "\t to...\t" <<
00307 groupMemberPair.first << ":v" << groupMemberPair.second << std::endl;
00308 }
00309
00310 theInterface_->saveConfigurationGroup(activeGroupMembersPair.second,
00311 ConfigurationGroupKey::getFullGroupString(
00312 activeGroupMembersPair.first,
00313 ConfigurationGroupKey(ConfigurationGroupKey::getDefaultKey())));
00314 }
00315
00316 CLEAN_UP:
00317
00318 std::cout << __COUT_HDR_FL__ << "\n\nEnd of Flattening Active Configuration Groups!\n\n\n" << std::endl;
00319
00320
00321 std::cout << __COUT_HDR_FL__ << "Run the following to return to your previous database structure:" <<
00322 std::endl;
00323 std::cout << __COUT_HDR_FL__ << "\t otsdaq_flatten_active_to_version -1 " << moveToDir <<
00324 "\n\n" << std::endl;
00325
00326
00327 return;
00328 }
00329
00330 int main(int argc, char* argv[])
00331 {
00332 FlattenActiveConfigurationGroups(argc,argv);
00333 return 0;
00334 }
00335
00336