From 4221f363fb534b0093e28672a4680865f702886a Mon Sep 17 00:00:00 2001 From: elad-codefresh <82316166+elad-codefresh@users.noreply.github.com> Date: Tue, 10 Aug 2021 13:46:04 +0300 Subject: [PATCH] CR-5108 (#50) * final * bump * git-source * new file * corrections * lint * Delete yarn.lock * Delete .yarn-integrity * after codegen * gs using path param * removed redundant * name * example * without redundant condition * . * added files * bump * . * gitSourceOptions * go.mod no replace * optional * with --git-src-repo check * example in error * Use: * Join * removed redundant * after codegen * tested * bump --- Makefile | 2 +- cmd/commands/git-source.go | 198 ++++++++++++++++++++++ cmd/commands/root.go | 1 + cmd/commands/runtime.go | 78 ++------- docs/commands/cli-v2.md | 1 + docs/commands/cli-v2_git-source.md | 28 +++ docs/commands/cli-v2_git-source_create.md | 41 +++++ docs/commands/cli-v2_runtime_list.md | 2 +- docs/releases/release_notes.md | 4 +- go.mod | 5 +- go.sum | 3 +- manifests/runtime.yaml | 2 +- 12 files changed, 289 insertions(+), 76 deletions(-) create mode 100644 cmd/commands/git-source.go create mode 100644 docs/commands/cli-v2_git-source.md create mode 100644 docs/commands/cli-v2_git-source_create.md diff --git a/Makefile b/Makefile index 90cc895f5..fff0337d0 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -VERSION=v0.0.55 +VERSION=v0.0.56 OUT_DIR=dist YEAR?=$(shell date +"%Y") diff --git a/cmd/commands/git-source.go b/cmd/commands/git-source.go new file mode 100644 index 000000000..4307919aa --- /dev/null +++ b/cmd/commands/git-source.go @@ -0,0 +1,198 @@ +// Copyright 2021 The Codefresh Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package commands + +import ( + "context" + "fmt" + + "github.com/codefresh-io/cli-v2/pkg/log" + "github.com/codefresh-io/cli-v2/pkg/runtime" + "github.com/codefresh-io/cli-v2/pkg/store" + "github.com/codefresh-io/cli-v2/pkg/util" + + "github.com/argoproj-labs/argocd-autopilot/pkg/application" + "github.com/argoproj-labs/argocd-autopilot/pkg/fs" + "github.com/argoproj-labs/argocd-autopilot/pkg/git" + apstore "github.com/argoproj-labs/argocd-autopilot/pkg/store" + wf "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow" + wfv1alpha1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1" + "github.com/go-git/go-billy/v5/memfs" + "github.com/spf13/cobra" + v1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +type ( + GitSourceCreateOptions struct { + insCloneOpts *git.CloneOptions + gsCloneOpts *git.CloneOptions + gsName string + runtimeName string + fullGsPath string + } +) + +func NewGitSourceCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "git-source", + Short: "Manage git-sources of Codefresh runtimes", + Run: func(cmd *cobra.Command, args []string) { + cmd.HelpFunc()(cmd, args) + exit(1) + }, + } + + cmd.AddCommand(NewGitSourceCreateCommand()) + + return cmd +} + +func NewGitSourceCreateCommand() *cobra.Command { + var ( + insCloneOpts *git.CloneOptions + gsCloneOpts *git.CloneOptions + ) + + cmd := &cobra.Command{ + Use: "create runtime_name git-source_name", + Short: "add a new git-source to an existing runtime", + Example: util.Doc(` + git-source create runtime_name git-source-name https://github.com/owner/repo-name/my-workflow + `), + PreRun: func(cmd *cobra.Command, args []string) { + ctx := cmd.Context() + + if len(args) < 1 { + log.G(ctx).Fatal("must enter runtime name") + } + + if len(args) < 2 { + log.G(ctx).Fatal("must enter git-source name") + } + + if gsCloneOpts.Repo == "" { + log.G(ctx).Fatal("must enter a valid value to --git-src-repo. Example: https://github.com/owner/repo-name/path/to/workflow") + } + + if gsCloneOpts.Auth.Password == "" { + gsCloneOpts.Auth.Password = insCloneOpts.Auth.Password + } + + insCloneOpts.Parse() + gsCloneOpts.Parse() + }, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := cmd.Context() + + return RunCreateGitSource(ctx, &GitSourceCreateOptions{ + insCloneOpts: insCloneOpts, + gsCloneOpts: gsCloneOpts, + gsName: args[1], + runtimeName: args[0], + fullGsPath: gsCloneOpts.Path(), + }) + }, + } + + insCloneOpts = git.AddFlags(cmd, &git.AddFlagsOptions{ + CreateIfNotExist: true, + FS: memfs.New(), + }) + gsCloneOpts = git.AddFlags(cmd, &git.AddFlagsOptions{ + Prefix: "git-src", + Optional: true, + CreateIfNotExist: true, + FS: memfs.New(), + }) + + return cmd +} + +func RunCreateGitSource(ctx context.Context, opts *GitSourceCreateOptions) error { + gsRepo, gsFs, err := opts.gsCloneOpts.GetRepo(ctx) + if err != nil { + return err + } + + fi, err := gsFs.ReadDir(".") + + if err != nil { + return fmt.Errorf("failed to read files in git-source repo. Err: %w", err) + } + + if len(fi) == 0 { + if err = createDemoWorkflowTemplate(gsFs, opts.gsName, opts.runtimeName); err != nil { + return fmt.Errorf("failed to create demo workflowTemplate: %w", err) + } + + _, err = gsRepo.Persist(ctx, &git.PushOptions{ + CommitMsg: fmt.Sprintf("Created demo workflow template in %s Directory", opts.gsCloneOpts.Path()), + }) + + if err != nil { + return fmt.Errorf("failed to push changes. Err: %w", err) + } + } + + appDef := &runtime.AppDef{ + Name: opts.gsName, + Type: application.AppTypeDirectory, + URL: opts.gsCloneOpts.Repo, + } + if err := appDef.CreateApp(ctx, nil, opts.insCloneOpts, opts.runtimeName, store.Get().CFGitSourceType, nil); err != nil { + return fmt.Errorf("failed to create git-source application. Err: %w", err) + } + + log.G(ctx).Infof("done creating a new git-source: '%s'", opts.gsName) + + return nil +} + +func createDemoWorkflowTemplate(gsFs fs.FS, gsName, runtimeName string) error { + var err error + + gsPath := gsFs.Join(apstore.Default.AppsDir, gsName, runtimeName, "demo-wf-template.yaml") + wfTemplate := &wfv1alpha1.WorkflowTemplate{ + TypeMeta: metav1.TypeMeta{ + Kind: wf.WorkflowTemplateKind, + APIVersion: wfv1alpha1.SchemeGroupVersion.String(), + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "demo-workflow-template", + Namespace: runtimeName, + }, + Spec: wfv1alpha1.WorkflowTemplateSpec{ + WorkflowSpec: wfv1alpha1.WorkflowSpec{ + Entrypoint: "whalesay", + Templates: []wfv1alpha1.Template{ + { + Name: "whalesay", + Container: &v1.Container{ + Image: "docker/whalesay", + Command: []string{"cowsay"}, + Args: []string{"Hello World"}, + }, + }, + }, + }, + }, + } + if err = gsFs.WriteYamls(gsPath, wfTemplate); err != nil { + return err + } + + return err +} diff --git a/cmd/commands/root.go b/cmd/commands/root.go index 5277810e3..bde0ecf6d 100644 --- a/cmd/commands/root.go +++ b/cmd/commands/root.go @@ -51,6 +51,7 @@ variables in advanced to simplify the use of those commands. cmd.AddCommand(NewVersionCommand()) cmd.AddCommand(NewConfigCommand()) cmd.AddCommand(NewRuntimeCommand()) + cmd.AddCommand(NewGitSourceCommand()) cobra.OnInitialize(func() { postInitCommands(cmd.Commands()) }) diff --git a/cmd/commands/runtime.go b/cmd/commands/runtime.go index ed72cd686..c1e994367 100644 --- a/cmd/commands/runtime.go +++ b/cmd/commands/runtime.go @@ -37,8 +37,6 @@ import ( apstore "github.com/argoproj-labs/argocd-autopilot/pkg/store" aputil "github.com/argoproj-labs/argocd-autopilot/pkg/util" argocdv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" - wf "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow" - wfv1alpha1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1" "github.com/ghodss/yaml" "github.com/go-git/go-billy/v5/memfs" "github.com/juju/ansiterm" @@ -127,7 +125,7 @@ func NewRuntimeInstallCommand() *cobra.Command { insCloneOpts.Parse() if gsCloneOpts.Repo == "" { host, orgRepo, _, _, _, suffix, _ := aputil.ParseGitUrl(insCloneOpts.Repo) - gsCloneOpts.Repo = host + orgRepo + "_git_source" + suffix + gsCloneOpts.Repo = host + orgRepo + "_git-source" + suffix + "/workflows" } gsCloneOpts.Parse() @@ -243,12 +241,16 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error { return fmt.Errorf("failed to create workflows-reporter: %w", err) } - if err = createDemoWorkflowTemplate(ctx, opts.gsCloneOpts, store.Get().GitSourceName, opts.RuntimeName); err != nil { - return fmt.Errorf("failed to create demo workflowTemplate: %w", err) - } + gsPath := opts.gsCloneOpts.FS.Join(apstore.Default.AppsDir, store.Get().GitSourceName, opts.RuntimeName) + fullGsPath := opts.gsCloneOpts.FS.Join(opts.gsCloneOpts.FS.Root(), gsPath)[1:] - if err = createGitSource(ctx, opts.insCloneOpts, opts.gsCloneOpts, store.Get().GitSourceName, opts.RuntimeName, - opts.commonConfig.CodefreshBaseURL); err != nil { + if err = RunCreateGitSource(ctx, &GitSourceCreateOptions{ + insCloneOpts: opts.insCloneOpts, + gsCloneOpts: opts.gsCloneOpts, + gsName: store.Get().GitSourceName, + runtimeName: opts.RuntimeName, + fullGsPath: fullGsPath, + }); err != nil { return fmt.Errorf("failed to create `%s`: %w", store.Get().GitSourceName, err) } @@ -258,7 +260,7 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error { func NewRuntimeListCommand() *cobra.Command { cmd := &cobra.Command{ - Use: "list ", + Use: "list [runtime_name]", Short: "List all Codefresh runtimes", Example: util.Doc(` runtime list`), RunE: func(_ *cobra.Command, _ []string) error { @@ -759,61 +761,3 @@ func createSensor(repofs fs.FS, name, path, namespace, eventSourceName, trigger, }) return repofs.WriteYamls(repofs.Join(path, "sensor.yaml"), sensor) } - -func createDemoWorkflowTemplate(ctx context.Context, gsCloneOpts *git.CloneOptions, gsName, runtimeName string) error { - gsRepo, gsFs, err := gsCloneOpts.GetRepo(ctx) - if err != nil { - return err - } - - gsPath := gsCloneOpts.FS.Join(apstore.Default.AppsDir, gsName, runtimeName) - wfTemplate := &wfv1alpha1.WorkflowTemplate{ - TypeMeta: metav1.TypeMeta{ - Kind: wf.WorkflowTemplateKind, - APIVersion: wfv1alpha1.SchemeGroupVersion.String(), - }, - ObjectMeta: metav1.ObjectMeta{ - Name: "demo-workflow-template", - Namespace: runtimeName, - }, - Spec: wfv1alpha1.WorkflowTemplateSpec{ - WorkflowSpec: wfv1alpha1.WorkflowSpec{ - Entrypoint: "whalesay", - Templates: []wfv1alpha1.Template{ - { - Name: "whalesay", - Container: &v1.Container{ - Image: "docker/whalesay", - Command: []string{"cowsay"}, - Args: []string{"Hello World"}, - }, - }, - }, - }, - }, - } - if err = gsFs.WriteYamls(gsFs.Join(gsPath, "demo-wf-template.yaml"), wfTemplate); err != nil { - return err - } - - _, err = gsRepo.Persist(ctx, &git.PushOptions{ - CommitMsg: fmt.Sprintf("Created %s Directory", gsPath), - }) - return err -} - -func createGitSource(ctx context.Context, insCloneOpts *git.CloneOptions, gsCloneOpts *git.CloneOptions, gsName, runtimeName, cfBaseURL string) error { - gsPath := gsCloneOpts.FS.Join(apstore.Default.AppsDir, gsName, runtimeName) - fullGsPath := gsCloneOpts.FS.Join(gsCloneOpts.FS.Root(), gsPath)[1:] - - appDef := &runtime.AppDef{ - Name: gsName, - Type: application.AppTypeDirectory, - URL: gsCloneOpts.URL() + fullGsPath, - } - if err := appDef.CreateApp(ctx, nil, insCloneOpts, runtimeName, store.Get().CFGitSourceType, nil); err != nil { - return fmt.Errorf("failed to create git-source: %w", err) - } - - return nil -} diff --git a/docs/commands/cli-v2.md b/docs/commands/cli-v2.md index 851f5d8d7..fac8682bf 100644 --- a/docs/commands/cli-v2.md +++ b/docs/commands/cli-v2.md @@ -32,6 +32,7 @@ cli-v2 [flags] ### SEE ALSO * [cli-v2 config](cli-v2_config.md) - Manage Codefresh authentication contexts +* [cli-v2 git-source](cli-v2_git-source.md) - Manage git-sources of Codefresh runtimes * [cli-v2 runtime](cli-v2_runtime.md) - Manage Codefresh runtimes * [cli-v2 version](cli-v2_version.md) - Show cli version diff --git a/docs/commands/cli-v2_git-source.md b/docs/commands/cli-v2_git-source.md new file mode 100644 index 000000000..7f0e7131f --- /dev/null +++ b/docs/commands/cli-v2_git-source.md @@ -0,0 +1,28 @@ +## cli-v2 git-source + +Manage git-sources of Codefresh runtimes + +``` +cli-v2 git-source [flags] +``` + +### Options + +``` + -h, --help help for git-source +``` + +### Options inherited from parent commands + +``` + --auth-context string Run the next command using a specific authentication context + --cfconfig string Custom path for authentication contexts config file (default "/home/user") + --insecure Disable certificate validation for TLS connections (e.g. to g.codefresh.io) + --request-timeout duration Request timeout (default 30s) +``` + +### SEE ALSO + +* [cli-v2](cli-v2.md) - cli-v2 is used for installing and managing codefresh installations using gitops +* [cli-v2 git-source create](cli-v2_git-source_create.md) - add a new git-source to an existing runtime + diff --git a/docs/commands/cli-v2_git-source_create.md b/docs/commands/cli-v2_git-source_create.md new file mode 100644 index 000000000..2acaa61dd --- /dev/null +++ b/docs/commands/cli-v2_git-source_create.md @@ -0,0 +1,41 @@ +## cli-v2 git-source create + +add a new git-source to an existing runtime + +``` +cli-v2 git-source create runtime_name git-source_name [flags] +``` + +### Examples + +``` + + cli-v2 git-source create runtime_name git-source-name https://github.com/owner/repo-name/my-workflow + +``` + +### Options + +``` + --git-src-git-token string Your git provider api token [GIT_SRC_GIT_TOKEN] + --git-src-provider string The git provider, one of: gitea|github + --git-src-repo string Repository URL [GIT_SRC_GIT_REPO] + -t, --git-token string Your git provider api token [GIT_TOKEN] + -h, --help help for create + --provider string The git provider, one of: gitea|github + --repo string Repository URL [GIT_REPO] +``` + +### Options inherited from parent commands + +``` + --auth-context string Run the next command using a specific authentication context + --cfconfig string Custom path for authentication contexts config file (default "/home/user") + --insecure Disable certificate validation for TLS connections (e.g. to g.codefresh.io) + --request-timeout duration Request timeout (default 30s) +``` + +### SEE ALSO + +* [cli-v2 git-source](cli-v2_git-source.md) - Manage git-sources of Codefresh runtimes + diff --git a/docs/commands/cli-v2_runtime_list.md b/docs/commands/cli-v2_runtime_list.md index 4fcd2cc71..24eb8470e 100644 --- a/docs/commands/cli-v2_runtime_list.md +++ b/docs/commands/cli-v2_runtime_list.md @@ -3,7 +3,7 @@ List all Codefresh runtimes ``` -cli-v2 runtime list [flags] +cli-v2 runtime list [runtime_name] [flags] ``` ### Examples diff --git a/docs/releases/release_notes.md b/docs/releases/release_notes.md index 711b70772..b58369d56 100644 --- a/docs/releases/release_notes.md +++ b/docs/releases/release_notes.md @@ -20,7 +20,7 @@ cf version ### Linux ```bash # download and extract the binary -curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.55/cf-linux-amd64.tar.gz | tar zx +curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.56/cf-linux-amd64.tar.gz | tar zx # move the binary to your $PATH mv ./cf-linux-amd64 /usr/local/bin/cf @@ -32,7 +32,7 @@ cf version ### Mac ```bash # download and extract the binary -curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.55/cf-darwin-amd64.tar.gz | tar zx +curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.56/cf-darwin-amd64.tar.gz | tar zx # move the binary to your $PATH mv ./cf-darwin-amd64 /usr/local/bin/cf diff --git a/go.mod b/go.mod index 23eb0e666..a986cb65f 100644 --- a/go.mod +++ b/go.mod @@ -9,8 +9,7 @@ require ( github.com/argoproj/argo-cd/v2 v2.1.0-rc1 github.com/argoproj/argo-events v1.3.1 github.com/argoproj/argo-workflows/v3 v3.1.0 - github.com/argoproj/gitops-engine v0.3.3 // indirect - github.com/briandowns/spinner v1.13.0 + github.com/briandowns/spinner v1.16.0 github.com/codefresh-io/go-sdk v0.30.2 github.com/fatih/color v1.12.0 github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32 @@ -23,7 +22,7 @@ require ( github.com/spf13/pflag v1.0.5 github.com/spf13/viper v1.7.1 github.com/stretchr/testify v1.7.0 - k8s.io/api v0.21.1 + k8s.io/api v0.21.3 k8s.io/apimachinery v0.21.1 k8s.io/client-go v11.0.1-0.20190816222228-6d55c1b1f1ca+incompatible sigs.k8s.io/kustomize/api v0.8.8 diff --git a/go.sum b/go.sum index 59feb99f2..4c2654bee 100644 --- a/go.sum +++ b/go.sum @@ -234,8 +234,9 @@ github.com/bombsimon/logrusr v1.0.0/go.mod h1:Jq0nHtvxabKE5EMwAAdgTaz7dfWE8C4i11 github.com/boynton/repl v0.0.0-20170116235056-348863958e3e/go.mod h1:Crc/GCZ3NXDVCio7Yr0o+SSrytpcFhLmVCIzi0s49t4= github.com/bradleyfalzon/ghinstallation v1.1.1 h1:pmBXkxgM1WeF8QYvDLT5kuQiHMcmf+X015GI0KM/E3I= github.com/bradleyfalzon/ghinstallation v1.1.1/go.mod h1:vyCmHTciHx/uuyN82Zc3rXN3X2KTK8nUTCrTMwAhcug= -github.com/briandowns/spinner v1.13.0 h1:q/Y9LtpwtvL0CRzXrAMj0keVXqNhBYUFg6tBOUiY8ek= github.com/briandowns/spinner v1.13.0/go.mod h1:QOuQk7x+EaDASo80FEXwlwiA+j/PPIcX3FScO+3/ZPQ= +github.com/briandowns/spinner v1.16.0 h1:DFmp6hEaIx2QXXuqSJmtfSBSAjRmpGiKG6ip2Wm/yOs= +github.com/briandowns/spinner v1.16.0/go.mod h1:QOuQk7x+EaDASo80FEXwlwiA+j/PPIcX3FScO+3/ZPQ= github.com/caddyserver/caddy v1.0.3/go.mod h1:G+ouvOY32gENkJC+jhgl62TyhvqEsFaDiZ4uw0RzP1E= github.com/casbin/casbin v1.9.1 h1:ucjbS5zTrmSLtH4XogqOG920Poe6QatdXtz1FEbApeM= github.com/casbin/casbin v1.9.1/go.mod h1:z8uPsfBJGUsnkagrt3G8QvjgTKFMBJ32UP8HpZllfog= diff --git a/manifests/runtime.yaml b/manifests/runtime.yaml index 0c03d115d..174538e2f 100644 --- a/manifests/runtime.yaml +++ b/manifests/runtime.yaml @@ -5,7 +5,7 @@ metadata: namespace: "{{ namespace }}" spec: defVersion: 1.0.0 - version: 0.0.55 + version: 0.0.56 bootstrapSpecifier: github.com/codefresh-io/cli-v2/manifests/argo-cd components: - name: events