|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +* ******************************************************* |
| 4 | +* Copyright (c) VMware, Inc. 2022. All Rights Reserved. |
| 5 | +* SPDX-License-Identifier: MIT |
| 6 | +* ******************************************************* |
| 7 | +* |
| 8 | +* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT |
| 9 | +* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN, |
| 10 | +* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED |
| 11 | +* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, |
| 12 | +* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | +""" |
| 14 | + |
| 15 | +__author__ = 'VMware, Inc.' |
| 16 | +__vcenter_version__ = '8.0' |
| 17 | + |
| 18 | +from com.vmware.appliance.update_client import Pending, Staged |
| 19 | +from com.vmware.appliance_client import Update |
| 20 | + |
| 21 | +from samples.vsphere.common import sample_cli, sample_util |
| 22 | +from samples.vsphere.common.ssl_helper import get_unverified_session |
| 23 | +from samples.vsphere.vcenter.hcl.utils import get_configuration |
| 24 | +import time |
| 25 | + |
| 26 | + |
| 27 | +class SampleUpdate(object): |
| 28 | + """ |
| 29 | + Sample demonstrating Patching APIs |
| 30 | + Sample Prerequisites: |
| 31 | + vCenter on linux platform |
| 32 | + """ |
| 33 | + |
| 34 | + def __init__(self): |
| 35 | + parser = sample_cli.build_arg_parser() |
| 36 | + parser.add_argument('-com', '--component', |
| 37 | + action='store', |
| 38 | + required=False, |
| 39 | + help='Component to be updated.') |
| 40 | + |
| 41 | + parser.add_argument('-url', '--url', |
| 42 | + action='store', |
| 43 | + required=False, |
| 44 | + help='Custom target url.') |
| 45 | + args = sample_util.process_cli_args(parser.parse_args()) |
| 46 | + # cis session |
| 47 | + session = get_unverified_session() if args.skipverification else None |
| 48 | + stub_config = get_configuration( |
| 49 | + args.server, args.username, args.password, |
| 50 | + session) |
| 51 | + self.url = args.url if args.url else None |
| 52 | + self.component = args.component if args.component else None |
| 53 | + self.pending_client = Pending(stub_config) |
| 54 | + self.staged_client = Staged(stub_config) |
| 55 | + self.appliance_client = Update(stub_config) |
| 56 | + self.password = args.password |
| 57 | + |
| 58 | + def run(self): |
| 59 | + """ |
| 60 | + Access the Pending APIs to list and do a pending update |
| 61 | + """ |
| 62 | + |
| 63 | + version_list = self.pending_client.list("LOCAL_AND_ONLINE", self.url) |
| 64 | + print("\nFull patch update list: \n", version_list) |
| 65 | + version = version_list[0].version |
| 66 | + |
| 67 | + # Upgradeable Component List |
| 68 | + if self.component: |
| 69 | + component_list = self.pending_client.list_upgradeable_components(version) |
| 70 | + # component = component_list[0]["component"] |
| 71 | + print("\nUpgradeable Component list: \n", component_list) |
| 72 | + |
| 73 | + # get Update info |
| 74 | + update_info = self.pending_client.get(version, self.component) |
| 75 | + print("\nGet Information of Update: ") |
| 76 | + ServicesToBeStopped = [x.service for x in update_info.services_will_be_stopped] |
| 77 | + print("name: ", update_info.name) |
| 78 | + print("services_will_be_stopped: ", ServicesToBeStopped) |
| 79 | + print("staged: ", update_info.staged) |
| 80 | + print("knowledge_base: ", update_info.knowledge_base) |
| 81 | + print("priority: ", update_info.priority) |
| 82 | + print("severity: ", update_info.severity) |
| 83 | + print("update_type: ", update_info.update_type) |
| 84 | + print("release_date: ", update_info.release_date) |
| 85 | + print("reboot_required: ", update_info.reboot_required) |
| 86 | + print("size: ", update_info.size) |
| 87 | + |
| 88 | + user_data = {"vmdir.password": self.password} |
| 89 | + # Precheck Update |
| 90 | + precheck_result = self.pending_client.precheck(version, self.component) |
| 91 | + print("\nPrecheck result : \n", precheck_result) |
| 92 | + for question in precheck_result.questions: |
| 93 | + print("Please provide answer to following question") |
| 94 | + print(question.text.default_message) |
| 95 | + print("Question Description : ", question.description.default_message) |
| 96 | + print("Provide your answer: ") |
| 97 | + ans = str(input()) |
| 98 | + user_data[question.data_item] = ans |
| 99 | + |
| 100 | + # Validate an Update |
| 101 | + validate_result = self.pending_client.validate(version, user_data, self.component) |
| 102 | + print("\nValidate result for update: \n", validate_result) |
| 103 | + |
| 104 | + # Stage an Update |
| 105 | + |
| 106 | + self.pending_client.stage(version, self.component) |
| 107 | + print("Staging the update") |
| 108 | + |
| 109 | + # Monitor Stage |
| 110 | + monitor_stage = self.appliance_client.get() |
| 111 | + while monitor_stage.task.status == "RUNNING": |
| 112 | + time.sleep(50) |
| 113 | + monitor_stage = self.appliance_client.get() |
| 114 | + |
| 115 | + print("State: ", monitor_stage.state) |
| 116 | + print("Status: ", monitor_stage.task.status) |
| 117 | + if monitor_stage.task.status == "FAILED": |
| 118 | + print("") |
| 119 | + return |
| 120 | + print("\nStage result: \n", monitor_stage) |
| 121 | + |
| 122 | + staged_result = self.staged_client.get() |
| 123 | + print("\nStaged Update: ", staged_result) |
| 124 | + |
| 125 | + # Install an update |
| 126 | + self.pending_client.install(version, user_data, self.component) |
| 127 | + |
| 128 | + # Monitor Install |
| 129 | + print("Installing the update") |
| 130 | + monitor_install = self.appliance_client.get() |
| 131 | + while monitor_install.task.status == "RUNNING": |
| 132 | + monitor_install = self.appliance_client.get() |
| 133 | + print("\nInstall result: \n", monitor_install) |
| 134 | + |
| 135 | + |
| 136 | +def main(): |
| 137 | + """ |
| 138 | + Entry point for the sample client |
| 139 | + """ |
| 140 | + pending_update = SampleUpdate() |
| 141 | + pending_update.run() |
| 142 | + |
| 143 | + |
| 144 | +if __name__ == '__main__': |
| 145 | + main() |
0 commit comments