This repository has been archived by the owner on Nov 11, 2020. It is now read-only.
/
env_factory.go
170 lines (148 loc) · 5.49 KB
/
env_factory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package envfactory
import (
"context"
"fmt"
"io/ioutil"
"github.com/jenkins-x-labs/helmboot/pkg/common"
"github.com/jenkins-x-labs/helmboot/pkg/jxadapt"
"github.com/jenkins-x-labs/helmboot/pkg/reqhelpers"
"github.com/jenkins-x/go-scm/scm"
"github.com/jenkins-x/jx/pkg/auth"
"github.com/jenkins-x/jx/pkg/cmd/step/verify"
"github.com/jenkins-x/jx/pkg/config"
"github.com/jenkins-x/jx/pkg/gits"
"github.com/jenkins-x/jx/pkg/jxfactory"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
type EnvFactory struct {
JXFactory jxfactory.Factory
Gitter gits.Gitter
RepoName string
GitURLOutFile string
OutDir string
IOFileHandles *util.IOFileHandles
ScmClient *scm.Client
BatchMode bool
NoOAuth bool
}
// AddFlags adds common CLI flags
func (o *EnvFactory) AddFlags(cmd *cobra.Command) {
cmd.Flags().BoolVarP(&o.BatchMode, "batch-mode", "b", false, "Enables batch mode which avoids prompting for user input")
cmd.Flags().BoolVarP(&o.NoOAuth, "no-oauth", "", false, "Disables the use of OAuth login to github.com to get a github access token")
cmd.Flags().StringVarP(&o.RepoName, "repo", "", "", "the name of the development git repository to create")
cmd.Flags().StringVarP(&o.GitURLOutFile, "out", "", "", "the name of the file to save with the created git URL inside")
}
// CreateDevEnvGitRepository creates the dev environment git repository from the given directory
func (o *EnvFactory) CreateDevEnvGitRepository(dir string, gitPublic bool) error {
o.OutDir = dir
requirements, fileName, err := config.LoadRequirementsConfig(dir)
if err != nil {
return errors.Wrapf(err, "failed to load requirements from %s", dir)
}
dev := reqhelpers.GetDevEnvironmentConfig(requirements)
if dev == nil {
return fmt.Errorf("the file %s does not contain a development environment", fileName)
}
cr := &CreateRepository{
GitServer: requirements.Cluster.GitServer,
GitKind: requirements.Cluster.GitKind,
Owner: dev.Owner,
Repository: dev.Repository,
GitPublic: gitPublic,
}
if cr.Owner == "" {
cr.Owner = requirements.Cluster.EnvironmentGitOwner
}
if cr.Repository == "" {
cr.Repository = o.RepoName
}
handles := jxadapt.ToIOHandles(o.IOFileHandles)
err = cr.ConfirmValues(o.BatchMode, handles)
if err != nil {
return err
}
scmClient, token, err := o.CreateScmClient(cr.GitServer, cr.Owner, cr.GitKind)
if err != nil {
return errors.Wrapf(err, "failed to create SCM client for server %s", cr.GitServer)
}
o.ScmClient = scmClient
user, _, err := scmClient.Users.Find(context.Background())
if err != nil {
return errors.Wrap(err, "failed to find the current SCM user")
}
cr.CurrentUsername = user.Login
userAuth := &auth.UserAuth{
Username: user.Login,
ApiToken: token,
}
repo, err := cr.CreateRepository(scmClient)
if err != nil {
return err
}
err = o.PushToGit(repo.Clone, userAuth, dir)
if err != nil {
return errors.Wrap(err, "failed to push to the git repository")
}
err = o.PrintBootJobInstructions(requirements, repo.Link)
if err != nil {
return err
}
if o.GitURLOutFile != "" {
err = ioutil.WriteFile(o.GitURLOutFile, []byte(repo.Link), util.DefaultFileWritePermissions)
if err != nil {
return errors.Wrapf(err, "failed to save Git URL to file %s", o.GitURLOutFile)
}
}
return nil
}
// CreateScmClient creates a new scm client
func (o *EnvFactory) CreateScmClient(gitServer, owner, gitKind string) (*scm.Client, string, error) {
return o.JXAdapter().ScmClient(gitServer, owner, gitKind)
}
// VerifyPreInstall verify the pre install of boot
func (o *EnvFactory) VerifyPreInstall(disableVerifyPackages bool, dir string) error {
vo := verify.StepVerifyPreInstallOptions{}
vo.CommonOptions = o.JXAdapter().NewCommonOptions()
vo.Dir = dir
vo.DisableVerifyPackages = disableVerifyPackages
vo.NoSecretYAMLValidate = true
return vo.Run()
}
// PrintBootJobInstructions prints the instructions to run the installer
func (o *EnvFactory) PrintBootJobInstructions(requirements *config.RequirementsConfig, link string) error {
gitInfo, err := gits.ParseGitURL(link)
if err != nil {
return errors.Wrapf(err, "failed to parse git URL %s", link)
}
info := util.ColorInfo
log.Logger().Info("\nto configure your installation clone the environment repository and push changes, see https://jenkins-x.io/docs/labs/boot/getting-started/config/:\n\n")
log.Logger().Infof("%s", info(fmt.Sprintf("git clone %s", link)))
log.Logger().Info("\nto boot your cluster run the following commands:\n\n")
log.Logger().Infof("%s", info(fmt.Sprintf("cd %s", gitInfo.Name)))
log.Logger().Infof("%s", info(fmt.Sprintf("%s secrets edit", common.BinaryName)))
log.Logger().Infof("%s", info(fmt.Sprintf("%s run", common.BinaryName)))
return nil
}
// PushToGit pushes to the git repository
func (o *EnvFactory) PushToGit(cloneURL string, userAuth *auth.UserAuth, dir string) error {
forkPushURL, err := o.Gitter.CreateAuthenticatedURL(cloneURL, userAuth)
if err != nil {
return errors.Wrapf(err, "creating push URL for %s", cloneURL)
}
remoteBranch := "master"
err = o.Gitter.Push(dir, forkPushURL, true, fmt.Sprintf("%s:%s", "HEAD", remoteBranch))
if err != nil {
return errors.Wrapf(err, "pushing merged branch %s", remoteBranch)
}
log.Logger().Infof("pushed code to the repository")
return nil
}
// JXAdapter creates an adapter to the jx code
func (o *EnvFactory) JXAdapter() *jxadapt.JXAdapter {
a := jxadapt.NewJXAdapter(o.JXFactory, o.Gitter, o.BatchMode)
a.NoOAuth = o.NoOAuth
return a
}