(ctx context.Context, opts InitClusterOptions)
| 191 | } |
| 192 | |
| 193 | func (cli *CLI) initRemoteMachine(ctx context.Context, opts InitClusterOptions) (*client.Client, error) { |
| 194 | contextName, err := cli.newContextName(opts.Context) |
| 195 | if err != nil { |
| 196 | return nil, err |
| 197 | } |
| 198 | |
| 199 | machineClient, err := provisionOrConnectRemoteMachine(ctx, opts.RemoteMachine, opts.SkipInstall, opts.Version) |
| 200 | if err != nil { |
| 201 | return nil, err |
| 202 | } |
| 203 | // Ensure machineClient is closed on error. |
| 204 | defer func() { |
| 205 | if err != nil { |
| 206 | machineClient.Close() |
| 207 | } |
| 208 | }() |
| 209 | |
| 210 | // Check if the machine is already initialised as a cluster member and prompt the user to reset it first. |
| 211 | minfo, err := machineClient.Inspect(ctx, &emptypb.Empty{}) |
| 212 | if err != nil { |
| 213 | return nil, fmt.Errorf("inspect machine: %w", err) |
| 214 | } |
| 215 | if minfo.Id != "" { |
| 216 | if !opts.AutoConfirm { |
| 217 | if err = promptResetMachine(); err != nil { |
| 218 | return nil, err |
| 219 | } |
| 220 | } |
| 221 | if err = resetAndWaitMachine(ctx, machineClient.MachineClient); err != nil { |
| 222 | return nil, err |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | // Check machine meets all necessary system requirements before proceeding. |
| 227 | checkResp, err := machineClient.CheckPrerequisites(ctx, &emptypb.Empty{}) |
| 228 | // TODO(lhf): remove Unimplemented check when v0.9.0 is released. |
| 229 | if err != nil { |
| 230 | if status.Convert(err).Code() != codes.Unimplemented { |
| 231 | return nil, fmt.Errorf("check machine prerequisites: %w", err) |
| 232 | } |
| 233 | } else if !checkResp.Satisfied { |
| 234 | return nil, fmt.Errorf("machine prerequisites not satisfied: %s", checkResp.Error) |
| 235 | } |
| 236 | |
| 237 | req := &pb.InitClusterRequest{ |
| 238 | MachineName: opts.MachineName, |
| 239 | Network: pb.NewIPPrefix(opts.Network), |
| 240 | WireguardEndpoints: opts.WireguardEndpoints, |
| 241 | WireguardMtu: int32(opts.WireguardMTU), |
| 242 | WireguardPort: int32(opts.WireguardPort), |
| 243 | } |
| 244 | if opts.PublicIP != nil { |
| 245 | if opts.PublicIP.IsValid() { |
| 246 | req.PublicIpConfig = &pb.InitClusterRequest_PublicIp{PublicIp: pb.NewIP(*opts.PublicIP)} |
| 247 | } else { |
| 248 | // Invalid or in other words zero IP means to automatically detect the public IP. |
| 249 | req.PublicIpConfig = &pb.InitClusterRequest_PublicIpAuto{PublicIpAuto: true} |
| 250 | } |
no test coverage detected