github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/peer/node/stop.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package node
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"io/ioutil"
    23  	"os"
    24  	"strconv"
    25  	"syscall"
    26  
    27  	"github.com/golang/protobuf/ptypes/empty"
    28  	"github.com/hyperledger/fabric/core/config"
    29  	"github.com/hyperledger/fabric/core/peer"
    30  	pb "github.com/hyperledger/fabric/protos/peer"
    31  	"github.com/spf13/cobra"
    32  	"golang.org/x/net/context"
    33  )
    34  
    35  func stopCmd() *cobra.Command {
    36  	nodeStopCmd.Flags().StringVar(&stopPidFile, "stop-peer-pid-file",
    37  		config.GetPath("peer.fileSystemPath"),
    38  		"Location of peer pid local file, for forces kill")
    39  
    40  	return nodeStopCmd
    41  }
    42  
    43  var nodeStopCmd = &cobra.Command{
    44  	Use:   "stop",
    45  	Short: "Stops the running node.",
    46  	Long:  `Stops the running node, disconnecting from the network.`,
    47  	Run: func(cmd *cobra.Command, args []string) {
    48  		stop()
    49  	},
    50  }
    51  
    52  func stop() (err error) {
    53  	clientConn, err := peer.NewPeerClientConnection()
    54  	if err != nil {
    55  		pidFile := stopPidFile + "/peer.pid"
    56  		//fmt.Printf("Stopping local peer using process pid from %s \n", pidFile)
    57  		logger.Infof("Error trying to connect to local peer: %s", err)
    58  		logger.Infof("Stopping local peer using process pid from %s", pidFile)
    59  		pid, ferr := readPid(pidFile)
    60  		if ferr != nil {
    61  			err = fmt.Errorf("Error trying to read pid from %s: %s", pidFile, ferr)
    62  			return
    63  		}
    64  		killerr := syscall.Kill(pid, syscall.SIGTERM)
    65  		if killerr != nil {
    66  			err = fmt.Errorf("Error trying to kill -9 pid %d: %s", pid, killerr)
    67  			return
    68  		}
    69  		return nil
    70  	}
    71  	logger.Info("Stopping peer using grpc")
    72  	serverClient := pb.NewAdminClient(clientConn)
    73  
    74  	status, err := serverClient.StopServer(context.Background(), &empty.Empty{})
    75  	if err != nil {
    76  		fmt.Println(&pb.ServerStatus{Status: pb.ServerStatus_STOPPED})
    77  		return nil
    78  	}
    79  
    80  	err = fmt.Errorf("Connection remain opened, peer process doesn't exit")
    81  	fmt.Println(status)
    82  	return err
    83  }
    84  
    85  func readPid(fileName string) (int, error) {
    86  	fd, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE, 0644)
    87  	if err != nil {
    88  		return 0, err
    89  	}
    90  	defer fd.Close()
    91  	if err := syscall.Flock(int(fd.Fd()), syscall.LOCK_EX|syscall.LOCK_NB); err != nil {
    92  		return 0, fmt.Errorf("can't lock '%s', lock is held", fd.Name())
    93  	}
    94  
    95  	if _, err := fd.Seek(0, 0); err != nil {
    96  		return 0, err
    97  	}
    98  
    99  	data, err := ioutil.ReadAll(fd)
   100  	if err != nil {
   101  		return 0, err
   102  	}
   103  
   104  	pid, err := strconv.Atoi(string(bytes.TrimSpace(data)))
   105  	if err != nil {
   106  		return 0, fmt.Errorf("error parsing pid from %s: %s", fd.Name(), err)
   107  	}
   108  
   109  	if err := syscall.Flock(int(fd.Fd()), syscall.LOCK_UN); err != nil {
   110  		return 0, fmt.Errorf("can't release lock '%s', lock is held", fd.Name())
   111  	}
   112  
   113  	return pid, nil
   114  
   115  }