github.com/openshift/installer@v1.4.17/pkg/asset/installconfig/platform.go (about)

     1  package installconfig
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"sort"
     7  
     8  	survey "github.com/AlecAivazis/survey/v2"
     9  	"github.com/AlecAivazis/survey/v2/core"
    10  	"github.com/pkg/errors"
    11  
    12  	"github.com/openshift/installer/pkg/asset"
    13  	awsconfig "github.com/openshift/installer/pkg/asset/installconfig/aws"
    14  	azureconfig "github.com/openshift/installer/pkg/asset/installconfig/azure"
    15  	baremetalconfig "github.com/openshift/installer/pkg/asset/installconfig/baremetal"
    16  	gcpconfig "github.com/openshift/installer/pkg/asset/installconfig/gcp"
    17  	ibmcloudconfig "github.com/openshift/installer/pkg/asset/installconfig/ibmcloud"
    18  	nutanixconfig "github.com/openshift/installer/pkg/asset/installconfig/nutanix"
    19  	openstackconfig "github.com/openshift/installer/pkg/asset/installconfig/openstack"
    20  	powervsconfig "github.com/openshift/installer/pkg/asset/installconfig/powervs"
    21  	vsphereconfig "github.com/openshift/installer/pkg/asset/installconfig/vsphere"
    22  	"github.com/openshift/installer/pkg/types"
    23  	"github.com/openshift/installer/pkg/types/aws"
    24  	"github.com/openshift/installer/pkg/types/azure"
    25  	"github.com/openshift/installer/pkg/types/baremetal"
    26  	"github.com/openshift/installer/pkg/types/external"
    27  	"github.com/openshift/installer/pkg/types/gcp"
    28  	"github.com/openshift/installer/pkg/types/ibmcloud"
    29  	"github.com/openshift/installer/pkg/types/none"
    30  	"github.com/openshift/installer/pkg/types/nutanix"
    31  	"github.com/openshift/installer/pkg/types/openstack"
    32  	"github.com/openshift/installer/pkg/types/ovirt"
    33  	"github.com/openshift/installer/pkg/types/powervs"
    34  	"github.com/openshift/installer/pkg/types/vsphere"
    35  )
    36  
    37  // Platform is an asset that queries the user for the platform on which to install
    38  // the cluster.
    39  type platform struct {
    40  	types.Platform
    41  }
    42  
    43  var _ asset.Asset = (*platform)(nil)
    44  
    45  // Dependencies returns no dependencies.
    46  func (a *platform) Dependencies() []asset.Asset {
    47  	return []asset.Asset{}
    48  }
    49  
    50  // Generate queries for input from the user.
    51  func (a *platform) Generate(ctx context.Context, _ asset.Parents) error {
    52  	platform, err := a.queryUserForPlatform()
    53  	if err != nil {
    54  		return err
    55  	}
    56  
    57  	switch platform {
    58  	case aws.Name:
    59  		a.AWS, err = awsconfig.Platform()
    60  		if err != nil {
    61  			return err
    62  		}
    63  	case azure.Name:
    64  		a.Azure, err = azureconfig.Platform()
    65  		if err != nil {
    66  			return err
    67  		}
    68  	case baremetal.Name:
    69  		a.BareMetal, err = baremetalconfig.Platform()
    70  		if err != nil {
    71  			return err
    72  		}
    73  	case gcp.Name:
    74  		a.GCP, err = gcpconfig.Platform()
    75  		if err != nil {
    76  			return err
    77  		}
    78  	case ibmcloud.Name:
    79  		a.IBMCloud, err = ibmcloudconfig.Platform()
    80  		if err != nil {
    81  			return err
    82  		}
    83  	case external.Name:
    84  		a.External = &external.Platform{}
    85  	case none.Name:
    86  		a.None = &none.Platform{}
    87  	case openstack.Name:
    88  		a.OpenStack, err = openstackconfig.Platform(ctx)
    89  		if err != nil {
    90  			return err
    91  		}
    92  	case ovirt.Name:
    93  		return fmt.Errorf("platform oVirt is no longer supported")
    94  	case vsphere.Name:
    95  		a.VSphere, err = vsphereconfig.Platform()
    96  		if err != nil {
    97  			return err
    98  		}
    99  	case powervs.Name:
   100  		a.PowerVS, err = powervsconfig.Platform()
   101  		if err != nil {
   102  			return err
   103  		}
   104  	case nutanix.Name:
   105  		a.Nutanix, err = nutanixconfig.Platform()
   106  		if err != nil {
   107  			return err
   108  		}
   109  	default:
   110  		return fmt.Errorf("unknown platform type %q", platform)
   111  	}
   112  
   113  	return nil
   114  }
   115  
   116  // Name returns the human-friendly name of the asset.
   117  func (a *platform) Name() string {
   118  	return "Platform"
   119  }
   120  
   121  func (a *platform) queryUserForPlatform() (platform string, err error) {
   122  	err = survey.Ask([]*survey.Question{
   123  		{
   124  			Prompt: &survey.Select{
   125  				Message: "Platform",
   126  				Options: types.PlatformNames,
   127  				Help:    "The platform on which the cluster will run.  For a full list of platforms, including those not supported by this wizard, see https://github.com/openshift/installer",
   128  			},
   129  			Validate: survey.ComposeValidators(survey.Required, func(ans interface{}) error {
   130  				choice := ans.(core.OptionAnswer).Value
   131  				i := sort.SearchStrings(types.PlatformNames, choice)
   132  				if i == len(types.PlatformNames) || types.PlatformNames[i] != choice {
   133  					return errors.Errorf("invalid platform %q", choice)
   134  				}
   135  				return nil
   136  			}),
   137  		},
   138  	}, &platform)
   139  	return
   140  }
   141  
   142  func (a *platform) CurrentName() string {
   143  	return a.Platform.Name()
   144  }