Created
May 5, 2021 16:07
-
-
Save f5-rahm/3859467c9608d0f5c8492746d00fa1b3 to your computer and use it in GitHub Desktop.
Upload the F5 Automation Toolchain packages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import sys | |
from time import sleep | |
from bigrest.bigip import BIGIP | |
from bigrest.common.exceptions import RESTAPIError | |
from pathlib import Path | |
DO = "/Users/rahm/Downloads/f5-declarative-onboarding-1.20.0-2.noarch.rpm" | |
AS3 = "/Users/rahm/Downloads/f5-appsvcs-3.27.0-3.noarch.rpm" | |
TS = "/Users/rahm/Downloads/f5-telemetry-1.19.0-3.noarch.rpm" | |
RPM = [DO, AS3, TS] | |
def build_parser(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("device", help="BIG-IP address or FQDN") | |
parser.add_argument("user", help="BIG-IP username") | |
parser.add_argument("password", help="BIG-IP password") | |
return parser.parse_args() | |
def instantiate_bigip(host, user, password): | |
try: | |
obj = BIGIP(host, user, password) | |
except Exception as e: | |
print(f"Failed to connect to {host} due to {type(e).__name__}:\n") | |
print(f"{e}") | |
sys.exit() | |
return obj | |
def upload_file(obj, file_name): | |
try: | |
obj.upload(f"/mgmt/shared/file-transfer/uploads/", file_name) | |
except Exception as e: | |
print(f"Failed to upload the component due to {type(e).__name__}:\n") | |
print(f"{e}") | |
sys.exit() | |
def install_package(obj, file_name): | |
try: | |
data = {"operation": "INSTALL", "packageFilePath": f"/var/config/rest/downloads/{Path(file_name).name}"} | |
obj.command("/mgmt/shared/iapp/package-management-tasks", data) | |
except Exception as e: | |
print(f"Failed to install the component due to {type(e).__name__}:\n") | |
print(f"{e}") | |
sys.exit() | |
def verify_package(obj, file_name): | |
component = Path(file_name).name | |
if "f5-declarative-onboarding" in component: | |
try: | |
result = obj.load("/mgmt/shared/declarative-onboarding/info") | |
if result.properties[0].get('version') in component: | |
return True | |
else: | |
return False | |
except RESTAPIError: | |
return False | |
elif "f5-appsvcs" in component: | |
try: | |
result = obj.load("/mgmt/shared/appsvcs/info") | |
if result.properties.get('version') in component: | |
return True | |
else: | |
return False | |
except RESTAPIError: | |
return False | |
elif "f5-telemetry" in component: | |
try: | |
result = obj.load("/mgmt/shared/telemetry/info") | |
if result.properties.get('version') in component: | |
return True | |
else: | |
return False | |
except RESTAPIError: | |
return False | |
else: | |
return False | |
if __name__ == "__main__": | |
args = build_parser() | |
host = args.device | |
user = args.user | |
pw = args.password | |
print(f"\nInstantiating BIG-IP (host {host})") | |
bigip = instantiate_bigip(host, user, pw) | |
# Upload the packages | |
print("\nUploading packages") | |
for package in RPM: | |
package_name = Path(package).name | |
print(f"\tUploading {package_name}") | |
upload_file(bigip, package) | |
# Install the packages | |
print("\nInstalling packages") | |
for package in RPM: | |
package_name = Path(package).name | |
print(f"\tInstalling {package_name}") | |
install_package(bigip, package) | |
print("\nQuick break to register packages...") | |
sleep(20) | |
# Verify the packages | |
print("\nVerifying packages") | |
for package in RPM: | |
package_name = Path(package).name | |
if verify_package(bigip, package): | |
print(f"\t{package_name} installed and verified") | |
else: | |
print(f"\t{package_name} cannot be verified") | |
print("\n\n---complete---\n\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment