forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassess_upgrade.py
executable file
·266 lines (220 loc) · 8.17 KB
/
assess_upgrade.py
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env python
""" Assess upgrading juju controllers and models.
Bootstrap a previous version of juju and then upgrade the controller and models
to the new one.
- Spins up a local streams server (requires to be run on lxd or on a machine
with an externally routable interface.
- Bootstraps using the 'stable' juju then upgrades to the 'devel' version.
- Deploys keystone and percona-cluster to ensure
workloads continue working.
"""
from __future__ import print_function
import argparse
import logging
import re
import sys
from collections import (
namedtuple,
)
from utility import (
add_basic_testing_arguments,
configure_logging,
temp_dir,
)
from textwrap import (
dedent,
)
from deploy_stack import (
BootstrapManager
)
from jujupy.binaries import (
get_stable_juju
)
from jujupy.client import (
ModelClient,
get_stripped_version_number,
get_version_string_parts,
)
from jujupy.stream_server import (
StreamServer,
agent_tgz_from_juju_binary,
)
from jujupy.wait_condition import (
WaitModelVersion,
wait_until_model_upgrades,
)
from jujupy.workloads import (
deploy_keystone_with_db,
assert_keystone_is_responding
)
__metaclass__ = type
log = logging.getLogger("assess_upgrade")
VersionParts = namedtuple('VersionParts', ['version', 'series', 'arch'])
def assess_upgrade_from_stable_to_develop(args, stable_bsm, devel_client):
stable_client = stable_bsm.client
with temp_dir() as base_dir:
stream_server = StreamServer(base_dir)
setup_agent_metadata(
stream_server, args.stable_juju_agent,
stable_client, base_dir, 'released')
setup_agent_metadata(
stream_server, args.devel_juju_agent,
devel_client, base_dir, 'released')
with stream_server.server() as url:
stable_client.env.update_config({
'agent-metadata-url': url,
'agent-stream': 'released'
})
with stable_bsm.booted_context(False):
assert_upgrade_is_successful(stable_client, devel_client)
def assess_upgrade_passing_agent_stream(args, devel_client):
# Use the same devel juju to ensure this feature is supported.
stable_bsm = BootstrapManager.from_client(args, devel_client)
stable_client = stable_bsm.client
devel_version_parts = get_version_parts(devel_client.version)
forced_devel_client = devel_client.clone(
version='{vers}-{series}-{arch}'.format(
vers=increment_version(devel_client.version),
series=devel_version_parts.series,
arch=devel_version_parts.arch))
with temp_dir() as base_dir:
stream_server = StreamServer(base_dir)
setup_agent_metadata(
stream_server, args.devel_juju_agent,
stable_client, base_dir, 'released')
setup_agent_metadata(
stream_server, args.devel_juju_agent,
forced_devel_client, base_dir, 'proposed',
force_version=True)
with stream_server.server() as url:
stable_client.env.update_config({
'agent-metadata-url': url,
'agent-stream': 'released'
})
with stable_bsm.booted_context(False):
assert_upgrade_is_successful(
stable_client,
forced_devel_client,
('--agent-stream', 'proposed'))
def assert_upgrade_is_successful(
stable_client, devel_client, extra_upgrade_args=()):
if not isinstance(extra_upgrade_args, tuple):
raise ValueError("extra_upgrade_args must be a tuple")
assert_stable_model_is_correct(stable_client)
deploy_keystone_with_db(stable_client)
assert_keystone_is_responding(stable_client)
upgrade_stable_to_devel_version(devel_client, extra_upgrade_args)
assert_keystone_is_responding(devel_client)
def upgrade_stable_to_devel_version(client, extra_args):
devel_version = get_stripped_version_number(client.version)
client.get_controller_client().juju(
'upgrade-juju', ('-m', 'controller', '--debug') + extra_args)
assert_model_is_version(client.get_controller_client(), devel_version)
wait_until_model_upgrades(client)
client.juju('upgrade-juju', ('--debug',) + extra_args)
assert_model_is_version(client, devel_version)
wait_until_model_upgrades(client)
client.wait_for_started()
client.wait_for_workloads()
def assert_stable_model_is_correct(stable_client):
assert_model_is_version(
stable_client.get_controller_client(),
get_stripped_version_number(stable_client.version))
assert_model_is_version(
stable_client,
get_stripped_version_number(stable_client.version))
def assert_model_is_version(client, expected_version, timeout=600):
# Check agent & machine version
client.wait_for_version(expected_version, timeout)
# Also check model version
client.wait_for(WaitModelVersion(expected_version, timeout))
def setup_agent_metadata(
stream_server, agent, client, tmp_dir, stream, force_version=False):
version_parts = get_version_parts(client.version)
if agent is None:
agent_details = agent_tgz_from_juju_binary(
client.full_path,
tmp_dir,
force_version=version_parts.version if force_version else None)
else:
agent_details = agent
stream_server.add_product(
stream,
version_parts.version,
version_parts.arch,
client.env.get_option('default-series'),
agent_details)
# Trusty needed for wikimedia charm.
stream_server.add_product(
stream,
version_parts.version,
version_parts.arch,
'trusty',
agent_details)
def get_version_parts(version_string):
parts = get_version_string_parts(version_string)
return VersionParts(parts[0], parts[1], parts[2])
def increment_version(version_string):
"""Increment the patch version of the given version.
example:
2.3.7 -> 2.3.8
2.4-beta1 -> 2.4-beta2
"""
juju_version = get_stripped_version_number(version_string)
try:
major, minor, patch = juju_version.split('.')
return '{}.{}.{}'.format(major, minor, increment_patch_version(patch))
except ValueError:
# Named patch version
major, minor_patch = juju_version.split('.')
minor, patch = minor_patch.split('-')
return '{}.{}-{}'.format(major, minor, increment_patch_version(patch))
def increment_patch_version(patch_version):
"""Increment the patch version of the given version.
example:
7 -> 8
beta1 -> beta2
"""
try:
return int(patch_version) + 1
except ValueError:
# Named patch version (alpha/beta etc.)
name, num = re.search('(\D+)(\d+)', patch_version).groups()
return "{name}{num}".format(
name=name,
num=int(num) + 1,
)
def parse_args(argv):
"""Parse all arguments."""
parser = argparse.ArgumentParser(
description=dedent("""\
Test juju upgrades for controllers and models.
Uses `juju_bin` as the development version of juju to upgrade to."""),
formatter_class=argparse.RawDescriptionHelpFormatter)
add_basic_testing_arguments(parser, existing=False)
parser.add_argument(
'--stable-juju-bin',
help='Path to juju binary to be used as the stable version of juju.')
parser.add_argument(
'--stable-juju-agent',
help='Path to agent to use when bootstrapping with stable binary'
)
parser.add_argument(
'--devel-juju-agent',
help='Path to agent to use when bootstrapping with stable binary'
)
return parser.parse_args(argv)
def main(argv=None):
args = parse_args(argv)
configure_logging(args.verbose)
stable_bsm = get_stable_juju(args, args.stable_juju_bin)
devel_client = stable_bsm.client.clone(
version=ModelClient.get_version(args.juju_bin),
full_path=args.juju_bin,
debug=args.debug
)
assess_upgrade_from_stable_to_develop(args, stable_bsm, devel_client)
assess_upgrade_passing_agent_stream(args, devel_client)
return 0
if __name__ == '__main__':
sys.exit(main())