33"""
44from __future__ import annotations
55
6+ import logging
7+
68from pathlib import Path
79from typing import TYPE_CHECKING , TypedDict
810
911import click
10- from click import ClickException
1112
1213from boardwalk .ansible import (
1314 ansible_runner_run_tasks ,
1617 AnsibleRunnerGeneralError ,
1718 AnsibleRunnerUnreachableHost ,
1819)
20+ from boardwalk .app_exceptions import BoardwalkException
1921from boardwalk .host import Host
2022from boardwalk .manifest import get_ws , NoActiveWorkspace , Workspace
2123
@@ -34,6 +36,9 @@ class runnerKwargs(TypedDict, total=False):
3436 timeout : int
3537
3638
39+ logger = logging .getLogger (__name__ )
40+
41+
3742@click .command (short_help = "Inits local workspace state by getting host facts" )
3843@click .option (
3944 "--limit" ,
@@ -58,13 +63,13 @@ def init(ctx: click.Context, limit: str, retry: bool):
5863 """
5964 if retry and limit not in ["all" , "" ]:
6065 # We don't allow limit and retry to be specified together at the moment
61- raise ClickException ("--limit and --retry cannot be supplied together" )
66+ raise BoardwalkException ("--limit and --retry cannot be supplied together" )
6267
6368 try :
6469 ws = get_ws ()
6570 except NoActiveWorkspace as e :
66- raise ClickException (e .message )
67- click . echo (f"Using workspace: { ws .name } " )
71+ raise BoardwalkException (e .message )
72+ logger . info (f"Using workspace: { ws .name } " )
6873
6974 ws .assert_host_pattern_unchanged ()
7075
@@ -84,7 +89,7 @@ def init(ctx: click.Context, limit: str, retry: bool):
8489 }
8590 if retry :
8691 if not retry_file_path .exists ():
87- raise ClickException ("No retry file exists" )
92+ raise BoardwalkException ("No retry file exists" )
8893 runner_kwargs ["limit" ] = f"@{ str (retry_file_path )} "
8994
9095 # Save the host pattern we are initializing with. If the pattern changes after
@@ -109,10 +114,10 @@ def init(ctx: click.Context, limit: str, retry: bool):
109114 # we try to print out some debug info and bail
110115 for event in e .runner .events :
111116 try :
112- click . echo (event ["stdout" ])
117+ logger . error (event ["stdout" ])
113118 except KeyError :
114119 pass
115- raise ClickException ("Failed to start fact gathering" )
120+ raise BoardwalkException ("Failed to start fact gathering" )
116121
117122 # Clear the retry file after we use it to start fresh before we build a new one
118123 retry_file_path .unlink (missing_ok = True )
@@ -131,11 +136,11 @@ def init(ctx: click.Context, limit: str, retry: bool):
131136
132137 # Note if any hosts were unreachable
133138 if hosts_were_unreachable :
134- click . echo ("Some hosts were unreachable. Consider running again with --retry" )
139+ logger . warn ("Some hosts were unreachable. Consider running again with --retry" )
135140
136141 # If we didn't find any hosts, raise an exception
137142 if len (ws .state .hosts ) == 0 :
138- raise ClickException ("No hosts gathered" )
143+ raise BoardwalkException ("No hosts gathered" )
139144
140145
141146def add_gathered_facts_to_state (event : RunnerEvent , ws : Workspace ):
@@ -164,9 +169,9 @@ def handle_failed_init_hosts(event: RunnerEvent, retry_file_path: Path):
164169 event ["event" ] == "runner_on_unreachable"
165170 or event ["event" ] == "runner_on_failed"
166171 ):
167- click . echo (event ["stdout" ])
172+ logger . warn (event ["stdout" ])
168173 with open (retry_file_path , "a" ) as file :
169174 file .write (f"{ event ['event_data' ]['host' ]} \n " )
170175 # If no hosts matched or there are warnings, write them out
171176 if event ["event" ] == "warning" or event ["event" ] == "playbook_on_no_hosts_matched" :
172- click . echo (event ["stdout" ])
177+ logger . warn (event ["stdout" ])
0 commit comments