Skip to content

Commit

Permalink
fix: update bootstrap, save concurrency bug' (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredallard authored Oct 20, 2021
1 parent bd068b7 commit 05df5ed
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 138 deletions.
8 changes: 4 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
test: # runs not using Workflows must have a build job as entry point
docker: # run the steps with Docker
# CircleCI Go images available at: https://hub.docker.com/r/cimg/go
- image: gcr.io/outreach-docker/bootstrap/testbed:1.16.2
- image: gcr.io/outreach-docker/bootstrap/testbed:1.17.1
auth:
username: _json_key
password: $GCLOUD_SERVICE_ACCOUNT
Expand Down Expand Up @@ -105,7 +105,7 @@ jobs:

release-dryrun:
docker:
- image: gcr.io/outreach-docker/bootstrap/testbed:1.16.2
- image: gcr.io/outreach-docker/bootstrap/testbed:1.17.1
auth:
username: _json_key
password: $GCLOUD_SERVICE_ACCOUNT
Expand Down Expand Up @@ -179,7 +179,7 @@ jobs:
export CIRCLE_BRANCH=$OLD_CIRCLE_BRANCH
release:
docker:
- image: gcr.io/outreach-docker/bootstrap/testbed:1.16.2
- image: gcr.io/outreach-docker/bootstrap/testbed:1.17.1
auth:
username: _json_key
password: $GCLOUD_SERVICE_ACCOUNT
Expand Down Expand Up @@ -224,7 +224,7 @@ jobs:
trigger_documentation:
docker:
- image: gcr.io/outreach-docker/bootstrap/testbed:1.16.2
- image: gcr.io/outreach-docker/bootstrap/testbed:1.17.1
auth:
username: _json_key
password: $GCLOUD_SERVICE_ACCOUNT
Expand Down
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
golang 1.16.2
golang 1.17.1
terraform 0.13.5
###Block(toolver)
###EndBlock(toolver)
6 changes: 3 additions & 3 deletions bootstrap.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 41 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/getoutreach/localizer

go 1.16
go 1.17

