-
Notifications
You must be signed in to change notification settings - Fork 0
/
format-1.18.go
192 lines (173 loc) · 5.52 KB
/
format-1.18.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package agent
import (
"fmt"
"net"
"strconv"
"github.com/juju/errors"
"github.com/juju/names"
goyaml "gopkg.in/yaml.v1"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/state/multiwatcher"
"github.com/juju/juju/version"
)
var format_1_18 = formatter_1_18{}
// formatter_1_18 is the formatter for the 1.18 format.
type formatter_1_18 struct {
}
// Ensure that the formatter_1_18 struct implements the formatter interface.
var _ formatter = formatter_1_18{}
// format_1_18Serialization holds information for a given agent.
type format_1_18Serialization struct {
Tag string
DataDir string
LogDir string
Nonce string
Jobs []multiwatcher.MachineJob `yaml:",omitempty"`
UpgradedToVersion *version.Number `yaml:"upgradedToVersion"`
CACert string
StateAddresses []string `yaml:",omitempty"`
StatePassword string `yaml:",omitempty"`
Environment string `yaml:",omitempty"`
APIAddresses []string `yaml:",omitempty"`
APIPassword string `yaml:",omitempty"`
OldPassword string
Values map[string]string
PreferIPv6 bool `yaml:"prefer-ipv6,omitempty"`
// Only state server machines have these next items set.
StateServerCert string `yaml:",omitempty"`
StateServerKey string `yaml:",omitempty"`
CAPrivateKey string `yaml:",omitempty"`
APIPort int `yaml:",omitempty"`
StatePort int `yaml:",omitempty"`
SharedSecret string `yaml:",omitempty"`
SystemIdentity string `yaml:",omitempty"`
}
func init() {
registerFormat(format_1_18)
}
func (formatter_1_18) version() string {
return "1.18"
}
func (formatter_1_18) unmarshal(data []byte) (*configInternal, error) {
// NOTE: this needs to handle the absence of StatePort and get it from the
// address
var format format_1_18Serialization
if err := goyaml.Unmarshal(data, &format); err != nil {
return nil, err
}
if format.UpgradedToVersion == nil || *format.UpgradedToVersion == version.Zero {
// Assume we upgrade from 1.16.
upgradedToVersion := version.MustParse("1.16.0")
format.UpgradedToVersion = &upgradedToVersion
}
tag, err := names.ParseTag(format.Tag)
if err != nil {
return nil, err
}
var envTag names.EnvironTag
if format.Environment != "" {
envTag, err = names.ParseEnvironTag(format.Environment)
if err != nil {
return nil, errors.Trace(err)
}
}
config := &configInternal{
tag: tag,
dataDir: format.DataDir,
logDir: format.LogDir,
jobs: format.Jobs,
upgradedToVersion: *format.UpgradedToVersion,
nonce: format.Nonce,
environment: envTag,
caCert: format.CACert,
oldPassword: format.OldPassword,
values: format.Values,
preferIPv6: format.PreferIPv6,
}
if config.logDir == "" {
config.logDir = DefaultLogDir
}
if config.dataDir == "" {
config.dataDir = DefaultDataDir
}
if len(format.StateAddresses) > 0 {
config.stateDetails = &connectionDetails{
format.StateAddresses,
format.StatePassword,
}
}
if len(format.APIAddresses) > 0 {
config.apiDetails = &connectionDetails{
format.APIAddresses,
format.APIPassword,
}
}
if len(format.StateServerKey) != 0 {
config.servingInfo = ¶ms.StateServingInfo{
Cert: format.StateServerCert,
PrivateKey: format.StateServerKey,
CAPrivateKey: format.CAPrivateKey,
APIPort: format.APIPort,
StatePort: format.StatePort,
SharedSecret: format.SharedSecret,
SystemIdentity: format.SystemIdentity,
}
// There's a private key, then we need the state port,
// which wasn't always in the 1.18 format. If it's not present
// we can infer it from the ports in the state addresses.
if config.servingInfo.StatePort == 0 {
if len(format.StateAddresses) == 0 {
return nil, fmt.Errorf("server key found but no state port")
}
_, 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
}
}
return config, nil
}
func (formatter_1_18) marshal(config *configInternal) ([]byte, error) {
var envTag string
if config.environment.Id() != "" {
envTag = config.environment.String()
}
format := &format_1_18Serialization{
Tag: config.tag.String(),
DataDir: config.dataDir,
LogDir: config.logDir,
Jobs: config.jobs,
UpgradedToVersion: &config.upgradedToVersion,
Nonce: config.nonce,
Environment: envTag,
CACert: string(config.caCert),
OldPassword: config.oldPassword,
Values: config.values,
PreferIPv6: config.preferIPv6,
}
if config.servingInfo != nil {
format.StateServerCert = config.servingInfo.Cert
format.StateServerKey = config.servingInfo.PrivateKey
format.CAPrivateKey = config.servingInfo.CAPrivateKey
format.APIPort = config.servingInfo.APIPort
format.StatePort = config.servingInfo.StatePort
format.SharedSecret = config.servingInfo.SharedSecret
format.SystemIdentity = config.servingInfo.SystemIdentity
}
if config.stateDetails != nil {
format.StateAddresses = config.stateDetails.addresses
format.StatePassword = config.stateDetails.password
}
if config.apiDetails != nil {
format.APIAddresses = config.apiDetails.addresses
format.APIPassword = config.apiDetails.password
}
return goyaml.Marshal(format)
}