forked from redhat-developer/odo
/
common_push.go
288 lines (239 loc) · 7.95 KB
/
common_push.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package component
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/fatih/color"
"github.com/golang/glog"
"github.com/openshift/odo/pkg/component"
"github.com/openshift/odo/pkg/config"
"github.com/openshift/odo/pkg/log"
"github.com/openshift/odo/pkg/occlient"
"github.com/openshift/odo/pkg/odo/genericclioptions"
odoutil "github.com/openshift/odo/pkg/odo/util"
"github.com/openshift/odo/pkg/project"
"github.com/openshift/odo/pkg/util"
"github.com/pkg/errors"
)
// CommonPushOptions has data needed for all pushes
type CommonPushOptions struct {
ignores []string
show bool
sourceType config.SrcType
sourcePath string
componentContext string
client *occlient.Client
localConfigInfo *config.LocalConfigInfo
pushConfig bool
pushSource bool
forceBuild bool
isCmpExists bool
*genericclioptions.Context
}
// NewCommonPushOptions instantiates a commonPushOptions object
func NewCommonPushOptions() *CommonPushOptions {
return &CommonPushOptions{
show: false,
}
}
// ResolveSrcAndConfigFlags sets all pushes if none is asked
func (cpo *CommonPushOptions) ResolveSrcAndConfigFlags() {
// If neither config nor source flag is passed, update both config and source to the component
if !cpo.pushConfig && !cpo.pushSource {
cpo.pushConfig = true
cpo.pushSource = true
}
}
func (cpo *CommonPushOptions) createCmpIfNotExistsAndApplyCmpConfig(stdout io.Writer) error {
if !cpo.pushConfig {
// Not the case of component creation or updation(with new config)
// So nothing to do here and hence return from here
return nil
}
cmpName := cpo.localConfigInfo.GetName()
// Output the "new" section (applying changes)
log.Info("\nConfiguration changes")
// If the component does not exist, we will create it for the first time.
if !cpo.isCmpExists {
// Classic case of component creation
if err := component.CreateComponent(cpo.Context.Client, *cpo.localConfigInfo, cpo.componentContext, stdout); err != nil {
log.Errorf(
"Failed to create component with name %s. Please use `odo config view` to view settings used to create component. Error: %+v",
cmpName,
err,
)
os.Exit(1)
}
}
// Apply config
err := component.ApplyConfig(cpo.Context.Client, *cpo.localConfigInfo, stdout, cpo.isCmpExists)
if err != nil {
odoutil.LogErrorAndExit(err, "Failed to update config to component deployed")
}
return nil
}
// ResolveProject completes the push options as needed
func (cpo *CommonPushOptions) ResolveProject(prjName string) (err error) {
// check if project exist
isPrjExists, err := project.Exists(cpo.Context.Client, prjName)
if err != nil {
return errors.Wrapf(err, "failed to check if project with name %s exists", prjName)
}
if !isPrjExists {
err = project.Create(cpo.Context.Client, prjName, true)
if err != nil {
return errors.Wrapf(
err,
"project %s does not exist. Failed creating it. Please try after creating project using `odo project create <project_name>`",
prjName,
)
}
}
cpo.Context.Client.Namespace = prjName
return
}
// SetSourceInfo sets up source information
func (cpo *CommonPushOptions) SetSourceInfo() (err error) {
cpo.sourceType = cpo.localConfigInfo.GetSourceType()
glog.V(4).Infof("SourceLocation: %s", cpo.localConfigInfo.GetSourceLocation())
// Get SourceLocation here...
cpo.sourcePath, err = cpo.localConfigInfo.GetOSSourcePath()
if err != nil {
return errors.Wrap(err, "unable to retrieve absolute path to source location")
}
glog.V(4).Infof("Source Path: %s", cpo.sourcePath)
return
}
// Push pushes changes as per set options
func (cpo *CommonPushOptions) Push() (err error) {
deletedFiles := []string{}
changedFiles := []string{}
isForcePush := false
stdout := color.Output
cmpName := cpo.localConfigInfo.GetName()
appName := cpo.localConfigInfo.GetApplication()
if cpo.componentContext == "" {
cpo.componentContext = strings.Trim(filepath.Dir(cpo.localConfigInfo.Filename), ".odo")
}
err = cpo.createCmpIfNotExistsAndApplyCmpConfig(stdout)
if err != nil {
return
}
if !cpo.pushSource {
// If source is not requested for update, return
return nil
}
log.Infof("\nPushing to component %s of type %s", cmpName, cpo.sourceType)
if !cpo.forceBuild && cpo.sourceType != config.GIT {
absIgnoreRules := util.GetAbsGlobExps(cpo.sourcePath, cpo.ignores)
spinner := log.NewStatus(log.GetStdout())
defer spinner.End(true)
if cpo.isCmpExists {
spinner.Start("Checking file changes for pushing", false)
} else {
// if the component doesn't exist, we don't check for changes in the files
// thus we show a different message
spinner.Start("Checking files for pushing", false)
}
// run the indexer and find the modified/added/deleted/renamed files
filesChanged, filesDeleted, err := util.RunIndexer(cpo.componentContext, absIgnoreRules)
spinner.End(true)
if err != nil {
return err
}
if cpo.isCmpExists {
// apply the glob rules from the .gitignore/.odo file
// and ignore the files on which the rules apply and filter them out
filesChangedFiltered, filesDeletedFiltered := filterIgnores(filesChanged, filesDeleted, absIgnoreRules)
// Remove the relative file directory from the list of deleted files
// in order to make the changes correctly within the OpenShift pod
deletedFiles, err = util.RemoveRelativePathFromFiles(filesDeletedFiltered, cpo.sourcePath)
if err != nil {
return err
}
glog.V(4).Infof("List of files to be deleted: +%v", deletedFiles)
changedFiles = filesChangedFiltered
if len(filesChangedFiltered) == 0 && len(filesDeletedFiltered) == 0 {
// no file was modified/added/deleted/renamed, thus return without building
log.Success("No file changes detected, skipping build. Use the '-f' flag to force the build.")
return nil
}
}
}
if cpo.forceBuild || !cpo.isCmpExists {
isForcePush = true
}
// Get SourceLocation here...
cpo.sourcePath, err = cpo.localConfigInfo.GetOSSourcePath()
if err != nil {
return errors.Wrap(err, "unable to retrieve OS source path to source location")
}
switch cpo.sourceType {
case config.LOCAL:
glog.V(4).Infof("Copying directory %s to pod", cpo.sourcePath)
err = component.PushLocal(
cpo.Context.Client,
cmpName,
appName,
cpo.sourcePath,
os.Stdout,
changedFiles,
deletedFiles,
isForcePush,
util.GetAbsGlobExps(cpo.sourcePath, cpo.ignores),
cpo.show,
)
if err != nil {
return errors.Wrapf(err, fmt.Sprintf("Failed to push component: %v", cmpName))
}
case config.BINARY:
// We will pass in the directory, NOT filepath since this is a binary..
binaryDirectory := filepath.Dir(cpo.sourcePath)
glog.V(4).Infof("Copying binary file %s to pod", cpo.sourcePath)
err = component.PushLocal(
cpo.Context.Client,
cmpName,
appName,
binaryDirectory,
os.Stdout,
[]string{cpo.sourcePath},
deletedFiles,
isForcePush,
util.GetAbsGlobExps(cpo.sourcePath, cpo.ignores),
cpo.show,
)
if err != nil {
return errors.Wrapf(err, fmt.Sprintf("Failed to push component: %v", cmpName))
}
// we don't need a case for building git components
// the build happens before deployment
return errors.Wrapf(err, fmt.Sprintf("failed to push component: %v", cmpName))
}
log.Success("Changes successfully pushed to component")
return
}
// filterIgnores applies the glob rules on the filesChanged and filesDeleted and filters them
// returns the filtered results which match any of the glob rules
func filterIgnores(filesChanged, filesDeleted, absIgnoreRules []string) (filesChangedFiltered, filesDeletedFiltered []string) {
for _, file := range filesChanged {
match, err := util.IsGlobExpMatch(file, absIgnoreRules)
if err != nil {
continue
}
if !match {
filesChangedFiltered = append(filesChangedFiltered, file)
}
}
for _, file := range filesDeleted {
match, err := util.IsGlobExpMatch(file, absIgnoreRules)
if err != nil {
continue
}
if !match {
filesDeletedFiltered = append(filesDeletedFiltered, file)
}
}
return filesChangedFiltered, filesDeletedFiltered
}