-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat-1.16.go
145 lines (126 loc) · 3.7 KB
/
format-1.16.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package agent
import (
"encoding/base64"
"fmt"
"net"
"strconv"
"github.com/juju/names"
goyaml "gopkg.in/yaml.v1"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/version"
)
var format_1_16 = formatter_1_16{}
// formatter_1_16 is the formatter for the 1.16 format.
type formatter_1_16 struct {
}
// Ensure that the formatter_1_16 struct implements the formatter interface.
var _ formatter = formatter_1_16{}
// format_1_16Serialization holds information for a given agent.
type format_1_16Serialization struct {
Tag string
Nonce string
UpgradedToVersion *version.Number `yaml:"upgradedToVersion"`
CACert string
StateAddresses []string `yaml:",omitempty"`
StatePassword string `yaml:",omitempty"`
APIAddresses []string `yaml:",omitempty"`
APIPassword string `yaml:",omitempty"`
OldPassword string
Values map[string]string
// Only state server machines have these next three items
StateServerCert string `yaml:",omitempty"`
StateServerKey string `yaml:",omitempty"`
APIPort int `yaml:",omitempty"`
}
func init() {
registerFormat(format_1_16)
}
const legacyFormatFilename = "format"
// legacyFormatPrefix is the prefix of the legacy format file.
const legacyFormatPrefix = "format "
// decode64 makes sure that for an empty string we have a nil slice, not an
// empty slice, which is what the base64 DecodeString function returns.
func decode64(value string) (result []byte, err error) {
if value != "" {
result, err = base64.StdEncoding.DecodeString(value)
}
return
}
func (formatter_1_16) version() string {
return "1.16"
}
func (formatter_1_16) unmarshal(data []byte) (*configInternal, error) {
var format format_1_16Serialization
if err := goyaml.Unmarshal(data, &format); err != nil {
return nil, err
}
caCert, err := decode64(format.CACert)
if err != nil {
return nil, err
}
stateServerCert, err := decode64(format.StateServerCert)
if err != nil {
return nil, err
}
stateServerKey, err := decode64(format.StateServerKey)
if err != nil {
return nil, err
}
if format.UpgradedToVersion == nil {
// Assume it's 1.16.0.
upgradedToVersion := version.MustParse("1.16.0")
format.UpgradedToVersion = &upgradedToVersion
}
tag, err := names.ParseTag(format.Tag)
if err != nil {
return nil, err
}
config := &configInternal{
tag: tag,
nonce: format.Nonce,
dataDir: DefaultDataDir,
logDir: DefaultLogDir,
upgradedToVersion: *format.UpgradedToVersion,
caCert: string(caCert),
oldPassword: format.OldPassword,
values: format.Values,
}
if len(format.StateAddresses) > 0 {
config.stateDetails = &connectionDetails{
format.StateAddresses,
format.StatePassword,
}
}
if len(stateServerKey) != 0 {
config.servingInfo = ¶ms.StateServingInfo{
Cert: string(stateServerCert),
PrivateKey: string(stateServerKey),
APIPort: format.APIPort,
}
// There's a private key, then we need the state
// port, which wasn't directly available in the 1.16 format,
// but we can infer it from the ports in the state addresses.
if len(format.StateAddresses) > 0 {
_, portString, err := net.SplitHostPort(format.StateAddresses[0])
if err != nil {
return nil, err
}
statePort, err := strconv.Atoi(portString)
if err != nil {
return nil, err
}
config.servingInfo.StatePort = statePort
} else {
return nil, fmt.Errorf("server key found but no state port")
}
}
if len(format.APIAddresses) > 0 {
config.apiDetails = &connectionDetails{
format.APIAddresses,
format.APIPassword,
}
}
return config, nil
}