aboutsummaryrefslogtreecommitdiff
path: root/weed/shell/command_fs_configure.go
blob: d97f8cc15637e96786de33c645f40d26b05a9627 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package shell

import (
	"bytes"
	"flag"
	"fmt"
	"io"
	"strings"

	"github.com/chrislusf/seaweedfs/weed/filer"
	"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
	"github.com/chrislusf/seaweedfs/weed/storage/super_block"
)

func init() {
	Commands = append(Commands, &commandFsConfigure{})
}

type commandFsConfigure struct {
}

func (c *commandFsConfigure) Name() string {
	return "fs.configure"
}

func (c *commandFsConfigure) Help() string {
	return `configure and apply storage options for each location

	# see the current configuration file content
	fs.configure

	# trying the changes and see the possible configuration file content
	fs.configure -locationPrfix=/my/folder -collection=abc
	fs.configure -locationPrfix=/my/folder -collection=abc -ttl=7d

	# example: configure adding only 1 physical volume for each bucket collection
	fs.configure -locationPrfix=/buckets/ -volumeGrowthCount=1

	# apply the changes
	fs.configure -locationPrfix=/my/folder -collection=abc -apply

	# delete the changes
	fs.configure -locationPrfix=/my/folder -delete -apply

`
}

func (c *commandFsConfigure) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {

	fsConfigureCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
	locationPrefix := fsConfigureCommand.String("locationPrefix", "", "path prefix, required to update the path-specific configuration")
	collection := fsConfigureCommand.String("collection", "", "assign writes to this collection")
	replication := fsConfigureCommand.String("replication", "", "assign writes with this replication")
	ttl := fsConfigureCommand.String("ttl", "", "assign writes with this ttl")
	diskType := fsConfigureCommand.String("diskType", "", "[hdd|ssd] choose between hard drive or solid state drive")
	fsync := fsConfigureCommand.Bool("fsync", false, "fsync for the writes")
	volumeGrowthCount := fsConfigureCommand.Int("volumeGrowthCount", 0, "the number of physical volumes to add if no writable volumes")
	isDelete := fsConfigureCommand.Bool("delete", false, "delete the configuration by locationPrefix")
	apply := fsConfigureCommand.Bool("apply", false, "update and apply filer configuration")
	if err = fsConfigureCommand.Parse(args); err != nil {
		return nil
	}

	var buf bytes.Buffer
	if err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
		return filer.ReadEntry(commandEnv.MasterClient, client, filer.DirectoryEtcSeaweedFS, filer.FilerConfName, &buf)
	}); err != nil && err != filer_pb.ErrNotFound {
		return err
	}

	fc := filer.NewFilerConf()
	if buf.Len() > 0 {
		if err = fc.LoadFromBytes(buf.Bytes()); err != nil {
			return err
		}
	}

	if *locationPrefix != "" {
		locConf := &filer_pb.FilerConf_PathConf{
			LocationPrefix:    *locationPrefix,
			Collection:        *collection,
			Replication:       *replication,
			Ttl:               *ttl,
			Fsync:             *fsync,
			DiskType:          *diskType,
			VolumeGrowthCount: uint32(*volumeGrowthCount),
		}

		// check collection
		if *collection != "" && strings.HasPrefix(*locationPrefix, "/buckets/") {
			return fmt.Errorf("one s3 bucket goes to one collection and not customizable.")
		}

		// check replication
		if *replication != "" {
			rp, err := super_block.NewReplicaPlacementFromString(*replication)
			if err != nil {
				return fmt.Errorf("parse replication %s: %v", *replication, err)
			}
			if *volumeGrowthCount%rp.GetCopyCount() != 0 {
				return fmt.Errorf("volumeGrowthCount %d should be devided by replication copy count %d", *volumeGrowthCount, rp.GetCopyCount())
			}
		}

		// save it
		if *isDelete {
			fc.DeleteLocationConf(*locationPrefix)
		} else {
			fc.AddLocationConf(locConf)
		}
	}

	buf.Reset()
	fc.ToText(&buf)

	fmt.Fprintf(writer, string(buf.Bytes()))
	fmt.Fprintln(writer)

	if *apply {

		if err := filer.SaveAs(commandEnv.option.FilerHost, int(commandEnv.option.FilerPort), filer.DirectoryEtcSeaweedFS, filer.FilerConfName, "text/plain; charset=utf-8", &buf); err != nil {
			return err
		}

	}

	return nil

}