diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 925866c..30fcb52 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -10,19 +10,18 @@ on: branches: [ "main" ] jobs: - build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Set up Go uses: actions/setup-go@v4 with: - go-version: '1.20' + go-version: '1.21' + + - name: Format + run: gofmt -s -w . - name: Build run: go build -v ./... - - - name: Test - run: go test -v ./... diff --git a/README.md b/README.md index 43669bb..939cae5 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ -Clone the repo -cd KbaFabriX -go run main.go -give domain name (eg: auto.com) -give number of organisations -give name of organisation -give number of peers +# FabriX + +FabriX is a tool that helps chaincode developers easily set up a Hyperledger Fabric network. + +## 🛠 Built With + +[![Go](https://img.shields.io/badge/go-dodgerblue?style=for-the-badge&logo=go&logoColor=white)](https://go.dev/) diff --git a/cmd/root.go b/cmd/root.go index 9c7910b..9a3189e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -22,7 +22,7 @@ var rootCmd = &cobra.Command{ menu.ShowMainMenu() var userInput string fmt.Scan(&userInput) - if userInput == "N"{ + if userInput == "N" { menu.GetInputsFromUser() } }, diff --git a/pkg/configs/configs.go b/pkg/configs/configs.go index 0a4256e..fbf8b50 100644 --- a/pkg/configs/configs.go +++ b/pkg/configs/configs.go @@ -10,10 +10,10 @@ import ( "golang.org/x/text/language" ) -func CreateConfigs(domainName string,orgPeers map[string]int) { +func CreateConfigs(domainName string, orgPeers map[string]int) { CreateDockerComposeCA(orgPeers) - CreateDockerComposeMembers(domainName,orgPeers) + CreateDockerComposeMembers(domainName, orgPeers) } @@ -66,7 +66,7 @@ func CreateDockerComposeCA(orgPeers map[string]int) { "test", } viper.Set("services.ca_orderer.networks", networkSlice) - + // create configs for all the organisations for org := range orgPeers { org := strings.ToLower(org) @@ -106,11 +106,9 @@ func CreateDockerComposeCA(orgPeers map[string]int) { viper.Set(fmt.Sprintf("services.ca_%v.networks", org), networkSlice) - err := viper.SafeWriteConfig() - if err != nil { + if err := viper.SafeWriteConfig(); err != nil { if _, ok := err.(viper.ConfigFileAlreadyExistsError); ok { - err = viper.WriteConfig() - if err != nil { + if err = viper.WriteConfig(); err != nil { log.Fatalf("Error while updating config file %s", err) } } else { @@ -129,7 +127,7 @@ func CreateRegisterEnroll(orgPeers map[string]int) { // how to impliment since viper dont support script file } -func CreateDockerComposeMembers(domainName string,orgPeers map[string]int) { +func CreateDockerComposeMembers(domainName string, orgPeers map[string]int) { //viper.KeyDelimiter(":") to adjest the key delimiter from "." to ":" // for adding keys like "orderer.example.com" @@ -140,14 +138,14 @@ func CreateDockerComposeMembers(domainName string,orgPeers map[string]int) { custom_viper.AddConfigPath("pkg/configs/generated/docker") custom_viper.Set("version", "3.7") custom_viper.Set("networks:test:name", "fabric_test") -// volumes will be added when the peers are created + // volumes will be added when the peers are created // creating configs for ordering service - - custom_viper.Set(fmt.Sprintf("volumes:orderer.%v",domainName), "") - custom_viper.Set(fmt.Sprintf("services:orderer.%v:container_name",domainName ) ,fmt.Sprintf("orderer.%v", domainName) ) - custom_viper.Set(fmt.Sprintf("services:orderer.%v:image", domainName) , "hyperledger/fabric-orderer:2.5.4") - custom_viper.Set(fmt.Sprintf("services:orderer.%v:labels:service",domainName ) , "hyperledger-fabric") + + custom_viper.Set(fmt.Sprintf("volumes:orderer.%v", domainName), "") + custom_viper.Set(fmt.Sprintf("services:orderer.%v:container_name", domainName), fmt.Sprintf("orderer.%v", domainName)) + custom_viper.Set(fmt.Sprintf("services:orderer.%v:image", domainName), "hyperledger/fabric-orderer:2.5.4") + custom_viper.Set(fmt.Sprintf("services:orderer.%v:labels:service", domainName), "hyperledger-fabric") ordererEnv := []string{ "FABRIC_LOGGING_SPEC=INFO", @@ -171,11 +169,11 @@ func CreateDockerComposeMembers(domainName string,orgPeers map[string]int) { "ORDERER_ADMIN_TLS_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]", "ORDERER_ADMIN_TLS_CLIENTROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]", "ORDERER_ADMIN_LISTENADDRESS=0.0.0.0:7053", - fmt.Sprintf("ORDERER_OPERATIONS_LISTENADDRESS=orderer.%v:9443",domainName), + fmt.Sprintf("ORDERER_OPERATIONS_LISTENADDRESS=orderer.%v:9443", domainName), "ORDERER_METRICS_PROVIDER=prometheus", } - custom_viper.Set(fmt.Sprintf("services:orderer.%v:environment",domainName), ordererEnv) - custom_viper.Set(fmt.Sprintf("services:orderer.%v:working_dir", domainName) , "/root") + custom_viper.Set(fmt.Sprintf("services:orderer.%v:environment", domainName), ordererEnv) + custom_viper.Set(fmt.Sprintf("services:orderer.%v:working_dir", domainName), "/root") custom_viper.Set(fmt.Sprintf("services:orderer.%v:command", domainName), "orderer") // correct the domain name or keep the example.com @@ -196,7 +194,7 @@ func CreateDockerComposeMembers(domainName string,orgPeers map[string]int) { networkSlice := []string{ "test", } - custom_viper.Set(fmt.Sprintf("services:orderer.%v:networks", domainName) , networkSlice) + custom_viper.Set(fmt.Sprintf("services:orderer.%v:networks", domainName), networkSlice) // configs for CLI custom_viper.Set("services:cli:container_name", "cli") @@ -224,7 +222,7 @@ func CreateDockerComposeMembers(domainName string,orgPeers map[string]int) { // CLI depends will be added from the for loop CLIDepends := []string{} - + // for creating port numbers dynamically as well keeping the peer count i := 0 ports := []int{ @@ -278,18 +276,18 @@ func CreateDockerComposeMembers(domainName string,orgPeers map[string]int) { fmt.Sprintf("CORE_PEER_ID=peer%v.%v.%v", peer, org, domainName), fmt.Sprintf("CORE_PEER_ADDRESS=peer%v.%v.%v:%v", peer, org, domainName, ports[1]+i*2000), fmt.Sprintf("CORE_PEER_LISTENADDRESS=0.0.0.0:%v", ports[1]+i*2000), - fmt.Sprintf("CORE_PEER_CHAINCODEADDRESS=peer%v.%v.%v:%v", peer, org, domainName,ports[1]+i*2000+1), + fmt.Sprintf("CORE_PEER_CHAINCODEADDRESS=peer%v.%v.%v:%v", peer, org, domainName, ports[1]+i*2000+1), fmt.Sprintf("CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:%v", ports[1]+i*2000+1), fmt.Sprintf("CORE_PEER_GOSSIP_BOOTSTRAP=peer%v.%v.%v:%v", peer, org, domainName, ports[1]+i*2000), - fmt.Sprintf("CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer%v.%v.%v:%v", peer, org, domainName,ports[1]+i*2000), + fmt.Sprintf("CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer%v.%v.%v:%v", peer, org, domainName, ports[1]+i*2000), fmt.Sprintf("CORE_PEER_LOCALMSPID=%v", orgMSP), "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/fabric/msp", - fmt.Sprintf("CORE_OPERATIONS_LISTENADDRESS=peer%v.%v.%v:%v", peer, org, domainName,ports[2]+i*1), + fmt.Sprintf("CORE_OPERATIONS_LISTENADDRESS=peer%v.%v.%v:%v", peer, org, domainName, ports[2]+i*1), "CORE_METRICS_PROVIDER=prometheus", fmt.Sprintf("CHAINCODE_AS_A_SERVICE_BUILDER_CONFIG={'peername':'peer%v%v'}", peer, org), "CORE_CHAINCODE_EXECUTETIMEOUT=300s", "CORE_LEDGER_STATE_STATEDATABASE=CouchDB", - fmt.Sprintf("CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=%v:5984", fmt.Sprintf("%vpeer%vdb", org, peer)), + fmt.Sprintf("CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=%v:5984", fmt.Sprintf("%vpeer%vdb", org, peer)), "CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=admin", "CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=adminpw", } @@ -297,8 +295,8 @@ func CreateDockerComposeMembers(domainName string,orgPeers map[string]int) { peerVolumes := []string{ "/var/run/docker.sock:/host/var/run/docker.sock", - fmt.Sprintf("../organizations/peerOrganizations/%v.%v/peers/peer%v.%v.%v:/etc/hyperledger/fabric",org,domainName,peer,org, domainName), - fmt.Sprintf("peer%v.%v.%v:/var/hyperledger/production",peer,org, domainName ), + fmt.Sprintf("../organizations/peerOrganizations/%v.%v/peers/peer%v.%v.%v:/etc/hyperledger/fabric", org, domainName, peer, org, domainName), + fmt.Sprintf("peer%v.%v.%v:/var/hyperledger/production", peer, org, domainName), } custom_viper.Set(fmt.Sprintf("services:peer%v.%v.%v:volumes", peer, org, domainName), peerVolumes) @@ -306,7 +304,7 @@ func CreateDockerComposeMembers(domainName string,orgPeers map[string]int) { custom_viper.Set(fmt.Sprintf("services:peer%v.%v.%v:command", peer, org, domainName), "peer node start") peerPorts := []string{ fmt.Sprintf("%v:%v", ports[1]+i*2000, ports[1]+i*2000), - fmt.Sprintf("%v:%v",ports[2]+i*1, ports[2]+i*1), + fmt.Sprintf("%v:%v", ports[2]+i*1, ports[2]+i*1), } custom_viper.Set(fmt.Sprintf("services:peer%v.%v.%v:ports", peer, org, domainName), peerPorts) @@ -324,12 +322,9 @@ func CreateDockerComposeMembers(domainName string,orgPeers map[string]int) { CLIDepends = append(CLIDepends, fmt.Sprintf("peer%v.%v.%v", peer, org, domainName)) custom_viper.Set("services:cli:depends_on", CLIDepends) - - err := custom_viper.SafeWriteConfig() - if err != nil { + if err := custom_viper.SafeWriteConfig(); err != nil { if _, ok := err.(viper.ConfigFileAlreadyExistsError); ok { - err = custom_viper.WriteConfig() - if err != nil { + if err = custom_viper.WriteConfig(); err != nil { log.Fatalf("Error while updating config file %s", err) } } else { @@ -418,9 +413,8 @@ func CreateConfigTx() { // fmt.Println("Configuration file created/updated successfully!") // } - - // volumeMap := map[string]string{ - // "orderer.example.com": "", - // "peer0.manufacturer.example.com": "", - // "peer0.dealer.example.com": "", - // } +// volumeMap := map[string]string{ +// "orderer.example.com": "", +// "peer0.manufacturer.example.com": "", +// "peer0.dealer.example.com": "", +// }