-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
125 lines (120 loc) · 2.73 KB
/
main.go
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"github.com/joshmyers/ddbsync"
"github.com/joshmyers/dynolocker/dynamodb"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
"os"
"sort"
"sync"
"time"
)
func lock(c *cli.Context, m sync.Locker) {
log.WithFields(log.Fields{
"name": c.GlobalString("name"),
"ttl": c.GlobalInt64("ttl"),
"table": c.GlobalString("table")},
).Debug("Locking table...")
m.Lock()
}
func unlock(c *cli.Context, m sync.Locker) {
log.WithFields(log.Fields{
"name": c.GlobalString("name"),
"table": c.GlobalString("table")},
).Debug("Unlocking table...")
m.Unlock()
}
func newDynolocker(c *cli.Context) sync.Locker {
s := ddbsync.NewLockService(c.GlobalString("table"), c.GlobalString("region"), "", c.GlobalBool("disable_ssl"))
return s.NewLock(c.GlobalString("name"), c.GlobalInt64("ttl"), c.GlobalDuration("retry"))
}
func main() {
app := cli.NewApp()
app.Name = "dynolocker"
app.Usage = "distributed locking using DynamoDB"
app.Version = "0.0.1"
app.Compiled = time.Now()
app.Authors = []cli.Author{
cli.Author{
Name: "Joshua Myers",
Email: "[email protected]",
},
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "table",
Value: "dynolocker",
Usage: "DynamoDB table for locks",
EnvVar: "DB_TABLE_NAME",
},
cli.StringFlag{
Name: "name",
Value: "lock",
Usage: "DynamoDB lock name",
EnvVar: "DB_LOCK_NAME",
},
cli.StringFlag{
Name: "region",
Value: "eu-west-1",
Usage: "AWS region",
EnvVar: "AWS_DEFAULT_REGION",
},
cli.DurationFlag{
Name: "retry",
Value: time.Second * 3,
Usage: "Lock reattempt wait duration",
},
cli.BoolTFlag{
Name: "disable-ssl",
Usage: "Disable SSL on calls to AWS (default: false)",
},
cli.Int64Flag{
Name: "ttl",
Value: 60,
Usage: "Lock duration",
EnvVar: "DB_TTL",
},
cli.BoolTFlag{
Name: "debug",
Usage: "Show debug output",
},
}
app.Before = func(c *cli.Context) error {
if c.GlobalBoolT("debug") {
log.SetLevel(log.DebugLevel)
}
return nil
}
app.Commands = []cli.Command{
{
Name: "lock",
Usage: "Create a lock",
Action: func(c *cli.Context) error {
if c.BoolT("create-table") {
dynamodb.CreateLockTableIfNecessary(c.GlobalString("table"), c.GlobalString("region"))
}
m := newDynolocker(c)
lock(c, m)
return nil
},
Flags: []cli.Flag{
cli.BoolTFlag{
Name: "create-table",
Usage: "If we should create the DynamoDB table (default: true)",
},
},
},
{
Name: "unlock",
Usage: "Force an unlock",
Action: func(c *cli.Context) error {
m := newDynolocker(c)
unlock(c, m)
return nil
},
},
}
sort.Sort(cli.FlagsByName(app.Flags))
sort.Sort(cli.CommandsByName(app.Commands))
app.Run(os.Args)
}