diff --git a/anvil-python/anvil/provider/local/commands.py b/anvil-python/anvil/provider/local/commands.py index cb46aeb..280da3f 100644 --- a/anvil-python/anvil/provider/local/commands.py +++ b/anvil-python/anvil/provider/local/commands.py @@ -13,8 +13,10 @@ # See the License for the specific language governing permissions and # limitations under the License. import asyncio +import json import logging from pathlib import Path +import subprocess from typing import List import click @@ -96,7 +98,7 @@ from anvil.jobs.juju import CONTROLLER from anvil.jobs.manifest import Manifest from anvil.provider.local.deployment import LocalDeployment -from anvil.utils import CatchGroup +from anvil.utils import CatchGroup, machines_missing_juju_controllers LOG = logging.getLogger(__name__) console = Console() @@ -448,17 +450,21 @@ def join( ) ) - machines = asyncio.run( - jhelper.get_machines(deployment.infrastructure_model) + machines_res = subprocess.run( + ["juju", "machines", "--format", "json"], capture_output=True ) + machines = json.loads(machines_res.stdout)["machines"] n_machines = len(machines) - LOG.debug(f"Juju machines: {machines}") if n_machines > 2 and n_machines % 2 == 1: + machines_to_join = machines_missing_juju_controllers() + LOG.debug( + f"Will enable Juju controller on machines {machines_to_join}" + ) plan2.append( ScaleJujuStep( controller, n_machines, - ["--to", ",".join(machines.keys())], + ["--to", ",".join(machines_to_join)], ) ) run_plan(plan2, console) diff --git a/anvil-python/anvil/utils.py b/anvil-python/anvil/utils.py index ce35682..3ab326a 100644 --- a/anvil-python/anvil/utils.py +++ b/anvil-python/anvil/utils.py @@ -13,7 +13,9 @@ # See the License for the specific language governing permissions and # limitations under the License. +import json import logging +import subprocess import sys import click @@ -43,3 +45,20 @@ def __call__(self, *args, **kwargs): # type: ignore[no-untyped-def] LOG.warn(message) LOG.error("Error: %s", e) sys.exit(1) + + +def machines_missing_juju_controllers() -> list[str]: + result = subprocess.run( + ["juju", "show-controller", "anvil-controller", "--format", "json"], + capture_output=True, + ) + controllers = json.loads(result.stdout) + controller_machines = set( + controllers["anvil-controller"]["controller-machines"].keys() + ) + + machines_res = subprocess.run( + ["juju", "machines", "--format", "json"], capture_output=True + ) + machines = set(json.loads(machines_res.stdout)["machines"].keys()) + return list(machines - controller_machines)