Skip to content

Commit

Permalink
Merge pull request #468 from nicholasSUSE/release-charts-push
Browse files Browse the repository at this point in the history
Release charts push
  • Loading branch information
nicholasSUSE committed Aug 26, 2024
2 parents 21af474 + 3668c03 commit 6ccddb4
Show file tree
Hide file tree
Showing 7 changed files with 425 additions and 10 deletions.
35 changes: 33 additions & 2 deletions cmd/release/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ release -h
* All commands require a Github token (classic) with the following permissions:
* Be generated on behalf of an account with access to the `k3s-io/k3s` repo
* `repo`
* `write:packages`
* `write:packages`
* An SSH key, follow the Github [Documentation](https://docs.github.com/en/authentication/connecting-to-github-with-ssh) to generate one.
* A valid config file at `~/.ecm-distro-tools/config.json`

Expand All @@ -37,7 +37,7 @@ release tag system-agent-installer-k3s ga v1.29.2
```bash
$ release generate k3s tags v1.26.12
> failed to rebase and create tags: chown: changing ownership of '/home/go/.cache': Operation not permitted
failed to initialize build cache at /home/go/.cache: mkdir /home/go/.cache/00: permission denied
failed to initialize build cache at /home/go/.cache: mkdir /home/go/.cache/00: permission denied
```
Verify if the `$GOPATH/.cache` directory is owned by the same user that is running the command. If not, change the ownership of the directory:
```bash
Expand All @@ -56,6 +56,37 @@ release list rancher rc-deps 8c7bbcaabcfabb00b1c89e55ed4f68117f938262
release list rancher rc-deps v2.7.12-rc1
```

### Charts Release
#### Examples
##### Default workflow

```bash
release list charts 2.9
release update charts 2.9 rancher-vsphere-csi 104.0.1+up3.3.0-rancher2
release push charts 2.9

# to inspect before pushing
release push charts 2.9 debug
```

Configure your autocompletion on `zsh` or `bash` for `release chart` commands.

#### `zsh` configuration example:
```
./release completion zsh > completion.zsh
mv completion.zsh ~/.zsh/completion/completion.zsh
chmod +x ~/.zsh/completion/completion.zsh
```

Your `.zshrc` file must have something like the following:
```
# ECM-DISTRO-TOOLS
export PATH=$PATH:/<home_path>/.local/bin/ecm-distro-tools
source ~/.zsh/completion/completion.zsh
fpath=(~/.zsh/completion $fpath)
autoload -Uz compinit && compinit
```

## Contributions

* File Issue with details of the problem, feature request, etc.
Expand Down
20 changes: 17 additions & 3 deletions cmd/release/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"os"

"github.com/rancher/ecm-distro-tools/release/charts"
"github.com/rancher/ecm-distro-tools/release/rancher"
Expand Down Expand Up @@ -45,13 +46,26 @@ var rancherListRCDepsSubCmd = &cobra.Command{
}

var chartsListSubCmd = &cobra.Command{
Use: "charts [branch] [charts](optional)",
Short: "List Charts assets versions state for release process",
Use: "charts [branch-line] [charts](optional)",
Short: "List Charts assets versions state for release process",
Example: "release list charts 2.9",
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if err := validateChartConfig(); err != nil {
fmt.Println(err)
os.Exit(1)
}

if len(args) == 0 {
return rootConfig.Charts.BranchLines, cobra.ShellCompDirectiveNoFileComp
}

return nil, cobra.ShellCompDirectiveNoFileComp
},
RunE: func(cmd *cobra.Command, args []string) error {
var branch, chart string

if len(args) < 1 {
return errors.New("expected at least one argument: [branch]")
return errors.New("expected at least one argument: [branch-line]")
}
branch = args[0]

Expand Down
59 changes: 59 additions & 0 deletions cmd/release/cmd/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package cmd
import (
"context"
"errors"
"fmt"
"os"

"github.com/rancher/ecm-distro-tools/release/charts"
"github.com/rancher/ecm-distro-tools/release/k3s"
"github.com/rancher/ecm-distro-tools/repository"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -37,8 +40,64 @@ var pushK3sTagsCmd = &cobra.Command{
},
}

var pushChartsCmd = &cobra.Command{
Use: "charts [branch-line] [debug (optional)]",
Short: "Push charts updates to remote upstream charts repository",
Example: "release push charts 2.9",
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if err := validateChartConfig(); err != nil {
fmt.Println(err)
os.Exit(1)
}

if len(args) == 0 {
return rootConfig.Charts.BranchLines, cobra.ShellCompDirectiveNoFileComp
}

return nil, cobra.ShellCompDirectiveNoFileComp
},
Args: func(cmd *cobra.Command, args []string) error {
if err := validateChartConfig(); err != nil {
fmt.Println(err)
os.Exit(1)
}

if len(args) < 1 {
return errors.New("expected 1 argument: [branch-line]")
}

if found := charts.IsBranchAvailable(args[0], rootConfig.Charts.BranchLines); !found {
return errors.New("release branch not available: " + args[0])
}

return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
// arguments
releaseBranch := charts.MountReleaseBranch(args[0])
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
return err
}

token := rootConfig.Auth.GithubToken

ctx := context.Background()
ghc := repository.NewGithub(ctx, token)

prURL, err := charts.Push(ctx, rootConfig.Charts, rootConfig.User, ghc, releaseBranch, token, debug)
if err != nil {
return err
}

fmt.Println("Pull request created: " + prURL)
return nil
},
}

