Skip to content

Commit

Permalink
Updates to build on windows
Browse files Browse the repository at this point in the history
Signed-off-by: James Sturtevant <[email protected]>
  • Loading branch information
jsturtevant committed Apr 27, 2021
1 parent e0ee259 commit eb0f8a3
Show file tree
Hide file tree
Showing 41 changed files with 1,164 additions and 417 deletions.
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
# -----------------------------------------------------------------------------

GO ?= go

GOOS ?= $(shell go env GOOS)
ifeq ($(GOOS),windows)
BIN_EXT := .exe
endif

PACKAGE := github.com/containerd/nerdctl
BINDIR ?= /usr/local/bin
Expand All @@ -28,7 +31,7 @@ VERSION=$(shell git describe --match 'v[0-9]*' --dirty='.m' --always --tags)
VERSION_TRIMMED := $(VERSION:v%=%)
REVISION=$(shell git rev-parse HEAD)$(shell if ! git diff --no-ext-diff --quiet --exit-code; then echo .m; fi)

export GO_BUILD=GO111MODULE=on CGO_ENABLED=0 $(GO) build -ldflags "-s -w -X $(PACKAGE)/pkg/version.Version=$(VERSION) -X $(PACKAGE)/pkg/version.Revision=$(REVISION)"
export GO_BUILD=GO111MODULE=on CGO_ENABLED=0 GOOS=$(GOOS) $(GO) build -ldflags "-s -w -X $(PACKAGE)/pkg/version.Version=$(VERSION) -X $(PACKAGE)/pkg/version.Revision=$(REVISION)"

all: binaries

Expand All @@ -40,7 +43,7 @@ help:
@echo " * 'clean' - Clean artifacts."

nerdctl:
$(GO_BUILD) -o $(CURDIR)/_output/nerdctl $(PACKAGE)
$(GO_BUILD) -o $(CURDIR)/_output/nerdctl$(BIN_EXT) $(PACKAGE)

