-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_cmd.py
64 lines (54 loc) · 2.38 KB
/
run_cmd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import logging
from cterasdk import CTERAException
from filer import get_filer, get_filers
def single_filer_run(filer, command: str):
"""Run command against a single device on current tenant.
:param str command: command to run
:param str device_name: Name of device on current tenant
"""
try:
response = filer.cli.run_command(command)
logging.info(response)
logging.info("Finished single run_cmd task on %s", filer.name)
except AttributeError as ae:
logging.debug(ae)
except CTERAException as ce:
logging.debug(ce)
logging.info("Failed run_cmd task on %s", filer.name)
def multi_filer_run(self, command: str, all_tenants=False):
"""Run command against all devices on a tenant or all tenants.
:param str command: command to run
:param bool,optional all_tenants: Scan all tenants
"""
filers = get_filers(self, all_tenants)
for filer in filers:
try:
logging.info("Running command on: %s", filer.name)
response = filer.cli.run_command(command)
logging.info(response)
logging.info("Finished command on: %s", filer.name)
except CTERAException as error:
logging.debug(error)
logging.warning("Something went wrong running the command on %s", filer.name)
def run_cmd(self, command: str, tenant_name=None, all_tenants=False, device_name=None):
"""Run a "hidden CLI command" on connected Filers.
i.e. execute a RESTful API request to connected Filers, and
print the response. On CLI, quote the command string.
:param str command: command to be run
:param bool,optional all_tenants: Scan all tenants true or false
:param str,optional device_name: Name of device on current tenant
"""
logging.info('Starting run_cmd task.')
try:
#tenant = self.users.session().user.tenant
if device_name:
filer = get_filer(self, device_name, tenant_name)
single_filer_run(filer, command)
elif all_tenants is True:
multi_filer_run(self, command, all_tenants=True)
logging.info('Finished run_cmd task on all Filers.')
else:
multi_filer_run(self, command, all_tenants=False)
logging.info("Finished run_cmd task on all Filers in Tenant: %s", tenant_name)
except Exception as e:
logging.warning("An error occurred: " + str(e))