Skip to content

Commit

Permalink
Use juju CLI to determine machine count and which machines need contr…
Browse files Browse the repository at this point in the history
…ollers
  • Loading branch information
wyattrees committed Jul 3, 2024
1 parent 1b51e85 commit c9fea2f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
16 changes: 11 additions & 5 deletions anvil-python/anvil/provider/local/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions anvil-python/anvil/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

0 comments on commit c9fea2f

Please sign in to comment.