Skip to content
This repository has been archived by the owner on Sep 6, 2022. It is now read-only.

Add proper daemonize support for minfs. #55

Merged
merged 1 commit into from
Sep 22, 2017
Merged
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
37 changes: 23 additions & 14 deletions fs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ type Config struct {
bucket string
basePath string

cache string
accountID string
accessKey string
secretKey string
target *url.URL
mountpoint string
debug bool
cache string
accountID string
accessKey string
secretKey string
secretToken string
target *url.URL
mountpoint string
debug bool

uid uint32
gid uint32
Expand All @@ -48,9 +49,10 @@ type Config struct {

// AccessConfig - access credentials and version of `config.json`.
type AccessConfig struct {
Version string `json:"version"`
AccessKey string `json:"accessKey"`
SecretKey string `json:"secretKey"`
Version string `json:"version"`
AccessKey string `json:"accessKey"`
SecretKey string `json:"secretKey"`
SecretToken string `json:"secretToken"`
}

// InitMinFSConfig - Initialize MinFS configuration file.
Expand All @@ -64,9 +66,10 @@ func InitMinFSConfig() (*AccessConfig, error) {
if os.IsNotExist(err) {
console.Println("Initializing config.json for the first time, please update your access credentials.")
ac := &AccessConfig{
Version: "1",
AccessKey: os.Getenv("MINFS_ACCESS_KEY"),
SecretKey: os.Getenv("MINFS_SECRET_KEY"),
Version: "1",
AccessKey: os.Getenv("MINFS_ACCESS_KEY"),
SecretKey: os.Getenv("MINFS_SECRET_KEY"),
SecretToken: os.Getenv("MINFS_SECRET_TOKEN"),
}
acBytes, jerr := json.Marshal(ac)
if jerr != nil {
Expand All @@ -90,10 +93,16 @@ func InitMinFSConfig() (*AccessConfig, error) {
// Override if access keys are set through env.
accessKey := os.Getenv("MINFS_ACCESS_KEY")
secretKey := os.Getenv("MINFS_SECRET_KEY")
if accessKey != "" && secretKey != "" {
secretToken := os.Getenv("MINFS_SECRET_TOKEN")
if accessKey != "" {
ac.AccessKey = accessKey
}
if secretKey != "" {
ac.SecretKey = secretKey
}
if secretToken != "" {
ac.SecretToken = secretToken
}
return ac, nil
}

Expand Down
35 changes: 20 additions & 15 deletions fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (

"github.com/minio/minfs/meta"
"github.com/minio/minio-go"
"github.com/minio/minio-go/pkg/credentials"

"bazil.org/fuse"
"bazil.org/fuse/fs"
Expand Down Expand Up @@ -170,34 +171,38 @@ func (mfs *MinFS) Serve() (err error) {
}

mfs.log.Println("Initializing minio client...")
host := mfs.config.target.Host
access := mfs.config.accessKey
secret := mfs.config.secretKey
secure := mfs.config.target.Scheme == "https"
mfs.api, err = minio.NewV4(host, access, secret, secure)

var (
host = mfs.config.target.Host
access = mfs.config.accessKey
secret = mfs.config.secretKey
token = mfs.config.secretToken
secure = mfs.config.target.Scheme == "https"
)

creds := credentials.NewStaticV4(access, secret, token)
mfs.api, err = minio.NewWithCredentials(host, creds, secure, "")
if err != nil {
return err
}

// Validate if the bucket is valid and accessible.
exists, err := mfs.api.BucketExists(mfs.config.bucket)
if err != nil {
return err
}
if !exists {
return minio.ErrorResponse{
BucketName: mfs.config.bucket,
Code: "NoSuchBucket",
Message: "The specified bucket does not exist",
mfs.log.Println("Bucket doesn't not exist... attempting to create")
if err = mfs.api.MakeBucket(mfs.config.bucket, ""); err != nil {
return err
}
}

// Set notifications
/*
mfs.log.Println("Starting monitoring server...")
if err = mfs.startNotificationListener(); err != nil {
return err
}
*/
// mfs.log.Println("Starting monitoring server...")
// if err = mfs.startNotificationListener(); err != nil {
// return err
// }

if err = mfs.startSync(); err != nil {
return err
Expand Down
58 changes: 12 additions & 46 deletions fs/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@
package minfs

import (
"fmt"
"net/url"
"os"
"path"
"time"
"strings"

"github.com/minio/minfs/meta"
minio "github.com/minio/minio-go"
)

Expand All @@ -45,28 +43,16 @@ func (mfs *MinFS) startNotificationListener() error {
if err != nil {
panic(err)
}

defer tx.Rollback()

for _, record := range notificationInfo.Records {
key, e := url.QueryUnescape(record.S3.Object.Key)
if e != nil {
fmt.Print("Error:", err)
mfs.log.Println("Error:", err)
tx.Rollback()
continue
}

dir, file := path.Split(key)

b := tx.Bucket("minio/")
if dir != "" {
v, err := b.CreateBucketIfNotExists(dir)
if err != nil {
fmt.Print("Error:", err)
continue
}
b = v
}

var d *Dir
if dir == "" {
d = &Dir{
Expand All @@ -88,35 +74,14 @@ func (mfs *MinFS) startNotificationListener() error {
}
}

var f interface{}
if err := b.Get(file, &f); err == nil {
} else if !meta.IsNoSuchObject(err) {
fmt.Println("Error:", err)
continue
} else if i, err := mfs.NextSequence(tx); err != nil {
fmt.Println("Error:", err)
continue
} else {
objMeta := record.S3.Object
lastModified := time.Now().UTC()
f = &File{
dir: d,
mfs: mfs,
Size: uint64(objMeta.Size),
Inode: i,
UID: mfs.config.uid,
GID: mfs.config.gid,
Mode: mfs.config.mode,
Path: file,
Chgtime: lastModified,
Crtime: lastModified,
Mtime: lastModified,
Atime: lastModified,
ETag: objMeta.ETag,
}

if err := f.(*File).store(tx); err != nil {
fmt.Println("Error:", err)
if strings.HasPrefix(record.EventName, "s3:ObjectCreated:") {
if err = d.storeFile(d.bucket(tx), tx, file, minio.ObjectInfo{
Key: record.S3.Object.Key,
Size: record.S3.Object.Size,
ETag: record.S3.Object.ETag,
}); err != nil {
tx.Rollback()
mfs.log.Println("Error:", err)
continue
}
}
Expand All @@ -125,6 +90,7 @@ func (mfs *MinFS) startNotificationListener() error {

// Commit the transaction and check for error.
if err := tx.Commit(); err != nil {
tx.Rollback()
panic(err)
}
case <-mfs.listenerDoneCh:
Expand Down
28 changes: 27 additions & 1 deletion minfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,34 @@

package main // import "github.com/minio/minfs"

import minfs "github.com/minio/minfs/cmd"
import (
"log"
"os"

minfs "github.com/minio/minfs/cmd"
daemon "github.com/sevlyar/go-daemon"
)

func main() {
dctx := &daemon.Context{
PidFileName: "/var/log/minfs.pid",
PidFilePerm: 0644,
LogFileName: "/var/log/minfs.log",
LogFilePerm: 0640,
WorkDir: "./",
Umask: 027,
Args: os.Args,
}

d, err := dctx.Reborn()
if err != nil {
log.Fatalln("Unable to run: ", err)
}
if d != nil {
return
}
defer dctx.Release()

// daemon business logic starts here
minfs.Main()
}
2 changes: 1 addition & 1 deletion mount.minfs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ _init ()
start_minfs ()
{
cmd_line=$(echo "$cmd_line $mount_opts $minio_endpoint $mount_point");
$cmd_line &
$cmd_line
}

print_usage ()
Expand Down