Skip to content

Commit

Permalink
add rancher dashboard cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelbreno committed Sep 3, 2024
1 parent 6e81e6d commit cd5900c
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 7 deletions.
35 changes: 35 additions & 0 deletions cmd/release/cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"errors"
"fmt"
"os"
"strings"

"github.com/rancher/ecm-distro-tools/release/charts"
"github.com/rancher/ecm-distro-tools/release/k3s"
"github.com/rancher/ecm-distro-tools/release/rancher"
"github.com/rancher/ecm-distro-tools/repository"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -130,11 +132,44 @@ var updateChartsCmd = &cobra.Command{
},
}

var updateRancherCmd = &cobra.Command{
Use: "rancher",
Short: "Update rancher files",
}

var updateRancherDashboardCmd = &cobra.Command{
Use: "dashboard [version]",
Short: "Update Rancher's Dashboard and UI references and create a PR",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("expected at least one argument: [version]")
}

version := args[0]
versionTrimmed, _, _ := strings.Cut(version, "-rc")

dashboardRelease, found := rootConfig.Dashboard.Versions[versionTrimmed]
if !found {
return errors.New("verify your config file, version not found: " + version)
}

dashboardRelease.Tag = version

ctx := context.Background()

ghClient := repository.NewGithub(ctx, rootConfig.Auth.GithubToken)

return rancher.UpdateDashboardReferences(ctx, ghClient, &dashboardRelease, rootConfig.User)
},
}

func init() {
rootCmd.AddCommand(updateCmd)
updateCmd.AddCommand(updateChartsCmd)
updateCmd.AddCommand(updateK3sCmd)
updateK3sCmd.AddCommand(updateK3sReferencesCmd)
updateCmd.AddCommand(updateRancherCmd)
updateRancherCmd.AddCommand(updateRancherDashboardCmd)
}

