forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatelocker.go
More file actions
53 lines (43 loc) · 1.04 KB
/
statelocker.go
File metadata and controls
53 lines (43 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// 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"
"time"
"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],
}
info := state.NewLockInfo()
info.Operation = "test"
info.Info = "state locker"
lockID, err := s.Lock(info)
if err != nil {
io.WriteString(os.Stderr, err.Error())
return
}
// signal to the caller that we're locked
io.WriteString(os.Stdout, "LOCKID "+lockID)
defer func() {
if err := s.Unlock(lockID); err != nil {
io.WriteString(os.Stderr, err.Error())
}
}()
c := make(chan os.Signal, 1)
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:
}
}