github.com/openshift/installer@v1.4.17/pkg/terraform/providers/providers.go (about)

     1  package providers
     2  
     3  import (
     4  	"embed"
     5  	"fmt"
     6  	"io"
     7  	"os"
     8  	"path/filepath"
     9  	"strings"
    10  
    11  	"github.com/pkg/errors"
    12  	"github.com/sirupsen/logrus"
    13  )
    14  
    15  var (
    16  	// AWS is the provider for creating resources in AWS.
    17  	AWS = provider("aws")
    18  	// AzureStack is the provider for creating resources in Azure Stack.
    19  	AzureStack = provider("azurestack")
    20  	// Google is the provider for creating resources in GCP.
    21  	Google = provider("google")
    22  	// IBM is the provider for creating resources in IBM Cloud.
    23  	IBM = provider("ibm")
    24  	// Ignition is the provider for creating ignition config files.
    25  	Ignition = provider("ignition")
    26  	// Libvirt is the provider for provisioning VMs on a libvirt host.
    27  	Libvirt = provider("libvirt")
    28  	// Local is the provider for creating local files.
    29  	Local = provider("local")
    30  	// Nutanix is the provider for creating resources in Nutanix.
    31  	Nutanix = provider("nutanix")
    32  	// OpenStack is the provider for creating resources in OpenStack.
    33  	OpenStack = provider("openstack")
    34  	// OVirt is the provider for creating resources in oVirt.
    35  	OVirt = provider("ovirt")
    36  	// Time is the provider for adding create and sleep requirements for resources.
    37  	Time = provider("time")
    38  )
    39  
    40  // Provider is a terraform provider.
    41  type Provider struct {
    42  	// Name of the provider.
    43  	Name string
    44  	// Source of the provider.
    45  	Source string
    46  }
    47  
    48  // provider configures a provider built locally.
    49  func provider(name string) Provider {
    50  	return Provider{
    51  		Name:   name,
    52  		Source: fmt.Sprintf("openshift/local/%s", name),
    53  	}
    54  }
    55  
    56  //go:embed mirror/*
    57  var mirror embed.FS
    58  
    59  // Extract extracts the provider from the embedded data into the specified directory.
    60  func (p Provider) Extract(dir string) error {
    61  	providerDir := filepath.Join(strings.Split(p.Source, "/")...)
    62  	destProviderDir := filepath.Join(dir, providerDir)
    63  	destDir := destProviderDir
    64  	srcDir := filepath.Join("mirror", providerDir)
    65  	logrus.Debugf("creating %s directory", destDir)
    66  	if err := os.MkdirAll(destDir, 0777); err != nil {
    67  		return errors.Wrapf(err, "could not make directory for the %s provider", p.Name)
    68  	}
    69  	if err := unpack(srcDir, destDir); err != nil {
    70  		return errors.Wrapf(err, "could not unpack the directory for the %s provider", p.Name)
    71  	}
    72  	return nil
    73  }
    74  
    75  func unpack(srcDir, destDir string) error {
    76  	entries, err := mirror.ReadDir(srcDir)
    77  	if err != nil {
    78  		return err
    79  	}
    80  	for _, entry := range entries {
    81  		if entry.IsDir() {
    82  			childSrcDir := filepath.Join(srcDir, entry.Name())
    83  			childDestDir := filepath.Join(destDir, entry.Name())
    84  			logrus.Debugf("creating %s directory", childDestDir)
    85  			if err := os.Mkdir(childDestDir, 0777); err != nil {
    86  				return err
    87  			}
    88  			if err := unpack(childSrcDir, childDestDir); err != nil {
    89  				return err
    90  			}
    91  			continue
    92  		}
    93  		logrus.Debugf("creating %s file", filepath.Join(destDir, entry.Name()))
    94  		if err := unpackFile(filepath.Join(srcDir, entry.Name()), filepath.Join(destDir, entry.Name())); err != nil {
    95  			return err
    96  		}
    97  	}
    98  	return nil
    99  }
   100  
   101  func unpackFile(srcPath, destPath string) error {
   102  	srcFile, err := mirror.Open(srcPath)
   103  	if err != nil {
   104  		return err
   105  	}
   106  	defer srcFile.Close()
   107  	destFile, err := os.OpenFile(destPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0777)
   108  	if err != nil {
   109  		return err
   110  	}
   111  	defer destFile.Close()
   112  	if _, err := io.Copy(destFile, srcFile); err != nil {
   113  		return err
   114  	}
   115  	return nil
   116  }
   117  
   118  // UnpackTerraformBinary unpacks the terraform binary from the embedded data so that it can be run to create the
   119  // infrastructure for the cluster.
   120  func UnpackTerraformBinary(dir string) error {
   121  	if err := os.MkdirAll(dir, 0777); err != nil {
   122  		return err
   123  	}
   124  	return unpack("mirror/terraform", dir)
   125  }