require (
github.com/Microsoft/go-winio v0.5.0 // indirect
Expand All @@ -11,7 +11,7 @@ require (
github.com/function61/gokit v0.0.0-20210402130425-341c2c9ecfd0
github.com/go-logr/logr v0.4.0
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/google/go-cmp v0.5.5
github.com/google/go-cmp v0.5.6
github.com/google/gofuzz v1.2.0 // indirect
github.com/googleapis/gnostic v0.5.4 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
Expand All @@ -22,10 +22,10 @@ require (
github.com/sirupsen/logrus v1.8.1
github.com/urfave/cli/v2 v2.3.0
go.opencensus.io v0.23.0 // indirect
golang.org/x/crypto v0.0.0-20210503195802-e9a32991a82e
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5
golang.org/x/oauth2 v0.0.0-20210427180440-81ed05c6b58c // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
golang.org/x/term v0.0.0-20210503060354-a79de5458b56 // indirect
golang.org/x/sys v0.0.0-20210910150752-751e447fb3d0 // indirect
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20210716133855-ce7ef5c701ea // indirect
google.golang.org/grpc v1.39.0
Expand All @@ -41,4 +41,40 @@ require (
k8s.io/utils v0.0.0-20210111153108-fddb29f9d009 // indirect
)

require (
cloud.google.com/go v0.65.0 // indirect
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
github.com/Azure/go-autorest/autorest v0.11.12 // indirect
github.com/Azure/go-autorest/autorest/adal v0.9.5 // indirect
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
github.com/Azure/go-autorest/logger v0.2.0 // indirect
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
github.com/avast/retry-go v3.0.0+incompatible // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/form3tech-oss/jwt-go v3.2.2+incompatible // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/jmoiron/sqlx v1.3.4 // indirect
github.com/json-iterator/go v1.1.11 // indirect
github.com/lib/pq v1.10.1 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go4.org/intern v0.0.0-20210108033219-3eb7198706b2 // indirect
go4.org/unsafe/assume-no-moving-gc v0.0.0-20201222180813-1025295fd063 // indirect
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
inet.af/netaddr v0.0.0-20210511181906-37180328850c // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.1.0 // indirect
sigs.k8s.io/yaml v1.2.0 // indirect
)

replace k8s.io/client-go => github.com/jaredallard/client-go v0.21.0-jaredallard
28 changes: 17 additions & 11 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
"devDependencies": {
"//": "This version of prettier is only for non-js formatting",
"@outreach/prettier-config": "^1.0.3",
"@semantic-release/commit-analyzer": "^8.0.1",
"@semantic-release/exec": "^5.0.0",
"@semantic-release/git": "^9.0.1",
"@semantic-release/github": "^7.2.3",
"@semantic-release/npm": "^7.1.3",
"@semantic-release/release-notes-generator": "^9.0.3",
"conventional-changelog-conventionalcommits": "^4.6.0",
"@semantic-release/commit-analyzer": "^9.0.1",
"@semantic-release/exec": "^6.0.1",
"@semantic-release/git": "^10.0.0",
"@semantic-release/github": "^8.0.1",
"@semantic-release/npm": "^8.0.0",
"@semantic-release/release-notes-generator": "^10.0.2",
"conventional-changelog-conventionalcommits": "^4.6.1",
"prettier": "^2.3.2",
"semantic-release": "^17.4.7",
"semantic-release": "^18.0.0",
"semver": "^7.3.5"
}
}
40 changes: 26 additions & 14 deletions pkg/hostsfile/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"sort"
"strings"
"sync"
Expand All @@ -36,8 +38,10 @@ type File struct {

// if this came from a file, this will be populated
fileLocation string
contents []byte
blockName string
fileInfo os.FileInfo

contents []byte
blockName string

lock sync.Mutex
saveLock sync.Mutex
Expand Down Expand Up @@ -79,13 +83,25 @@ func New(fileLocation, sectionName string) (*File, error) {
fileLocation = "/etc/hosts"
}

b, err := ioutil.ReadFile(fileLocation)
hostF, err := os.Open(fileLocation)
if err != nil {
return nil, err
}
defer hostF.Close()

info, err := hostF.Stat()
if err != nil {
return nil, err
}

b, err := io.ReadAll(hostF)
if err != nil {
return nil, err
}

f := NewWithContents(sectionName, b)
f.fileLocation = fileLocation
f.fileInfo = info

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

// Save marshalls the hosts file and then saves it to disk.
func (f *File) Save(ctx context.Context) error {
// ensure we don't write to the file at the same time
f.saveLock.Lock()
defer f.saveLock.Unlock()

if f.fileLocation == "" {
return fmt.Errorf("can't write, was not loaded from a file")
}
Expand All @@ -293,14 +313,11 @@ func (f *File) Save(ctx context.Context) error {
var err error
if f.fileLocation != "" {
f.lock.Lock()
// re-read the hosts file to get potential
// changes outside of our block
//nolint:govet // Why: We're OK shadowing err
b, err := ioutil.ReadFile(f.fileLocation)
contents, err := ioutil.ReadFile(f.fileLocation)
if err != nil {
return err
}
f.contents = b
f.contents = contents
f.lock.Unlock()
}

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

// ensure we don't write to the file at the same time
f.saveLock.Lock()
defer f.saveLock.Unlock()

//nolint:gosec // We should eventually just use the existing perms here
return ioutil.WriteFile(f.fileLocation, b, 0644)
return ioutil.WriteFile(f.fileLocation, b, f.fileInfo.Mode())
}

// AddHosts adds a line into the hosts file for the given hosts to resolve
Expand Down
2 changes: 0 additions & 2 deletions service.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
name: localizer
reportingTeam: cia-dev-tooling
library: true
versions:
go: 1.16.2
commands:
- localizer
oss: true
Loading

0 comments on commit 05df5ed

Please sign in to comment.