-
Notifications
You must be signed in to change notification settings - Fork 9
/
pureTakeFBsnapshot.py
168 lines (139 loc) · 5.93 KB
/
pureTakeFBsnapshot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import urllib3
urllib3.disable_warnings()
from base64 import b64encode
import os
import sys
import json
import getpass
from optparse import OptionParser
from datetime import datetime, timedelta
import time
from time import gmtime, strftime, strptime
from operator import itemgetter, attrgetter
from purity_fb import PurityFb, FileSystem, FileSystemSnapshot, SnapshotSuffix, rest
# Global Variables
VERSION = '2.0.0'
HEADER = 'Pure Storage Take FlashBlade Snapshot (' + VERSION + ')'
BANNER = ('=' * 132)
DEBUG_FLAG = False
VERBOSE_FLAG = False
COOKIE = ''
def parsecl():
usage = 'usage: %prog [options]'
version = '%prog ' + VERSION
description = "This application has been developed using Pure Storage v1.12 RESTful Web Service interfaces. Developed and tested using Python 3.9.5 Please contact ron@purestorage.com for assistance."
parser = OptionParser(usage=usage, version=version, description=description)
parser.add_option('-d', '--debug',
action = 'store_true',
dest = 'DEBUG_FLAG',
default = False,
help = 'Debug [default: %default]')
parser.add_option('-f', '--filesystem',
action = 'store',
type = 'string',
dest = 'fs',
help = 'FlashBlade File System')
parser.add_option('-r', '--replicant',
action = 'store',
type = 'string',
dest = 'flashBladeRep',
help = 'FlashBlade Replicant array')
parser.add_option('-s', '--server',
action = 'store',
type = 'string',
dest = 'flashBlade',
help = 'FlashBlade array')
parser.add_option('-t', '--token',
action = 'store',
type = 'string',
dest = 'API_TOKEN',
help = 'Pure API Token')
parser.add_option('-S', '--suffix',
action = 'store',
type = 'string',
dest = 'suffix',
help = 'File system snapshot suffix')
parser.add_option('-v', '--verbose',
action = 'store_true',
dest = 'VERBOSE_FLAG',
default = False,
help = 'Verbose [default: %default]')
(options, args) = parser.parse_args()
'''
print("Options:", options)
print("Args:", args)
'''
return(options)
def main():
# Setup variables
global DEBUG_FLAG
exit_code = 0
# Check for command line parameters
options = parsecl()
API_TOKEN = options.API_TOKEN
flashBlade = options.flashBlade
flashBladeRep = options.flashBladeRep
fs = options.fs
suffix = options.suffix
DEBUG_FLAG = options.DEBUG_FLAG
VERBOSE_FLAG = options.VERBOSE_FLAG
if DEBUG_FLAG:
print('API Token:', API_TOKEN)
print('FlashBlade:', flashBlade)
print('Relplicant:', flashBladeRep)
print('File System:', fs)
print('Suffix:', suffix)
print('Debug Flag:', DEBUG_FLAG)
print('Verbose Flag:', VERBOSE_FLAG)
if flashBlade == None:
sys.exit('Exiting: You must provide FlashBlade details')
if API_TOKEN == None:
sys.exit('Exiting: You must provide FlashBlade API Token details')
if fs == None:
sys.exit('Exiting: You must provide FlashBlade file system')
print(BANNER)
print(HEADER + ' - ' + flashBlade)
print(strftime('%d/%m/%Y %H:%M:%S %Z', gmtime()))
print(BANNER)
# create PurityFb object for a certain array
fb = PurityFb(flashBlade)
# this is required for versions before Purity//FB 2.1.3 because they only supports self-signed
# certificates. in later versions, this may be unnecessary if you have imported a certificate.
fb.disable_verify_ssl()
try:
res= fb.login(API_TOKEN)
except rest.ApiException as e:
print("Exception when logging in to the array: %s\n" % e)
if res:
try:
if flashBladeRep:
if suffix:
# create a snapshot with suffix and replicate to target array
res = fb.file_system_snapshots.create_file_system_snapshots(sources=[fs],
suffix=SnapshotSuffix(suffix),
send=True,
targets=[flashBladeRep])
else:
# create a snapshot without suffix and replicate to target array
res = fb.file_system_snapshots.create_file_system_snapshots(sources=[fs],
send=True,
targets=[flashBladeRep])
else:
if suffix:
# create a snapshot with suffix for the file system
res = fb.file_system_snapshots.create_file_system_snapshots(sources=[fs],
suffix=SnapshotSuffix(suffix))
else:
# create a snapshot without suffix for the file system
res = fb.file_system_snapshots.create_file_system_snapshots(sources=[fs])
if VERBOSE_FLAG:
print(res)
print('Snapshot created for', fs, 'suffix', res.items[0].suffix)
except rest.ApiException as e:
print("Exception when creating file system snapshots: %s\n" % e)
fb.logout()
print(BANNER)
print(strftime('%d/%m/%Y %H:%M:%S %Z', gmtime()))
print(BANNER)
sys.exit(exit_code)
main()