Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add postgres datastore type #5403

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}