This repository has been archived by the owner on Jun 12, 2023. It is now read-only.
/
state.go
241 lines (191 loc) · 5.52 KB
/
state.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package atlas
import (
"context"
"encoding/json"
"fmt"
"github.com/brunoscheufler/atlas/atlasfile"
"github.com/brunoscheufler/atlas/docker"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
"os"
"path/filepath"
)
type Statefile struct {
Version string `json:"version"`
Stacks []StateStack `json:"stacks"`
Volumes []string `json:"volumes"`
}
func (s *Statefile) GetStack(stackName string) *StateStack {
for _, stack := range s.Stacks {
if stack.Name == stackName {
return &stack
}
}
return nil
}
func (s *StateStack) GetService(serviceName string) *StateService {
for _, service := range s.Services {
if service.Name == serviceName {
return &service
}
}
return nil
}
func (s *Statefile) GetStacks(stackNames []string) ([]StateStack, error) {
if len(stackNames) == 0 {
return s.Stacks, nil
}
stacks := make([]StateStack, 0)
for _, stackName := range stackNames {
for _, stack := range s.Stacks {
if stack.Name == stackName {
stacks = append(stacks, stack)
break
}
}
}
return stacks, nil
}
type StateStack struct {
Name string `json:"name"`
Network string `json:"network"`
Services []StateService `json:"services"`
}
type StateService struct {
Name string `json:"name"`
ContainerName string `json:"containerName"`
ContainerInfos *docker.ContainerInfos `json:"containerInfo"`
}
func getStatefilePath(cwd string) string {
return filepath.Join(cwd, ".atlas", "state.json")
}
func refreshState(ctx context.Context, rootDir string, stateFile *Statefile) error {
newStacks := make([]StateStack, 0)
for _, stack := range stateFile.Stacks {
// Refresh service containers
currentServices := make([]StateService, 0)
for _, service := range stack.Services {
infos, err := docker.GetContainerInfo(ctx, service.ContainerName)
if err != nil {
return fmt.Errorf("could not get container info: %w", err)
}
if infos == nil {
continue
}
currentServices = append(currentServices, StateService{
Name: service.Name,
ContainerName: service.ContainerName,
ContainerInfos: infos,
})
}
stack.Services = currentServices
// Refresh network
networkId, err := docker.GetNetworkId(ctx, stack.Network)
if err != nil {
return fmt.Errorf("could not get network id: %w", err)
}
if networkId != "" && len(currentServices) > 0 {
newStacks = append(newStacks, stack)
}
}
stateFile.Stacks = newStacks
err := writeStateFileRaw(rootDir, stateFile)
if err != nil {
return fmt.Errorf("could not write state file: %w", err)
}
return nil
}
func readState(ctx context.Context, rootDir, version string, logger logrus.FieldLogger) (*Statefile, error) {
stateFile := Statefile{}
stateFilePath := getStatefilePath(rootDir)
_, err := os.Stat(stateFilePath)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, fmt.Errorf("could not stat state file: %w", err)
}
marshalled, err := os.ReadFile(stateFilePath)
if err != nil {
return nil, fmt.Errorf("could not read state file: %w", err)
}
err = json.Unmarshal(marshalled, &stateFile)
if err != nil {
return nil, fmt.Errorf("could not unmarshal state file: %w", err)
}
if stateFile.Version != version {
_ = clearStatefile(rootDir)
logger.Warnf("Found an existing state file from older (current: %s, stored: %s) version. Please clean remaining resources manually or run atlas down --all to remove all containers and volumes.\n", version, stateFile.Version)
return nil, nil
}
err = refreshState(ctx, rootDir, &stateFile)
if err != nil {
return nil, fmt.Errorf("could not refresh state: %w", err)
}
return &stateFile, nil
}
func clearStatefile(rootDir string) error {
stateFilePath := getStatefilePath(rootDir)
err := os.Remove(stateFilePath)
if err != nil {
return fmt.Errorf("could not remove state file: %w", err)
}
return nil
}
func writeState(ctx context.Context, rootDir, version string, stacks []atlasfile.StackConfig, volumes docker.EnsuredVolumes, networks docker.EnsuredNetworks) error {
stateStacks := make([]StateStack, len(stacks))
for i := range stacks {
services := make([]StateService, len(stacks[i].Services))
stack := stacks[i]
{
g, ctx := errgroup.WithContext(ctx)
for j := range stack.Services {
j := j
svc := stack.Services[j]
g.Go(func() error {
containerName := stack.GetContainerName(svc.Name)
containerInfos, err := docker.GetContainerInfo(ctx, containerName)
if err != nil {
return fmt.Errorf("could not get container infos: %w", err)
}
services[j] = StateService{
Name: svc.Name,
ContainerName: containerName,
ContainerInfos: containerInfos,
}
return nil
})
}
err := g.Wait()
if err != nil {
return fmt.Errorf("could not write container state: %w", err)
}
}
stateStacks[i] = StateStack{
Name: stack.Name,
Services: services,
Network: networks.Get(stack.Name),
}
}
volumeNames := make([]string, len(volumes))
for i := range volumes {
volumeNames[i] = volumes[i].PhysicalName
}
stateFile := Statefile{
Version: version,
Stacks: stateStacks,
Volumes: volumeNames,
}
return writeStateFileRaw(rootDir, &stateFile)
}
func writeStateFileRaw(rootDir string, stateFile *Statefile) error {
marshalled, err := json.Marshal(stateFile)
if err != nil {
return fmt.Errorf("could not marshal state file: %w", err)
}
err = os.WriteFile(getStatefilePath(rootDir), marshalled, 0644)
if err != nil {
return fmt.Errorf("could not write state file: %w", err)
}
return nil
}