MCPcopy Create free account
hub / github.com/Icinga/icinga2 / ExecuteScriptHelper

Method ExecuteScriptHelper

lib/remote/consolehandler.cpp:122–201  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

120}
121
122bool ConsoleHandler::ExecuteScriptHelper(const HttpApiRequest& request, HttpApiResponse& response,
123 const String& command, const String& session, bool sandboxed)
124{
125 namespace http = boost::beast::http;
126
127 Log(LogNotice, "Console")
128 << "Executing expression: " << command;
129
130 EnsureFrameCleanupTimer();
131
132 auto lsf = GetOrCreateScriptFrame(session);
133
134 std::unique_lock frameLock(lsf->Mutex, std::try_to_lock);
135 if (!frameLock) {
136 HttpUtility::SendJsonError(response, request.Params(), 409, "Session is currently in use by another request.");
137 return true;
138 }
139
140 lsf->Seen = Utility::GetTime();
141
142 if (!lsf->Locals)
143 lsf->Locals = new Dictionary();
144
145 String fileName = "<" + Convert::ToString(lsf->NextLine) + ">";
146 lsf->NextLine++;
147
148 lsf->Lines[fileName] = command;
149
150 Dictionary::Ptr resultInfo;
151 std::unique_ptr<Expression> expr;
152 Value exprResult;
153
154 try {
155 expr = ConfigCompiler::CompileText(fileName, command);
156
157 ScriptFrame frame(true);
158 frame.Locals = lsf->Locals;
159 frame.Self = lsf->Locals;
160 frame.Sandboxed = sandboxed;
161
162 exprResult = expr->Evaluate(frame);
163
164 resultInfo = new Dictionary({
165 { "code", 200 },
166 { "status", "Executed successfully." },
167 { "result", Serialize(exprResult, 0) }
168 });
169 } catch (const ScriptError& ex) {
170 DebugInfo di = ex.GetDebugInfo();
171
172 std::ostringstream msgbuf;
173
174 msgbuf << di.Path << ": " << lsf->Lines[di.Path] << "\n"
175 << String(di.Path.GetLength() + 2, ' ')
176 << String(di.FirstColumn, ' ') << String(di.LastColumn - di.FirstColumn + 1, '^') << "\n"
177 << ex.what() << "\n";
178
179 resultInfo = new Dictionary({

Callers

nothing calls this directly

Calls 10

LogClass · 0.85
EnsureFrameCleanupTimerFunction · 0.85
GetOrCreateScriptFrameFunction · 0.85
ParamsMethod · 0.80
EvaluateMethod · 0.80
StringClass · 0.50
GetDebugInfoMethod · 0.45
GetLengthMethod · 0.45
whatMethod · 0.45

Tested by

no test coverage detected