From 0c92a778b1b7bd3e608bd820673cc89fbacdad88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Sun, 17 Sep 2017 23:07:43 +0200 Subject: [PATCH 1/2] plugin: support system-wide plugins MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Łukasz Magiera --- Rules.mk | 11 ++++++----- cmd/ipfs/Rules.mk | 5 ++++- cmd/ipfs/main.go | 11 ++++++++--- repo/config/version.go | 3 +++ 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/Rules.mk b/Rules.mk index ca9d1bf25d7..2b51c24f7e5 100644 --- a/Rules.mk +++ b/Rules.mk @@ -128,11 +128,12 @@ help: @echo '' @echo 'BUILD TARGETS:' @echo '' - @echo ' all - print this help message' - @echo ' build - Build binary at ./cmd/ipfs/ipfs' - @echo ' nofuse - Build binary with no fuse support' - @echo ' install - Build binary and install into $$GOPATH/bin' -# @echo ' dist_install - TODO: c.f. ./cmd/ipfs/dist/README.md' + @echo ' all - print this help message' + @echo ' build - Build binary at ./cmd/ipfs/ipfs' + @echo ' nofuse - Build binary with no fuse support' + @echo ' install - Build binary and install into $$GOPATH/bin' +# @echo ' dist_install - TODO: c.f. ./cmd/ipfs/dist/README.md' + @echo ' build_plugins - Build plugin binaries' @echo '' @echo 'CLEANING TARGETS:' @echo '' diff --git a/cmd/ipfs/Rules.mk b/cmd/ipfs/Rules.mk index 175b098eea2..8c9a15a8692 100644 --- a/cmd/ipfs/Rules.mk +++ b/cmd/ipfs/Rules.mk @@ -13,7 +13,10 @@ PATH := $(realpath $(d)):$(PATH) # DEPS_OO_$(d) += merkledag/pb/merkledag.pb.go namesys/pb/namesys.pb.go # DEPS_OO_$(d) += pin/internal/pb/header.pb.go unixfs/pb/unixfs.pb.go -$(d)_flags =-ldflags="-X "github.com/ipfs/go-ipfs/repo/config".CurrentCommit=$(shell git rev-parse --short HEAD)" +CONFIG_COMMIT ?= github.com/ipfs/go-ipfs/repo/config.CurrentCommit=$(shell git rev-parse --short HEAD) +CONFIG_PLUGIN_PATH ?= github.com/ipfs/go-ipfs/repo/config.SystemPluginPath=${PLUGIN_PATH} + +$(d)_flags =-ldflags="-X ${CONFIG_COMMIT} -X ${CONFIG_PLUGIN_PATH}" $(d)-try-build $(IPFS_BIN_$(d)): GOFLAGS += $(cmd/ipfs_flags) diff --git a/cmd/ipfs/main.go b/cmd/ipfs/main.go index 7b8b68b6ccd..aff2f79a542 100644 --- a/cmd/ipfs/main.go +++ b/cmd/ipfs/main.go @@ -52,7 +52,6 @@ const ( type cmdInvocation struct { req *cmds.Request node *core.IpfsNode - ctx *oldcmds.Context } type exitErr int @@ -181,7 +180,7 @@ func makeExecutor(req *cmds.Request, env interface{}) (cmds.Executor, error) { exctr = client.(cmds.Executor) } else { cctx := env.(*oldcmds.Context) - pluginpath := filepath.Join(cctx.ConfigRoot, "plugins") + pluginPath := filepath.Join(cctx.ConfigRoot, "plugins") // check if repo is accessible before loading plugins ok, err := checkPermissions(cctx.ConfigRoot) @@ -189,11 +188,17 @@ func makeExecutor(req *cmds.Request, env interface{}) (cmds.Executor, error) { return nil, err } if ok { - if _, err := loader.LoadPlugins(pluginpath); err != nil { + if _, err := loader.LoadPlugins(pluginPath); err != nil { log.Warning("error loading plugins: ", err) } } + if config.SystemPluginPath != "" { + if _, err := loader.LoadPlugins(config.SystemPluginPath); err != nil { + return nil, err + } + } + exctr = cmds.NewExecutor(req.Root) } diff --git a/repo/config/version.go b/repo/config/version.go index 43546fc11a9..586d8d85516 100644 --- a/repo/config/version.go +++ b/repo/config/version.go @@ -3,6 +3,9 @@ package config // CurrentCommit is the current git commit, this is set as a ldflag in the Makefile var CurrentCommit string +// SystemPluginPath is the a system-global plugin path, this is set as a ldflag in the Makefile +var SystemPluginPath string + // CurrentVersionNumber is the current application's version literal const CurrentVersionNumber = "0.4.14-dev" From 5fae9a4aa90a37ca6918306b2d470b2501eae895 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Tue, 26 Sep 2017 00:56:25 +0200 Subject: [PATCH 2/2] plugin: add sharness tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Łukasz Magiera --- Makefile | 1 + cmd/ipfs/Rules.mk | 8 ++++---- config.mk | 2 ++ test/sharness/Rules.mk | 7 +++++-- test/sharness/t0280-plugin-git.sh | 32 ++++++++++++++++++++++++++----- 5 files changed, 39 insertions(+), 11 deletions(-) create mode 100644 config.mk diff --git a/Makefile b/Makefile index 9f5e509d60c..629ed12e1b7 100644 --- a/Makefile +++ b/Makefile @@ -7,4 +7,5 @@ PROTOC = protoc --gogo_out=. --proto_path=.:/usr/local/opt/protobuf/include:$(di # enable second expansion .SECONDEXPANSION: +include config.mk include Rules.mk diff --git a/cmd/ipfs/Rules.mk b/cmd/ipfs/Rules.mk index 8c9a15a8692..40cee7a87f8 100644 --- a/cmd/ipfs/Rules.mk +++ b/cmd/ipfs/Rules.mk @@ -13,10 +13,10 @@ PATH := $(realpath $(d)):$(PATH) # DEPS_OO_$(d) += merkledag/pb/merkledag.pb.go namesys/pb/namesys.pb.go # DEPS_OO_$(d) += pin/internal/pb/header.pb.go unixfs/pb/unixfs.pb.go -CONFIG_COMMIT ?= github.com/ipfs/go-ipfs/repo/config.CurrentCommit=$(shell git rev-parse --short HEAD) -CONFIG_PLUGIN_PATH ?= github.com/ipfs/go-ipfs/repo/config.SystemPluginPath=${PLUGIN_PATH} - -$(d)_flags =-ldflags="-X ${CONFIG_COMMIT} -X ${CONFIG_PLUGIN_PATH}" +$(d)_flags =-ldflags=" +$(d)_flags += -X github.com/ipfs/go-ipfs/repo/config.CurrentCommit=${CONFIG_COMMIT} +$(d)_flags += -X github.com/ipfs/go-ipfs/repo/config.SystemPluginPath=${CONFIG_PLUGIN_PATH} +$(d)_flags += " $(d)-try-build $(IPFS_BIN_$(d)): GOFLAGS += $(cmd/ipfs_flags) diff --git a/config.mk b/config.mk new file mode 100644 index 00000000000..de8ebfb764c --- /dev/null +++ b/config.mk @@ -0,0 +1,2 @@ +CONFIG_COMMIT := $(shell git rev-parse --short HEAD) +CONFIG_PLUGIN_PATH = ${PLUGIN_PATH} diff --git a/test/sharness/Rules.mk b/test/sharness/Rules.mk index 188cb50cb9b..ba69f64332f 100644 --- a/test/sharness/Rules.mk +++ b/test/sharness/Rules.mk @@ -7,9 +7,9 @@ T_$(d) = $(sort $(wildcard $(d)/t[0-9][0-9][0-9][0-9]-*.sh)) DEPS_$(d) := test/bin/random test/bin/multihash test/bin/pollEndpoint \ test/bin/iptb test/bin/go-sleep test/bin/random-files \ test/bin/go-timeout test/bin/hang-fds test/bin/ma-pipe-unidir -DEPS_$(d) += cmd/ipfs/ipfs DEPS_$(d) += $(d)/clean-test-results DEPS_$(d) += $(SHARNESS_$(d)) +DEPS_$(d) += $(d)/sharness-ipfs ifeq ($(OS),Linux) PLUGINS_DIR_$(d) := $(d)/plugins/ @@ -49,13 +49,16 @@ $(SHARNESS_$(d)): $(d) ALWAYS $(d)/deps: $(SHARNESS_$(d)) $$(DEPS_$(d)) # use second expansion so coverage can inject dependency .PHONY: $(d)/deps +$(d)/sharness-ipfs: PLUGIN_PATH=./system_plugins +$(d)/sharness-ipfs: cmd/ipfs/ipfs +.PHONY: $(d)/sharness-ipfs + test_sharness_deps: $(d)/deps .PHONY: test_sharness_deps test_sharness_short: $(d)/aggregate .PHONY: test_sharness_short - test_sharness_expensive: export TEST_EXPENSIVE=1 test_sharness_expensive: test_sharness_short .PHONY: test_sharness_expensive diff --git a/test/sharness/t0280-plugin-git.sh b/test/sharness/t0280-plugin-git.sh index a93dfdebab3..f3378b106c4 100755 --- a/test/sharness/t0280-plugin-git.sh +++ b/test/sharness/t0280-plugin-git.sh @@ -17,11 +17,6 @@ fi test_init_ipfs -test_expect_success "copy plugin" ' - mkdir -p "$IPFS_PATH/plugins" && - cp ../plugins/git.so "$IPFS_PATH/plugins/" -' - # from https://github.com/ipfs/go-ipld-git/blob/master/make-test-repo.sh test_expect_success "prepare test data" ' tar xzf ../t0280-plugin-git-data/git.tar.gz @@ -46,6 +41,33 @@ test_dag_git() { ' } +test_expect_success "copy plugin to local plugin directory" ' + mkdir -p "$IPFS_PATH/plugins" && + chmod +x ../plugins/git.so && + cp ../plugins/git.so "$IPFS_PATH/plugins/" +' + +# should work offline +#test_dag_git + +# should work online +test_launch_ipfs_daemon +test_dag_git +test_kill_ipfs_daemon + +test_expect_success "copy plugin to global plugin directory" ' + mkdir -p "./system_plugins" && + cp ../plugins/git.so "./system_plugins/" +' + +test_expect_success "fail with duplicate plugin" ' + echo 123 | test_must_fail ipfs dag put +' + +test_expect_success "remove local plugin" ' + rm -r "$IPFS_PATH/plugins/" +' + # should work offline #test_dag_git