github.com/didip/deis@v1.4.1/deisctl/config/config.go (about)

     1  package config
     2  
     3  import (
     4  	"encoding/base64"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"strings"
     8  
     9  	"github.com/deis/deis/deisctl/utils"
    10  )
    11  
    12  // fileKeys define config keys to be read from local files
    13  var fileKeys = []string{
    14  	"/deis/platform/sshPrivateKey",
    15  	"/deis/router/sslCert",
    16  	"/deis/router/sslKey"}
    17  
    18  // b64Keys define config keys to be base64 encoded before stored
    19  var b64Keys = []string{"/deis/platform/sshPrivateKey"}
    20  
    21  // Config runs the config subcommand
    22  func Config(args map[string]interface{}) error {
    23  	return doConfig(args)
    24  }
    25  
    26  // CheckConfig looks for a value at a keyspace path
    27  // and returns an error if a value is not found
    28  func CheckConfig(root string, k string) error {
    29  
    30  	client, err := getEtcdClient()
    31  	if err != nil {
    32  		return err
    33  	}
    34  
    35  	_, err = doConfigGet(client, root, []string{k})
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	return nil
    41  }
    42  
    43  func doConfig(args map[string]interface{}) error {
    44  	client, err := getEtcdClient()
    45  	if err != nil {
    46  		return err
    47  	}
    48  
    49  	rootPath := "/deis/" + args["<target>"].(string) + "/"
    50  
    51  	var vals []string
    52  	if args["set"] == true {
    53  		vals, err = doConfigSet(client, rootPath, args["<key=val>"].([]string))
    54  	} else {
    55  		vals, err = doConfigGet(client, rootPath, args["<key>"].([]string))
    56  	}
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	// print results
    62  	for _, v := range vals {
    63  		fmt.Printf("%v\n", v)
    64  	}
    65  	return nil
    66  }
    67  
    68  func doConfigSet(client *etcdClient, root string, kvs []string) ([]string, error) {
    69  	var result []string
    70  
    71  	for _, kv := range kvs {
    72  
    73  		// split k/v from args
    74  		split := strings.SplitN(kv, "=", 2)
    75  		k, v := split[0], split[1]
    76  
    77  		// prepare path and value
    78  		path := root + k
    79  		val, err := valueForPath(path, v)
    80  		if err != nil {
    81  			return result, err
    82  		}
    83  
    84  		// set key/value in etcd
    85  		ret, err := client.Set(path, val)
    86  		if err != nil {
    87  			return result, err
    88  		}
    89  		result = append(result, ret)
    90  
    91  	}
    92  	return result, nil
    93  }
    94  
    95  func doConfigGet(client *etcdClient, root string, keys []string) ([]string, error) {
    96  	var result []string
    97  	for _, k := range keys {
    98  		val, err := client.Get(root + k)
    99  		if err != nil {
   100  			return result, err
   101  		}
   102  		result = append(result, val)
   103  	}
   104  	return result, nil
   105  }
   106  
   107  // valueForPath returns the canonical value for a user-defined path and value
   108  func valueForPath(path string, v string) (string, error) {
   109  
   110  	// check if path is part of fileKeys
   111  	for _, p := range fileKeys {
   112  
   113  		if path == p {
   114  
   115  			// read value from filesystem
   116  			bytes, err := ioutil.ReadFile(utils.ResolvePath(v))
   117  			if err != nil {
   118  				return "", err
   119  			}
   120  
   121  			// see if we should return base64 encoded value
   122  			for _, pp := range b64Keys {
   123  				if path == pp {
   124  					return base64.StdEncoding.EncodeToString(bytes), nil
   125  				}
   126  			}
   127  
   128  			return string(bytes), nil
   129  		}
   130  	}
   131  
   132  	return v, nil
   133  
   134  }