2015-01-21 02:16:06 -05:00
|
|
|
/*
|
2015-03-19 17:35:50 -04:00
|
|
|
* Minimalist Object Storage, (C) 2014 Minio, Inc.
|
2015-01-21 02:16:06 -05:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-01-18 16:31:22 -05:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2015-03-01 18:12:09 -05:00
|
|
|
"os/user"
|
2015-01-27 20:47:11 -05:00
|
|
|
"path"
|
2015-01-18 16:31:22 -05:00
|
|
|
"reflect"
|
|
|
|
|
2015-03-25 20:04:26 -04:00
|
|
|
"errors"
|
|
|
|
"github.com/minio-io/iodine"
|
2015-03-18 20:49:33 -04:00
|
|
|
"github.com/minio-io/minio/pkg/api"
|
|
|
|
"github.com/minio-io/minio/pkg/api/web"
|
2015-03-23 23:40:21 -04:00
|
|
|
"github.com/minio-io/minio/pkg/drivers"
|
|
|
|
"github.com/minio-io/minio/pkg/drivers/donut"
|
|
|
|
"github.com/minio-io/minio/pkg/drivers/file"
|
|
|
|
"github.com/minio-io/minio/pkg/drivers/memory"
|
2015-02-23 14:03:01 -05:00
|
|
|
"github.com/minio-io/minio/pkg/server/httpserver"
|
2015-03-25 20:04:26 -04:00
|
|
|
"github.com/minio-io/minio/pkg/utils/log"
|
2015-01-18 16:31:22 -05:00
|
|
|
)
|
|
|
|
|
2015-03-06 00:07:19 -05:00
|
|
|
// Config - http server parameters
|
|
|
|
type Config struct {
|
2015-02-23 05:11:27 -05:00
|
|
|
Domain string
|
2015-02-01 05:35:00 -05:00
|
|
|
Address string
|
2015-03-06 00:07:19 -05:00
|
|
|
TLS bool
|
2015-02-01 05:35:00 -05:00
|
|
|
CertFile string
|
|
|
|
KeyFile string
|
2015-03-06 00:07:19 -05:00
|
|
|
APIType interface{}
|
2015-02-01 05:35:00 -05:00
|
|
|
}
|
|
|
|
|
2015-03-23 23:40:21 -04:00
|
|
|
// MinioAPI - driver type donut, file, memory
|
2015-03-06 00:07:19 -05:00
|
|
|
type MinioAPI struct {
|
2015-03-23 23:40:21 -04:00
|
|
|
DriverType DriverType
|
2015-01-28 19:07:53 -05:00
|
|
|
}
|
|
|
|
|
2015-03-18 20:49:33 -04:00
|
|
|
// Web - web related
|
|
|
|
type Web struct {
|
2015-03-06 00:07:19 -05:00
|
|
|
Websocket bool // TODO
|
2015-02-01 05:35:00 -05:00
|
|
|
}
|
|
|
|
|
2015-03-23 23:40:21 -04:00
|
|
|
// DriverType - different driver types supported by minio
|
|
|
|
type DriverType int
|
2015-01-28 19:07:53 -05:00
|
|
|
|
2015-03-23 23:40:21 -04:00
|
|
|
// Driver types
|
2015-01-28 19:07:53 -05:00
|
|
|
const (
|
2015-03-23 23:40:21 -04:00
|
|
|
Memory DriverType = iota
|
2015-03-06 00:07:19 -05:00
|
|
|
File
|
|
|
|
Donut
|
2015-01-28 19:07:53 -05:00
|
|
|
)
|
|
|
|
|
2015-03-06 00:07:19 -05:00
|
|
|
func getHTTPChannels(configs []Config) (ctrlChans []chan<- string, statusChans []<-chan error) {
|
2015-01-28 19:29:47 -05:00
|
|
|
// a pair of control channels, we use these primarily to add to the lists above
|
2015-01-21 15:44:09 -05:00
|
|
|
var ctrlChan chan<- string
|
|
|
|
var statusChan <-chan error
|
2015-01-28 19:29:47 -05:00
|
|
|
|
2015-02-01 05:35:00 -05:00
|
|
|
for _, config := range configs {
|
2015-03-06 00:09:16 -05:00
|
|
|
switch k := config.APIType.(type) {
|
|
|
|
case MinioAPI:
|
2015-02-01 05:35:00 -05:00
|
|
|
{
|
|
|
|
// configure web server
|
2015-03-23 23:40:21 -04:00
|
|
|
var driver drivers.Driver
|
2015-03-06 00:07:19 -05:00
|
|
|
var httpConfig = httpserver.Config{}
|
2015-02-01 05:35:00 -05:00
|
|
|
httpConfig.Address = config.Address
|
|
|
|
httpConfig.Websocket = false
|
2015-03-06 00:09:16 -05:00
|
|
|
httpConfig.TLS = config.TLS
|
2015-01-25 20:15:18 -05:00
|
|
|
|
2015-02-01 05:35:00 -05:00
|
|
|
if config.CertFile != "" {
|
|
|
|
httpConfig.CertFile = config.CertFile
|
|
|
|
}
|
|
|
|
if config.KeyFile != "" {
|
|
|
|
httpConfig.KeyFile = config.KeyFile
|
|
|
|
}
|
|
|
|
|
2015-03-23 23:40:21 -04:00
|
|
|
ctrlChans, statusChans, driver = getDriverChannels(k.DriverType)
|
|
|
|
// start minio api in a web server, pass driver driver into it
|
|
|
|
ctrlChan, statusChan, _ = httpserver.Start(api.HTTPHandler(config.Domain, driver), httpConfig)
|
2015-02-01 05:35:00 -05:00
|
|
|
|
|
|
|
ctrlChans = append(ctrlChans, ctrlChan)
|
|
|
|
statusChans = append(statusChans, statusChan)
|
|
|
|
|
|
|
|
}
|
2015-03-18 20:49:33 -04:00
|
|
|
case Web:
|
2015-02-01 05:35:00 -05:00
|
|
|
{
|
2015-03-06 00:07:19 -05:00
|
|
|
var httpConfig = httpserver.Config{}
|
2015-02-01 05:35:00 -05:00
|
|
|
httpConfig.Address = config.Address
|
2015-03-06 00:09:16 -05:00
|
|
|
httpConfig.TLS = config.TLS
|
2015-02-01 05:35:00 -05:00
|
|
|
httpConfig.CertFile = config.CertFile
|
|
|
|
httpConfig.KeyFile = config.KeyFile
|
|
|
|
|
|
|
|
httpConfig.Websocket = k.Websocket
|
2015-03-18 20:49:33 -04:00
|
|
|
ctrlChan, statusChan, _ = httpserver.Start(web.HTTPHandler(), httpConfig)
|
2015-02-01 05:35:00 -05:00
|
|
|
|
|
|
|
ctrlChans = append(ctrlChans, ctrlChan)
|
|
|
|
statusChans = append(statusChans, statusChan)
|
|
|
|
}
|
|
|
|
default:
|
2015-03-25 20:04:26 -04:00
|
|
|
{
|
2015-03-26 18:55:06 -04:00
|
|
|
err := iodine.New(errors.New("Invalid API type"), nil)
|
2015-03-25 22:12:39 -04:00
|
|
|
log.Fatal(err)
|
2015-03-25 20:04:26 -04:00
|
|
|
}
|
2015-02-01 05:35:00 -05:00
|
|
|
}
|
2015-01-25 20:15:18 -05:00
|
|
|
}
|
2015-02-01 05:35:00 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-23 23:40:21 -04:00
|
|
|
func getDriverChannels(driverType DriverType) (ctrlChans []chan<- string, statusChans []<-chan error, driver drivers.Driver) {
|
2015-02-01 05:35:00 -05:00
|
|
|
// a pair of control channels, we use these primarily to add to the lists above
|
|
|
|
var ctrlChan chan<- string
|
|
|
|
var statusChan <-chan error
|
2015-01-21 15:44:09 -05:00
|
|
|
|
2015-03-23 23:40:21 -04:00
|
|
|
// instantiate driver
|
2015-01-28 19:29:47 -05:00
|
|
|
// preconditions:
|
2015-03-23 23:40:21 -04:00
|
|
|
// - driver type specified
|
|
|
|
// - any configuration for driver is populated
|
2015-01-28 19:29:47 -05:00
|
|
|
// postconditions:
|
2015-03-23 23:40:21 -04:00
|
|
|
// - driver driver is initialized
|
|
|
|
// - ctrlChans has channel to communicate to driver
|
|
|
|
// - statusChans has channel for messages coming from driver
|
2015-01-28 19:29:47 -05:00
|
|
|
switch {
|
2015-03-23 23:40:21 -04:00
|
|
|
case driverType == Memory:
|
2015-01-28 19:29:47 -05:00
|
|
|
{
|
2015-03-23 23:40:21 -04:00
|
|
|
ctrlChan, statusChan, driver = memory.Start()
|
2015-01-28 19:29:47 -05:00
|
|
|
ctrlChans = append(ctrlChans, ctrlChan)
|
|
|
|
statusChans = append(statusChans, statusChan)
|
2015-01-27 20:47:11 -05:00
|
|
|
}
|
2015-03-23 23:40:21 -04:00
|
|
|
case driverType == File:
|
2015-01-28 19:29:47 -05:00
|
|
|
{
|
2015-03-01 18:12:09 -05:00
|
|
|
u, err := user.Current()
|
|
|
|
if err != nil {
|
2015-03-28 18:58:48 -04:00
|
|
|
log.Error.Println(iodine.New(err, nil))
|
2015-03-01 18:12:09 -05:00
|
|
|
return nil, nil, nil
|
|
|
|
}
|
2015-03-23 23:06:15 -04:00
|
|
|
root := path.Join(u.HomeDir, "minio-storage", "file")
|
2015-03-23 23:40:21 -04:00
|
|
|
ctrlChan, statusChan, driver = file.Start(root)
|
2015-01-28 19:29:47 -05:00
|
|
|
ctrlChans = append(ctrlChans, ctrlChan)
|
|
|
|
statusChans = append(statusChans, statusChan)
|
2015-01-27 20:47:11 -05:00
|
|
|
}
|
2015-03-23 23:40:21 -04:00
|
|
|
case driverType == Donut:
|
2015-03-23 23:06:15 -04:00
|
|
|
{
|
|
|
|
u, err := user.Current()
|
|
|
|
if err != nil {
|
2015-03-28 18:58:48 -04:00
|
|
|
log.Error.Println(iodine.New(err, nil))
|
2015-03-23 23:06:15 -04:00
|
|
|
return nil, nil, nil
|
|
|
|
}
|
2015-03-27 15:44:09 -04:00
|
|
|
root := path.Join(u.HomeDir, "minio-storage", "donut")
|
2015-03-23 23:40:21 -04:00
|
|
|
ctrlChan, statusChan, driver = donut.Start(root)
|
2015-03-23 23:06:15 -04:00
|
|
|
ctrlChans = append(ctrlChans, ctrlChan)
|
|
|
|
statusChans = append(statusChans, statusChan)
|
|
|
|
}
|
2015-01-28 19:29:47 -05:00
|
|
|
default: // should never happen
|
2015-03-25 20:04:26 -04:00
|
|
|
{
|
2015-03-26 18:55:06 -04:00
|
|
|
log.Fatal(iodine.New(errors.New("No driver found"), nil))
|
2015-03-25 20:04:26 -04:00
|
|
|
}
|
2015-01-27 20:47:11 -05:00
|
|
|
}
|
2015-02-01 05:35:00 -05:00
|
|
|
return
|
|
|
|
}
|
2015-01-18 16:31:22 -05:00
|
|
|
|
2015-03-06 00:07:19 -05:00
|
|
|
// Start - create channels
|
|
|
|
func Start(configs []Config) {
|
2015-01-28 19:33:20 -05:00
|
|
|
// reflected looping is necessary to remove dead channels from loop and not flood switch
|
2015-03-06 00:07:19 -05:00
|
|
|
ctrlChans, statusChans := getHTTPChannels(configs)
|
2015-01-18 16:31:22 -05:00
|
|
|
cases := createSelectCases(statusChans)
|
2015-01-18 19:59:23 -05:00
|
|
|
for len(cases) > 0 {
|
2015-01-18 16:31:22 -05:00
|
|
|
chosen, value, recvOk := reflect.Select(cases)
|
2015-02-18 02:48:42 -05:00
|
|
|
switch recvOk {
|
|
|
|
case true:
|
2015-01-18 16:31:22 -05:00
|
|
|
// Status Message Received
|
2015-02-18 02:48:42 -05:00
|
|
|
switch true {
|
|
|
|
case value.Interface() != nil:
|
|
|
|
// For any error received cleanup all existing channels and fail
|
|
|
|
for _, ch := range ctrlChans {
|
|
|
|
close(ch)
|
|
|
|
}
|
|
|
|
log.Fatal(value.Interface())
|
|
|
|
}
|
|
|
|
case false:
|
2015-01-18 16:31:22 -05:00
|
|
|
// Channel closed, remove from list
|
2015-01-20 17:07:39 -05:00
|
|
|
var aliveStatusChans []<-chan error
|
2015-01-18 16:31:22 -05:00
|
|
|
for i, ch := range statusChans {
|
|
|
|
if i != chosen {
|
|
|
|
aliveStatusChans = append(aliveStatusChans, ch)
|
|
|
|
}
|
|
|
|
}
|
2015-01-18 18:50:53 -05:00
|
|
|
// create new select cases without defunct channel
|
2015-01-18 16:31:22 -05:00
|
|
|
statusChans = aliveStatusChans
|
|
|
|
cases = createSelectCases(statusChans)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-28 19:33:20 -05:00
|
|
|
// creates select cases for reflect to switch over dynamically
|
|
|
|
// this is necessary in order to remove dead channels and not flood
|
|
|
|
// the loop with closed channel errors
|
2015-01-18 16:31:22 -05:00
|
|
|
func createSelectCases(channels []<-chan error) []reflect.SelectCase {
|
|
|
|
cases := make([]reflect.SelectCase, len(channels))
|
|
|
|
for i, ch := range channels {
|
|
|
|
cases[i] = reflect.SelectCase{
|
|
|
|
Dir: reflect.SelectRecv,
|
|
|
|
Chan: reflect.ValueOf(ch),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cases
|
|
|
|
}
|