From 2c4b8e55786bc93e10ecb9344ff7a90da42707dd Mon Sep 17 00:00:00 2001 From: s-finn <37446215+s-finn@users.noreply.github.com> Date: Wed, 15 Jul 2020 12:38:37 -0400 Subject: [PATCH] Add files via upload --- VRA Install/README.md | 16 +++++ VRA Install/vra_install.py | 138 +++++++++++++++++++++++++++++++++++++ VRA Install/vras_json.json | 24 +++++++ 3 files changed, 178 insertions(+) create mode 100644 VRA Install/README.md create mode 100644 VRA Install/vra_install.py create mode 100644 VRA Install/vras_json.json diff --git a/VRA Install/README.md b/VRA Install/README.md new file mode 100644 index 0000000..97d1407 --- /dev/null +++ b/VRA Install/README.md @@ -0,0 +1,16 @@ +# Legal Disclaimer +This script is an example script and is not supported under any Zerto support program or service. The author and Zerto further disclaim all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. + +In no event shall Zerto, its authors or anyone else involved in the creation, production or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or the inability to use the sample scripts or documentation, even if the author or Zerto has been advised of the possibility of such damages. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. + +# Python---Zerto-VRA-Deployment- +Python based Zerto VRA deployment, user will need to fill out the vras.json file first for each host they would like to deploy a Zerto VRA on. The json file will then be read in by Python to determine the VRA installation options. Within the JSON example they will need to specificy required VRA install options including datastore, network, vra group, as well as static IP information + +# Environment Requirements +- Python 3.7 +- Network access to Zerto Virtual Manager(ZVM) + +Script Requirements +- ZVM IP, user, password with permissions to access the API of the ZVM +- vras.json file completed and location specified within Python +- vras.json file accessible by host running Python diff --git a/VRA Install/vra_install.py b/VRA Install/vra_install.py new file mode 100644 index 0000000..b484099 --- /dev/null +++ b/VRA Install/vra_install.py @@ -0,0 +1,138 @@ +#This script is an example script and is not supported under any Zerto support program or service. The author and Zerto further disclaim all implied warranties including, without +#limitation, any implied warranties of merchantability or of fitness for a particular purpose. + +#In no event shall Zerto, its authors or anyone else involved in the creation, production or delivery of the scripts be liable for any damages whatsoever (including, without +#limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or the inability to use +#the sample scripts or documentation, even if the author or Zerto has been advised of the possibility of such damages. The entire risk arising out of the use or performance of +#the sample scripts and documentation remains with you. + +#importing request, json +#importing HTTPBasicAuth library for ZVM basic authentication +import requests +import json +from requests.auth import HTTPBasicAuth +import urllib3 +urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) +from time import sleep + +#Declaring Environment variables +zvm_ip = "ZVMIP" +zvm_u = "ZVMUser" +zvm_p = "ZVMPassword" +base_url = f"https://{zvm_ip}:9669/v1" +session = f"{base_url}/session/add" +vrainstall_url = f"{base_url}/vras" + +###Functions#### +def login(session_url, zvm_user, zvm_password): + print("Getting ZVM API token...") + auth_info = "{\r\n\t\"AuthenticationMethod\":1\r\n}" + headers = { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + } + response = requests.post(session_url, headers=headers, data=auth_info, verify=False, auth=HTTPBasicAuth(zvm_user, zvm_password)) + if response.ok: + auth_token = response.headers['x-zerto-session'] + print("Api Token: " + auth_token) + return auth_token + else: + print("HTTP %i - %s, Message %s" % (response.status_code, response.reason, response.text)) + +returned_token = login(session, zvm_u, zvm_p) + +# Creating Header with x-zerto-session +headers = { + 'Accept': 'application/json', + 'Content-Type': 'application/json', + 'x-zerto-session': returned_token +} + +#Gather ZVM Site ID for future use +site_url = f"{base_url}/localsite" +site_return = requests.get(site_url, headers=headers, verify=False) +site_return = site_return.json() +site_id = site_return.get('SiteIdentifier') + +#Gather network IDs for future use +network_url = f"{base_url}/virtualizationsites/{site_id}/networks" +network_ids = requests.get(network_url, headers=headers, verify=False) +network_ids = network_ids.json() + + +#Gather host IDs for future use +host_url = f"{base_url}/virtualizationsites/{site_id}/hosts" +host_ids = requests.get(host_url, headers=headers, verify=False) +host_ids = host_ids.json() + +#Gather Datastore IDs for future use +datastore_url = f"{base_url}/virtualizationsites/{site_id}/datastores" +datastore_ids = requests.get(datastore_url, headers=headers, verify=False) +datastore_ids = datastore_ids.json() + + +#Read in JSON configuration file +with open('vra_json file path', 'r') as f: + vra_configuration = json.load(f) +f.closed + + +#Iterate through each host listed in vras JSON +#Gather MoRefs, IPs to POST JSON request + +for host in vra_configuration['Hosts']: + datastore_name = host['DatastoreName'] + network_name = host['PortGroup'] + host_name = host['ESXiHostName'] + memory = host['MemoryGB'] + vra_group = host['VRAGroup'] + vra_gateway = host['DefaultGateway'] + vra_subnet = host['SubnetMask'] + vra_ip = host['VRAIPAddress'] + + #Iterate through all networks returned by Zerto to find MoRef + for network in network_ids: + if network['VirtualizationNetworkName'] == network_name: + network_moref = network['NetworkIdentifier'] + else: + pass + #Iterate through all hosts returned by Zerto to find MoRef + for host in host_ids: + if host['VirtualizationHostName'] == host_name: + host_moref = host['HostIdentifier'] + else: + pass + #Iterate through all datastores returned by Zerto to find MoRef + for datastore in datastore_ids: + if datastore['DatastoreName'] == datastore_name: + datastore_moref = datastore['DatastoreIdentifier'] + else: + pass + + #Build VRA dict containing morefs and static IPs + vra_dict = { + "DatastoreIdentifier": datastore_moref, + "GroupName": vra_group , + "HostIdentifier": host_moref, + "HostRootPassword": None, + "MemoryInGb": memory, + "NetworkIdentifier": network_moref, + "UsePublicKeyInsteadOfCredentials": True, + "VraNetworkDataApi": { + "DefaultGateway": vra_gateway, + "SubnetMask": vra_subnet, + "VraIPAddress": vra_ip, + "VraIPConfigurationTypeApi": "Static" + } + } + + #Convert VRA dict to JSON, post request for install to /vras + vra_json = json.dumps(vra_dict) + response = requests.post(vrainstall_url, data=vra_json, headers=headers, verify=False) + if response.status_code != 200: + print(response.text) + + #Wait 30 seconds between VRA Installation tasks + sleep(30) +print("") + diff --git a/VRA Install/vras_json.json b/VRA Install/vras_json.json new file mode 100644 index 0000000..ae220b1 --- /dev/null +++ b/VRA Install/vras_json.json @@ -0,0 +1,24 @@ +{ + "Hosts": [ + { + "ESXiHostName": "HostName", + "DatastoreName": "DatastoreName", + "PortGroup": "PortGroupName", + "VRAGroup": "VRAGroupName", + "MemoryGB": "RAMinGB", + "DefaultGateway": "GatewayIP", + "SubnetMask": "SubnetMask", + "VRAIPAddress": "VRAStaticIP" + }, + { + "ESXiHostName": "HostName", + "DatastoreName": "DatastoreName", + "PortGroup": "PortGroupName", + "VRAGroup": "VRAGroupName", + "MemoryGB": "RAMinGB", + "DefaultGateway": "GatewayIP", + "SubnetMask": "SubnetMask", + "VRAIPAddress": "VRAStaticIP" + } + ] +}