blob: f4c05bba4b81eb361cf95dc9ea4f5d5a5329b709 [file] [log] [blame]
gioe72b54f2024-04-22 10:44:41 +04001package soft
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +04002
3import (
gio308105e2024-04-19 13:12:13 +04004 "encoding/json"
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +04005 "errors"
gio0eaf2712024-04-14 13:08:46 +04006 "fmt"
Giorgi Lekveishvili87be4ae2023-06-11 23:41:09 +04007 "io"
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +04008 "io/fs"
Giorgi Lekveishvili76951482023-06-30 23:25:09 +04009 "io/ioutil"
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040010 "net"
gio0eaf2712024-04-14 13:08:46 +040011 "os"
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +040012 "path/filepath"
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +040013 "sync"
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +040014 "time"
15
gioe72b54f2024-04-22 10:44:41 +040016 pio "github.com/giolekva/pcloud/core/installer/io"
17
gio3af43942024-04-16 08:13:50 +040018 "github.com/go-git/go-billy/v5"
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040019 "github.com/go-git/go-billy/v5/util"
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +040020 "github.com/go-git/go-git/v5"
gio0eaf2712024-04-14 13:08:46 +040021 "github.com/go-git/go-git/v5/config"
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +040022 "github.com/go-git/go-git/v5/plumbing/object"
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040023 gitssh "github.com/go-git/go-git/v5/plumbing/transport/ssh"
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +040024 "golang.org/x/crypto/ssh"
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040025 "sigs.k8s.io/yaml"
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +040026)
27
gio3af43942024-04-16 08:13:50 +040028type RepoFS interface {
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040029 Reader(path string) (io.ReadCloser, error)
Giorgi Lekveishvili87be4ae2023-06-11 23:41:09 +040030 Writer(path string) (io.WriteCloser, error)
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040031 CreateDir(path string) error
32 RemoveDir(path string) error
giof8843412024-05-22 16:38:05 +040033 ListDir(path string) ([]os.FileInfo, error)
gio3af43942024-04-16 08:13:50 +040034}
35
gio308105e2024-04-19 13:12:13 +040036type DoFn func(r RepoFS) (string, error)
37
38type doOptions struct {
giof71a0832024-06-27 14:45:45 +040039 NoPull bool
gio308105e2024-04-19 13:12:13 +040040 NoCommit bool
gio0eaf2712024-04-14 13:08:46 +040041 Force bool
42 ToBranch string
gio9d66f322024-07-06 13:45:10 +040043 NoLock bool
gio308105e2024-04-19 13:12:13 +040044}
45
46type DoOption func(*doOptions)
47
gio9d66f322024-07-06 13:45:10 +040048func WithNoLock() DoOption {
49 return func(o *doOptions) {
50 o.NoLock = true
51 }
52}
53
giof71a0832024-06-27 14:45:45 +040054func WithNoPull() DoOption {
55 return func(o *doOptions) {
56 o.NoPull = true
57 }
58}
59
gio308105e2024-04-19 13:12:13 +040060func WithNoCommit() DoOption {
61 return func(o *doOptions) {
62 o.NoCommit = true
63 }
64}
gio3af43942024-04-16 08:13:50 +040065
gio0eaf2712024-04-14 13:08:46 +040066func WithForce() DoOption {
67 return func(o *doOptions) {
68 o.Force = true
69 }
70}
71
72func WithCommitToBranch(branch string) DoOption {
73 return func(o *doOptions) {
74 o.ToBranch = branch
75 }
76}
77
78type pushOptions struct {
79 ToBranch string
80 Force bool
81}
82
83type PushOption func(*pushOptions)
84
85func WithToBranch(branch string) PushOption {
86 return func(o *pushOptions) {
87 o.ToBranch = branch
88 }
89}
90
91func PushWithForce() PushOption {
92 return func(o *pushOptions) {
93 o.Force = true
94 }
95}
96
gio3af43942024-04-16 08:13:50 +040097type RepoIO interface {
98 RepoFS
99 FullAddress() string
100 Pull() error
gio0eaf2712024-04-14 13:08:46 +0400101 CommitAndPush(message string, opts ...PushOption) error
gio308105e2024-04-19 13:12:13 +0400102 Do(op DoFn, opts ...DoOption) error
gio3af43942024-04-16 08:13:50 +0400103}
104
105type repoFS struct {
106 fs billy.Filesystem
107}
108
gioe72b54f2024-04-22 10:44:41 +0400109func NewBillyRepoFS(fs billy.Filesystem) RepoFS {
110 return &repoFS{fs}
111}
112
gio3af43942024-04-16 08:13:50 +0400113func (r *repoFS) Reader(path string) (io.ReadCloser, error) {
114 return r.fs.Open(path)
115}
116
117func (r *repoFS) Writer(path string) (io.WriteCloser, error) {
gio9d66f322024-07-06 13:45:10 +0400118 if err := r.CreateDir(filepath.Dir(path)); err != nil {
gio3af43942024-04-16 08:13:50 +0400119 return nil, err
120 }
121 return r.fs.Create(path)
122}
123
124func (r *repoFS) CreateDir(path string) error {
125 return r.fs.MkdirAll(path, fs.ModePerm)
126}
127
128func (r *repoFS) RemoveDir(path string) error {
129 if err := util.RemoveAll(r.fs, path); err != nil {
130 if errors.Is(err, fs.ErrNotExist) {
131 return nil
132 }
133 return err
134 }
135 return nil
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +0400136}
137
giof8843412024-05-22 16:38:05 +0400138func (r *repoFS) ListDir(path string) ([]os.FileInfo, error) {
139 return r.fs.ReadDir(path)
140}
141
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +0400142type repoIO struct {
gio3af43942024-04-16 08:13:50 +0400143 *repoFS
gioe72b54f2024-04-22 10:44:41 +0400144 repo *Repository
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +0400145 signer ssh.Signer
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +0400146 l sync.Locker
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +0400147}
148
gioe72b54f2024-04-22 10:44:41 +0400149func NewRepoIO(repo *Repository, signer ssh.Signer) (RepoIO, error) {
gio3af43942024-04-16 08:13:50 +0400150 wt, err := repo.Worktree()
151 if err != nil {
152 return nil, err
153 }
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +0400154 return &repoIO{
gio3af43942024-04-16 08:13:50 +0400155 &repoFS{wt.Filesystem},
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +0400156 repo,
157 signer,
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +0400158 &sync.Mutex{},
gio3af43942024-04-16 08:13:50 +0400159 }, nil
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +0400160}
161
gio3af43942024-04-16 08:13:50 +0400162func (r *repoIO) FullAddress() string {
163 return r.repo.Addr.FullAddress()
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +0400164}
165
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +0400166func (r *repoIO) Pull() error {
167 r.l.Lock()
168 defer r.l.Unlock()
169 return r.pullWithoutLock()
170}
171
172func (r *repoIO) pullWithoutLock() error {
173 wt, err := r.repo.Worktree()
174 if err != nil {
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400175 return nil
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +0400176 }
gio3cdee592024-04-17 10:15:56 +0400177 err = wt.Pull(&git.PullOptions{
gio0eaf2712024-04-14 13:08:46 +0400178 Auth: auth(r.signer),
179 Force: true,
180 Progress: os.Stdout,
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +0400181 })
gio3cdee592024-04-17 10:15:56 +0400182 if err == nil {
183 return nil
184 }
185 if errors.Is(err, git.NoErrAlreadyUpToDate) {
186 return nil
187 }
188 // TODO(gio): check `remote repository is empty`
giof5ffedb2024-06-19 14:14:43 +0400189 fmt.Printf("-- GIT PULL: %s\n", err.Error())
gio3cdee592024-04-17 10:15:56 +0400190 return nil
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400191}
192
gio0eaf2712024-04-14 13:08:46 +0400193func (r *repoIO) CommitAndPush(message string, opts ...PushOption) error {
194 var o pushOptions
195 for _, i := range opts {
196 i(&o)
197 }
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +0400198 wt, err := r.repo.Worktree()
199 if err != nil {
200 return err
201 }
202 if err := wt.AddGlob("*"); err != nil {
203 return err
204 }
gio03fd0c72024-06-18 12:31:42 +0400205 st, err := wt.Status()
206 if err != nil {
207 return err
208 }
209 if len(st) == 0 {
210 return nil // TODO(gio): maybe return ErrorNothingToCommit
211 }
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +0400212 if _, err := wt.Commit(message, &git.CommitOptions{
213 Author: &object.Signature{
214 Name: "pcloud-installer",
215 When: time.Now(),
216 },
217 }); err != nil {
218 return err
219 }
gio0eaf2712024-04-14 13:08:46 +0400220 gopts := &git.PushOptions{
Giorgi Lekveishvili87be4ae2023-06-11 23:41:09 +0400221 RemoteName: "origin",
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +0400222 Auth: auth(r.signer),
gio0eaf2712024-04-14 13:08:46 +0400223 }
224 if o.ToBranch != "" {
225 gopts.RefSpecs = []config.RefSpec{config.RefSpec(fmt.Sprintf("refs/heads/master:refs/heads/%s", o.ToBranch))}
226 }
227 if o.Force {
228 gopts.Force = true
229 }
230 return r.repo.Push(gopts)
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +0400231}
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400232
gio308105e2024-04-19 13:12:13 +0400233func (r *repoIO) Do(op DoFn, opts ...DoOption) error {
gio308105e2024-04-19 13:12:13 +0400234 o := &doOptions{}
235 for _, i := range opts {
236 i(o)
237 }
gio9d66f322024-07-06 13:45:10 +0400238 if o.NoLock {
239 r.l.Lock()
240 defer r.l.Unlock()
241 }
giof71a0832024-06-27 14:45:45 +0400242 if !o.NoPull {
243 if err := r.pullWithoutLock(); err != nil {
244 return err
245 }
246 }
gio9d66f322024-07-06 13:45:10 +0400247 msg, err := op(r)
248 if err != nil {
Giorgi Lekveishvilib59b7c22024-04-03 22:17:50 +0400249 return err
250 }
gio9d66f322024-07-06 13:45:10 +0400251 if o.NoCommit {
252 return nil
253 }
254 popts := []PushOption{}
255 if o.Force {
256 popts = append(popts, PushWithForce())
257 }
258 if o.ToBranch != "" {
259 popts = append(popts, WithToBranch(o.ToBranch))
260 }
261 return r.CommitAndPush(msg, popts...)
Giorgi Lekveishvili76951482023-06-30 23:25:09 +0400262}
263
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400264func auth(signer ssh.Signer) *gitssh.PublicKeys {
265 return &gitssh.PublicKeys{
266 Signer: signer,
267 HostKeyCallbackHelper: gitssh.HostKeyCallbackHelper{
268 HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
269 // TODO(giolekva): verify server public key
270 // fmt.Printf("## %s || %s -- \n", serverPubKey, ssh.MarshalAuthorizedKey(key))
271 return nil
272 },
273 },
274 }
275}
Giorgi Lekveishvili76951482023-06-30 23:25:09 +0400276
gio3af43942024-04-16 08:13:50 +0400277func ReadYaml[T any](repo RepoFS, path string, o *T) error {
278 r, err := repo.Reader(path)
279 if err != nil {
280 return err
281 }
282 defer r.Close()
Giorgi Lekveishvili76951482023-06-30 23:25:09 +0400283 if contents, err := ioutil.ReadAll(r); err != nil {
284 return err
285 } else {
286 return yaml.UnmarshalStrict(contents, o)
287 }
288}
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400289
gio3af43942024-04-16 08:13:50 +0400290func WriteYaml(repo RepoFS, path string, data any) error {
gioe72b54f2024-04-22 10:44:41 +0400291 if d, ok := data.(*pio.Kustomization); ok {
gio3af43942024-04-16 08:13:50 +0400292 data = d
293 }
294 out, err := repo.Writer(path)
295 if err != nil {
296 return err
297 }
gio94904702024-07-26 16:58:34 +0400298 defer out.Close()
gio3af43942024-04-16 08:13:50 +0400299 serialized, err := yaml.Marshal(data)
300 if err != nil {
301 return err
302 }
303 if _, err := out.Write(serialized); err != nil {
304 return err
305 }
306 return nil
307}
308
gio308105e2024-04-19 13:12:13 +0400309func ReadJson[T any](repo RepoFS, path string, o *T) error {
310 r, err := repo.Reader(path)
311 if err != nil {
312 return err
313 }
314 defer r.Close()
315 return json.NewDecoder(r).Decode(o)
316}
317
318func WriteJson(repo RepoFS, path string, data any) error {
gioe72b54f2024-04-22 10:44:41 +0400319 if d, ok := data.(*pio.Kustomization); ok {
gio308105e2024-04-19 13:12:13 +0400320 data = d
321 }
322 w, err := repo.Writer(path)
323 if err != nil {
324 return err
325 }
326 e := json.NewEncoder(w)
327 e.SetIndent("", "\t")
328 return e.Encode(data)
329}
330
gioe72b54f2024-04-22 10:44:41 +0400331func ReadKustomization(repo RepoFS, path string) (*pio.Kustomization, error) {
332 ret := &pio.Kustomization{}
gio3af43942024-04-16 08:13:50 +0400333 if err := ReadYaml(repo, path, &ret); err != nil {
334 return nil, err
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400335 }
336 return ret, nil
337}
gio0eaf2712024-04-14 13:08:46 +0400338
339func ReadFile(repo RepoFS, path string) ([]byte, error) {
340 r, err := repo.Reader(path)
341 if err != nil {
342 return nil, err
343 }
344 defer r.Close()
345 return io.ReadAll(r)
346}
gio5e49bb62024-07-20 10:43:19 +0400347
348func WriteFile(repo RepoFS, path, contents string) error {
349 w, err := repo.Writer(path)
350 if err != nil {
351 return err
352 }
353 defer w.Close()
354 _, err = io.WriteString(w, contents)
355 return err
356}