From 3a310f8a30430eb3f0c3b517430dc05cf2548a71 Mon Sep 17 00:00:00 2001 From: royalcat Date: Tue, 17 Oct 2023 23:12:07 +0300 Subject: [PATCH] fix docker build --- Dockerfile | 34 +++++++++++++++----------------- build_tools/Dockerfile | 19 ------------------ cmd/tstor/main.go | 5 ++--- http_fs.go => embed.go | 0 go.work | 3 +++ go.work.sum | 27 +++++++++++++++++++++++++ src/mounts/fuse/handler.go | 4 +++- src/mounts/fuse/handler_nocgo.go | 23 +++++++++++++++++++++ src/mounts/fuse/mount.go | 24 +++++++++++----------- src/mounts/fuse/mount_test.go | 2 ++ 10 files changed, 89 insertions(+), 52 deletions(-) delete mode 100644 build_tools/Dockerfile rename http_fs.go => embed.go (100%) create mode 100644 go.work create mode 100644 go.work.sum create mode 100644 src/mounts/fuse/handler_nocgo.go diff --git a/Dockerfile b/Dockerfile index d75f316..622d21a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,32 +2,30 @@ # Stage 1: Build #=============== -FROM golang:1.20 as builder +FROM golang:1.21 as builder -ENV BIN_REPO=git.kmsign.ru/royalcat/tstor -ENV BIN_PATH=$GOPATH/src/$BIN_REPO +WORKDIR /app -COPY . $BIN_PATH -WORKDIR $BIN_PATH +COPY go.mod ./ +COPY go.sum ./ +RUN go mod download -RUN apk add fuse-dev git gcc libc-dev g++ make +COPY ./src ./src +COPY ./cmd ./cmd +COPY ./assets ./assets +COPY ./templates ./templates +COPY embed.go embed.go -RUN BIN_OUTPUT=/bin/tstor make build +RUN go generate ./... +RUN CGO_ENABLED=0 go build -tags timetzdata -o /tstor ./cmd/tstor/main.go #=============== # Stage 2: Run #=============== -FROM alpine:3 +FROM scratch -RUN apk add gcc libc-dev fuse-dev +COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt +COPY --from=builder /tstor /tstor -COPY --from=builder /bin/tstor /bin/tstor -RUN chmod +x /bin/tstor - -RUN mkdir /tstor-data - -RUN echo "user_allow_other" >> /etc/fuse.conf -ENV tstor_FUSE_ALLOW_OTHER=true - -ENTRYPOINT ["./bin/tstor"] +ENTRYPOINT ["/tstor"] diff --git a/build_tools/Dockerfile b/build_tools/Dockerfile deleted file mode 100644 index d21a348..0000000 --- a/build_tools/Dockerfile +++ /dev/null @@ -1,19 +0,0 @@ -FROM techknowlogick/xgo:go-1.17.x - -# add 32-bit and 64-bit architectures and install 7zip -RUN \ - dpkg --add-architecture i386 && \ - dpkg --add-architecture amd64 && \ - apt-get update && \ - apt-get install -y --no-install-recommends p7zip-full - -# install LIBFUSE -RUN \ - apt-get update && \ - apt-get install -y --no-install-recommends libfuse-dev:i386 && \ - apt-get install -y --no-install-recommends libfuse-dev:amd64 && \ - apt-get download libfuse-dev:i386 && \ - dpkg -x libfuse-dev*i386*.deb / - -ENV \ - OSXCROSS_NO_INCLUDE_PATH_WARNINGS 1 diff --git a/cmd/tstor/main.go b/cmd/tstor/main.go index 979118d..ca84dab 100644 --- a/cmd/tstor/main.go +++ b/cmd/tstor/main.go @@ -27,7 +27,6 @@ import ( const ( configFlag = "config" - fuseAllowOther = "fuse-allow-other" portFlag = "http-port" webDAVPortFlag = "webdav-port" ) @@ -45,7 +44,7 @@ func main() { }, Action: func(c *cli.Context) error { - err := load(c.String(configFlag), c.Int(portFlag), c.Int(webDAVPortFlag), c.Bool(fuseAllowOther)) + err := load(c.String(configFlag)) // stop program execution on errors to avoid flashing consoles if err != nil && runtime.GOOS == "windows" { @@ -108,7 +107,7 @@ func (s *stc) Close() error { return nil } -func load(configPath string, port, webDAVPort int, fuseAllowOther bool) error { +func load(configPath string) error { conf, err := config.Load(configPath) if err != nil { return fmt.Errorf("error loading configuration: %w", err) diff --git a/http_fs.go b/embed.go similarity index 100% rename from http_fs.go rename to embed.go diff --git a/go.work b/go.work new file mode 100644 index 0000000..767c526 --- /dev/null +++ b/go.work @@ -0,0 +1,3 @@ +go 1.21.3 + +use . diff --git a/go.work.sum b/go.work.sum new file mode 100644 index 0000000..bfca658 --- /dev/null +++ b/go.work.sum @@ -0,0 +1,27 @@ +github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= +github.com/cenkalti/backoff/v4 v4.1.3/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw= +github.com/chzyer/readline v1.5.0/go.mod h1:x22KAscuvRqlLoK9CsoYsmxoXZMMFVyOl86cAH8qUic= +github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0/go.mod h1:hgWBS7lorOAVIJEQMi4ZsPv9hVvWI6+ch50m39Pf2Ks= +github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4= +github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= +github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= +github.com/pierrec/lz4 v2.0.5+incompatible h1:2xWsjqPFWcplujydGg4WmhC/6fZqK42wMM8aXeqhl0I= +github.com/prometheus/client_golang v1.12.2/go.mod h1:3Z9XVyYiZYEO+YQWt3RD2R3jrbd179Rt297l4aS6nDY= +github.com/prometheus/common v0.35.0/go.mod h1:phzohg0JFMnBEFGxTDbfu3QyL5GI8gTQJFhYO5B3mfA= +github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= +github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU= +github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8= +google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f/go.mod h1:RGgjbofJ8xD9Sq1VVhDM1Vok1vRONV+rg+CjzG4SZKM= +google.golang.org/grpc v1.53.0/go.mod h1:OnIrk0ipVdj4N5d9IUoFUx72/VlD7+jUsHwZgwSMQpw= +lukechampine.com/uint128 v1.2.0/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk= +modernc.org/cc/v3 v3.40.0/go.mod h1:/bTg4dnWkSXowUO6ssQKnOV0yMVxDYNIsIrzqTFDGH0= +modernc.org/ccgo/v3 v3.16.13/go.mod h1:2Quk+5YgpImhPjv2Qsob1DnZ/4som1lJTodubIcoUkY= +modernc.org/httpfs v1.0.6/go.mod h1:7dosgurJGp0sPaRanU53W4xZYKh14wfzX420oZADeHM= +modernc.org/opt v0.1.3/go.mod h1:WdSiB5evDcignE70guQKxYUl14mgWtbClRi5wmkkTX0= +modernc.org/strutil v1.1.3/go.mod h1:MEHNA7PdEnEwLvspRMtWTNnp2nnyvMfkimT1NKNAGbw= +modernc.org/token v1.0.1/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM= +modernc.org/z v1.7.0/go.mod h1:hVdgNMh8ggTuRG1rGU8x+xGRFfiQUIAw0ZqlPy8+HyQ= diff --git a/src/mounts/fuse/handler.go b/src/mounts/fuse/handler.go index c3b4af9..867e466 100644 --- a/src/mounts/fuse/handler.go +++ b/src/mounts/fuse/handler.go @@ -1,3 +1,5 @@ +//go:build cgo + package fuse import ( @@ -38,7 +40,7 @@ func (s *Handler) Mount(vfs vfs.Filesystem) error { } } - host := fuse.NewFileSystemHost(NewFS(vfs)) + host := fuse.NewFileSystemHost(newFuseFS(vfs)) // TODO improve error handling here go func() { diff --git a/src/mounts/fuse/handler_nocgo.go b/src/mounts/fuse/handler_nocgo.go new file mode 100644 index 0000000..4e0e92e --- /dev/null +++ b/src/mounts/fuse/handler_nocgo.go @@ -0,0 +1,23 @@ +//go:build !cgo + +package fuse + +import ( + "fmt" + + "git.kmsign.ru/royalcat/tstor/src/host/vfs" +) + +type Handler struct{} + +func NewHandler(fuseAllowOther bool, path string) *Handler { + return &Handler{} +} + +func (s *Handler) Mount(vfs vfs.Filesystem) error { + return fmt.Errorf("tstor was build without fuse support") + +} + +func (s *Handler) Unmount() { +} diff --git a/src/mounts/fuse/mount.go b/src/mounts/fuse/mount.go index e57a6f2..da6a390 100644 --- a/src/mounts/fuse/mount.go +++ b/src/mounts/fuse/mount.go @@ -1,3 +1,5 @@ +//go:build cgo + package fuse import ( @@ -14,22 +16,22 @@ import ( "github.com/rs/zerolog/log" ) -type FS struct { +type fuseFS struct { fuse.FileSystemBase fh *fileHandler log zerolog.Logger } -func NewFS(fs vfs.Filesystem) fuse.FileSystemInterface { +func newFuseFS(fs vfs.Filesystem) fuse.FileSystemInterface { l := log.Logger.With().Str("component", "fuse").Logger() - return &FS{ + return &fuseFS{ fh: &fileHandler{fs: fs}, log: l, } } -func (fs *FS) Open(path string, flags int) (errc int, fh uint64) { +func (fs *fuseFS) Open(path string, flags int) (errc int, fh uint64) { fh, err := fs.fh.OpenHolder(path) if os.IsNotExist(err) { fs.log.Debug().Str("path", path).Msg("file does not exists") @@ -46,15 +48,15 @@ func (fs *FS) Open(path string, flags int) (errc int, fh uint64) { // Unlink removes a file. // The FileSystemBase implementation returns -ENOSYS. -func (fs *FS) Unlink(path string) int { +func (fs *fuseFS) Unlink(path string) int { return -fuse.ENOSYS } -func (fs *FS) Opendir(path string) (errc int, fh uint64) { +func (fs *fuseFS) Opendir(path string) (errc int, fh uint64) { return fs.Open(path, 0) } -func (fs *FS) Getattr(path string, stat *fuse.Stat_t, fh uint64) (errc int) { +func (fs *fuseFS) Getattr(path string, stat *fuse.Stat_t, fh uint64) (errc int) { if path == "/" { stat.Mode = fuse.S_IFDIR | 0555 return 0 @@ -81,7 +83,7 @@ func (fs *FS) Getattr(path string, stat *fuse.Stat_t, fh uint64) (errc int) { return 0 } -func (fs *FS) Read(path string, dest []byte, off int64, fh uint64) int { +func (fs *fuseFS) Read(path string, dest []byte, off int64, fh uint64) int { file, err := fs.fh.GetFile(path, fh) if os.IsNotExist(err) { fs.log.Error().Err(err).Str("path", path).Msg("file not found on READ operation") @@ -110,7 +112,7 @@ func (fs *FS) Read(path string, dest []byte, off int64, fh uint64) int { return n } -func (fs *FS) Release(path string, fh uint64) int { +func (fs *fuseFS) Release(path string, fh uint64) int { if err := fs.fh.Remove(fh); err != nil { fs.log.Error().Err(err).Str("path", path).Msg("error getting holder when releasing file") return -fuse.EIO @@ -119,11 +121,11 @@ func (fs *FS) Release(path string, fh uint64) int { return 0 } -func (fs *FS) Releasedir(path string, fh uint64) int { +func (fs *fuseFS) Releasedir(path string, fh uint64) int { return fs.Release(path, fh) } -func (fs *FS) Readdir(path string, +func (fs *fuseFS) Readdir(path string, fill func(name string, stat *fuse.Stat_t, ofst int64) bool, ofst int64, fh uint64) (errc int) { diff --git a/src/mounts/fuse/mount_test.go b/src/mounts/fuse/mount_test.go index 0717955..3d16ca4 100644 --- a/src/mounts/fuse/mount_test.go +++ b/src/mounts/fuse/mount_test.go @@ -1,3 +1,5 @@ +//go:build cgo + package fuse import (