/
platforms.go
205 lines (170 loc) · 7.7 KB
/
platforms.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
*/
package platforms
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"strings"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/common/metadata"
"github.com/hyperledger/fabric/core/chaincode/platforms/ccmetadata"
cutil "github.com/hyperledger/fabric/core/container/util"
pb "github.com/hyperledger/fabric/protos/peer"
)
// Interface for validating the specification and and writing the package for
// the given platform
type Platform interface {
Name() string
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
GetMetadataProvider(spec *pb.ChaincodeDeploymentSpec) ccmetadata.MetadataProvider
}
type PackageWriter interface {
Write(name string, payload []byte, tw *tar.Writer) error
}
type PackageWriterWrapper func(name string, payload []byte, tw *tar.Writer) error
func (pw PackageWriterWrapper) Write(name string, payload []byte, tw *tar.Writer) error {
return pw(name, payload, tw)
}
type Registry struct {
Platforms map[string]Platform
PackageWriter PackageWriter
}
var logger = flogging.MustGetLogger("chaincode-platform")
func NewRegistry(platformTypes ...Platform) *Registry {
platforms := make(map[string]Platform)
for _, platform := range platformTypes {
if _, ok := platforms[platform.Name()]; ok {
logger.Panicf("Multiple platforms of the same name specified: %s", platform.Name())
}
platforms[platform.Name()] = platform
}
return &Registry{
Platforms: platforms,
PackageWriter: PackageWriterWrapper(cutil.WriteBytesToPackage),
}
}
func (r *Registry) ValidateSpec(spec *pb.ChaincodeSpec) error {
platform, ok := r.Platforms[spec.Type.String()]
if !ok {
return fmt.Errorf("Unknown chaincodeType: %s", spec.Type)
}
return platform.ValidateSpec(spec)
}
func (r *Registry) ValidateDeploymentSpec(spec *pb.ChaincodeDeploymentSpec) error {
platform, ok := r.Platforms[spec.ChaincodeSpec.Type.String()]
if !ok {
return fmt.Errorf("Unknown chaincodeType: %s", spec.ChaincodeSpec.Type)
}
return platform.ValidateDeploymentSpec(spec)
}
func (r *Registry) GetMetadataProvider(spec *pb.ChaincodeDeploymentSpec) (ccmetadata.MetadataProvider, error) {
platform, ok := r.Platforms[spec.ChaincodeSpec.Type.String()]
if !ok {
return nil, fmt.Errorf("Unknown chaincodeType: %s", spec.ChaincodeSpec.Type)
}
return platform.GetMetadataProvider(spec), nil
}
func (r *Registry) GetDeploymentPayload(spec *pb.ChaincodeSpec) ([]byte, error) {
platform, ok := r.Platforms[spec.Type.String()]
if !ok {
return nil, fmt.Errorf("Unknown chaincodeType: %s", spec.Type)
}
return platform.GetDeploymentPayload(spec)
}
func (r *Registry) GenerateDockerfile(cds *pb.ChaincodeDeploymentSpec) (string, error) {
platform, ok := r.Platforms[cds.ChaincodeSpec.Type.String()]
if !ok {
return "", fmt.Errorf("Unknown chaincodeType: %s", cds.ChaincodeSpec.Type)
}
var buf []string
// ----------------------------------------------------------------------------------------------------
// Let the platform define the base Dockerfile
// ----------------------------------------------------------------------------------------------------
base, err := platform.GenerateDockerfile(cds)
if err != nil {
return "", 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 compared against peer build version
buf = append(buf, fmt.Sprintf("ENV CORE_CHAINCODE_BUILDLEVEL=%s", metadata.Version))
// ----------------------------------------------------------------------------------------------------
// Finalize it
// ----------------------------------------------------------------------------------------------------
contents := strings.Join(buf, "\n")
logger.Debugf("\n%s", contents)
return contents, nil
}
func (r *Registry) StreamDockerBuild(cds *pb.ChaincodeDeploymentSpec, inputFiles map[string][]byte, tw *tar.Writer) error {
var err error
// ----------------------------------------------------------------------------------------------------
// Determine our platform driver from the spec
// ----------------------------------------------------------------------------------------------------
platform, ok := r.Platforms[cds.ChaincodeSpec.Type.String()]
if !ok {
return fmt.Errorf("could not find platform of type: %s", cds.ChaincodeSpec.Type.String())
}
// ----------------------------------------------------------------------------------------------------
// First stream out our static inputFiles
// ----------------------------------------------------------------------------------------------------
for name, data := range inputFiles {
err = r.PackageWriter.Write(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 (r *Registry) GenerateDockerBuild(cds *pb.ChaincodeDeploymentSpec) (io.Reader, error) {
inputFiles := make(map[string][]byte)
// ----------------------------------------------------------------------------------------------------
// Generate the Dockerfile specific to our context
// ----------------------------------------------------------------------------------------------------
dockerFile, err := r.GenerateDockerfile(cds)
if err != nil {
return nil, fmt.Errorf("Failed to generate a Dockerfile: %s", err)
}
inputFiles["Dockerfile"] = []byte(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 := r.StreamDockerBuild(cds, inputFiles, tw)
if err != nil {
logger.Error(err)
}
tw.Close()
gw.Close()
output.CloseWithError(err)
}()
return input, nil
}