2022-09-05 03:06:08 -04:00
/ *
Copyright ( c ) Edgeless Systems GmbH
SPDX - License - Identifier : AGPL - 3.0 - only
* /
2022-04-13 07:01:38 -04:00
package cloudcmd
import (
"context"
2023-03-20 08:33:04 -04:00
"encoding/base64"
2023-02-27 12:19:52 -05:00
"errors"
2022-04-13 07:01:38 -04:00
"fmt"
"io"
2023-03-20 08:33:04 -04:00
"net/http"
2022-10-05 03:11:30 -04:00
"net/url"
"os"
2022-12-07 05:48:54 -05:00
"path"
2022-10-13 11:38:38 -04:00
"regexp"
2022-09-26 09:52:31 -04:00
"runtime"
2022-10-05 03:11:30 -04:00
"strings"
2022-04-13 07:01:38 -04:00
2023-03-20 08:33:04 -04:00
"github.com/Azure/azure-sdk-for-go/profiles/latest/attestation/attestation"
azpolicy "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
2022-10-11 06:24:33 -04:00
"github.com/edgelesssys/constellation/v2/cli/internal/clusterid"
2023-01-03 03:58:35 -05:00
"github.com/edgelesssys/constellation/v2/cli/internal/image"
2022-10-05 03:11:30 -04:00
"github.com/edgelesssys/constellation/v2/cli/internal/libvirt"
2022-09-26 09:52:31 -04:00
"github.com/edgelesssys/constellation/v2/cli/internal/terraform"
2022-09-21 07:47:57 -04:00
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
"github.com/edgelesssys/constellation/v2/internal/config"
2022-11-14 12:18:58 -05:00
"github.com/edgelesssys/constellation/v2/internal/constants"
2022-04-13 07:01:38 -04:00
)
// Creator creates cloud resources.
type Creator struct {
2022-09-27 03:22:29 -04:00
out io . Writer
2022-11-22 12:47:08 -05:00
image imageFetcher
2022-10-26 09:57:00 -04:00
newTerraformClient func ( ctx context . Context ) ( terraformClient , error )
2022-10-05 03:11:30 -04:00
newLibvirtRunner func ( ) libvirtRunner
2022-11-22 12:47:08 -05:00
newRawDownloader func ( ) rawDownloader
2023-03-20 08:33:04 -04:00
policyPatcher PolicyPatcher
2022-04-13 07:01:38 -04:00
}
// NewCreator creates a new creator.
func NewCreator ( out io . Writer ) * Creator {
return & Creator {
2022-11-22 12:47:08 -05:00
out : out ,
image : image . New ( ) ,
2022-10-26 09:57:00 -04:00
newTerraformClient : func ( ctx context . Context ) ( terraformClient , error ) {
2022-11-14 12:18:58 -05:00
return terraform . New ( ctx , constants . TerraformWorkingDir )
2022-04-13 07:01:38 -04:00
} ,
2022-10-05 03:11:30 -04:00
newLibvirtRunner : func ( ) libvirtRunner {
return libvirt . New ( )
} ,
2022-11-22 12:47:08 -05:00
newRawDownloader : func ( ) rawDownloader {
return image . NewDownloader ( )
} ,
2023-03-20 08:33:04 -04:00
policyPatcher : policyPatcher { } ,
2022-04-13 07:01:38 -04:00
}
}
// Create creates the handed amount of instances and all the needed resources.
2023-02-10 07:27:22 -05:00
func ( c * Creator ) Create ( ctx context . Context , provider cloudprovider . Provider , config * config . Config , insType string , controlPlaneCount , workerCount int ,
2022-10-11 06:24:33 -04:00
) ( clusterid . File , error ) {
2022-11-22 12:47:08 -05:00
image , err := c . image . FetchReference ( ctx , config )
if err != nil {
return clusterid . File { } , fmt . Errorf ( "fetching image reference: %w" , err )
}
2022-04-13 07:01:38 -04:00
switch provider {
2022-10-21 06:24:18 -04:00
case cloudprovider . AWS :
2022-10-26 09:57:00 -04:00
cl , err := c . newTerraformClient ( ctx )
2022-10-21 06:24:18 -04:00
if err != nil {
return clusterid . File { } , err
}
defer cl . RemoveInstaller ( )
2023-02-10 07:27:22 -05:00
return c . createAWS ( ctx , cl , config , insType , controlPlaneCount , workerCount , image )
2022-04-13 07:01:38 -04:00
case cloudprovider . GCP :
2022-10-26 09:57:00 -04:00
cl , err := c . newTerraformClient ( ctx )
2022-04-13 07:01:38 -04:00
if err != nil {
2022-10-11 06:24:33 -04:00
return clusterid . File { } , err
2022-04-13 07:01:38 -04:00
}
2022-09-27 03:22:29 -04:00
defer cl . RemoveInstaller ( )
2023-02-10 07:27:22 -05:00
return c . createGCP ( ctx , cl , config , insType , controlPlaneCount , workerCount , image )
2022-04-13 07:01:38 -04:00
case cloudprovider . Azure :
2022-10-26 09:57:00 -04:00
cl , err := c . newTerraformClient ( ctx )
2022-04-13 07:01:38 -04:00
if err != nil {
2022-10-11 06:24:33 -04:00
return clusterid . File { } , err
2022-04-13 07:01:38 -04:00
}
2022-10-06 05:52:19 -04:00
defer cl . RemoveInstaller ( )
2023-02-10 07:27:22 -05:00
return c . createAzure ( ctx , cl , config , insType , controlPlaneCount , workerCount , image )
2023-02-27 12:19:52 -05:00
case cloudprovider . OpenStack :
cl , err := c . newTerraformClient ( ctx )
if err != nil {
return clusterid . File { } , err
}
defer cl . RemoveInstaller ( )
return c . createOpenStack ( ctx , cl , config , controlPlaneCount , workerCount , image )
2022-09-26 09:52:31 -04:00
case cloudprovider . QEMU :
2022-09-27 03:22:29 -04:00
if runtime . GOARCH != "amd64" || runtime . GOOS != "linux" {
2022-10-11 06:24:33 -04:00
return clusterid . File { } , fmt . Errorf ( "creation of a QEMU based Constellation is not supported for %s/%s" , runtime . GOOS , runtime . GOARCH )
2022-09-27 03:22:29 -04:00
}
2022-10-26 09:57:00 -04:00
cl , err := c . newTerraformClient ( ctx )
2022-09-26 09:52:31 -04:00
if err != nil {
2022-10-11 06:24:33 -04:00
return clusterid . File { } , err
2022-09-26 09:52:31 -04:00
}
defer cl . RemoveInstaller ( )
2022-10-05 03:11:30 -04:00
lv := c . newLibvirtRunner ( )
2023-02-10 07:27:22 -05:00
return c . createQEMU ( ctx , cl , lv , config , controlPlaneCount , workerCount , image )
2022-04-13 07:01:38 -04:00
default :
2022-10-11 06:24:33 -04:00
return clusterid . File { } , fmt . Errorf ( "unsupported cloud provider: %s" , provider )
2022-04-13 07:01:38 -04:00
}
}
2022-10-21 06:24:18 -04:00
func ( c * Creator ) createAWS ( ctx context . Context , cl terraformClient , config * config . Config ,
2023-02-10 07:27:22 -05:00
insType string , controlPlaneCount , workerCount int , image string ,
2022-10-21 06:24:18 -04:00
) ( idFile clusterid . File , retErr error ) {
2022-12-07 05:48:54 -05:00
vars := terraform . AWSClusterVariables {
2022-10-21 06:24:18 -04:00
CommonVariables : terraform . CommonVariables {
2023-02-10 07:27:22 -05:00
Name : config . Name ,
2022-10-21 06:24:18 -04:00
CountControlPlanes : controlPlaneCount ,
CountWorkers : workerCount ,
StateDiskSizeGB : config . StateDiskSizeGB ,
} ,
2022-10-24 18:50:30 -04:00
StateDiskType : config . Provider . AWS . StateDiskType ,
2022-10-21 06:24:18 -04:00
Region : config . Provider . AWS . Region ,
Zone : config . Provider . AWS . Zone ,
InstanceType : insType ,
2022-11-22 12:47:08 -05:00
AMIImageID : image ,
2022-10-21 06:24:18 -04:00
IAMProfileControlPlane : config . Provider . AWS . IAMProfileControlPlane ,
IAMProfileWorkerNodes : config . Provider . AWS . IAMProfileWorkerNodes ,
Debug : config . IsDebugCluster ( ) ,
}
2022-12-07 05:48:54 -05:00
if err := cl . PrepareWorkspace ( path . Join ( "terraform" , strings . ToLower ( cloudprovider . AWS . String ( ) ) ) , & vars ) ; err != nil {
2022-11-15 08:00:44 -05:00
return clusterid . File { } , err
}
2023-03-19 11:05:01 -04:00
defer rollbackOnError ( c . out , & retErr , & rollbackerTerraform { client : cl } )
2023-01-19 04:41:07 -05:00
tfOutput , err := cl . CreateCluster ( ctx )
2022-10-21 06:24:18 -04:00
if err != nil {
return clusterid . File { } , err
}
return clusterid . File {
CloudProvider : cloudprovider . AWS ,
2023-01-19 04:41:07 -05:00
InitSecret : [ ] byte ( tfOutput . Secret ) ,
IP : tfOutput . IP ,
UID : tfOutput . UID ,
2022-10-21 06:24:18 -04:00
} , nil
}
2022-09-27 03:22:29 -04:00
func ( c * Creator ) createGCP ( ctx context . Context , cl terraformClient , config * config . Config ,
2023-02-10 07:27:22 -05:00
insType string , controlPlaneCount , workerCount int , image string ,
2022-10-11 06:24:33 -04:00
) ( idFile clusterid . File , retErr error ) {
2022-12-07 05:48:54 -05:00
vars := terraform . GCPClusterVariables {
2022-09-27 03:22:29 -04:00
CommonVariables : terraform . CommonVariables {
2023-02-10 07:27:22 -05:00
Name : config . Name ,
2022-09-27 03:22:29 -04:00
CountControlPlanes : controlPlaneCount ,
CountWorkers : workerCount ,
StateDiskSizeGB : config . StateDiskSizeGB ,
2022-05-24 04:04:42 -04:00
} ,
2022-09-27 03:22:29 -04:00
Project : config . Provider . GCP . Project ,
Region : config . Provider . GCP . Region ,
Zone : config . Provider . GCP . Zone ,
CredentialsFile : config . Provider . GCP . ServiceAccountKeyPath ,
InstanceType : insType ,
StateDiskType : config . Provider . GCP . StateDiskType ,
2022-11-22 12:47:08 -05:00
ImageID : image ,
2022-09-27 03:22:29 -04:00
Debug : config . IsDebugCluster ( ) ,
2022-04-13 07:01:38 -04:00
}
2022-12-07 05:48:54 -05:00
if err := cl . PrepareWorkspace ( path . Join ( "terraform" , strings . ToLower ( cloudprovider . GCP . String ( ) ) ) , & vars ) ; err != nil {
2022-11-15 08:00:44 -05:00
return clusterid . File { } , err
}
2023-03-19 11:05:01 -04:00
defer rollbackOnError ( c . out , & retErr , & rollbackerTerraform { client : cl } )
2023-01-19 04:41:07 -05:00
tfOutput , err := cl . CreateCluster ( ctx )
2022-10-11 06:24:33 -04:00
if err != nil {
return clusterid . File { } , err
2022-06-09 16:26:36 -04:00
}
2022-10-11 06:24:33 -04:00
return clusterid . File {
CloudProvider : cloudprovider . GCP ,
2023-01-19 04:41:07 -05:00
InitSecret : [ ] byte ( tfOutput . Secret ) ,
IP : tfOutput . IP ,
UID : tfOutput . UID ,
2022-10-11 06:24:33 -04:00
} , nil
2022-04-13 07:01:38 -04:00
}
2023-03-20 08:33:04 -04:00
func ( c * Creator ) createAzure ( ctx context . Context , cl terraformClient , config * config . Config , insType string , controlPlaneCount , workerCount int , image string ,
2022-10-11 06:24:33 -04:00
) ( idFile clusterid . File , retErr error ) {
2022-12-07 05:48:54 -05:00
vars := terraform . AzureClusterVariables {
2022-10-06 05:52:19 -04:00
CommonVariables : terraform . CommonVariables {
2023-02-10 07:27:22 -05:00
Name : config . Name ,
2022-10-06 05:52:19 -04:00
CountControlPlanes : controlPlaneCount ,
CountWorkers : workerCount ,
StateDiskSizeGB : config . StateDiskSizeGB ,
} ,
Location : config . Provider . Azure . Location ,
ResourceGroup : config . Provider . Azure . ResourceGroup ,
UserAssignedIdentity : config . Provider . Azure . UserAssignedIdentity ,
2022-04-13 07:01:38 -04:00
InstanceType : insType ,
2022-08-02 06:24:55 -04:00
StateDiskType : config . Provider . Azure . StateDiskType ,
2022-11-22 12:47:08 -05:00
ImageID : image ,
2022-08-25 09:24:31 -04:00
ConfidentialVM : * config . Provider . Azure . ConfidentialVM ,
2022-10-19 07:10:15 -04:00
SecureBoot : * config . Provider . Azure . SecureBoot ,
2023-03-20 08:33:04 -04:00
CreateMAA : * config . Provider . Azure . EnforceIDKeyDigest ,
2022-10-06 05:52:19 -04:00
Debug : config . IsDebugCluster ( ) ,
2022-04-13 07:01:38 -04:00
}
2022-10-06 05:52:19 -04:00
2022-10-12 11:00:59 -04:00
vars = normalizeAzureURIs ( vars )
2022-12-07 05:48:54 -05:00
if err := cl . PrepareWorkspace ( path . Join ( "terraform" , strings . ToLower ( cloudprovider . Azure . String ( ) ) ) , & vars ) ; err != nil {
2022-11-15 08:00:44 -05:00
return clusterid . File { } , err
}
2023-03-19 11:05:01 -04:00
defer rollbackOnError ( c . out , & retErr , & rollbackerTerraform { client : cl } )
2023-01-19 04:41:07 -05:00
tfOutput , err := cl . CreateCluster ( ctx )
2022-10-11 06:24:33 -04:00
if err != nil {
return clusterid . File { } , err
2022-04-13 07:01:38 -04:00
}
2023-03-20 08:33:04 -04:00
if vars . CreateMAA {
// Patch the attestation policy to allow the cluster to boot while having secure boot disabled.
if err := c . policyPatcher . Patch ( ctx , tfOutput . AttestationURL ) ; err != nil {
return clusterid . File { } , err
}
}
2022-10-11 06:24:33 -04:00
return clusterid . File {
2023-03-20 08:33:04 -04:00
CloudProvider : cloudprovider . Azure ,
IP : tfOutput . IP ,
InitSecret : [ ] byte ( tfOutput . Secret ) ,
UID : tfOutput . UID ,
AttestationURL : tfOutput . AttestationURL ,
2022-10-11 06:24:33 -04:00
} , nil
2022-04-13 07:01:38 -04:00
}
2022-09-26 09:52:31 -04:00
2023-03-20 08:33:04 -04:00
// PolicyPatcher interacts with Azure to update the attestation policy.
type PolicyPatcher interface {
Patch ( ctx context . Context , attestationURL string ) error
}
type policyPatcher struct { }
// Patch updates the attestation policy to the base64-encoded attestation policy JWT for the given attestation URL.
// https://learn.microsoft.com/en-us/azure/attestation/author-sign-policy#next-steps
func ( p policyPatcher ) Patch ( ctx context . Context , attestationURL string ) error {
// hacky way to update the MAA attestation policy. This should be changed as soon as either the Terraform provider supports it
// or the Go SDK gets updated to a recent API version.
// https://github.com/hashicorp/terraform-provider-azurerm/issues/20804
cred , err := azidentity . NewDefaultAzureCredential ( nil )
if err != nil {
return fmt . Errorf ( "retrieving default Azure credentials: %w" , err )
}
token , err := cred . GetToken ( ctx , azpolicy . TokenRequestOptions {
Scopes : [ ] string { "https://attest.azure.net/.default" } ,
} )
if err != nil {
return fmt . Errorf ( "retrieving token from default Azure credentials: %w" , err )
}
client := attestation . NewPolicyClient ( )
// azureGuest is the id for the "Azure VM" attestation type. Other types are documented here:
// https://learn.microsoft.com/en-us/rest/api/attestation/policy/set
req , err := client . SetPreparer ( ctx , attestationURL , "azureGuest" , p . encodeAttestationPolicy ( ) )
req . Header . Set ( "Authorization" , fmt . Sprintf ( "Bearer %s" , token . Token ) )
if err != nil {
return fmt . Errorf ( "preparing request: %w" , err )
}
resp , err := client . Send ( req )
if err != nil {
return fmt . Errorf ( "sending request: %w" , err )
}
resp . Body . Close ( )
if resp . StatusCode != http . StatusOK {
return fmt . Errorf ( "updating attestation policy: unexpected status code: %s" , resp . Status )
}
return nil
}
// encodeAttestationPolicy encodes the base64-encoded attestation policy in the JWS format specified here:
// https://learn.microsoft.com/en-us/azure/attestation/author-sign-policy#creating-the-policy-file-in-json-web-signature-format
func ( p policyPatcher ) encodeAttestationPolicy ( ) string {
const policy = `
version = 1.0 ;
authorizationrules
{
[ type == "x-ms-azurevm-default-securebootkeysvalidated" , value == false ] = > deny ( ) ;
[ type == "x-ms-azurevm-debuggersdisabled" , value == false ] = > deny ( ) ;
// The line below was edited by the Constellation CLI. Do not edit manually.
//[type=="secureboot", value==false] => deny();
[ type == "x-ms-azurevm-signingdisabled" , value == false ] = > deny ( ) ;
[ type == "x-ms-azurevm-dbvalidated" , value == false ] = > deny ( ) ;
[ type == "x-ms-azurevm-dbxvalidated" , value == false ] = > deny ( ) ;
= > permit ( ) ;
} ;
issuancerules
{
} ; `
encodedPolicy := base64 . RawURLEncoding . EncodeToString ( [ ] byte ( policy ) )
const header = ` { "alg":"none"} `
payload := fmt . Sprintf ( ` { "AttestationPolicy":"%s"} ` , encodedPolicy )
encodedHeader := base64 . RawURLEncoding . EncodeToString ( [ ] byte ( header ) )
encodedPayload := base64 . RawURLEncoding . EncodeToString ( [ ] byte ( payload ) )
return fmt . Sprintf ( "%s.%s." , encodedHeader , encodedPayload )
}
2022-10-13 11:38:38 -04:00
// The azurerm Terraform provider enforces its own convention of case sensitivity for Azure URIs which Azure's API itself does not enforce or, even worse, actually returns.
// Let's go loco with case insensitive Regexp here and fix the user input here to be compliant with this arbitrary design decision.
var (
caseInsensitiveSubscriptionsRegexp = regexp . MustCompile ( ` (?i)\/subscriptions\/ ` )
caseInsensitiveResourceGroupRegexp = regexp . MustCompile ( ` (?i)\/resourcegroups\/ ` )
caseInsensitiveProvidersRegexp = regexp . MustCompile ( ` (?i)\/providers\/ ` )
caseInsensitiveUserAssignedIdentitiesRegexp = regexp . MustCompile ( ` (?i)\/userassignedidentities\/ ` )
caseInsensitiveMicrosoftManagedIdentity = regexp . MustCompile ( ` (?i)\/microsoft.managedidentity\/ ` )
caseInsensitiveCommunityGalleriesRegexp = regexp . MustCompile ( ` (?i)\/communitygalleries\/ ` )
caseInsensitiveImagesRegExp = regexp . MustCompile ( ` (?i)\/images\/ ` )
caseInsensitiveVersionsRegExp = regexp . MustCompile ( ` (?i)\/versions\/ ` )
)
2022-10-12 11:00:59 -04:00
2022-12-07 05:48:54 -05:00
func normalizeAzureURIs ( vars terraform . AzureClusterVariables ) terraform . AzureClusterVariables {
2022-10-13 11:38:38 -04:00
vars . UserAssignedIdentity = caseInsensitiveSubscriptionsRegexp . ReplaceAllString ( vars . UserAssignedIdentity , "/subscriptions/" )
vars . UserAssignedIdentity = caseInsensitiveResourceGroupRegexp . ReplaceAllString ( vars . UserAssignedIdentity , "/resourceGroups/" )
vars . UserAssignedIdentity = caseInsensitiveProvidersRegexp . ReplaceAllString ( vars . UserAssignedIdentity , "/providers/" )
vars . UserAssignedIdentity = caseInsensitiveUserAssignedIdentitiesRegexp . ReplaceAllString ( vars . UserAssignedIdentity , "/userAssignedIdentities/" )
vars . UserAssignedIdentity = caseInsensitiveMicrosoftManagedIdentity . ReplaceAllString ( vars . UserAssignedIdentity , "/Microsoft.ManagedIdentity/" )
vars . ImageID = caseInsensitiveCommunityGalleriesRegexp . ReplaceAllString ( vars . ImageID , "/communityGalleries/" )
vars . ImageID = caseInsensitiveImagesRegExp . ReplaceAllString ( vars . ImageID , "/images/" )
vars . ImageID = caseInsensitiveVersionsRegExp . ReplaceAllString ( vars . ImageID , "/versions/" )
2022-10-12 11:00:59 -04:00
return vars
}
2023-02-27 12:19:52 -05:00
func ( c * Creator ) createOpenStack ( ctx context . Context , cl terraformClient , config * config . Config ,
controlPlaneCount , workerCount int , image string ,
) ( idFile clusterid . File , retErr error ) {
// TODO: Remove this once OpenStack is supported.
if os . Getenv ( "CONSTELLATION_OPENSTACK_DEV" ) != "1" {
return clusterid . File { } , errors . New ( "OpenStack isn't supported yet" )
}
if _ , hasOSAuthURL := os . LookupEnv ( "OS_AUTH_URL" ) ; ! hasOSAuthURL && config . Provider . OpenStack . Cloud == "" {
return clusterid . File { } , errors . New (
"neither environment variable OS_AUTH_URL nor cloud name for \"clouds.yaml\" is set. OpenStack authentication requires a set of " +
"OS_* environment variables that are typically sourced into the current shell with an openrc file " +
"or a cloud name for \"clouds.yaml\". " +
"See https://docs.openstack.org/openstacksdk/latest/user/config/configuration.html for more information" ,
)
}
vars := terraform . OpenStackClusterVariables {
CommonVariables : terraform . CommonVariables {
Name : config . Name ,
CountControlPlanes : controlPlaneCount ,
CountWorkers : workerCount ,
StateDiskSizeGB : config . StateDiskSizeGB ,
} ,
2023-03-03 09:28:28 -05:00
Cloud : config . Provider . OpenStack . Cloud ,
AvailabilityZone : config . Provider . OpenStack . AvailabilityZone ,
FloatingIPPoolID : config . Provider . OpenStack . FloatingIPPoolID ,
FlavorID : config . Provider . OpenStack . FlavorID ,
ImageURL : image ,
DirectDownload : * config . Provider . OpenStack . DirectDownload ,
OpenstackUserDomainName : config . Provider . OpenStack . UserDomainName ,
OpenstackUsername : config . Provider . OpenStack . Username ,
OpenstackPassword : config . Provider . OpenStack . Password ,
Debug : config . IsDebugCluster ( ) ,
2023-02-27 12:19:52 -05:00
}
if err := cl . PrepareWorkspace ( path . Join ( "terraform" , strings . ToLower ( cloudprovider . OpenStack . String ( ) ) ) , & vars ) ; err != nil {
return clusterid . File { } , err
}
2023-03-19 11:05:01 -04:00
defer rollbackOnError ( c . out , & retErr , & rollbackerTerraform { client : cl } )
2023-02-27 12:19:52 -05:00
tfOutput , err := cl . CreateCluster ( ctx )
if err != nil {
return clusterid . File { } , err
}
return clusterid . File {
CloudProvider : cloudprovider . OpenStack ,
IP : tfOutput . IP ,
InitSecret : [ ] byte ( tfOutput . Secret ) ,
UID : tfOutput . UID ,
} , nil
}
2023-02-10 07:27:22 -05:00
func ( c * Creator ) createQEMU ( ctx context . Context , cl terraformClient , lv libvirtRunner , config * config . Config ,
2022-11-22 12:47:08 -05:00
controlPlaneCount , workerCount int , source string ,
2022-10-11 06:24:33 -04:00
) ( idFile clusterid . File , retErr error ) {
2022-11-15 08:00:44 -05:00
qemuRollbacker := & rollbackerQEMU { client : cl , libvirt : lv , createdWorkspace : false }
2023-03-19 11:05:01 -04:00
defer rollbackOnError ( c . out , & retErr , qemuRollbacker )
2022-10-05 03:11:30 -04:00
2022-11-22 12:47:08 -05:00
// TODO: render progress bar
downloader := c . newRawDownloader ( )
imagePath , err := downloader . Download ( ctx , c . out , false , source , config . Image )
if err != nil {
return clusterid . File { } , fmt . Errorf ( "download raw image: %w" , err )
}
2022-10-05 03:11:30 -04:00
libvirtURI := config . Provider . QEMU . LibvirtURI
libvirtSocketPath := "."
switch {
// if no libvirt URI is specified, start a libvirt container
case libvirtURI == "" :
2023-02-10 07:27:22 -05:00
if err := lv . Start ( ctx , config . Name , config . Provider . QEMU . LibvirtContainerImage ) ; err != nil {
2022-10-11 06:24:33 -04:00
return clusterid . File { } , err
2022-10-05 03:11:30 -04:00
}
2022-10-07 03:38:43 -04:00
libvirtURI = libvirt . LibvirtTCPConnectURI
2022-10-05 03:11:30 -04:00
// socket for system URI should be in /var/run/libvirt/libvirt-sock
case libvirtURI == "qemu:///system" :
libvirtSocketPath = "/var/run/libvirt/libvirt-sock"
// socket for session URI should be in /run/user/<uid>/libvirt/libvirt-sock
case libvirtURI == "qemu:///session" :
libvirtSocketPath = fmt . Sprintf ( "/run/user/%d/libvirt/libvirt-sock" , os . Getuid ( ) )
// if a unix socket is specified we need to parse the URI to get the socket path
case strings . HasPrefix ( libvirtURI , "qemu+unix://" ) :
unixURI , err := url . Parse ( strings . TrimPrefix ( libvirtURI , "qemu+unix://" ) )
if err != nil {
2022-10-11 06:24:33 -04:00
return clusterid . File { } , err
2022-10-05 03:11:30 -04:00
}
libvirtSocketPath = unixURI . Query ( ) . Get ( "socket" )
if libvirtSocketPath == "" {
2022-10-11 06:24:33 -04:00
return clusterid . File { } , fmt . Errorf ( "socket path not specified in qemu+unix URI: %s" , libvirtURI )
2022-10-05 03:11:30 -04:00
}
}
metadataLibvirtURI := libvirtURI
if libvirtSocketPath != "." {
metadataLibvirtURI = "qemu:///system"
}
2022-09-27 03:22:29 -04:00
2022-10-12 11:00:59 -04:00
vars := terraform . QEMUVariables {
2022-09-27 03:22:29 -04:00
CommonVariables : terraform . CommonVariables {
2023-02-10 07:27:22 -05:00
Name : config . Name ,
2022-09-27 03:22:29 -04:00
CountControlPlanes : controlPlaneCount ,
CountWorkers : workerCount ,
StateDiskSizeGB : config . StateDiskSizeGB ,
2022-09-26 09:52:31 -04:00
} ,
2022-10-05 03:11:30 -04:00
LibvirtURI : libvirtURI ,
LibvirtSocketPath : libvirtSocketPath ,
2022-11-22 12:47:08 -05:00
ImagePath : imagePath ,
2022-10-05 03:11:30 -04:00
ImageFormat : config . Provider . QEMU . ImageFormat ,
CPUCount : config . Provider . QEMU . VCPUs ,
MemorySizeMiB : config . Provider . QEMU . Memory ,
MetadataAPIImage : config . Provider . QEMU . MetadataAPIImage ,
MetadataLibvirtURI : metadataLibvirtURI ,
2022-10-19 07:10:15 -04:00
NVRAM : config . Provider . QEMU . NVRAM ,
Firmware : config . Provider . QEMU . Firmware ,
2022-09-26 09:52:31 -04:00
}
2022-12-07 05:48:54 -05:00
if err := cl . PrepareWorkspace ( path . Join ( "terraform" , strings . ToLower ( cloudprovider . QEMU . String ( ) ) ) , & vars ) ; err != nil {
2022-11-15 08:00:44 -05:00
return clusterid . File { } , err
}
// Allow rollback of QEMU Terraform workspace from this point on
qemuRollbacker . createdWorkspace = true
2023-01-19 04:41:07 -05:00
tfOutput , err := cl . CreateCluster ( ctx )
2022-10-11 06:24:33 -04:00
if err != nil {
return clusterid . File { } , err
2022-09-26 09:52:31 -04:00
}
2022-10-11 06:24:33 -04:00
return clusterid . File {
CloudProvider : cloudprovider . QEMU ,
2023-01-19 04:41:07 -05:00
InitSecret : [ ] byte ( tfOutput . Secret ) ,
IP : tfOutput . IP ,
UID : tfOutput . UID ,
2022-10-11 06:24:33 -04:00
} , nil
2022-09-26 09:52:31 -04:00
}