This repository was archived by the owner on Jan 31, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathServiceDeployManager.cpp
More file actions
849 lines (692 loc) · 35.1 KB
/
Copy pathServiceDeployManager.cpp
File metadata and controls
849 lines (692 loc) · 35.1 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
// Copyright (c) 2020 The Orbit Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "SessionSetup/ServiceDeployManager.h"
#include <absl/flags/flag.h>
#include <absl/strings/match.h>
#include <absl/strings/str_format.h>
#include <absl/strings/str_split.h>
#include <QApplication>
#include <QEventLoop>
#include <QMetaObject>
#include <QNonConstOverload>
#include <QPointer>
#include <QThread>
#include <Qt>
#include <algorithm>
#include <chrono>
#include <cstddef>
#include <filesystem>
#include <string>
#include <string_view>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <utility>
#include <variant>
#include <vector>
#include "ClientFlags/ClientFlags.h"
#include "OrbitBase/CanceledOr.h"
#include "OrbitBase/Future.h"
#include "OrbitBase/Logging.h"
#include "OrbitBase/Promise.h"
#include "OrbitBase/Result.h"
#include "OrbitBase/StopToken.h"
#include "OrbitSsh/AddrAndPort.h"
#include "OrbitSshQt/ScopedConnection.h"
#include "OrbitSshQt/Session.h"
#include "OrbitSshQt/SftpChannel.h"
#include "OrbitSshQt/SftpCopyToLocalOperation.h"
#include "OrbitSshQt/SftpCopyToRemoteOperation.h"
#include "OrbitSshQt/Task.h"
#include "QtUtils/EventLoop.h"
#include "SessionSetup/DeploymentConfigurations.h"
#include "SessionSetup/Error.h"
static const std::string kLocalhost = "127.0.0.1";
static const std::string kDebDestinationPath = "/tmp/orbitprofiler.deb";
static const std::string kSigDestinationPath = "/tmp/orbitprofiler.deb.asc";
constexpr std::string_view kSshWatchdogPassphrase = "start_watchdog";
constexpr std::chrono::milliseconds kSshWatchdogInterval{1000};
constexpr std::chrono::seconds kServiceStartupTimeout{10};
using orbit_base::CanceledOr;
namespace orbit_session_setup {
namespace {
template <typename Func>
[[nodiscard]] orbit_ssh_qt::ScopedConnection ConnectQuitHandler(
orbit_qt_utils::EventLoop* loop,
const typename QtPrivate::FunctionPointer<Func>::Object* sender, Func signal) {
return orbit_ssh_qt::ScopedConnection{
QObject::connect(sender, signal, loop, &orbit_qt_utils::EventLoop::quit)};
}
template <typename Func>
[[nodiscard]] orbit_ssh_qt::ScopedConnection ConnectErrorHandler(
orbit_qt_utils::EventLoop* loop,
const typename QtPrivate::FunctionPointer<Func>::Object* sender, Func signal) {
return orbit_ssh_qt::ScopedConnection{QObject::connect(
sender, signal, loop, qOverload<std::error_code>(&orbit_qt_utils::EventLoop::error))};
}
[[nodiscard]] orbit_ssh_qt::ScopedConnection ConnectCancelHandler(orbit_qt_utils::EventLoop* loop,
ServiceDeployManager* sdm) {
return orbit_ssh_qt::ScopedConnection{QObject::connect(
sdm, &ServiceDeployManager::cancelRequested, loop,
[loop]() { loop->error(make_error_code(Error::kUserCanceledServiceDeployment)); })};
}
void PrintAsOrbitService(std::string_view buffer) {
std::vector<std::string_view> lines = absl::StrSplit(buffer, '\n');
for (const auto& line : lines) {
if (!line.empty()) {
ORBIT_INTERNAL_PLATFORM_LOG(
absl::StrFormat("[ OrbitService] %s\n", line).c_str());
}
}
};
// This function makes it easy to execute a function object on a different thread in a synchronous
// way.
//
// While waiting for the function to finish executing on a different thread a Qt event loop
// processes other (UI-) events. The thread is determined by the associated thread of the QObject
// context.
template <typename Func>
void DeferToBackgroundThreadAndWait(QObject* context, Func&& func) {
QEventLoop waiting_loop; // This event loop processes main thread events while we wait for the
// background thread to finish executing func();
QMetaObject::invokeMethod(context,
[func = std::forward<Func>(func),
waiting_loop = QPointer<QEventLoop>{&waiting_loop}]() mutable {
func();
if (waiting_loop)
QMetaObject::invokeMethod(waiting_loop, &QEventLoop::quit);
});
waiting_loop.exec();
}
} // namespace
template <typename T>
static ErrorMessageOr<T> MapError(ErrorMessageOr<T> result, Error new_error) {
if (result.has_value()) {
return result;
}
const auto new_error_code = make_error_code(new_error);
ORBIT_ERROR("%s: %s", new_error_code.message(), result.error().message());
return outcome::failure(new_error_code);
}
template <typename T>
static ErrorMessageOr<T> MapError(outcome::result<T> result, Error new_error) {
if (result.has_value()) {
return result.value();
}
const auto new_error_code = make_error_code(new_error);
return ErrorMessage{
absl::StrFormat("%s: %s", new_error_code.message(), result.error().message())};
}
ServiceDeployManager::ServiceDeployManager(const DeploymentConfiguration* deployment_configuration,
const orbit_ssh::Context* context,
orbit_ssh::Credentials credentials,
const ServiceDeployManager::GrpcPort& grpc_port,
QObject* parent)
: QObject(parent),
deployment_configuration_(deployment_configuration),
context_(context),
credentials_(std::move(credentials)),
grpc_port_(grpc_port),
ssh_watchdog_timer_(this) {
ORBIT_CHECK(deployment_configuration != nullptr);
ORBIT_CHECK(context != nullptr);
moveToThread(background_executor_.GetThread());
QObject::connect(
this, &ServiceDeployManager::statusMessage, this, [](const QString& status_message) {
ORBIT_LOG("ServiceDeployManager status message: \"%s\"", status_message.toStdString());
});
}
ServiceDeployManager::~ServiceDeployManager() { Shutdown(); }
void ServiceDeployManager::Cancel() {
// By transforming this function call into a signal we leverage Qt's automatic thread
// synchronization and don't have to bother from what thread Cancel was called.
emit cancelRequested();
}
ErrorMessageOr<bool> ServiceDeployManager::CheckIfInstalled() {
ORBIT_CHECK(QThread::currentThread() == thread());
emit statusMessage(QString("Checking if OrbitService is already installed in version %1 on the "
"remote instance.")
.arg(QApplication::applicationVersion()));
auto version = QApplication::applicationVersion().toStdString();
if (!version.empty() && version.front() == 'v') {
// The old git tags have a 'v' in front which is not supported by debian
// packages. So we have to remove it.
version = version.substr(1);
}
const auto command = absl::StrFormat(
"/usr/bin/dpkg-query -W -f '${Version}' orbitprofiler | grep -xF '%s' && cd / && md5sum -c "
"/var/lib/dpkg/info/orbitprofiler.md5sums",
version);
orbit_ssh_qt::Task check_if_installed_task{&session_.value(), command};
orbit_qt_utils::EventLoop loop{};
QObject::connect(&check_if_installed_task, &orbit_ssh_qt::Task::readyReadStdOut, this,
[&check_if_installed_task]() {
ORBIT_LOG("CheckIfInstalled stdout: %s", check_if_installed_task.ReadStdOut());
});
QObject::connect(&check_if_installed_task, &orbit_ssh_qt::Task::readyReadStdErr, this,
[&check_if_installed_task]() {
ORBIT_LOG("CheckIfInstalled stderr: %s", check_if_installed_task.ReadStdErr());
});
QObject::connect(&check_if_installed_task, &orbit_ssh_qt::Task::finished, &loop,
&orbit_qt_utils::EventLoop::exit);
auto error_handler =
ConnectErrorHandler(&loop, &check_if_installed_task, &orbit_ssh_qt::Task::errorOccurred);
auto cancel_handler = ConnectCancelHandler(&loop, this);
std::ignore = check_if_installed_task.Start();
OUTCOME_TRY(auto&& result, loop.exec());
ORBIT_LOG("CheckIfInstalled task returned exit code: %d", result);
if (result == 0) {
// Already installed
emit statusMessage("The correct version of OrbitService is already installed.");
return outcome::success(true);
}
emit statusMessage("The correct version of OrbitService is not yet installed.");
return outcome::success(false);
}
ErrorMessageOr<uint16_t> ServiceDeployManager::StartTunnel(
std::optional<orbit_ssh_qt::Tunnel>* tunnel, uint16_t port) {
ORBIT_CHECK(QThread::currentThread() == thread());
emit statusMessage("Setting up port forwarding...");
ORBIT_LOG("Setting up tunnel on port %d", port);
tunnel->emplace(&session_.value(), kLocalhost, port, this);
orbit_qt_utils::EventLoop loop{};
auto error_handler =
ConnectErrorHandler(&loop, &tunnel->value(), &orbit_ssh_qt::Tunnel::errorOccurred);
auto quit_handler = ConnectQuitHandler(&loop, &tunnel->value(), &orbit_ssh_qt::Tunnel::started);
auto cancel_handler = ConnectCancelHandler(&loop, this);
std::ignore = tunnel->value().Start();
OUTCOME_TRY(MapError(loop.exec(), Error::kCouldNotStartTunnel));
QObject::connect(&tunnel->value(), &orbit_ssh_qt::Tunnel::errorOccurred, this,
&ServiceDeployManager::handleSocketError);
return outcome::success(tunnel->value().GetListenPort());
}
ErrorMessageOr<std::unique_ptr<orbit_ssh_qt::SftpChannel>>
ServiceDeployManager::StartSftpChannel() {
ORBIT_CHECK(QThread::currentThread() == thread());
auto sftp_channel = std::make_unique<orbit_ssh_qt::SftpChannel>(&session_.value());
orbit_qt_utils::EventLoop loop{};
auto quit_handler =
ConnectQuitHandler(&loop, sftp_channel.get(), &orbit_ssh_qt::SftpChannel::started);
auto error_handler =
ConnectErrorHandler(&loop, sftp_channel.get(), &orbit_ssh_qt::SftpChannel::errorOccurred);
auto cancel_handler = ConnectCancelHandler(&loop, this);
sftp_channel->Start();
OUTCOME_TRY(loop.exec());
return sftp_channel;
}
ErrorMessageOr<void> ServiceDeployManager::CopyFileToRemote(
std::string_view source, std::string_view dest,
orbit_ssh_qt::SftpCopyToRemoteOperation::FileMode dest_mode) {
ORBIT_CHECK(QThread::currentThread() == thread());
orbit_ssh_qt::SftpCopyToRemoteOperation operation{&session_.value(), sftp_channel_.get()};
orbit_qt_utils::EventLoop loop{};
auto quit_handler =
ConnectQuitHandler(&loop, &operation, &orbit_ssh_qt::SftpCopyToRemoteOperation::stopped);
auto error_handler = ConnectErrorHandler(&loop, &operation,
&orbit_ssh_qt::SftpCopyToRemoteOperation::errorOccurred);
auto cancel_handler = ConnectCancelHandler(&loop, this);
ORBIT_LOG("About to start copying from %s to %s...", source, dest);
operation.CopyFileToRemote(source, dest, dest_mode);
OUTCOME_TRY(loop.exec());
return outcome::success();
}
ErrorMessageOr<void> ServiceDeployManager::ShutdownSftpOperations() {
ORBIT_SCOPED_TIMED_LOG("ServiceDeployManager::ShutdownSftpOperations");
ORBIT_CHECK(QThread::currentThread() == thread());
ORBIT_CHECK(copy_to_local_operation_ != nullptr);
waiting_copy_operations_.clear();
orbit_qt_utils::EventLoop loop{};
auto quit_handler = ConnectQuitHandler(&loop, copy_to_local_operation_,
&orbit_ssh_qt::SftpCopyToLocalOperation::stopped);
auto error_handler = ConnectErrorHandler(&loop, copy_to_local_operation_,
&orbit_ssh_qt::SftpCopyToLocalOperation::errorOccurred);
auto cancel_handler = ConnectCancelHandler(&loop, this);
copy_to_local_operation_->Stop();
OUTCOME_TRY(loop.exec());
delete copy_to_local_operation_;
copy_to_local_operation_ = nullptr;
return outcome::success();
}
ErrorMessageOr<void> ServiceDeployManager::ShutdownSftpChannel(
orbit_ssh_qt::SftpChannel* sftp_channel) {
ORBIT_SCOPED_TIMED_LOG("ServiceDeployManager::ShutdownSftpChannel");
ORBIT_CHECK(QThread::currentThread() == thread());
ORBIT_CHECK(sftp_channel != nullptr);
orbit_qt_utils::EventLoop loop{};
auto quit_handler = ConnectQuitHandler(&loop, sftp_channel, &orbit_ssh_qt::SftpChannel::stopped);
auto error_handler =
ConnectErrorHandler(&loop, sftp_channel, &orbit_ssh_qt::SftpChannel::errorOccurred);
auto cancel_handler = ConnectCancelHandler(&loop, this);
sftp_channel->Stop();
OUTCOME_TRY(loop.exec());
return outcome::success();
}
ErrorMessageOr<void> ServiceDeployManager::CopyOrbitServicePackage() {
ORBIT_CHECK(QThread::currentThread() == thread());
emit statusMessage("Copying OrbitService package to the remote instance...");
const auto& config = std::get<SignedDebianPackageDeployment>(*deployment_configuration_);
using FileMode = orbit_ssh_qt::SftpCopyToRemoteOperation::FileMode;
OUTCOME_TRY(MapError(CopyFileToRemote(config.path_to_package.string(), kDebDestinationPath,
FileMode::kUserWritable),
Error::kCouldNotUploadPackage));
OUTCOME_TRY(MapError(CopyFileToRemote(config.path_to_signature.string(), kSigDestinationPath,
FileMode::kUserWritable),
Error::kCouldNotUploadSignature));
emit statusMessage("Finished copying the OrbitService package to the remote instance.");
return outcome::success();
}
orbit_base::Future<ErrorMessageOr<CanceledOr<void>>> ServiceDeployManager::CopyFileToLocal(
std::filesystem::path source, std::filesystem::path destination,
orbit_base::StopToken stop_token) {
orbit_base::Promise<ErrorMessageOr<CanceledOr<void>>> promise;
auto future = promise.GetFuture();
// This schedules the call of `CopyFileToLocalImpl` on the background thread.
QMetaObject::invokeMethod(
this, [this, source = std::move(source), destination = std::move(destination),
promise = std::move(promise), stop_token = std::move(stop_token)]() mutable {
CopyFileToLocalImpl(std::move(promise), std::move(source), std::move(destination),
std::move(stop_token));
});
return future;
}
void ServiceDeployManager::CopyFileToLocalImpl(
orbit_base::Promise<ErrorMessageOr<CanceledOr<void>>> promise, std::filesystem::path source,
std::filesystem::path destination, orbit_base::StopToken stop_token) {
ORBIT_CHECK(QThread::currentThread() == thread());
if (copy_to_local_operation_ != nullptr) {
waiting_copy_operations_.emplace_back(
[this, promise = std::move(promise), source = std::move(source),
destination = std::move(destination), stop_token = std::move(stop_token)]() mutable {
CopyFileToLocalImpl(std::move(promise), source, destination, std::move(stop_token));
});
return;
}
ORBIT_LOG("Copying remote \"%s\" to local \"%s\"", source.string(), destination.string());
// NOLINTNEXTLINE - Unfortunately we have to fall back to a raw `new` here.
copy_to_local_operation_ = new orbit_ssh_qt::SftpCopyToLocalOperation{
&session_.value(), sftp_channel_.get(), stop_token};
// copy_to_local_operation_ will get deleted either in finish_handler (via delete later) or in
// ShutdownSftpOperations().
// The finish handler handles both the error and the success case and will be triggered
// from the ::stopped and ::errorOccured signals (see below).
// By having a single handler we don't need to worry about sharing resources that are not supposed
// to be shared like the promise.
auto finish_handler = [this, promise = std::move(promise), source, destination,
stop_token = std::move(stop_token)](ErrorMessageOr<void> result) mutable {
if (promise.HasResult()) return;
// We can't just call `delete copy_to_local_operation_;` here because that also triggers the
// deletion of this closure object. Instead we queue a job on the event queue for deleting it
// later.
copy_to_local_operation_->deleteLater();
copy_to_local_operation_ = nullptr;
if (!waiting_copy_operations_.empty()) {
// This calls the copy operation from the event loop in the background thread
QMetaObject::invokeMethod(this, std::move(waiting_copy_operations_.front()),
Qt::QueuedConnection);
waiting_copy_operations_.pop_front();
}
if (stop_token.IsStopRequested()) {
promise.SetResult(orbit_base::Canceled{});
return;
}
if (result.has_error()) {
promise.SetResult(
ErrorMessage{absl::StrFormat(R"(Error copying remote "%s" to "%s": %s)", source.string(),
destination.string(), result.error().message())});
return;
}
promise.SetResult(outcome::success());
};
// Since we need to call the finish handler from two different slots and it's not copyable,
// we first have to move the handler into a shared_ptr which we can share between the two slots.
// This will also take care of lifetime management since the finish handler closure will get
// deleted when the `copy_to_local_operation_` object gets deleted.
auto shared_finish_handler =
std::make_shared<decltype(finish_handler)>(std::move(finish_handler));
QObject::connect(copy_to_local_operation_, &orbit_ssh_qt::SftpCopyToLocalOperation::stopped,
[shared_finish_handler]() { (*shared_finish_handler)(outcome::success()); });
QObject::connect(copy_to_local_operation_, &orbit_ssh_qt::SftpCopyToLocalOperation::errorOccurred,
[shared_finish_handler](std::error_code error_code) {
(*shared_finish_handler)(ErrorMessage{error_code.message()});
});
copy_to_local_operation_->CopyFileToLocal(std::move(source), std::move(destination));
}
ErrorMessageOr<void> ServiceDeployManager::CopyOrbitServiceExecutable(
const BareExecutableAndRootPasswordDeployment& config) {
ORBIT_CHECK(QThread::currentThread() == thread());
emit statusMessage("Copying OrbitService executable to the remote instance...");
const std::string exe_destination_path = "/tmp/OrbitService";
OUTCOME_TRY(CopyFileToRemote(
config.path_to_executable.string(), exe_destination_path,
orbit_ssh_qt::SftpCopyToRemoteOperation::FileMode::kUserWritableAllExecutable));
emit statusMessage("Finished copying the OrbitService executable to the remote instance.");
return outcome::success();
}
ErrorMessageOr<void> ServiceDeployManager::CopyOrbitApiLibrary(
const BareExecutableAndRootPasswordDeployment& config) {
ORBIT_CHECK(QThread::currentThread() == thread());
emit statusMessage("Copying liborbit.so to the remote instance...");
const std::string library_destination_path = "/tmp/liborbit.so";
const auto library_source_path = config.path_to_executable.parent_path() / "../lib/liborbit.so";
OUTCOME_TRY(CopyFileToRemote(
library_source_path.string(), library_destination_path,
orbit_ssh_qt::SftpCopyToRemoteOperation::FileMode::kUserWritableAllExecutable));
emit statusMessage("Finished copying liborbit.so to the remote instance.");
return outcome::success();
}
ErrorMessageOr<void> ServiceDeployManager::CopyOrbitUserSpaceInstrumentationLibrary(
const BareExecutableAndRootPasswordDeployment& config) {
ORBIT_CHECK(QThread::currentThread() == thread());
emit statusMessage("Copying liborbituserspaceinstrumentation.so to the remote instance...");
const std::string library_destination_path = "/tmp/liborbituserspaceinstrumentation.so";
const auto library_source_path =
config.path_to_executable.parent_path() / "../lib/liborbituserspaceinstrumentation.so";
OUTCOME_TRY(CopyFileToRemote(
library_source_path.string(), library_destination_path,
orbit_ssh_qt::SftpCopyToRemoteOperation::FileMode::kUserWritableAllExecutable));
emit statusMessage(
"Finished copying liborbituserspaceinstrumentation.so to the remote instance.");
return outcome::success();
}
[[nodiscard]] static std::string GenerateStartOrbitServiceCommand(
const std::variant<SignedDebianPackageDeployment, BareExecutableAndRootPasswordDeployment>&
deployment_config) {
std::string command;
if (std::holds_alternative<SignedDebianPackageDeployment>(deployment_config)) {
command = "/opt/developer/tools/OrbitService";
} else {
command = "sudo --stdin /tmp/OrbitService";
}
if (absl::GetFlag(FLAGS_devmode)) {
command += " --devmode";
}
return command;
}
ErrorMessageOr<void> ServiceDeployManager::StartOrbitService(
const std::variant<SignedDebianPackageDeployment, BareExecutableAndRootPasswordDeployment>&
deployment_config) {
ORBIT_CHECK(QThread::currentThread() == thread());
emit statusMessage("Starting OrbitService on the remote instance...");
std::string task_string = GenerateStartOrbitServiceCommand(deployment_config);
orbit_service_task_.emplace(&session_.value(), task_string);
if (std::holds_alternative<BareExecutableAndRootPasswordDeployment>(deployment_config)) {
const auto& config = std::get<BareExecutableAndRootPasswordDeployment>(deployment_config);
std::ignore = orbit_service_task_->Write(absl::StrFormat("%s\n", config.root_password));
// TODO(antonrohr) Check whether the password was incorrect.
// There are multiple ways of doing this. the best way is probably to have a
// second task running before OrbitService that sets the SUID bit. It might be
// necessary to close stdin by sending EOF, since sudo would ask for trying to
// enter the password again. Another option is to use std err as soon as its
// implemented in OrbitSshQt::Task.
}
orbit_qt_utils::EventLoop loop{};
auto error_handler =
ConnectErrorHandler(&loop, &orbit_service_task_.value(), &orbit_ssh_qt::Task::errorOccurred);
auto cancel_handler = ConnectCancelHandler(&loop, this);
std::string stdout_buffer;
QObject::connect(&orbit_service_task_.value(), &orbit_ssh_qt::Task::finished, &loop,
[&loop, &stdout_buffer](int exit_code) {
constexpr int kExitCodeIndicatingErrorMessage = 42;
if (exit_code == kExitCodeIndicatingErrorMessage) {
// We convert to QString here because there could be UTF-8 multibyte
// codepoints in the stdout_buffer which makes limiting to a certain number
// of characters non-trivial.
auto error_message = QString::fromStdString(stdout_buffer).trimmed();
constexpr int kMaximumErrorMessageLength = 1000;
if (error_message.size() > kMaximumErrorMessageLength) {
error_message =
error_message.left(kMaximumErrorMessageLength - 3).append("...");
}
loop.error(ErrorMessage{error_message.toStdString()});
return;
}
loop.error(ErrorMessage{absl::StrFormat(
"The service exited prematurely with exit code %d.", exit_code)});
});
QObject::connect(&orbit_service_task_.value(), &orbit_ssh_qt::Task::readyReadStdOut, &loop,
[&]() {
// We are looking for the kReadyKeyword. Since it might be split up into
// consecutive chunks in the stdout stream we reassemble to whole string into a
// buffer and check that for the keyword.
stdout_buffer.append(orbit_service_task_->ReadStdOut());
// That's what we expect the service to send through stdout when it's ready to
// accept a connection from the client.
constexpr std::string_view kReadyKeyword = "READY";
if (absl::StrContains(stdout_buffer, kReadyKeyword)) {
ORBIT_LOG("The service reported to be ready to accept connections.");
loop.quit();
return;
}
// This is protecting us against consuming unreasonable amount of memory when
// for whatever reason there is a lot of data coming through the stdout
// channel.
constexpr size_t kMaxBufferSize = 100ul * 1024; // 100 KiB
if (stdout_buffer.size() > kMaxBufferSize) {
const auto number_of_bytes_to_remove =
static_cast<ptrdiff_t>(stdout_buffer.size() - kMaxBufferSize);
stdout_buffer.erase(stdout_buffer.begin(),
stdout_buffer.begin() + number_of_bytes_to_remove);
}
});
QObject::connect(&orbit_service_task_.value(), &orbit_ssh_qt::Task::readyReadStdErr, this,
[this]() { PrintAsOrbitService(orbit_service_task_->ReadStdErr()); });
QTimer::singleShot(kServiceStartupTimeout, &loop, [&]() {
// OrbitService took too long to start. That's an indication that something is wrong.
std::string error_message = absl::StrFormat(
"The service took more than %d seconds to start up.", kServiceStartupTimeout.count());
if (std::holds_alternative<BareExecutableAndRootPasswordDeployment>(deployment_config)) {
error_message.append(" (An outdated version of OrbitService could have caused this.)");
}
loop.error(ErrorMessage{std::move(error_message)});
});
std::ignore = orbit_service_task_->Start();
OUTCOME_TRY(loop.exec());
QObject::connect(&orbit_service_task_.value(), &orbit_ssh_qt::Task::readyReadStdOut, this,
[this]() { PrintAsOrbitService(orbit_service_task_->ReadStdOut()); });
QObject::connect(&orbit_service_task_.value(), &orbit_ssh_qt::Task::errorOccurred, this,
&ServiceDeployManager::handleSocketError);
QObject::connect(&orbit_service_task_.value(), &orbit_ssh_qt::Task::finished, this,
[](int exit_code) {
ORBIT_LOG("The OrbitService Task finished with exit code: %d", exit_code);
});
return outcome::success();
}
ErrorMessageOr<void> ServiceDeployManager::InstallOrbitServicePackage() {
ORBIT_CHECK(QThread::currentThread() == thread());
emit statusMessage("Installing the OrbitService package on the remote instance...");
const auto command = absl::StrFormat(
"sudo /usr/local/cloudcast/sbin/install_signed_package.sh %s", kDebDestinationPath);
orbit_ssh_qt::Task install_service_task{&session_.value(), command};
orbit_qt_utils::EventLoop loop{};
QObject::connect(&install_service_task, &orbit_ssh_qt::Task::finished, this, [&](int exit_code) {
if (exit_code == 0) {
loop.quit();
} else {
// TODO(antonrohr) use stderr message once its implemented in
// OrbitSshQt::Task
ORBIT_ERROR("Unable to install install OrbitService package, exit code: %d", exit_code);
loop.error(make_error_code(Error::kCouldNotInstallPackage));
}
});
auto error_handler =
ConnectErrorHandler(&loop, &install_service_task, &orbit_ssh_qt::Task::errorOccurred);
auto cancel_handler = ConnectCancelHandler(&loop, this);
std::ignore = install_service_task.Start();
OUTCOME_TRY(loop.exec());
return outcome::success();
}
ErrorMessageOr<void> ServiceDeployManager::ConnectToServer() {
ORBIT_CHECK(QThread::currentThread() == thread());
emit statusMessage(QString("Connecting to %1:%2...")
.arg(QString::fromStdString(credentials_.addr_and_port.addr))
.arg(credentials_.addr_and_port.port));
session_.emplace(context_, this);
using orbit_ssh_qt::Session;
orbit_qt_utils::EventLoop loop{};
auto quit_handler = ConnectQuitHandler(&loop, &session_.value(), &Session::started);
auto error_handler = ConnectErrorHandler(&loop, &session_.value(), &Session::errorOccurred);
auto cancel_handler = ConnectCancelHandler(&loop, this);
std::ignore = session_->ConnectToServer(credentials_);
OUTCOME_TRY(MapError(loop.exec(), Error::kCouldNotConnectToServer));
emit statusMessage(QString("Successfully connected to %1:%2.")
.arg(QString::fromStdString(credentials_.addr_and_port.addr))
.arg(credentials_.addr_and_port.port));
QObject::connect(&session_.value(), &Session::errorOccurred, this,
&ServiceDeployManager::handleSocketError);
return outcome::success();
}
void ServiceDeployManager::StartWatchdog() {
ORBIT_CHECK(QThread::currentThread() == thread());
std::ignore = orbit_service_task_->Write(kSshWatchdogPassphrase);
QObject::connect(&ssh_watchdog_timer_, &QTimer::timeout, [this]() {
ORBIT_CHECK(orbit_service_task_.has_value());
std::ignore = orbit_service_task_->Write(".");
});
ssh_watchdog_timer_.start(kSshWatchdogInterval);
}
ErrorMessageOr<ServiceDeployManager::GrpcPort> ServiceDeployManager::Exec() {
ErrorMessageOr<GrpcPort> result = outcome::success(GrpcPort{0});
DeferToBackgroundThreadAndWait(this, [&]() { result = ExecImpl(); });
if (!result.has_value()) {
if (result.error() == make_error_code(Error::kUserCanceledServiceDeployment)) {
ORBIT_LOG("OrbitService deployment has been aborted by the user");
} else {
ORBIT_ERROR("OrbitService deployment failed, error: %s", result.error().message());
}
} else {
ORBIT_LOG("Deployment successful, grpc_port: %d", result.value().grpc_port);
}
return result;
}
ErrorMessageOr<ServiceDeployManager::GrpcPort> ServiceDeployManager::ExecImpl() {
ORBIT_CHECK(QThread::currentThread() == thread());
OUTCOME_TRY(ConnectToServer());
OUTCOME_TRY(auto&& sftp_channel, StartSftpChannel());
sftp_channel_ = std::move(sftp_channel);
// Release mode: Deploying a signed debian package. No password required.
if (std::holds_alternative<SignedDebianPackageDeployment>(*deployment_configuration_)) {
const auto& config = std::get<SignedDebianPackageDeployment>(*deployment_configuration_);
OUTCOME_TRY(auto&& service_already_installed, CheckIfInstalled());
if (!service_already_installed) {
OUTCOME_TRY(CopyOrbitServicePackage());
OUTCOME_TRY(InstallOrbitServicePackage());
}
OUTCOME_TRY(StartOrbitService(config));
StartWatchdog();
// Developer mode: Deploying a bare executable and start it via sudo.
} else if (std::holds_alternative<BareExecutableAndRootPasswordDeployment>(
*deployment_configuration_)) {
const auto& config =
std::get<BareExecutableAndRootPasswordDeployment>(*deployment_configuration_);
OUTCOME_TRY(CopyOrbitServiceExecutable(config));
OUTCOME_TRY(CopyOrbitApiLibrary(config));
OUTCOME_TRY(CopyOrbitUserSpaceInstrumentationLibrary(config));
OUTCOME_TRY(StartOrbitService(config));
StartWatchdog();
// Manual Developer mode: No deployment, no starting. Just the tunnels.
} else if (std::holds_alternative<NoDeployment>(*deployment_configuration_)) {
// Nothing to deploy
emit statusMessage(
"Skipping deployment step. Expecting that OrbitService is already "
"running...");
}
ErrorMessageOr<uint16_t> local_grpc_port_result =
StartTunnel(&grpc_tunnel_, grpc_port_.grpc_port);
int retry = 3;
while (retry > 0 && local_grpc_port_result.has_error()) {
ORBIT_ERROR("Failed to establish tunnel. Trying again in 500ms");
std::this_thread::sleep_for(std::chrono::milliseconds{500});
local_grpc_port_result = StartTunnel(&grpc_tunnel_, grpc_port_.grpc_port);
retry--;
}
OUTCOME_TRY(auto&& local_grpc_port, local_grpc_port_result);
emit statusMessage("Successfully set up port forwarding!");
ORBIT_LOG("Local port for gRPC is %d", local_grpc_port);
return outcome::success(GrpcPort{local_grpc_port});
}
void ServiceDeployManager::handleSocketError(std::error_code e) {
ORBIT_LOG("Socket error: %s", e.message());
emit socketErrorOccurred(e);
}
ErrorMessageOr<void> ServiceDeployManager::ShutdownTunnel(orbit_ssh_qt::Tunnel* tunnel) {
ORBIT_SCOPED_TIMED_LOG("ServiceDeployManager::StopTunnel");
ORBIT_CHECK(tunnel != nullptr);
orbit_qt_utils::EventLoop loop{};
auto quit_handler = ConnectQuitHandler(&loop, tunnel, &orbit_ssh_qt::Tunnel::stopped);
auto error_handler = ConnectQuitHandler(&loop, tunnel, &orbit_ssh_qt::Tunnel::errorOccurred);
auto cancel_handler = ConnectCancelHandler(&loop, this);
std::ignore = tunnel->Stop();
OUTCOME_TRY(loop.exec());
return outcome::success();
}
ErrorMessageOr<void> ServiceDeployManager::ShutdownTask(orbit_ssh_qt::Task* task) {
ORBIT_SCOPED_TIMED_LOG("ServiceDeployManager::ShutdownOrbitService");
ORBIT_CHECK(task != nullptr);
orbit_qt_utils::EventLoop loop{};
auto quit_handler = ConnectQuitHandler(&loop, task, &orbit_ssh_qt::Task::stopped);
auto error_handler = ConnectQuitHandler(&loop, task, &orbit_ssh_qt::Task::errorOccurred);
auto cancel_handler = ConnectCancelHandler(&loop, this);
std::ignore = task->Stop();
OUTCOME_TRY(loop.exec());
return outcome::success();
}
ErrorMessageOr<void> ServiceDeployManager::ShutdownSession(orbit_ssh_qt::Session* session) {
ORBIT_SCOPED_TIMED_LOG("ServiceDeployManager::ShutdownSession");
ORBIT_CHECK(session != nullptr);
orbit_qt_utils::EventLoop loop{};
auto quit_handler = ConnectQuitHandler(&loop, session, &orbit_ssh_qt::Session::stopped);
auto error_handler = ConnectQuitHandler(&loop, session, &orbit_ssh_qt::Session::errorOccurred);
auto cancel_handler = ConnectCancelHandler(&loop, this);
orbit_base::Future<ErrorMessageOr<void>> disconnect_future = session->Disconnect();
if (!disconnect_future.IsFinished()) {
OUTCOME_TRY(loop.exec());
}
return outcome::success();
}
void ServiceDeployManager::Shutdown() {
ORBIT_SCOPED_TIMED_LOG("ServiceDeployManager::Shutdown");
QMetaObject::invokeMethod(
this,
[this]() {
if (copy_to_local_operation_ != nullptr) {
ErrorMessageOr<void> shutdown_result = ShutdownSftpOperations();
if (shutdown_result.has_error()) {
ORBIT_ERROR("Unable to shut down ongoing copy to local operation: %s",
shutdown_result.error().message());
}
}
if (sftp_channel_ != nullptr) {
ErrorMessageOr<void> shutdown_result = ShutdownSftpChannel(sftp_channel_.get());
if (shutdown_result.has_error()) {
ORBIT_ERROR("Unable to ShutdownSftpChannel: %s", shutdown_result.error().message());
}
sftp_channel_.reset();
}
if (grpc_tunnel_.has_value()) {
ErrorMessageOr<void> shutdown_result = ShutdownTunnel(&grpc_tunnel_.value());
if (shutdown_result.has_error()) {
ORBIT_ERROR("Unable to ShutdownTunnel: %s", shutdown_result.error().message());
}
grpc_tunnel_ = std::nullopt;
}
ssh_watchdog_timer_.stop();
if (orbit_service_task_.has_value()) {
ErrorMessageOr<void> shutdown_result = ShutdownTask(&orbit_service_task_.value());
if (shutdown_result.has_error()) {
ORBIT_ERROR("Unable to ShutdownTask: %s", shutdown_result.error().message());
}
orbit_service_task_ = std::nullopt;
}
if (session_.has_value()) {
ErrorMessageOr<void> shutdown_result = ShutdownSession(&session_.value());
if (shutdown_result.has_error()) {
ORBIT_ERROR("Unable to ShutdownSession: %s", shutdown_result.error().message());
}
session_ = std::nullopt;
}
},
Qt::BlockingQueuedConnection);
}
} // namespace orbit_session_setup