From 01ac8c6eaa10e80f06ab5cdfea8de1f22f379ef4 Mon Sep 17 00:00:00 2001 From: KeerthanaAP Date: Tue, 7 Nov 2023 10:56:19 +0530 Subject: [PATCH] Added cloud-config template option to override --- cmd/image/qcow2ova/prep/prepare.go | 5 +++-- cmd/image/qcow2ova/prep/templates.go | 2 +- cmd/image/qcow2ova/qcow2ova.go | 25 ++++++++++++++++++++++++- pkg/options.go | 2 ++ 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/cmd/image/qcow2ova/prep/prepare.go b/cmd/image/qcow2ova/prep/prepare.go index 06d874f8..b2b760c1 100644 --- a/cmd/image/qcow2ova/prep/prepare.go +++ b/cmd/image/qcow2ova/prep/prepare.go @@ -16,12 +16,13 @@ package prep import ( "fmt" - "github.com/ppc64le-cloud/pvsadm/pkg/utils" "io/ioutil" "os" "path/filepath" "strings" + "github.com/ppc64le-cloud/pvsadm/pkg/utils" + "k8s.io/klog/v2" ) @@ -101,7 +102,7 @@ func prepare(mnt, volume, dist, rhnuser, rhnpasswd, rootpasswd string) error { return err } - err = ioutil.WriteFile(filepath.Join(mnt, "/etc/cloud/cloud.cfg"), []byte(cloudConfig), 0644) + err = ioutil.WriteFile(filepath.Join(mnt, "/etc/cloud/cloud.cfg"), []byte(CloudConfig), 0644) if err != nil { return err } diff --git a/cmd/image/qcow2ova/prep/templates.go b/cmd/image/qcow2ova/prep/templates.go index 2ffa08fd..0fd39fff 100644 --- a/cmd/image/qcow2ova/prep/templates.go +++ b/cmd/image/qcow2ova/prep/templates.go @@ -82,7 +82,7 @@ mv /etc/resolv.conf.orig /etc/resolv.conf || true touch /.autorelabel ` -var cloudConfig = `# latest file from cloud-init-22.1-1.el8.noarch +var CloudConfig = `# latest file from cloud-init-22.1-1.el8.noarch users: - default diff --git a/cmd/image/qcow2ova/qcow2ova.go b/cmd/image/qcow2ova/qcow2ova.go index 48b3f1c7..42406ecc 100644 --- a/cmd/image/qcow2ova/qcow2ova.go +++ b/cmd/image/qcow2ova/qcow2ova.go @@ -61,6 +61,15 @@ Examples: # Step 2 - Make the necessary changes to the above generated template file(bash shell script) - image-prep.template # Step 3 - Run the qcow2ova with the modified image preparation template 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 + + # Customize the cloud config and Convert image with user defined cloud config template. + # Step 1 - Dump the default cloud config template + pvsadm image qcow2ova --cloud-config-default > user_cloud.config + # Step 2 - Make the necessary changes to the above generated template file - user_cloud.config + # Step 3 - Run the qcow2ova with the modified cloud config template + 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 + + Qcow2 images location: @@ -73,12 +82,16 @@ Qcow2 images location: `, PreRunE: func(cmd *cobra.Command, args []string) error { opt := pkg.ImageCMDOptions - if opt.PrepTemplateDefault { fmt.Println(prep.SetupTemplate) os.Exit(0) } + if opt.CloudConfigDefault { + fmt.Println(prep.CloudConfig) + os.Exit(0) + } + // Override the prep.SetupTemplate if --prep-template supplied if opt.PrepTemplate != "" { if strings.ToLower(opt.ImageDist) == "coreos" { @@ -92,7 +105,15 @@ Qcow2 images location: prep.SetupTemplate = string(content) } } + if opt.CloudConfig != "" { + klog.V(2).Info("Overriding with the user defined cloud config.") + content, err := ioutil.ReadFile(opt.CloudConfig) + if err != nil { + return err + } + prep.CloudConfig = string(content) + } if !utils.Contains([]string{"rhel", "centos", "coreos"}, strings.ToLower(opt.ImageDist)) { klog.Errorln("--image-dist is a mandatory flag and one of these [rhel, centos, coreos]") os.Exit(1) @@ -269,6 +290,8 @@ func init() { 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)") Cmd.Flags().StringSliceVar(&pkg.ImageCMDOptions.PreflightSkip, "skip-preflight-checks", []string{}, "Skip the preflight checks(e.g: diskspace, platform, tools) - dev-only option") Cmd.Flags().BoolVar(&pkg.ImageCMDOptions.OSPasswordSkip, "skip-os-password", false, "Skip the root user password") + Cmd.Flags().StringVar(&pkg.ImageCMDOptions.CloudConfig, "cloud-config", "", "Set the custom cloud config, use --cloud-config-default to print the default cloud config") + 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") _ = Cmd.Flags().MarkHidden("skip-preflight-checks") _ = Cmd.MarkFlagRequired("image-name") _ = Cmd.MarkFlagRequired("image-url") diff --git a/pkg/options.go b/pkg/options.go index 9f0cf9a8..bbff8dfe 100644 --- a/pkg/options.go +++ b/pkg/options.go @@ -52,6 +52,8 @@ type imageCMDOptions struct { TempDir string PrepTemplate string PrepTemplateDefault bool + CloudConfig string + CloudConfigDefault bool OSPasswordSkip bool //upload options InstanceName string