From 63dd43b8cd0028ea11b531fd4cfca172d1f7a078 Mon Sep 17 00:00:00 2001 From: guseggert <877588+guseggert@users.noreply.github.com> Date: Mon, 23 Aug 2021 21:44:23 -0400 Subject: [PATCH] ci: preload peerlog plugin, disable by default This preloads the peerlog plugin in the ipfs binary, but keeps it disabled by default. To enabled it, set Enabled=true in its config. The motivation is to simplify building and deploying gateways, and for them to use binaries that are more similar to release bins. (cherry picked from commit a35dd2ea0d0493f1d7524a70f2d414d51523efe9) --- plugin/loader/preload.go | 2 + plugin/loader/preload_list | 1 + plugin/plugins/Rules.mk | 2 +- plugin/plugins/peerlog/peerlog.go | 24 +++++++++++- test/sharness/t0280-plugin-peerlog.sh | 53 +++++++++++++++++++++++++++ 5 files changed, 80 insertions(+), 2 deletions(-) create mode 100755 test/sharness/t0280-plugin-peerlog.sh diff --git a/plugin/loader/preload.go b/plugin/loader/preload.go index 2c6d512bf..4be60625f 100644 --- a/plugin/loader/preload.go +++ b/plugin/loader/preload.go @@ -5,6 +5,7 @@ import ( pluginflatfs "github.com/ipfs/go-ipfs/plugin/plugins/flatfs" pluginipldgit "github.com/ipfs/go-ipfs/plugin/plugins/git" pluginlevelds "github.com/ipfs/go-ipfs/plugin/plugins/levelds" + pluginpeerlog "github.com/ipfs/go-ipfs/plugin/plugins/peerlog" ) // DO NOT EDIT THIS FILE @@ -16,4 +17,5 @@ func init() { Preload(pluginbadgerds.Plugins...) Preload(pluginflatfs.Plugins...) Preload(pluginlevelds.Plugins...) + Preload(pluginpeerlog.Plugins...) } diff --git a/plugin/loader/preload_list b/plugin/loader/preload_list index 509ea5a22..d648da935 100644 --- a/plugin/loader/preload_list +++ b/plugin/loader/preload_list @@ -8,3 +8,4 @@ ipldgit github.com/ipfs/go-ipfs/plugin/plugins/git * badgerds github.com/ipfs/go-ipfs/plugin/plugins/badgerds * flatfs github.com/ipfs/go-ipfs/plugin/plugins/flatfs * levelds github.com/ipfs/go-ipfs/plugin/plugins/levelds * +peerlog github.com/ipfs/go-ipfs/plugin/plugins/peerlog * \ No newline at end of file diff --git a/plugin/plugins/Rules.mk b/plugin/plugins/Rules.mk index 12c7cf0b3..334ea4491 100644 --- a/plugin/plugins/Rules.mk +++ b/plugin/plugins/Rules.mk @@ -1,6 +1,6 @@ include mk/header.mk -$(d)_plugins:=$(d)/git $(d)/badgerds $(d)/flatfs $(d)/levelds +$(d)_plugins:=$(d)/git $(d)/badgerds $(d)/flatfs $(d)/levelds $(d)/peerlog $(d)_plugins_so:=$(addsuffix .so,$($(d)_plugins)) $(d)_plugins_main:=$(addsuffix /main/main.go,$($(d)_plugins)) diff --git a/plugin/plugins/peerlog/peerlog.go b/plugin/plugins/peerlog/peerlog.go index 97f11eee6..33fb26372 100644 --- a/plugin/plugins/peerlog/peerlog.go +++ b/plugin/plugins/peerlog/peerlog.go @@ -45,6 +45,7 @@ type plEvent struct { // {"level":"info","ts":"2020-02-10T13:54:59.095Z","logger":"plugin/peerlog","caller":"peerlog/peerlog.go:56","msg":"identified","peer":"QmS2H72gdrekXJggGdE9SunXPntBqdkJdkXQJjuxcH8Cbt","agent":"go-ipfs/0.5.0/"} // type peerLogPlugin struct { + enabled bool droppedCount uint64 events chan plEvent } @@ -67,8 +68,25 @@ func (*peerLogPlugin) Version() string { } // Init initializes plugin -func (pl *peerLogPlugin) Init(*plugin.Environment) error { +func (pl *peerLogPlugin) Init(env *plugin.Environment) error { pl.events = make(chan plEvent, eventQueueSize) + + // plugin is disabled by default, unless Enabled=true + if env.Config != nil { + mapIface, ok := env.Config.(map[string]interface{}) + if !ok { + return nil + } + enabledIface, ok := mapIface["Enabled"] + if !ok || enabledIface == nil { + return nil + } + enabled, ok := enabledIface.(bool) + if !ok { + return nil + } + pl.enabled = enabled + } return nil } @@ -153,6 +171,10 @@ func (pl *peerLogPlugin) emit(evt eventType, p peer.ID) { } func (pl *peerLogPlugin) Start(node *core.IpfsNode) error { + if !pl.enabled { + return nil + } + // Ensure logs from this plugin get printed regardless of global IPFS_LOGGING value if err := logging.SetLogLevel("plugin/peerlog", "info"); err != nil { return fmt.Errorf("failed to set log level: %w", err) diff --git a/test/sharness/t0280-plugin-peerlog.sh b/test/sharness/t0280-plugin-peerlog.sh new file mode 100755 index 000000000..f240582b8 --- /dev/null +++ b/test/sharness/t0280-plugin-peerlog.sh @@ -0,0 +1,53 @@ +#!/usr/bin/env bash +# +# Copyright (c) 2017 Jakub Sztandera +# MIT Licensed; see the LICENSE file in this repository. +# + +test_description="Test peerlog plugin" + +. lib/test-lib.sh + +test_expect_success "setup testbed" ' + iptb testbed create -type localipfs -count 2 -force -init +' + +startup_cluster 2 + +test_expect_success "peerlog is disabled by default" ' + go-sleep 100ms + iptb logs 0 >node0logs + test_expect_code 1 grep peerlog node0logs +' + +test_expect_success 'stop iptb' 'iptb stop' + + + +test_expect_success "setup testbed" ' + iptb testbed create -type localipfs -count 2 -force -init +' + +test_expect_success "enable peerlog config setting" ' + iptb run -- ipfs config --json Plugins.Plugins.peerlog.Config.Enabled true +' + +startup_cluster 2 + +test_expect_success "peerlog plugin is logged" ' + go-sleep 100ms + iptb logs 0 >node0logs + grep peerlog node0logs +' + +test_expect_success 'peer id' ' + PEERID_1=$(iptb attr get 1 id) +' + +test_expect_success "peer id is logged" ' + iptb logs 0 | grep -q "$PEERID_1" +' + +test_expect_success 'stop iptb' 'iptb stop' + +test_done