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
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/state"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if len(os.Args) != 2 {
|
|
|
|
log.Fatal(os.Args[0], "statefile")
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &state.LocalState{
|
|
|
|
Path: os.Args[1],
|
|
|
|
}
|
|
|
|
|
2017-02-15 00:51:32 +01:00
|
|
|
lockID, err := s.Lock(&state.LockInfo{Operation: "test", Info: "state locker"})
|
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
|
|
|
|
io.WriteString(os.Stdout, "LOCKED")
|
|
|
|
|
|
|
|
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
|
|
|
}
|