|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/SwissDataScienceCenter/renku-dev-utils/pkg/github" |
| 9 | + ns "github.com/SwissDataScienceCenter/renku-dev-utils/pkg/namespace" |
| 10 | + "github.com/SwissDataScienceCenter/renku-dev-utils/pkg/renkuapi" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +var updateGlobalImagesCmd = &cobra.Command{ |
| 15 | + Use: "update-global-images", |
| 16 | + Short: "Updates the global images", |
| 17 | + Run: updateGlobalImages, |
| 18 | +} |
| 19 | + |
| 20 | +func updateGlobalImages(cmd *cobra.Command, args []string) { |
| 21 | + ctx := context.Background() |
| 22 | + |
| 23 | + release, err := cmd.Flags().GetString("release") |
| 24 | + if err != nil { |
| 25 | + fmt.Println(err) |
| 26 | + os.Exit(1) |
| 27 | + } |
| 28 | + |
| 29 | + if release == "" { |
| 30 | + cli, err := github.NewGitHubCLI("") |
| 31 | + if err != nil { |
| 32 | + fmt.Println(err) |
| 33 | + os.Exit(1) |
| 34 | + } |
| 35 | + release, err = cli.GetLatestRenkuRelease(ctx) |
| 36 | + if err != nil { |
| 37 | + fmt.Println(err) |
| 38 | + os.Exit(1) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + fmt.Printf("Renku release: %s\n", release) |
| 43 | + |
| 44 | + url, err := cmd.Flags().GetString("url") |
| 45 | + if err != nil { |
| 46 | + fmt.Println(err) |
| 47 | + os.Exit(1) |
| 48 | + } |
| 49 | + |
| 50 | + if url == "" { |
| 51 | + namespace, err := cmd.Flags().GetString("namespace") |
| 52 | + if err != nil { |
| 53 | + fmt.Println(err) |
| 54 | + os.Exit(1) |
| 55 | + } |
| 56 | + if namespace == "" { |
| 57 | + cli, err := github.NewGitHubCLI("") |
| 58 | + if err != nil { |
| 59 | + fmt.Println(err) |
| 60 | + os.Exit(1) |
| 61 | + } |
| 62 | + namespace, err = ns.FindCurrentNamespace(ctx, cli) |
| 63 | + if err != nil { |
| 64 | + fmt.Println(err) |
| 65 | + os.Exit(1) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + deploymentURL, err := ns.GetDeploymentURL(namespace) |
| 70 | + if err != nil { |
| 71 | + fmt.Println(err) |
| 72 | + os.Exit(1) |
| 73 | + } |
| 74 | + url = deploymentURL.String() |
| 75 | + } |
| 76 | + |
| 77 | + fmt.Printf("Renku URL: %s\n", url) |
| 78 | + |
| 79 | + rac, err := renkuapi.NewRenkuApiClient(url) |
| 80 | + if err != nil { |
| 81 | + fmt.Println(err) |
| 82 | + os.Exit(1) |
| 83 | + } |
| 84 | + |
| 85 | + if !rac.IsLoggedIn(ctx) { |
| 86 | + fmt.Println("Error: not logged in") |
| 87 | + showCmd := "rdu login" |
| 88 | + if url != "" { |
| 89 | + showCmd = showCmd + fmt.Sprintf(" --url %s", url) |
| 90 | + } |
| 91 | + if namespace != "" { |
| 92 | + showCmd = showCmd + fmt.Sprintf(" --namespace %s", namespace) |
| 93 | + } |
| 94 | + fmt.Printf("Please run \"%s\" before running this command\n", showCmd) |
| 95 | + os.Exit(1) |
| 96 | + } |
| 97 | + |
| 98 | + if !rac.IsAdmin(ctx) { |
| 99 | + fmt.Println("Error: not an admin") |
| 100 | + fmt.Println("Please make sure you are a Renku admin before running this command") |
| 101 | + fmt.Println("See: rdu make-me-admin --help") |
| 102 | + os.Exit(1) |
| 103 | + } |
| 104 | + |
| 105 | + rsc, err := rac.Session() |
| 106 | + if err != nil { |
| 107 | + fmt.Println(err) |
| 108 | + os.Exit(1) |
| 109 | + } |
| 110 | + |
| 111 | + envs, err := rsc.GetGlobalEnvironments(ctx) |
| 112 | + if err != nil { |
| 113 | + fmt.Println(err) |
| 114 | + os.Exit(1) |
| 115 | + } |
| 116 | + |
| 117 | + dryRun, err := cmd.Flags().GetBool("dry-run") |
| 118 | + if err != nil { |
| 119 | + fmt.Println(err) |
| 120 | + os.Exit(1) |
| 121 | + } |
| 122 | + |
| 123 | + err = rsc.UpdateGlobalImages(ctx, github.GetGlobalImages(), release, envs, dryRun) |
| 124 | + if err != nil { |
| 125 | + fmt.Println(err) |
| 126 | + os.Exit(1) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +func init() { |
| 131 | + updateGlobalImagesCmd.Flags().String("url", "", "instance URL") |
| 132 | + updateGlobalImagesCmd.Flags().StringP("namespace", "n", "", "k8s namespace") |
| 133 | + updateGlobalImagesCmd.Flags().String("release", "", "renku release") |
| 134 | + updateGlobalImagesCmd.Flags().Bool("dry-run", false, "dry run") |
| 135 | +} |
0 commit comments