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

config validator, custom location and flag #462

Merged
merged 14 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from 10 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
55 changes: 35 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,57 @@

ECM Distro Tools is a collection of utilities that provide for easier administration, management, and interaction with the great Rancher ecosystems, including RKE2 and K3s.

## Building
## Installation

There's a mix of code in this repository. The shell scripts and shell libraries reside in the `bin` directory and are ready to use. The Go programs are rooted in the `cmd` directory and need to be compiled.
The easiest way to install a single utility is to go to the release page, choose the release you want, and download the utility for your operation system and architecture.

To compile the programs run the following in the root of the project:
### Install Script

To install all executables and shell libraries, run the install script as follows:

**Install the latest version**
```sh
make all
curl -sfL https://raw.githubusercontent.com/rancher/ecm-distro-tools/master/install.sh | sh -
```
**Install a specific version**
```sh
curl -sfL https://raw.githubusercontent.com/rancher/ecm-distro-tools/master/install.sh | ECM_VERSION=v0.31.2 sh -
```

To compile the container image locally:
This will download all binaries and shell libraries and install them to `/usr/local/bin/ecm-distro-tools`. You'll need to add that directory to your path after installation.

```sh
docker build . -t rancher/ecm-distro-tools

## Release CLI
### Configuration
**New Configuration File**
```bash
release config gen > $HOME/.ecm-distro-tools/config.json
```
**Load config from custom path**
```bash
release config view -c ./config.json
```
**Load config from string**
```bash
release generate rancher missing-images-list v2.7.15 -C '{"rancher": { "versions": {"v2.7.15": {"check_images": ["rancher/rancher:v2.7.15"]}}}}' -i "https://prime.ribs.rancher.io/rancher/v2.7.15/rancher-images.txt" --ignore-validate
```

## Installation

The easiest way to install a single utility is to go to the release page, choose the release you want, and download the utility for your operation system and architecture.
## Building

### Install Script
There's a mix of code in this repository. The shell scripts and shell libraries reside in the `bin` directory and are ready to use. The Go programs are rooted in the `cmd` directory and need to be compiled.

To install all executables and shell libraries, run the install script as follows:
To compile the programs run the following in the root of the project:

```sh
# to install the latest version
curl -sfL https://raw.githubusercontent.com/rancher/ecm-distro-tools/master/install.sh | sh -
# or
./install.sh
# to install a specific version
curl -sfL https://raw.githubusercontent.com/rancher/ecm-distro-tools/master/install.sh | ECM_VERSION=v0.31.2 sh -
# or
./install.sh v0.31.2
make all
```

This will download all binaries and shell libraries and install them to `/usr/local/bin/ecm-distro-tools`. You'll need to add that directory to your path after installation.
To compile the container image locally:

```sh
docker build . -t rancher/ecm-distro-tools
```

## Utility Index

Expand Down
21 changes: 4 additions & 17 deletions cmd/release/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@ var genConfigSubCmd = &cobra.Command{
Use: "gen",
Short: "Generates a config file in the default location if it doesn't exists",
RunE: func(cmd *cobra.Command, args []string) error {
if err := config.Generate(); err != nil {
conf, err := config.ExampleConfig()
if err != nil {
return err
}

fmt.Println("config generated")
fmt.Println("to view it, run: release config view")
fmt.Println("to edit it, run: release config edit")

fmt.Println(conf)
return nil
},
}
Expand All @@ -42,7 +39,7 @@ var editConfigSubCmd = &cobra.Command{
Short: "Open the config file in your default editor",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error {
return config.OpenOnEditor()
return config.OpenOnEditor(configFile)
},
}

Expand All @@ -52,14 +49,4 @@ func init() {
configCmd.AddCommand(genConfigSubCmd)
configCmd.AddCommand(viewConfigSubCmd)
configCmd.AddCommand(editConfigSubCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// configCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// configCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
2 changes: 1 addition & 1 deletion cmd/release/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func init() {
}

// rancher generate missing-images-list
rancherGenerateMissingImagesListSubCmd.Flags().IntVarP(&concurrencyLimit, "concurrency-limit", "c", 3, "Concurrency Limit")
rancherGenerateMissingImagesListSubCmd.Flags().IntVarP(&concurrencyLimit, "concurrency-limit", "l", 3, "Concurrency Limit")
rancherGenerateMissingImagesListSubCmd.Flags().BoolVarP(&rancherMissingImagesJSONOutput, "json", "j", false, "JSON Output")
rancherGenerateMissingImagesListSubCmd.Flags().StringVarP(&imagesListURL, "images-list-url", "i", "", "URL of the artifact containing all images for a given version 'rancher-images.txt' (required)")
if err := rancherGenerateMissingImagesListSubCmd.MarkFlagRequired("images-list-url"); err != nil {
Expand Down
57 changes: 39 additions & 18 deletions cmd/release/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package cmd

import (
"fmt"
"log"
"os"
"strings"

"github.com/rancher/ecm-distro-tools/cmd/release/config"
"github.com/spf13/cobra"
)

var dryRun *bool
var rootConfig *config.Config
var (
debug bool
dryRun bool
ignoreValidate bool
rootConfig *config.Config
configFile string
stringConfig string
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Expand All @@ -21,9 +28,9 @@ var rootCmd = &cobra.Command{
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
cobra.OnInitialize(initConfig)
if err := rootCmd.Execute(); err != nil {
fmt.Println("error: " + err.Error())
os.Exit(1)
log.Fatal(err)
tashima42 marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -32,26 +39,40 @@ func SetVersion(version string) {
}

func init() {
rootCmd.PersistentFlags().BoolP("debug", "d", false, "Debug")

configPath, err := config.DefaultConfigPath()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "D", false, "Debug")
briandowns marked this conversation as resolved.
Show resolved Hide resolved
rootCmd.PersistentFlags().BoolVarP(&dryRun, "dry-run", "R", false, "Dry Run")
rootCmd.PersistentFlags().BoolVarP(&ignoreValidate, "ignore-validate", "I", false, "Ignore the validate config step")
rootCmd.PersistentFlags().StringVarP(&configFile, "config-file", "c", "$HOME/.ecm-distro-tools/config.json", "Path for the config.json file")
rootCmd.PersistentFlags().StringVarP(&stringConfig, "config", "C", "", "JSON config string")
}

func initConfig() {
if len(os.Args) >= 2 {
if os.Args[1] == "config" && os.Args[2] == "gen" {
fmt.Println("running release config gen, skipping config load")
return
}
}
conf, err := config.Load(configPath)
if err != nil {
fmt.Println("failed to load config, use 'release config gen' to create a new one at: " + configPath)
fmt.Println(err)
os.Exit(1)
var conf *config.Config
var err error
if stringConfig != "" {
conf, err = config.Read(strings.NewReader(stringConfig))
if err != nil {
panic(err)
tashima42 marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
configFile = os.ExpandEnv(configFile)
conf, err = config.Load(configFile)
if err != nil {
log.Println("failed to load config, use 'release config gen' to create a new one at: " + configFile)
panic(err)
}
}

rootConfig = conf

if !ignoreValidate {
if err := rootConfig.Validate(); err != nil {
panic(err)
}
}
}
8 changes: 3 additions & 5 deletions cmd/release/cmd/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ var rke2TagSubCmd = &cobra.Command{

switch args[0] {
case "image-build-base":
if err := rke2.ImageBuildBaseRelease(ctx, client, *tagRKE2Flags.AlpineVersion, *dryRun); err != nil {
if err := rke2.ImageBuildBaseRelease(ctx, client, *tagRKE2Flags.AlpineVersion, dryRun); err != nil {
return err
}
case "image-build-kubernetes":
now := time.Now().UTC().Format("20060102")
suffix := "-rke2" + *tagRKE2Flags.ReleaseVersion + "-build" + now

if *dryRun {
if dryRun {
fmt.Println("dry-run:")
for _, version := range rootConfig.RKE2.Versions {
fmt.Println("\t" + version + suffix)
Expand Down Expand Up @@ -111,7 +111,7 @@ var rke2TagSubCmd = &cobra.Command{
rpmTag = fmt.Sprintf("+rke2%s-rc%s.%s.%d", *tagRKE2Flags.ReleaseVersion, *tagRKE2Flags.RCVersion, args[1], *tagRKE2Flags.RPMVersion)
}

if *dryRun {
if dryRun {
fmt.Print("(dry-run)\n\nTagging github.com/rancher/rke2-packaging:\n\n")
for _, version := range rootConfig.RKE2.Versions {
fmt.Println("\t" + version + rpmTag)
Expand Down Expand Up @@ -212,8 +212,6 @@ func init() {
tagCmd.AddCommand(rancherTagSubCmd)
tagCmd.AddCommand(systemAgentInstallerK3sTagSubCmd)

dryRun = tagCmd.PersistentFlags().BoolP("dry-run", "r", false, "dry run")

// rke2
tagRKE2Flags.AlpineVersion = rke2TagSubCmd.Flags().StringP("alpine-version", "a", "", "Alpine version")
tagRKE2Flags.ReleaseVersion = rke2TagSubCmd.Flags().StringP("release-version", "r", "r1", "Release version")
Expand Down
Loading
Loading