1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00
VILLASnode/lib/api/requests/restart.cpp

143 lines
3.9 KiB
C++
Raw Permalink Normal View History

2020-08-17 17:03:54 +02:00
/** The "restart" API request.
2018-10-20 14:20:06 +02:00
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2020-01-20 17:17:00 +01:00
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
2018-10-20 14:20:06 +02:00
* @license GNU General Public License (version 3)
*
* VILLASnode
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#include <villas/super_node.hpp>
#include <villas/log.hpp>
2019-03-26 07:11:27 +01:00
#include <villas/node/exceptions.hpp>
#include <villas/utils.hpp>
2020-08-17 17:03:54 +02:00
#include <villas/api/request.hpp>
#include <villas/api/response.hpp>
#include <villas/api/session.hpp>
2018-10-20 14:20:06 +02:00
namespace villas {
namespace node {
namespace api {
2020-08-17 17:03:54 +02:00
class RestartRequest : public Request {
2018-10-20 14:20:06 +02:00
protected:
static std::string configUri;
static void handler()
{
int ret;
const char *cfg = !configUri.empty()
? configUri.c_str()
: nullptr;
const char *argv[] = { "villas-node", cfg, nullptr };
2019-01-21 10:34:45 +01:00
Logger logger = logging.get("api");
2018-10-20 14:20:06 +02:00
logger->info("Restart instance: config={}", cfg);
2019-01-21 10:34:22 +01:00
2018-10-20 14:20:06 +02:00
ret = execvp("/proc/self/exe", (char **) argv);
if (ret)
throw SystemError("Failed to restart");
2018-10-20 14:20:06 +02:00
}
public:
2020-08-17 17:03:54 +02:00
using Request::Request;
2019-01-21 10:34:22 +01:00
2020-08-17 17:03:54 +02:00
virtual Response * execute()
2018-10-20 14:20:06 +02:00
{
int ret;
json_error_t err;
if (method != Session::Method::POST)
2020-08-17 17:03:54 +02:00
throw InvalidMethod(this);
json_t *json_config = nullptr;
2018-10-20 14:20:06 +02:00
2020-08-17 17:03:54 +02:00
if (body) {
ret = json_unpack_ex(body, &err, 0, "{ s?: o }",
"config", &json_config
);
if (ret < 0)
2020-09-30 16:11:13 +02:00
throw BadRequest("Failed to parse request body");
2018-10-20 14:20:06 +02:00
}
if (json_config) {
if (json_is_string(json_config))
configUri = json_string_value(json_config);
else if (json_is_object(json_config)) {
char configUriBuf[] = "villas-node.json.XXXXXX";
int configFd = mkstemp(configUriBuf);
FILE *configFile = fdopen(configFd, "w+");
ret = json_dumpf(json_config, configFile, JSON_INDENT(4));
if (ret < 0)
throw Error(HTTP_STATUS_INTERNAL_SERVER_ERROR, "Failed to create temporary config file");
fclose(configFile);
configUri = configUriBuf;
}
2020-10-16 11:43:24 +02:00
else if (json_config != nullptr)
throw BadRequest("Parameter 'config' must be either a URL (string) or a configuration (object)");
}
else /* If no config is provided via request, we will use the previous one */
configUri = session->getSuperNode()->getConfigUri();
2018-10-20 14:20:06 +02:00
2020-08-25 20:22:19 +02:00
logger->info("Restarting to {}", configUri.c_str());
2018-10-20 14:20:06 +02:00
/* Increment API restart counter */
char *scnt = getenv("VILLAS_API_RESTART_COUNT");
int cnt = scnt ? atoi(scnt) : 0;
char buf[32];
snprintf(buf, sizeof(buf), "%d", cnt + 1);
/* We pass some env variables to the new process */
setenv("VILLAS_API_RESTART_COUNT", buf, 1);
2020-08-17 17:03:54 +02:00
auto *json_response = json_pack("{ s: i, s: o }",
2018-10-20 14:20:06 +02:00
"restarts", cnt,
"config", configUri.empty()
? json_null()
: json_string(configUri.c_str())
);
/* Register exit handler */
ret = atexit(handler);
if (ret)
2020-08-17 17:03:54 +02:00
throw Error(HTTP_STATUS_INTERNAL_SERVER_ERROR, "Failed to restart VILLASnode instance");
2018-10-20 14:20:06 +02:00
/* Properly terminate current instance */
2019-06-04 16:55:38 +02:00
utils::killme(SIGTERM);
2018-10-20 14:20:06 +02:00
return new JsonResponse(session, HTTP_STATUS_OK, json_response);
2018-10-20 14:20:06 +02:00
}
};
2020-08-17 17:03:54 +02:00
std::string RestartRequest::configUri;
2018-10-20 14:20:06 +02:00
2020-08-17 17:03:54 +02:00
/* Register API request */
static char n[] = "restart";
2020-08-17 17:03:54 +02:00
static char r[] = "/restart";
static char d[] = "restart VILLASnode with new configuration";
2020-08-17 17:03:54 +02:00
static RequestPlugin<RestartRequest, n, r, d> p;
2018-10-20 14:20:06 +02:00
} /* namespace api */
} /* namespace node */
} /* namespace villas */
2018-10-20 14:20:06 +02:00