|
3 | 3 | # See LICENSE file for licensing details. |
4 | 4 |
|
5 | 5 | import json |
6 | | -import os |
7 | 6 | import subprocess |
8 | 7 | from collections.abc import Callable |
9 | | -from pathlib import Path |
10 | 8 |
|
11 | 9 | import jubilant_backports |
12 | 10 | from jubilant_backports import Juju |
|
28 | 26 | JujuAppsStatusFn = Callable[[Status, str], bool] |
29 | 27 |
|
30 | 28 |
|
31 | | -def _get_juju_keys() -> tuple[str, str]: |
32 | | - """Get Juju public and private keys.""" |
33 | | - config_dir = Path("~/.local/share/juju") |
34 | | - if juju_data := os.getenv("JUJU_DATA"): |
35 | | - config_dir = Path(juju_data) |
36 | | - |
37 | | - return ( |
38 | | - str(config_dir.expanduser().resolve() / "ssh" / "juju_id_rsa.pub"), |
39 | | - str(config_dir.expanduser().resolve() / "ssh" / "juju_id_rsa"), |
40 | | - ) |
41 | | - |
42 | | - |
43 | 29 | def check_mysql_instances_online(juju: Juju, app_name: str) -> bool: |
44 | 30 | """Checks whether all MySQL cluster instances are online. |
45 | 31 |
|
@@ -121,20 +107,16 @@ def get_app_units(juju: Juju, app_name: str) -> dict[str, UnitStatus]: |
121 | 107 |
|
122 | 108 | def scale_app_units(juju: Juju, app_name: str, num_units: int) -> None: |
123 | 109 | """Scale a given application to a number of units.""" |
124 | | - app_units = get_app_units(juju, app_name) |
125 | | - app_units_diff = len(app_units) - num_units |
| 110 | + app_units = list(get_app_units(juju, app_name)) |
| 111 | + app_units_diff = num_units - len(app_units) |
126 | 112 |
|
127 | | - scale_func = None |
128 | 113 | if app_units_diff > 0: |
129 | | - scale_func = juju.remove_unit |
| 114 | + juju.add_unit(app_name, num_units=app_units_diff) |
130 | 115 | if app_units_diff < 0: |
131 | | - scale_func = juju.add_unit |
| 116 | + juju.remove_unit(*app_units[app_units_diff:]) |
132 | 117 | if app_units_diff == 0: |
133 | 118 | return |
134 | 119 |
|
135 | | - for _ in range(abs(app_units_diff)): |
136 | | - scale_func(app_name, num_units=1) |
137 | | - |
138 | 120 | juju.wait( |
139 | 121 | ready=lambda status: len(status.apps[app_name].units) == num_units, |
140 | 122 | timeout=20 * MINUTE_SECS, |
@@ -206,54 +188,6 @@ def get_unit_status_log(juju: Juju, unit_name: str, log_lines: int = 0) -> list[ |
206 | 188 | return json.loads(output) |
207 | 189 |
|
208 | 190 |
|
209 | | -# TODO: |
210 | | -# Rely on Jubilant scp command once they make it easier |
211 | | -# for package users to deal with temporal directories |
212 | | -# https://github.com/canonical/jubilant/issues/201 |
213 | | -def scp_unit_file_from( |
214 | | - juju: Juju, app_name: str, unit_name: str, source_path: str, target_path: str |
215 | | -) -> None: |
216 | | - """Copy a file from a given unit.""" |
217 | | - juju_keys = _get_juju_keys() |
218 | | - unit_address = get_unit_ip(juju, app_name, unit_name) |
219 | | - unit_username = "ubuntu" |
220 | | - |
221 | | - subprocess.check_output([ |
222 | | - "scp", |
223 | | - "-B", |
224 | | - "-o", |
225 | | - "StrictHostKeyChecking=no", |
226 | | - "-i", |
227 | | - juju_keys[1], |
228 | | - f"{unit_username}@{unit_address}:{source_path}", |
229 | | - f"{target_path}", |
230 | | - ]) |
231 | | - |
232 | | - |
233 | | -# TODO: |
234 | | -# Rely on Jubilant scp command once they make it easier |
235 | | -# for package users to deal with temporal directories |
236 | | -# https://github.com/canonical/jubilant/issues/201 |
237 | | -def scp_unit_file_into( |
238 | | - juju: Juju, app_name: str, unit_name: str, source_path: str, target_path: str |
239 | | -) -> None: |
240 | | - """Copy a file into a given unit.""" |
241 | | - juju_keys = _get_juju_keys() |
242 | | - unit_address = get_unit_ip(juju, app_name, unit_name) |
243 | | - unit_username = "ubuntu" |
244 | | - |
245 | | - subprocess.check_output([ |
246 | | - "scp", |
247 | | - "-B", |
248 | | - "-o", |
249 | | - "StrictHostKeyChecking=no", |
250 | | - "-i", |
251 | | - juju_keys[1], |
252 | | - f"{source_path}", |
253 | | - f"{unit_username}@{unit_address}:{target_path}", |
254 | | - ]) |
255 | | - |
256 | | - |
257 | 191 | def get_relation_data(juju: Juju, app_name: str, rel_name: str) -> list[dict]: |
258 | 192 | """Returns a list that contains the relation-data. |
259 | 193 |
|
|
0 commit comments