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

     1  // Package baremetal collects bare metal specific configuration.
     2  package baremetal
     3  
     4  import (
     5  	"fmt"
     6  
     7  	"github.com/AlecAivazis/survey/v2"
     8  	"github.com/AlecAivazis/survey/v2/terminal"
     9  	"github.com/pkg/errors"
    10  
    11  	"github.com/openshift/installer/pkg/ipnet"
    12  	"github.com/openshift/installer/pkg/types/baremetal"
    13  	baremetaldefaults "github.com/openshift/installer/pkg/types/baremetal/defaults"
    14  )
    15  
    16  // Platform collects bare metal specific configuration.
    17  func Platform() (*baremetal.Platform, error) {
    18  	var provisioningNetworkCIDR, externalBridge, provisioningBridge, provisioningNetwork, provisioningNetworkInterface string
    19  	var parsedCIDR *ipnet.IPNet
    20  	var hosts []*baremetal.Host
    21  
    22  	survey.AskOne(&survey.Select{
    23  		Message: "Provisioning Network",
    24  		Help:    "Select whether the provisioning network will be managed, unmanaged, or disabled. In managed mode, the cluster deploys DHCP and TFTP services for PXE provisioning.",
    25  		Options: []string{"Managed", "Unmanaged", "Disabled"},
    26  		Default: "Managed",
    27  	}, &provisioningNetwork, nil)
    28  
    29  	if provisioningNetwork != string(baremetal.DisabledProvisioningNetwork) {
    30  		if err := survey.Ask([]*survey.Question{
    31  			{
    32  				Prompt: &survey.Input{
    33  					Message: "Provisioning Network CIDR",
    34  					Help:    "The network used for provisioning.",
    35  					Default: "172.22.0.0/24",
    36  				},
    37  				Validate: survey.ComposeValidators(survey.Required, ipNetValidator),
    38  			},
    39  		}, &provisioningNetworkCIDR); err != nil {
    40  			return nil, err
    41  		}
    42  		provNetCIDR, err := ipnet.ParseCIDR(provisioningNetworkCIDR)
    43  		if err != nil {
    44  			return nil, err
    45  		}
    46  		parsedCIDR = provNetCIDR
    47  
    48  		if err := survey.Ask([]*survey.Question{
    49  			{
    50  				Prompt: &survey.Input{
    51  					Message: "Provisioning bridge",
    52  					Help:    "Provisioning bridge is used to provision machines by the bootstrap virtual machine.",
    53  					Default: baremetaldefaults.ProvisioningBridge,
    54  				},
    55  			},
    56  		}, &provisioningBridge); err != nil {
    57  			return nil, err
    58  		}
    59  
    60  		if err := survey.Ask([]*survey.Question{
    61  			{
    62  				Prompt: &survey.Input{
    63  					Message: "Provisioning Network Interface",
    64  					Help:    "The name of the network interface on a control plane host connected to the provisioning network.",
    65  				},
    66  			},
    67  		}, &provisioningNetworkInterface); err != nil {
    68  			return nil, err
    69  		}
    70  	}
    71  
    72  	if err := survey.Ask([]*survey.Question{
    73  		{
    74  			Prompt: &survey.Input{
    75  				Message: "External bridge",
    76  				Help:    "External bridge is used for external communication by the bootstrap virtual machine.",
    77  				Default: baremetaldefaults.ExternalBridge,
    78  			},
    79  		},
    80  	}, &externalBridge); err != nil {
    81  		return nil, err
    82  	}
    83  
    84  	// Keep prompting for hosts
    85  	for {
    86  		var hostRole string
    87  		survey.AskOne(&survey.Select{
    88  			Message: "Add a Host:",
    89  			Options: []string{"control plane", "worker"},
    90  		}, &hostRole, nil)
    91  
    92  		var host *baremetal.Host
    93  		var err error
    94  		host, err = Host()
    95  		// Check for keyboard interrupt or else we'll loop forever
    96  		if errors.Is(err, terminal.InterruptErr) {
    97  			fmt.Println("interrupted - hosts were not added")
    98  			break
    99  		} else if err != nil {
   100  			fmt.Printf("invalid host - please try again")
   101  			continue
   102  		}
   103  		if hostRole == "control plane" {
   104  			host.Role = "master"
   105  		} else {
   106  			host.Role = hostRole
   107  		}
   108  		hosts = append(hosts, host)
   109  
   110  		more := false
   111  		survey.AskOne(&survey.Confirm{
   112  			Message: "Add another host?",
   113  		}, &more, nil)
   114  		if !more {
   115  			break
   116  		}
   117  	}
   118  
   119  	return &baremetal.Platform{
   120  		ExternalBridge:               externalBridge,
   121  		ProvisioningBridge:           provisioningBridge,
   122  		ProvisioningNetworkCIDR:      parsedCIDR,
   123  		ProvisioningNetworkInterface: provisioningNetworkInterface,
   124  		Hosts:                        hosts,
   125  	}, nil
   126  }
   127  
   128  // ipNetValidator validates for a valid IP
   129  func ipNetValidator(ans interface{}) error {
   130  	_, err := ipnet.ParseCIDR(ans.(string))
   131  	return err
   132  }