codecraftai-interop 1.5.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. ccai/interop/agents/__init__.py +12 -0
  2. ccai/interop/agents/localcommandagent.py +247 -0
  3. ccai/interop/agents/poweragentbase.py +75 -0
  4. ccai/interop/agents/serialagentbase.py +29 -0
  5. ccai/interop/agents/systemagentbase.py +28 -0
  6. ccai/interop/aspects.py +219 -0
  7. ccai/interop/cli/__init__.py +5 -0
  8. ccai/interop/cli/am_interop_command.py +83 -0
  9. ccai/interop/cli/cmdtree/README.md +1 -0
  10. ccai/interop/cli/cmdtree/__init__.py +12 -0
  11. ccai/interop/constants.py +6 -0
  12. ccai/interop/interfaces/__init__.py +12 -0
  13. ccai/interop/interfaces/inetworkclient.py +27 -0
  14. ccai/interop/interfaces/isystemcontext.py +134 -0
  15. ccai/interop/manufacturers/README.md +1 -0
  16. ccai/interop/models/__init__.py +0 -0
  17. ccai/interop/models/command/__init__.py +0 -0
  18. ccai/interop/models/command/commandbuilder.py +338 -0
  19. ccai/interop/models/command/commandresult.py +80 -0
  20. ccai/interop/protocols/dns/__init__.py +25 -0
  21. ccai/interop/protocols/dns/aspectsoverlaydns.py +57 -0
  22. ccai/interop/protocols/dns/dnsaddress.py +108 -0
  23. ccai/interop/protocols/dns/dnsapi.py +58 -0
  24. ccai/interop/protocols/dns/dnsconst.py +202 -0
  25. ccai/interop/protocols/dns/dnsflags.py +193 -0
  26. ccai/interop/protocols/dns/dnshostinfo.py +105 -0
  27. ccai/interop/protocols/dns/dnsinboundmessage.py +630 -0
  28. ccai/interop/protocols/dns/dnsmailexchange.py +103 -0
  29. ccai/interop/protocols/dns/dnsnaptr.py +109 -0
  30. ccai/interop/protocols/dns/dnsoutboundmessage.py +646 -0
  31. ccai/interop/protocols/dns/dnspointer.py +99 -0
  32. ccai/interop/protocols/dns/dnsquestion.py +139 -0
  33. ccai/interop/protocols/dns/dnsquestionresults.py +75 -0
  34. ccai/interop/protocols/dns/dnsrecord.py +290 -0
  35. ccai/interop/protocols/dns/dnsserver.py +196 -0
  36. ccai/interop/protocols/dns/dnsservice.py +136 -0
  37. ccai/interop/protocols/dns/dnssoa.py +93 -0
  38. ccai/interop/protocols/dns/dnstext.py +104 -0
  39. ccai/interop/protocols/dns/dnsutil.py +108 -0
  40. ccai/interop/protocols/dns/dnsvalidation.py +67 -0
  41. ccai/interop/protocols/dns/exceptions.py +52 -0
  42. ccai/interop/protocols/dns/matchpredicate.py +120 -0
  43. ccai/interop/protocols/dns/mdnsbrowser.py +569 -0
  44. ccai/interop/protocols/dns/mdnspresence.py +233 -0
  45. ccai/interop/protocols/dns/mdnsserviceinfo.py +191 -0
  46. ccai/interop/protocols/dns/overlaycandidate.py +47 -0
  47. ccai/interop/protocols/dns/overlaydnsresolver.py +604 -0
  48. ccai/interop/protocols/dns/testmessagesipv4.py +666 -0
  49. ccai/interop/protocols/dns/testmessagesipv6.py +744 -0
  50. ccai/interop/protocols/power/README.md +1 -0
  51. ccai/interop/protocols/power/dlipower/__init__.py +12 -0
  52. ccai/interop/protocols/power/dlipower/dlipowerclient.py +168 -0
  53. ccai/interop/protocols/power/dlipower/dlipoweroutletcontrol.py +60 -0
  54. ccai/interop/protocols/power/dlipower/groups/__init__.py +12 -0
  55. ccai/interop/protocols/power/dlipower/groups/authapigroup.py +157 -0
  56. ccai/interop/protocols/power/dlipower/groups/autopingapigroup.py +84 -0
  57. ccai/interop/protocols/power/dlipower/groups/configapigroup.py +126 -0
  58. ccai/interop/protocols/power/dlipower/groups/meterapigroup.py +77 -0
  59. ccai/interop/protocols/power/dlipower/groups/networkapigroup.py +105 -0
  60. ccai/interop/protocols/power/dlipower/groups/relayapigroup.py +183 -0
  61. ccai/interop/protocols/power/dlipower/groups/rendererapigroup.py +61 -0
  62. ccai/interop/protocols/power/dlipower/groups/scriptapigroup.py +65 -0
  63. ccai/interop/protocols/power/dlipower/responsemodel.py +92 -0
  64. ccai/interop/protocols/power/dlipower/spec/dli-power-restapi.md +770 -0
  65. ccai/interop/protocols/serial/tcpserialagent.py +392 -0
  66. ccai/interop/protocols/ssh/__init__.py +18 -0
  67. ccai/interop/protocols/ssh/exceptions.py +28 -0
  68. ccai/interop/protocols/ssh/planeresolver.py +120 -0
  69. ccai/interop/protocols/ssh/sshagent.py +375 -0
  70. ccai/interop/protocols/ssh/sshaspects.py +35 -0
  71. ccai/interop/protocols/ssh/sshbase.py +970 -0
  72. ccai/interop/protocols/ssh/sshconst.py +49 -0
  73. ccai/interop/protocols/ssh/sshhelpers.py +667 -0
  74. ccai/interop/protocols/ssh/sshsession.py +433 -0
  75. ccai/interop/protocols/ssh/sshtunnel.py +372 -0
  76. ccai/interop/py.typed +0 -0
  77. ccai/interop/testing/__init__.py +1 -0
  78. ccai/interop/vpn/__init__.py +20 -0
  79. ccai/interop/vpn/aspectsvpnclient.py +142 -0
  80. ccai/interop/vpn/tailscale/__init__.py +20 -0
  81. ccai/interop/vpn/tailscale/aspectstailscale.py +96 -0
  82. ccai/interop/vpn/tailscale/exceptions.py +173 -0
  83. ccai/interop/vpn/tailscale/statusparser.py +228 -0
  84. ccai/interop/vpn/tailscale/tailscaleclient.py +267 -0
  85. ccai/interop/vpn/twingate/__init__.py +27 -0
  86. ccai/interop/vpn/twingate/apigroups/__init__.py +21 -0
  87. ccai/interop/vpn/twingate/apigroups/accessgroup.py +167 -0
  88. ccai/interop/vpn/twingate/apigroups/connectorsgroup.py +399 -0
  89. ccai/interop/vpn/twingate/apigroups/groupsgroup.py +242 -0
  90. ccai/interop/vpn/twingate/apigroups/resourcesgroup.py +276 -0
  91. ccai/interop/vpn/twingate/aspectstwingate.py +99 -0
  92. ccai/interop/vpn/twingate/connectorinvocation.py +128 -0
  93. ccai/interop/vpn/twingate/exceptions.py +40 -0
  94. ccai/interop/vpn/twingate/model/__init__.py +20 -0
  95. ccai/interop/vpn/twingate/model/twingateconnector.py +36 -0
  96. ccai/interop/vpn/twingate/model/twingategroup.py +33 -0
  97. ccai/interop/vpn/twingate/model/twingateresource.py +37 -0
  98. ccai/interop/vpn/twingate/twingateclient.py +367 -0
  99. codecraftai_interop-1.5.0.dist-info/METADATA +101 -0
  100. codecraftai_interop-1.5.0.dist-info/RECORD +103 -0
  101. codecraftai_interop-1.5.0.dist-info/WHEEL +4 -0
  102. codecraftai_interop-1.5.0.dist-info/entry_points.txt +3 -0
  103. codecraftai_interop-1.5.0.dist-info/licenses/LICENSE.md +204 -0
