Skip to content

Commit

Permalink
add in a default file hash and cleaned up init functiona bit
Browse files Browse the repository at this point in the history
  • Loading branch information
whyrusleeping committed Nov 4, 2014
1 parent 60ef8e5 commit eaa68be
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 44 deletions.
138 changes: 94 additions & 44 deletions cmd/ipfs/init.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package main

import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"os"
"path/filepath"

"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
config "github.com/jbenet/go-ipfs/config"
core "github.com/jbenet/go-ipfs/core"
ci "github.com/jbenet/go-ipfs/crypto"
imp "github.com/jbenet/go-ipfs/importer"
chunk "github.com/jbenet/go-ipfs/importer/chunk"
peer "github.com/jbenet/go-ipfs/peer"
updates "github.com/jbenet/go-ipfs/updates"
u "github.com/jbenet/go-ipfs/util"
Expand All @@ -34,6 +39,66 @@ func init() {
cmdIpfsInit.Flag.String("d", "", "Change default datastore location")
}

var defaultPeers = []*config.BootstrapPeer{
&config.BootstrapPeer{
// mars.i.ipfs.io
PeerID: "QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
Address: "/ip4/104.131.131.82/tcp/4001",
},
}

func datastoreConfig(dspath string) (config.Datastore, error) {
ds := config.Datastore{}
if len(dspath) == 0 {
var err error
dspath, err = config.DataStorePath("")
if err != nil {
return ds, err
}
}
ds.Path = dspath
ds.Type = "leveldb"

// Construct the data store if missing
if err := os.MkdirAll(dspath, os.ModePerm); err != nil {
return ds, err
}

// Check the directory is writeable
if f, err := os.Create(filepath.Join(dspath, "._check_writeable")); err == nil {
os.Remove(f.Name())
} else {
return ds, errors.New("Datastore '" + dspath + "' is not writeable")
}

return ds, nil
}

func identityConfig(nbits int) (config.Identity, error) {
ident := config.Identity{}
fmt.Println("generating key pair...")
sk, pk, err := ci.GenerateKeyPair(ci.RSA, nbits)
if err != nil {
return ident, err
}

// currently storing key unencrypted. in the future we need to encrypt it.
// TODO(security)
skbytes, err := sk.Bytes()
if err != nil {
return ident, err
}
ident.PrivKey = base64.StdEncoding.EncodeToString(skbytes)

id, err := peer.IDFromPubKey(pk)
if err != nil {
return ident, err
}
ident.PeerID = id.Pretty()

return ident, nil
}

func initCmd(c *commander.Command, inp []string) error {
configpath, err := getConfigDir(c.Parent)
if err != nil {
Expand Down Expand Up @@ -63,30 +128,12 @@ func initCmd(c *commander.Command, inp []string) error {
}
cfg := new(config.Config)

cfg.Datastore = config.Datastore{}
if len(dspath) == 0 {
dspath, err = config.DataStorePath("")
if err != nil {
return err
}
}
cfg.Datastore.Path = dspath
cfg.Datastore.Type = "leveldb"

// Construct the data store if missing
if err := os.MkdirAll(dspath, os.ModePerm); err != nil {
// setup the datastore
cfg.Datastore, err = datastoreConfig(dspath)
if err != nil {
return err
}

// Check the directory is writeable
if f, err := os.Create(filepath.Join(dspath, "._check_writeable")); err == nil {
os.Remove(f.Name())
} else {
return errors.New("Datastore '" + dspath + "' is not writeable")
}

cfg.Identity = config.Identity{}

// setup the node addresses.
cfg.Addresses = config.Addresses{
Swarm: "/ip4/0.0.0.0/tcp/4001",
Expand All @@ -107,44 +154,47 @@ func initCmd(c *commander.Command, inp []string) error {
return errors.New("Bitsize less than 1024 is considered unsafe.")
}

u.POut("generating key pair\n")
sk, pk, err := ci.GenerateKeyPair(ci.RSA, nbits)
cfg.Identity, err = identityConfig(nbits)
if err != nil {
return err
}

// currently storing key unencrypted. in the future we need to encrypt it.
// TODO(security)
skbytes, err := sk.Bytes()
if err != nil {
return err
// Use these hardcoded bootstrap peers for now.
cfg.Bootstrap = defaultPeers

// tracking ipfs version used to generate the init folder
// and adding update checker default setting.
cfg.Version = config.Version{
Check: "error",
Current: updates.Version,
}
cfg.Identity.PrivKey = base64.StdEncoding.EncodeToString(skbytes)

id, err := peer.IDFromPubKey(pk)
err = config.WriteConfigFile(filename, cfg)
if err != nil {
return err
}
cfg.Identity.PeerID = id.Pretty()

// Use these hardcoded bootstrap peers for now.
cfg.Bootstrap = []*config.BootstrapPeer{
&config.BootstrapPeer{
// mars.i.ipfs.io
PeerID: "QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
Address: "/ip4/104.131.131.82/tcp/4001",
},
nd, err := core.NewIpfsNode(cfg, false)
if err != nil {
return err
}
defer nd.Close()

// tracking ipfs version used to generate the init folder and adding update checker default setting.
cfg.Version = config.Version{
Check: "error",
Current: updates.Version,
}
// Set up default file
msg := `Hello and Welcome to IPFS!
If you're seeing this, that means that you have successfully
installed ipfs and are now interfacing with the wonderful
world of DAGs and hashes!
`
reader := bytes.NewBufferString(msg)

err = config.WriteConfigFile(filename, cfg)
defnd, err := imp.BuildDagFromReader(reader, nd.DAG, nd.Pinning.GetManual(), chunk.DefaultSplitter)
if err != nil {
return err
}

k, _ := defnd.Key()
fmt.Printf("Default file key: %s\n", k)

return nil
}
5 changes: 5 additions & 0 deletions pin/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Pinner interface {
Pin(*mdag.Node, bool) error
Unpin(util.Key, bool) error
Flush() error
GetManual() ManualPinner
}

// ManualPinner is for manually editing the pin structure
Expand Down Expand Up @@ -261,3 +262,7 @@ func (p *pinner) PinWithMode(k util.Key, mode PinMode) {
p.indirPin.Increment(k)
}
}

func (p *pinner) GetManual() ManualPinner {
return p
}

4 comments on commit eaa68be

@btc
Copy link
Contributor

@btc btc commented on eaa68be Nov 5, 2014

Choose a reason for hiding this comment

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

@whyrusleeping
@mappum
@jbenet

#263 introduces the cli library that makes use of the new commands framework. While that PR is in progress, if we're not diligent, the two versions of the CLI will drift.

In may take a couple days to get the PR merged. During that time, we should probably no longer modify commands in the old package.

init was updated in the new package yesterday, here:

https://github.com/jbenet/go-ipfs/blob/cmd-ref-part2/cmd/ipfs2/init.go

@btc
Copy link
Contributor

@btc btc commented on eaa68be Nov 5, 2014

Choose a reason for hiding this comment

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

I must say...

I prefer your decomposition to mine.

@jbenet
Copy link
Member

@jbenet jbenet commented on eaa68be Nov 5, 2014

Choose a reason for hiding this comment

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

Ack

@btc
Copy link
Contributor

@btc btc commented on eaa68be Nov 5, 2014

Choose a reason for hiding this comment

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

@whyrusleeping I will take care of adding these adjustments to the new package

Please sign in to comment.