|
| 1 | +/* |
| 2 | +Copyright IBM Corp. 2016-2017 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 chaincode |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "io/ioutil" |
| 22 | + "os" |
| 23 | + |
| 24 | + "github.com/hyperledger/fabric/peer/common" |
| 25 | + |
| 26 | + "github.com/golang/protobuf/proto" |
| 27 | + "github.com/spf13/cobra" |
| 28 | + "github.com/spf13/viper" |
| 29 | +) |
| 30 | + |
| 31 | +var chaincodeInstallCmd *cobra.Command |
| 32 | + |
| 33 | +// installCmd returns the cobra command for Chaincode Deploy |
| 34 | +func installCmd(cf *ChaincodeCmdFactory) *cobra.Command { |
| 35 | + chaincodeInstallCmd = &cobra.Command{ |
| 36 | + Use: "install", |
| 37 | + Short: fmt.Sprintf("Package the specified chaincode into a deployment spec and save it on the peer's path."), |
| 38 | + Long: fmt.Sprintf(`Package the specified chaincode into a deployment spec and save it on the peer's path.`), |
| 39 | + ValidArgs: []string{"1"}, |
| 40 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 41 | + return chaincodeInstall(cmd, args, cf) |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + return chaincodeInstallCmd |
| 46 | +} |
| 47 | + |
| 48 | +func createCCInstallPath(path string) (string, error) { |
| 49 | + if _, err := os.Stat(path); err != nil { |
| 50 | + return "", err |
| 51 | + } |
| 52 | + chaincodePath := path + "/chaincodes" |
| 53 | + if s, err := os.Stat(chaincodePath); err != nil { |
| 54 | + if os.IsNotExist(err) { |
| 55 | + if err := os.Mkdir(chaincodePath, 0755); err != nil { |
| 56 | + return "", err |
| 57 | + } |
| 58 | + return chaincodePath, nil |
| 59 | + } |
| 60 | + return "", err |
| 61 | + } else if !s.IsDir() { |
| 62 | + return "", fmt.Errorf("chaincode path exists but not a dir: %s", chaincodePath) |
| 63 | + } |
| 64 | + |
| 65 | + return chaincodePath, nil |
| 66 | +} |
| 67 | + |
| 68 | +func packageCC(chaincodeBin []byte) ([]byte, error) { |
| 69 | + //TODO create proper, secured package, for now return chaincode binary asis |
| 70 | + return chaincodeBin, nil |
| 71 | +} |
| 72 | + |
| 73 | +func installCC(path string, bin []byte) error { |
| 74 | + if _, err := os.Stat(path); err == nil || !os.IsNotExist(err) { |
| 75 | + return fmt.Errorf("chaincode %s exists", path) |
| 76 | + } |
| 77 | + |
| 78 | + if err := ioutil.WriteFile(path, bin, 0644); err != nil { |
| 79 | + logger.Errorf("Failed writing deployment spec to file [%s]: [%s]", path, err) |
| 80 | + return err |
| 81 | + } |
| 82 | + |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +// chaincodeInstall deploys the chaincode. On success, the chaincode name |
| 87 | +// (hash) is printed to STDOUT for use by subsequent chaincode-related CLI |
| 88 | +// commands. |
| 89 | +func chaincodeInstall(cmd *cobra.Command, args []string, cf *ChaincodeCmdFactory) error { |
| 90 | + if chaincodePath == common.UndefinedParamValue || chaincodeVersion == common.UndefinedParamValue { |
| 91 | + return fmt.Errorf("Must supply value for %s path and version parameters.\n", chainFuncName) |
| 92 | + } |
| 93 | + |
| 94 | + peerPath := viper.GetString("peer.fileSystemPath") |
| 95 | + if peerPath == "" { |
| 96 | + return fmt.Errorf("Peer's environment \"peer.fileSystemPath\" is not set") |
| 97 | + } |
| 98 | + |
| 99 | + var ccpath string |
| 100 | + var err error |
| 101 | + |
| 102 | + //create the chaincodes dir if necessary |
| 103 | + if ccpath, err = createCCInstallPath(peerPath); err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + |
| 107 | + //check if chaincode already exists |
| 108 | + fileToWrite := ccpath + "/" + chaincodeName + "." + chaincodeVersion |
| 109 | + if _, err := os.Stat(fileToWrite); err == nil || !os.IsNotExist(err) { |
| 110 | + return fmt.Errorf("chaincode %s exists", fileToWrite) |
| 111 | + } |
| 112 | + |
| 113 | + spec, err := getChaincodeSpecification(cmd) |
| 114 | + if err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + |
| 118 | + cds, err := getChaincodeBytes(spec) |
| 119 | + if err != nil { |
| 120 | + return fmt.Errorf("Error getting chaincode code %s: %s", chainFuncName, err) |
| 121 | + } |
| 122 | + |
| 123 | + cdsBytes, err := proto.Marshal(cds) |
| 124 | + if err != nil { |
| 125 | + return fmt.Errorf("Error marshalling chaincode deployment spec : %s", err) |
| 126 | + } |
| 127 | + |
| 128 | + //TODO - packageCC is just a stub. It needs to be filled out with other items |
| 129 | + //such as serialized policy and has to be signed. |
| 130 | + pkgBytes, err := packageCC(cdsBytes) |
| 131 | + if err != nil { |
| 132 | + logger.Errorf("Failed creating package [%s]", err) |
| 133 | + return err |
| 134 | + } |
| 135 | + |
| 136 | + err = installCC(fileToWrite, pkgBytes) |
| 137 | + if err != nil { |
| 138 | + logger.Errorf("Failed writing deployment spec to file [%s]: [%s]", fileToWrite, err) |
| 139 | + return err |
| 140 | + } |
| 141 | + |
| 142 | + logger.Debugf("Installed chaincode (%s,%s) of size <%d>", chaincodeName, chaincodeVersion, len(cdsBytes)) |
| 143 | + return err |
| 144 | +} |
0 commit comments