func stop() (err error) { clientConn, err := peer.NewPeerClientConnection() if err != nil { pidFile := stopPidFile + "/peer.pid" //fmt.Printf("Stopping local peer using process pid from %s \n", pidFile) logger.Infof("Error trying to connect to local peer: %s", err) logger.Infof("Stopping local peer using process pid from %s", pidFile) pid, ferr := readPid(pidFile) if ferr != nil { err = fmt.Errorf("Error trying to read pid from %s: %s", pidFile, ferr) return } killerr := syscall.Kill(pid, syscall.SIGTERM) if killerr != nil { err = fmt.Errorf("Error trying to kill -9 pid %d: %s", pid, killerr) return } return nil } logger.Info("Stopping peer using grpc") serverClient := pb.NewAdminClient(clientConn) status, err := serverClient.StopServer(context.Background(), &google_protobuf.Empty{}) if err != nil { fmt.Println(&pb.ServerStatus{Status: pb.ServerStatus_STOPPED}) return nil } err = fmt.Errorf("Connection remain opened, peer process doesn't exit") fmt.Println(status) return err }
// GetDevopsClient returns a new client connection for this peer func GetDevopsClient(cmd *cobra.Command) (pb.DevopsClient, error) { clientConn, err := peer.NewPeerClientConnection() if err != nil { return nil, fmt.Errorf("Error trying to connect to local peer: %s", err) } devopsClient := pb.NewDevopsClient(clientConn) return devopsClient, nil }
// GetAdminClient returns a new admin client connection for this peer func GetAdminClient() (pb.AdminClient, error) { clientConn, err := peer.NewPeerClientConnection() if err != nil { err = errors.ErrorWithCallstack(errors.Peer, errors.PeerConnectionError, err.Error()) return nil, err } adminClient := pb.NewAdminClient(clientConn) return adminClient, nil }
// GetEndorserClient returns a new endorser client connection for this peer func GetEndorserClient() (pb.EndorserClient, error) { clientConn, err := peer.NewPeerClientConnection() if err != nil { err = errors.ErrorWithCallstack(errors.Peer, errors.PeerConnectionError, err.Error()) return nil, err } endorserClient := pb.NewEndorserClient(clientConn) return endorserClient, nil }
func stopOpenchain(t *testing.T) { clientConn, err := peer.NewPeerClientConnection() if err != nil { t.Log(fmt.Errorf("Error trying to connect to local peer:", err)) t.Fail() } t.Log("Stopping peer...") serverClient := pb.NewAdminClient(clientConn) status, err := serverClient.StopServer(context.Background(), &google_protobuf.Empty{}) t.Logf("Current status: %s", status) }
// Show a list of all existing network connections for the target peer node, // includes both validating and non-validating peers func networkList() (err error) { clientConn, err := peer.NewPeerClientConnection() if err != nil { err = fmt.Errorf("Error trying to connect to local peer: %s", err) return } openchainClient := pb.NewOpenchainClient(clientConn) peers, err := openchainClient.GetPeers(context.Background(), &google_protobuf.Empty{}) if err != nil { err = fmt.Errorf("Error trying to get peers: %s", err) return } jsonOutput, _ := json.Marshal(peers) fmt.Println(string(jsonOutput)) return nil }
// Show a list of all existing network connections for the target peer node, // includes both validating and non-validating peers func networkList() (err error) { clientConn, err := peer.NewPeerClientConnection() if err != nil { err = fmt.Errorf("Error trying to connect to local peer: %s", err) return } openchainClient := pb.NewOpenchainClient(clientConn) peers, err := openchainClient.GetPeers(context.Background(), &empty.Empty{}) if err != nil { err = fmt.Errorf("Error trying to get peers: %s", err) return } // The generated pb.PeersMessage struct will be added "omitempty" tag automatically. // But we still want to print it when pb.PeersMessage is empty. jsonOutput, _ := json.Marshal(struct{ Peers []*pb.PeerEndpoint }{append([]*pb.PeerEndpoint{}, peers.GetPeers()...)}) fmt.Println(string(jsonOutput)) return nil }
func initPeerClient() (err error) { config.SetupTestConfig(".") viper.Set("ledger.blockchain.deploy-system-chaincode", "false") viper.Set("peer.validator.validity-period.verification", "false") peerClientConn, err = peer.NewPeerClientConnection() if err != nil { fmt.Printf("error connection to server at host:port = %s\n", viper.GetString("peer.address")) return } serverClient = pb.NewPeerClient(peerClientConn) // Logging var formatter = logging.MustStringFormatter( `%{color}[%{module}] %{shortfunc} [%{shortfile}] -> %{level:.4s} %{id:03x}%{color:reset} %{message}`, ) logging.SetFormatter(formatter) return }
func status() (err error) { clientConn, err := peer.NewPeerClientConnection() if err != nil { logger.Infof("Error trying to connect to local peer: %s", err) err = fmt.Errorf("Error trying to connect to local peer: %s", err) fmt.Println(&pb.ServerStatus{Status: pb.ServerStatus_UNKNOWN}) return err } serverClient := pb.NewAdminClient(clientConn) status, err := serverClient.GetStatus(context.Background(), &google_protobuf.Empty{}) if err != nil { logger.Infof("Error trying to get status from local peer: %s", err) err = fmt.Errorf("Error trying to connect to local peer: %s", err) fmt.Println(&pb.ServerStatus{Status: pb.ServerStatus_UNKNOWN}) return err } fmt.Println(status) return nil }
// login confirms the enrollmentID and secret password of the client with the // CA and stores the enrollment certificate and key in the Devops server. func networkLogin(args []string) (err error) { logger.Info("CLI client login...") // Check for username argument if len(args) == 0 { err = errors.New("Must supply username") return } // Check for other extraneous arguments if len(args) != 1 { err = errors.New("Must supply username as the 1st and only parameter") return } // Retrieve the CLI data storage path // Returns /var/openchain/production/client/ localStore := getCliFilePath() logger.Infof("Local data store for client loginToken: %s", localStore) // If the user is already logged in, return if _, err = os.Stat(localStore + "loginToken_" + args[0]); err == nil { logger.Infof("User '%s' is already logged in.\n", args[0]) return } // If the '--password' flag is not specified, need read it from the terminal if loginPW == "" { // User is not logged in, prompt for password fmt.Printf("Enter password for user '%s': ", args[0]) var pw []byte if pw, err = gopass.GetPasswdMasked(); err != nil { err = fmt.Errorf("Error trying to read password from console: %s", err) return } loginPW = string(pw) } // Log in the user logger.Infof("Logging in user '%s' on CLI interface...\n", args[0]) // Get a devopsClient to perform the login clientConn, err := peer.NewPeerClientConnection() if err != nil { err = fmt.Errorf("Error trying to connect to local peer: %s", err) return } devopsClient := pb.NewDevopsClient(clientConn) // Build the login spec and login loginSpec := &pb.Secret{EnrollId: args[0], EnrollSecret: loginPW} loginResult, err := devopsClient.Login(context.Background(), loginSpec) // Check if login is successful if loginResult.Status == pb.Response_SUCCESS { // If /var/openchain/production/client/ directory does not exist, create it if _, err := os.Stat(localStore); err != nil { if os.IsNotExist(err) { // Directory does not exist, create it if err := os.Mkdir(localStore, 0755); err != nil { panic(fmt.Errorf("Fatal error when creating %s directory: %s\n", localStore, err)) } } else { // Unexpected error panic(fmt.Errorf("Fatal error on os.Stat of %s directory: %s\n", localStore, err)) } } // Store client security context into a file logger.Infof("Storing login token for user '%s'.\n", args[0]) err = ioutil.WriteFile(localStore+"loginToken_"+args[0], []byte(args[0]), 0755) if err != nil { panic(fmt.Errorf("Fatal error when storing client login token: %s\n", err)) } logger.Infof("Login successful for user '%s'.\n", args[0]) } else { err = fmt.Errorf("Error on client login: %s", string(loginResult.Msg)) return } return nil }