-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
94 lines (87 loc) · 2.14 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
package main
import (
"github.com/alphagov/dynolocker/dynamodb"
"github.com/urfave/cli"
"github.com/zencoder/ddbsync"
"os"
"sort"
"time"
)
const (
DB_LOCK_RETRY time.Duration = 10 * time.Second
)
func lock(c *cli.Context) {
if c.GlobalBool("create_table") {
dynamodb.CreateLockTableIfNecessary(c.GlobalString("table_name"), c.GlobalString("region"))
}
s := ddbsync.NewLockService(c.GlobalString("table_name"), c.GlobalString("region"), "", c.GlobalBool("disable_ssl"))
m := s.NewLock(c.GlobalString("lock_name"), c.GlobalInt64("lock_ttl"), DB_LOCK_RETRY)
m.Lock()
}
func unlock(c *cli.Context) {
s := ddbsync.NewLockService(c.GlobalString("table_name"), c.GlobalString("region"), "", c.GlobalBool("disable_ssl"))
m := s.NewLock(c.GlobalString("lock_name"), c.GlobalInt64("lock_ttl"), DB_LOCK_RETRY)
m.Unlock()
}
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: "GOV.UK Pay Operations",
Email: "gds-team-payments-web-ops@digital.cabinet-office.gov.uk",
},
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "table_name",
Value: "dynolocker",
Usage: "DynamoDB table for locks",
EnvVar: "DB_TABLE_NAME",
},
cli.StringFlag{
Name: "lock_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.Int64Flag{
Name: "lock_ttl",
Value: 60,
Usage: "Lock duration",
EnvVar: "DB_TTL",
},
cli.BoolFlag{
Name: "disable_ssl",
Usage: "Disable SSL on calls to AWS (default: false)",
},
cli.BoolTFlag{
Name: "create_table",
Usage: "If we should create the DynamoDB table (default: true)",
},
}
app.Commands = []cli.Command{
{
Name: "lock",
Usage: "Create a lock",
Action: lock,
},
{
Name: "unlock",
Usage: "Force an unlock",
Action: unlock,
},
}
sort.Sort(cli.FlagsByName(app.Flags))
sort.Sort(cli.CommandsByName(app.Commands))
app.Run(os.Args)
}