Skip to content

Commit

Permalink
refactor(fsrepo): remove profile.json storage from fsrepo
Browse files Browse the repository at this point in the history
  • Loading branch information
b5 committed Apr 5, 2018
1 parent 5ea4e22 commit abcd937
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 103 deletions.
31 changes: 16 additions & 15 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,27 @@ func New(r repo.Repo, options ...func(*config.Config)) (s *Server, err error) {
s.qriNode, err = p2p.NewQriNode(r, func(c *config.P2P) {
*c = *cfg.P2P
})
if err != nil {
return s, err
}

return s, nil
}

// Serve starts the server. It will block while the server is running
func (s *Server) Serve() (err error) {
server := &http.Server{}
server.Handler = NewServerRoutes(s)

go s.ServeRPC()
go s.ServeWebapp()

// func(p2pcfg *config.P2P) {
// p2pcfg.Online = s.cfg.Online
// if cfg.BoostrapAddrs != nil {
// p2pcfg.QriBootstrapAddrs = cfg.BoostrapAddrs
// }
// })
if err != nil {
return s, err
}

// bootstrapped := false
peerBootstrapped := func(peerId string) {
Expand All @@ -71,20 +83,9 @@ func New(r repo.Repo, options ...func(*config.Config)) (s *Server, err error) {

err = s.qriNode.StartOnlineServices(peerBootstrapped)
if err != nil {
return nil, fmt.Errorf("error starting P2P service: %s", err.Error())
return fmt.Errorf("error starting P2P service: %s", err.Error())
}

return s, nil
}

// Serve starts the server. It will block while the server is running
func (s *Server) Serve() (err error) {
server := &http.Server{}
server.Handler = NewServerRoutes(s)

go s.ServeRPC()
go s.ServeWebapp()

if node, err := s.qriNode.IPFSNode(); err == nil {
go func() {
if err := core.CheckVersion(context.Background(), node.Namesys); err == core.ErrUpdateRequired {
Expand Down
1 change: 1 addition & 0 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ func initDataset(name repo.DatasetRef) {
ref := repo.DatasetRef{}
err = req.Init(p, &ref)
ExitIfErr(err)

if ref.Dataset.Structure.ErrCount > 0 {
printWarning(fmt.Sprintf("this dataset has %d validation errors", ref.Dataset.Structure.ErrCount))
if addDsShowValidation {
Expand Down
8 changes: 8 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"path/filepath"

golog "github.com/ipfs/go-log"
"github.com/qri-io/qri/config"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -113,5 +114,12 @@ we won't be shipping changes that require starting over.
err = fmt.Errorf(str, err.Error(), QriRepoPath)
}

// configure logging straight away
if cfg != nil && cfg.Logging != nil {
for name, level := range cfg.Logging.Levels {
golog.SetLogLevel(name, level)
}
}

return err
}
10 changes: 6 additions & 4 deletions cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,16 @@ peers & swapping data.`,
}

func init() {
connectCmd.Flags().StringVarP(&connectCmdAPIPort, "api-port", "", config.DefaultAPIPort, "port to start api on")
connectCmd.Flags().StringVarP(&connectCmdRPCPort, "rpc-port", "", config.DefaultRPCPort, "port to start rpc listener on")
connectCmd.Flags().StringVarP(&connectCmdWebappPort, "webapp-port", "", config.DefaultWebappPort, "port to serve webapp on")
connectCmd.Flags().BoolVarP(&connectSetup, "setup", "", false, "run setup if necessary, reading options from enviornment variables")
connectCmd.Flags().StringVarP(&connectCmdAPIPort, "api-port", "", "", "port to start api on")
connectCmd.Flags().StringVarP(&connectCmdRPCPort, "rpc-port", "", "", "port to start rpc listener on")
connectCmd.Flags().StringVarP(&connectCmdWebappPort, "webapp-port", "", "", "port to serve webapp on")

connectCmd.Flags().BoolVarP(&disableAPI, "disable-api", "", false, "disables api, overrides the api-port flag")
connectCmd.Flags().BoolVarP(&disableRPC, "disable-rpc", "", false, "disables rpc, overrides the rpc-port flag")
connectCmd.Flags().BoolVarP(&disableWebapp, "disable-webapp", "", false, "disables webapp, overrides the webapp-port flag")
connectCmd.Flags().BoolVarP(&disableP2P, "disable-p2p", "", false, "disable peer-2-peer networking")

connectCmd.Flags().BoolVarP(&connectSetup, "setup", "", false, "run setup if necessary, reading options from enviornment variables")
connectCmd.Flags().BoolVarP(&connectReadOnly, "read-only", "", false, "run qri in read-only mode, limits the api endpoints")
RootCmd.AddCommand(connectCmd)
}
6 changes: 3 additions & 3 deletions cmd/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func getRepo(online bool) repo.Repo {
ExitIfErr(err)

fs := getIpfsFilestore(online)
r, err := fsrepo.NewRepo(fs, QriRepoPath, cfg.Profile.ID)
r, err := fsrepo.NewRepo(fs, cfg.Profile, QriRepoPath)
r.SetPrivateKey(pk)
r.SetProfile(pro)

Expand Down Expand Up @@ -148,7 +148,7 @@ func repoOrClient(online bool) (repo.Repo, *rpc.Client, error) {
// cfg, err := readConfigFile()
// ExitIfErr(err)

r, err := fsrepo.NewRepo(fs, QriRepoPath, cfg.Profile.ID)
r, err := fsrepo.NewRepo(fs, cfg.Profile, QriRepoPath)
ExitIfErr(err)

// c, _ := json.MarshalIndent(cfg, "", " ")
Expand Down Expand Up @@ -193,7 +193,7 @@ func qriNode(online bool) (node *p2p.QriNode, err error) {
// return
// }

r, err = fsrepo.NewRepo(fs, QriRepoPath, cfg.Profile.ID)
r, err = fsrepo.NewRepo(fs, cfg.Profile, QriRepoPath)
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion config/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (API) Default() *API {
Port: DefaultAPIPort,
TLS: false,
AllowedOrigins: []string{
"http://localhost:2505",
"http://localhost:" + DefaultWebappPort,
},
}
}
1 change: 1 addition & 0 deletions config/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func (Logging) Default() *Logging {
return &Logging{
Levels: map[string]string{
"qriapi": "info",
"qrip2p": "info",
},
}
}
7 changes: 0 additions & 7 deletions config/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,6 @@ func (cfg *P2P) DecodePeerID() (peer.ID, error) {
// if r == nil {
// return fmt.Errorf("need a qri Repo to create a qri node")
// }
// // if r == nil && cfg.RepoPath != "" {
// // repo, err := fs_repo.NewRepo(store, cfg.RepoPath, cfg.canonicalPeerId(store))
// // if err != nil {
// // return err
// // }
// // cfg.Repo = repo
// // }

// // If no listening addresses are set, allocate
// // a tcp multiaddress on local host bound to the default port
Expand Down
2 changes: 1 addition & 1 deletion p2p/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (n *QriNode) Bootstrap(boostrapAddrs []string, boostrapPeers chan pstore.Pe
log.Infof("boostrapping to: %s", p.ID.Pretty())
if err := n.Host.Connect(context.Background(), p); err == nil {
if err = n.AddQriPeer(p); err != nil {
log.Infof("error adding peer: %s", err.Error())
log.Errorf("error adding peer: %s", err.Error())
} else {
boostrapPeers <- p
}
Expand Down
11 changes: 7 additions & 4 deletions p2p/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const qriSupportKey = "qri-support"
// StartDiscovery initiates peer discovery, allocating a discovery
// services if one doesn't exist, then registering to be notified on peer discovery
func (n *QriNode) StartDiscovery(bootstrapPeers chan pstore.PeerInfo) error {
log.Info("starting peer discovery")

if n.Discovery == nil {
service, err := discovery.NewMdnsService(context.Background(), n.Host, time.Second*5, QriServiceTag)
if err != nil {
Expand All @@ -40,23 +42,24 @@ func (n *QriNode) StartDiscovery(bootstrapPeers chan pstore.PeerInfo) error {
// HandlePeerFound deals with the discovery of a peer that may or may not support
// the qri protocol
func (n *QriNode) HandlePeerFound(pinfo pstore.PeerInfo) {

// first check to see if we've seen this peer before
if _, err := n.Host.Peerstore().Get(pinfo.ID, qriSupportKey); err == nil {
return
} else if support, err := n.SupportsQriProtocol(pinfo.ID); err == nil {
if err := n.Host.Peerstore().Put(pinfo.ID, qriSupportKey, support); err != nil {
fmt.Println("error setting qri support flag", err.Error())
log.Errorf("error setting qri support flag", err.Error())
return
}

if support {
if err := n.AddQriPeer(pinfo); err != nil {
fmt.Println(err.Error())
log.Errorf("error adding qri peer: %s", err.Error())
} else {
log.Infof("discovered qri peer: %s", pinfo.ID)
}
}
} else if err != nil && err != errNoProtos {
fmt.Println("error checking for qri support:", err.Error())
log.Errorf("error checking for qri support:", err.Error())
}
}

Expand Down
6 changes: 2 additions & 4 deletions p2p/p2p.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// Package p2p implements qri peer-to-peer communication.
// This is very, very early days, with message passing sorely in need of a
// rewrite, but hey it's a start.
package p2p

import (
Expand All @@ -12,7 +10,7 @@ import (
protocol "gx/ipfs/QmZNkThpqfVXs9GNbexPrfBbXSLNYeKrE7jwFM2oqHbyqN/go-libp2p-protocol"
)

var log = golog.Logger("p2p")
var log = golog.Logger("qrip2p")

// QriProtocolID is the top level Protocol Identifier
const QriProtocolID = protocol.ID("/qri")
Expand All @@ -21,7 +19,7 @@ const QriProtocolID = protocol.ID("/qri")
const QriServiceTag = "qri/0.2.1"

func init() {
// golog.SetLogLevel("p2p", "debug")
// golog.SetLogLevel("qrip2p", "debug")

// ipfs core includes a client version. seems like a good idea.
// TODO - understand where & how client versions are used
Expand Down
3 changes: 0 additions & 3 deletions repo/fs/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ const (
// FileInfo stores information about this repository
// like version number, size of repo, etc.
FileInfo
// FileProfile is this node's user profile
FileProfile
// FileConfig holds configuration specific to this repo
FileConfig
// FileDatasets holds the list of datasets
Expand Down Expand Up @@ -67,7 +65,6 @@ var paths = map[File]string{
FileUnknown: "",
FileLockfile: "/repo.lock",
FileInfo: "/info.json",
FileProfile: "/profile.json",
FileConfig: "/config.json",
FileDatasets: "/datasets.json",
FileEventLogs: "/events.json",
Expand Down
73 changes: 13 additions & 60 deletions repo/fs/fs.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package fsrepo

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"

golog "github.com/ipfs/go-log"
"github.com/libp2p/go-libp2p-crypto"
"github.com/qri-io/cafs"
"github.com/qri-io/dataset/dsgraph"
"github.com/qri-io/doggos"
"github.com/qri-io/qri/config"
"github.com/qri-io/qri/repo"
"github.com/qri-io/qri/repo/actions"
"github.com/qri-io/qri/repo/profile"
Expand All @@ -25,7 +23,9 @@ func init() {

// Repo is a filesystem-based implementation of the Repo interface
type Repo struct {
pk crypto.PrivKey
profile *profile.Profile
pk crypto.PrivKey

store cafs.Filestore
basepath
graph map[string]*dsgraph.Node
Expand All @@ -39,22 +39,25 @@ type Repo struct {
}

// NewRepo creates a new file-based repository
func NewRepo(store cafs.Filestore, base, profileidstr string) (repo.Repo, error) {
func NewRepo(store cafs.Filestore, cfg *config.Profile, base string) (repo.Repo, error) {
if err := os.MkdirAll(base, os.ModePerm); err != nil {
return nil, err
}
bp := basepath(base)

id, err := profile.IDB58Decode(profileidstr)
p, err := cfg.DecodeProfile()
if err != nil {
return nil, err
}

if err := ensureProfile(bp, id); err != nil {
pk, err := cfg.DecodePrivateKey()
if err != nil {
return nil, err
}

r := &Repo{
profile: p,
pk: pk,

store: store,
basepath: bp,

Expand Down Expand Up @@ -98,62 +101,12 @@ func (r *Repo) Graph() (map[string]*dsgraph.Node, error) {

// Profile gives this repo's peer profile
func (r *Repo) Profile() (*profile.Profile, error) {
p := &profile.Profile{}
data, err := ioutil.ReadFile(r.filepath(FileProfile))
if err != nil {
if os.IsNotExist(err) {
return p, nil
}
log.Debug(err.Error())
return p, fmt.Errorf("error loading profile: %s", err.Error())
}

if err := json.Unmarshal(data, &p); err != nil {
log.Debug(err.Error())
return p, fmt.Errorf("error unmarshaling profile: %s", err.Error())
}

return p, nil
return r.profile, nil
}

// SetProfile updates this repo's peer profile info
func (r *Repo) SetProfile(p *profile.Profile) error {
return r.saveFile(p, FileProfile)
}

// ensureProfile makes sure a profile file is saved locally
// makes it easier to edit that file to change user data
func ensureProfile(bp basepath, id profile.ID) error {
if _, err := os.Stat(bp.filepath(FileProfile)); os.IsNotExist(err) {
return bp.saveFile(&profile.Profile{
ID: id,
Peername: doggos.DoggoNick(id.String()),
}, FileProfile)
}

p := &profile.Profile{}
data, err := ioutil.ReadFile(bp.filepath(FileProfile))
if err != nil {
if os.IsNotExist(err) {
return nil
}
log.Debug(err.Error())
return fmt.Errorf("error loading profile: %s", err.Error())
}

if err := json.Unmarshal(data, &p); err != nil {
log.Debug(err.Error())
return fmt.Errorf("error unmarshaling profile: %s", err.Error())
}

if p.ID != id {
p.ID = id
if p.Peername == "" {
p.Peername = doggos.DoggoNick(p.ID.String())
}
bp.saveFile(p, FileProfile)
}

r.profile = p
return nil
}

Expand Down
3 changes: 2 additions & 1 deletion repo/fs/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/qri-io/cafs"
"github.com/qri-io/qri/config"
"github.com/qri-io/qri/repo"
"github.com/qri-io/qri/repo/test"
)
Expand All @@ -19,7 +20,7 @@ func TestRepo(t *testing.T) {
t.Errorf("error removing files: %s", err.Error())
}

r, err := NewRepo(cafs.NewMapstore(), path, "QmZePf5LeXow3RW5U1AgEiNbW46YnRGhZ7HPvm1UmPFPwt")
r, err := NewRepo(cafs.NewMapstore(), config.Profile{}.Default(), path)
if err != nil {
t.Errorf("error creating repo: %s", err.Error())
}
Expand Down

0 comments on commit abcd937

Please sign in to comment.