|
| 1 | +// Copyright 2021 The Codefresh Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package commands |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + |
| 21 | + "github.com/codefresh-io/cli-v2/pkg/log" |
| 22 | + "github.com/codefresh-io/cli-v2/pkg/runtime" |
| 23 | + "github.com/codefresh-io/cli-v2/pkg/store" |
| 24 | + "github.com/codefresh-io/cli-v2/pkg/util" |
| 25 | + |
| 26 | + "github.com/argoproj-labs/argocd-autopilot/pkg/application" |
| 27 | + "github.com/argoproj-labs/argocd-autopilot/pkg/fs" |
| 28 | + "github.com/argoproj-labs/argocd-autopilot/pkg/git" |
| 29 | + apstore "github.com/argoproj-labs/argocd-autopilot/pkg/store" |
| 30 | + wf "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow" |
| 31 | + wfv1alpha1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1" |
| 32 | + "github.com/go-git/go-billy/v5/memfs" |
| 33 | + "github.com/spf13/cobra" |
| 34 | + v1 "k8s.io/api/core/v1" |
| 35 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 36 | +) |
| 37 | + |
| 38 | +type ( |
| 39 | + GitSourceCreateOptions struct { |
| 40 | + insCloneOpts *git.CloneOptions |
| 41 | + gsCloneOpts *git.CloneOptions |
| 42 | + gsName string |
| 43 | + runtimeName string |
| 44 | + fullGsPath string |
| 45 | + } |
| 46 | +) |
| 47 | + |
| 48 | +func NewGitSourceCommand() *cobra.Command { |
| 49 | + cmd := &cobra.Command{ |
| 50 | + Use: "git-source", |
| 51 | + Short: "Manage git-sources of Codefresh runtimes", |
| 52 | + Run: func(cmd *cobra.Command, args []string) { |
| 53 | + cmd.HelpFunc()(cmd, args) |
| 54 | + exit(1) |
| 55 | + }, |
| 56 | + } |
| 57 | + |
| 58 | + cmd.AddCommand(NewGitSourceCreateCommand()) |
| 59 | + |
| 60 | + return cmd |
| 61 | +} |
| 62 | + |
| 63 | +func NewGitSourceCreateCommand() *cobra.Command { |
| 64 | + var ( |
| 65 | + insCloneOpts *git.CloneOptions |
| 66 | + gsCloneOpts *git.CloneOptions |
| 67 | + ) |
| 68 | + |
| 69 | + cmd := &cobra.Command{ |
| 70 | + Use: "create runtime_name git-source_name", |
| 71 | + Short: "add a new git-source to an existing runtime", |
| 72 | + Example: util.Doc(` |
| 73 | + <BIN> git-source create runtime_name git-source-name https://github.com/owner/repo-name/my-workflow |
| 74 | + `), |
| 75 | + PreRun: func(cmd *cobra.Command, args []string) { |
| 76 | + ctx := cmd.Context() |
| 77 | + |
| 78 | + if len(args) < 1 { |
| 79 | + log.G(ctx).Fatal("must enter runtime name") |
| 80 | + } |
| 81 | + |
| 82 | + if len(args) < 2 { |
| 83 | + log.G(ctx).Fatal("must enter git-source name") |
| 84 | + } |
| 85 | + |
| 86 | + if gsCloneOpts.Repo == "" { |
| 87 | + log.G(ctx).Fatal("must enter a valid value to --git-src-repo. Example: https://github.com/owner/repo-name/path/to/workflow") |
| 88 | + } |
| 89 | + |
| 90 | + if gsCloneOpts.Auth.Password == "" { |
| 91 | + gsCloneOpts.Auth.Password = insCloneOpts.Auth.Password |
| 92 | + } |
| 93 | + |
| 94 | + insCloneOpts.Parse() |
| 95 | + gsCloneOpts.Parse() |
| 96 | + }, |
| 97 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 98 | + ctx := cmd.Context() |
| 99 | + |
| 100 | + return RunCreateGitSource(ctx, &GitSourceCreateOptions{ |
| 101 | + insCloneOpts: insCloneOpts, |
| 102 | + gsCloneOpts: gsCloneOpts, |
| 103 | + gsName: args[1], |
| 104 | + runtimeName: args[0], |
| 105 | + fullGsPath: gsCloneOpts.Path(), |
| 106 | + }) |
| 107 | + }, |
| 108 | + } |
| 109 | + |
| 110 | + insCloneOpts = git.AddFlags(cmd, &git.AddFlagsOptions{ |
| 111 | + CreateIfNotExist: true, |
| 112 | + FS: memfs.New(), |
| 113 | + }) |
| 114 | + gsCloneOpts = git.AddFlags(cmd, &git.AddFlagsOptions{ |
| 115 | + Prefix: "git-src", |
| 116 | + Optional: true, |
| 117 | + CreateIfNotExist: true, |
| 118 | + FS: memfs.New(), |
| 119 | + }) |
| 120 | + |
| 121 | + return cmd |
| 122 | +} |
| 123 | + |
| 124 | +func RunCreateGitSource(ctx context.Context, opts *GitSourceCreateOptions) error { |
| 125 | + gsRepo, gsFs, err := opts.gsCloneOpts.GetRepo(ctx) |
| 126 | + if err != nil { |
| 127 | + return err |
| 128 | + } |
| 129 | + |
| 130 | + fi, err := gsFs.ReadDir(".") |
| 131 | + |
| 132 | + if err != nil { |
| 133 | + return fmt.Errorf("failed to read files in git-source repo. Err: %w", err) |
| 134 | + } |
| 135 | + |
| 136 | + if len(fi) == 0 { |
| 137 | + if err = createDemoWorkflowTemplate(gsFs, opts.gsName, opts.runtimeName); err != nil { |
| 138 | + return fmt.Errorf("failed to create demo workflowTemplate: %w", err) |
| 139 | + } |
| 140 | + |
| 141 | + _, err = gsRepo.Persist(ctx, &git.PushOptions{ |
| 142 | + CommitMsg: fmt.Sprintf("Created demo workflow template in %s Directory", opts.gsCloneOpts.Path()), |
| 143 | + }) |
| 144 | + |
| 145 | + if err != nil { |
| 146 | + return fmt.Errorf("failed to push changes. Err: %w", err) |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + appDef := &runtime.AppDef{ |
| 151 | + Name: opts.gsName, |
| 152 | + Type: application.AppTypeDirectory, |
| 153 | + URL: opts.gsCloneOpts.Repo, |
| 154 | + } |
| 155 | + if err := appDef.CreateApp(ctx, nil, opts.insCloneOpts, opts.runtimeName, store.Get().CFGitSourceType, nil); err != nil { |
| 156 | + return fmt.Errorf("failed to create git-source application. Err: %w", err) |
| 157 | + } |
| 158 | + |
| 159 | + log.G(ctx).Infof("done creating a new git-source: '%s'", opts.gsName) |
| 160 | + |
| 161 | + return nil |
| 162 | +} |
| 163 | + |
| 164 | +func createDemoWorkflowTemplate(gsFs fs.FS, gsName, runtimeName string) error { |
| 165 | + var err error |
| 166 | + |
| 167 | + gsPath := gsFs.Join(apstore.Default.AppsDir, gsName, runtimeName, "demo-wf-template.yaml") |
| 168 | + wfTemplate := &wfv1alpha1.WorkflowTemplate{ |
| 169 | + TypeMeta: metav1.TypeMeta{ |
| 170 | + Kind: wf.WorkflowTemplateKind, |
| 171 | + APIVersion: wfv1alpha1.SchemeGroupVersion.String(), |
| 172 | + }, |
| 173 | + ObjectMeta: metav1.ObjectMeta{ |
| 174 | + Name: "demo-workflow-template", |
| 175 | + Namespace: runtimeName, |
| 176 | + }, |
| 177 | + Spec: wfv1alpha1.WorkflowTemplateSpec{ |
| 178 | + WorkflowSpec: wfv1alpha1.WorkflowSpec{ |
| 179 | + Entrypoint: "whalesay", |
| 180 | + Templates: []wfv1alpha1.Template{ |
| 181 | + { |
| 182 | + Name: "whalesay", |
| 183 | + Container: &v1.Container{ |
| 184 | + Image: "docker/whalesay", |
| 185 | + Command: []string{"cowsay"}, |
| 186 | + Args: []string{"Hello World"}, |
| 187 | + }, |
| 188 | + }, |
| 189 | + }, |
| 190 | + }, |
| 191 | + }, |
| 192 | + } |
| 193 | + if err = gsFs.WriteYamls(gsPath, wfTemplate); err != nil { |
| 194 | + return err |
| 195 | + } |
| 196 | + |
| 197 | + return err |
| 198 | +} |
0 commit comments