github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/core/chaincode/platforms/car/platform.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 car
    18  
    19  import (
    20  	"archive/tar"
    21  	"io/ioutil"
    22  	"strings"
    23  
    24  	"bytes"
    25  	"fmt"
    26  	"io"
    27  
    28  	"github.com/hyperledger/fabric/core/chaincode/platforms/util"
    29  	cutil "github.com/hyperledger/fabric/core/container/util"
    30  	pb "github.com/hyperledger/fabric/protos/peer"
    31  )
    32  
    33  // Platform for the CAR type
    34  type Platform struct {
    35  }
    36  
    37  // ValidateSpec validates the chaincode specification for CAR types to satisfy
    38  // the platform interface.  This chaincode type currently doesn't
    39  // require anything specific so we just implicitly approve any spec
    40  func (carPlatform *Platform) ValidateSpec(spec *pb.ChaincodeSpec) error {
    41  	return nil
    42  }
    43  
    44  func (carPlatform *Platform) ValidateDeploymentSpec(cds *pb.ChaincodeDeploymentSpec) error {
    45  	// CAR platform will validate the code package within chaintool
    46  	return nil
    47  }
    48  
    49  func (carPlatform *Platform) GetDeploymentPayload(spec *pb.ChaincodeSpec) ([]byte, error) {
    50  
    51  	return ioutil.ReadFile(spec.ChaincodeId.Path)
    52  }
    53  
    54  func (carPlatform *Platform) GenerateDockerfile(cds *pb.ChaincodeDeploymentSpec) (string, error) {
    55  
    56  	var buf []string
    57  
    58  	//let the executable's name be chaincode ID's name
    59  	buf = append(buf, "FROM "+cutil.GetDockerfileFromConfig("chaincode.car.runtime"))
    60  	buf = append(buf, "ADD binpackage.tar /usr/local/bin")
    61  
    62  	dockerFileContents := strings.Join(buf, "\n")
    63  
    64  	return dockerFileContents, nil
    65  }
    66  
    67  func (carPlatform *Platform) GenerateDockerBuild(cds *pb.ChaincodeDeploymentSpec, tw *tar.Writer) error {
    68  
    69  	// Bundle the .car file into a tar stream so it may be transferred to the builder container
    70  	codepackage, output := io.Pipe()
    71  	go func() {
    72  		tw := tar.NewWriter(output)
    73  
    74  		err := cutil.WriteBytesToPackage("codepackage.car", cds.CodePackage, tw)
    75  
    76  		tw.Close()
    77  		output.CloseWithError(err)
    78  	}()
    79  
    80  	binpackage := bytes.NewBuffer(nil)
    81  	err := util.DockerBuild(util.DockerBuildOptions{
    82  		Cmd:          "java -jar /usr/local/bin/chaintool buildcar /chaincode/input/codepackage.car -o /chaincode/output/chaincode",
    83  		InputStream:  codepackage,
    84  		OutputStream: binpackage,
    85  	})
    86  	if err != nil {
    87  		return fmt.Errorf("Error building CAR: %s", err)
    88  	}
    89  
    90  	return cutil.WriteBytesToPackage("binpackage.tar", binpackage.Bytes(), tw)
    91  }