import salt.client | |
import salt.cloud | |
import sys | |
import time | |
def usage(): | |
print "Command Usage: python snapshot_datacenter.py <target> <form>" | |
print "Target: What vms you would like to target" | |
print "Form: What type your targeting, like Grains, Lists, Compounds, Etc." | |
def load_minions(client, target, expr_form): | |
minions = client.cmd(target, 'test.ping', expr_form=expr_form) | |
return minions | |
def load_virtual_machines(): | |
vms = [] | |
load = salt.cloud.CloudClient(path='/etc/salt/cloud.providers') | |
vms = load.action( | |
fun='list_nodes_min', | |
provider='vmware' | |
) | |
load = None | |
return vms['vmware-config']['vmware'] | |
def snapshot_VM(target): | |
snap = salt.cloud.CloudClient(path='/etc/salt/cloud.providers') | |
time_stamp = str(time.strftime("%c")) | |
des = str('Salt Stack Job ' + time_stamp) | |
result = snap.action( | |
fun='create_snapshot', | |
names=[target], | |
kwargs={ | |
'snapshot_name':'Pre-SALT work', | |
'description':des, | |
'memdump':'False' | |
} | |
) | |
snap = None | |
if 'Snapshot created successfully' in str(result): | |
return True | |
else: | |
return False | |
# set target, if found | |
if len(sys.argv) > 1 and sys.argv[1] != '': | |
# target Value | |
target = sys.argv[1] | |
if len(sys.argv) > 2 and sys.argv[2] != '': | |
# expr_form value | |
form = sys.argv[2] | |
client = salt.client.LocalClient() | |
minions = load_minions(client, target, form) | |
vms = load_virtual_machines() | |
new_list = [new_vms.lower() for new_vms in vms] | |
for minion in minions: | |
if minion in new_list: | |
vm_index = new_list.index(minion) | |
vm = str(vms.keys()[vm_index]) | |
result = snapshot_VM(vm) | |
if result: | |
print '[INF] ' + vm + ' snapshotted successfully' | |
else: | |
print '[ERR] ' + vm + ' snapshotted unsuccessfully' | |
else: | |
print "VM Not part of environment" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment