github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/core/container/util/dockerutil.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 util
    18  
    19  import (
    20  	"runtime"
    21  	"strings"
    22  
    23  	docker "github.com/fsouza/go-dockerclient"
    24  	"github.com/hyperledger/fabric/common/metadata"
    25  	"github.com/hyperledger/fabric/core/config"
    26  	"github.com/spf13/viper"
    27  )
    28  
    29  //NewDockerClient creates a docker client
    30  func NewDockerClient() (client *docker.Client, err error) {
    31  	endpoint := viper.GetString("vm.endpoint")
    32  	tlsenabled := viper.GetBool("vm.docker.tls.enabled")
    33  	if tlsenabled {
    34  		cert := config.GetPath("vm.docker.tls.cert.file")
    35  		key := config.GetPath("vm.docker.tls.key.file")
    36  		ca := config.GetPath("vm.docker.tls.ca.file")
    37  		client, err = docker.NewTLSClient(endpoint, cert, key, ca)
    38  	} else {
    39  		client, err = docker.NewClient(endpoint)
    40  	}
    41  	return
    42  }
    43  
    44  // Our docker images retrieve $ARCH via "uname -m", which is typically "x86_64" for, well, x86_64.
    45  // However, GOARCH uses "amd64".  We therefore need to normalize any discrepancies between "uname -m"
    46  // and GOARCH here.
    47  var archRemap = map[string]string{
    48  	"amd64": "x86_64",
    49  }
    50  
    51  func getArch() string {
    52  	if remap, ok := archRemap[runtime.GOARCH]; ok {
    53  		return remap
    54  	} else {
    55  		return runtime.GOARCH
    56  	}
    57  }
    58  
    59  func parseDockerfileTemplate(template string) string {
    60  	r := strings.NewReplacer(
    61  		"$(ARCH)", getArch(),
    62  		"$(PROJECT_VERSION)", metadata.Version,
    63  		"$(BASE_VERSION)", metadata.BaseVersion,
    64  		"$(DOCKER_NS)", metadata.DockerNamespace,
    65  		"$(BASE_DOCKER_NS)", metadata.BaseDockerNamespace)
    66  
    67  	return r.Replace(template)
    68  }
    69  
    70  func GetDockerfileFromConfig(path string) string {
    71  	return parseDockerfileTemplate(viper.GetString(path))
    72  }