forked from hyperledger/fabric
/
instance.go
195 lines (162 loc) · 4.91 KB
/
instance.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package externalbuilder
import (
"encoding/json"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"syscall"
"time"
"github.com/hyperledger/fabric/core/container/ccintf"
"github.com/hyperledger/fabric/internal/pkg/comm"
"github.com/pkg/errors"
)
const (
DialTimeout = 3 * time.Second
CCServerReleaseDir = "chaincode/server"
)
type Instance struct {
PackageID string
BldDir string
ReleaseDir string
Builder *Builder
Session *Session
TermTimeout time.Duration
}
// Duration used for the DialTimeout property
type Duration struct {
time.Duration
}
func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(d.Seconds())
}
func (d *Duration) UnmarshalJSON(b []byte) error {
var v interface{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
switch value := v.(type) {
case float64:
d.Duration = time.Duration(value)
return nil
case string:
var err error
if d.Duration, err = time.ParseDuration(value); err != nil {
return err
}
return nil
default:
return errors.New("invalid duration")
}
}
// ChaincodeServerUserData holds "connection.json" information
type ChaincodeServerUserData struct {
Address string `json:"address"`
DialTimeout Duration `json:"dial_timeout"`
TLSRequired bool `json:"tls_required"`
ClientAuthRequired bool `json:"client_auth_required"`
ClientKey string `json:"client_key"` // PEM encoded client key
ClientCert string `json:"client_cert"` // PEM encoded client certificate
RootCert string `json:"root_cert"` // PEM encoded peer chaincode certificate
}
func (c *ChaincodeServerUserData) ChaincodeServerInfo(cryptoDir string) (*ccintf.ChaincodeServerInfo, error) {
if c.Address == "" {
return nil, errors.New("chaincode address not provided")
}
connInfo := &ccintf.ChaincodeServerInfo{Address: c.Address}
if c.DialTimeout == (Duration{}) {
connInfo.ClientConfig.Timeout = DialTimeout
} else {
connInfo.ClientConfig.Timeout = c.DialTimeout.Duration
}
// we can expose this if necessary
connInfo.ClientConfig.KaOpts = comm.DefaultKeepaliveOptions
if !c.TLSRequired {
return connInfo, nil
}
if c.ClientAuthRequired && c.ClientKey == "" {
return nil, errors.New("chaincode tls key not provided")
}
if c.ClientAuthRequired && c.ClientCert == "" {
return nil, errors.New("chaincode tls cert not provided")
}
if c.RootCert == "" {
return nil, errors.New("chaincode tls root cert not provided")
}
connInfo.ClientConfig.SecOpts.UseTLS = true
if c.ClientAuthRequired {
connInfo.ClientConfig.SecOpts.RequireClientCert = true
connInfo.ClientConfig.SecOpts.Certificate = []byte(c.ClientCert)
connInfo.ClientConfig.SecOpts.Key = []byte(c.ClientKey)
}
connInfo.ClientConfig.SecOpts.ServerRootCAs = [][]byte{[]byte(c.RootCert)}
return connInfo, nil
}
func (i *Instance) ChaincodeServerReleaseDir() string {
return filepath.Join(i.ReleaseDir, CCServerReleaseDir)
}
func (i *Instance) ChaincodeServerInfo() (*ccintf.ChaincodeServerInfo, error) {
ccinfoPath := filepath.Join(i.ChaincodeServerReleaseDir(), "connection.json")
_, err := os.Stat(ccinfoPath)
if os.IsNotExist(err) {
return nil, nil
}
if err != nil {
return nil, errors.WithMessage(err, "connection information not provided")
}
b, err := ioutil.ReadFile(ccinfoPath)
if err != nil {
return nil, errors.WithMessagef(err, "could not read '%s' for chaincode info", ccinfoPath)
}
ccdata := &ChaincodeServerUserData{}
err = json.Unmarshal(b, &ccdata)
if err != nil {
return nil, errors.WithMessagef(err, "malformed chaincode info at '%s'", ccinfoPath)
}
return ccdata.ChaincodeServerInfo(i.ChaincodeServerReleaseDir())
}
func (i *Instance) Start(peerConnection *ccintf.PeerConnection) error {
sess, err := i.Builder.Run(i.PackageID, i.BldDir, peerConnection)
if err != nil {
return errors.WithMessage(err, "could not execute run")
}
i.Session = sess
return nil
}
// Stop signals the process to terminate with SIGTERM. If the process doesn't
// terminate within TermTimeout, the process is killed with SIGKILL.
func (i *Instance) Stop() error {
if i.Session == nil {
return errors.Errorf("instance has not been started")
}
done := make(chan struct{})
go func() { i.Wait(); close(done) }()
i.Session.Signal(syscall.SIGTERM)
select {
case <-time.After(i.TermTimeout):
i.Session.Signal(syscall.SIGKILL)
case <-done:
return nil
}
select {
case <-time.After(5 * time.Second):
return errors.Errorf("failed to stop instance '%s'", i.PackageID)
case <-done:
return nil
}
}
func (i *Instance) Wait() (int, error) {
if i.Session == nil {
return -1, errors.Errorf("instance was not successfully started")
}
err := i.Session.Wait()
err = errors.Wrapf(err, "builder '%s' run failed", i.Builder.Name)
if exitErr, ok := errors.Cause(err).(*exec.ExitError); ok {
return exitErr.ExitCode(), err
}
return 0, err
}