@@ -0,0 +1,12 @@
1
+ """
2
+ .. module:: __init__
3
+ :platform: Darwin, Linux, Unix, Windows
4
+ :synopsis: __init__ module.
5
+
6
+ .. moduleauthor:: Myron Walker <myron.walker@gmail.com>
7
+ """
8
+
9
+ __author__ = "Myron Walker"
10
+ __copyright__ = "Copyright 2025, Code Craft AI LLC"
11
+ __credits__ = []
12
+
@@ -0,0 +1,247 @@
1
+ """
2
+ .. module:: localcommandagent
3
+ :platform: Darwin, Linux, Unix, Windows
4
+ :synopsis: Module containing the :class:`LocalCommandAgent` class used to
5
+ execute commands on the local host.
6
+
7
+ .. moduleauthor:: Myron Walker <myron.walker@gmail.com>
8
+ """
9
+
10
+ __author__ = "Myron Walker"
11
+ __copyright__ = "Copyright 2025, Code Craft AI LLC"
12
+ __credits__ = []
13
+
14
+
15
+ from typing import Optional, Sequence, Tuple, Union
16
+
17
+ import subprocess
18
+ import threading
19
+ import time
20
+
21
+ from ccai.xmodules.xlogging.enhancedloggers import getEnhancedLogger
22
+
23
+ from ccai.waiting import TimeoutContext
24
+
25
+ from ccai.errors.exceptions import SemanticError
26
+
27
+ from ccai.interop.interfaces.isystemcontext import ISystemContext
28
+ from ccai.interop.aspects import AspectsCmd, DEFAULT_CMD_ASPECTS, ActionPattern, LoggingPattern
29
+
30
+ from ccai.xmodules.xformatting import indent_lines, format_command_result
31
+ from ccai.xmodules.xlogging.monitoredscope import MonitoredScope
32
+
33
+ logger = getEnhancedLogger()
34
+
35
+ class LocalCommandAgent(ISystemContext):
36
+ """
37
+ Provides command execution capabilities on the local host without
38
+ relying on a remote connection.
39
+ """
40
+ def __init__(self, aspects: AspectsCmd = DEFAULT_CMD_ASPECTS):
41
+ """
42
+ Initializes a :class:`LocalCommandAgent` instance.
43
+
44
+ :param aspects: The default :class:`AspectsCmd` object to use when
45
+ running commands.
46
+ """
47
+
48
+ self._aspects = aspects
49
+ return
50
+
51
+ @property
52
+ def aspects(self) -> AspectsCmd:
53
+ """
54
+ The logging, iteration and other aspects that govern how commands
55
+ are executed and monitored on the local host.
56
+ """
57
+ return self._aspects
58
+
59
+ def open_session(self, sysctx: Optional[ISystemContext] = None, aspects: Optional[AspectsCmd] = None, **kwargs) -> ISystemContext: # pylint: disable=arguments-differ
60
+ """
61
+ Create and return a system context for executing commands locally.
62
+
63
+ :param sysctx: Optional :class:`ISystemContext` instance to reuse.
64
+ :param aspects: Execution aspects to apply for the session.
65
+
66
+ :returns: The current :class:`LocalCommandAgent` instance.
67
+ """
68
+ return self
69
+
70
+ def run_cmd(self, command: str, exp_status: Union[int, Sequence] = 0, aspects: Optional[AspectsCmd] = None) -> Tuple[int, str, str]:
71
+ """
72
+ Runs a command on the local host using the specified parameters.
73
+
74
+ :param command: The command to run.
75
+ :param exp_status: An integer or sequence of integers that specify
76
+ the set of expected status codes from the
77
+ command.
78
+ :param aspects: The run aspects to use when running the command.
79
+
80
+ :returns: The status, stdout and stderr from the command that was
81
+ run.
82
+ """
83
+ # Go through the overrides and if they are not passed use the agent defaults.
84
+
85
+ if aspects is None:
86
+ aspects = self._aspects
87
+
88
+ completion_toctx = TimeoutContext(aspects.completion_timeout, aspects.completion_interval)
89
+
90
+ inactivity_timeout = aspects.inactivity_timeout
91
+ monitor_delay = aspects.monitor_delay
92
+ logging_pattern = aspects.logging_pattern
93
+
94
+ status, stdout, stderr = None, None, None
95
+
96
+ this_thr = threading.current_thread()
97
+ monmsg= "Thread failed to exit monitored scope. thid=%s thname=%s cmd=%s" % (this_thr.ident, this_thr.name, command)
98
+
99
+ # Run the command using the SINGULAR pattern, we run it once and then return the result
100
+ if aspects.action_pattern == ActionPattern.SINGLE_CALL:
101
+
102
+ # Setup a monitored scope for the local call in case of timeout failure
103
+ with MonitoredScope("RUNCMD-SINGULAR", monmsg, completion_toctx, notify_delay=monitor_delay) as _:
104
+
105
+ proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
106
+ stdout, stderr = proc.communicate(timeout=inactivity_timeout)
107
+ status = proc.returncode
108
+
109
+
110
+ stdout = stdout.decode('utf8')
111
+ stderr = stderr.decode('utf8')
112
+ self._log_command_result(command, status, stdout, stderr, exp_status, logging_pattern)
113
+
114
+ # DO_UNTIL_SUCCESS, run the command until we get a successful expected result or a completion timeout has occured
115
+ elif aspects.action_pattern == ActionPattern.DO_UNTIL_SUCCESS:
116
+
117
+ completion_toctx.mark_begin()
118
+
119
+ while True:
120
+
121
+ # Setup a monitored scope for the local call in case of timeout failure
122
+ with MonitoredScope("RUNCMD-DO_UNTIL_SUCCESS", monmsg, completion_toctx, notify_delay=monitor_delay) as _:
123
+ proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
124
+ stdout, stderr = proc.communicate(timeout=inactivity_timeout)
125
+ status = proc.returncode
126
+
127
+ stdout = stdout.decode('utf8')
128
+ stderr = stderr.decode('utf8')
129
+ self._log_command_result(command, status, stdout, stderr, exp_status, logging_pattern)
130
+
131
+ if isinstance(exp_status, int):
132
+ if status == exp_status:
133
+ break
134
+ elif status in exp_status:
135
+ break
136
+
137
+ if completion_toctx.final_attempt:
138
+ what_for="command success"
139
+ details = [
140
+ "CMD: %s" % command,
141
+ "STDOUT:",
142
+ indent_lines(stdout, 1),
143
+ "STDERR:",
144
+ indent_lines(stderr, 1),
145
+ ]
146
+ toerr = completion_toctx.create_timeout(what_for=what_for, detail=details)
147
+ raise toerr
148
+
149
+ elif not completion_toctx.should_continue():
150
+ completion_toctx.mark_final_attempt()
151
+
152
+ time.sleep(completion_toctx.interval)
153
+
154
+ # DO_WHILE_SUCCESS, run the command while it is succeeds or a completion timeout has occured
155
+ elif aspects.action_pattern == ActionPattern.DO_WHILE_SUCCESS:
156
+
157
+ completion_toctx.mark_begin()
158
+
159
+ while True:
160
+
161
+ with MonitoredScope("RUNCMD-DO_WHILE_SUCCESS", monmsg, completion_toctx, notify_delay=monitor_delay) as _:
162
+ proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
163
+ stdout, stderr = proc.communicate(timeout=inactivity_timeout)
164
+ status = proc.returncode
165
+
166
+ stdout = stdout.decode('utf8')
167
+ stderr = stderr.decode('utf8')
168
+ self._log_command_result(command, status, stdout, stderr, exp_status, logging_pattern)
169
+
170
+ if isinstance(exp_status, int):
171
+ if status != exp_status:
172
+ break
173
+ elif status not in exp_status:
174
+ break
175
+
176
+ if completion_toctx.final_attempt:
177
+ what_for="command failure"
178
+ details = [
179
+ "CMD: %s" % command,
180
+ "STDOUT:",
181
+ indent_lines(stdout, 1),
182
+ "STDERR:",
183
+ indent_lines(stderr, 1),
184
+ ]
185
+ toerr = completion_toctx.create_timeout(what_for=what_for, detail=details)
186
+ raise toerr
187
+
188
+ elif not completion_toctx.should_continue():
189
+ completion_toctx.mark_final_attempt()
190
+
191
+ time.sleep(completion_toctx.interval)
192
+
193
+ elif aspects.action_pattern == ActionPattern.SINGLE_CONNECTED_CALL or aspects.action_pattern == ActionPattern.DO_UNTIL_CONNECTION_FAILURE:
194
+ errmsg = "LocalAgent currently does not support the SINGLE_CONNECTED_CALL or DO_UNTIL_CONNECTION_FAILURE action patterns."
195
+ raise SemanticError(errmsg) from None
196
+ else:
197
+ errmsg = "LocalAgent: Unknown ActionPattern encountered. action_pattern={}".format(aspects.action_pattern)
198
+ raise SemanticError(errmsg) from None
199
+
200
+ return status, stdout, stderr
201
+
202
+ def verify_connectivity(self) -> bool:
203
+ """
204
+ Method that can be used to verify that the local command execution
205
+ environment is reachable.
206
+
207
+ :returns: A boolean value indicating whether connectivity with the
208
+ local machine was successful.
209
+ """
210
+ return True
211
+
212
+ def _log_command_result(self, command: str, status: int, stdout: str, stderr: str, exp_status: Union[int, Sequence[int]],
213
+ logging_pattern: LoggingPattern):
214
+ """
215
+ Private method that handles the logging of command results based on
216
+ the expected status and logging pattern specified.
217
+
218
+ :param command: The command that was run.
219
+ :param status: The status code returned from running the command.
220
+ :param stdout: The contents of the standard output from running the
221
+ command.
222
+ :param stderr: The contents of the error output from running the
223
+ command.
224
+ :param exp_status: The status code or possible status codes that was
225
+ expected to be returned from running the command.
226
+ :param logging_pattern: The result logging pattern (:class:`LoggingPattern`) to use when logging.
227
+ """
228
+ # pylint: disable=no-self-use
229
+
230
+ msg = "Command result from a local agent."
231
+
232
+ if isinstance(exp_status, int):
233
+ if status == exp_status:
234
+ if logging_pattern == LoggingPattern.ALL_RESULTS or logging_pattern == LoggingPattern.SUCCESS_ONLY:
235
+ fullmsg = format_command_result(msg, command, status, stdout, stderr)
236
+ logger.info(fullmsg)
237
+ else:
238
+ if status in exp_status:
239
+ if logging_pattern == LoggingPattern.ALL_RESULTS or logging_pattern == LoggingPattern.SUCCESS_ONLY:
240
+ fullmsg = format_command_result(msg, command, status, stdout, stderr)
241
+ logger.info(fullmsg)
242
+ else:
243
+ if logging_pattern == LoggingPattern.ALL_RESULTS or logging_pattern == LoggingPattern.FAILURE_ONLY:
244
+ fullmsg = format_command_result(msg, command, status, stdout, stderr)
245
+ logger.info(fullmsg)
246
+
247
+ return
@@ -0,0 +1,75 @@
1
+ """
2
+ .. module:: poweragentbase
3
+ :platform: Darwin, Linux, Unix, Windows
4
+ :synopsis: Module contains the :class:`PowerAgentBase` object which is a base
5
+ class for object that allow for controlling the power of devices.
6
+
7
+ .. moduleauthor:: Myron Walker <myron.walker@gmail.com>
8
+
9
+ """
10
+
11
+ __author__ = "Myron Walker"
12
+ __copyright__ = "Copyright 2025, Code Craft AI LLC"
13
+ __credits__ = []
14
+
15
+
16
+ from typing import Union
17
+
18
+ from ccai.errors.exceptions import ConfigurationError
19
+
20
+ from ccai.credentials.basecredential import BaseCredential
21
+ from ccai.credentials.basiccredential import BasicCredential
22
+
23
+
24
+ class PowerAgentBase:
25
+ """
26
+ Base class for objects that control power of devices.
27
+ """
28
+
29
+ def __init__(self, host: str, credential: BaseCredential):
30
+ """
31
+ Initializes the :class:`PowerAgentBase` instance.
32
+
33
+ :param host: Hostname or address of the power device.
34
+ :param credential: Credentials used to authenticate to the device.
35
+ """
36
+ self._host = host
37
+ self._credential = credential
38
+ self._power_interfaces = {}
39
+ self._power_config = {}
40
+ return
41
+
42
+ def _lookup_power_interface(self, interface_name: str) -> Union[dict, None]:
43
+ """
44
+ Looks up a power interface by power interface name.
45
+
46
+ :param interface_name: The name of the interface to look up.
47
+
48
+ :returns: The power interface information or ``None`` if not found.
49
+ """
50
+ power_iface = None
51
+
52
+ if interface_name in self._power_interfaces:
53
+ power_iface = self._power_interfaces[interface_name]
54
+ else:
55
+
56
+ interface_config = self._power_config[interface_name]
57
+
58
+ powerType = interface_config["powerType"]
59
+
60
+ if powerType == "DliPowerSwitch":
61
+ try:
62
+ import dlipower
63
+ except ImportError:
64
+ errmsg = "The 'dlipower' package must be installed to use a 'DliPowerSwitch' power interface."
65
+ raise ConfigurationError(errmsg) from None
66
+
67
+ credobj: BasicCredential = self._credential
68
+ power_iface = dlipower.PowerSwitch(userid=credobj.username, password=credobj.password, hostname=self._host)
69
+
70
+ self._power_interfaces[interface_name] = power_iface
71
+ else:
72
+ errmsg = "Un-Supported power interface type={}.".format(powerType)
73
+ raise ConfigurationError(errmsg) from None
74
+
75
+ return power_iface
@@ -0,0 +1,29 @@
1
+ """
2
+ .. module:: serialagentbase
3
+ :platform: Darwin, Linux, Unix, Windows
4
+ :synopsis: Module contains the :class:`SerialAgentBase` object which is a base
5
+ class for objects that allow for sending commands to a device via a
6
+ serial interface.
7
+
8
+ .. moduleauthor:: Myron Walker <myron.walker@gmail.com>
9
+
10
+ """
11
+
12
+ __author__ = "Myron Walker"
13
+ __copyright__ = "Copyright 2025, Code Craft AI LLC"
14
+ __credits__ = []
15
+
16
+
17
+
18
+ from ccai.interop.interfaces.isystemcontext import ISystemContext
19
+
20
+ class SerialAgentBase(ISystemContext):
21
+ """
22
+ Base class for agents that interact over a serial connection.
23
+ """
24
+
25
+ def __init__(self) -> None:
26
+ """
27
+ Initializes the :class:`SerialAgentBase` instance.
28
+ """
29
+ return
@@ -0,0 +1,28 @@
1
+
2
+ """
3
+ .. module:: systemagentbase
4
+ :platform: Darwin, Linux, Unix, Windows
5
+ :synopsis: Module defining :class:`SystemAgentBase` which is the base class
6
+ for agents that execute system commands on a device.
7
+
8
+ .. moduleauthor:: Myron Walker <myron.walker@gmail.com>
9
+ """
10
+
11
+ __author__ = "Myron Walker"
12
+ __copyright__ = "Copyright 2025, Code Craft AI LLC"
13
+ __credits__ = []
14
+
15
+
16
+
17
+ from ccai.interop.interfaces.isystemcontext import ISystemContext
18
+
19
+ class SystemAgentBase(ISystemContext):
20
+ """
21
+ Base class for agents that execute system commands on a device.
22
+ """
23
+
24
+ def __init__(self) -> None:
25
+ """
26
+ Initializes the :class:`SystemAgentBase` instance.
27
+ """
28
+ return
@@ -0,0 +1,219 @@
1
+ """
2
+ .. module:: aspects
3
+ :platform: Darwin, Linux, Unix, Windows
4
+ :synopsis: Module that contains the :class:`Aspects` class and the constants used to provide aspect behaviors.
5
+
6
+ .. moduleauthor:: Myron Walker <myron.walker@gmail.com>
7
+ """
8
+
9
+ __author__ = "Myron Walker"
10
+ __copyright__ = "Copyright 2025, Code Craft AI LLC"
11
+ __credits__ = []
12
+
13
+
14
+ from typing import Tuple, Type, Optional, Union
15
+
16
+ from enum import IntEnum
17
+
18
+ import logging
19
+
20
+ from ccai.aspects.aspects import Aspects as AspectsBase
21
+ from ccai.xmodules.xlogging.enhancedloggers import getEnhancedLogger
22
+
23
+
24
+ class ActionPattern(IntEnum):
25
+ """
26
+ An action pattern to use when using aspects to change the behavior of an API.
27
+ """
28
+ SINGLE_CALL = 0
29
+ SINGLE_CONNECTED_CALL = 1
30
+ DO_UNTIL_SUCCESS = 2
31
+ DO_WHILE_SUCCESS = 3
32
+ DO_UNTIL_CONNECTION_FAILURE = 4
33
+
34
+ class LoggingPattern:
35
+ """
36
+ A logging pattern to use when logging commands.
37
+ """
38
+ ALL_RESULTS = 0
39
+ FAILURE_ONLY = 1
40
+ SUCCESS_ONLY = 2
41
+
42
+
43
+ DEFAULT_COMPLETION_TIMEOUT = 600
44
+ DEFAULT_COMPLETION_INTERVAL = 10
45
+
46
+ DEFAULT_CONNECTION_TIMEOUT = 16
47
+ DEFAULT_CONNECTION_INTERVAL = 2
48
+ DEFAULT_ALLOWED_CONNECTION_EXCEPTIONS: Tuple[Type[BaseException], ...] = ()
49
+
50
+ DEFAULT_INACTIVITY_TIMEOUT = 600
51
+ DEFAULT_INACTIVITY_INTERVAL = .5
52
+
53
+ DEFAULT_MONITOR_DELAY = 30
54
+
55
+ DEFAULT_RETRY_LOGGING_INTERVAL = 5
56
+
57
+ DEFAULT_LOGGING_PATTERN = LoggingPattern.ALL_RESULTS
58
+
59
+ DEFAULT_MUST_CONNECT = False
60
+
61
+ DEFAULT_ALLOWED_ERROR_CODES: Tuple[int, ...] = ()
62
+
63
+ class Aspects(AspectsBase):
64
+ """
65
+ Aspects are utilized with the interop APIs and agents such as the :class:`SSHAgent` class in order
66
+ to modify the behavior of APIs with respect to retry parameter such as timeout, interval, looping patterns
67
+ logging, etc. The aspects object provides a way to package this common criteria into a single parameter
68
+ or constant you can pass to multiple APIs
69
+
70
+ This class derives from :class:`ccai.aspects.aspects.Aspects`, the root of the aspects hierarchy,
71
+ so any interop aspects object is accepted wherever the contract-layer base is expected. Instances
72
+ compare by identity, are hashable and are frozen at the end of ``__init__``; a subclass that adds
73
+ fields sets them before calling ``super().__init__``.
74
+ """
75
+
76
+ def __init__(self, action_pattern: ActionPattern = ActionPattern.SINGLE_CALL,
77
+ completion_timeout: float = DEFAULT_COMPLETION_TIMEOUT,
78
+ completion_interval: float = DEFAULT_COMPLETION_INTERVAL,
79
+ connection_timeout: float = DEFAULT_CONNECTION_TIMEOUT,
80
+ connection_interval: float = DEFAULT_CONNECTION_INTERVAL,
81
+ inactivity_timeout: float = DEFAULT_INACTIVITY_TIMEOUT,
82
+ inactivity_interval: float = DEFAULT_INACTIVITY_INTERVAL,
83
+ monitor_delay: float = DEFAULT_MONITOR_DELAY,
84
+ logging_pattern: LoggingPattern = DEFAULT_LOGGING_PATTERN,
85
+ retry_logging_interval: int = DEFAULT_RETRY_LOGGING_INTERVAL,
86
+ allowed_error_codes: Tuple[int] = DEFAULT_ALLOWED_ERROR_CODES,
87
+ allowed_connection_exceptions: Tuple[BaseException] = DEFAULT_ALLOWED_CONNECTION_EXCEPTIONS,
88
+ must_connect: bool = DEFAULT_MUST_CONNECT,
89
+ max_attempts: int = 1,
90
+ logger_name: Optional[str] = None,
91
+ logger: Optional[logging.Logger]=None):
92
+ """
93
+ Creates an :class:`Aspects` instance package.
94
+
95
+ :param action_pattern: The :class:`ActionPattern` that the API should exhibit such as
96
+ SINGULAR, DO_UNTIL_SUCCESS, DO_WHILE_SUCCESS
97
+ :param completion_timeout: The time in seconds as a float that is the max time before
98
+ timeout for the activity to complete.
99
+ :param completion_interval: The time in seconds as a float that is waited before reattempting
100
+ an activity.
101
+ :param connection_timeout: The time in seconds as a float that is the timeout for making a connection.
102
+ :param connection_interval: The time in seconds as a float that is waited before reattempting
103
+ to connect.
104
+ :param inactivity_timeout: The time in seconds as a float that is the max time before timeout
105
+ that is waited before a :class:`TimeoutError` is raised due to inactivity.
106
+ :param inactivity_interval: The time in seconds as a float that is waited before reattempting an activity.
107
+ :param monitor_delay: Delay in seconds before monitoring activity after startup.
108
+ :param logging_pattern: Pattern controlling how command results are logged.
109
+ :param retry_logging_interval: The logging interval for retry loops.
110
+ :param allowed_error_codes: A list of error codes that allow retry
111
+ :param allowed_connection_exceptions: List of exceptions that do not count as connection failures.
112
+ :param must_connect: Used to indicate that connection failures should be raised.
113
+ :param max_attempts: Maximum attempts for callers that reconcile against a remote system.
114
+ :param logger_name: Optional logger name a caller can route logs into.
115
+ :param logger: Optional logger for output.
116
+ """
117
+ super().__init__(completion_interval=completion_interval, completion_timeout=completion_timeout,
118
+ connection_timeout=connection_timeout, max_attempts=max_attempts, logger_name=logger_name)
119
+
120
+ self.action_pattern = action_pattern
121
+ self.connection_interval = connection_interval
122
+ self.inactivity_timeout = inactivity_timeout
123
+ self.inactivity_interval = inactivity_interval
124
+ self.monitor_delay = monitor_delay
125
+ self.logging_pattern = logging_pattern
126
+ self.retry_logging_interval = retry_logging_interval
127
+ self.allowed_error_codes = tuple(allowed_error_codes)
128
+ self.allowed_connection_exceptions = tuple(allowed_connection_exceptions)
129
+ self.must_connect=must_connect
130
+
131
+ if logger is None:
132
+ self.logger = getEnhancedLogger()
133
+ else:
134
+ self.logger = logger
135
+
136
+ self.freeze()
137
+ return
138
+
139
+
140
+ class AspectsCmd(Aspects):
141
+ """
142
+ Extends :class:`Aspects` with command-specific parameters.
143
+ """
144
+ def __init__(self, expected_status: Union[int, Tuple[int]]=0,
145
+ user: Optional[str]=None,
146
+ pty_params: Optional[dict]=None,
147
+ action_pattern: ActionPattern = ActionPattern.SINGLE_CALL,
148
+ completion_timeout: float = DEFAULT_COMPLETION_TIMEOUT,
149
+ completion_interval: float = DEFAULT_COMPLETION_INTERVAL,
150
+ connection_timeout: float = DEFAULT_CONNECTION_TIMEOUT,
151
+ connection_interval: float = DEFAULT_CONNECTION_INTERVAL,
152
+ inactivity_timeout: float = DEFAULT_INACTIVITY_TIMEOUT,
153
+ inactivity_interval: float = DEFAULT_INACTIVITY_INTERVAL,
154
+ monitor_delay: float = DEFAULT_MONITOR_DELAY,
155
+ logging_pattern: LoggingPattern = DEFAULT_LOGGING_PATTERN,
156
+ retry_logging_interval: int = DEFAULT_RETRY_LOGGING_INTERVAL,
157
+ allowed_error_codes: Tuple[int] = DEFAULT_ALLOWED_ERROR_CODES,
158
+ allowed_connection_exceptions: Tuple[BaseException] = DEFAULT_ALLOWED_CONNECTION_EXCEPTIONS,
159
+ must_connect: bool = DEFAULT_MUST_CONNECT,
160
+ max_attempts: int = 1,
161
+ logger_name: Optional[str] = None,
162
+ logger: Optional[logging.Logger]=None):
163
+ """
164
+ Creates an :class:`AspectsCmd` instance.
165
+
166
+ :param expected_status: Status code or list of acceptable status codes.
167
+ :param user: Optional user name to use for command execution.
168
+ :param pty_params: Dictionary of pseudo-terminal parameters.
169
+ :param action_pattern: The :class:`ActionPattern` that the API should exhibit such as
170
+ SINGULAR, DO_UNTIL_SUCCESS, DO_WHILE_SUCCESS
171
+ :param completion_timeout: The time in seconds as a float that is the max time before
172
+ timeout for the activity to complete.
173
+ :param completion_interval: The time in seconds as a float that is waited before reattempting
174
+ an activity.
175
+ :param connection_timeout: The time in seconds as a float that is the timeout for making a connection.
176
+ :param connection_interval: The time in seconds as a float that is waited before reattempting
177
+ to connect.
178
+ :param inactivity_timeout: The time in seconds as a float that is the max time before timeout
179
+ that is waited before a :class:`TimeoutError` is raised due to inactivity.
180
+ :param inactivity_interval: The time in seconds as a float that is waited before reattempting an activity.
181
+ :param monitor_delay: Delay in seconds before monitoring activity after startup.
182
+ :param logging_pattern: Pattern controlling how command results are logged.
183
+ :param retry_logging_interval: The logging interval for retry loops.
184
+ :param allowed_error_codes: A list of error codes that allow retry
185
+ :param allowed_connection_exceptions: List of exceptions that do not count as connection failures.
186
+ :param must_connect: Used to indicate that connection failures should be raised.
187
+ :param max_attempts: Maximum attempts for callers that reconcile against a remote system.
188
+ :param logger_name: Optional logger name a caller can route logs into.
189
+ :param logger: Optional logger for output.
190
+ """
191
+ # Command fields are set first because the base initialiser freezes the instance.
192
+ self.expected_status = expected_status
193
+ self.user = user
194
+ self.pty_params = pty_params
195
+
196
+ super().__init__(action_pattern=action_pattern,
197
+ completion_timeout=completion_timeout,
198
+ completion_interval=completion_interval,
199
+ connection_timeout=connection_timeout,
200
+ connection_interval=connection_interval,
201
+ inactivity_timeout=inactivity_timeout,
202
+ inactivity_interval=inactivity_interval,
203
+ monitor_delay=monitor_delay,
204
+ logging_pattern=logging_pattern,
205
+ retry_logging_interval=retry_logging_interval,
206
+ allowed_error_codes=allowed_error_codes,
207
+ allowed_connection_exceptions=allowed_connection_exceptions,
208
+ must_connect=must_connect,
209
+ max_attempts=max_attempts,
210
+ logger_name=logger_name,
211
+ logger=logger)
212
+
213
+ return
214
+
215
+
216
+
217
+
218
+ DEFAULT_ASPECTS = Aspects()
219
+ DEFAULT_CMD_ASPECTS = AspectsCmd()
@@ -0,0 +1,5 @@
1
+
2
+
3
+ # Extension distributions ship their own ``ccai/interop/cli/cmdtree/<group>`` directories; merging
4
+ # every ``cli`` directory on sys.path here is what lets the ``cmdtree`` package below merge theirs.
5
+ __path__ = __import__("pkgutil").extend_path(__path__, __name__)