Example #1
0
// addAlias - add new aliases
func addAlias(aliases []string) (quick.Config, error) {
	if len(aliases) < 2 {
		return nil, NewIodine(iodine.New(errInvalidArgument{}, nil))
	}
	conf := newConfigV1()
	config, err := quick.New(conf)
	if err != nil {
		return nil, NewIodine(iodine.New(err, nil))
	}
	config.Load(mustGetMcConfigPath())

	aliasName := aliases[0]
	url := strings.TrimSuffix(aliases[1], "/")
	if strings.HasPrefix(aliasName, "http") {
		return nil, NewIodine(iodine.New(errInvalidAliasName{name: aliasName}, nil))
	}
	if !strings.HasPrefix(url, "http") {
		return nil, NewIodine(iodine.New(errInvalidURL{URL: url}, nil))
	}
	if !isValidAliasName(aliasName) {
		return nil, NewIodine(iodine.New(errInvalidAliasName{name: aliasName}, nil))
	}
	// convert interface{} back to its original struct
	newConf := config.Data().(*configV1)
	if _, ok := newConf.Aliases[aliasName]; ok {
		return nil, NewIodine(iodine.New(errAliasExists{}, nil))
	}
	newConf.Aliases[aliasName] = url
	newConfig, err := quick.New(newConf)
	if err != nil {
		return nil, NewIodine(iodine.New(err, nil))
	}
	return newConfig, nil
}
Example #2
0
func migrateConfigV1ToV101() {
	if !isMcConfigExists() {
		return
	}
	conf := newConfigV1()
	config, err := quick.New(conf)
	if err != nil {
		console.Fatalln(NewIodine(iodine.New(err, nil)))
	}
	config.Load(mustGetMcConfigPath())
	conf = config.Data().(*configV1)
	// version is the same return
	if conf.Version == mcCurrentConfigVersion {
		return
	}
	conf.Version = mcCurrentConfigVersion

	localHostConfig := new(hostConfig)
	localHostConfig.AccessKeyID = ""
	localHostConfig.SecretAccessKey = ""

	if _, ok := conf.Hosts["localhost:*"]; !ok {
		conf.Hosts["localhost:*"] = localHostConfig
	}
	if _, ok := conf.Hosts["127.0.0.1:*"]; !ok {
		conf.Hosts["127.0.0.1:*"] = localHostConfig
	}

	newConfig, err := quick.New(conf)
	if err := newConfig.Save(mustGetMcConfigPath()); err != nil {
		console.Fatalln(NewIodine(iodine.New(err, nil)))
	}
	console.Infof("Successfully migrated %s from version: %s to version: %s\n", mustGetMcConfigPath(), mcPreviousConfigVersion, mcCurrentConfigVersion)
}
Example #3
0
// getMcConfig - reads configuration file and returns config
func getMcConfig() (*configV1, error) {
	if !isMcConfigExists() {
		return nil, iodine.New(errInvalidArgument{}, nil)
	}

	configFile, err := getMcConfigPath()
	if err != nil {
		return nil, iodine.New(err, nil)
	}

	// Cached in private global variable.
	if v := cache.Get(); v != nil { // Use previously cached config.
		return v.(quick.Config).Data().(*configV1), nil
	}

	conf := newConfigV1()
	qconf, err := quick.New(conf)
	if err != nil {
		return nil, iodine.New(err, nil)
	}

	err = qconf.Load(configFile)
	if err != nil {
		return nil, iodine.New(err, nil)
	}
	cache.Put(qconf)
	return qconf.Data().(*configV1), nil

}
Example #4
0
// loadSession - reads session file if exists and re-initiates internal variables
func loadSession(sid string) (*sessionV1, error) {
	if !isSessionDirExists() {
		return nil, iodine.New(errInvalidArgument{}, nil)
	}
	sessionFile, err := getSessionFile(sid)
	if err != nil {
		return nil, iodine.New(err, nil)
	}
	_, err = os.Stat(sessionFile)
	if err != nil {
		return nil, iodine.New(err, nil)
	}
	s := new(sessionV1)
	s.Version = mcCurrentSessionVersion
	// map of command and files copied
	s.URLs = nil
	s.Lock = new(sync.Mutex)
	s.Files = make(map[string]struct{})
	qs, err := quick.New(s)
	if err != nil {
		return nil, iodine.New(err, nil)
	}
	err = qs.Load(sessionFile)
	if err != nil {
		return nil, iodine.New(err, nil)
	}
	return qs.Data().(*sessionV1), nil

}
Example #5
0
// loadSession - reads session file if exists and re-initiates internal variables
func loadSessionV2(sid string) (*sessionV2, error) {
	if !isSessionDirExists() {
		return nil, NewIodine(iodine.New(errInvalidArgument{}, nil))
	}
	sessionFile := getSessionFile(sid)

	_, err := os.Stat(sessionFile)
	if err != nil {
		return nil, NewIodine(iodine.New(err, nil))
	}

	s := &sessionV2{}
	s.Header = &sessionV2Header{}
	s.SessionID = sid
	s.Header.Version = "1.1.0"
	qs, err := quick.New(s.Header)
	if err != nil {
		return nil, NewIodine(iodine.New(err, nil))
	}
	err = qs.Load(sessionFile)
	if err != nil {
		return nil, NewIodine(iodine.New(err, nil))
	}

	s.mutex = new(sync.Mutex)
	s.Header = qs.Data().(*sessionV2Header)

	s.DataFP, err = os.Open(getSessionDataFile(s.SessionID))
	if err != nil {
		console.Fatalf("Unable to open session data file \""+getSessionDataFile(s.SessionID)+"\". %s", NewIodine(iodine.New(errNotConfigured{}, nil)))
	}

	return s, nil
}
Example #6
0
// loadSession - reads session file if exists and re-initiates internal variables
func loadSessionV1(sid string) (*sessionV1, error) {
	if !isSessionDirExists() {
		return nil, NewIodine(iodine.New(errInvalidArgument{}, nil))
	}
	sessionFile := getSessionFileV1(sid)

	_, err := os.Stat(sessionFile)
	if err != nil {
		return nil, NewIodine(iodine.New(err, nil))
	}
	s := new(sessionV1)
	s.Version = "1.0.0"
	// map of command and files copied
	s.URLs = nil
	s.Lock = new(sync.Mutex)
	s.Files = make(map[string]bool)
	qs, err := quick.New(s)
	if err != nil {
		return nil, NewIodine(iodine.New(err, nil))
	}
	err = qs.Load(sessionFile)
	if err != nil {
		return nil, NewIodine(iodine.New(err, nil))
	}
	return qs.Data().(*sessionV1), nil
}
Example #7
0
// newConfig - get new config interface
func newConfig() (config quick.Config, err error) {
	conf := newConfigV101()
	config, err = quick.New(conf)
	if err != nil {
		return nil, NewIodine(iodine.New(err, nil))
	}
	return config, nil
}
Example #8
0
func (s *CmdTestSuite) TestNewConfigV1(c *C) {
	root, err := ioutil.TempDir(os.TempDir(), "mc-")
	c.Assert(err, IsNil)
	defer os.RemoveAll(root)

	conf, err := newConfig()
	c.Assert(err, IsNil)
	configFile := filepath.Join(root, "config.json")
	err = conf.Save(configFile)
	c.Assert(err, IsNil)

	confNew := newConfigV101()
	config, err := quick.New(confNew)
	c.Assert(err, IsNil)
	err = config.Load(configFile)
	c.Assert(err, IsNil)
	data := config.Data().(*configV1)

	type aliases struct {
		name string
		url  string
	}

	wantAliases := []aliases{
		{
			"s3",
			"https://s3.amazonaws.com",
		},
		{
			"play",
			"https://play.minio.io:9000",
		},
		{
			"localhost",
			"http://localhost:9000",
		},
	}
	for _, alias := range wantAliases {
		url, ok := data.Aliases[alias.name]
		c.Assert(ok, Equals, true)
		c.Assert(url, Equals, alias.url)
	}

	wantHosts := []string{
		"localhost:*",
		"127.0.0.1:*",
		"play.minio.io:9000",
		"dl.minio.io:9000",
		"s3*.amazonaws.com",
	}
	for _, host := range wantHosts {
		_, ok := data.Hosts[host]
		c.Assert(ok, Equals, true)
	}
}
Example #9
0
func newSessionV1() (config quick.Config, err error) {
	s := new(sessionV1)
	s.Version = mcCurrentSessionVersion
	// map of command and files copied
	s.URLs = nil
	s.Started = time.Now().UTC()
	s.Files = make(map[string]struct{})
	s.Lock = new(sync.Mutex)
	s.SessionID = newSID(8)
	return quick.New(s)
}
Example #10
0
// save a session
func saveSession(s *sessionV1) error {
	sessionFile, err := getSessionFile(s.SessionID)
	if err != nil {
		return err
	}
	qs, err := quick.New(s)
	if err != nil {
		return err
	}
	return qs.Save(sessionFile)
}
Example #11
0
// save this session
func (s *sessionV2) Save() error {
	s.mutex.Lock()
	defer s.mutex.Unlock()

	err := s.DataFP.Sync()
	if err != nil {
		return NewIodine(iodine.New(err, nil))
	}

	qs, err := quick.New(s.Header)
	if err != nil {
		return NewIodine(iodine.New(err, nil))
	}

	return qs.Save(getSessionFile(s.SessionID))
}
Example #12
0
// newConfig - get new config interface
func newConfig() (config quick.Config, err error) {
	conf := newConfigV1()
	s3HostConf := new(hostConfig)
	s3HostConf.AccessKeyID = globalAccessKeyID
	s3HostConf.SecretAccessKey = globalSecretAccessKey

	playHostConfig := new(hostConfig)
	playHostConfig.AccessKeyID = ""
	playHostConfig.SecretAccessKey = ""

	dlHostConfig := new(hostConfig)
	dlHostConfig.AccessKeyID = ""
	dlHostConfig.SecretAccessKey = ""

	// Your example host config
	exampleHostConf := new(hostConfig)
	exampleHostConf.AccessKeyID = globalAccessKeyID
	exampleHostConf.SecretAccessKey = globalSecretAccessKey

	conf.Hosts[exampleHostURL] = exampleHostConf
	conf.Hosts["s3*.amazonaws.com"] = s3HostConf
	conf.Hosts["play.minio.io:9000"] = playHostConfig
	conf.Hosts["dl.minio.io:9000"] = dlHostConfig

	aliases := make(map[string]string)
	aliases["s3"] = "https://s3.amazonaws.com"
	aliases["play"] = "https://play.minio.io:9000"
	aliases["dl"] = "https://dl.minio.io:9000"
	aliases["localhost"] = "http://localhost:9000"
	conf.Aliases = aliases
	config, err = quick.New(conf)
	if err != nil {
		return nil, iodine.New(err, nil)
	}

	return config, nil
}