Skip to content

Commit

Permalink
sia run-after-scripts for cmd line options must be in blocking mode (#…
Browse files Browse the repository at this point in the history
…2529)

Signed-off-by: Henry Avetisyan <[email protected]>
Co-authored-by: Henry Avetisyan <[email protected]>
  • Loading branch information
havetisyan and havetisyan authored Feb 27, 2024
1 parent 11e5262 commit aebd6fd
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 20 deletions.
12 changes: 6 additions & 6 deletions libs/go/sia/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,15 +629,15 @@ func RunAgent(siaCmd, ztsUrl string, opts *options.Options) {
log.Fatalf("unable to fetch %d out of %d requested role certificates\n", failures, count)
}
if count != 0 {
util.ExecuteScriptWithoutBlock(opts.RunAfterParts)
util.ExecuteScript(opts.RunAfterParts)
}
case "token":
if tokenOpts != nil {
err := fetchAccessToken(tokenOpts)
if err != nil && !skipErrors {
log.Fatalf("Unable to fetch access tokens, err: %v\n", err)
}
util.ExecuteScriptWithoutBlock(opts.RunAfterTokensParts)
util.ExecuteScript(opts.RunAfterTokensParts)
} else {
log.Print("unable to fetch access tokens, invalid or missing configuration")
}
Expand All @@ -646,14 +646,14 @@ func RunAgent(siaCmd, ztsUrl string, opts *options.Options) {
if err != nil {
log.Fatalf("Unable to register identity, err: %v\n", err)
}
util.ExecuteScriptWithoutBlock(opts.RunAfterParts)
util.ExecuteScript(opts.RunAfterParts)
log.Printf("identity registered for services: %s\n", svcs)
case "rotate", "refresh":
err = RefreshInstance(ztsUrl, opts)
if err != nil {
log.Fatalf("Refresh identity failed, err: %v\n", err)
}
util.ExecuteScriptWithoutBlock(opts.RunAfterParts)
util.ExecuteScript(opts.RunAfterParts)
log.Printf("Identity successfully refreshed for services: %s\n", svcs)
case "init":
err := RegisterInstance(ztsUrl, opts, false)
Expand All @@ -665,13 +665,13 @@ func RunAgent(siaCmd, ztsUrl string, opts *options.Options) {
if failures != 0 && !skipErrors {
log.Fatalf("unable to fetch %d out of %d requested role certificates\n", failures, count)
}
util.ExecuteScriptWithoutBlock(opts.RunAfterParts)
util.ExecuteScript(opts.RunAfterParts)
if tokenOpts != nil {
err := fetchAccessToken(tokenOpts)
if err != nil && !skipErrors {
log.Fatalf("Unable to fetch access tokens, err: %v\n", err)
}
util.ExecuteScriptWithoutBlock(opts.RunAfterTokensParts)
util.ExecuteScript(opts.RunAfterTokensParts)
}
default:
// we're going to iterate through our configured services.
Expand Down
12 changes: 6 additions & 6 deletions libs/go/sia/aws/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,15 +625,15 @@ func RunAgent(siaCmd, ztsUrl string, opts *options.Options) {
log.Fatalf("unable to fetch %d out of %d requested role certificates\n", failures, count)
}
if count != 0 {
util.ExecuteScriptWithoutBlock(opts.RunAfterParts)
util.ExecuteScript(opts.RunAfterParts)
}
case "token":
if tokenOpts != nil {
err := fetchAccessToken(tokenOpts)
if err != nil && !skipErrors {
log.Fatalf("Unable to fetch access tokens, err: %v\n", err)
}
util.ExecuteScriptWithoutBlock(opts.RunAfterTokensParts)
util.ExecuteScript(opts.RunAfterTokensParts)
} else {
log.Print("unable to fetch access tokens, invalid or missing configuration")
}
Expand All @@ -642,14 +642,14 @@ func RunAgent(siaCmd, ztsUrl string, opts *options.Options) {
if err != nil {
log.Fatalf("Unable to register identity, err: %v\n", err)
}
util.ExecuteScriptWithoutBlock(opts.RunAfterParts)
util.ExecuteScript(opts.RunAfterParts)
log.Printf("identity registered for services: %s\n", svcs)
case "rotate", "refresh":
err = RefreshInstance(data, ztsUrl, opts)
if err != nil {
log.Fatalf("Refresh identity failed, err: %v\n", err)
}
util.ExecuteScriptWithoutBlock(opts.RunAfterParts)
util.ExecuteScript(opts.RunAfterParts)
log.Printf("Identity successfully refreshed for services: %s\n", svcs)
case "init":
err := RegisterInstance(data, ztsUrl, opts, false)
Expand All @@ -661,13 +661,13 @@ func RunAgent(siaCmd, ztsUrl string, opts *options.Options) {
if failures != 0 && !skipErrors {
log.Fatalf("unable to fetch %d out of %d requested role certificates\n", failures, count)
}
util.ExecuteScriptWithoutBlock(opts.RunAfterParts)
util.ExecuteScript(opts.RunAfterParts)
if tokenOpts != nil {
err := fetchAccessToken(tokenOpts)
if err != nil && !skipErrors {
log.Fatalf("Unable to fetch access tokens, err: %v\n", err)
}
util.ExecuteScriptWithoutBlock(opts.RunAfterTokensParts)
util.ExecuteScript(opts.RunAfterTokensParts)
}
default:
// we're going to iterate through our configured services.
Expand Down
3 changes: 3 additions & 0 deletions libs/go/sia/util/data/test_after_script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

touch /tmp/test-after-script
23 changes: 15 additions & 8 deletions libs/go/sia/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -1280,19 +1280,26 @@ func ParseScriptArguments(script string) []string {
return parts
}

// ExecuteScriptWithoutBlock executes a script along with the provided
// arguments in a go subroutine without blocking the agent
func ExecuteScriptWithoutBlock(script []string) {
// ExecuteScript executes a script along with the provided
// arguments while blocking the agent
func ExecuteScript(script []string) error {
// execute run after script (if provided)
if len(script) == 0 {
return
return nil
}
log.Printf("executing run after hook for: %v", script)
err := exec.Command(script[0], script[1:]...).Run()
if err != nil {
log.Printf("unable to execute: %q, err: %v", script, err)
}
return err
}

// ExecuteScriptWithoutBlock executes a script along with the provided
// arguments in a go subroutine without blocking the agent
func ExecuteScriptWithoutBlock(script []string) {
go func() {
log.Printf("executing run after hook for: %v", script)
if err := exec.Command(script[0], script[1:]...).Run(); err != nil {
log.Printf("unable to execute: %q, err: %v", script, err)
}
ExecuteScript(script)
}()
}

Expand Down
16 changes: 16 additions & 0 deletions libs/go/sia/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1681,3 +1681,19 @@ func TestParseSiaCmd(test *testing.T) {
})
}
}

func TestExecuteScript(test *testing.T) {

// non-existent script
err := ExecuteScript([]string{"unknown-script"})
assert.NotNil(test, err)

// remove our test file if it exists
os.Remove("/tmp/test-after-script")
// valid script
err = ExecuteScript([]string{"data/test_after_script.sh"})
assert.Nil(test, err)
// verify our test file was created
_, err = os.Stat("/tmp/test-after-script")
assert.Nil(test, err)
}

0 comments on commit aebd6fd

Please sign in to comment.