Skip to content

Commit

Permalink
Merge pull request #63 from mkumatag/config-template
Browse files Browse the repository at this point in the history
Make template script configurable
  • Loading branch information
ltccci authored Dec 9, 2020
2 parents 309bc3a + 84d8d44 commit b485aea
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
4 changes: 2 additions & 2 deletions cmd/image/qcow2ova/prep/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// TODO: add a logic to make the package versions as an argument
var setup = `#!/bin/bash
var SetupTemplate = `#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
Expand Down Expand Up @@ -172,7 +172,7 @@ func Render(dist, rhnuser, rhnpasswd, rootpasswd string) (string, error) {
dist, rhnuser, rhnpasswd, rootpasswd,
}
var wr bytes.Buffer
t := template.Must(template.New("setup").Parse(setup))
t := template.Must(template.New("setup").Parse(SetupTemplate))
err := t.Execute(&wr, s)
if err != nil {
return "", fmt.Errorf("error while rendoring the script template: %v", err)
Expand Down
29 changes: 29 additions & 0 deletions cmd/image/qcow2ova/qcow2ova.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,36 @@ Examples:
# Converts the CentOS image from the local filesystem with OS password set
pvsadm image qcow2ova --image-name centos-82 --image-dist centos --os-password s0meC0mplexPassword --image-url /root/CentOS-8-GenericCloud-8.2.2004-20200611.2.ppc64le.qcow2
# Customize the image preparation script for RHEL/CentOS distro, e.g: add additional yum repository or packages, change name servers etc.
# Step 1 - Dump the default image preparation template
pvsadm image qcow2ova --prep-template-default > image-prep.template
# 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
`,
PreRunE: func(cmd *cobra.Command, args []string) error {
opt := pkg.ImageCMDOptions

if opt.PrepTemplateDefault {
fmt.Println(prep.SetupTemplate)
os.Exit(0)
}

// Override the prep.SetupTemplate if --prep-template supplied
if opt.PrepTemplate != "" {
if strings.ToLower(opt.ImageDist) == "coreos" {
return fmt.Errorf("--prep-template option is not supported for coreos distro")
} else {
klog.Info("Overriding with the user defined image preparation template.")
content, err := ioutil.ReadFile(opt.PrepTemplate)
if err != nil {
return err
}
prep.SetupTemplate = 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 @@ -209,6 +236,8 @@ func init() {
Cmd.Flags().StringVar(&pkg.ImageCMDOptions.RHNPassword, "rhn-password", "", "RedHat Subscription password. Required when Image distribution is rhel")
Cmd.Flags().StringVar(&pkg.ImageCMDOptions.OSPassword, "os-password", "", "Root user password, will auto-generate the 12 bits password(applicable only for redhat and cento distro)")
Cmd.Flags().StringVarP(&pkg.ImageCMDOptions.TempDir, "temp-dir", "t", os.TempDir(), "Scratch space to use for OVA generation")
Cmd.Flags().StringVar(&pkg.ImageCMDOptions.PrepTemplate, "prep-template", "", "Image preparation script template, use --prep-template-default to print the default template(supported distros: rhel and centos)")
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().MarkHidden("skip-preflight-checks")
_ = Cmd.MarkFlagRequired("image-name")
Expand Down
20 changes: 11 additions & 9 deletions pkg/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@ var ImageCMDOptions = &imageCMDOptions{}

type imageCMDOptions struct {
//qcow2ova options
ImageDist string
ImageName string
ImageSize uint64
ImageURL string
OSPassword string
PreflightSkip []string
RHNUser string
RHNPassword string
TempDir string
ImageDist string
ImageName string
ImageSize uint64
ImageURL string
OSPassword string
PreflightSkip []string
RHNUser string
RHNPassword string
TempDir string
PrepTemplate string
PrepTemplateDefault bool
//upload options
InstanceName string
Region string
Expand Down

0 comments on commit b485aea

Please sign in to comment.