-
Notifications
You must be signed in to change notification settings - Fork 135
/
deploysource.go
210 lines (176 loc) · 5.83 KB
/
deploysource.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
// Copyright 2020 The PipeCD 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 deploysource
import (
"context"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"sync"
"github.com/pipe-cd/pipe/pkg/app/piped/sourcedecrypter"
"github.com/pipe-cd/pipe/pkg/config"
"github.com/pipe-cd/pipe/pkg/model"
)
type DeploySource struct {
RepoDir string
AppDir string
Revision string
ApplicationConfig *config.Config
GenericApplicationConfig config.GenericApplicationSpec
}
type Provider interface {
Revision() string
Get(ctx context.Context, logWriter io.Writer) (*DeploySource, error)
GetReadOnly(ctx context.Context, logWriter io.Writer) (*DeploySource, error)
}
type secretDecrypter interface {
Decrypt(string) (string, error)
}
type provider struct {
workingDir string
cloner SourceCloner
revisionName string
revision string
appGitPath model.ApplicationGitPath
secretDecrypter secretDecrypter
done bool
source *DeploySource
err error
copyNum int
mu sync.Mutex
}
func NewProvider(
workingDir string,
cloner SourceCloner,
appGitPath model.ApplicationGitPath,
sd secretDecrypter,
) Provider {
return &provider{
workingDir: workingDir,
cloner: cloner,
revisionName: cloner.RevisionName(),
revision: cloner.Revision(),
appGitPath: appGitPath,
secretDecrypter: sd,
}
}
func (p *provider) Revision() string {
return p.cloner.Revision()
}
func (p *provider) Get(ctx context.Context, lw io.Writer) (*DeploySource, error) {
fmt.Fprintf(lw, "Preparing deploy source at %s commit (%s)\n", p.revisionName, p.revision)
p.mu.Lock()
defer p.mu.Unlock()
if !p.done {
p.source, p.err = p.prepare(ctx, lw)
p.done = true
}
if p.err != nil {
return nil, p.err
}
ds, err := p.copy(lw)
if err != nil {
return nil, err
}
fmt.Fprintf(lw, "Successfully prepared deploy source at %s commit (%s)\n", p.revisionName, p.revision)
return ds, nil
}
func (p *provider) GetReadOnly(ctx context.Context, lw io.Writer) (*DeploySource, error) {
fmt.Fprintf(lw, "Preparing deploy source at %s commit (%s)\n", p.revisionName, p.revision)
p.mu.Lock()
defer p.mu.Unlock()
if !p.done {
p.source, p.err = p.prepare(ctx, lw)
p.done = true
}
if p.err != nil {
return nil, p.err
}
fmt.Fprintf(lw, "Successfully prepared deploy source at %s commit (%s)\n", p.revisionName, p.revision)
return p.source, nil
}
func (p *provider) prepare(ctx context.Context, lw io.Writer) (*DeploySource, error) {
// Ensure the existence of the working directory.
if err := os.MkdirAll(p.workingDir, 0700); err != nil {
fmt.Fprintf(lw, "Unable to create the working directory to store deploy source (%v)\n", err)
return nil, err
}
// Create a temporary directory for storing the source.
dir, err := os.MkdirTemp(p.workingDir, "deploysource")
if err != nil {
fmt.Fprintf(lw, "Unable to create a temp directory to store the deploy source (%v)\n", err)
return nil, err
}
repoDir := filepath.Join(dir, "repo")
appDir := filepath.Join(repoDir, p.appGitPath.Path)
// Clone the specified revision of the repository.
if err := p.cloner.Clone(ctx, repoDir); err != nil {
fmt.Fprintf(lw, "Unable to clone the %s commit (%v)\n", p.revisionName, err)
return nil, err
}
fmt.Fprintf(lw, "Successfully cloned the %s commit\n", p.revisionName)
// Load the application configuration file.
var (
cfgFileRelPath = p.appGitPath.GetApplicationConfigFilePath()
cfgFileAbsPath = filepath.Join(repoDir, cfgFileRelPath)
)
cfg, err := config.LoadFromYAML(cfgFileAbsPath)
if err != nil {
fmt.Fprintf(lw, "Unable to load the application configuration file at %s (%v)\n", cfgFileRelPath, err)
if os.IsNotExist(err) {
return nil, fmt.Errorf("application config file %s was not found", cfgFileRelPath)
}
return nil, err
}
gac, ok := cfg.GetGenericApplication()
if !ok {
fmt.Fprintf(lw, "Invalid application kind %s\n", cfg.Kind)
return nil, fmt.Errorf("unsupport application kind %s", cfg.Kind)
}
fmt.Fprintln(lw, "Successfully loaded the application configuration file")
// Decrypt the sealed secrets if needed.
if gac.Encryption != nil && p.secretDecrypter != nil && len(gac.Encryption.DecryptionTargets) > 0 {
if err := sourcedecrypter.DecryptSecrets(appDir, *gac.Encryption, p.secretDecrypter); err != nil {
fmt.Fprintf(lw, "Unable to decrypt the secrets (%v)\n", err)
return nil, err
}
fmt.Fprintf(lw, "Successfully decrypted secrets: %v\n", gac.Encryption.DecryptionTargets)
}
return &DeploySource{
RepoDir: repoDir,
AppDir: appDir,
Revision: p.revision,
ApplicationConfig: cfg,
GenericApplicationConfig: gac,
}, nil
}
func (p *provider) copy(lw io.Writer) (*DeploySource, error) {
p.copyNum++
dest := fmt.Sprintf("%s-%d", p.source.RepoDir, p.copyNum)
cmd := exec.Command("cp", "-rf", p.source.RepoDir, dest)
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Fprintf(lw, "Unable to copy deploy source data (%v, %s)\n", err, string(out))
return nil, err
}
return &DeploySource{
RepoDir: dest,
AppDir: filepath.Join(dest, p.appGitPath.Path),
Revision: p.revision,
ApplicationConfig: p.source.ApplicationConfig,
GenericApplicationConfig: p.source.GenericApplicationConfig,
}, nil
}