2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-05-24 03:44:32 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
|
2022-08-18 09:59:01 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/cloud/cloudprovider"
|
2022-06-08 02:14:28 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/cloud/cloudtypes"
|
2022-05-24 03:44:32 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/state"
|
|
|
|
)
|
|
|
|
|
|
|
|
type terraformOutput struct {
|
|
|
|
ControlPlaneIPs struct {
|
|
|
|
Value []string `json:"value"`
|
|
|
|
} `json:"control_plane_ips"`
|
|
|
|
WorkerIPs struct {
|
|
|
|
Value []string `json:"value"`
|
|
|
|
} `json:"worker_ips"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func terraformOut(workspaceDir string) (terraformOutput, error) {
|
|
|
|
cmd := exec.Command("terraform", "output", "--json")
|
|
|
|
cmd.Dir = workspaceDir
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
cmd.Stdout = &stdout
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
if err := cmd.Run(); err != nil {
|
2022-06-09 10:04:30 -04:00
|
|
|
return terraformOutput{}, fmt.Errorf("command terraform output failed: %q: %w", stderr.String(), err)
|
2022-05-24 03:44:32 -04:00
|
|
|
}
|
|
|
|
var tfOut terraformOutput
|
|
|
|
if err := json.Unmarshal(stdout.Bytes(), &tfOut); err != nil {
|
2022-06-09 10:04:30 -04:00
|
|
|
return terraformOutput{}, fmt.Errorf("unmarshaling terraform output: %w", err)
|
2022-05-24 03:44:32 -04:00
|
|
|
}
|
|
|
|
return tfOut, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func transformState(tfOut terraformOutput) state.ConstellationState {
|
|
|
|
conState := state.ConstellationState{
|
2022-07-29 02:10:51 -04:00
|
|
|
Name: "qemu",
|
|
|
|
UID: "debug",
|
|
|
|
CloudProvider: cloudprovider.QEMU.String(),
|
2022-08-09 03:05:05 -04:00
|
|
|
LoadBalancerIP: tfOut.ControlPlaneIPs.Value[0],
|
2022-07-29 02:10:51 -04:00
|
|
|
QEMUWorkerInstances: cloudtypes.Instances{},
|
|
|
|
QEMUControlPlaneInstances: cloudtypes.Instances{},
|
2022-05-24 03:44:32 -04:00
|
|
|
}
|
|
|
|
for i, ip := range tfOut.ControlPlaneIPs.Value {
|
2022-07-29 02:10:51 -04:00
|
|
|
conState.QEMUControlPlaneInstances[fmt.Sprintf("control-plane-%d", i)] = cloudtypes.Instance{
|
2022-05-24 03:44:32 -04:00
|
|
|
PublicIP: ip,
|
|
|
|
PrivateIP: ip,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i, ip := range tfOut.WorkerIPs.Value {
|
2022-07-29 02:10:51 -04:00
|
|
|
conState.QEMUWorkerInstances[fmt.Sprintf("worker-%d", i)] = cloudtypes.Instance{
|
2022-05-24 03:44:32 -04:00
|
|
|
PublicIP: ip,
|
|
|
|
PrivateIP: ip,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return conState
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeState(workspaceDir string, conState state.ConstellationState) error {
|
|
|
|
rawState, err := json.Marshal(conState)
|
|
|
|
if err != nil {
|
2022-06-09 10:04:30 -04:00
|
|
|
return fmt.Errorf("marshaling state: %w", err)
|
2022-05-24 03:44:32 -04:00
|
|
|
}
|
|
|
|
stateFile := fmt.Sprintf("%s/constellation-state.json", workspaceDir)
|
|
|
|
if err := os.WriteFile(stateFile, rawState, 0o644); err != nil {
|
2022-06-09 10:04:30 -04:00
|
|
|
return fmt.Errorf("writing state: %w", err)
|
2022-05-24 03:44:32 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if len(os.Args) != 3 {
|
|
|
|
fmt.Printf("Usage: %v <terraform-workspace-dir> <constellation-workspace-dir>\n", os.Args[0])
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
tfOut, err := terraformOut(os.Args[1])
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
conState := transformState(tfOut)
|
|
|
|
if err := writeState(os.Args[2], conState); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|