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 {
00129 __SS__ << "filesystemdb:// was not found in $ARTDAQ_DATABASE_URI!" << std::endl;
00130 __COUT_ERR__ << "\n" << ss.str();
00131 throw std::runtime_error(ss.str());
00132 }
00133
00134 currentDir = currentDir.substr(std::string("filesystemdb://").length());
00135 while(currentDir.length() && currentDir[currentDir.length()-1] == '/')
00136 currentDir = currentDir.substr(0,currentDir.length()-1);
00137 std::string moveToDir = currentDir + "_" + std::to_string(time(0));
00138
00139 std::cout << __COUT_HDR_FL__ << "Moving current directory: \t" << currentDir << std::endl;
00140 std::cout << __COUT_HDR_FL__ << "\t... to: \t\t" << moveToDir << std::endl;
00141
00142 if(argc < 2)
00143 {
00144 __SS__ << ("Aborting move! Must at least give version argument to flatten to!") << std::endl;
00145 __COUT_ERR__ << "\n" << ss.str();
00146 throw std::runtime_error(ss.str());
00147 }
00148
00149 rename(currentDir.c_str(),moveToDir.c_str());
00150 FILE *fp = fopen((moveToDir + "/README_otsdaq_flatten.txt").c_str(),"a");
00151 if(!fp)
00152 std::cout << __COUT_HDR_FL__ << "\tError opening README file!" << std::endl;
00153 else
00154 {
00155 time_t rawtime;
00156 struct tm * timeinfo;
00157 char buffer [200];
00158
00159 time (&rawtime);
00160 timeinfo = localtime (&rawtime);
00161 strftime (buffer,200,"%b %d, %Y %I:%M%p %Z",timeinfo);
00162
00163 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",
00164 currentDir.c_str(),moveToDir.c_str(),time(0),buffer);
00165
00166 fclose(fp);
00167 }
00168
00169 if(pathToSwapIn != "")
00170 {
00171 DIR *dp;
00172 if((dp = opendir(pathToSwapIn.c_str())) == 0)
00173 {
00174 std::cout << __COUT_HDR_FL__<< "ERROR:(" << errno << "). Can't open directory: " << pathToSwapIn << std::endl;
00175 exit(0);
00176 }
00177 closedir(dp);
00178
00179 std::cout << __COUT_HDR_FL__ << "Swapping in directory: \t" << pathToSwapIn << std::endl;
00180 std::cout << __COUT_HDR_FL__ << "\t.. to: \t\t" << currentDir << std::endl;
00181
00182 rename(pathToSwapIn.c_str(),currentDir.c_str());
00183 FILE *fp = fopen((currentDir + "/README_otsdaq_flatten.txt").c_str(),"a");
00184 if(!fp)
00185 std::cout << __COUT_HDR_FL__ << "\tError opening README file!" << std::endl;
00186 else
00187 {
00188 time_t rawtime;
00189 struct tm * timeinfo;
00190 char buffer [200];
00191
00192 time (&rawtime);
00193 timeinfo = localtime (&rawtime);
00194 strftime (buffer,200,"%b %d, %Y %I:%M:%S%p %Z",timeinfo);
00195
00196 fprintf(fp,"This database was moved from...\t %s \t to...\t %s at this time \t %lu \t %s\n\n",
00197 pathToSwapIn.c_str(),currentDir.c_str(),time(0),buffer);
00198 fclose(fp);
00199 }
00200 }
00201
00202
00203
00204
00205
00206 ConfigurationInterface* theInterface_ = ConfigurationInterface::getInstance(false);
00207 ConfigurationView* cfgView;
00208 ConfigurationBase* config;
00209
00210 std::map<std::string, ConfigurationVersion> activeMap = cfgMgr->getActiveVersions();
00211
00212
00213
00214
00215
00216 const std::string groupAliasesName = "GroupAliasesConfiguration";
00217 const std::string versionAliasesName = "VersionAliasesConfiguration";
00218
00219
00220
00221 if(ConfigurationVersion(flatVersion).isInvalid() ||
00222 ConfigurationVersion(flatVersion).isTemporaryVersion())
00223 {
00224 std::cout << __COUT_HDR_FL__ << "\n\nflatVersion " << ConfigurationVersion(flatVersion) <<
00225 " is an invalid or temporary version. Skipping to end!" << std::endl;
00226 goto CLEAN_UP;
00227 }
00228
00229
00230
00231 if(activeMap.find(groupAliasesName) != activeMap.end())
00232 {
00233 std::cout << __COUT_HDR_FL__ << "\n\nModifying " << groupAliasesName << std::endl;
00234 config = cfgMgr->getConfigurationByName(groupAliasesName);
00235 cfgView = config->getViewP();
00236
00237 unsigned int col1 = cfgView->findCol("GroupName");
00238 unsigned int col2 = cfgView->findCol("GroupKey");
00239
00240
00241 for(unsigned int row = 0; row<cfgView->getNumberOfRows(); ++row )
00242 for(auto &activeGroupPair: activeGroupsMap)
00243 if(activeGroupPair.second.second.isInvalid()) continue;
00244 else if(cfgView->getDataView()[row][col1] == activeGroupPair.second.first &&
00245 cfgView->getDataView()[row][col2] == activeGroupPair.second.second.toString())
00246 {
00247
00248 std::cout << __COUT_HDR_FL__ <<
00249 "Changing row " << row << " for " <<
00250 cfgView->getDataView()[row][col1] << " key=" <<
00251 cfgView->getDataView()[row][col2] << " to DEFAULT=" <<
00252 ConfigurationGroupKey(ConfigurationGroupKey::getDefaultKey()) << std::endl;
00253 cfgView->setValue(
00254 ConfigurationGroupKey(ConfigurationGroupKey::getDefaultKey()).toString(),
00255 row,col2);
00256 break;
00257 }
00258
00259 }
00260
00261
00262 if(activeMap.find(versionAliasesName) != activeMap.end())
00263 {
00264 std::cout << __COUT_HDR_FL__ << "\n\nModifying " << versionAliasesName << std::endl;
00265 config = cfgMgr->getConfigurationByName(versionAliasesName);
00266 cfgView = config->getViewP();
00267 unsigned int col1 = cfgView->findCol("ConfigurationName");
00268 unsigned int col2 = cfgView->findCol("Version");
00269
00270
00271 for(unsigned int row = 0; row<cfgView->getNumberOfRows(); ++row )
00272 for(auto &activePair:activeMap)
00273 if(cfgView->getDataView()[row][col1] == activePair.first &&
00274 cfgView->getDataView()[row][col2] == activePair.second.toString())
00275 {
00276
00277 std::cout << __COUT_HDR_FL__ << "Changing row " << row << " for " <<
00278 cfgView->getDataView()[row][col1] << " version=" <<
00279 cfgView->getDataView()[row][col2] << " to flatVersion=" <<
00280 ConfigurationVersion(flatVersion) << std::endl;
00281 cfgView->setValue(ConfigurationVersion(flatVersion).toString(),row,col2);
00282 break;
00283 }
00284 }
00285
00286 std::cout << __COUT_HDR_FL__ << "\n\nChanging versions... " << std::endl;
00287
00288 for(auto &activePair:activeMap)
00289 {
00290 std::cout << __COUT_HDR_FL__ << activePair.first << ":v" << activePair.second << std::endl;
00291
00292 config = cfgMgr->getConfigurationByName(activePair.first);
00293 cfgView = config->getViewP();
00294 cfgView->setVersion(ConfigurationVersion(flatVersion));
00295 theInterface_->saveActiveVersion(config);
00296 }
00297
00298
00299
00300
00301 for(auto &activeGroupMembersPair: activeGroupMembersMap)
00302 {
00303 std::cout << __COUT_HDR_FL__ << "Group " << activeGroupMembersPair.first << std::endl;
00304
00305 for(auto &groupMemberPair: activeGroupMembersPair.second)
00306 {
00307 std::cout << __COUT_HDR_FL__ << "\t from...\t" <<
00308 groupMemberPair.first << ":v" << groupMemberPair.second << std::endl;
00309 groupMemberPair.second = flatVersion;
00310 }
00311
00312 for(auto &groupMemberPair: activeGroupMembersPair.second)
00313 {
00314 std::cout << __COUT_HDR_FL__ << "\t to...\t" <<
00315 groupMemberPair.first << ":v" << groupMemberPair.second << std::endl;
00316 }
00317
00318 theInterface_->saveConfigurationGroup(activeGroupMembersPair.second,
00319 ConfigurationGroupKey::getFullGroupString(
00320 activeGroupMembersPair.first,
00321 ConfigurationGroupKey(ConfigurationGroupKey::getDefaultKey())));
00322 }
00323
00324 CLEAN_UP:
00325
00326 std::cout << __COUT_HDR_FL__ << "\n\nEnd of Flattening Active Configuration Groups!\n\n\n" << std::endl;
00327
00328
00329 std::cout << __COUT_HDR_FL__ << "Run the following to return to your previous database structure:" <<
00330 std::endl;
00331 std::cout << __COUT_HDR_FL__ << "\t otsdaq_flatten_active_to_version -1 " << moveToDir <<
00332 "\n\n" << std::endl;
00333
00334
00335 return;
00336 }
00337
00338 int main(int argc, char* argv[])
00339 {
00340 FlattenActiveConfigurationGroups(argc,argv);
00341 return 0;
00342 }
00343
00344