Last active
April 17, 2022 02:51
-
-
Save imtrinity94/38ca08e7253e404a95386e56ff9be6fc to your computer and use it in GitHub Desktop.
Automated Installation of vRO Plugin via Python Script
This file contains 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
# -*- coding: utf-8 -*- | |
""" | |
Automated Installation of vRO Plugin 1.0 | |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
It is a python script to allow quick installation of plugins inside vRealize Orchestrator. | |
""" | |
__author__ = "Mayank Goyal, Harshit Kesharwani" | |
import base64 | |
import csv | |
import email | |
import json | |
import os | |
import re | |
import smtplib | |
import time | |
import requests | |
import urllib3 | |
from pathlib import Path | |
from string import Template | |
urllib3.disable_warnings() | |
def deployPlugin(vroUrl, username, password, filename): | |
"""Function to deploy plugin. Calls sub-functions like updateInstallPlugin() & finishInstallPlugin().""" | |
url = (f"https://{vroUrl}/vco-controlcenter/api/plugins/installation") | |
print(url) | |
files = {'file': open(filename, 'rb')} | |
headers = {'Accept': 'application/json'} | |
response = requests.post(url, auth=(username, password), headers=headers, files=files, verify=False) | |
statusCode = response.status_code | |
if (statusCode == 201): | |
responseData = response.json() | |
payloadToUpdate = (responseData) | |
pluginInstallationID = responseData['id'] | |
pluginName = payloadToUpdate.get("plugins")[0].get("name") | |
print(f"pluginInstallationID : {pluginInstallationID}") | |
time.sleep(2) | |
updateStatus = updateInstallPlugin(vroUrl, username, password, pluginInstallationID, payloadToUpdate) | |
if updateStatus: | |
time.sleep(2) | |
finishStatus = finishInstallPlugin(vroUrl, username, password, pluginInstallationID,pluginName) | |
return finishStatus | |
else: | |
return False | |
else: | |
print(f"[ERROR]: Failed to execute Deploy request. Error code : {statusCode}") | |
return False | |
def updateInstallPlugin(vroUrl, username, password, pluginInstallationID, payloadToUpdate): | |
"""Function to update plugin state.""" | |
url = (f"https://{vroUrl}/vco-controlcenter/api/plugins/installation/{pluginInstallationID}") | |
#payload = "{\"eulaAccepted\":true,\"forceInstall\":true,\"installPluginStates\":[{\"state\":\"NEW_INSTALLED\"}],\"plugins\":[{\"enabled\":true,\"logLevel\":\"DEFAULT\"}]}" | |
t1 = payloadToUpdate.get("plugins")[0] | |
t1.__setitem__("enabled", True) | |
t1.__setitem__("logLevel", "DEFAULT") | |
payloadToUpdate.__setitem__("plugins", [t1]) | |
t2 = { | |
"name": str(payloadToUpdate.get("plugins")[0].get("name")), | |
"newBuild": str(payloadToUpdate.get("plugins")[0].get("buildNumber")), | |
"newVersion": str(payloadToUpdate.get("plugins")[0].get("version")), | |
"state": "NEW_INSTALLED" | |
} | |
payloadToUpdate.__setitem__("installPluginStates", [t2]) | |
payloadToUpdate.__setitem__("eulaAccepted", True) | |
payloadToUpdate.__setitem__("forceInstall", True) | |
payload = json.dumps(payloadToUpdate) | |
headers = {'Content-Type': 'application/json'} | |
response = requests.request("POST", url, auth=(username, password), headers=headers, data=payload, verify=False) | |
statusCode = response.status_code | |
if (statusCode == 200): | |
return True | |
else: | |
print(f"[ERROR]: Failed to execute Update request. Error code : {statusCode}") | |
return False | |
def finishInstallPlugin(vroUrl, username, password, pluginInstallationID, pluginName): | |
"""Function to finalize the plugin installation.""" | |
url = (f"https://{vroUrl}/vco-controlcenter/api/plugins/installation/{pluginInstallationID}/finish") | |
headers = {'Content-Type': 'application/json'} | |
response = requests.request("POST", url, auth=(username, password), headers=headers, verify=False) | |
statusCode = response.status_code | |
if (statusCode == 200): | |
responseData = response.json() | |
print(f"Enable plugin name {pluginName}") | |
time.sleep(2) | |
changePluginState(vroUrl, username, password, pluginName) | |
return True | |
else: | |
print(f"[ERROR]: Failed to execute Finish request. Error code : {statusCode}") | |
return False | |
def changePluginState(vroUrl, username, password, pluginName): | |
"""Function to change set enabled=true which actually completes the installation process.""" | |
url = (f"https://{vroUrl}/vco-controlcenter/api/plugins/state") | |
payload = json.dumps([{"enabled": True, "name": pluginName}]) | |
headers = {'Content-Type': 'application/json'} | |
response = requests.request("POST", url, auth=(username, password), headers=headers, data=payload, verify=False) | |
statusCode = response.status_code | |
if (statusCode == 200): | |
return True | |
else: | |
print(f"[ERROR]: Failed to execute changePluginState request. Error code : {statusCode}") | |
return False | |
def checkPluginInstallation(vroUrl, username, password, pluginInstallationID): | |
"""Function to check if installation is completed or not.""" | |
url = (f"https://{vroUrl}/vco-controlcenter/api/plugins/installation/{pluginInstallationID}") | |
headers = { | |
'Content-Type': 'application/json', | |
'Accept': 'application/json' | |
} | |
response = requests.request("GET", url, auth=(username, password), headers=headers, verify=False) | |
statusCode = response.status_code | |
print(f"statusCode : {statusCode}") | |
if (statusCode == 200): | |
return True | |
else: | |
print(f"[ERROR]: Failed to execute checkPluginInstallation request. Error code : {statusCode}") | |
return False | |
if __name__ == '__main__': | |
#User provided data | |
vroUrl = input("Enter vRO FQDN: [Eg: fqdn.vro.local] ") | |
username = input("Enter username: [Tip: try root] ") | |
password = input("Enter password: [Tip: try root password] ") | |
filename = input("Enter full path of plugin: [Eg: C:/Users/abcd/Downloads/plugin_file.vmoapp] ") | |
deployPlugin(vroUrl, username, password, filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment