github.com/hyperledger-labs/bdls@v2.1.1+incompatible/integration/pluggable/plugin_activation.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package pluggable 8 9 import ( 10 "fmt" 11 "io/ioutil" 12 "os" 13 "path/filepath" 14 15 "github.com/spf13/viper" 16 ) 17 18 const ( 19 EndorsementPluginEnvVar = "ENDORSEMENT_PLUGIN_ENV_VAR" 20 ValidationPluginEnvVar = "VALIDATION_PLUGIN_ENV_VAR" 21 ) 22 23 // EndorsementPluginActivationFolder returns the name of the folder that if 24 // the file of the peer's id in it exists - it indicates that the endorsement plugin was activated 25 // for that peer 26 func EndorsementPluginActivationFolder() string { 27 return os.Getenv(EndorsementPluginEnvVar) 28 } 29 30 // SetEndorsementPluginActivationFolder sets the name of the folder 31 // that if the file of the peer's id in it exists - it indicates that the endorsement plugin was activated 32 // for that peer 33 func SetEndorsementPluginActivationFolder(path string) { 34 os.Setenv(EndorsementPluginEnvVar, path) 35 } 36 37 // ValidationPluginActivationFilePath returns the name of the folder that if 38 // the file of the peer's id in it exists - it indicates that the validation plugin was activated 39 // for that peer 40 func ValidationPluginActivationFolder() string { 41 return os.Getenv(ValidationPluginEnvVar) 42 } 43 44 // SetValidationPluginActivationFolder sets the name of the folder 45 // that if the file of the peer's id in it exists - it indicates that the validation plugin was activated 46 // for that peer 47 func SetValidationPluginActivationFolder(path string) { 48 os.Setenv(ValidationPluginEnvVar, path) 49 } 50 51 func markPluginActivation(dir string) { 52 fileName := filepath.Join(dir, viper.GetString("peer.id")) 53 _, err := os.Create(fileName) 54 if err != nil { 55 panic(fmt.Sprintf("failed to create file %s: %v", fileName, err)) 56 } 57 } 58 59 // PublishEndorsementPluginActivation makes it known that the endorsement plugin 60 // was activated for the peer that is invoking this function 61 func PublishEndorsementPluginActivation() { 62 markPluginActivation(EndorsementPluginActivationFolder()) 63 } 64 65 // PublishValidationPluginActivation makes it known that the validation plugin 66 // was activated for the peer that is invoking this function 67 func PublishValidationPluginActivation() { 68 markPluginActivation(ValidationPluginActivationFolder()) 69 } 70 71 // CountEndorsementPluginActivations returns the number of peers that activated 72 // the endorsement plugin 73 func CountEndorsementPluginActivations() int { 74 return listDir(EndorsementPluginActivationFolder()) 75 } 76 77 // CountValidationPluginActivations returns the number of peers that activated 78 // the validation plugin 79 func CountValidationPluginActivations() int { 80 return listDir(ValidationPluginActivationFolder()) 81 } 82 83 func listDir(d string) int { 84 dir, err := ioutil.ReadDir(d) 85 if err != nil { 86 panic(fmt.Sprintf("failed listing directory %s: %v", d, err)) 87 } 88 return len(dir) 89 }