From 12e3d10970f68c4ddb5a883fd70df5957fc6683c Mon Sep 17 00:00:00 2001 From: Ian Norden Date: Tue, 18 Dec 2018 14:44:34 -0600 Subject: [PATCH] move postgres to plugins; test in head context with updated dependencies; undo gx releases; address feedback; gx hashes will need to be updated once go-ipfs-config PR is merged and gx released" --- .gitignore | 2 - misc/utility/ipfs_postgres.sh | 19 ++++ package.json | 6 ++ plugin/loader/preload.go | 2 + plugin/loader/preload_list | 1 + plugin/plugins/Rules.mk | 2 +- plugin/plugins/postgresds/postgresds.go | 126 ++++++++++++++++++++++++ 7 files changed, 155 insertions(+), 3 deletions(-) create mode 100755 misc/utility/ipfs_postgres.sh create mode 100644 plugin/plugins/postgresds/postgresds.go diff --git a/.gitignore b/.gitignore index 8cf45a5e6e4d..1e21b5cc2fe6 100644 --- a/.gitignore +++ b/.gitignore @@ -22,5 +22,3 @@ bin/tmp vendor .tarball go-ipfs-source.tar.gz - -.idea diff --git a/misc/utility/ipfs_postgres.sh b/misc/utility/ipfs_postgres.sh new file mode 100755 index 000000000000..58cc40824377 --- /dev/null +++ b/misc/utility/ipfs_postgres.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] + then + echo "Env variables not provided" + echo "Usage: ./ipfs_postgres.sh " + exit 1 +fi + +export IPFS_PGHOST=$1 +export IPFS_PGUSER=$2 +export IPFS_PGDATABASE=$3 +printf "${IPFS_PGUSER}@${IPFS_PGHOST} password:" +stty -echo +read IPFS_PGPASSWORD +stty echo +export IPFS_PGPASSWORD + +ipfs init --profile=postgresds diff --git a/package.json b/package.json index 5154d713fc0a..06b52e53ace5 100644 --- a/package.json +++ b/package.json @@ -605,6 +605,12 @@ "hash": "QmRmMbeY5QC5iMsuW16wchtFt8wmYTv2suWb8t9MV8dsxm", "name": "go-libp2p-autonat-svc", "version": "1.0.5" + }, + { + "author": "whyrusleeping", + "hash": "QmaVEopfh4uAWPA3fGDs41vsHNo9HinHJMCYczLjDqFoyb", + "name": "sql-datastore", + "version": "1.0.0" } ], "gxVersion": "0.10.0", diff --git a/plugin/loader/preload.go b/plugin/loader/preload.go index 730f3538e74c..987e21aa1abc 100644 --- a/plugin/loader/preload.go +++ b/plugin/loader/preload.go @@ -6,6 +6,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" + pluginpostgresds "github.com/ipfs/go-ipfs/plugin/plugins/postgresds" ) // DO NOT EDIT THIS FILE @@ -17,4 +18,5 @@ var preloadPlugins = []plugin.Plugin{ pluginbadgerds.Plugins[0], pluginflatfs.Plugins[0], pluginlevelds.Plugins[0], + pluginpostgresds.Plugins[0], } diff --git a/plugin/loader/preload_list b/plugin/loader/preload_list index 6155dfbc782b..3141f330dba0 100644 --- a/plugin/loader/preload_list +++ b/plugin/loader/preload_list @@ -8,3 +8,4 @@ ipldgit github.com/ipfs/go-ipfs/plugin/plugins/git 0 badgerds github.com/ipfs/go-ipfs/plugin/plugins/badgerds 0 flatfs github.com/ipfs/go-ipfs/plugin/plugins/flatfs 0 levelds github.com/ipfs/go-ipfs/plugin/plugins/levelds 0 +# postgresds github.com/ipfs/go-ipfs/plugin/plugins/postgresds 0 diff --git a/plugin/plugins/Rules.mk b/plugin/plugins/Rules.mk index 80924bad2026..b0f0adab4e36 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)/postgresds $(d)_plugins_so:=$(addsuffix .so,$($(d)_plugins)) $(d)_plugins_main:=$(addsuffix /main/main.go,$($(d)_plugins)) diff --git a/plugin/plugins/postgresds/postgresds.go b/plugin/plugins/postgresds/postgresds.go new file mode 100644 index 000000000000..7e0d30762835 --- /dev/null +++ b/plugin/plugins/postgresds/postgresds.go @@ -0,0 +1,126 @@ +package postgresds + +import ( + "fmt" + "io/ioutil" + "path/filepath" + "strings" + + "github.com/ipfs/go-ipfs/plugin" + "github.com/ipfs/go-ipfs/repo" + "github.com/ipfs/go-ipfs/repo/fsrepo" + + postgresdb "gx/ipfs/QmaVEopfh4uAWPA3fGDs41vsHNo9HinHJMCYczLjDqFoyb/sql-datastore/postgres" + "gx/ipfs/QmdcULN1WCzgoQmcCaUAmEhwcxHYsDrbZ2LvRJKCL8dMrK/go-homedir" +) + +// Plugins is exported list of plugins that will be loaded +var Plugins = []plugin.Plugin{ + &postgresdsPlugin{}, +} + +type postgresdsPlugin struct{} + +var _ plugin.PluginDatastore = (*postgresdsPlugin)(nil) + +func (*postgresdsPlugin) Name() string { + return "ds-postgresds" +} + +func (*postgresdsPlugin) Version() string { + return "0.1.0" +} + +func (*postgresdsPlugin) Init() error { + return nil +} + +func (*postgresdsPlugin) DatastoreTypeName() string { + return "postgres" +} + +type datastoreConfig struct { + host string + port string + user string + passfile string + password string + dbname string +} + +// Returns a configuration stub for a postgres datastore from the given parameters +func (*postgresdsPlugin) DatastoreConfigParser() fsrepo.ConfigFromMap { + return func(params map[string]interface{}) (fsrepo.DatastoreConfig, error) { + var c datastoreConfig + var ok bool + c.passfile, ok = params["passfile"].(string) + if !ok { + return nil, fmt.Errorf("'passfile' field was not a string") + } + if c.passfile != "" { + path, err := homedir.Expand(filepath.Clean(c.passfile)) + if err != nil { + return nil, err + } + info, err := ioutil.ReadFile(path) + if err != nil { + return nil, err + } + envVars := strings.Split(string(info), ":") + if len(envVars) != 5 { + return nil, fmt.Errorf("passfile at %s not of format: ::::", c.passfile) + } + c.host = envVars[0] + c.port = envVars[1] + c.dbname = envVars[2] + c.user = envVars[3] + c.password = envVars[4] + return &c, nil + } + c.host, ok = params["host"].(string) + if !ok { + return nil, fmt.Errorf("'path' field was not a string") + } + c.port, ok = params["port"].(string) + if !ok { + return nil, fmt.Errorf("'port' field was not a string") + } + c.user, ok = params["user"].(string) + if !ok { + return nil, fmt.Errorf("'user' field was not a string") + } + c.dbname, ok = params["dbname"].(string) + if !ok { + return nil, fmt.Errorf("'dbname' field was not a string") + } + c.password, ok = params["password"].(string) + if !ok { + return nil, fmt.Errorf("'password' field was not a string") + } + return &c, nil + } +} + +func (c *datastoreConfig) DiskSpec() fsrepo.DiskSpec { + return map[string]interface{}{ + "type": "postgres", + "user": c.user, + "database": c.dbname, + } +} + +func (c *datastoreConfig) Create(path string) (repo.Datastore, error) { + pg := postgresdb.Options{ + Host: c.host, + User: c.user, + Database: c.dbname, + Password: c.password, + Port: c.port, + } + ds, err := pg.Create() + if err != nil { + fmt.Println("error loading pg: ", err) + return ds, err + } + return ds, nil +}