Skip to content

Commit 05df5ed

Browse files
authored
fix: update bootstrap, save concurrency bug' (#136)
1 parent bd068b7 commit 05df5ed

File tree

9 files changed

+168
-138
lines changed

9 files changed

+168
-138
lines changed

.circleci/config.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
test: # runs not using Workflows must have a build job as entry point
3535
docker: # run the steps with Docker
3636
# CircleCI Go images available at: https://hub.docker.com/r/cimg/go
37-
- image: gcr.io/outreach-docker/bootstrap/testbed:1.16.2
37+
- image: gcr.io/outreach-docker/bootstrap/testbed:1.17.1
3838
auth:
3939
username: _json_key
4040
password: $GCLOUD_SERVICE_ACCOUNT
@@ -105,7 +105,7 @@ jobs:
105105

106106
release-dryrun:
107107
docker:
108-
- image: gcr.io/outreach-docker/bootstrap/testbed:1.16.2
108+
- image: gcr.io/outreach-docker/bootstrap/testbed:1.17.1
109109
auth:
110110
username: _json_key
111111
password: $GCLOUD_SERVICE_ACCOUNT
@@ -179,7 +179,7 @@ jobs:
179179
export CIRCLE_BRANCH=$OLD_CIRCLE_BRANCH
180180
release:
181181
docker:
182-
- image: gcr.io/outreach-docker/bootstrap/testbed:1.16.2
182+
- image: gcr.io/outreach-docker/bootstrap/testbed:1.17.1
183183
auth:
184184
username: _json_key
185185
password: $GCLOUD_SERVICE_ACCOUNT
@@ -224,7 +224,7 @@ jobs:
224224
225225
trigger_documentation:
226226
docker:
227-
- image: gcr.io/outreach-docker/bootstrap/testbed:1.16.2
227+
- image: gcr.io/outreach-docker/bootstrap/testbed:1.17.1
228228
auth:
229229
username: _json_key
230230
password: $GCLOUD_SERVICE_ACCOUNT

.tool-versions

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
golang 1.16.2
1+
golang 1.17.1
22
terraform 0.13.5
33
###Block(toolver)
44
###EndBlock(toolver)

bootstrap.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.mod

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/getoutreach/localizer
22

3-
go 1.16
3+
go 1.17
44

