github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/agent/format-1.16.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package agent
     5  
     6  import (
     7  	"encoding/base64"
     8  
     9  	"launchpad.net/goyaml"
    10  	"launchpad.net/juju-core/version"
    11  )
    12  
    13  var format_1_16 = formatter_1_16{}
    14  
    15  // formatter_1_16 is the formatter for the 1.16 format.
    16  type formatter_1_16 struct {
    17  }
    18  
    19  // Ensure that the formatter_1_16 struct implements the formatter interface.
    20  var _ formatter = formatter_1_16{}
    21  
    22  // format_1_16Serialization holds information for a given agent.
    23  type format_1_16Serialization struct {
    24  	Tag               string
    25  	Nonce             string
    26  	UpgradedToVersion *version.Number `yaml:"upgradedToVersion"`
    27  
    28  	CACert         string
    29  	StateAddresses []string `yaml:",omitempty"`
    30  	StatePassword  string   `yaml:",omitempty"`
    31  
    32  	APIAddresses []string `yaml:",omitempty"`
    33  	APIPassword  string   `yaml:",omitempty"`
    34  
    35  	OldPassword string
    36  	Values      map[string]string
    37  
    38  	// Only state server machines have these next three items
    39  	StateServerCert string `yaml:",omitempty"`
    40  	StateServerKey  string `yaml:",omitempty"`
    41  	APIPort         int    `yaml:",omitempty"`
    42  }
    43  
    44  func init() {
    45  	registerFormat(format_1_16)
    46  }
    47  
    48  const legacyFormatFilename = "format"
    49  
    50  // legacyFormatPrefix is the prefix of the legacy format file.
    51  const legacyFormatPrefix = "format "
    52  
    53  // decode64 makes sure that for an empty string we have a nil slice, not an
    54  // empty slice, which is what the base64 DecodeString function returns.
    55  func decode64(value string) (result []byte, err error) {
    56  	if value != "" {
    57  		result, err = base64.StdEncoding.DecodeString(value)
    58  	}
    59  	return
    60  }
    61  
    62  func (formatter_1_16) version() string {
    63  	return "1.16"
    64  }
    65  
    66  func (formatter_1_16) unmarshal(data []byte) (*configInternal, error) {
    67  	var format format_1_16Serialization
    68  	if err := goyaml.Unmarshal(data, &format); err != nil {
    69  		return nil, err
    70  	}
    71  	caCert, err := decode64(format.CACert)
    72  	if err != nil {
    73  		return nil, err
    74  	}
    75  	stateServerCert, err := decode64(format.StateServerCert)
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  	stateServerKey, err := decode64(format.StateServerKey)
    80  	if err != nil {
    81  		return nil, err
    82  	}
    83  	if format.UpgradedToVersion == nil {
    84  		// Assume it's 1.16.0.
    85  		upgradedToVersion := version.MustParse("1.16.0")
    86  		format.UpgradedToVersion = &upgradedToVersion
    87  	}
    88  	config := &configInternal{
    89  		tag:               format.Tag,
    90  		nonce:             format.Nonce,
    91  		upgradedToVersion: *format.UpgradedToVersion,
    92  		caCert:            caCert,
    93  		oldPassword:       format.OldPassword,
    94  		stateServerCert:   stateServerCert,
    95  		stateServerKey:    stateServerKey,
    96  		apiPort:           format.APIPort,
    97  		values:            format.Values,
    98  	}
    99  	if len(format.StateAddresses) > 0 {
   100  		config.stateDetails = &connectionDetails{
   101  			format.StateAddresses,
   102  			format.StatePassword,
   103  		}
   104  	}
   105  	if len(format.APIAddresses) > 0 {
   106  		config.apiDetails = &connectionDetails{
   107  			format.APIAddresses,
   108  			format.APIPassword,
   109  		}
   110  	}
   111  	return config, nil
   112  }