Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloud-config template made configurable. #509

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cmd/image/qcow2ova/prep/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/image/qcow2ova/prep/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 24 additions & 1 deletion cmd/image/qcow2ova/qcow2ova.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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" {
Expand All @@ -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)
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions pkg/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ type imageCMDOptions struct {
TempDir string
PrepTemplate string
PrepTemplateDefault bool
CloudConfig string
CloudConfigDefault bool
OSPasswordSkip bool
//upload options
InstanceName string
Expand Down
Loading