|
| 1 | +// local.go has methods for resolving a local ship.yaml file, and patching in api.Release info |
| 2 | +// that would usually be returned by pg.replicated.com |
| 3 | +package replicatedapp |
| 4 | + |
| 5 | +import ( |
| 6 | + "bytes" |
| 7 | + "crypto/sha256" |
| 8 | + "encoding/base64" |
| 9 | + "fmt" |
| 10 | + "os" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/go-kit/kit/log" |
| 14 | + "github.com/go-kit/kit/log/level" |
| 15 | + "github.com/pkg/errors" |
| 16 | + "github.com/replicatedhq/ship/pkg/constants" |
| 17 | +) |
| 18 | + |
| 19 | +func (r *resolver) resolveRunbookRelease() (*ShipRelease, error) { |
| 20 | + debug := level.Debug(log.With(r.Logger, "method", "resolveRunbookRelease")) |
| 21 | + debug.Log("phase", "load-specs", "from", "runbook", "file", r.Runbook) |
| 22 | + |
| 23 | + specYAML, err := r.FS.ReadFile(r.Runbook) |
| 24 | + if err != nil { |
| 25 | + return nil, errors.Wrapf(err, "read specs from %s", r.Runbook) |
| 26 | + } |
| 27 | + debug.Log("phase", "load-specs", "from", "runbook", "file", r.Runbook, "spec", specYAML) |
| 28 | + |
| 29 | + if err := r.persistSpec(specYAML); err != nil { |
| 30 | + return nil, errors.Wrapf(err, "serialize last-used YAML to disk") |
| 31 | + } |
| 32 | + debug.Log("phase", "write-yaml", "from", r.Runbook, "write-location", constants.ReleasePath) |
| 33 | + |
| 34 | + fakeGithubContents, err := r.loadLocalGitHubContents() |
| 35 | + if err != nil { |
| 36 | + return nil, errors.Wrapf(err, "load fake github contents") |
| 37 | + } |
| 38 | + |
| 39 | + return &ShipRelease{ |
| 40 | + Spec: string(specYAML), |
| 41 | + ChannelName: r.SetChannelName, |
| 42 | + ChannelIcon: r.SetChannelIcon, |
| 43 | + Semver: r.RunbookReleaseSemver, |
| 44 | + GithubContents: fakeGithubContents, |
| 45 | + }, nil |
| 46 | +} |
| 47 | + |
| 48 | +func (r *resolver) loadLocalGitHubContents() ([]GithubContent, error) { |
| 49 | + debug := level.Debug(log.With(r.Logger, "method", "loadLocalGitHubContents")) |
| 50 | + var fakeGithubContents []GithubContent |
| 51 | + for _, content := range r.SetGitHubContents { |
| 52 | + debug.Log("event", "githubcontents.set", "received", content) |
| 53 | + split := strings.Split(content, ":") |
| 54 | + if len(split) != 4 { |
| 55 | + return nil, errors.Errorf("set-github-contents %q invalid, expected a REPO:REPO_PATH:REF:LOCAL_PATH", content) |
| 56 | + } |
| 57 | + repo := split[0] |
| 58 | + repoPath := split[1] |
| 59 | + ref := split[2] |
| 60 | + localpath := split[3] |
| 61 | + |
| 62 | + debug.Log("event", "githubcontents.loadFiles", "localPath", localpath) |
| 63 | + files, err := r.loadLocalGithubFiles(localpath, repoPath) |
| 64 | + if err != nil { |
| 65 | + return nil, errors.Wrapf(err, "set github files") |
| 66 | + } |
| 67 | + |
| 68 | + fakeGithubContents = append(fakeGithubContents, GithubContent{ |
| 69 | + Repo: repo, |
| 70 | + Path: repoPath, |
| 71 | + Ref: ref, |
| 72 | + Files: files, |
| 73 | + }) |
| 74 | + debug.Log("event", "githubcontents.set.finished", "received", content) |
| 75 | + } |
| 76 | + return fakeGithubContents, nil |
| 77 | +} |
| 78 | + |
| 79 | +func (r *resolver) loadLocalGithubFiles(localpath string, repoPath string) ([]GithubFile, error) { |
| 80 | + debug := level.Debug(log.With(r.Logger, "method", "loadLocalGitHubFiles")) |
| 81 | + var files []GithubFile |
| 82 | + err := r.FS.Walk(localpath, func(path string, info os.FileInfo, err error) error { |
| 83 | + if err != nil { |
| 84 | + return errors.Wrapf(err, "walk %s from %s", info.Name(), path) |
| 85 | + } |
| 86 | + |
| 87 | + if info.IsDir() { |
| 88 | + return nil |
| 89 | + } |
| 90 | + |
| 91 | + walkRepoPath := strings.TrimPrefix(path, localpath) |
| 92 | + if !strings.HasPrefix(walkRepoPath, repoPath) { |
| 93 | + return nil |
| 94 | + } |
| 95 | + |
| 96 | + contents, err := r.FS.ReadFile(path) |
| 97 | + if err != nil { |
| 98 | + return errors.Wrapf(err, "read %s from %s", info.Name(), path) |
| 99 | + } |
| 100 | + debug.Log("event", "githubcontents.loadFile.complete", "path", path, "name", info.Name()) |
| 101 | + |
| 102 | + encodedData := &bytes.Buffer{} |
| 103 | + encoder := base64.NewEncoder(base64.StdEncoding, encodedData) |
| 104 | + defer encoder.Close() |
| 105 | + encoder.Write(contents) |
| 106 | + sha := fmt.Sprintf("%x", sha256.Sum256(contents)) |
| 107 | + files = append(files, GithubFile{ |
| 108 | + Name: info.Name(), |
| 109 | + Path: walkRepoPath, |
| 110 | + Sha: sha, |
| 111 | + Size: info.Size(), |
| 112 | + Data: encodedData.String(), |
| 113 | + }) |
| 114 | + return nil |
| 115 | + }) |
| 116 | + return files, err |
| 117 | +} |
0 commit comments