clean:
find . -name \*~ -delete
Expand Down Expand Up @@ -72,6 +75,9 @@ artifacts: clean
GOOS=linux GOARCH=s390x make -C $(CURDIR) binaries
tar $(TAR_FLAGS) -czvf $(CURDIR)/_output/nerdctl-$(VERSION_TRIMMED)-linux-s390x.tar.gz _output/nerdctl extras/rootless/*

GOOS=windows GOARCH=amd64 make -C $(CURDIR) binaries
tar $(TAR_FLAGS) -czvf $(CURDIR)/_output/nerdctl-$(VERSION_TRIMMED)-windows-amd64.tar.gz

rm -f $(CURDIR)/_output/nerdctl

DOCKER_BUILDKIT=1 docker build --output type=tar,dest=$(CURDIR)/_output/nerdctl-full-$(VERSION_TRIMMED)-linux-amd64.tar --target out-full $(CURDIR)
Expand Down
19 changes: 7 additions & 12 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ import (
"context"
"os"
"path/filepath"
"runtime"
"strings"

"github.com/pkg/errors"

"github.com/containerd/containerd"
"github.com/containerd/containerd/namespaces"
"github.com/opencontainers/go-digest"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
"golang.org/x/sys/unix"
)

func newClient(clicontext *cli.Context) (*containerd.Client, context.Context, context.CancelFunc, error) {
Expand All @@ -54,15 +55,6 @@ func newClient(clicontext *cli.Context) (*containerd.Client, context.Context, co
return client, ctx, cancel, nil
}

func isSocketAccessible(s string) error {
abs, err := filepath.Abs(s)
if err != nil {
return err
}
// set AT_EACCESS to allow running nerdctl as a setuid binary
return unix.Faccessat(-1, abs, unix.R_OK|unix.W_OK, unix.AT_EACCESS)
}

// getDataStore returns a string like "/var/lib/nerdctl/1935db59".
// "1935db9" is from `$(echo -n "/run/containerd/containerd.sock" | sha256sum | cut -c1-8)``
func getDataStore(clicontext *cli.Context) (string, error) {
Expand All @@ -84,7 +76,10 @@ func getDataStore(clicontext *cli.Context) (string, error) {
func getAddrHash(addr string) (string, error) {
const addrHashLen = 8

addr = strings.TrimPrefix(addr, "unix://")
if runtime.GOOS != "windows" {
addr = strings.TrimPrefix(addr, "unix://")
}

var err error
addr, err = filepath.EvalSymlinks(addr)
if err != nil {
Expand Down
32 changes: 32 additions & 0 deletions client_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Copyright The containerd 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 (
"path/filepath"

"golang.org/x/sys/unix"
)

func isSocketAccessible(s string) error {
abs, err := filepath.Abs(s)
if err != nil {
return err
}
// set AT_EACCESS to allow running nerdctl as a setuid binary
return unix.Faccessat(-1, abs, unix.R_OK|unix.W_OK, unix.AT_EACCESS)
}
30 changes: 30 additions & 0 deletions client_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Copyright The containerd 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 (
"time"

"github.com/Microsoft/go-winio"
)

func isSocketAccessible(s string) error {
// test if we can access the pipe
timeout := 2 * time.Second
_, err := winio.DialPipe(s, &timeout)
return err
}
14 changes: 1 addition & 13 deletions exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/containerd/containerd/cio"
"github.com/containerd/containerd/cmd/ctr/commands"
"github.com/containerd/containerd/cmd/ctr/commands/tasks"
"github.com/containerd/containerd/pkg/cap"
"github.com/containerd/nerdctl/pkg/idgen"
"github.com/containerd/nerdctl/pkg/idutil/containerwalker"
"github.com/containerd/nerdctl/pkg/strutil"
Expand Down Expand Up @@ -224,21 +223,10 @@ func generateExecProcessSpec(ctx context.Context, clicontext *cli.Context, conta
}

if clicontext.Bool("privileged") {
if pspec.Capabilities == nil {
pspec.Capabilities = &specs.LinuxCapabilities{}
}
allCaps, err := cap.Current()
err = setExecCapabilities(pspec)
if err != nil {
return nil, err
}
pspec.Capabilities.Bounding = allCaps
pspec.Capabilities.Permitted = pspec.Capabilities.Bounding
pspec.Capabilities.Inheritable = pspec.Capabilities.Bounding
pspec.Capabilities.Effective = pspec.Capabilities.Bounding

// https://github.com/moby/moby/pull/36466/files
// > `docker exec --privileged` does not currently disable AppArmor
// > profiles. Privileged configuration of the container is inherited
}

return pspec, nil
Expand Down
41 changes: 41 additions & 0 deletions exec_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright The containerd 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 (
"github.com/containerd/containerd/pkg/cap"
"github.com/opencontainers/runtime-spec/specs-go"
)

func setExecCapabilities(pspec *specs.Process) error {
if pspec.Capabilities == nil {
pspec.Capabilities = &specs.LinuxCapabilities{}
}
allCaps, err := cap.Current()
if err != nil {
return err
}
pspec.Capabilities.Bounding = allCaps
pspec.Capabilities.Permitted = pspec.Capabilities.Bounding
pspec.Capabilities.Inheritable = pspec.Capabilities.Bounding
pspec.Capabilities.Effective = pspec.Capabilities.Bounding

// https://github.com/moby/moby/pull/36466/files
// > `docker exec --privileged` does not currently disable AppArmor
// > profiles. Privileged configuration of the container is inherited
return nil
}
26 changes: 26 additions & 0 deletions exec_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright The containerd 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 (
"github.com/opencontainers/runtime-spec/specs-go"
)

func setExecCapabilities(pspec *specs.Process) error {
//no op windows
return nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/containerd/nerdctl
go 1.16

require (
github.com/Microsoft/go-winio v0.4.17 // indirect
github.com/compose-spec/compose-go v0.0.0-20210420125800-01e9e6b4c64c
github.com/containerd/cgroups v1.0.0
github.com/containerd/console v1.0.2
Expand Down
20 changes: 3 additions & 17 deletions login.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ import (
"context"
"fmt"
"io/ioutil"
"os"
"runtime"
"strings"
"syscall"

"github.com/containerd/nerdctl/pkg/version"
dockercliconfig "github.com/docker/cli/cli/config"
Expand All @@ -35,7 +33,6 @@ import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"golang.org/x/term"
)

type loginOptions struct {
Expand Down Expand Up @@ -210,22 +207,11 @@ func ConfigureAuthentification(clicontext *cli.Context, authConfig *types.AuthCo
if options.password == "" {

fmt.Print("Enter Password: ")
var fd int
if term.IsTerminal(syscall.Stdin) {
fd = syscall.Stdin
} else {
tty, err := os.Open("/dev/tty")
if err != nil {
return errors.Wrap(err, "error allocating terminal")
}
defer tty.Close()
fd = int(tty.Fd())
}
bytePassword, err := term.ReadPassword(fd)
pwd, err := readPassword()
if err != nil {
return errors.Wrap(err, "error reading password")
return err
}
options.password = string(bytePassword)
options.password = pwd
}

if options.password == "" {
Expand Down
45 changes: 45 additions & 0 deletions login_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright The containerd 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 (
"os"
"syscall"

"github.com/pkg/errors"
"golang.org/x/term"
)

func readPassword() (string, error) {
var fd int
if term.IsTerminal(syscall.Stdin) {
fd = syscall.Stdin
} else {
tty, err := os.Open("/dev/tty")
if err != nil {
return "", errors.Wrap(err, "error allocating terminal")
}
defer tty.Close()
fd = int(tty.Fd())
}
bytePassword, err := term.ReadPassword(fd)
if err != nil {
return "", errors.Wrap(err, "error reading password")
}

return string(bytePassword), nil
}
40 changes: 40 additions & 0 deletions login_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright The containerd 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"
"syscall"

"github.com/pkg/errors"
"golang.org/x/term"
)

func readPassword() (string, error) {
var fd int
if term.IsTerminal(int(syscall.Stdin)) {
fd = int(syscall.Stdin)
} else {
return "", fmt.Errorf("error allocating terminal")
}
bytePassword, err := term.ReadPassword(fd)
if err != nil {
return "", errors.Wrap(err, "error reading password")
}

return string(bytePassword), nil
}
Loading

0 comments on commit eb0f8a3

Please sign in to comment.