Skip to content

Commit

Permalink
Add finally hooks type
Browse files Browse the repository at this point in the history
It's now possible define `finally` hooks
executed also when something goes wrong.

The `finally` hooks could be defined only
in the node.

Closes: #5
  • Loading branch information
geaaru committed Dec 8, 2024
1 parent e9264ea commit 2279417
Showing 1 changed file with 133 additions and 0 deletions.
133 changes: 133 additions & 0 deletions pkg/loader/deploy_node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
Copyright © 2024 Daniele Rondina <[email protected]>
See AUTHORS and LICENSE for the license details and contributors.
*/
package loader

import (
"fmt"
"path/filepath"
"strings"

specs "github.com/MottainaiCI/ssh-compose/pkg/specs"
"github.com/MottainaiCI/ssh-compose/pkg/template"
)

func (i *SshCInstance) ApplyNode(node *specs.SshCNode,
group *specs.SshCGroup, proj *specs.SshCProject, env *specs.SshCEnvironment,
compiler template.SshCTemplateCompiler) error {

syncSourceDir := ""
envBaseAbs, err := filepath.Abs(filepath.Dir(env.File))
if err != nil {
return err
}

// Retrieve pre-node-sync hooks of the node from project
preSyncHooks := i.GetNodeHooks4Event(specs.HookPreNodeSync, proj, group, node)

// Run pre-node-sync hooks
err = i.ProcessHooks(&preSyncHooks, proj, group, env, node)
if err != nil {
return err
}

// We need reload variables updated from out2var/err2var hooks.
compiler.InitVars()

// Compile node templates
err = template.CompileNodeFiles(*node, compiler, template.CompilerOpts{})
if err != nil {
return err
}

if len(node.SyncResources) > 0 && !i.SkipSync {
if node.SourceDir != "" {
if node.IsSourcePathRelative() {
syncSourceDir = filepath.Join(envBaseAbs, node.SourceDir)
} else {
syncSourceDir = node.SourceDir
}
} else {
// Use env file directory
syncSourceDir = envBaseAbs
}

executor, err := i.getExecutor(node.GetName(), node.Endpoint)
if err != nil {
i.Logger.Error("Error on retrieve executor of the node " +
node.GetName() + ": " + err.Error())
return err
}
// TODO: propagate sftp client options
err = executor.SetupSftp()
if err != nil {
i.Logger.Error("Error on setup sftp client on executor of the node " +
node.GetName() + ": " + err.Error())
return err
}

i.Logger.Debug(i.Logger.Aurora.Bold(
i.Logger.Aurora.BrightCyan(
">>> [" + node.GetName() + "] Using sync source basedir " +
syncSourceDir)))

nResources := len(node.SyncResources)
i.Logger.InfoC(
i.Logger.Aurora.Bold(
i.Logger.Aurora.BrightCyan(
fmt.Sprintf(">>> [%s] Syncing %d resources... - :bus:",
node.GetName(), nResources))))

for idx, resource := range node.SyncResources {

var sourcePath string

if filepath.IsAbs(resource.Source) {
sourcePath = resource.Source
} else {
sourcePath = filepath.Join(syncSourceDir, resource.Source)
}

i.Logger.DebugC(
i.Logger.Aurora.Italic(
i.Logger.Aurora.BrightCyan(
fmt.Sprintf(">>> [%s] %s => %s",
node.GetName(), resource.Source,
resource.Destination))))

// TODO: Propagate this options from config
ensurePerms := false

if strings.HasSuffix(resource.Source, "/") {
sourcePath += "/"
}

err = executor.RecursivePushFile(node.GetName(),
sourcePath, resource.Destination, ensurePerms)
if err != nil {
i.Logger.Debug("Error on sync from sourcePath " + sourcePath +
" to dest " + resource.Destination)
i.Logger.Error("Error on sync " + resource.Source + ": " + err.Error())
return err
}

i.Logger.InfoC(
i.Logger.Aurora.BrightCyan(
fmt.Sprintf(">>> [%s] - [%2d/%2d] %s - :check_mark:",
node.GetName(), idx+1, nResources, resource.Destination)))
}

}

// Retrieve post-node-sync hooks of the node from project
postSyncHooks := i.GetNodeHooks4Event(specs.HookPostNodeSync, proj, group, node)

// Run post-node-sync hooks
err = i.ProcessHooks(&postSyncHooks, proj, group, env, node)
if err != nil {
return err
}

return nil
}

0 comments on commit 2279417

Please sign in to comment.