| 
									
										
										
										
											2016-12-16 14:26:15 +08:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2017-03-19 02:28:41 +08:00
										 |  |  |  * Minio Cloud Storage, (C) 2016, 2017 Minio, Inc. | 
					
						
							| 
									
										
										
										
											2016-12-16 14:26:15 +08: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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package cmd | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2017-02-21 04:58:50 +08:00
										 |  |  | 	"encoding/json" | 
					
						
							| 
									
										
										
										
											2017-01-23 16:32:55 +08:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2017-03-03 06:21:30 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2017-02-28 03:40:27 +08:00
										 |  |  | 	"io/ioutil" | 
					
						
							| 
									
										
										
										
											2016-12-16 14:26:15 +08:00
										 |  |  | 	"net/rpc" | 
					
						
							| 
									
										
										
										
											2017-02-28 03:40:27 +08:00
										 |  |  | 	"os" | 
					
						
							|  |  |  | 	"path/filepath" | 
					
						
							| 
									
										
										
										
											2017-01-04 15:39:22 +08:00
										 |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2016-12-16 14:26:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	router "github.com/gorilla/mux" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 15:39:22 +08:00
										 |  |  | const adminPath = "/admin" | 
					
						
							| 
									
										
										
										
											2016-12-16 14:26:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-23 16:32:55 +08:00
										 |  |  | var errUnsupportedBackend = errors.New("not supported for non erasure-code backend") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 15:39:22 +08:00
										 |  |  | // adminCmd - exports RPC methods for service status, stop and
 | 
					
						
							| 
									
										
										
										
											2016-12-16 14:26:15 +08:00
										 |  |  | // restart commands.
 | 
					
						
							| 
									
										
										
										
											2017-01-04 15:39:22 +08:00
										 |  |  | type adminCmd struct { | 
					
						
							| 
									
										
										
										
											2016-12-23 23:12:19 +08:00
										 |  |  | 	AuthRPCServer | 
					
						
							| 
									
										
										
										
											2016-12-16 14:26:15 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 15:39:22 +08:00
										 |  |  | // ListLocksQuery - wraps ListLocks API's query values to send over RPC.
 | 
					
						
							|  |  |  | type ListLocksQuery struct { | 
					
						
							|  |  |  | 	AuthRPCArgs | 
					
						
							| 
									
										
										
										
											2017-02-02 03:17:30 +08:00
										 |  |  | 	bucket   string | 
					
						
							|  |  |  | 	prefix   string | 
					
						
							|  |  |  | 	duration time.Duration | 
					
						
							| 
									
										
										
										
											2017-01-04 15:39:22 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ListLocksReply - wraps ListLocks response over RPC.
 | 
					
						
							|  |  |  | type ListLocksReply struct { | 
					
						
							|  |  |  | 	AuthRPCReply | 
					
						
							|  |  |  | 	volLocks []VolumeLockInfo | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-08 16:13:02 +08:00
										 |  |  | // UptimeReply - wraps the uptime response over RPC.
 | 
					
						
							|  |  |  | type UptimeReply struct { | 
					
						
							|  |  |  | 	AuthRPCReply | 
					
						
							|  |  |  | 	Uptime time.Duration | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 04:58:50 +08:00
										 |  |  | // ConfigReply - wraps the server config response over RPC.
 | 
					
						
							|  |  |  | type ConfigReply struct { | 
					
						
							|  |  |  | 	AuthRPCReply | 
					
						
							|  |  |  | 	Config []byte // json-marshalled bytes of serverConfigV13
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-16 14:26:15 +08:00
										 |  |  | // Restart - Restart this instance of minio server.
 | 
					
						
							| 
									
										
										
										
											2017-01-04 15:39:22 +08:00
										 |  |  | func (s *adminCmd) Restart(args *AuthRPCArgs, reply *AuthRPCReply) error { | 
					
						
							| 
									
										
										
										
											2016-12-23 23:12:19 +08:00
										 |  |  | 	if err := args.IsAuthenticated(); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2016-12-16 14:26:15 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-12-23 23:12:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-16 14:26:15 +08:00
										 |  |  | 	globalServiceSignalCh <- serviceRestart | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 15:39:22 +08:00
										 |  |  | // ListLocks - lists locks held by requests handled by this server instance.
 | 
					
						
							|  |  |  | func (s *adminCmd) ListLocks(query *ListLocksQuery, reply *ListLocksReply) error { | 
					
						
							| 
									
										
										
										
											2017-01-23 16:32:55 +08:00
										 |  |  | 	if err := query.IsAuthenticated(); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-02-02 03:17:30 +08:00
										 |  |  | 	volLocks := listLocksInfo(query.bucket, query.prefix, query.duration) | 
					
						
							| 
									
										
										
										
											2017-01-04 15:39:22 +08:00
										 |  |  | 	*reply = ListLocksReply{volLocks: volLocks} | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-23 16:32:55 +08:00
										 |  |  | // ReInitDisk - reinitialize storage disks and object layer to use the
 | 
					
						
							|  |  |  | // new format.
 | 
					
						
							|  |  |  | func (s *adminCmd) ReInitDisks(args *AuthRPCArgs, reply *AuthRPCReply) error { | 
					
						
							|  |  |  | 	if err := args.IsAuthenticated(); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if !globalIsXL { | 
					
						
							|  |  |  | 		return errUnsupportedBackend | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Get the current object layer instance.
 | 
					
						
							|  |  |  | 	objLayer := newObjectLayerFn() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Initialize new disks to include the newly formatted disks.
 | 
					
						
							|  |  |  | 	bootstrapDisks, err := initStorageDisks(globalEndpoints) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Initialize new object layer with newly formatted disks.
 | 
					
						
							|  |  |  | 	newObjectAPI, err := newXLObjects(bootstrapDisks) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Replace object layer with newly formatted storage.
 | 
					
						
							|  |  |  | 	globalObjLayerMutex.Lock() | 
					
						
							|  |  |  | 	globalObjectAPI = newObjectAPI | 
					
						
							|  |  |  | 	globalObjLayerMutex.Unlock() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Shutdown storage belonging to old object layer instance.
 | 
					
						
							|  |  |  | 	objLayer.Shutdown() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-08 16:13:02 +08:00
										 |  |  | // Uptime - returns the time when object layer was initialized on this server.
 | 
					
						
							|  |  |  | func (s *adminCmd) Uptime(args *AuthRPCArgs, reply *UptimeReply) error { | 
					
						
							|  |  |  | 	if err := args.IsAuthenticated(); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if globalBootTime.IsZero() { | 
					
						
							|  |  |  | 		return errServerNotInitialized | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// N B The uptime is computed assuming that the system time is
 | 
					
						
							|  |  |  | 	// monotonic. This is not the case in time pkg in Go, see
 | 
					
						
							|  |  |  | 	// https://github.com/golang/go/issues/12914. This is expected
 | 
					
						
							|  |  |  | 	// to be fixed by go1.9.
 | 
					
						
							|  |  |  | 	*reply = UptimeReply{ | 
					
						
							| 
									
										
										
										
											2017-03-19 02:28:41 +08:00
										 |  |  | 		Uptime: UTCNow().Sub(globalBootTime), | 
					
						
							| 
									
										
										
										
											2017-02-08 16:13:02 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 04:58:50 +08:00
										 |  |  | // GetConfig - returns the config.json of this server.
 | 
					
						
							|  |  |  | func (s *adminCmd) GetConfig(args *AuthRPCArgs, reply *ConfigReply) error { | 
					
						
							|  |  |  | 	if err := args.IsAuthenticated(); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if serverConfig == nil { | 
					
						
							|  |  |  | 		return errors.New("config not present") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	jsonBytes, err := json.Marshal(serverConfig) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	reply.Config = jsonBytes | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-28 03:40:27 +08:00
										 |  |  | // WriteConfigArgs - wraps the bytes to be written and temporary file name.
 | 
					
						
							|  |  |  | type WriteConfigArgs struct { | 
					
						
							|  |  |  | 	AuthRPCArgs | 
					
						
							|  |  |  | 	TmpFileName string | 
					
						
							|  |  |  | 	Buf         []byte | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // WriteConfigReply - wraps the result of a writing config into a temporary file.
 | 
					
						
							|  |  |  | // the remote node.
 | 
					
						
							|  |  |  | type WriteConfigReply struct { | 
					
						
							|  |  |  | 	AuthRPCReply | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func writeTmpConfigCommon(tmpFileName string, configBytes []byte) error { | 
					
						
							| 
									
										
										
										
											2017-03-03 06:21:30 +08:00
										 |  |  | 	tmpConfigFile := filepath.Join(getConfigDir(), tmpFileName) | 
					
						
							|  |  |  | 	err := ioutil.WriteFile(tmpConfigFile, configBytes, 0666) | 
					
						
							|  |  |  | 	errorIf(err, fmt.Sprintf("Failed to write to temporary config file %s", tmpConfigFile)) | 
					
						
							| 
									
										
										
										
											2017-02-28 03:40:27 +08:00
										 |  |  | 	return err | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // WriteTmpConfig - writes the supplied config contents onto the
 | 
					
						
							|  |  |  | // supplied temporary file.
 | 
					
						
							|  |  |  | func (s *adminCmd) WriteTmpConfig(wArgs *WriteConfigArgs, wReply *WriteConfigReply) error { | 
					
						
							|  |  |  | 	if err := wArgs.IsAuthenticated(); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return writeTmpConfigCommon(wArgs.TmpFileName, wArgs.Buf) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // CommitConfigArgs - wraps the config file name that needs to be
 | 
					
						
							|  |  |  | // committed into config.json on this node.
 | 
					
						
							|  |  |  | type CommitConfigArgs struct { | 
					
						
							|  |  |  | 	AuthRPCArgs | 
					
						
							|  |  |  | 	FileName string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // CommitConfigReply - represents response to commit of config file on
 | 
					
						
							|  |  |  | // this node.
 | 
					
						
							|  |  |  | type CommitConfigReply struct { | 
					
						
							|  |  |  | 	AuthRPCReply | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // CommitConfig - Renames the temporary file into config.json on this node.
 | 
					
						
							|  |  |  | func (s *adminCmd) CommitConfig(cArgs *CommitConfigArgs, cReply *CommitConfigReply) error { | 
					
						
							| 
									
										
										
										
											2017-03-03 06:21:30 +08:00
										 |  |  | 	configFile := getConfigFile() | 
					
						
							|  |  |  | 	tmpConfigFile := filepath.Join(getConfigDir(), cArgs.FileName) | 
					
						
							| 
									
										
										
										
											2017-02-28 03:40:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-03 06:21:30 +08:00
										 |  |  | 	err := os.Rename(tmpConfigFile, configFile) | 
					
						
							|  |  |  | 	errorIf(err, fmt.Sprintf("Failed to rename %s to %s", tmpConfigFile, configFile)) | 
					
						
							|  |  |  | 	return err | 
					
						
							| 
									
										
										
										
											2017-02-28 03:40:27 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-16 14:26:15 +08:00
										 |  |  | // registerAdminRPCRouter - registers RPC methods for service status,
 | 
					
						
							|  |  |  | // stop and restart commands.
 | 
					
						
							|  |  |  | func registerAdminRPCRouter(mux *router.Router) error { | 
					
						
							| 
									
										
										
										
											2017-01-04 15:39:22 +08:00
										 |  |  | 	adminRPCHandler := &adminCmd{} | 
					
						
							| 
									
										
										
										
											2016-12-16 14:26:15 +08:00
										 |  |  | 	adminRPCServer := rpc.NewServer() | 
					
						
							| 
									
										
										
										
											2017-01-04 15:39:22 +08:00
										 |  |  | 	err := adminRPCServer.RegisterName("Admin", adminRPCHandler) | 
					
						
							| 
									
										
										
										
											2016-12-16 14:26:15 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return traceError(err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-02-17 06:52:14 +08:00
										 |  |  | 	adminRouter := mux.NewRoute().PathPrefix(minioReservedBucketPath).Subrouter() | 
					
						
							| 
									
										
										
										
											2017-01-04 15:39:22 +08:00
										 |  |  | 	adminRouter.Path(adminPath).Handler(adminRPCServer) | 
					
						
							| 
									
										
										
										
											2016-12-16 14:26:15 +08:00
										 |  |  | 	return nil | 
					
						
							|  |  |  | } |