55
require (
66
github.com/Microsoft/go-winio v0.5.0 // indirect
@@ -11,7 +11,7 @@ require (
1111
github.com/function61/gokit v0.0.0-20210402130425-341c2c9ecfd0
1212
github.com/go-logr/logr v0.4.0
1313
github.com/go-sql-driver/mysql v1.6.0 // indirect
14-
github.com/google/go-cmp v0.5.5
14+
github.com/google/go-cmp v0.5.6
1515
github.com/google/gofuzz v1.2.0 // indirect
1616
github.com/googleapis/gnostic v0.5.4 // indirect
1717
github.com/hashicorp/golang-lru v0.5.4 // indirect
@@ -22,10 +22,10 @@ require (
2222
github.com/sirupsen/logrus v1.8.1
2323
github.com/urfave/cli/v2 v2.3.0
2424
go.opencensus.io v0.23.0 // indirect
25-
golang.org/x/crypto v0.0.0-20210503195802-e9a32991a82e
25+
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5
2626
golang.org/x/oauth2 v0.0.0-20210427180440-81ed05c6b58c // indirect
27-
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
28-
golang.org/x/term v0.0.0-20210503060354-a79de5458b56 // indirect
27+
golang.org/x/sys v0.0.0-20210910150752-751e447fb3d0 // indirect
28+
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect
2929
google.golang.org/appengine v1.6.7 // indirect
3030
google.golang.org/genproto v0.0.0-20210716133855-ce7ef5c701ea // indirect
3131
google.golang.org/grpc v1.39.0
@@ -41,4 +41,40 @@ require (
4141
k8s.io/utils v0.0.0-20210111153108-fddb29f9d009 // indirect
4242
)
4343

44+
require (
45+
cloud.google.com/go v0.65.0 // indirect
46+
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
47+
github.com/Azure/go-autorest/autorest v0.11.12 // indirect
48+
github.com/Azure/go-autorest/autorest/adal v0.9.5 // indirect
49+
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
50+
github.com/Azure/go-autorest/logger v0.2.0 // indirect
51+
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
52+
github.com/avast/retry-go v3.0.0+incompatible // indirect
53+
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
54+
github.com/form3tech-oss/jwt-go v3.2.2+incompatible // indirect
55+
github.com/gogo/protobuf v1.3.2 // indirect
56+
github.com/golang/protobuf v1.5.2 // indirect
57+
github.com/google/uuid v1.3.0 // indirect
58+
github.com/jmoiron/sqlx v1.3.4 // indirect
59+
github.com/json-iterator/go v1.1.11 // indirect
60+
github.com/lib/pq v1.10.1 // indirect
61+
github.com/moby/spdystream v0.2.0 // indirect
62+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
63+
github.com/modern-go/reflect2 v1.0.1 // indirect
64+
github.com/russross/blackfriday/v2 v2.0.1 // indirect
65+
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
66+
github.com/spf13/pflag v1.0.5 // indirect
67+
go4.org/intern v0.0.0-20210108033219-3eb7198706b2 // indirect
68+
go4.org/unsafe/assume-no-moving-gc v0.0.0-20201222180813-1025295fd063 // indirect
69+
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d // indirect
70+
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
71+
golang.org/x/text v0.3.6 // indirect
72+
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba // indirect
73+
gopkg.in/inf.v0 v0.9.1 // indirect
74+
gopkg.in/yaml.v2 v2.4.0 // indirect
75+
inet.af/netaddr v0.0.0-20210511181906-37180328850c // indirect
76+
sigs.k8s.io/structured-merge-diff/v4 v4.1.0 // indirect
77+
sigs.k8s.io/yaml v1.2.0 // indirect
78+
)
79+
4480
replace k8s.io/client-go => github.com/jaredallard/client-go v0.21.0-jaredallard

go.sum

Lines changed: 17 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
"devDependencies": {
44
"//": "This version of prettier is only for non-js formatting",
55
"@outreach/prettier-config": "^1.0.3",
6-
"@semantic-release/commit-analyzer": "^8.0.1",
7-
"@semantic-release/exec": "^5.0.0",
8-
"@semantic-release/git": "^9.0.1",
9-
"@semantic-release/github": "^7.2.3",
10-
"@semantic-release/npm": "^7.1.3",
11-
"@semantic-release/release-notes-generator": "^9.0.3",
12-
"conventional-changelog-conventionalcommits": "^4.6.0",
6+
"@semantic-release/commit-analyzer": "^9.0.1",
7+
"@semantic-release/exec": "^6.0.1",
8+
"@semantic-release/git": "^10.0.0",
9+
"@semantic-release/github": "^8.0.1",
10+
"@semantic-release/npm": "^8.0.0",
11+
"@semantic-release/release-notes-generator": "^10.0.2",
12+
"conventional-changelog-conventionalcommits": "^4.6.1",
1313
"prettier": "^2.3.2",
14-
"semantic-release": "^17.4.7",
14+
"semantic-release": "^18.0.0",
1515
"semver": "^7.3.5"
1616
}
1717
}

pkg/hostsfile/parser.go

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ import (
1919
"context"
2020
"encoding/json"
2121
"fmt"
22+
"io"
2223
"io/ioutil"
2324
"net"
25+
"os"
2426
"sort"
2527
"strings"
2628
"sync"
@@ -36,8 +38,10 @@ type File struct {
3638

3739
// if this came from a file, this will be populated
3840
fileLocation string
39-
contents []byte
40-
blockName string
41+
fileInfo os.FileInfo
42+
43+
contents []byte
44+
blockName string
4145

4246
lock sync.Mutex
4347
saveLock sync.Mutex
@@ -79,13 +83,25 @@ func New(fileLocation, sectionName string) (*File, error) {
7983
fileLocation = "/etc/hosts"
8084
}
8185

82-
b, err := ioutil.ReadFile(fileLocation)
86+
hostF, err := os.Open(fileLocation)
87+
if err != nil {
88+
return nil, err
89+
}
90+
defer hostF.Close()
91+
92+
info, err := hostF.Stat()
93+
if err != nil {
94+
return nil, err
95+
}
96+
97+
b, err := io.ReadAll(hostF)
8398
if err != nil {
8499
return nil, err
85100
}
86101

87102
f := NewWithContents(sectionName, b)
88103
f.fileLocation = fileLocation
104+
f.fileInfo = info
89105

90106
return f, nil
91107
}
@@ -285,6 +301,10 @@ func (f *File) Marshal(ctx context.Context) ([]byte, error) { //nolint:funlen
285301

286302
// Save marshalls the hosts file and then saves it to disk.
287303
func (f *File) Save(ctx context.Context) error {
304+
// ensure we don't write to the file at the same time
305+
f.saveLock.Lock()
306+
defer f.saveLock.Unlock()
307+
288308
if f.fileLocation == "" {
289309
return fmt.Errorf("can't write, was not loaded from a file")
290310
}
@@ -293,14 +313,11 @@ func (f *File) Save(ctx context.Context) error {
293313
var err error
294314
if f.fileLocation != "" {
295315
f.lock.Lock()
296-
// re-read the hosts file to get potential
297-
// changes outside of our block
298-
//nolint:govet // Why: We're OK shadowing err
299-
b, err := ioutil.ReadFile(f.fileLocation)
316+
contents, err := ioutil.ReadFile(f.fileLocation)
300317
if err != nil {
301318
return err
302319
}
303-
f.contents = b
320+
f.contents = contents
304321
f.lock.Unlock()
305322
}
306323

@@ -309,12 +326,7 @@ func (f *File) Save(ctx context.Context) error {
309326
return errors.Wrap(err, "failed to marshal hostsfile")
310327
}
311328

312-
// ensure we don't write to the file at the same time
313-
f.saveLock.Lock()
314-
defer f.saveLock.Unlock()
315-
316-
//nolint:gosec // We should eventually just use the existing perms here
317-
return ioutil.WriteFile(f.fileLocation, b, 0644)
329+
return ioutil.WriteFile(f.fileLocation, b, f.fileInfo.Mode())
318330
}
319331

320332
// AddHosts adds a line into the hosts file for the given hosts to resolve

service.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
name: localizer
22
reportingTeam: cia-dev-tooling
33
library: true
4-
versions:
5-
go: 1.16.2
64
commands:
75
- localizer
86
oss: true

0 commit comments

Comments
 (0)