forked from openshift/installer
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Most of the changes to get the survey working (openshift#26)
initial install-config.yaml generation via the user survey
- Loading branch information
Showing
8 changed files
with
254 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package powervs | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"sort" | ||
"strings" | ||
|
||
survey "github.com/AlecAivazis/survey/v2" | ||
"github.com/AlecAivazis/survey/v2/core" | ||
"github.com/openshift/installer/pkg/types/powervs" | ||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// Platform collects powervs-specific configuration. | ||
func Platform() (*powervs.Platform, error) { | ||
regions := knownRegions() | ||
|
||
// TODO(cklokman): This section came from aws and transforms the response from knownRegions | ||
// into long and short regions to prompt the user for region select this section | ||
// need need to be different based on powervs's implementation of knownRegions | ||
// | ||
|
||
longRegions := make([]string, 0, len(regions)) | ||
shortRegions := make([]string, 0, len(regions)) | ||
for id, location := range regions { | ||
longRegions = append(longRegions, fmt.Sprintf("%s (%s)", id, location)) | ||
shortRegions = append(shortRegions, id) | ||
} | ||
|
||
var regionTransform survey.Transformer = func(ans interface{}) interface{} { | ||
switch v := ans.(type) { | ||
case core.OptionAnswer: | ||
return core.OptionAnswer{Value: strings.SplitN(v.Value, " ", 2)[0], Index: v.Index} | ||
case string: | ||
return strings.SplitN(v, " ", 2)[0] | ||
} | ||
return "" | ||
} | ||
|
||
ssn, err := GetSession() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var region string | ||
|
||
sessionRegion := ssn.Session.Region | ||
if sessionRegion != "" { | ||
if IsKnownRegion(sessionRegion) { | ||
region = sessionRegion | ||
} else { | ||
logrus.Warnf("Unrecognized Power VS region %s, ignoring IC_REGION", sessionRegion) | ||
} | ||
} | ||
|
||
sort.Strings(longRegions) | ||
sort.Strings(shortRegions) | ||
if region == "" { | ||
err = survey.Ask([]*survey.Question{ | ||
{ | ||
Prompt: &survey.Select{ | ||
Message: "Region", | ||
Help: "The Power VS region to be used for installation.", | ||
// Default: fmt.Sprintf("%s (%s)", defaultRegion, regions[defaultRegion]), | ||
Options: longRegions, | ||
}, | ||
Validate: survey.ComposeValidators(survey.Required, func(ans interface{}) error { | ||
choice := regionTransform(ans).(core.OptionAnswer).Value | ||
i := sort.SearchStrings(shortRegions, choice) | ||
if i == len(shortRegions) || shortRegions[i] != choice { | ||
return errors.Errorf("invalid region %q", choice) | ||
} | ||
return nil | ||
}), | ||
Transform: regionTransform, | ||
}, | ||
}, ®ion) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
zones := knownZones(region) | ||
defaultZone := zones[0] | ||
|
||
var zoneTransform survey.Transformer = func(ans interface{}) interface{} { | ||
switch v := ans.(type) { | ||
case core.OptionAnswer: | ||
return core.OptionAnswer{Value: strings.SplitN(v.Value, " ", 2)[0], Index: v.Index} | ||
case string: | ||
return strings.SplitN(v, " ", 2)[0] | ||
} | ||
return "" | ||
} | ||
|
||
var zone string | ||
err = survey.Ask([]*survey.Question{ | ||
{ | ||
Prompt: &survey.Select{ | ||
Message: "Zone", | ||
Help: "The powervs zone within the region to be used for installation.", | ||
Default: fmt.Sprintf("%s", defaultZone), | ||
Options: zones, | ||
}, | ||
Validate: survey.ComposeValidators(survey.Required, func(ans interface{}) error { | ||
choice := zoneTransform(ans).(core.OptionAnswer).Value | ||
i := sort.SearchStrings(zones, choice) | ||
if i == len(zones) || zones[i] != choice { | ||
return errors.Errorf("invalid zone %q", choice) | ||
} | ||
return nil | ||
}), | ||
Transform: zoneTransform, | ||
}, | ||
}, &zone) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var p powervs.Platform | ||
if osOverride := os.Getenv("OPENSHIFT_INSTALL_OS_IMAGE_OVERRIDE"); len(osOverride) != 0 { | ||
p.BootstrapOSImage = osOverride | ||
p.ClusterOSImage = osOverride | ||
} | ||
|
||
p.Region = region | ||
p.Zone = zone | ||
p.APIKey = ssn.Creds.APIKey | ||
p.UserID = ssn.Creds.UserID | ||
|
||
return &p, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package powervs | ||
|
||
import ( | ||
"github.com/openshift/installer/pkg/rhcos" | ||
) | ||
|
||
func knownRegions() map[string]string { | ||
|
||
regions := make(map[string]string) | ||
|
||
for _, region := range rhcos.PowerVSRegions { | ||
regions[region["name"]] = region["description"] | ||
} | ||
return regions | ||
} | ||
|
||
// IsKnownRegion return true is a specified region is Known to the installer. | ||
// A known region is subset of AWS regions and the regions where RHEL CoreOS images are published. | ||
func IsKnownRegion(region string) bool { | ||
if _, ok := knownRegions()[region]; ok { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
// Todo(cklokman): Need some form of error handing in this function... | ||
func knownZones(region string) []string { | ||
return rhcos.PowerVSZones[region] | ||
} | ||
|
||
// IsKnownZone return true is a specified zone is Known to the installer. | ||
func IsKnownZone(region string, zone string) bool { | ||
if _, ok := knownRegions()[region]; ok { | ||
zones := knownZones(region) | ||
for _, z := range zones { | ||
if z == zone { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package rhcos | ||
|
||
// TODO(cklokman): This entire file should be programatically generated similar to aws | ||
|
||
// PowerVSZones holds the zones cooresponding to each region found in PowerVS Regions | ||
// TODO(cklokman): These are fictional zones for testing | ||
var PowerVSZones = map[string][]string{ | ||
"us-south": []string{ | ||
"us-south-zone1", | ||
"us-south-zone2", | ||
}, | ||
} | ||
|
||
// PowerVSRegions holds the regions for IBM Power VS, and descriptions used during the survey | ||
// TODO(cklokman): These are fictional regtions for testing | ||
var PowerVSRegions = []map[string]string{ | ||
map[string]string{ | ||
"name": "us-south", | ||
"description": "This is the us-south test region.", | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters