I've google'd quite a bit, and read the argparse documentation that I think suggests using something with vars(). I get the Namespace violation as expected, I just cant figure out the path around this issue.
Essentially, I would like to take an argparse multi-value argument and create a list from those values so I can run a for-loop through them. This is to interface with our VNX array to reset the data snapshot on all the Developer environments.
When I run the command I can see the argparse is getting the values correctly, but its throwing the Namespace exception and not actually using the values for the argument.
Much appreciation for any guidance, even a link to some better docs that will explain my problem better. I know the issue, and how I want to fix it, I'm just not sure what to even read(or google) to get around this syntax-wise?
This is what I get when i run the code:
[[email protected] tmp]# ./envrestore.py -e dev1 dev2 dev3
Namespace(myenv=['dev1', 'dev2', 'dev3'])
Traceback (most recent call last): File "./envrestore.py", line 43, in run_create_snap() File "./envrestore.py", line 36, in run_create_snap for e in myenv: TypeError: 'Namespace' object is not iterable
[[email protected] tmp]#
#!/usr/bin/env python
import pexpect, sys, datetime, argparse, time
from fabric.api import *
parser = argparse.ArgumentParser()
parser.add_argument('-e', '--myenv', nargs='*', type=str)
print parser.parse_args()
array = "vnx.lipsum.com"
seckey = "/opt/Navisphere/blah"
myenv = parser.parse_args()
dbhosts = ['mongo01', 'mysql01']
# !! DO NOT CHANGE IDs !!
lunpnum = "0000000"
mongo_plunid = "3"
mysql_plunid = "4"
def delete_snap(env=myenv, host=dbhosts):
child = pexpect.spawn('naviseccli -secfilepath %s -h %s snap -destroy -id %s-%s-snap' % (seckey, array, host, env))
print child
child.logfile = sys.stdout
child.expect('Are you sure you want to perform this operation\?\(y\/n\):')
child.sendline('n')
def create_snap(env=myenv, host=dbhosts, lunid=''):
print "naviseccli -secfilepath %s -h %s snap -create -res %s -name %s-%s-snap -allowReadWrite yes" % (seckey, array, lunid, host, env)
def run_delete_snap():
for e in myenv:
for h in dbhosts:
delete_snap(env=e, host=h)
def run_create_snap():
for e in myenv:
for h in dbhosts:
if "mysql" in h:
create_snap(env=e, host=h, lunid=mysql_plunid)
elif "mongo" in h:
create_snap(env=e, host=h, lunid=mongo_plunid)
run_create_snap()