Skip to content

Commit

Permalink
add support for build tag to allow to disable some features
Browse files Browse the repository at this point in the history
The following build tags are available:

- "nogcs", disable Google Cloud Storage backend
- "nos3", disable S3 Compabible Object Storage backends
- "nobolt", disable Bolt data provider
- "nomysql", disable MySQL data provider
- "nopgsql", disable PostgreSQL data provider
- "nosqlite", disable SQLite data provider
- "noportable", disable portable mode
  • Loading branch information
drakkan committed May 23, 2020
1 parent 15298b0 commit ad53429
Show file tree
Hide file tree
Showing 26 changed files with 406 additions and 189 deletions.
5 changes: 5 additions & 0 deletions cmd/portable.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build !noportable

package cmd

import (
Expand All @@ -14,6 +16,7 @@ import (
"github.com/drakkan/sftpgo/dataprovider"
"github.com/drakkan/sftpgo/service"
"github.com/drakkan/sftpgo/sftpd"
"github.com/drakkan/sftpgo/utils"
"github.com/drakkan/sftpgo/vfs"
)

Expand Down Expand Up @@ -138,6 +141,8 @@ Please take a look at the usage below to customize the serving parameters`,
)

func init() {
utils.AddFeature("+portable")

portableCmd.Flags().StringVarP(&directoryToServe, "directory", "d", ".",
"Path to the directory to serve. This can be an absolute path or a path relative to the current directory")
portableCmd.Flags().IntVarP(&portableSFTPDPort, "sftpd-port", "s", 0, "0 means a random non privileged port")
Expand Down
9 changes: 9 additions & 0 deletions cmd/portable_disabled.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// +build noportable

package cmd

import "github.com/drakkan/sftpgo/utils"

func init() {
utils.AddFeature("-portable")
}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func init() {
version := utils.GetAppVersion()
rootCmd.Flags().BoolP("version", "v", false, "")
rootCmd.Version = version.GetVersionAsString()
rootCmd.SetVersionTemplate(`{{printf "SFTPGo version: "}}{{printf "%s" .Version}}
rootCmd.SetVersionTemplate(`{{printf "SFTPGo "}}{{printf "%s" .Version}}
`)
}

Expand Down
6 changes: 6 additions & 0 deletions dataprovider/bolt.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build !nobolt

package dataprovider

