• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2022 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #pragma once
17 
18 #include <memory>
19 #include <optional>
20 #include <string>
21 #include <vector>
22 
23 #include <android-base/file.h>
24 
25 #include "common/libs/utils/result.h"
26 #include "common/libs/utils/subprocess.h"
27 #include "host/commands/test_gce_driver/gce_api.h"
28 
29 namespace cuttlefish {
30 
31 // TODO(schuffelen): Implement this with libssh2
32 class SshCommand {
33  public:
34   SshCommand() = default;
35 
36   SshCommand& PrivKey(const std::string& path) &;
37   SshCommand PrivKey(const std::string& path) &&;
38 
39   SshCommand& WithoutKnownHosts() &;
40   SshCommand WithoutKnownHosts() &&;
41 
42   SshCommand& Username(const std::string& username) &;
43   SshCommand Username(const std::string& username) &&;
44 
45   SshCommand& Host(const std::string& host) &;
46   SshCommand Host(const std::string& host) &&;
47 
48   SshCommand& RemotePortForward(uint16_t remote, uint16_t local) &;
49   SshCommand RemotePortForward(uint16_t remote, uint16_t local) &&;
50 
51   SshCommand& RemoteParameter(const std::string& param) &;
52   SshCommand RemoteParameter(const std::string& param) &&;
53 
54   Command Build() const;
55 
56  private:
57   struct RemotePortForwardType {
58     uint16_t remote_port;
59     uint16_t local_port;
60   };
61 
62   std::optional<std::string> privkey_path_;
63   bool without_known_hosts_;
64   std::optional<std::string> username_;
65   std::optional<std::string> host_;
66   std::vector<RemotePortForwardType> remote_port_forwards_;
67   std::vector<std::string> parameters_;
68 };
69 
70 class ScopedGceInstance {
71  public:
72   static Result<std::unique_ptr<ScopedGceInstance>> CreateDefault(
73       GceApi& gce, const std::string& zone, const std::string& instance_name,
74       bool internal_addresses);
75   ~ScopedGceInstance();
76 
77   Result<SshCommand> Ssh();
78   Result<void> Reset();
79 
80  private:
81   ScopedGceInstance(GceApi& gce, const GceInstanceInfo& instance,
82                     std::unique_ptr<TemporaryFile> privkey,
83                     bool internal_addresses);
84 
85   Result<void> EnforceSshReady();
86 
87   GceApi& gce_;
88   GceInstanceInfo instance_;
89   std::unique_ptr<TemporaryFile> privkey_;
90   bool use_internal_address_;
91 };
92 
93 }  // namespace cuttlefish
94