2017-02-03 21:31:21 +01:00
|
|
|
// statelocker use used for testing command with a locked state.
|
|
|
|
// This will lock the state file at a given path, then wait for a sigal. On
|
|
|
|
// SIGINT and SIGTERM the state will be Unlocked before exit.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2017-02-03 21:56:19 +01:00
|
|
|
"time"
|
2017-02-03 21:31:21 +01:00
|
|
|
|
2021-05-17 21:07:38 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/command/clistate"
|
2021-05-17 21:43:35 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/states/statemgr"
|
2017-02-03 21:31:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if len(os.Args) != 2 {
|
|
|
|
log.Fatal(os.Args[0], "statefile")
|
|
|
|
}
|
|
|
|
|
2020-08-11 17:43:01 +02:00
|
|
|
s := &clistate.LocalState{
|
2017-02-03 21:31:21 +01:00
|
|
|
Path: os.Args[1],
|
|
|
|
}
|
|
|
|
|
2020-08-11 17:43:01 +02:00
|
|
|
info := statemgr.NewLockInfo()
|
2017-02-15 16:25:04 +01:00
|
|
|
info.Operation = "test"
|
|
|
|
info.Info = "state locker"
|
|
|
|
|
|
|
|
lockID, err := s.Lock(info)
|
2017-02-03 21:31:21 +01:00
|
|
|
if err != nil {
|
|
|
|
io.WriteString(os.Stderr, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// signal to the caller that we're locked
|
2017-02-15 17:53:19 +01:00
|
|
|
io.WriteString(os.Stdout, "LOCKID "+lockID)
|
2017-02-03 21:31:21 +01:00
|
|
|
|
|
|
|
defer func() {
|
2017-02-15 00:51:32 +01:00
|
|
|
if err := s.Unlock(lockID); err != nil {
|
2017-02-03 21:31:21 +01:00
|
|
|
io.WriteString(os.Stderr, err.Error())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
c := make(chan os.Signal, 1)
|
2017-02-03 21:56:19 +01:00
|
|
|
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
|
|
|
|
|
|
|
|
// timeout after 10 second in case we don't get cleaned up by the test
|
|
|
|
select {
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
case <-c:
|
|
|
|
}
|
2017-02-03 21:31:21 +01:00
|
|
|
}
|