2017-02-03 14:31:21 -06: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 14:56:19 -06:00
|
|
|
"time"
|
2017-02-03 14:31:21 -06:00
|
|
|
|
2021-05-17 14:07:38 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/command/clistate"
|
2021-05-17 14:43:35 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/states/statemgr"
|
2017-02-03 14:31:21 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if len(os.Args) != 2 {
|
|
|
|
log.Fatal(os.Args[0], "statefile")
|
|
|
|
}
|
|
|
|
|
2020-08-11 10:43:01 -05:00
|
|
|
s := &clistate.LocalState{
|
2017-02-03 14:31:21 -06:00
|
|
|
Path: os.Args[1],
|
|
|
|
}
|
|
|
|
|
2020-08-11 10:43:01 -05:00
|
|
|
info := statemgr.NewLockInfo()
|
2017-02-15 09:25:04 -06:00
|
|
|
info.Operation = "test"
|
|
|
|
info.Info = "state locker"
|
|
|
|
|
|
|
|
lockID, err := s.Lock(info)
|
2017-02-03 14:31:21 -06:00
|
|
|
if err != nil {
|
|
|
|
io.WriteString(os.Stderr, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// signal to the caller that we're locked
|
2017-02-15 10:53:19 -06:00
|
|
|
io.WriteString(os.Stdout, "LOCKID "+lockID)
|
2017-02-03 14:31:21 -06:00
|
|
|
|
|
|
|
defer func() {
|
2017-02-14 17:51:32 -06:00
|
|
|
if err := s.Unlock(lockID); err != nil {
|
2017-02-03 14:31:21 -06:00
|
|
|
io.WriteString(os.Stderr, err.Error())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
c := make(chan os.Signal, 1)
|
2017-02-03 14:56:19 -06: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 14:31:21 -06:00
|
|
|
}
|