1 Star 0 Fork 0

13683679291/fabric

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
platforms.go 8.51 KB
一键复制 编辑 原始数据 按行查看 历史
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package platforms
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"os"
"strings"
"io/ioutil"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/common/metadata"
"github.com/hyperledger/fabric/core/chaincode/platforms/car"
"github.com/hyperledger/fabric/core/chaincode/platforms/golang"
"github.com/hyperledger/fabric/core/chaincode/platforms/java"
"github.com/hyperledger/fabric/core/config"
cutil "github.com/hyperledger/fabric/core/container/util"
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/spf13/viper"
)
// Interface for validating the specification and and writing the package for
// the given platform
type Platform interface {
ValidateSpec(spec *pb.ChaincodeSpec) error
ValidateDeploymentSpec(spec *pb.ChaincodeDeploymentSpec) error
GetDeploymentPayload(spec *pb.ChaincodeSpec) ([]byte, error)
GenerateDockerfile(spec *pb.ChaincodeDeploymentSpec) (string, error)
GenerateDockerBuild(spec *pb.ChaincodeDeploymentSpec, tw *tar.Writer) error
}
var logger = flogging.MustGetLogger("chaincode-platform")
// Find returns the platform interface for the given platform type
func Find(chaincodeType pb.ChaincodeSpec_Type) (Platform, error) {
switch chaincodeType {
case pb.ChaincodeSpec_GOLANG:
return &golang.Platform{}, nil
case pb.ChaincodeSpec_CAR:
return &car.Platform{}, nil
case pb.ChaincodeSpec_JAVA:
return &java.Platform{}, nil
default:
return nil, fmt.Errorf("Unknown chaincodeType: %s", chaincodeType)
}
}
func GetDeploymentPayload(spec *pb.ChaincodeSpec) ([]byte, error) {
platform, err := Find(spec.Type)
if err != nil {
return nil, err
}
return platform.GetDeploymentPayload(spec)
}
func getPeerTLSCert() ([]byte, error) {
if viper.GetBool("peer.tls.enabled") == false {
// no need for certificates if TLS is not enabled
return nil, nil
}
var path string
// first we check for the rootcert
path = config.GetPath("peer.tls.rootcert.file")
if path == "" {
// check for tls cert
path = config.GetPath("peer.tls.cert.file")
}
// this should not happen if the peer is running with TLS enabled
if _, err := os.Stat(path); err != nil {
return nil, err
}
// FIXME: FAB-2037 - ensure we sanely resolve relative paths specified in the yaml
return ioutil.ReadFile(path)
}
func generateDockerfile(platform Platform, cds *pb.ChaincodeDeploymentSpec, tls bool) ([]byte, error) {
var buf []string
// ----------------------------------------------------------------------------------------------------
// Let the platform define the base Dockerfile
// ----------------------------------------------------------------------------------------------------
base, err := platform.GenerateDockerfile(cds)
if err != nil {
return nil, fmt.Errorf("Failed to generate platform-specific Dockerfile: %s", err)
}
buf = append(buf, base)
// ----------------------------------------------------------------------------------------------------
// Add some handy labels
// ----------------------------------------------------------------------------------------------------
buf = append(buf, fmt.Sprintf("LABEL %s.chaincode.id.name=\"%s\" \\", metadata.BaseDockerLabel, cds.ChaincodeSpec.ChaincodeId.Name))
buf = append(buf, fmt.Sprintf(" %s.chaincode.id.version=\"%s\" \\", metadata.BaseDockerLabel, cds.ChaincodeSpec.ChaincodeId.Version))
buf = append(buf, fmt.Sprintf(" %s.chaincode.type=\"%s\" \\", metadata.BaseDockerLabel, cds.ChaincodeSpec.Type.String()))
buf = append(buf, fmt.Sprintf(" %s.version=\"%s\" \\", metadata.BaseDockerLabel, metadata.Version))
buf = append(buf, fmt.Sprintf(" %s.base.version=\"%s\"", metadata.BaseDockerLabel, metadata.BaseVersion))
// ----------------------------------------------------------------------------------------------------
// Then augment it with any general options
// ----------------------------------------------------------------------------------------------------
//append version so chaincode build version can be campared against peer build version
buf = append(buf, fmt.Sprintf("ENV CORE_CHAINCODE_BUILDLEVEL=%s", metadata.Version))
if tls {
const guestTLSPath = "/etc/hyperledger/fabric/peer.crt"
buf = append(buf, "ENV CORE_PEER_TLS_ROOTCERT_FILE="+guestTLSPath)
buf = append(buf, "COPY peer.crt "+guestTLSPath)
}
// ----------------------------------------------------------------------------------------------------
// Finalize it
// ----------------------------------------------------------------------------------------------------
contents := strings.Join(buf, "\n")
logger.Debugf("\n%s", contents)
return []byte(contents), nil
}
type InputFiles map[string][]byte
func generateDockerBuild(platform Platform, cds *pb.ChaincodeDeploymentSpec, inputFiles InputFiles, tw *tar.Writer) error {
var err error
// ----------------------------------------------------------------------------------------------------
// First stream out our static inputFiles
// ----------------------------------------------------------------------------------------------------
for name, data := range inputFiles {
err = cutil.WriteBytesToPackage(name, data, tw)
if err != nil {
return fmt.Errorf("Failed to inject \"%s\": %s", name, err)
}
}
// ----------------------------------------------------------------------------------------------------
// Now give the platform an opportunity to contribute its own context to the build
// ----------------------------------------------------------------------------------------------------
err = platform.GenerateDockerBuild(cds, tw)
if err != nil {
return fmt.Errorf("Failed to generate platform-specific docker build: %s", err)
}
return nil
}
func GenerateDockerBuild(cds *pb.ChaincodeDeploymentSpec) (io.Reader, error) {
inputFiles := make(InputFiles)
// ----------------------------------------------------------------------------------------------------
// Determine our platform driver from the spec
// ----------------------------------------------------------------------------------------------------
platform, err := Find(cds.ChaincodeSpec.Type)
if err != nil {
return nil, fmt.Errorf("Failed to determine platform type: %s", err)
}
// ----------------------------------------------------------------------------------------------------
// Transfer the peer's TLS certificate to our list of input files, if applicable
// ----------------------------------------------------------------------------------------------------
// NOTE: We bake the peer TLS certificate in at the time we build the chaincode container if a cert is
// found, regardless of whether TLS is enabled or not. The main implication is that if the administrator
// updates the peer cert, the chaincode containers will need to be invalidated and rebuilt.
// We will manage enabling or disabling TLS at container run time via CORE_PEER_TLS_ENABLED
cert, err := getPeerTLSCert()
if err != nil {
return nil, fmt.Errorf("Failed to read the TLS certificate: %s", err)
}
inputFiles["peer.crt"] = cert
// ----------------------------------------------------------------------------------------------------
// Generate the Dockerfile specific to our context
// ----------------------------------------------------------------------------------------------------
dockerFile, err := generateDockerfile(platform, cds, cert != nil)
if err != nil {
return nil, fmt.Errorf("Failed to generate a Dockerfile: %s", err)
}
inputFiles["Dockerfile"] = dockerFile
// ----------------------------------------------------------------------------------------------------
// Finally, launch an asynchronous process to stream all of the above into a docker build context
// ----------------------------------------------------------------------------------------------------
input, output := io.Pipe()
go func() {
gw := gzip.NewWriter(output)
tw := tar.NewWriter(gw)
err := generateDockerBuild(platform, cds, inputFiles, tw)
if err != nil {
logger.Error(err)
}
tw.Close()
gw.Close()
output.CloseWithError(err)
}()
return input, nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mmcro/fabric.git
git@gitee.com:mmcro/fabric.git
mmcro
fabric
fabric
v1.0.6

搜索帮助

0d507c66 1850385 C8b1a773 1850385