diff --git a/ecs/resolv/Dockerfile b/ecs/resolv/Dockerfile new file mode 100644 index 000000000..679c4a909 --- /dev/null +++ b/ecs/resolv/Dockerfile @@ -0,0 +1,23 @@ +# Copyright 2020 Docker Compose CLI authors + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FROM golang:1.14.4-alpine AS builder +WORKDIR $GOPATH/src/github.com/docker/compose-cli/ecs/resolv +COPY . . +RUN GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o /go/bin/resolv main/main.go +RUN chmod +x /go/bin/resolv + +FROM scratch +COPY --from=builder /go/bin/resolv /resolv +ENTRYPOINT ["/resolv"] diff --git a/ecs/resolv/main/main.go b/ecs/resolv/main/main.go new file mode 100644 index 000000000..e8ac924af --- /dev/null +++ b/ecs/resolv/main/main.go @@ -0,0 +1,38 @@ +/* + Copyright 2020 Docker Compose CLI authors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +package main + +import ( + "fmt" + "os" + + "github.com/docker/compose-cli/ecs/resolv" +) + +const resolvconf = "/etc/resolv.conf" + +func main() { + if len(os.Args) < 2 { + fmt.Fprint(os.Stderr, "usage: resolv DOMAIN [DOMAIN]") + os.Exit(1) + } + + err := resolv.SetSearchDomains(resolvconf, os.Args[1:]...) + if err != nil { + fmt.Fprint(os.Stderr, err.Error()) + os.Exit(1) + } +} diff --git a/ecs/resolv/resolv.go b/ecs/resolv/resolv.go new file mode 100644 index 000000000..d3dc04116 --- /dev/null +++ b/ecs/resolv/resolv.go @@ -0,0 +1,34 @@ +/* + Copyright 2020 Docker Compose CLI authors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package resolv + +import ( + "os" + "strings" +) + +func SetSearchDomains(file string, domains ...string) error { + search := strings.Join(domains, " ") + + f, err := os.OpenFile(file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + return err + } + defer f.Close() + _, err = f.WriteString("\nsearch " + search) + return err +} diff --git a/ecs/resolv/resolv_test.go b/ecs/resolv/resolv_test.go new file mode 100644 index 000000000..ffa2f0c0f --- /dev/null +++ b/ecs/resolv/resolv_test.go @@ -0,0 +1,46 @@ +/* + Copyright 2020 Docker Compose CLI authors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package resolv + +import ( + "io/ioutil" + "os" + "path/filepath" + "testing" + + "gotest.tools/v3/assert" + "gotest.tools/v3/fs" + "gotest.tools/v3/golden" +) + +func TestSetDomain(t *testing.T) { + dir := fs.NewDir(t, "resolv").Path() + f := filepath.Join(dir, "resolv.conf") + touch(t, f) + + err := SetSearchDomains(f, "foo", "bar", "zot") + assert.NilError(t, err) + + got, err := ioutil.ReadFile(f) + golden.Assert(t, string(got), "resolv.conf.golden") +} + +func touch(t *testing.T, f string) { + file, err := os.Create(f) + assert.NilError(t, err) + defer file.Close() +} diff --git a/ecs/resolv/testdata/resolv.conf.golden b/ecs/resolv/testdata/resolv.conf.golden new file mode 100644 index 000000000..ed0e2453a --- /dev/null +++ b/ecs/resolv/testdata/resolv.conf.golden @@ -0,0 +1,2 @@ + +search foo bar zot \ No newline at end of file