Created
November 11, 2018 09:47
-
-
Save gowatana/9e197d734bd2b17ca69b5ccec3552fea to your computer and use it in GitHub Desktop.
Nutanix CE の VM を パワーオン / オフする。
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
import sys | |
import json | |
import requests | |
import time | |
args = sys.argv | |
conf_file = args[1] | |
vm_list_file = args[2] | |
power_state = args[3] | |
seconds_to_wait_task_report = 5 | |
def main(): | |
with open(conf_file, "r") as file: | |
conf = file.read() | |
conf = json.loads(conf) | |
prism_addr = conf["prism_address"] | |
prism_user = conf["user_name"] | |
prism_pass = conf["password"] | |
vm_list = [] | |
with open(vm_list_file, "r") as file: | |
vm_list_from_file = file.readlines() | |
for vm_name in vm_list_from_file: | |
vm_name = vm_name.strip() | |
if(len(vm_name) != 0): | |
vm_list.append(vm_name) | |
print('Change VM State: ' + power_state + ' ' + vm_name) | |
base_url = 'https://' + prism_addr + ':9440/PrismGateway/services/rest/v2.0/' | |
insecure_warn = requests.packages.urllib3.exceptions.InsecureRequestWarning | |
requests.packages.urllib3.disable_warnings(insecure_warn) | |
s = requests.Session() | |
s.auth = (prism_user, prism_pass) | |
s.headers.update({'Content-Type': 'application/json; charset=utf-8'}) | |
# ['ON', 'OFF', 'POWERCYCLE', 'RESET', 'PAUSE', 'SUSPEND', 'RESUME', 'ACPI_SHUTDOWN', 'ACPI_REBOOT'] | |
body = {'transition': power_state} | |
vm_to_uuid_map = {} | |
data = s.get(base_url + 'vms', verify=False).json() | |
for vm_name_from_file in vm_list: | |
for entity in data['entities']: | |
vm_name = entity['name'] | |
if(vm_name == vm_name_from_file): | |
vm_uuid = entity['uuid'] | |
vm_to_uuid_map[vm_name] = vm_uuid | |
task_to_vm_map = {} | |
for vm_name in vm_to_uuid_map.keys(): | |
vm_uuid = vm_to_uuid_map[vm_name] | |
url = base_url + 'vms/' + vm_uuid + '/set_power_state/' | |
task = s.post(url, data=json.dumps(body), verify=False).json() | |
task_uuid = task['task_uuid'] | |
task_to_vm_map[task_uuid] = vm_name | |
print('Task UUID: ' + task_uuid + ' VM Name: ' + vm_name) | |
time.sleep(seconds_to_wait_task_report) | |
for task_uuid in task_to_vm_map.keys(): | |
vm_name = task_to_vm_map[task_uuid] | |
url = base_url + 'tasks/' + task_uuid | |
task = s.get(url, verify=False).json() | |
print('--') | |
print('VM: ' + vm_name) | |
print('TASK UUID: ' + task['uuid']) | |
print('operation_type: ' + task['operation_type']) | |
print('start_time_usecs: ' + str(task['start_time_usecs'])) | |
print('last_updated_time_usecs:' + str(task['last_updated_time_usecs'])) | |
print('percentage_complete: ' + str(task['percentage_complete'])) | |
print('progress_status: ' + task['progress_status']) | |
if(task['progress_status'] == "Failed"): | |
print('error_code: ' + str(task['meta_response']['error_code'])) | |
print('error_detail: ' + task['meta_response']['error_detail']) | |
if __name__ == '__main__': | |
main() |
実行例。
Prism ログイン設定。 prism-config.json
{
"prism_address": "Nutanix Cluster VIP",
"user_name": "ユーザ",
"password": "パスワード"
}
VM リスト。 vm_list.txt
vm01
vm02
対象 VM をシャットダウン。
$ python ./set_vm_power-state.py ./prism-config.json ./vm_list.txt ACPI_SHUTDOWN
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
下記の投稿むけ。
Nutanix REST API v2 で VM を停止 / 起動してみる。(Python)
http://blog.ntnx.jp/entry/2018/11/11/191703