-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathDeployment.cpp
232 lines (190 loc) · 7 KB
/
Deployment.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include "Deployment.hpp"
#include <boost/tokenizer.hpp>
#include <boost/filesystem.hpp>
#include <fstream>
#include <iostream>
#include "PkgConfigRegistry.hpp"
#include "PkgConfigHelper.hpp"
#include <lib_config/Bundle.hpp>
#include <base-logging/Logging.hpp>
using namespace orocos_cpp;
Deployment::Deployment(const std::string& name, bool load_pkg_config) : deploymentName(name), withValgrind(false)
{
if(load_pkg_config){
loadPkgConfigFile(name);
}
if(!checkExecutable(name))
throw std::runtime_error("Deployment::Error, executable for deployment '" + name + "' could not be found in PATH");
}
Deployment::Deployment(const std::string &cmp1, const std::string &as, bool load_pkg_config) : withValgrind(false)
{
//cmp1 is expected in the format "module::TaskSpec"
std::string::size_type pos = cmp1.find_first_of(":");
if(pos == std::string::npos || cmp1.at(pos +1) != ':')
{
throw std::runtime_error("given component name " + cmp1 + " is not in the format 'module::TaskSpec'");
}
std::string moduleName = cmp1.substr(0, pos);
std::string taskModelName = cmp1.substr(pos + 2, cmp1.size()) ;
std::string defaultDeploymentName = "orogen_default_" + moduleName + "__" + taskModelName;
deploymentName = defaultDeploymentName;
if(load_pkg_config){
try {
loadPkgConfigFile(deploymentName);
}
catch (const std::runtime_error &e)
{
throw std::runtime_error("Deployment::Error, could not find pkgConfig file for deployment " + deploymentName);
}
}
if(!checkExecutable(deploymentName))
throw std::runtime_error("Deployment::Error, executable for deployment " + deploymentName + " could not be found in PATH");
std::string taskName = as;
std::vector<std::string> args;
if(!taskName.empty())
{
renameTask(defaultDeploymentName, taskName);
renameTask(defaultDeploymentName + "_Logger", taskName + "_Logger");
}
}
void Deployment::regenerateNameVectors()
{
//regenerate the task list
tasks.clear();
for(std::pair<std::string, std::string> p: renameMap)
{
tasks.push_back(p.first);
}
}
bool Deployment::checkExecutable(const std::string& name)
{
if(boost::filesystem::exists(name))
return true;
const char *binPath = getenv("PATH");
if(!binPath)
{
throw std::runtime_error("Deployment::Internal Error, PATH is not set found.");
}
std::string binPathS = binPath;
boost::char_separator<char> sep(":");
boost::tokenizer<boost::char_separator<char> > paths(binPathS, sep);
for(const std::string &path: paths)
{
std::string candidate = path + "/" + name;
if(boost::filesystem::exists(candidate))
{
return true;
break;
}
}
return false;
}
bool Deployment::loadPkgConfigFile(const std::string& deploymentName)
{
PkgConfigRegistryPtr pkgreg = PkgConfigRegistry::get();
PkgConfig pkg;
if(!pkgreg->getDeployment(deploymentName, pkg))
throw std::runtime_error("PkgConfig file for deployment " + deploymentName + " was not loaded." );
//Extract required information from PkgConfig
std::string typekitsString, deployedTasksString;
if(!pkg.getVariable("typekits", typekitsString)){
throw(std::runtime_error("PkgConfig file for deployment "+deploymentName+" does not describe required typekits."));
}
std::vector<std::string> typekits = PkgConfigHelper::vectorizeTokenSeparatedString(typekitsString, " ");
if(!pkg.getVariable("deployed_tasks", deployedTasksString)){
throw(std::runtime_error("PkgConfig file for deployment "+deploymentName+" does not describe required typekits."));
}
std::vector<std::string> deployedTasks = PkgConfigHelper::vectorizeTokenSeparatedString(deployedTasksString, ",");
//Populate renameMap with deployed tasks specified in PkgConfig file
renameMap.clear();
for(const std::string &taskName: deployedTasks)
{
renameMap[taskName] = taskName;
tasks.push_back(taskName);
//Identify if task is logger...
//FIXME: Looks complicated. Does it look for the longest task name with '_Logger' suffix? WHy not simply assume '#{deploymentName}_Logger'?
std::string loggerString("_Logger");
if(taskName.length() > loggerString.length() && taskName.substr(taskName.length() - loggerString.length(), taskName.length()) == loggerString)
{
loggerName = taskName;
}
}
return true;
}
const std::string& Deployment::getName() const
{
return deploymentName;
}
const std::vector< std::string >& Deployment::getTaskNames() const
{
return tasks;
}
const std::vector< std::string >& Deployment::getNeededTypekits() const
{
return typekits;
}
void Deployment::renameTask(const std::string& orignalName, const std::string& newName, bool check_existence_in_rename_map)
{
if(check_existence_in_rename_map){
auto it = renameMap.find(orignalName);
if(it == renameMap.end())
{
throw std::out_of_range("Deployment::renameTask : Error, deployment " + deploymentName + " has no task " + orignalName);
}
renameMap.erase(it);
}
renameMap[newName] = orignalName;
//regenerate the task list
regenerateNameVectors();
}
bool Deployment::getExecString(std::string& cmd, std::vector< std::string >& args)
{
args.clear();
cmd = deploymentName;
//FIXME: Better handle Valgrind (and GDB and possibly more) at the software module that actually controls the processes. E.g. ProcessServer in cnd/orogen/execution
if(withValgrind)
{
args.push_back("--trace-children=yes");
args.push_back("--leak-check=full");
args.push_back("--track-origins=yes");
args.push_back("--log-file=" + libConfig::Bundle::getInstance().getLogDirectory() + "/" + deploymentName + "-valgrind.txt");
args.push_back(deploymentName);
cmd = "valgrind";
}
for(std::pair<std::string, std::string> p: renameMap)
{
if(p.second != p.first)
{
// std::cout << "Task " << p.second << " is renmaed to " << p.first << std::endl;
args.push_back("--rename");
args.push_back(p.second + ":" + p.first);
}
}
for(const std::string& arg : cmdLineArgs)
args.push_back(arg);
return true;
}
const std::string Deployment::getLoggerName() const
{
//Logger was possibly renamed.Thus, resolve new name of loggerName.
for(std::pair<std::string, std::string> p: renameMap)
{
if(p.second == loggerName)
return p.first;
}
//Deployment has no logger
LOG_ERROR_S << "getLoggerName() was called on a Delployment that has no logger.";
return "";
}
bool Deployment::hasLogger() const
{
return !loggerName.empty();
}
void Deployment::runWithValgrind()
{
withValgrind = true;
}
void Deployment::setCmdLineArgs(const std::vector<std::string> &args)
{
cmdLineArgs = args;
}