Skip to content

Commit

Permalink
#60: ability to hack build with uncommitted values
Browse files Browse the repository at this point in the history
  • Loading branch information
iignatevich committed Dec 12, 2024
1 parent df82ec2 commit 0307bfe
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 35 deletions.
109 changes: 76 additions & 33 deletions actionSync.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var (
const (
vaultpassKey = "vaultpass"
domainNamespace = "domain"
buildHackAuthor = "override"
)

// SyncAction is a type representing a resources version synchronization action.
Expand All @@ -51,8 +52,9 @@ type SyncAction struct {
timeline []sync.TimelineItem

// options.
dryRun bool
vaultPass string
dryRun bool
allowOverride bool
vaultPass string
}

type hashStruct struct {
Expand Down Expand Up @@ -382,6 +384,27 @@ func (s *SyncAction) findResourcesChangeTime(namespaceResources *sync.OrderedMap

hashesMap := make(map[string]*hashStruct)
toIterate := namespaceResources.ToDict()
currentVersions := map[string]string{}

if s.allowOverride {
for k, resource := range toIterate {
if _, ok := hashesMap[k]; !ok {
hashesMap[k] = &hashStruct{}
}

hashesMap[k].hash = buildHackAuthor
hashesMap[k].hashTime = time.Now()
hashesMap[k].author = buildHackAuthor

buildResource := sync.NewResource(resource.GetName(), s.buildDir)
version, err := buildResource.GetVersion()
if err != nil {
return err
}

currentVersions[k] = version
}
}

remainingDebug := len(toIterate)
err = cIter.ForEach(func(c *object.Commit) error {
Expand All @@ -400,23 +423,28 @@ func (s *SyncAction) findResourcesChangeTime(namespaceResources *sync.OrderedMap
}

resourceMetaPath := resource.BuildMetaPath()
resourceVersion, err := resource.GetVersion()
if err != nil {
return err
resourceVersion, ok := currentVersions[k]
if !ok {
resourceVersion, err = resource.GetVersion()
if err != nil {
return err
}
currentVersions[k] = resourceVersion
}

file, errIt := c.File(resourceMetaPath)
if errIt != nil {
if !errors.Is(errIt, object.ErrFileNotFound) {
return fmt.Errorf("open file %s in commit %s > %w", resourceMetaPath, c.Hash, errIt)
}

if hashesMap[k].hash == "" {
hashesMap[k].hash = c.Hash.String()
hashesMap[k].hashTime = c.Author.When
hashesMap[k].author = c.Author.Name
}

// File didn't exist before, take current hash as version",
// File didn't exist before, take current hash as version,
delete(toIterate, k)
continue
}
Expand Down Expand Up @@ -449,14 +477,12 @@ func (s *SyncAction) findResourcesChangeTime(namespaceResources *sync.OrderedMap

for n, hm := range hashesMap {
r, _ := namespaceResources.Get(n)
resourceVersion, err := r.GetVersion()
if err != nil {
return err
}
resourceVersion := currentVersions[n]

launchr.Log().Debug("add resource to timeline",
slog.String("mrn", r.GetName()),
slog.String("version", hm.hash),
slog.String("commit", hm.hash),
slog.String("version", resourceVersion),
slog.Time("date", hm.hashTime),
)

Expand Down Expand Up @@ -550,20 +576,53 @@ func (s *SyncAction) findVariableUpdateTime(varsFile string, gitPath string, mx
return err
}

isVault := sync.IsVaultFile(varsFile)
varsYaml, err := sync.LoadVariablesFile(varsFile, s.vaultPass, sync.IsVaultFile(varsFile))
headCommit, err := repo.CommitObject(ref.Hash())
if err != nil {
return err
}

var varsYaml map[string]any
hashesMap := make(map[string]*hashStruct)
variablesMap := sync.NewOrderedMap[*sync.Variable]()
isVault := sync.IsVaultFile(varsFile)

if s.allowOverride {
varsYaml, err = sync.LoadVariablesFile(filepath.Join(s.buildDir, varsFile), s.vaultPass, isVault)
if err != nil {
return err
}
} else {
file, errF := headCommit.File(varsFile)
if errF != nil {
return errF
}

varsYaml, err = s.loadVariablesFileFromBytes(file, varsFile, isVault)
if err != nil {
return err
}
}

for k, value := range varsYaml {
v := sync.NewVariable(varsFile, k, HashString(fmt.Sprint(value)), isVault)
variablesMap.Set(k, v)

if _, ok := hashesMap[k]; !ok {
hashesMap[k] = &hashStruct{}
}

if s.allowOverride {
hashesMap[k].hash = fmt.Sprint(v.GetHash())
hashesMap[k].hashTime = time.Now()
hashesMap[k].author = buildHackAuthor
} else {
hashesMap[k].hash = headCommit.Hash.String()
hashesMap[k].hashTime = headCommit.Author.When
hashesMap[k].author = headCommit.Author.Name
}
}

toIterate := variablesMap.ToDict()
hashesMap := make(map[string]*hashStruct)

cIter, err := repo.Log(&git.LogOptions{From: ref.Hash()})
if err != nil {
Expand All @@ -581,18 +640,6 @@ func (s *SyncAction) findVariableUpdateTime(varsFile string, gitPath string, mx
launchr.Log().Debug(fmt.Sprintf("Remaining unidentified variables, %s - %d", varsFile, remainingDebug))
}

if ref.Hash().String() == c.Hash.String() {
for k := range toIterate {
if _, ok := hashesMap[k]; !ok {
hashesMap[k] = &hashStruct{}
}

hashesMap[k].hash = c.Hash.String()
hashesMap[k].hashTime = c.Author.When
hashesMap[k].author = c.Author.Name
}
}

file, errIt := c.File(varsFile)
if errIt != nil {
if !errors.Is(errIt, object.ErrFileNotFound) {
Expand All @@ -605,23 +652,19 @@ func (s *SyncAction) findVariableUpdateTime(varsFile string, gitPath string, mx
varFile, errIt := s.loadVariablesFileFromBytes(file, varsFile, isVault)
if errIt != nil {
if strings.Contains(errIt.Error(), "did not find expected key") || strings.Contains(errIt.Error(), "could not find expected") {
launchr.Term().Warning().Printfln("Broken file %s in commit %s detected > %w", varsFile, c.Hash.String(), err)
launchr.Term().Warning().Printfln("Broken file %s in commit %s detected > %s", varsFile, c.Hash.String(), errIt.Error())
return nil
}

if strings.Contains(errIt.Error(), "invalid password for vault") || strings.Contains(errIt.Error(), "could not find expected") {
launchr.Term().Warning().Printfln("invalid password for vault - %s in commit %s detected stopping iteration", varsFile, c.Hash.String())
launchr.Term().Warning().Printfln("invalid password for vault - %s in commit %s detected, stopping iteration", varsFile, c.Hash.String())
return storer.ErrStop
}

return fmt.Errorf("commit %s > %w", c.Hash, errIt)
}

for k, hh := range toIterate {
if _, ok := hashesMap[k]; !ok {
hashesMap[k] = &hashStruct{}
}

prevVar, exists := varFile[k]
if !exists {
// Variable didn't exist before, take current hash as version
Expand Down Expand Up @@ -656,7 +699,7 @@ func (s *SyncAction) findVariableUpdateTime(varsFile string, gitPath string, mx
version := hm.hash[:13]
launchr.Log().Debug("add variable to timeline",
slog.String("variable", v.GetName()),
slog.String("version", hm.hash),
slog.String("version", version),
slog.Time("date", hm.hashTime),
slog.String("path", v.GetPath()),
)
Expand Down
7 changes: 5 additions & 2 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func (p *Plugin) OnAppInit(app launchr.App) error {
func (p *Plugin) CobraAddCommands(rootCmd *cobra.Command) error {
var doSync bool
var dryRun bool
var allowOverride bool
var vaultpass string
var last bool

Expand All @@ -57,8 +58,9 @@ func (p *Plugin) CobraAddCommands(rootCmd *cobra.Command) error {
buildDir: ".compose/build",
packagesDir: ".compose/packages",

dryRun: dryRun,
vaultPass: vaultpass,
dryRun: dryRun,
allowOverride: allowOverride,
vaultPass: vaultpass,
}

err := syncAction.Execute()
Expand All @@ -72,6 +74,7 @@ func (p *Plugin) CobraAddCommands(rootCmd *cobra.Command) error {

bumpCmd.Flags().BoolVarP(&doSync, "sync", "s", false, "Propagate versions of updated components to their dependencies")
bumpCmd.Flags().BoolVar(&dryRun, "dry-run", false, "Simulate bump or sync without updating anything")
bumpCmd.Flags().BoolVar(&allowOverride, "allow-override", false, "Allow override committed version by current build value")
bumpCmd.Flags().StringVar(&vaultpass, "vault-pass", "", "Password for Ansible Vault")
bumpCmd.Flags().BoolVarP(&last, "last", "l", false, "Bump resources modified in last commit only")

Expand Down

0 comments on commit 0307bfe

Please sign in to comment.