github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/core/container/vm.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 container
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  
    23  	"golang.org/x/net/context"
    24  
    25  	"github.com/fsouza/go-dockerclient"
    26  	"github.com/hyperledger/fabric/core/chaincode/platforms"
    27  	cutil "github.com/hyperledger/fabric/core/container/util"
    28  	pb "github.com/hyperledger/fabric/protos/peer"
    29  	"github.com/op/go-logging"
    30  )
    31  
    32  // VM implemenation of VM management functionality.
    33  type VM struct {
    34  	Client *docker.Client
    35  }
    36  
    37  // NewVM creates a new VM instance.
    38  func NewVM() (*VM, error) {
    39  	client, err := cutil.NewDockerClient()
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	VM := &VM{Client: client}
    44  	return VM, nil
    45  }
    46  
    47  var vmLogger = logging.MustGetLogger("container")
    48  
    49  // ListImages list the images available
    50  func (vm *VM) ListImages(context context.Context) error {
    51  	imgs, err := vm.Client.ListImages(docker.ListImagesOptions{All: false})
    52  	if err != nil {
    53  		return err
    54  	}
    55  	for _, img := range imgs {
    56  		fmt.Println("ID: ", img.ID)
    57  		fmt.Println("RepoTags: ", img.RepoTags)
    58  		fmt.Println("Created: ", img.Created)
    59  		fmt.Println("Size: ", img.Size)
    60  		fmt.Println("VirtualSize: ", img.VirtualSize)
    61  		fmt.Println("ParentId: ", img.ParentID)
    62  	}
    63  
    64  	return nil
    65  }
    66  
    67  // BuildChaincodeContainer builds the container for the supplied chaincode specification
    68  func (vm *VM) BuildChaincodeContainer(spec *pb.ChaincodeSpec) error {
    69  	codePackage, err := GetChaincodePackageBytes(spec)
    70  	if err != nil {
    71  		return fmt.Errorf("Error getting chaincode package bytes: %s", err)
    72  	}
    73  
    74  	cds := &pb.ChaincodeDeploymentSpec{ChaincodeSpec: spec, CodePackage: codePackage}
    75  	dockerSpec, err := platforms.GenerateDockerBuild(cds)
    76  	if err != nil {
    77  		return fmt.Errorf("Error getting chaincode docker image: %s", err)
    78  	}
    79  
    80  	output := bytes.NewBuffer(nil)
    81  
    82  	err = vm.Client.BuildImage(docker.BuildImageOptions{
    83  		Name:         spec.ChaincodeId.Name,
    84  		InputStream:  dockerSpec,
    85  		OutputStream: output,
    86  	})
    87  	if err != nil {
    88  		return fmt.Errorf("Error building docker: %s (output = %s)", err, output.String())
    89  	}
    90  
    91  	return nil
    92  }
    93  
    94  // GetChaincodePackageBytes creates bytes for docker container generation using the supplied chaincode specification
    95  func GetChaincodePackageBytes(spec *pb.ChaincodeSpec) ([]byte, error) {
    96  	if spec == nil || spec.ChaincodeId == nil {
    97  		return nil, fmt.Errorf("invalid chaincode spec")
    98  	}
    99  
   100  	return platforms.GetDeploymentPayload(spec)
   101  }