├── _test ├── md │ └── metadata.json ├── output │ ├── metadata.json │ ├── META-INF │ │ └── statedb │ │ │ └── couchdb │ │ │ └── indexes │ │ │ └── indexOwner.json │ └── connection.json ├── release │ ├── statedb │ │ └── couchdb │ │ │ └── indexes │ │ │ └── indexOwner.json │ └── chaincode │ │ └── server │ │ └── connection.json └── src │ ├── META-INF │ └── statedb │ │ └── couchdb │ │ └── indexes │ │ └── indexOwner.json │ └── connection.json ├── go.mod ├── Dockerfile ├── .gitignore ├── justfile ├── go.sum ├── cmd ├── detect │ └── main.go ├── release │ └── main.go └── build │ └── main.go ├── .github └── workflows │ └── publish.yaml ├── README.md └── LICENSE /_test/md/metadata.json: -------------------------------------------------------------------------------- 1 | { 2 | "type":"external", 3 | "hello":"world" 4 | } 5 | -------------------------------------------------------------------------------- /_test/output/metadata.json: -------------------------------------------------------------------------------- 1 | { 2 | "type":"external", 3 | "hello":"world" 4 | } 5 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module hyperledgendary/fabric-ext-builder/v2 2 | 3 | go 1.16 4 | 5 | require github.com/otiai10/copy v1.6.0 // indirect 6 | -------------------------------------------------------------------------------- /_test/release/statedb/couchdb/indexes/indexOwner.json: -------------------------------------------------------------------------------- 1 | {"index":{"fields":["docType","owner"]},"ddoc":"indexOwnerDoc", "name":"indexOwner","type":"json"} 2 | -------------------------------------------------------------------------------- /_test/src/META-INF/statedb/couchdb/indexes/indexOwner.json: -------------------------------------------------------------------------------- 1 | {"index":{"fields":["docType","owner"]},"ddoc":"indexOwnerDoc", "name":"indexOwner","type":"json"} 2 | -------------------------------------------------------------------------------- /_test/output/META-INF/statedb/couchdb/indexes/indexOwner.json: -------------------------------------------------------------------------------- 1 | {"index":{"fields":["docType","owner"]},"ddoc":"indexOwnerDoc", "name":"indexOwner","type":"json"} 2 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # Copyright IBM Corp. All Rights Reserved. 2 | # 3 | # SPDX-License-Identifier: Apache-2.0 4 | 5 | ARG GO_VER=1.14.4 6 | ARG ALPINE_VER=3.12 7 | 8 | FROM golang:${GO_VER}-alpine${ALPINE_VER} 9 | 10 | WORKDIR /go/src/github.com/hyperledgendary/fabric-ccs-builder 11 | 12 | COPY . . 13 | 14 | RUN go install -v ./cmd/... 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Test binary, built with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | 14 | # Dependency directories (remove the comment below to include it) 15 | # vendor/ 16 | bin/ -------------------------------------------------------------------------------- /justfile: -------------------------------------------------------------------------------- 1 | build: 2 | go build -o bin ./cmd/build/ 3 | go build -o bin ./cmd/detect/ 4 | go build -o bin ./cmd/release/ 5 | 6 | test: build 7 | rm -rf ./_test/release && rm -rf ./_test/output 8 | ./bin/detect ./_test/src/ ./_test/md/ && echo "== Detect OK ==" 9 | ./bin/build ./_test/src/ ./_test/md/ ./_test/output/ && echo "== Build OK ==" 10 | ./bin/release ./_test/output/ ./_test/release/ && echo "== Release OK ==" 11 | cp -r ./bin ../../hyperledger/fabric-samples/external-chaincode/builder/ -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/otiai10/copy v1.6.0 h1:IinKAryFFuPONZ7cm6T6E2QX/vcJwSnlaA5lfoaXIiQ= 2 | github.com/otiai10/copy v1.6.0/go.mod h1:XWfuS3CrI0R6IE0FbgHsEazaXO8G0LpMp9o8tos0x4E= 3 | github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE= 4 | github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs= 5 | github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo= 6 | github.com/otiai10/mint v1.3.2/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc= 7 | -------------------------------------------------------------------------------- /cmd/detect/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "io/ioutil" 7 | "log" 8 | "os" 9 | "path/filepath" 10 | "strings" 11 | ) 12 | 13 | var logger = log.New(os.Stderr, "", 0) 14 | 15 | func main() { 16 | logger.Println("::Detect 002") 17 | 18 | if err := run(); err != nil { 19 | logger.Printf("::Error: %v\n", err) 20 | os.Exit(1) 21 | } else { 22 | logger.Printf("::Type detected as external") 23 | } 24 | } 25 | 26 | type ChaincodeMetadata struct { 27 | Type string `json:"type"` 28 | } 29 | 30 | func run() error { 31 | if len(os.Args) > 2 { 32 | chaincodeMetaData := os.Args[2] 33 | mdbytes, err := ioutil.ReadFile(filepath.Join(chaincodeMetaData, "metadata.json")) 34 | if err != nil { 35 | return err 36 | } 37 | 38 | var metadata ChaincodeMetadata 39 | err = json.Unmarshal(mdbytes, &metadata) 40 | if err != nil { 41 | return err 42 | } 43 | 44 | switch strings.ToLower(metadata.Type) { 45 | case "external": 46 | return nil 47 | default: 48 | return fmt.Errorf("chaincode type not supported: %s", metadata.Type) 49 | } 50 | 51 | } else { 52 | return fmt.Errorf("Too few arguments") 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /.github/workflows/publish.yaml: -------------------------------------------------------------------------------- 1 | name: fabric-ccs-builder 2 | 3 | on: 4 | push: 5 | # Publish `main` as Docker `latest` image. 6 | branches: 7 | - main 8 | 9 | # Publish `v1.2.3` tags as releases. 10 | tags: 11 | - v* 12 | 13 | # Run tests for any PRs. 14 | pull_request: 15 | 16 | env: 17 | IMAGE_NAME: fabric-ccs-builder 18 | 19 | jobs: 20 | # Push image to GitHub Packages. 21 | # See also https://docs.docker.com/docker-hub/builds/ 22 | push: 23 | runs-on: ubuntu-latest 24 | permissions: 25 | packages: write 26 | contents: read 27 | 28 | steps: 29 | - uses: actions/checkout@v2 30 | 31 | - name: Build image 32 | run: docker build . --file Dockerfile --tag $IMAGE_NAME --label "runnumber=${GITHUB_RUN_ID}" 33 | 34 | - name: Log in to registry 35 | # This is where you will update the PAT to GITHUB_TOKEN 36 | run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin 37 | 38 | - name: Push image 39 | run: | 40 | IMAGE_ID=ghcr.io/${{ github.repository_owner }}/$IMAGE_NAME 41 | 42 | # Change all uppercase to lowercase 43 | IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]') 44 | # Strip git ref prefix from version 45 | VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') 46 | # Strip "v" prefix from tag name 47 | [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//') 48 | # Use Docker `latest` tag convention 49 | [ "$VERSION" == "main" ] && VERSION=latest 50 | echo IMAGE_ID=$IMAGE_ID 51 | echo VERSION=$VERSION 52 | docker tag $IMAGE_NAME $IMAGE_ID:$VERSION 53 | docker push $IMAGE_ID:$VERSION -------------------------------------------------------------------------------- /_test/output/connection.json: -------------------------------------------------------------------------------- 1 | {"address":"cc.example.com:9999","dial_timeout":"10s","tls_required":false,"client_auth_required":false,"root_cert":"-----BEGIN CERTIFICATE-----\nMIICFDCCAbqgAwIBAgIUfBkPeOyUJPKoI1sUinUkdtin6JYwCgYIKoZIzj0EAwIw\naDELMAkGA1UEBhMCVVMxFzAVBgNVBAgTDk5vcnRoIENhcm9saW5hMRQwEgYDVQQK\nEwtIeXBlcmxlZGdlcjEPMA0GA1UECxMGRmFicmljMRkwFwYDVQQDExBkaWdpYmFu\na2NhLXRsc2NhMB4XDTIxMDYwNzA4MDQwMFoXDTM2MDYwMzA4MDQwMFowaDELMAkG\nA1UEBhMCVVMxFzAVBgNVBAgTDk5vcnRoIENhcm9saW5hMRQwEgYDVQQKEwtIeXBl\ncmxlZGdlcjEPMA0GA1UECxMGRmFicmljMRkwFwYDVQQDExBkaWdpYmFua2NhLXRs\nc2NhMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEImThmXShCC22x+E3WSebng9e\nojjd7UnBCaJ/w67OVeLbBetQTAkcABh/ObHLCc3lMiGXWt+FYO/kTZ83JDPFlKNC\nMEAwDgYDVR0PAQH/BAQDAgEGMA8GA1UdEwEB/wQFMAMBAf8wHQYDVR0OBBYEFBPF\n9Ys9RFYhOdHT2c03/rVkAEZjMAoGCCqGSM49BAMCA0gAMEUCIQCijYIQi0I6N/WH\nJNc8X5U9SUk/DZ+lEgiImgdickWIwAIgffnk39DOK2DdI0BqGWpM83/22X5Pjgk2\nYBH+FfegT7c=\n-----END CERTIFICATE-----\n","client_key":"-----BEGIN PRIVATE KEY-----\nMIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgIcu+Lj7+PFEmtGyC\nwVk7FzLwyap1dQ+RUEpMA5Q6+VehRANCAAS/Tj6gJ17iv3dbn1/a8/9nsgRQGPwf\nWTc3FaeldNbqhQcAMVTB4cEP/DMQmCHFy5GJ+m103msTNIEzZJuCh+6u\n-----END PRIVATE KEY-----\n","client_cert":"-----BEGIN CERTIFICATE-----\nMIICaTCCAhCgAwIBAgIUKovVcBJlc5OQw1rG/vNTueJ/pK4wCgYIKoZIzj0EAwIw\naDELMAkGA1UEBhMCVVMxFzAVBgNVBAgTDk5vcnRoIENhcm9saW5hMRQwEgYDVQQK\nEwtIeXBlcmxlZGdlcjEPMA0GA1UECxMGRmFicmljMRkwFwYDVQQDExBkaWdpYmFu\na2NhLXRsc2NhMB4XDTIxMDYwNzA4MjQwMFoXDTM2MDYwMzA4MDQwMFowIzEPMA0G\nA1UECxMGY2xpZW50MRAwDgYDVQQDDAdkYnBlZXJ9MFkwEwYHKoZIzj0CAQYIKoZI\nzj0DAQcDQgAEv04+oCde4r93W59f2vP/Z7IEUBj8H1k3NxWnpXTW6oUHADFUweHB\nD/wzEJghxcuRifptdN5rEzSBM2SbgofurqOB3DCB2TAOBgNVHQ8BAf8EBAMCA6gw\nHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMCMAwGA1UdEwEB/wQCMAAwHQYD\nVR0OBBYEFNb4JitHICVpWAmBD8smIM2jvPtFMB8GA1UdIwQYMBaAFBPF9Ys9RFYh\nOdHT2c03/rVkAEZjMFoGCCoDBAUGBwgBBE57ImF0dHJzIjp7ImhmLkFmZmlsaWF0\naW9uIjoiIiwiaGYuRW5yb2xsbWVudElEIjoiZGJwZWVyfSIsImhmLlR5cGUiOiJj\nbGllbnQifX0wCgYIKoZIzj0EAwIDRwAwRAIgHnFdA1YK+K+QeBL4yYCf0Hw73qwq\nz9bA641mOEg3DdACIGDdnt2QavXbaip5pxsqb9gqkg7mVBEORbE6/WTGw/2A\n-----END CERTIFICATE-----\n"} -------------------------------------------------------------------------------- /_test/release/chaincode/server/connection.json: -------------------------------------------------------------------------------- 1 | {"address":"cc.example.com:9999","dial_timeout":"10s","tls_required":false,"client_auth_required":false,"root_cert":"-----BEGIN CERTIFICATE-----\nMIICFDCCAbqgAwIBAgIUfBkPeOyUJPKoI1sUinUkdtin6JYwCgYIKoZIzj0EAwIw\naDELMAkGA1UEBhMCVVMxFzAVBgNVBAgTDk5vcnRoIENhcm9saW5hMRQwEgYDVQQK\nEwtIeXBlcmxlZGdlcjEPMA0GA1UECxMGRmFicmljMRkwFwYDVQQDExBkaWdpYmFu\na2NhLXRsc2NhMB4XDTIxMDYwNzA4MDQwMFoXDTM2MDYwMzA4MDQwMFowaDELMAkG\nA1UEBhMCVVMxFzAVBgNVBAgTDk5vcnRoIENhcm9saW5hMRQwEgYDVQQKEwtIeXBl\ncmxlZGdlcjEPMA0GA1UECxMGRmFicmljMRkwFwYDVQQDExBkaWdpYmFua2NhLXRs\nc2NhMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEImThmXShCC22x+E3WSebng9e\nojjd7UnBCaJ/w67OVeLbBetQTAkcABh/ObHLCc3lMiGXWt+FYO/kTZ83JDPFlKNC\nMEAwDgYDVR0PAQH/BAQDAgEGMA8GA1UdEwEB/wQFMAMBAf8wHQYDVR0OBBYEFBPF\n9Ys9RFYhOdHT2c03/rVkAEZjMAoGCCqGSM49BAMCA0gAMEUCIQCijYIQi0I6N/WH\nJNc8X5U9SUk/DZ+lEgiImgdickWIwAIgffnk39DOK2DdI0BqGWpM83/22X5Pjgk2\nYBH+FfegT7c=\n-----END CERTIFICATE-----\n","client_key":"-----BEGIN PRIVATE KEY-----\nMIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgIcu+Lj7+PFEmtGyC\nwVk7FzLwyap1dQ+RUEpMA5Q6+VehRANCAAS/Tj6gJ17iv3dbn1/a8/9nsgRQGPwf\nWTc3FaeldNbqhQcAMVTB4cEP/DMQmCHFy5GJ+m103msTNIEzZJuCh+6u\n-----END PRIVATE KEY-----\n","client_cert":"-----BEGIN CERTIFICATE-----\nMIICaTCCAhCgAwIBAgIUKovVcBJlc5OQw1rG/vNTueJ/pK4wCgYIKoZIzj0EAwIw\naDELMAkGA1UEBhMCVVMxFzAVBgNVBAgTDk5vcnRoIENhcm9saW5hMRQwEgYDVQQK\nEwtIeXBlcmxlZGdlcjEPMA0GA1UECxMGRmFicmljMRkwFwYDVQQDExBkaWdpYmFu\na2NhLXRsc2NhMB4XDTIxMDYwNzA4MjQwMFoXDTM2MDYwMzA4MDQwMFowIzEPMA0G\nA1UECxMGY2xpZW50MRAwDgYDVQQDDAdkYnBlZXJ9MFkwEwYHKoZIzj0CAQYIKoZI\nzj0DAQcDQgAEv04+oCde4r93W59f2vP/Z7IEUBj8H1k3NxWnpXTW6oUHADFUweHB\nD/wzEJghxcuRifptdN5rEzSBM2SbgofurqOB3DCB2TAOBgNVHQ8BAf8EBAMCA6gw\nHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMCMAwGA1UdEwEB/wQCMAAwHQYD\nVR0OBBYEFNb4JitHICVpWAmBD8smIM2jvPtFMB8GA1UdIwQYMBaAFBPF9Ys9RFYh\nOdHT2c03/rVkAEZjMFoGCCoDBAUGBwgBBE57ImF0dHJzIjp7ImhmLkFmZmlsaWF0\naW9uIjoiIiwiaGYuRW5yb2xsbWVudElEIjoiZGJwZWVyfSIsImhmLlR5cGUiOiJj\nbGllbnQifX0wCgYIKoZIzj0EAwIDRwAwRAIgHnFdA1YK+K+QeBL4yYCf0Hw73qwq\nz9bA641mOEg3DdACIGDdnt2QavXbaip5pxsqb9gqkg7mVBEORbE6/WTGw/2A\n-----END CERTIFICATE-----\n"} -------------------------------------------------------------------------------- /_test/src/connection.json: -------------------------------------------------------------------------------- 1 | { 2 | "address": "cc.example.com:9999", 3 | "dial_timeout":"10s", 4 | "tls_required": false, 5 | "client_auth_required": false, 6 | "root_cert": "-----BEGIN CERTIFICATE-----\nMIICFDCCAbqgAwIBAgIUfBkPeOyUJPKoI1sUinUkdtin6JYwCgYIKoZIzj0EAwIw\naDELMAkGA1UEBhMCVVMxFzAVBgNVBAgTDk5vcnRoIENhcm9saW5hMRQwEgYDVQQK\nEwtIeXBlcmxlZGdlcjEPMA0GA1UECxMGRmFicmljMRkwFwYDVQQDExBkaWdpYmFu\na2NhLXRsc2NhMB4XDTIxMDYwNzA4MDQwMFoXDTM2MDYwMzA4MDQwMFowaDELMAkG\nA1UEBhMCVVMxFzAVBgNVBAgTDk5vcnRoIENhcm9saW5hMRQwEgYDVQQKEwtIeXBl\ncmxlZGdlcjEPMA0GA1UECxMGRmFicmljMRkwFwYDVQQDExBkaWdpYmFua2NhLXRs\nc2NhMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEImThmXShCC22x+E3WSebng9e\nojjd7UnBCaJ/w67OVeLbBetQTAkcABh/ObHLCc3lMiGXWt+FYO/kTZ83JDPFlKNC\nMEAwDgYDVR0PAQH/BAQDAgEGMA8GA1UdEwEB/wQFMAMBAf8wHQYDVR0OBBYEFBPF\n9Ys9RFYhOdHT2c03/rVkAEZjMAoGCCqGSM49BAMCA0gAMEUCIQCijYIQi0I6N/WH\nJNc8X5U9SUk/DZ+lEgiImgdickWIwAIgffnk39DOK2DdI0BqGWpM83/22X5Pjgk2\nYBH+FfegT7c=\n-----END CERTIFICATE-----\n", 7 | "client_key": "-----BEGIN PRIVATE KEY-----\nMIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgIcu+Lj7+PFEmtGyC\nwVk7FzLwyap1dQ+RUEpMA5Q6+VehRANCAAS/Tj6gJ17iv3dbn1/a8/9nsgRQGPwf\nWTc3FaeldNbqhQcAMVTB4cEP/DMQmCHFy5GJ+m103msTNIEzZJuCh+6u\n-----END PRIVATE KEY-----\n", 8 | "client_cert": "-----BEGIN CERTIFICATE-----\nMIICaTCCAhCgAwIBAgIUKovVcBJlc5OQw1rG/vNTueJ/pK4wCgYIKoZIzj0EAwIw\naDELMAkGA1UEBhMCVVMxFzAVBgNVBAgTDk5vcnRoIENhcm9saW5hMRQwEgYDVQQK\nEwtIeXBlcmxlZGdlcjEPMA0GA1UECxMGRmFicmljMRkwFwYDVQQDExBkaWdpYmFu\na2NhLXRsc2NhMB4XDTIxMDYwNzA4MjQwMFoXDTM2MDYwMzA4MDQwMFowIzEPMA0G\nA1UECxMGY2xpZW50MRAwDgYDVQQDDAdkYnBlZXJ9MFkwEwYHKoZIzj0CAQYIKoZI\nzj0DAQcDQgAEv04+oCde4r93W59f2vP/Z7IEUBj8H1k3NxWnpXTW6oUHADFUweHB\nD/wzEJghxcuRifptdN5rEzSBM2SbgofurqOB3DCB2TAOBgNVHQ8BAf8EBAMCA6gw\nHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMCMAwGA1UdEwEB/wQCMAAwHQYD\nVR0OBBYEFNb4JitHICVpWAmBD8smIM2jvPtFMB8GA1UdIwQYMBaAFBPF9Ys9RFYh\nOdHT2c03/rVkAEZjMFoGCCoDBAUGBwgBBE57ImF0dHJzIjp7ImhmLkFmZmlsaWF0\naW9uIjoiIiwiaGYuRW5yb2xsbWVudElEIjoiZGJwZWVyfSIsImhmLlR5cGUiOiJj\nbGllbnQifX0wCgYIKoZIzj0EAwIDRwAwRAIgHnFdA1YK+K+QeBL4yYCf0Hw73qwq\nz9bA641mOEg3DdACIGDdnt2QavXbaip5pxsqb9gqkg7mVBEORbE6/WTGw/2A\n-----END CERTIFICATE-----\n" 9 | } 10 | -------------------------------------------------------------------------------- /cmd/release/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "io" 6 | "log" 7 | "os" 8 | "path/filepath" 9 | 10 | "github.com/otiai10/copy" 11 | ) 12 | 13 | var logger = log.New(os.Stderr, "", 0) 14 | 15 | type ChaincodeMetadata struct { 16 | Type string `json:"type"` 17 | } 18 | 19 | type Connection struct { 20 | Address string `json:"address"` 21 | DialTimeout string `json:"dial_timeout"` 22 | TLS bool `json:"tls_required"` 23 | ClientAuth bool `json:"client_auth_required"` 24 | RootCert string `json:"root_cert"` 25 | ClientKey string `json:"client_key"` 26 | ClientCert string `json:"client_cert"` 27 | } 28 | 29 | func main() { 30 | logger.Println("::Build") 31 | 32 | if err := run(); err != nil { 33 | logger.Printf("::Error: %v\n", err) 34 | os.Exit(1) 35 | } else { 36 | logger.Printf("::Type detected as external") 37 | } 38 | } 39 | 40 | func run() error { 41 | builderOutputDir, releaseDir := os.Args[1], os.Args[2] 42 | connectionSrcFile := filepath.Join(builderOutputDir, "/connection.json") 43 | 44 | connectionDir := filepath.Join(releaseDir, "chaincode/server/") 45 | connectionDestFile := filepath.Join(releaseDir, "chaincode/server/connection.json") 46 | 47 | metadataDir := filepath.Join(builderOutputDir, "META-INF/statedb") 48 | metadataDestDir := filepath.Join(releaseDir, "statedb") 49 | if _, err := os.Stat(metadataDir); !os.IsNotExist(err) { 50 | if err := copy.Copy(metadataDir, metadataDestDir); err != nil { 51 | return fmt.Errorf("failed to copy metadataDir directory folder: %s", err) 52 | } 53 | } 54 | 55 | // Process and update the connections file 56 | _, err := os.Stat(connectionSrcFile) 57 | if err != nil { 58 | return fmt.Errorf("connection.json not found in source folder: %s", err) 59 | } 60 | 61 | err = os.MkdirAll(connectionDir, 0750) 62 | if err != nil { 63 | return fmt.Errorf("failed to create target folder for connection.json: %s", err) 64 | } 65 | 66 | if err = Copy(connectionSrcFile, connectionDestFile); err != nil { 67 | return err 68 | } 69 | 70 | return nil 71 | 72 | } 73 | 74 | // Copy the src file to dst. Any existing file will be overwritten and will not 75 | // copy file attributes. 76 | func Copy(src, dst string) error { 77 | in, err := os.Open(src) 78 | if err != nil { 79 | return err 80 | } 81 | defer in.Close() 82 | 83 | out, err := os.Create(dst) 84 | if err != nil { 85 | return err 86 | } 87 | defer out.Close() 88 | 89 | _, err = io.Copy(out, in) 90 | if err != nil { 91 | return err 92 | } 93 | return out.Close() 94 | } 95 | 96 | func updateConnectionData(metadata *Connection) error { 97 | // do nothing for the moment 98 | return nil 99 | } 100 | -------------------------------------------------------------------------------- /cmd/build/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "io/ioutil" 7 | "log" 8 | "os" 9 | "path/filepath" 10 | "strings" 11 | 12 | "github.com/otiai10/copy" 13 | ) 14 | 15 | var logger = log.New(os.Stderr, "", 0) 16 | 17 | type ChaincodeMetadata struct { 18 | Type string `json:"type"` 19 | } 20 | 21 | type Connection struct { 22 | Address string `json:"address"` 23 | DialTimeout string `json:"dial_timeout"` 24 | TLS bool `json:"tls_required"` 25 | ClientAuth bool `json:"client_auth_required"` 26 | RootCert string `json:"root_cert"` 27 | ClientKey string `json:"client_key"` 28 | ClientCert string `json:"client_cert"` 29 | } 30 | 31 | func main() { 32 | logger.Println("::Build") 33 | 34 | if err := run(); err != nil { 35 | logger.Printf("::Error: %v\n", err) 36 | os.Exit(1) 37 | } else { 38 | logger.Printf("::Type detected as external") 39 | } 40 | } 41 | 42 | func run() error { 43 | sourceDir, metadataDir, outputDir := os.Args[1], os.Args[2], os.Args[3] 44 | connectionSrcFile := filepath.Join(sourceDir, "/connection.json") 45 | metadataFile := filepath.Clean(filepath.Join(metadataDir, "metadata.json")) 46 | connectionDestFile := filepath.Join(outputDir, "/connection.json") 47 | metainfoSrcDir := filepath.Join(sourceDir, "META-INF") 48 | metainfoDestDir := filepath.Join(outputDir, "META-INF") 49 | // Process and check the metadata file, then copy to the output location 50 | metadataFileContents, err := ioutil.ReadFile(metadataFile) 51 | if err != nil { 52 | return err 53 | } 54 | 55 | var metadata ChaincodeMetadata 56 | if err := json.Unmarshal(metadataFileContents, &metadata); err != nil { 57 | return err 58 | } 59 | 60 | if strings.ToLower(metadata.Type) != "external" { 61 | return fmt.Errorf("chaincode type should be external, it is %s", metadata.Type) 62 | } 63 | 64 | if err := copy.Copy(metadataDir, outputDir); err != nil { 65 | return fmt.Errorf("failed to copy build metadata folder: %s", err) 66 | } 67 | 68 | if _, err := os.Stat(metainfoSrcDir); !os.IsNotExist(err) { 69 | if err := copy.Copy(metainfoSrcDir, metainfoDestDir); err != nil { 70 | return fmt.Errorf("failed to copy build META-INF folder: %s", err) 71 | } 72 | } 73 | 74 | // Process and update the connections file 75 | fileInfo, err := os.Stat(connectionSrcFile) 76 | if err != nil { 77 | return fmt.Errorf("connection.json not found in source folder: %s", err) 78 | } 79 | 80 | connectionFileContents, err := ioutil.ReadFile(connectionSrcFile) 81 | if err != nil { 82 | return err 83 | } 84 | 85 | var connectionData Connection 86 | if err := json.Unmarshal(connectionFileContents, &connectionData); err != nil { 87 | return err 88 | } 89 | 90 | if err := updateConnectionData(&connectionData); err != nil { 91 | return err 92 | } 93 | 94 | updatedConnectionBytes, err := json.Marshal(connectionData) 95 | if err != nil { 96 | return fmt.Errorf("failed to marshal updated connection.json file: %s", err) 97 | } 98 | 99 | err = ioutil.WriteFile(connectionDestFile, updatedConnectionBytes, fileInfo.Mode()) 100 | if err != nil { 101 | return err 102 | } 103 | 104 | return nil 105 | 106 | } 107 | 108 | func updateConnectionData(metadata *Connection) error { 109 | // do nothing for the moment 110 | return nil 111 | } 112 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # fabric-ccs-builder 2 | 3 | **Archived:** This project has now been archived. A [chaincode as a service (CCaaS) builder](https://github.com/hyperledger/fabric/tree/main/ccaas_builder) is now available in the main fabric repository. 4 | 5 | ## Context 6 | 7 | The existing model for chaincode within Fabric, is based around the peer orchestrating the creation and starting of a docker image. Given a set of code, the peer would create a suitable docker container with this code and then start the container. The peer would supply and TLS Certificates, information about the identitiy of the chaincode and the address of the peer. The chaincode library then connects, via a long running gRPC connection, to the peer to 'register'. Transaction requests are then sent from the peer to the chaincode. 8 | 9 | 'out-of-the-box' Fabric can understand and create docker images for Java, Node and Go chaincode. The 'Chaincode Builder' interface within Fabric allows you to define a 'builder' that would allow the user to configure a set of scripts or binaries that know how to build and run your choice of chaincode. With this model, the chaincode is in a client role, and needs to connect to the peer to register. The builder code also needs to be able to control where the chaincode runs. 10 | 11 | With 'as-service' approach, the roles are swapped. The chaincode that is installed on the peer is a definition of where the chaincode is (host/port/tls certs etc), and any CouchDB indexes needed. If this is say a k8s enviroment, the chaincode needs to be started 'as-a-service' (sometimes called as-a-server). When the peer needs to send a transaction to the chaincode, the peer connects (in a client role) to the chaincode (in a server role). From this point onwards the communication between the peer/chaincode is indentical to all other cases. 12 | 13 | ### References 14 | 15 | - [Chaincode Builder Model](https://hyperledger-fabric.readthedocs.io/en/release-2.2/cc_launcher.html) 16 | 17 | - [Chaincode as a Service](https://hyperledger-fabric.readthedocs.io/en/release-2.2/cc_service.html) 18 | 19 | 20 | ## This repo 21 | 22 | The documentation (above) contains references and examples of how you can use bash scripts to setup this type of environment. However the usual docker images for Hyperledger Fabric don't contain BASH. 23 | 24 | Golang code is more readily and reliably run the peer, and this repo contains the go binaries to do exactly this. 25 | 26 | 27 | ## Docker Image 28 | 29 | Build a docker image embedding the `build`, `release`, and `detect` binaries into the /go/bin directory: 30 | 31 | ```shell 32 | docker build \ 33 | -t hyperledgendary/fabric-ccs-builder \ 34 | . 35 | ``` 36 | 37 | ## Peer Configuration 38 | 39 | For Fabric networks running on Kubernetes, the ccs-builder image may be used by a sidecar or init container to load the external builder routines into pods running the `hyperledger/fabric-peer`. For example, the registration of an external builder in core.yaml: 40 | 41 | ```yaml 42 | externalBuilders: 43 | - path: /var/hyperledger/fabric/chaincode/ccs-builder 44 | name: ccs-builder 45 | propagateEnvironment: 46 | - HOME 47 | - CORE_PEER_ID 48 | - CORE_PEER_LOCALMSPID 49 | ``` 50 | 51 | may be fulfilled by copying the binaries off of this image into the peer container at init time: 52 | 53 | ```yaml 54 | --- 55 | apiVersion: apps/v1 56 | kind: Deployment 57 | metadata: 58 | name: org1-peer1 59 | spec: 60 | replicas: 1 61 | selector: 62 | matchLabels: 63 | app: org1-peer1 64 | template: 65 | metadata: 66 | labels: 67 | app: org1-peer1 68 | spec: 69 | containers: 70 | - name: main 71 | image: hyperledger/fabric-peer:{{FABRIC_VERSION}} 72 | imagePullPolicy: IfNotPresent 73 | envFrom: 74 | - configMapRef: 75 | name: org1-peer1-config 76 | ports: 77 | - containerPort: 7051 78 | - containerPort: 7052 79 | - containerPort: 9443 80 | volumeMounts: 81 | - name: fabric-volume 82 | mountPath: /var/hyperledger 83 | - name: fabric-config 84 | mountPath: /var/hyperledger/fabric/config 85 | - name: ccs-builder 86 | mountPath: /var/hyperledger/fabric/chaincode/ccs-builder/bin 87 | 88 | # load the external chaincode builder into the peer image prior to peer launch. 89 | initContainers: 90 | - name: fabric-ccs-builder 91 | image: hyperledgendary/fabric-ccs-builder 92 | imagePullPolicy: IfNotPresent 93 | command: [sh, -c] 94 | args: ["cp /go/bin/* /var/hyperledger/fabric/chaincode/ccs-builder/bin/"] 95 | volumeMounts: 96 | - name: ccs-builder 97 | mountPath: /var/hyperledger/fabric/chaincode/ccs-builder/bin 98 | 99 | volumes: 100 | - name: fabric-volume 101 | persistentVolumeClaim: 102 | claimName: fabric-org1 103 | - name: fabric-config 104 | configMap: 105 | name: org1-config 106 | - name: ccs-builder 107 | emptyDir: {} 108 | 109 | ``` 110 | 111 | 112 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------