This repository has been archived by the owner on Aug 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
consul_build_id.go
157 lines (115 loc) · 3.07 KB
/
consul_build_id.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package ver
import (
"fmt"
"net/url"
"strconv"
"github.com/hashicorp/consul/api"
)
func RetrieveBuildFromConsul(consulUrl string, kvProjectRoot string, vi *VersionInformation) error {
// check version info
if kvProjectRoot == "" {
return fmt.Errorf("Consul KV project root path may not be empty")
}
if vi.Revision == "" {
return fmt.Errorf("No revision information available")
}
// parse the url
cu, err := url.Parse(consulUrl)
if err != nil {
return fmt.Errorf("Failed to parse consul URL: %v", err)
}
// determine port
port := cu.Port()
if port == "" {
switch cu.Scheme {
case "http":
port = "80"
case "https":
port = "443"
}
}
// assign config
cfg := api.DefaultConfig()
cfg.Address = fmt.Sprintf("%v:%v", cu.Host, port)
cfg.Scheme = cu.Scheme
cfg.Datacenter = cu.Path[1:] // trim first slash
if cu.User != nil {
if t, ok := cu.User.Password(); ok {
cfg.Token = t
}
}
// connect
client, err := api.NewClient(cfg)
if err != nil {
return fmt.Errorf("Failed to connect to consul: %v", err)
}
kv := client.KV()
// create session
session, _, err := client.Session().Create(nil, nil)
if err != nil {
return fmt.Errorf("Failed to create session: %v", err)
}
defer client.Session().Destroy(session, nil)
// acquire revision
revPath := fmt.Sprintf("%v/revs/%v", kvProjectRoot, vi.Revision)
lockPath := fmt.Sprintf("%v/~lock", revPath)
locked, _, err := kv.Acquire(&api.KVPair{Key: lockPath, Session: session}, nil)
if err != nil {
return fmt.Errorf("Failed to acquire lock: %v", err)
}
if !locked {
return fmt.Errorf("Failed to acquire lock on KV path: %v", lockPath)
}
defer kv.Release(&api.KVPair{Key: lockPath, Session: session}, nil)
// retrieve any existing
build, _, err := kv.Get(fmt.Sprintf("%v/build", revPath), nil)
if err != nil {
return fmt.Errorf("Failed to retrieve existing build id: %v", err)
}
if build != nil {
b, err := strconv.Atoi(string(build.Value))
if err != nil {
return fmt.Errorf("Failed to parse existing build id: %v", err)
}
vi.Build = b
return nil
}
// get new build id
for true {
nextIdPath := fmt.Sprintf("%v/nextId", kvProjectRoot)
nextId, _, err := kv.Get(nextIdPath, nil)
if err != nil {
return fmt.Errorf("Failed to get next build id: %v", err)
}
if nextId != nil {
nextId.Session = session
// parse the id
vi.Build, err = strconv.Atoi(string(nextId.Value))
if err != nil {
return fmt.Errorf("Failed to parse next build id: %v", err)
}
// increment the id
nextId.Value = []byte(strconv.Itoa(vi.Build + 1))
} else {
// use 1 as first ID, and register 2 as the next one
vi.Build = 1
nextId = &api.KVPair{Key: nextIdPath, Value: []byte("2"), Session: session}
}
// update the id
ok, _, err := kv.CAS(nextId, nil)
if ok {
break
}
}
// write the revision
_, err = kv.Put(&api.KVPair{
Key: fmt.Sprintf("%v/build", revPath),
Value: []byte(strconv.Itoa(vi.Build)),
Session: session,
}, nil)
if err != nil {
return fmt.Errorf("Failed to write build revision: %v", err)
}
// done
return nil
}