-
Notifications
You must be signed in to change notification settings - Fork 0
/
libvirt.go
82 lines (73 loc) · 1.98 KB
/
libvirt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Package libvirt collects libvirt-specific configuration.
package libvirt
import (
"context"
"fmt"
"net/url"
"os"
"github.com/pkg/errors"
survey "gopkg.in/AlecAivazis/survey.v1"
"github.com/openshift/installer/pkg/asset"
"github.com/openshift/installer/pkg/rhcos"
"github.com/openshift/installer/pkg/types/libvirt"
)
const (
defaultNetworkIfName = "tt0"
defaultNetworkIPRange = "192.168.126.0/24"
)
// Platform collects libvirt-specific configuration.
func Platform() (*libvirt.Platform, error) {
uri, err := asset.GenerateUserProvidedAsset(
"Libvirt Connection URI",
&survey.Question{
Prompt: &survey.Input{
Message: "Libvirt Connection URI",
Help: "The libvirt connection URI to be used. This must be accessible from the running cluster.",
Default: "qemu+tcp://192.168.122.1/system",
},
Validate: survey.ComposeValidators(survey.Required, uriValidator),
},
"OPENSHIFT_INSTALL_LIBVIRT_URI",
)
if err != nil {
return nil, err
}
qcowImage, ok := os.LookupEnv("OPENSHIFT_INSTALL_LIBVIRT_IMAGE")
if ok {
err = validURI(qcowImage)
if err != nil {
return nil, errors.Wrap(err, "resolve OPENSHIFT_INSTALL_LIBVIRT_IMAGE")
}
} else {
qcowImage, err = rhcos.QEMU(context.TODO(), rhcos.DefaultChannel)
if err != nil {
return nil, errors.Wrap(err, "failed to fetch QEMU image URL")
}
}
return &libvirt.Platform{
Network: libvirt.Network{
IfName: defaultNetworkIfName,
IPRange: defaultNetworkIPRange,
},
DefaultMachinePlatform: &libvirt.MachinePool{
Image: qcowImage,
},
URI: uri,
}, nil
}
// uriValidator validates if the answer provided in prompt is a valid
// url and has non-empty scheme.
func uriValidator(ans interface{}) error {
return validURI(ans.(string))
}
// validURI validates if the URI is a valid URI with a non-empty scheme.
func validURI(uri string) error {
parsed, err := url.Parse(uri)
if err != nil {
return err
}
if parsed.Scheme == "" {
return fmt.Errorf("invalid URI %q (no scheme)", uri)
}
return nil
}