Python から vSphere を操作できる pyvmomi のサンプルは pyvmomi-community-samples で公開されています。 しかし、「特定フォルダの仮想マシンをシャットダウンする」というサンプルが無いようだったので作ってみました。
ソースコード
ソースコードは以下の通りです。 既存のサンプルを改造して、やっつけで作りました…
#!/usr/bin/env python
"""
Python program for shutdown the VMs on an ESX / vCenter.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from __future__ import print_function
import atexit
from pyVim.connect import SmartConnectNoSSL, Disconnect
from pyVmomi import vim
from tools import cli
MAX_DEPTH = 10
def setup_args():
"""
Get standard connection arguments
"""
parser = cli.build_arg_parser()
parser.add_argument(
'-f', '--folder',
default='',
help='Folder name of the VirtualMachine you want to shutdown.')
my_args = parser.parse_args()
return cli.prompt_for_password(my_args)
def get_obj(content, vimtype, name):
"""
Get the vsphere object associated with a given text name
"""
obj = None
container = content.viewManager.CreateContainerView(
content.rootFolder, vimtype, True)
for c in container.view:
if c.name == name:
obj = c
break
return obj
def shutdown(vm, depth=1):
"""
Print information for a particular virtual machine or recurse into a folder
with depth protection
"""
# if this is a group it will have children. if it does, recurse into them
# and then return
if hasattr(vm, 'childEntity'):
if depth > MAX_DEPTH:
return
vmlist = vm.childEntity
for child in vmlist:
shutdown(child, depth+1)
return
summary = vm.summary
if vm.runtime.powerState == "poweredOn":
vm.ShutdownGuest()
print(
vm.parent.name + "\\" + vm.name + " is shutdown. (" +
summary.config.uuid + ")")
def main():
"""
Simple command-line program for listing the virtual machines on a host.
"""
args = setup_args()
si = None
try:
si = SmartConnectNoSSL(host=args.host,
user=args.user,
pwd=args.password,
port=int(args.port))
atexit.register(Disconnect, si)
except vim.fault.InvalidLogin:
raise SystemExit("Unable to connect to host "
"with supplied credentials.")
content = si.RetrieveContent()
if args.folder == "":
for child in content.rootFolder.childEntity:
if hasattr(child, 'vmFolder'):
datacenter = child
vmfolder = datacenter.vmFolder
vmlist = vmfolder.childEntity
for vm in vmlist:
shutdown(vm)
else:
vmfolder = get_obj(content, [vim.Folder], args.folder)
vmlist = vmfolder.childEntity
for vm in vmlist:
shutdown(vm)
# Start program
if __name__ == "__main__":
main()
使い方
オプションの意味は以下の通りです。
オプション | 必須か? | 説明 |
---|---|---|
-s | 必須 | vCenter または ESXi のアドレスを指定する。 |
-u | 必須 | ユーザ名を指定する。 |
-p | 必須 | パスワードを指定する。 |
-f | - | シャットダウンしたい仮想マシンの存在するフォルダを指定する。 |
pyvmomi-community-samples/samples
に移動して以下のように実行します。 但し、-f
オプションでフォルダを指定せずに実行すると全仮想マシンをシャットダウンするので 非常に危険です。
python shutdown_guest_vms.py -s ADDRESS -u USERNAME -p PASSWORD
特定フォルダの仮想マシンだけをシャットダウンするには以下のように -f
オプションでフォルダを指定します。
python shutdown_guest_vms.py -s ADDRESS -u USERNAME -p PASSWORD -f FOLDER
コメント