Last active
May 9, 2017 04:11
-
-
Save rbenigno/e7ba7bfbe0a00b31d3ad008df60ca35b to your computer and use it in GitHub Desktop.
Performs various administrative operations for Pure Storage Flash Array
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
#!/usr/bin/env python2.7 | |
''' | |
File name: pure_tools.py | |
Author: Ryan Benigno | |
Date created: 05/08/2017 | |
Purpose: Command line script to perform various administrative operations on | |
Pure Storage Flash Array. | |
''' | |
# Import purestorage | |
try: | |
from purestorage import * | |
except ImportError, e: | |
print "Import error: {}".format(str(e)) | |
print 'https://pypi.python.org/pypi/purestorage' | |
exit(1) | |
import argparse, json, pprint | |
# Disable warnings... | |
import requests | |
requests.packages.urllib3.disable_warnings() | |
# Define and parse command line arguements | |
def parse_args(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("--array", help="Pure FlashArray Host/IP") | |
parser.add_argument("--api_token", help="API Token") | |
subparsers = parser.add_subparsers(title='subcommands', | |
description='valid subcommands', | |
help='additional help', | |
dest='command') | |
parser_dump_luns = subparsers.add_parser('dump_luns', help='Dump list of LUNs') | |
#parser_dump_luns.add_argument('--lun_filter', type=str, help='Filter LUNs based on name') | |
parser_get_lun = subparsers.add_parser('get_lun', help='Get details for a LUN') | |
parser_get_lun.add_argument('--name', '-n', type=str, help='Name of the LUN', required=True) | |
parser_create_lun = subparsers.add_parser('create_lun', help='Create a LUN') | |
parser_create_lun.add_argument('--name', '-n', type=str, help='LUN Name', required=True) | |
parser_create_lun.add_argument('--size', '-s', type=str, help='Size', required=True) | |
parser_create_luns_from_csv = subparsers.add_parser('create_luns_from_csv', help='Create LUNs from a CSV import') | |
parser_create_luns_from_csv.add_argument('--csv', '-c', type=str, help='CSV file with the columns "lun_name,lun_size"', required=True) | |
return parser.parse_args() | |
# Grab an environmental variable | |
def get_env_var(var): | |
import os | |
if os.environ.get(var): | |
return os.environ.get(var) | |
# Convert from bytes to gigabytes | |
def convert_gb(size): | |
return '{0:.2f}'.format(size/float(float(1024) ** 3)) | |
# Print a list of LUNs from the array | |
def dump_luns(array): | |
vols = array.list_volumes() | |
print '{: >50}\t{: >11}\t{: >24}'.format("Name", "Size", "Serial") | |
print '{: >50}\t{: >11}\t{: >24}'.format('-' * 50, '-' * 11, '-' * 24) | |
for vol in vols: | |
volsize = '{} GB'.format(convert_gb(vol["size"])) | |
print '{: >50}\t{: >11}\t{: >24}'.format(vol["name"], volsize, vol["serial"].lower()) | |
# Get details on a LUN | |
def get_lun(array, lun_name): | |
try: | |
lun = array.get_volume(lun_name) | |
return lun | |
except: | |
pass | |
# Create a new LUN | |
def create_lun(array, lun_name, lun_size): | |
if get_lun(array, lun_name): | |
raise Exception, 'LUN "{}" already exists!'.format(lun_name) | |
return | |
# Create the LUN | |
try: | |
result = array.create_volume(lun_name, lun_size) | |
return result | |
except Exception as e: | |
print 'LUN Creation failed for "{}": \n{}'.format(lun_name, e) | |
# Parse the contents of CSV input file | |
def parse_csv_file(csv_path): | |
import csv | |
lun_list = [] | |
try: | |
input_file = csv.DictReader(open(csv_path)) | |
for row in input_file: | |
lun_name = row["lun_name"].strip() | |
lun_size = row["lun_size"].strip() | |
if lun_name and lun_size: | |
lun_list.append({'name': lun_name, | |
'size': lun_size}) | |
else: | |
raise Exception, row | |
return lun_list | |
except Exception as e: | |
print 'Bad CSV data. Expecting columns named "lun_name" and "lun_size."\n{}'.format(e) | |
exit() | |
# Batch create LUNs | |
def create_luns(array, lun_list): | |
for lun in lun_list: | |
lun_name = lun["name"] | |
lun_size = lun["size"] | |
try: | |
new_lun = create_lun(array, lun_name, lun_size) | |
print '{}'.format(new_lun) | |
except Exception as e: | |
print 'Exception in create_luns: {}'.format(e) | |
### --------------------------------------------------------------------------- | |
### MAIN | |
### --------------------------------------------------------------------------- | |
# Main function (when running as an executable) | |
if __name__ == '__main__': | |
# Retrive the command line arguments | |
args = parse_args() | |
#print('All %s' % args) | |
# Get array name | |
if args.array: | |
pure_host = args.array | |
else: | |
pure_host = get_env_var('PURE_ARRAY') | |
if not pure_host: | |
print "Pure Array not defined" | |
exit() | |
# Get API Token | |
if args.api_token: | |
api_token = args.api_token | |
else: | |
api_token = get_env_var('PURE_API_TOKEN') | |
if not api_token: | |
print "Pure API Token not defined" | |
exit() | |
# Connect to array | |
pure_array = purestorage.FlashArray(pure_host, api_token=api_token) | |
# Dump a list of LUNs | |
if args.command == 'dump_luns': | |
dump_luns(pure_array) | |
# Get a LUN | |
elif args.command == 'get_lun': | |
vol = get_lun(pure_array, args.name) | |
if vol: | |
print '{: >50}\t{: >11}\t{: >24}'.format(vol["name"], convert_gb(vol["size"]), vol["serial"].lower()) | |
else: | |
print 'LUN not found: "{}"'.format(args.name) | |
# Create a LUN | |
elif args.command == 'create_lun': | |
new_lun = create_lun(pure_array, args.name, args.size) | |
if new_lun: | |
print '{: >50}\t{: >11}\t{: >24}'.format(new_lun["name"], convert_gb(new_lun["size"]), new_lun["serial"].lower()) | |
# Batch create LUNs from a CSV file | |
elif args.command == 'create_luns_from_csv': | |
csvfile = args.csv | |
lun_list = parse_csv_file(csvfile) | |
print 'Creating LUNs from list provided in "{}"'.format(csvfile) | |
create_luns(pure_array, lun_list) | |
# Disconnect | |
pure_array.invalidate_cookie() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment