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