func validateChartConfig() error {
Expand Down
13 changes: 8 additions & 5 deletions cmd/release/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,14 @@ type UIRelease struct {
}

type DashboardRelease struct {
DashboardRepoOwner string `json:"dashboard_repo_owner" validate:"required"`
DashboardRepoName string `json:"dashboard_repo_name"`
PreviousTag string `json:"previous_tag"`
ReleaseBranch string `json:"release_branch" validate:"required"`
DryRun bool `json:"dry_run"`
DashboardRepoOwner string `json:"dashboard_repo_owner" validate:"required"`
DashboardRepoName string `json:"dashboard_repo_name"`
PreviousTag string `json:"previous_tag"`
ReleaseBranch string `json:"release_branch" validate:"required"`
Tag string
RancherUpstreamURL string `json:"rancher_upstream_url" validate:"required"`
RancherReleaseBranch string `json:"rancher_release_branch" validate:"required"`
DryRun bool `json:"dry_run"`
}

// RKE2
Expand Down
90 changes: 88 additions & 2 deletions release/rancher/rancher.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"fmt"
htmlTemplate "html/template"
"io"
"io/ioutil"
"log"
"log/slog"
"net/http"
Expand All @@ -26,7 +25,9 @@ import (
"time"

"github.com/google/go-github/v39/github"
"github.com/rancher/ecm-distro-tools/cmd/release/config"
ecmConfig "github.com/rancher/ecm-distro-tools/cmd/release/config"
ecmExec "github.com/rancher/ecm-distro-tools/exec"
ecmHTTP "github.com/rancher/ecm-distro-tools/http"
"github.com/rancher/ecm-distro-tools/release"
"github.com/rancher/ecm-distro-tools/repository"
Expand Down Expand Up @@ -149,6 +150,47 @@ func GeneratePrimeArtifactsIndex(path string, ignoreVersions []string) error {
return os.WriteFile(filepath.Join(path, "index-prerelease.html"), preReleaseIndex, 0644)
}

func UpdateDashboardReferences(ctx context.Context, ghClient *github.Client, r *config.DashboardRelease, u *config.User) error {
if err := updateDashboardReferencesAndPush(r, u); err != nil {
return err
}

if r.DryRun {
fmt.Println("dry run, skipping creating PR")
return nil
}
return createDashboardReferencesPR(ctx, ghClient, r, u)
}

func updateDashboardReferencesAndPush(r *ecmConfig.DashboardRelease, u *ecmConfig.User) error {
fmt.Println("verifying if workspace dir exists")
funcMap := template.FuncMap{"replaceAll": strings.ReplaceAll}
fmt.Println("creating update dashboard references script template")
updateScriptOut, err := ecmExec.RunTemplatedScript("./", "replase_dash_ref.sh", updateDashboardReferencesScript, funcMap, r)
if err != nil {
fmt.Println("AAAAAAA")
return err
}
fmt.Println(updateScriptOut)
return nil
}

func createDashboardReferencesPR(ctx context.Context, ghClient *github.Client, r *ecmConfig.DashboardRelease, u *ecmConfig.User) error {
const repo = "rancher"

pull := &github.NewPullRequest{
Title: github.String(fmt.Sprintf("Bump Dashboard to `%s`", r.Tag)),
Base: github.String(r.ReleaseBranch),
Head: github.String(u.GithubUsername + ":update-build-refs-" + r.Tag),
MaintainerCanModify: github.Bool(true),
}

// creating a pr from your fork branch
_, _, err := ghClient.PullRequests.Create(ctx, r.DashboardRepoOwner, r.DashboardRepoName, pull)

return err
}

func generateArtifactsIndexContent(listBucket ListBucketResult, ignoreVersions map[string]bool) ArtifactsIndexContent {
indexContent := ArtifactsIndexContent{
GA: ArtifactsIndexContentGroup{
Expand Down Expand Up @@ -635,7 +677,7 @@ func (d imageDigest) String() string {
}

func getLinesFromReader(body io.Reader) ([]string, error) {
lines, err := ioutil.ReadAll(body)
lines, err := io.ReadAll(body)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -838,3 +880,47 @@ const checkRancherRCDepsTemplate = `{{- define "componentsFile" -}}
* {{ .Content }} ({{ .File }}, line {{ .Line }})
{{- end}}
{{ end }}`

const updateDashboardReferencesScript = `#!/bin/bash
set -ex
OS=$(uname -s)
DRY_RUN={{ .DryRun }}
BRANCH_NAME=update-build-refs-{{ .Tag }}
VERSION={{ .Tag }}
FILENAME=package/Dockerfile
# using ls | grep is not a good idea because it doesn't support non-alphanumeric filenames, but since we're only ever checking 'k3s' it isn't a problem https://www.shellcheck.net/wiki/SC2010
git remote -v | grep -w upstream || git remote add upstream {{ .RancherUpstreamURL }}
git fetch upstream
git stash
git branch -D "${BRANCH_NAME}" &>/dev/null || true
git checkout -B "${BRANCH_NAME}" upstream/{{.RancherReleaseBranch}}
# git clean -xfd
# Function to update the file
update_file() {
local sed_cmd
case ${OS} in
Darwin)
sed_cmd="sed -i '' "
;;
Linux)
sed_cmd="sed -i"
;;
*)
>&2 echo "$(OS) not supported yet"
exit 1
;;
esac
$sed_cmd "s/ENV CATTLE_UI_VERSION .*/ENV CATTLE_UI_VERSION ${VERSION#v}/" "$FILENAME"
$sed_cmd "s/ENV CATTLE_DASHBOARD_UI_VERSION .*/ENV CATTLE_DASHBOARD_UI_VERSION $VERSION/" "$FILENAME"
}
# Run the update function
update_file
git add $FILENAME
git commit --signoff -m "Update to Dashboard refs to ${VERSION}"
if [ "${DRY_RUN}" = false ]; then
git push --set-upstream origin "${BRANCH_NAME}" # run git remote -v for your origin
fi`

0 comments on commit cd5900c

Please sign in to comment.