118 lines
3.2 KiB
Python
Executable File
118 lines
3.2 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
from pyVmomi import vim
|
|
import pyVim.connect
|
|
import atexit
|
|
import argparse
|
|
from pprint import pprint
|
|
import re
|
|
from datetime import datetime
|
|
from dateutil.tz import tzlocal
|
|
import siteconf
|
|
|
|
site_conf = siteconf.read()
|
|
|
|
|
|
def get_args():
|
|
parser = argparse.ArgumentParser(
|
|
description='Clone VM from template')
|
|
|
|
parser.add_argument('-s', '--site',
|
|
required=False,
|
|
action='store',
|
|
help='name of the site to connect to')
|
|
|
|
parser.add_argument('-H', '--host',
|
|
required=False,
|
|
action='store',
|
|
help='vcenter host name (override site)')
|
|
|
|
parser.add_argument('-u', '--user',
|
|
required=False,
|
|
action='store',
|
|
help='user name')
|
|
|
|
parser.add_argument('-p', '--passwd',
|
|
required=False,
|
|
action='store',
|
|
help='password')
|
|
|
|
parser.add_argument('-a', '--age',
|
|
required=False,
|
|
type=int,
|
|
default=5,
|
|
action='store',
|
|
help='[days] miniumum snapshot age to list')
|
|
|
|
args = parser.parse_args()
|
|
return args
|
|
|
|
|
|
def get_vms(content):
|
|
obj_view = content.viewManager.CreateContainerView(
|
|
content.rootFolder, [vim.VirtualMachine], True)
|
|
vms_list = obj_view.view
|
|
obj_view.Destroy()
|
|
return vms_list
|
|
|
|
|
|
def snapshots(snaplist, min_days, depth=0):
|
|
now = datetime.now(tzlocal())
|
|
snapshot_data = []
|
|
for s in snaplist:
|
|
dt = now - s.createTime
|
|
if dt.days >= min_days:
|
|
text = ". " * depth + "(%s) %s / %i days (%s)" % (s.state, s.name, dt.days, s.createTime)
|
|
snapshot_data.append(text)
|
|
snapshot_data = snapshot_data + snapshots(s.childSnapshotList, min_days, depth + 1)
|
|
return snapshot_data
|
|
|
|
|
|
def main():
|
|
args = get_args()
|
|
|
|
host, user, passwd = None, None, None
|
|
if args.site:
|
|
if not args.site in site_conf:
|
|
print(f"site not found: {args.site}")
|
|
exit(1)
|
|
host = site_conf[args.site]['vcenter']
|
|
user = site_conf[args.site]['vcenter_user']
|
|
passwd = site_conf[args.site]['vcenter_passwd']
|
|
if args.host:
|
|
host = args.host
|
|
if args.user:
|
|
host = args.user
|
|
if args.passwd:
|
|
host = args.passwd
|
|
|
|
if not host or not user or not passwd:
|
|
print("Missing site / host, user, passwd.")
|
|
exit(1)
|
|
#print(f"host {host} user {user} passwd {passwd}")
|
|
|
|
si = pyVim.connect.SmartConnect(
|
|
host = host,
|
|
user = user,
|
|
pwd = passwd,
|
|
disableSslCertValidation = True
|
|
)
|
|
atexit.register(pyVim.connect.Disconnect, si)
|
|
content = si.RetrieveContent()
|
|
|
|
vms = get_vms(content)
|
|
for vm in vms:
|
|
if vm.snapshot is None:
|
|
continue
|
|
|
|
snaplist = snapshots(vm.snapshot.rootSnapshotList, args.age)
|
|
#if re.search('_R$', vm.name):
|
|
# continue;
|
|
for s in snaplist:
|
|
print("%s: %s" % (vm.name, s))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
|
|
# vim: set tabstop=4 shiftwidth=4 expandtab smarttab:
|