func init() {
rootCmd.AddCommand(pushCmd)
pushCmd.AddCommand(pushK3sCmd)
pushCmd.AddCommand(pushChartsCmd)
pushK3sCmd.AddCommand(pushK3sTagsCmd)
}
32 changes: 32 additions & 0 deletions exec/exec.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package exec

import (
"bufio"
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"text/template"
)

Expand Down Expand Up @@ -49,3 +52,32 @@ func RunTemplatedScript(dir, fileName, scriptTemplate string, funcMap template.F
}
return output, nil
}

// UserInput will ask for user input with a given title
func UserInput(title string) bool {
fmt.Println(title)

reader := bufio.NewReader(os.Stdin)
fmt.Print("([Y]es/[N]o/[A]bort) Enter: ")

input, err := reader.ReadString('\n')
if err != nil {
fmt.Println(err)
os.Exit(1)
}
input = strings.TrimSpace(input)
input = strings.ToLower(input)

// Check user input
if input == "yes" || input == "y" {
fmt.Printf("Continue...\n___\n\n")
return true
}

if input == "a" || input == "abort" {
os.Exit(0)
}

fmt.Println("Stop.")
return false
}
13 changes: 9 additions & 4 deletions release/charts/chart_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ type asset struct {

// ChartArgs will return the list of available charts in the current branch
func ChartArgs(ctx context.Context, c *config.ChartsRelease) ([]string, error) {
var dirs []string

assets := filepath.Join(c.Workspace, "assets")

// Read the directory assets contents
files, err := os.ReadDir(assets)
if err != nil {
return nil, err
}

var dirs []string
for _, file := range files {
if file.IsDir() {
dirs = append(dirs, file.Name())
Expand Down Expand Up @@ -66,6 +66,11 @@ func VersionArgs(ctx context.Context, c *config.ChartsRelease, ch string) ([]str
return versions, nil
}

// MountReleaseBranch will mount the release branch name from the line provided
func MountReleaseBranch(line string) string {
return "release-v" + line
}

// IsBranchAvailable will check if the branch line exists
func IsBranchAvailable(branch string, availableBranches []string) bool {
for _, b := range availableBranches {
Expand Down Expand Up @@ -94,14 +99,14 @@ func IsChartAvailable(ctx context.Context, conf *config.ChartsRelease, ch string
}

// IsVersionAvailable exists to be released or forward ported
func IsVersionAvailable(ctx context.Context, conf *config.ChartsRelease, ch, v string) (bool, error) {
func IsVersionAvailable(ctx context.Context, conf *config.ChartsRelease, ch, version string) (bool, error) {
availableVersions, err := VersionArgs(ctx, conf, ch)
if err != nil {
return false, err
}

for _, c := range availableVersions {
if c == v {
if c == version {
return true, nil
}
}
Expand Down
Loading

0 comments on commit 6ccddb4

Please sign in to comment.