Skip to content

Commit

Permalink
move postgres to plugins; test in head context with updated dependenc…
Browse files Browse the repository at this point in the history
…ies; undo gx releases; address feedback; gx hashes will need to be updated once go-ipfs-config PR is merged and gx released"
  • Loading branch information
i-norden authored and Stebalien committed Jan 24, 2019
1 parent 0ffca94 commit d8f9c3b
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 3 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,3 @@ bin/tmp
vendor
.tarball
go-ipfs-source.tar.gz

.idea
19 changes: 19 additions & 0 deletions misc/utility/ipfs_postgres.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh

if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]
then
echo "Env variables not provided"
echo "Usage: ./ipfs_postgres.sh <IPFS_PGHOST> <IPFS_PGUSER> <IPFS_PGDATABASE>"
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
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,12 @@
"hash": "QmRmMbeY5QC5iMsuW16wchtFt8wmYTv2suWb8t9MV8dsxm",
"name": "go-libp2p-autonat-svc",
"version": "1.0.5"
},
{
"author": "whyrusleeping",
"hash": "QmZmPKg1RKY2Cy5czKEBJTfDYzenbGww2pFNQKxLSKrPRB",
"name": "sql-datastore",
"version": "1.0.2"
}
],
"gxVersion": "0.10.0",
Expand Down
2 changes: 2 additions & 0 deletions plugin/loader/preload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,4 +18,5 @@ var preloadPlugins = []plugin.Plugin{
pluginbadgerds.Plugins[0],
pluginflatfs.Plugins[0],
pluginlevelds.Plugins[0],
pluginpostgresds.Plugins[0],
}
1 change: 1 addition & 0 deletions plugin/loader/preload_list
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion plugin/plugins/Rules.mk
Original file line number Diff line number Diff line change
@@ -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))

Expand Down
126 changes: 126 additions & 0 deletions plugin/plugins/postgresds/postgresds.go
Original file line number Diff line number Diff line change
@@ -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/QmZmPKg1RKY2Cy5czKEBJTfDYzenbGww2pFNQKxLSKrPRB/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: <IPFS_PGHOST>:<IPFS_PGPORT>:<IPFS_PGDATABASE>:<IPFS_PGUSER>:<IPFS_PGPASSWORD>", 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
}

1 comment on commit d8f9c3b

@GitCop
Copy link

@GitCop GitCop commented on d8f9c3b Jan 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There were the following issues with your Pull Request

We ask for a few features in the commit message for Open Source licensing hygiene and commit message clarity.
git commit --amend can often help you quickly improve the commit message.
Guidelines and a script are available to help in the long run.
Your feedback on GitCop is welcome on this issue.


This message was auto-generated by https://gitcop.com

Please sign in to comment.