Command execution after certificate replacement
This commit is contained in:
parent
a2606b5b89
commit
50bff6a5cb
2 changed files with 49 additions and 1 deletions
48
buildcert.go
48
buildcert.go
|
@ -1,13 +1,17 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"os/user"
|
"os/user"
|
||||||
"strconv"
|
"strconv"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"time"
|
||||||
|
"unicode/utf8"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
@ -161,6 +165,50 @@ func (b *tCertificateBuilder) UpdatePrivileges() error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Run the necessary commands if the certificate file has been modified in
|
||||||
|
// any way. Execution will stop at the first failure.
|
||||||
|
func (b *tCertificateBuilder) RunCommandsIfChanged() error {
|
||||||
|
if !b.changed {
|
||||||
|
log.Debug("Not running commands")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
for i := range b.config.AfterUpdate {
|
||||||
|
err := b.RunCommand(i)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf(
|
||||||
|
"Failed while executing command '%s': %w",
|
||||||
|
b.config.AfterUpdate[i],
|
||||||
|
err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run a command through the `sh` shell.
|
||||||
|
func (b *tCertificateBuilder) RunCommand(pos int) error {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
log := b.logger.WithField("command", b.config.AfterUpdate[pos])
|
||||||
|
log.Debug("Executing command")
|
||||||
|
cmd := exec.CommandContext(ctx, "sh", "-c", b.config.AfterUpdate[pos])
|
||||||
|
output, err := cmd.CombinedOutput()
|
||||||
|
if len(output) != 0 {
|
||||||
|
if utf8.Valid(output) {
|
||||||
|
log = log.WithField("output", string(output))
|
||||||
|
} else {
|
||||||
|
log = log.WithField("output", string(output))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err == nil {
|
||||||
|
log.Info("Command executed")
|
||||||
|
} else {
|
||||||
|
log.WithField("error", err).Error("Command failed")
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// Append PEM files from a list.
|
// Append PEM files from a list.
|
||||||
func (b *tCertificateBuilder) appendPemFiles(files []string) error {
|
func (b *tCertificateBuilder) appendPemFiles(files []string) error {
|
||||||
for _, path := range files {
|
for _, path := range files {
|
||||||
|
|
2
main.go
2
main.go
|
@ -95,6 +95,6 @@ func main() {
|
||||||
log.WithField("error", err).Error("Failed to update privileges on '", cfg.Certificates[i].Path, "'")
|
log.WithField("error", err).Error("Failed to update privileges on '", cfg.Certificates[i].Path, "'")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// TODO builder.RunCommandsIfChanged()
|
builder.RunCommandsIfChanged()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue