Skip to content

Commit

Permalink
add work in progress setup server plugin service
Browse files Browse the repository at this point in the history
  • Loading branch information
jgstew committed Jun 6, 2024
1 parent d0f4a86 commit 99eb5af
Showing 1 changed file with 213 additions and 0 deletions.
213 changes: 213 additions & 0 deletions examples/setup_server_plugin_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
"""
Setup the root server server plugin service with creds provided
requires `besapi`, install with command `pip install besapi`
Example Usage:
python setup_server_plugin_service.py -r https://localhost:52311/api -u API_USER -p API_PASSWORD
References:
- https://developer.bigfix.com/rest-api/api/admin.html
- https://github.com/jgstew/besapi/blob/master/examples/rest_cmd_args.py
- https://github.com/jgstew/tools/blob/master/Python/locate_self.py
"""

import argparse
import configparser
import getpass
import logging
import logging.handlers
import os
import platform
import sys

import besapi

__version__ = "0.0.1"
verbose = 0
bes_conn = None
invoke_folder = None
config_ini = None


def get_invoke_folder():
"""Get the folder the script was invoked from
References:
- https://github.com/jgstew/tools/blob/master/Python/locate_self.py
"""
# using logging here won't actually log it to the file:

if getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS"):
if verbose:
print("running in a PyInstaller bundle")
invoke_folder = os.path.abspath(os.path.dirname(sys.executable))
else:
if verbose:
print("running in a normal Python process")
invoke_folder = os.path.abspath(os.path.dirname(__file__))

if verbose:
print(f"invoke_folder = {invoke_folder}")

return invoke_folder


def test_file_exists(path):
"""return true if file exists"""

if not (os.path.isfile(path) and os.access(path, os.R_OK)):
path = os.path.join(invoke_folder, path)

logging.info("testing if exists: `%s`", path)

if os.path.isfile(path) and os.access(path, os.R_OK) and os.access(path, os.W_OK):
return path

return False


def main():
"""Execution starts here"""
print("main() start")

parser = argparse.ArgumentParser(
description="Provde command line arguments for REST URL, username, and password"
)
parser.add_argument(
"-v",
"--verbose",
help="Set verbose output",
required=False,
action="count",
default=0,
)
parser.add_argument(
"-besserver", "--besserver", help="Specify the BES URL", required=False
)
parser.add_argument("-r", "--rest-url", help="Specify the REST URL", required=False)
parser.add_argument("-u", "--user", help="Specify the username", required=False)
parser.add_argument("-p", "--password", help="Specify the password", required=False)
# allow unknown args to be parsed instead of throwing an error:
args, _unknown = parser.parse_known_args()

# allow set global scoped vars
global bes_conn, verbose, config_ini, invoke_folder
verbose = args.verbose

# get folder the script was invoked from:
invoke_folder = get_invoke_folder()

# set different log levels:
log_level = logging.INFO
if verbose:
log_level = logging.INFO
if verbose > 1:
log_level = logging.DEBUG

# get path to put log file in:
log_filename = os.path.join(invoke_folder, "serversettings.log")

print(f"Log File Path: {log_filename}")

handlers = [
logging.handlers.RotatingFileHandler(
log_filename, maxBytes=5 * 1024 * 1024, backupCount=1
)
]

# log output to console if arg provided:
if verbose:
handlers.append(logging.StreamHandler())

# setup logging:
logging.basicConfig(
encoding="utf-8",
level=log_level,
format="%(asctime)s %(levelname)s:%(message)s",
handlers=handlers,
)
logging.info("----- Starting New Session ------")
logging.debug("invoke folder: %s", invoke_folder)
logging.debug("Python version: %s", platform.sys.version)
logging.debug("BESAPI Module version: %s", besapi.besapi.__version__)
logging.debug("this plugin's version: %s", __version__)

password = args.password

if not password:
logging.warning("Password was not provided, provide REST API password.")
print("Password was not provided, provide REST API password.")
password = getpass.getpass()

# process args, setup connection:
rest_url = args.rest_url

# normalize url to https://HostOrIP:52311
if rest_url and rest_url.endswith("/api"):
rest_url = rest_url.replace("/api", "")

try:
bes_conn = besapi.besapi.BESConnection(args.user, password, rest_url)
# bes_conn.login()
except (
AttributeError,
ConnectionRefusedError,
besapi.besapi.requests.exceptions.ConnectionError,
):
try:
# print(args.besserver)
bes_conn = besapi.besapi.BESConnection(args.user, password, args.besserver)
# handle case where args.besserver is None
# AttributeError: 'NoneType' object has no attribute 'startswith'
except AttributeError:
bes_conn = besapi.besapi.get_bes_conn_using_config_file()

root_id = int(
bes_conn.session_relevance_string(
"unique value of ids of bes computers whose(root server flag of it)"
)
)

# print(root_id)

InstallPluginService_id = int(
bes_conn.session_relevance_string(
'unique value of ids of fixlets whose(name of it contains "Install BES Server Plugin Service" AND exists applicable computers of it) of bes sites whose(name of it = "BES Support")'
)
)

# print(InstallPluginService_id)
BES_SourcedFixletAction = f"""\
<BES xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="BES.xsd">
<SourcedFixletAction>
<SourceFixlet>
<Sitename>BES Support</Sitename>
<FixletID>{InstallPluginService_id}</FixletID>
<Action>Action1</Action>
</SourceFixlet>
<Target>
<ComputerID>{root_id}</ComputerID>
</Target>
<Settings>
<HasEndTime>true</HasEndTime>
<EndDateTimeLocalOffset>P10D</EndDateTimeLocalOffset>
<ContinueOnErrors>true</ContinueOnErrors>
</Settings>
</SourcedFixletAction>
</BES>
"""

if InstallPluginService_id > 0:
# create action to setup server plugin service:
action_result = bes_conn.post("actions", BES_SourcedFixletAction)
print(action_result)

# NOTE: Work in progress

logging.info("----- Ending Session ------")
print("main() End")


if __name__ == "__main__":
main()

0 comments on commit 99eb5af

Please sign in to comment.