• 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 #include <android-base/result.h>
25 
26 #include "common/libs/utils/subprocess.h"
27 #include "host/commands/test_gce_driver/gce_api.h"
28 #include "host/commands/test_gce_driver/key_pair.h"
29 
30 namespace cuttlefish {
31 
32 // TODO(schuffelen): Implement this with libssh2
33 class SshCommand {
34  public:
35   SshCommand() = default;
36 
37   SshCommand& PrivKey(const std::string& path) &;
38   SshCommand PrivKey(const std::string& path) &&;
39 
40   SshCommand& WithoutKnownHosts() &;
41   SshCommand WithoutKnownHosts() &&;
42 
43   SshCommand& Username(const std::string& username) &;
44   SshCommand Username(const std::string& username) &&;
45 
46   SshCommand& Host(const std::string& host) &;
47   SshCommand Host(const std::string& host) &&;
48 
49   SshCommand& RemotePortForward(uint16_t remote, uint16_t local) &;
50   SshCommand RemotePortForward(uint16_t remote, uint16_t local) &&;
51 
52   SshCommand& RemoteParameter(const std::string& param) &;
53   SshCommand RemoteParameter(const std::string& param) &&;
54 
55   Command Build() const;
56 
57  private:
58   struct RemotePortForwardType {
59     uint16_t remote_port;
60     uint16_t local_port;
61   };
62 
63   std::optional<std::string> privkey_path_;
64   bool without_known_hosts_;
65   std::optional<std::string> username_;
66   std::optional<std::string> host_;
67   std::vector<RemotePortForwardType> remote_port_forwards_;
68   std::vector<std::string> parameters_;
69 };
70 
71 class ScopedGceInstance {
72  public:
73   static android::base::Result<std::unique_ptr<ScopedGceInstance>>
74   CreateDefault(GceApi& gce, const std::string& zone,
75                 const std::string& instance_name, bool internal_addresses);
76   ~ScopedGceInstance();
77 
78   android::base::Result<SshCommand> Ssh();
79   android::base::Result<void> Reset();
80 
81  private:
82   ScopedGceInstance(GceApi& gce, const GceInstanceInfo& instance,
83                     std::unique_ptr<TemporaryFile> privkey,
84                     bool internal_addresses);
85 
86   android::base::Result<void> EnforceSshReady();
87 
88   GceApi& gce_;
89   GceInstanceInfo instance_;
90   std::unique_ptr<TemporaryFile> privkey_;
91   bool use_internal_address_;
92 };
93 
94 }  // namespace cuttlefish
95