Skip to content

Commit 01ac8c6

Browse files
committed
Added cloud-config template option to override
1 parent d963e3c commit 01ac8c6

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

cmd/image/qcow2ova/prep/prepare.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ package prep
1616

1717
import (
1818
"fmt"
19-
"github.com/ppc64le-cloud/pvsadm/pkg/utils"
2019
"io/ioutil"
2120
"os"
2221
"path/filepath"
2322
"strings"
2423

24+
"github.com/ppc64le-cloud/pvsadm/pkg/utils"
25+
2526
"k8s.io/klog/v2"
2627
)
2728

@@ -101,7 +102,7 @@ func prepare(mnt, volume, dist, rhnuser, rhnpasswd, rootpasswd string) error {
101102
return err
102103
}
103104

104-
err = ioutil.WriteFile(filepath.Join(mnt, "/etc/cloud/cloud.cfg"), []byte(cloudConfig), 0644)
105+
err = ioutil.WriteFile(filepath.Join(mnt, "/etc/cloud/cloud.cfg"), []byte(CloudConfig), 0644)
105106
if err != nil {
106107
return err
107108
}

cmd/image/qcow2ova/prep/templates.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ mv /etc/resolv.conf.orig /etc/resolv.conf || true
8282
touch /.autorelabel
8383
`
8484

85-
var cloudConfig = `# latest file from cloud-init-22.1-1.el8.noarch
85+
var CloudConfig = `# latest file from cloud-init-22.1-1.el8.noarch
8686
users:
8787
- default
8888

cmd/image/qcow2ova/qcow2ova.go

+24-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ Examples:
6161
# Step 2 - Make the necessary changes to the above generated template file(bash shell script) - image-prep.template
6262
# Step 3 - Run the qcow2ova with the modified image preparation template
6363
pvsadm image qcow2ova --image-name centos-82 --image-dist centos --image-url /root/CentOS-8-GenericCloud-8.2.2004-20200611.2.ppc64le.qcow2 --prep-template image-prep.template
64+
65+
# Customize the cloud config and Convert image with user defined cloud config template.
66+
# Step 1 - Dump the default cloud config template
67+
pvsadm image qcow2ova --cloud-config-default > user_cloud.config
68+
# Step 2 - Make the necessary changes to the above generated template file - user_cloud.config
69+
# Step 3 - Run the qcow2ova with the modified cloud config template
70+
pvsadm image qcow2ova --image-name centos-82 --image-dist centos --image-url /root/CentOS-8-GenericCloud-8.2.2004-20200611.2.ppc64le.qcow2 --cloud-config user_cloud.config
71+
72+
6473
6574
Qcow2 images location:
6675
@@ -73,12 +82,16 @@ Qcow2 images location:
7382
`,
7483
PreRunE: func(cmd *cobra.Command, args []string) error {
7584
opt := pkg.ImageCMDOptions
76-
7785
if opt.PrepTemplateDefault {
7886
fmt.Println(prep.SetupTemplate)
7987
os.Exit(0)
8088
}
8189

90+
if opt.CloudConfigDefault {
91+
fmt.Println(prep.CloudConfig)
92+
os.Exit(0)
93+
}
94+
8295
// Override the prep.SetupTemplate if --prep-template supplied
8396
if opt.PrepTemplate != "" {
8497
if strings.ToLower(opt.ImageDist) == "coreos" {
@@ -92,7 +105,15 @@ Qcow2 images location:
92105
prep.SetupTemplate = string(content)
93106
}
94107
}
108+
if opt.CloudConfig != "" {
109+
klog.V(2).Info("Overriding with the user defined cloud config.")
110+
content, err := ioutil.ReadFile(opt.CloudConfig)
111+
if err != nil {
112+
return err
113+
}
114+
prep.CloudConfig = string(content)
95115

116+
}
96117
if !utils.Contains([]string{"rhel", "centos", "coreos"}, strings.ToLower(opt.ImageDist)) {
97118
klog.Errorln("--image-dist is a mandatory flag and one of these [rhel, centos, coreos]")
98119
os.Exit(1)
@@ -269,6 +290,8 @@ func init() {
269290
Cmd.Flags().BoolVar(&pkg.ImageCMDOptions.PrepTemplateDefault, "prep-template-default", false, "Prints the default image preparation script template, use --prep-template to set the custom template script(supported distros: rhel and centos)")
270291
Cmd.Flags().StringSliceVar(&pkg.ImageCMDOptions.PreflightSkip, "skip-preflight-checks", []string{}, "Skip the preflight checks(e.g: diskspace, platform, tools) - dev-only option")
271292
Cmd.Flags().BoolVar(&pkg.ImageCMDOptions.OSPasswordSkip, "skip-os-password", false, "Skip the root user password")
293+
Cmd.Flags().StringVar(&pkg.ImageCMDOptions.CloudConfig, "cloud-config", "", "Set the custom cloud config, use --cloud-config-default to print the default cloud config")
294+
Cmd.Flags().BoolVar(&pkg.ImageCMDOptions.CloudConfigDefault, "cloud-config-default", false, "Prints the default cloud config template, use --cloud-config to set the custom cloud config template")
272295
_ = Cmd.Flags().MarkHidden("skip-preflight-checks")
273296
_ = Cmd.MarkFlagRequired("image-name")
274297
_ = Cmd.MarkFlagRequired("image-url")

pkg/options.go

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ type imageCMDOptions struct {
5252
TempDir string
5353
PrepTemplate string
5454
PrepTemplateDefault bool
55+
CloudConfig string
56+
CloudConfigDefault bool
5557
OSPasswordSkip bool
5658
//upload options
5759
InstanceName string

0 commit comments

Comments
 (0)