import (
Expand Down Expand Up @@ -52,6 +54,10 @@ type compatUserV2 struct {
Status int `json:"status"`
}

func init() {
utils.AddFeature("+bolt")
}

func initializeBoltProvider(basePath string) error {
var err error
logSender = fmt.Sprintf("dataprovider_%v", BoltDataProviderName)
Expand Down
17 changes: 17 additions & 0 deletions dataprovider/bolt_disabled.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// +build nobolt

package dataprovider

import (
"errors"

"github.com/drakkan/sftpgo/utils"
)

func init() {
utils.AddFeature("-bolt")
}

func initializeBoltProvider(basePath string) error {
return errors.New("bolt disabled at build time")
}
10 changes: 10 additions & 0 deletions dataprovider/mysql.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build !nomysql

package dataprovider

import (
Expand All @@ -6,7 +8,11 @@ import (
"strings"
"time"

// we import go-sql-driver/mysql here to be able to disable MySQL support using a build tag
_ "github.com/go-sql-driver/mysql"

"github.com/drakkan/sftpgo/logger"
"github.com/drakkan/sftpgo/utils"
)

const (
Expand All @@ -28,6 +34,10 @@ type MySQLProvider struct {
dbHandle *sql.DB
}

func init() {
utils.AddFeature("+mysql")
}

func initializeMySQLProvider() error {
var err error
logSender = fmt.Sprintf("dataprovider_%v", MySQLDataProviderName)
Expand Down
17 changes: 17 additions & 0 deletions dataprovider/mysql_disabled.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// +build nomysql

package dataprovider

import (
"errors"

"github.com/drakkan/sftpgo/utils"
)

func init() {
utils.AddFeature("-mysql")
}

func initializeMySQLProvider() error {
return errors.New("MySQL disabled at build time")
}
10 changes: 10 additions & 0 deletions dataprovider/pgsql.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
// +build !nopgsql

package dataprovider

import (
"database/sql"
"fmt"
"strings"

// we import lib/pq here to be able to disable PostgreSQL support using a build tag
_ "github.com/lib/pq"

"github.com/drakkan/sftpgo/logger"
"github.com/drakkan/sftpgo/utils"
)

const (
Expand All @@ -26,6 +32,10 @@ type PGSQLProvider struct {
dbHandle *sql.DB
}

func init() {
utils.AddFeature("+pgsql")
}

func initializePGSQLProvider() error {
var err error
logSender = fmt.Sprintf("dataprovider_%v", PGSQLDataProviderName)
Expand Down
17 changes: 17 additions & 0 deletions dataprovider/pgsql_disabled.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// +build nopgsql

package dataprovider

import (
"errors"

"github.com/drakkan/sftpgo/utils"
)

func init() {
utils.AddFeature("-pgsql")
}

func initializePGSQLProvider() error {
return errors.New("PostgreSQL disabled at build time")
}
9 changes: 9 additions & 0 deletions dataprovider/sqlite.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build !nosqlite

package dataprovider

import (
Expand All @@ -6,6 +8,9 @@ import (
"path/filepath"
"strings"

// we import go-sqlite3 here to be able to disable SQLite support using a build tag
_ "github.com/mattn/go-sqlite3"

"github.com/drakkan/sftpgo/logger"
"github.com/drakkan/sftpgo/utils"
)
Expand Down Expand Up @@ -41,6 +46,10 @@ type SQLiteProvider struct {
dbHandle *sql.DB
}

func init() {
utils.AddFeature("+sqlite")
}

func initializeSQLiteProvider(basePath string) error {
var err error
var connectionString string
Expand Down
17 changes: 17 additions & 0 deletions dataprovider/sqlite_disabled.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// +build nosqlite

package dataprovider

import (
"errors"

"github.com/drakkan/sftpgo/utils"
)

func init() {
utils.AddFeature("-sqlite")
}

func initializeSQLiteProvider(basePath string) error {
return errors.New("SQLite disabled at build time")
}
9 changes: 4 additions & 5 deletions docker/sftpgo/alpine/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ RUN apk add --no-cache git gcc g++ ca-certificates \
&& go get -d github.com/drakkan/sftpgo
WORKDIR /go/src/github.com/drakkan/sftpgo
ARG TAG
ARG FEATURES
# Use --build-arg TAG=LATEST for latest tag. Use e.g. --build-arg TAG=0.9.6 for a specific tag/commit. Otherwise HEAD (master) is built.
RUN git checkout $(if [ "${TAG}" = LATEST ]; then echo `git rev-list --tags --max-count=1`; elif [ -n "${TAG}" ]; then echo "${TAG}"; else echo HEAD; fi)
RUN go build -i -ldflags "-s -w -X github.com/drakkan/sftpgo/utils.commit=`git describe --always --dirty` -X github.com/drakkan/sftpgo/utils.date=`date -u +%FT%TZ`" -o /go/bin/sftpgo
RUN go build -i $(if [ -n "${FEATURES}" ]; then echo "-tags ${FEATURES}"; fi) -ldflags "-s -w -X github.com/drakkan/sftpgo/utils.commit=`git describe --always --dirty` -X github.com/drakkan/sftpgo/utils.date=`date -u +%FT%TZ`" -o /go/bin/sftpgo

FROM alpine:latest

RUN apk add --no-cache ca-certificates su-exec \
&& mkdir -p /data /etc/sftpgo /srv/sftpgo/config /srv/sftpgo/web /srv/sftpgo/backups

# ca-certificates is needed for Cloud Storage Support and to expose the REST API over HTTPS.
# If you install git then ca-certificates will be automatically installed as dependency.
# git, rsync and ca-certificates are optional, uncomment the next line to add support for them if needed.
#RUN apk add --no-cache git rsync ca-certificates
# git and rsync are optional, uncomment the next line to add support for them if needed.
#RUN apk add --no-cache git rsync

COPY --from=builder /go/bin/sftpgo /bin/
COPY --from=builder /go/src/github.com/drakkan/sftpgo/sftpgo.json /etc/sftpgo/sftpgo.json
Expand Down
5 changes: 4 additions & 1 deletion docker/sftpgo/alpine/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ sudo groupadd -g 1003 sftpgrp && \

# Edit sftpgo.json as you need

# Get and build SFTPGo image (add --build-arg TAG=LATEST to build the latest tag or e.g. TAG=0.9.6 for a specific tag/commit).
# Get and build SFTPGo image.
# Add --build-arg TAG=LATEST to build the latest tag or e.g. TAG=0.9.6 for a specific tag/commit.
# Add --build-arg FEATURES=<features to disable> to disable some feature.
# Please take a look at the [build from source](./../../../docs/build-from-source.md) documentation for the complete list of the features that can be disabled.
git clone https://github.com/drakkan/sftpgo.git && \
cd sftpgo && \
sudo docker build -t sftpgo docker/sftpgo/alpine/
Expand Down
8 changes: 5 additions & 3 deletions docker/sftpgo/debian/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ LABEL maintainer="nicola.murino@gmail.com"
RUN go get -d github.com/drakkan/sftpgo
WORKDIR /go/src/github.com/drakkan/sftpgo
ARG TAG
ARG FEATURES
# Use --build-arg TAG=LATEST for latest tag. Use e.g. --build-arg TAG=0.9.6 for a specific tag/commit. Otherwise HEAD (master) is built.
RUN git checkout $(if [ "${TAG}" = LATEST ]; then echo `git rev-list --tags --max-count=1`; elif [ -n "${TAG}" ]; then echo "${TAG}"; else echo HEAD; fi)
RUN go build -i -ldflags "-s -w -X github.com/drakkan/sftpgo/utils.commit=`git describe --always --dirty` -X github.com/drakkan/sftpgo/utils.date=`date -u +%FT%TZ`" -o sftpgo
RUN go build -i $(if [ -n "${FEATURES}" ]; then echo "-tags ${FEATURES}"; fi) -ldflags "-s -w -X github.com/drakkan/sftpgo/utils.commit=`git describe --always --dirty` -X github.com/drakkan/sftpgo/utils.date=`date -u +%FT%TZ`" -o sftpgo

# now define the run environment
FROM debian:latest

# ca-certificates is needed for Cloud Storage Support and to expose the REST API over HTTPS.
# If you install git then ca-certificates will be automatically installed as dependency.
# git, rsync and ca-certificates are optional, uncomment the next line to add support for them if needed.
RUN apt-get update && apt-get install -y ca-certificates

# git and rsync are optional, uncomment the next line to add support for them if needed.
#RUN apt-get update && apt-get install -y git rsync ca-certificates

ARG BASE_DIR=/app
Expand Down
13 changes: 11 additions & 2 deletions docker/sftpgo/debian/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,22 @@ You can build the container image using `docker build`, for example:
docker build -t="drakkan/sftpgo" .
```

This will build master of github.com/drakkan/sftpgo. To build the latest tag you can add `--build-arg TAG=LATEST`
and to build a specific tag/commit you can use for example `TAG=0.9.6`, like this:
This will build master of github.com/drakkan/sftpgo.

To build the latest tag you can add `--build-arg TAG=LATEST` and to build a specific tag/commit you can use for example `TAG=0.9.6`, like this:

```bash
docker build -t="drakkan/sftpgo" --build-arg TAG=0.9.6 .
```

To disable some features you can add `--build-arg FEATURES=<features to disable>`. For example you can disable SQLite support like this:

```bash
docker build -t="drakkan/sftpgo" --build-arg FEATURES=nosqlite .
```

Please take a look at the [build from source](./../../../docs/build-from-source.md) documentation for the complete list of the features that can be disabled.

Now create the required folders on the host system, for example:

```bash
Expand Down
24 changes: 17 additions & 7 deletions docs/build-from-source.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,23 @@ go get -u github.com/drakkan/sftpgo

Make sure [Git](https://git-scm.com/downloads) is installed on your machine and in your system's `PATH`.

SFTPGo depends on [go-sqlite3](https://github.com/mattn/go-sqlite3) which is a CGO package and so it requires a `C` compiler at build time.
The following build tags are available to disable some features:

- `nogcs`, disable Google Cloud Storage backend
- `nos3`, disable S3 Compabible Object Storage backends
- `nobolt`, disable Bolt data provider
- `nomysql`, disable MySQL data provider
- `nopgsql`, disable PostgreSQL data provider
- `nosqlite`, disable SQLite data provider
- `noportable`, disable portable mode

If no build tag is specified all the features will be included.

The optional [SQLite driver](https://github.com/mattn/go-sqlite3 "go-sqlite3") is a `CGO` package and so it requires a `C` compiler at build time.
On Linux and macOS, a compiler is easy to install or already installed. On Windows, you need to download [MinGW-w64](https://sourceforge.net/projects/mingw-w64/files/) and build SFTPGo from its command prompt.

The compiler is a build time only dependency. It is not required at runtime.

If you don't need SQLite, you can also get/build SFTPGo setting the environment variable `GCO_ENABLED` to 0. This way, SQLite support will be disabled and PostgreSQL, MySQL, bbolt and memory data providers will keep working. In this way, you don't need a `C` compiler for building.

Version info, such as git commit and build date, can be embedded setting the following string variables at build time:

- `github.com/drakkan/sftpgo/utils.commit`
Expand All @@ -23,12 +33,12 @@ Version info, such as git commit and build date, can be embedded setting the fol
For example, you can build using the following command:

```bash
go build -i -ldflags "-s -w -X github.com/drakkan/sftpgo/utils.commit=`git describe --always --dirty` -X github.com/drakkan/sftpgo/utils.date=`date -u +%FT%TZ`" -o sftpgo
go build -i -tags nogcs,nos3,nosqlite -ldflags "-s -w -X github.com/drakkan/sftpgo/utils.commit=`git describe --always --dirty` -X github.com/drakkan/sftpgo/utils.date=`date -u +%FT%TZ`" -o sftpgo
```

You should get a version that includes git commit and build date like this one:
You should get a version that includes git commit, build date and available features like this one:

```bash
$ sftpgo -v
SFTPGo version: 0.9.0-dev-90607d4-dirty-2019-08-08T19:28:36Z
$ ./sftpgo -v
SFTPGo 0.9.6-dev-15298b0-dirty-2020-05-22T21:25:51Z -gcs -s3 +bolt +mysql +pgsql -sqlite +portable
```
7 changes: 6 additions & 1 deletion httpd/schema/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ openapi: 3.0.1
info:
title: SFTPGo
description: 'SFTPGo REST API'
version: 1.8.5
version: 1.8.6

servers:
- url: /api/v1
Expand Down Expand Up @@ -1278,6 +1278,11 @@ components:
type: string
commit_hash:
type: string
features:
type: array
items:
type: string
description: Features for the current build. Available features are "portable", "bolt", "mysql", "sqlite", "pgsql", "s3", "gcs". If a feature is available it has a "+" prefix, otherwise a "-" prefix
securitySchemes:
BasicAuth:
type: http
Expand Down
8 changes: 1 addition & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@
// https://github.com/drakkan/sftpgo/blob/master/README.md
package main // import "github.com/drakkan/sftpgo"

import (
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"

"github.com/drakkan/sftpgo/cmd"
)
import "github.com/drakkan/sftpgo/cmd"

func main() {
cmd.Execute()
Expand Down
Loading

0 comments on commit ad53429

Please sign in to comment.