aboutsummaryrefslogtreecommitdiffstats
path: root/configd
diff options
context:
space:
mode:
authorArne Juul <arnej@yahoo-inc.com>2019-03-05 12:13:14 +0000
committerArne Juul <arnej@yahoo-inc.com>2019-03-05 15:02:49 +0000
commit811173b3bca4863d28d56d02dc99aa8a8e83a5ed (patch)
tree6e4648b154348a418b55029eea9df2ea354350cd /configd
parent88ddf6bab8dc094e9099ffb41c4b4b568a716c1d (diff)
add skeleton for sentinel-cmd
Diffstat (limited to 'configd')
-rw-r--r--configd/CMakeLists.txt1
-rw-r--r--configd/src/apps/cmd/.gitignore1
-rw-r--r--configd/src/apps/cmd/CMakeLists.txt10
-rw-r--r--configd/src/apps/cmd/main.cpp140
4 files changed, 152 insertions, 0 deletions
diff --git a/configd/CMakeLists.txt b/configd/CMakeLists.txt
index d3926b59759..a83ecd3425e 100644
--- a/configd/CMakeLists.txt
+++ b/configd/CMakeLists.txt
@@ -2,6 +2,7 @@
vespa_define_module(
APPS
src/apps/sentinel
+ src/apps/cmd
src/apps/su
TESTS
diff --git a/configd/src/apps/cmd/.gitignore b/configd/src/apps/cmd/.gitignore
new file mode 100644
index 00000000000..c79a9a7a643
--- /dev/null
+++ b/configd/src/apps/cmd/.gitignore
@@ -0,0 +1 @@
+/vespa-sentinel-cmd-bin
diff --git a/configd/src/apps/cmd/CMakeLists.txt b/configd/src/apps/cmd/CMakeLists.txt
new file mode 100644
index 00000000000..c05fd1de37f
--- /dev/null
+++ b/configd/src/apps/cmd/CMakeLists.txt
@@ -0,0 +1,10 @@
+# Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+vespa_add_executable(configd_vespa-sentinel-cmd_app
+ SOURCES
+ main.cpp
+ OUTPUT_NAME vespa-sentinel-cmd-bin
+ INSTALL bin
+ DEPENDS
+ fnet
+ vespalib
+)
diff --git a/configd/src/apps/cmd/main.cpp b/configd/src/apps/cmd/main.cpp
new file mode 100644
index 00000000000..f2d6e161727
--- /dev/null
+++ b/configd/src/apps/cmd/main.cpp
@@ -0,0 +1,140 @@
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#include <vespa/vespalib/util/signalhandler.h>
+#include <vespa/fnet/frt/supervisor.h>
+#include <vespa/fnet/frt/target.h>
+#include <vespa/fnet/frt/rpcrequest.h>
+
+#include <vespa/log/log.h>
+LOG_SETUP("vespa-sentinel-cmd");
+
+class Cmd
+{
+private:
+ std::unique_ptr<FRT_Supervisor> _supervisor;
+ FRT_Target *_target;
+
+public:
+ Cmd() : _supervisor(), _target(nullptr) {}
+ ~Cmd();
+ int run(const char *cmd, const char *arg);
+ void initRPC(const char *spec);
+ void finiRPC();
+};
+
+Cmd::~Cmd()
+{
+ LOG_ASSERT(! _supervisor);
+ LOG_ASSERT(_target == nullptr);
+}
+
+void usage()
+{
+ fprintf(stderr, "usage: vespa-sentinel-cmd <cmd> [arg]\n");
+ fprintf(stderr, "with cmd one of:\n");
+ fprintf(stderr, " list\n");
+ fprintf(stderr, " start {service}\n");
+ fprintf(stderr, " stop {service}\n");
+}
+
+void
+Cmd::initRPC(const char *spec)
+{
+ _supervisor = std::make_unique<FRT_Supervisor>();
+ _target = _supervisor->GetTarget(spec);
+ _supervisor->Start();
+}
+
+
+void
+Cmd::finiRPC()
+{
+ if (_target != nullptr) {
+ _target->SubRef();
+ _target = nullptr;
+ }
+ if (_supervisor) {
+ _supervisor->ShutDown(true);
+ _supervisor.reset();
+ }
+}
+
+
+int
+Cmd::run(const char *cmd, const char *arg)
+{
+ int retval = 0;
+ initRPC("tcp/localhost:19097");
+
+ FRT_RPCRequest *req = _supervisor->AllocRPCRequest();
+ req->SetMethodName(cmd);
+
+ if (arg) {
+ // one param
+ req->GetParams()->AddString(arg);
+ }
+ _target->InvokeSync(req, 5.0);
+
+ if (req->IsError()) {
+ fprintf(stderr, "vespa-sentinel-cmd '%s' error %d: %s\n",
+ cmd, req->GetErrorCode(), req->GetErrorMessage());
+ retval = 1;
+ } else {
+ FRT_Values &answer = *(req->GetReturn());
+ const char *atypes = answer.GetTypeString();
+ fprintf(stderr, "vespa-sentinel-cmd '%s' OK.\n", cmd);
+ uint32_t idx = 0;
+ while (atypes != nullptr && *atypes != '\0') {
+ switch (*atypes) {
+ case 's':
+ printf("%s\n", answer[idx]._string._str);
+ break;
+ default:
+ printf("BAD: unknown type %c\n", *atypes);
+ }
+ ++atypes;
+ ++idx;
+ }
+ }
+ req->SubRef();
+ finiRPC();
+ return retval;
+}
+
+const char *
+parseCmd(const char *arg)
+{
+ if (strcmp(arg, "list") == 0) {
+ return "sentinel.ls";
+ } else if (strcmp(arg, "start") == 0) {
+ return "sentinel.service.start";
+ } else if (strcmp(arg, "stop") == 0) {
+ return "sentinel.service.stop";
+ }
+ return 0;
+}
+
+void hookSignals() {
+ using SIG = vespalib::SignalHandler;
+ SIG::PIPE.ignore();
+}
+
+int main(int argc, char** argv)
+{
+ int retval = 1;
+ const char *cmd = 0;
+ if (argc > 1) {
+ cmd = parseCmd(argv[1]);
+ }
+ if (cmd) {
+ hookSignals();
+ Cmd runner;
+ retval = runner.run(cmd, argc > 2 ? argv[2] : 0);
+ } else {
+ usage();
+ }
+ return retval;
+}