2017-04-12 00:44:27 +02:00
/ *
* Minio Cloud Storage , ( C ) 2017 Minio , Inc .
*
* 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 .
* /
package cmd
import (
"fmt"
"net"
"net/url"
"path"
2017-08-16 00:10:50 +02:00
"path/filepath"
2017-04-12 00:44:27 +02:00
"sort"
"strconv"
"strings"
"github.com/minio/minio-go/pkg/set"
2017-08-16 00:10:50 +02:00
"github.com/minio/minio/pkg/mountinfo"
2017-04-12 00:44:27 +02:00
)
// EndpointType - enum for endpoint type.
type EndpointType int
const (
// PathEndpointType - path style endpoint type enum.
PathEndpointType EndpointType = iota + 1
// URLEndpointType - URL style endpoint type enum.
URLEndpointType
)
// Endpoint - any type of endpoint.
type Endpoint struct {
* url . URL
IsLocal bool
}
func ( endpoint Endpoint ) String ( ) string {
if endpoint . Host == "" {
return endpoint . Path
}
return endpoint . URL . String ( )
}
// Type - returns type of endpoint.
func ( endpoint Endpoint ) Type ( ) EndpointType {
if endpoint . Host == "" {
return PathEndpointType
}
return URLEndpointType
}
// SetHTTPS - sets secure http for URLEndpointType.
func ( endpoint Endpoint ) SetHTTPS ( ) {
if endpoint . Host != "" {
endpoint . Scheme = "https"
}
}
// SetHTTP - sets insecure http for URLEndpointType.
func ( endpoint Endpoint ) SetHTTP ( ) {
if endpoint . Host != "" {
endpoint . Scheme = "http"
}
}
// NewEndpoint - returns new endpoint based on given arguments.
2017-06-22 04:53:09 +02:00
func NewEndpoint ( arg string ) ( ep Endpoint , e error ) {
2017-04-12 00:44:27 +02:00
// isEmptyPath - check whether given path is not empty.
isEmptyPath := func ( path string ) bool {
2017-06-09 03:58:51 +02:00
return path == "" || path == "/" || path == ` \ `
2017-04-12 00:44:27 +02:00
}
if isEmptyPath ( arg ) {
2017-06-22 04:53:09 +02:00
return ep , fmt . Errorf ( "empty or root endpoint is not supported" )
2017-04-12 00:44:27 +02:00
}
var isLocal bool
u , err := url . Parse ( arg )
if err == nil && u . Host != "" {
// URL style of endpoint.
// Valid URL style endpoint is
// - Scheme field must contain "http" or "https"
// - All field should be empty except Host and Path.
if ! ( ( u . Scheme == "http" || u . Scheme == "https" ) &&
u . User == nil && u . Opaque == "" && u . ForceQuery == false && u . RawQuery == "" && u . Fragment == "" ) {
2017-06-22 04:53:09 +02:00
return ep , fmt . Errorf ( "invalid URL endpoint format" )
2017-04-12 00:44:27 +02:00
}
2017-08-16 00:10:50 +02:00
var host , port string
host , port , err = net . SplitHostPort ( u . Host )
2017-04-12 00:44:27 +02:00
if err != nil {
if ! strings . Contains ( err . Error ( ) , "missing port in address" ) {
2017-06-22 04:53:09 +02:00
return ep , fmt . Errorf ( "invalid URL endpoint format: %s" , err )
2017-04-12 00:44:27 +02:00
}
host = u . Host
} else {
var p int
p , err = strconv . Atoi ( port )
if err != nil {
2017-06-22 04:53:09 +02:00
return ep , fmt . Errorf ( "invalid URL endpoint format: invalid port number" )
2017-04-12 00:44:27 +02:00
} else if p < 1 || p > 65535 {
2017-06-22 04:53:09 +02:00
return ep , fmt . Errorf ( "invalid URL endpoint format: port number must be between 1 to 65535" )
2017-04-12 00:44:27 +02:00
}
}
if host == "" {
2017-06-22 04:53:09 +02:00
return ep , fmt . Errorf ( "invalid URL endpoint format: empty host name" )
2017-04-12 00:44:27 +02:00
}
// As this is path in the URL, we should use path package, not filepath package.
// On MS Windows, filepath.Clean() converts into Windows path style ie `/foo` becomes `\foo`
u . Path = path . Clean ( u . Path )
if isEmptyPath ( u . Path ) {
2017-06-22 04:53:09 +02:00
return ep , fmt . Errorf ( "empty or root path is not supported in URL endpoint" )
2017-04-12 00:44:27 +02:00
}
2017-05-17 06:13:29 +02:00
isLocal , err = isLocalHost ( host )
2017-04-12 00:44:27 +02:00
if err != nil {
2017-06-22 04:53:09 +02:00
return ep , err
2017-04-12 00:44:27 +02:00
}
} else {
2017-08-11 01:54:19 +02:00
// Only check if the arg is an ip address and ask for scheme since its absent.
// localhost, example.com, any FQDN cannot be disambiguated from a regular file path such as
// /mnt/export1. So we go ahead and start the minio server in FS modes in these cases.
if isHostIPv4 ( arg ) {
return ep , fmt . Errorf ( "invalid URL endpoint format: missing scheme http or https" )
}
2017-04-12 00:44:27 +02:00
u = & url . URL { Path : path . Clean ( arg ) }
isLocal = true
}
return Endpoint {
URL : u ,
IsLocal : isLocal ,
} , nil
}
// EndpointList - list of same type of endpoint.
type EndpointList [ ] Endpoint
// Swap - helper method for sorting.
func ( endpoints EndpointList ) Swap ( i , j int ) {
endpoints [ i ] , endpoints [ j ] = endpoints [ j ] , endpoints [ i ]
}
// Len - helper method for sorting.
func ( endpoints EndpointList ) Len ( ) int {
return len ( endpoints )
}
// Less - helper method for sorting.
func ( endpoints EndpointList ) Less ( i , j int ) bool {
return endpoints [ i ] . String ( ) < endpoints [ j ] . String ( )
}
// SetHTTPS - sets secure http for URLEndpointType.
func ( endpoints EndpointList ) SetHTTPS ( ) {
for i := range endpoints {
endpoints [ i ] . SetHTTPS ( )
}
}
// SetHTTP - sets insecure http for URLEndpointType.
func ( endpoints EndpointList ) SetHTTP ( ) {
for i := range endpoints {
endpoints [ i ] . SetHTTP ( )
}
}
// NewEndpointList - returns new endpoint list based on input args.
func NewEndpointList ( args ... string ) ( endpoints EndpointList , err error ) {
// isValidDistribution - checks whether given count is a valid distribution for erasure coding.
isValidDistribution := func ( count int ) bool {
2017-04-24 19:00:33 +02:00
return ( count >= minErasureBlocks && count <= maxErasureBlocks && count % 2 == 0 )
2017-04-12 00:44:27 +02:00
}
// Check whether no. of args are valid for XL distribution.
if ! isValidDistribution ( len ( args ) ) {
2017-04-24 19:00:33 +02:00
return nil , fmt . Errorf ( "A total of %d endpoints were found. For erasure mode it should be an even number between %d and %d" , len ( args ) , minErasureBlocks , maxErasureBlocks )
2017-04-12 00:44:27 +02:00
}
var endpointType EndpointType
var scheme string
uniqueArgs := set . NewStringSet ( )
// Loop through args and adds to endpoint list.
for i , arg := range args {
endpoint , err := NewEndpoint ( arg )
if err != nil {
return nil , fmt . Errorf ( "'%s': %s" , arg , err . Error ( ) )
}
// All endpoints have to be same type and scheme if applicable.
if i == 0 {
endpointType = endpoint . Type ( )
scheme = endpoint . Scheme
} else if endpoint . Type ( ) != endpointType {
return nil , fmt . Errorf ( "mixed style endpoints are not supported" )
} else if endpoint . Scheme != scheme {
return nil , fmt . Errorf ( "mixed scheme is not supported" )
}
arg = endpoint . String ( )
if uniqueArgs . Contains ( arg ) {
return nil , fmt . Errorf ( "duplicate endpoints found" )
}
uniqueArgs . Add ( arg )
endpoints = append ( endpoints , endpoint )
}
sort . Sort ( endpoints )
return endpoints , nil
}
2017-08-16 00:10:50 +02:00
// Checks if there are any cross device mounts.
func checkCrossDeviceMounts ( endpoints EndpointList ) ( err error ) {
var absPaths [ ] string
for _ , endpoint := range endpoints {
if endpoint . IsLocal {
var absPath string
absPath , err = filepath . Abs ( endpoint . Path )
if err != nil {
return err
}
absPaths = append ( absPaths , absPath )
}
}
return mountinfo . CheckCrossDevice ( absPaths )
}
2017-04-12 00:44:27 +02:00
// CreateEndpoints - validates and creates new endpoints for given args.
func CreateEndpoints ( serverAddr string , args ... string ) ( string , EndpointList , SetupType , error ) {
var endpoints EndpointList
var setupType SetupType
var err error
// Check whether serverAddr is valid for this host.
if err = CheckLocalServerAddr ( serverAddr ) ; err != nil {
return serverAddr , endpoints , setupType , err
}
_ , serverAddrPort := mustSplitHostPort ( serverAddr )
// For single arg, return FS setup.
if len ( args ) == 1 {
var endpoint Endpoint
endpoint , err = NewEndpoint ( args [ 0 ] )
if err != nil {
return serverAddr , endpoints , setupType , err
}
if endpoint . Type ( ) != PathEndpointType {
return serverAddr , endpoints , setupType , fmt . Errorf ( "use path style endpoint for FS setup" )
}
endpoints = append ( endpoints , endpoint )
setupType = FSSetupType
2017-08-16 00:10:50 +02:00
// Check for cross device mounts if any.
if err = checkCrossDeviceMounts ( endpoints ) ; err != nil {
return serverAddr , endpoints , setupType , err
}
2017-04-12 00:44:27 +02:00
return serverAddr , endpoints , setupType , nil
}
// Convert args to endpoints
if endpoints , err = NewEndpointList ( args ... ) ; err != nil {
return serverAddr , endpoints , setupType , err
}
2017-08-16 00:10:50 +02:00
// Check for cross device mounts if any.
if err = checkCrossDeviceMounts ( endpoints ) ; err != nil {
return serverAddr , endpoints , setupType , err
}
2017-04-12 00:44:27 +02:00
// Return XL setup when all endpoints are path style.
if endpoints [ 0 ] . Type ( ) == PathEndpointType {
setupType = XLSetupType
return serverAddr , endpoints , setupType , nil
}
// Here all endpoints are URL style.
endpointPathSet := set . NewStringSet ( )
localEndpointCount := 0
localServerAddrSet := set . NewStringSet ( )
localPortSet := set . NewStringSet ( )
2017-08-11 01:54:19 +02:00
2017-04-12 00:44:27 +02:00
for _ , endpoint := range endpoints {
endpointPathSet . Add ( endpoint . Path )
if endpoint . IsLocal {
localServerAddrSet . Add ( endpoint . Host )
var port string
_ , port , err = net . SplitHostPort ( endpoint . Host )
if err != nil {
port = serverAddrPort
}
localPortSet . Add ( port )
localEndpointCount ++
}
}
// No local endpoint found.
if localEndpointCount == 0 {
return serverAddr , endpoints , setupType , fmt . Errorf ( "no endpoint found for this host" )
}
// Check whether same path is not used in endpoints of a host.
{
pathIPMap := make ( map [ string ] set . StringSet )
for _ , endpoint := range endpoints {
var host string
host , _ , err = net . SplitHostPort ( endpoint . Host )
if err != nil {
host = endpoint . Host
}
hostIPSet , _ := getHostIP4 ( host )
if IPSet , ok := pathIPMap [ endpoint . Path ] ; ok {
if ! IPSet . Intersection ( hostIPSet ) . IsEmpty ( ) {
2017-04-13 05:27:24 +02:00
err = fmt . Errorf ( "path '%s' can not be served by different port on same address" , endpoint . Path )
2017-04-12 00:44:27 +02:00
return serverAddr , endpoints , setupType , err
}
2017-04-13 05:27:24 +02:00
pathIPMap [ endpoint . Path ] = IPSet . Union ( hostIPSet )
2017-04-12 00:44:27 +02:00
} else {
pathIPMap [ endpoint . Path ] = hostIPSet
}
}
}
// Check whether serverAddrPort matches at least in one of port used in local endpoints.
{
if ! localPortSet . Contains ( serverAddrPort ) {
if len ( localPortSet ) > 1 {
err = fmt . Errorf ( "port number in server address must match with one of the port in local endpoints" )
} else {
err = fmt . Errorf ( "server address and local endpoint have different ports" )
}
return serverAddr , endpoints , setupType , err
}
}
2017-04-13 05:27:24 +02:00
// All endpoints are pointing to local host
if len ( endpoints ) == localEndpointCount {
// If all endpoints have same port number, then this is XL setup using URL style endpoints.
if len ( localPortSet ) == 1 {
if len ( localServerAddrSet ) > 1 {
// TODO: Eventhough all endpoints are local, the local host is referred by different IP/name.
// eg '172.0.0.1', 'localhost' and 'mylocalhostname' point to same local host.
//
// In this case, we bind to 0.0.0.0 ie to all interfaces.
// The actual way to do is bind to only IPs in uniqueLocalHosts.
serverAddr = net . JoinHostPort ( "" , serverAddrPort )
}
endpointPaths := endpointPathSet . ToSlice ( )
endpoints , _ = NewEndpointList ( endpointPaths ... )
setupType = XLSetupType
return serverAddr , endpoints , setupType , nil
2017-04-12 00:44:27 +02:00
}
2017-04-13 05:27:24 +02:00
// Eventhough all endpoints are local, but those endpoints use different ports.
// This means it is DistXL setup.
} else {
// This is DistXL setup.
// Check whether local server address are not 127.x.x.x
for _ , localServerAddr := range localServerAddrSet . ToSlice ( ) {
host , _ , err := net . SplitHostPort ( localServerAddr )
if err != nil {
host = localServerAddr
}
ipList , err := getHostIP4 ( host )
fatalIf ( err , "unexpected error when resolving host '%s'" , host )
// Filter ipList by IPs those start with '127.'.
loopBackIPs := ipList . FuncMatch ( func ( ip string , matchString string ) bool {
return strings . HasPrefix ( ip , "127." )
} , "" )
// If loop back IP is found and ipList contains only loop back IPs, then error out.
if len ( loopBackIPs ) > 0 && len ( loopBackIPs ) == len ( ipList ) {
err = fmt . Errorf ( "'%s' resolves to loopback address is not allowed for distributed XL" , localServerAddr )
return serverAddr , endpoints , setupType , err
}
}
2017-04-12 00:44:27 +02:00
}
// Add missing port in all endpoints.
for i := range endpoints {
_ , port , err := net . SplitHostPort ( endpoints [ i ] . Host )
if err != nil {
endpoints [ i ] . Host = net . JoinHostPort ( endpoints [ i ] . Host , serverAddrPort )
} else if endpoints [ i ] . IsLocal && serverAddrPort != port {
// If endpoint is local, but port is different than serverAddrPort, then make it as remote.
endpoints [ i ] . IsLocal = false
}
}
setupType = DistXLSetupType
return serverAddr , endpoints , setupType , nil
}
2017-10-09 05:23:42 +02:00
// GetLocalPeer - returns local peer value, returns globalMinioAddr
// for FS and Erasure mode. In case of distributed server return
// the first element from the set of peers which indicate that
// they are local. There is always one entry that is local
// even with repeated server endpoints.
func GetLocalPeer ( endpoints EndpointList ) ( localPeer string ) {
peerSet := set . NewStringSet ( )
for _ , endpoint := range endpoints {
if endpoint . Type ( ) != URLEndpointType {
continue
}
if endpoint . IsLocal && endpoint . Host != "" {
peerSet . Add ( endpoint . Host )
}
}
if peerSet . IsEmpty ( ) {
// If local peer is empty can happen in FS or Erasure coded mode.
// then set the value to globalMinioAddr instead.
return globalMinioAddr
}
return peerSet . ToSlice ( ) [ 0 ]
}
2017-04-12 00:44:27 +02:00
// GetRemotePeers - get hosts information other than this minio service.
func GetRemotePeers ( endpoints EndpointList ) [ ] string {
peerSet := set . NewStringSet ( )
for _ , endpoint := range endpoints {
if endpoint . Type ( ) != URLEndpointType {
continue
}
peer := endpoint . Host
if endpoint . IsLocal {
if _ , port := mustSplitHostPort ( peer ) ; port == globalMinioPort {
continue
}
}
peerSet . Add ( peer )
}
return peerSet . ToSlice ( )
}