Last active
August 29, 2015 14:06
-
-
Save jcpowermac/8934b72a95af0572817d to your computer and use it in GitHub Desktop.
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
| __author__ = 'jcallen' | |
| from subprocess import Popen, PIPE | |
| import syslog | |
| import httplib | |
| import sys | |
| import re | |
| import json | |
| import traceback | |
| vib_syslog_key = "VIB-KS" | |
| ''' | |
| If we receive something other than zip or vib this will be the exception we | |
| raise. Don't really need to modify it just inheriting the base Exception class | |
| ''' | |
| class IncorrectFileType(Exception): | |
| pass | |
| ''' | |
| Executes process using the shell, waits for the command to exit and | |
| returns returns output (string), err (string), returncode (int) | |
| ''' | |
| def execute_process(cmd_line): | |
| try: | |
| process = Popen(cmd_line, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True) | |
| process.wait() | |
| output, err = process.communicate() | |
| returncode = process.returncode | |
| return output, err, returncode | |
| except Exception as e: | |
| syslog.syslog("%s Exception %s in execute_process()" % (vib_syslog_key, str(e))) | |
| sys.exit(1) | |
| ''' | |
| From the web server retrieve the JSON string and convert to Python dictionary then return | |
| ''' | |
| def get_url_list(source_address, port, url): | |
| try: | |
| http_connection = httplib.HTTPConnection(source_address, port) | |
| http_connection.request("GET", url) | |
| response = http_connection.getresponse() | |
| vibs = json.load(response) | |
| except Exception as e: | |
| syslog.syslog("%s Exception %s in get_url_list()" % (vib_syslog_key, str(e))) | |
| sys.exit(1) | |
| finally: | |
| http_connection.close() | |
| return vibs | |
| ''' | |
| Using vim-cmd find the location of the scratch partition that can be used to download a ZIP | |
| based package. The output of the process needs to be parsed specifically for the value of | |
| the ConfiguredScratchLocation which is the UUID based path | |
| ''' | |
| def find_scratch_location(): | |
| output, err, returncode = execute_process('vim-cmd hostsvc/advopt/view ScratchConfig.ConfiguredScratchLocation') | |
| if returncode == 0: | |
| match = re.search('value = "(.*)"', output) | |
| if match: | |
| print match.group(1) | |
| return match.group(1) | |
| else: | |
| return "" | |
| ''' | |
| Check to make sure that we have a scratch partition. | |
| Parse result of vim-cmd, which will provide path of scratch | |
| Download zip package | |
| return empty string or path of local zip | |
| ''' | |
| def download_zip_package(url): | |
| scratch = find_scratch_location() | |
| if len(scratch) != 0: | |
| split_url = url.split('/') | |
| file_name = split_url[len(split_url)-1] | |
| filename_with_path = "%s/%s" % (scratch, file_name) | |
| cmd_line = "wget -q -O %s %s" % (filename_with_path, url) | |
| syslog.syslog("%s Executing %s" % (vib_syslog_key, cmd_line)) | |
| output, err, returncode = execute_process(cmd_line) | |
| return filename_with_path, returncode | |
| else: | |
| #No scratch partition cannot install! | |
| return "", 1 | |
| ''' | |
| Iterates over vibs with the corresponding command line to install VIB or ZIP packagess | |
| NOTES: I have removed exceptions from this method purposely. It could be a single VIB or ZIP that fails to | |
| install but the others may complete. | |
| ''' | |
| def do_software_vib_install(vibs): | |
| cmd_line = "" | |
| returncode = 0 | |
| for v in vibs: | |
| try: | |
| if v['url'].endswith("zip"): | |
| filename_with_path, returncode = download_zip_package(v['url']) | |
| cmd_line = "esxcli software vib install %s -d %s" % (("-f" if v['force'] else ""), filename_with_path) | |
| elif v['url'].endswith("vib"): | |
| cmd_line = "esxcli software vib install %s -v %s" % (("-f" if v['force'] else ""), v['url']) | |
| else: | |
| raise IncorrectFileType() | |
| if returncode == 0: | |
| syslog.syslog("%s Executing %s" % (vib_syslog_key, cmd_line)) | |
| output, err, returncode = execute_process(cmd_line) | |
| if returncode != 0: | |
| syslog.syslog("%s ERROR %s in do_software_vib_install()" % (vib_syslog_key, output)) | |
| else: | |
| syslog.syslog("%s ERROR %s in do_software_vib_install()" % (vib_syslog_key, v['url'])) | |
| except: | |
| syslog.syslog("%s ERROR %s in do_software_vib_install()" % (vib_syslog_key, v['url'])) | |
| def install_esxi_packages(source_address, port, url): | |
| try: | |
| vibs = get_url_list(source_address, port, url) | |
| do_software_vib_install(vibs) | |
| except IncorrectFileType: | |
| print "Incorrect file type for adding package to ESXi" | |
| sys.exit(1) | |
| except: | |
| print traceback.format_exc() | |
| sys.exit(1) | |
| install_esxi_packages("10.53.252.253", "8080", "/static/package.json") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment