Sample Python Script

001:	#!/usr/bin/env python3
002:	
003:	import requests
004:	import argparse
005:	import re
006:	import sys
007:	import urllib.request, urllib.parse, urllib.error
008:	from time import sleep
009:	
010:	global failures
011:	failures = 0
012:	global args
013:	global username
014:	global password
015:	global verbose
016:	
017:	def check_job_status( ws_rsp):
018:	    global args
019:	    global failures
020:	    global verbose
021:	
022:	    print("%s" % (ws_rsp,))
023:	    m = re.search( '"message" : "Job has been successfully submitted."', ws_rsp)
024:	    if m:
025:	        m = re.search( '"job" : "(\d+)"', ws_rsp)
026:	        if m:
027:	            jobno = m.group(1)
028:	        else:
029:	            print("job not found --------------------------------------------------")
030:	            return
031:	        ws_rsp2 = do_webservices_cmd( '/job/mover/info',
032:	                                     { 'job' : jobno } )
033:	        print(ws_rsp2)
034:	        ws_rsp = do_webservices_cmd( '/job/info',
035:	                                     { 'job' : jobno } )
036:	        count = 0
037:	        while re.search( 'RUNNING', ws_rsp):
038:	            sleep(1)
039:	            ws_rsp2 = do_webservices_cmd( '/job/mover/info',
040:	                                         { 'job' : jobno } )
041:	            print(ws_rsp2)
042:	            ws_rsp = do_webservices_cmd( '/job/info',
043:	                                         { 'job' : jobno } )
044:	            print(ws_rsp)
045:	            count += 1
046:	        ws_rsp2 = do_webservices_cmd( '/job/mover/info',
047:	                                     { 'job' : jobno } )
048:	        print(ws_rsp2)
049:	        print("Checked for completion: %d times" % (count,))
050:	        print(ws_rsp)
051:	        if re.search( 'Status: ERROR', ws_rsp):
052:	            print("Job had error -----------------------------------------------------")
053:	            failures += 1
054:	    else:
055:	        print("Job not submitted correctly -----------------------------------------------------")
056:	        failures += 1
057:	
058:	
059:	
060:	def process_args():
061:	    global args
062:	    global username
063:	    global password
064:	    global verbose
065:	
066:	    parser = argparse.ArgumentParser()
067:	    parser.add_argument("--verbose",
068:	                        action='store_true',
069:	                        dest='verbose',
070:	                        help=" Be verbose in output.")
071:	    parser.add_argument("--authenticate",
072:	                        action='store',
073:	                        dest='authenticate',
074:	                        metavar='[authentication]',
075:	                        default='',
076:	                        help=" Authenticate all WS calls, value is 'username/password'.")
077:	    parser.add_argument("--protocol",
078:	                        action='store',
079:	                        dest='protocol',
080:	                        metavar='[PROTOCOL]',
081:	                        choices=['http', 'https'],
082:	                        help=" The protocol to use.")
083:	    parser.add_argument("--format",
084:	                        action='store',
085:	                        dest='format',
086:	                        choices=['text', 'json', 'xml'],
087:	                        default='json',
088:	                        help=" The format for response.")
089:	    parser.add_argument("--ip",
090:	                        action='store',
091:	                        dest='ip',
092:	                        default='',
093:	                        help=" The ip of the MDC.")
094:	
095:	    # Parse the command-line
096:	    args = parser.parse_args()
097:	
098:	    print("authenticate: %s" % (args.authenticate,))
099:	    print("format:       %s" % (args.format,))
100:	    print("ip:           %s" % (args.ip,))
101:	    print("protocol:     %s" % (args.protocol,))
102:	    print("verbose:      %s" % (args.verbose,))
103:	    verbose = args.verbose
104:	
105:	    # check the ip, standard 4 numbers '.' separated
106:	    #
107:	    m = re.compile( '^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$')
108:	    if not m.match( args.ip):
109:	        print("ip not in correct format: %s" % (args.ip,))
110:	        sys.exit(1)
111:	
112:	    # check authentication
113:	    #
114:	    if args.authenticate == '':
115:	        username = ''
116:	        password = ''
117:	    elif '/' in args.authenticate:
118:	        username, password = args.authenticate.split( '/')
119:	        username = username + '_py'
120:	        password = password + '_py'
121:	    else:
122:	        print('Authenticate argument must have '/' between the username and password:')
123:	        print("value: %s" % (args.authenticate,))
124:	        sys.exit(1)
125:	
126:	
127:	def do_webservices_cmd( cmd, pieces={}):
128:	    global args
129:	    global failures
130:	    global username
131:	    global password
132:	    global verbose
133:	
134:	    # set up the URL prefix
135:	    #
136:	    if 'wsconfig' in cmd:
137:	        prefix = "http://%s:81/sws/v2" % (args.ip,)
138:	    elif 'https' in args.protocol:
139:	        prefix = "https://%s:81/sws/v2" % (args.ip,)
140:	    else:
141:	        prefix = "http://%s:81/sws/v2" % (args.ip,)
142:	
143:	    #? print "prefix: %s" % (prefix,) #?
144:	
145:	    ws = prefix + cmd
146:	
147:	    # if we do not already have a format parameter
148:	    #
149:	    if 'format' not in pieces:
150:	        #if 'job' not in cmd:
151:	        pieces['format'] = args.format
152:	
153:	    # add login parameters if we need to authenticate
154:	    #
155:	    if args.authenticate != '':
156:	        pieces['username'] = username
157:	        pieces['password'] = password
158:	
159:	    # all web-service URLs are lowercase
160:	    #
161:	    ws = ws.lower()
162:	
163:	    response = requests.get( ws, pieces, verify=False)
164:	    if verbose:
165:	        print()
166:	        print("WS: %s" % (urllib.parse.unquote(response.url),))
167:	
168:	    rsp = response.text
169:	    #? print "rsp; %s" % (rsp,) #?
170:	    if response.status_code == 200::
171:	        return rsp
172:	    else:
173:	        failures += 1
174:	        return "Fail: %s" % (rsp,)
175:	
176:	
177:	if __name__ == '__main__':
178:	
179:	    process_args()
180:	
181:	    #Description of Web Services sample programs:
182:	
183:	    # 0) Please turn on web-services using the StorNext GUI
184:	    #    Also choose protocol and authentication through the SN GUI
185:	
186:	    # 1) Do WS system info, returning TEXT, XML and JSON
187:	    # /sws/v2/system/info?format=text
188:	
189:	    ws_rsp = do_webservices_cmd( '/system/info',
190:	                                 { 'format' : 'text' } )
191:	    print(ws_rsp)
192:	
193:	    # /sws/v2/system/info?format=xml
194:	
195:	    ws_rsp = do_webservices_cmd( '/system/info',
196:	                                 { 'format' : 'xml' } )
197:	    print(ws_rsp)
198:	
199:	    # /sws/v2/system/info?format=json
200:	
201:	    ws_rsp = do_webservices_cmd( '/system/info',
202:	                                 { 'format' : 'json' } )
203:	    print(ws_rsp)
204:	
205:	    # 2) Create a policy for a managed file system
206:	
207:	    # 3) 3 directories should exist:
208:	    # a) Directory for single-file manipulation
209:	
210:	    singles_path = '/stornext/snfs1/sample_dir_singles/py'
211:	    singles_paths_sync = []
212:	    singles_paths_async = []
213:	
214:	    # b) Directory for directory manipulation
215:	
216:	    dirs_path_sync  = '/stornext/snfs1/sample_dir_dirs_sync/py'
217:	    dirs_paths_sync = []
218:	    dirs_path_async = '/stornext/snfs1/sample_dir_dirs_async/py'
219:	    dirs_paths_async = []
220:	
221:	    # c) Directory for multi-file manipulation
222:	
223:	    multi_path = '/stornext/snfs1/sample_dir_multi/py'
224:	    multi_paths_sync = []
225:	    multi_paths_async = []
226:	
227:	    # 4) Create arrays with path names of both files in each of those directories
228:	    # for single and multi commands, files 0 and 1 are for sync, files 2 and 3 are for async
229:	    # for directory commands, there is a directory for sync and one for async
230:	
231:	    for i in [0,1]:
232:	        filename = "file.%d" % (i,)
233:	        singles_paths_sync.append( "%s/%s" % (singles_path, filename))
234:	        dirs_paths_sync.append(    "%s/%s" % (dirs_path_sync, filename))
235:	        dirs_paths_async.append(   "%s/%s" % (dirs_path_async, filename))
236:	        multi_paths_sync.append(   "%s/%s" % (multi_path, filename))
237:	
238:	    for i in [2,3]:
239:	        filename = "file.%d" % (i,)
240:	        singles_paths_async.append( "%s/%s" % (singles_path, filename))
241:	        multi_paths_async.append(   "%s/%s" % (multi_path, filename))
242:	
243:	
244:	    # 5-11) steps 5-11 use sync mode, steps 12-18 use async mode
245:	
246:	    # 5) Use WS fsstore to save both files from the first directory to TAPE
247:	    # /sws/v2/file/fsstore?file=<filepath>
248:	
249:	    for filepath in singles_paths_sync:
250:	        ws_rsp = do_webservices_cmd( '/file/fsstore',
251:	                                     { 'file' : filepath } )
252:	        print(ws_rsp)
253:	
254:	    # /sws/v2/file/fsfileinfo?file=<filepath>
255:	
256:	    for filepath in singles_paths_sync:
257:	        ws_rsp = do_webservices_cmd( '/file/fsfileinfo',
258:	                                     { 'file' : filepath } )
259:	        print(ws_rsp)
260:	
261:	    # 6) Use WS fsstore to save the second directory to TAPE
262:	    # /sws/v2/file/fsstore?directory=<dirpath>
263:	
264:	    ws_rsp = do_webservices_cmd( '/file/fsstore',
265:	                                 { 'directory' : dirs_path_sync } )
266:	    print(ws_rsp)
267:	
268:	    # /sws/v2/file/fsfileinfo?directory=<dirpath>
269:	
270:	    ws_rsp = do_webservices_cmd( '/file/fsfileinfo',
271:	                                 { 'directory' : dirs_path_sync } )
272:	    print(ws_rsp)
273:	
274:	    # 7) Use WS fsstore to save both files from the third directory to TAPE
275:	    # /sws/v2/file/fsstore?file=<f1>&file=<f2>
276:	
277:	    ws_rsp = do_webservices_cmd( '/file/fsstore',
278:	                                 { 'file' : [ multi_paths_sync[0],
279:	                                              multi_paths_sync[1] ] } )
280:	    print(ws_rsp)
281:	
282:	
283:	    # /sws/v2/file/fsfileinfo?file=<f1>&file=<f2>
284:	
285:	    ws_rsp = do_webservices_cmd( '/file/fsfileinfo',
286:	                                 { 'file' : [ multi_paths_sync[0],
287:	                                              multi_paths_sync[1] ] } )
288:	    print(ws_rsp)
289:	
290:	
291:	    # 8) Use WS rmdiskcopy to truncate both files in each of 3 directories
292:	    # /sws/v2/file/fsrmdiskcopy
293:	
294:	    for filepath in singles_paths_sync:
295:	        ws_rsp = do_webservices_cmd( '/file/fsrmdiskcopy',
296:	                                     { 'file' : filepath } )
297:	        print(ws_rsp)
298:	
299:	    for filepath in dirs_paths_sync:
300:	        ws_rsp = do_webservices_cmd( '/file/fsrmdiskcopy',
301:	                                     { 'file' : filepath } )
302:	        print(ws_rsp)
303:	
304:	    for filepath in multi_paths_sync:
305:	        ws_rsp = do_webservices_cmd( '/file/fsrmdiskcopy',
306:	                                     { 'file' : filepath } )
307:	        print(ws_rsp)
308:	
309:	    # 9) Use WS fsretrieve to restore both files to first directory from TAPE
310:	    # /sws/v2/file/fsretrieve?file=<filepath>
311:	
312:	    for filepath in singles_paths_sync:
313:	        ws_rsp = do_webservices_cmd( '/file/fsretrieve',
314:	                                     { 'file' : filepath } )
315:	        print(ws_rsp)
316:	
317:	    # /sws/v2/file/fsfileinfo?file=<filepath>
318:	
319:	    for filepath in singles_paths_sync:
320:	        ws_rsp = do_webservices_cmd( '/file/fsfileinfo',
321:	                                     { 'file' : filepath } )
322:	        print(ws_rsp)
323:	
324:	    # 10) Use WS fsretrieve to restore the second directory from TAPE
325:	    # /sws/v2/file/fsretrieve?directory=<dirpath>
326:	
327:	    ws_rsp = do_webservices_cmd( '/file/fsretrieve',
328:	                                 { 'directory' : dirs_path_sync } )
329:	    print(ws_rsp)
330:	
331:	    # /sws/v2/file/fsfileinfo?directory=<dirpath>
332:	
333:	    ws_rsp = do_webservices_cmd( '/file/fsfileinfo',
334:	                                 { 'directory' : dirs_path_sync } )
335:	    print(ws_rsp)
336:	
337:	    # 11) Use WS fsretrieve to restore both files in the third directory from TAPE
338:	    # /sws/v2/file/fsretrieve?file=<f1>&file=<f2>
339:	
340:	    ws_rsp = do_webservices_cmd( '/file/fsretrieve',
341:	                                 { 'file' : [ multi_paths_sync[0],
342:	                                              multi_paths_sync[1] ] } )
343:	    print(ws_rsp)
344:	
345:	
346:	    # /sws/v2/file/fsfileinfo?file=<f1>&file=<f2>
347:	
348:	    ws_rsp = do_webservices_cmd( '/file/fsfileinfo',
349:	                                 { 'file' : [ multi_paths_sync[0],
350:	                                              multi_paths_sync[1] ] } )
351:	    print(ws_rsp)
352:	
353:	
354:	    # 12-18) Repeat steps 5-11 using async mode
355:	
356:	    # 12) Use WS fsstore to save both files from the first directory to TAPE
357:	    # /sws/v2/file/fsstore?file=<filepath>
358:	
359:	    for filepath in singles_paths_async:
360:	        ws_rsp = do_webservices_cmd( '/file/fsstore',
361:	                                     { 'file' : filepath,
362:	                                       'mode' : 'async' } )
363:	        check_job_status( ws_rsp)
364:	
365:	    # /sws/v2/file/fsfileinfo?file=<filepath>
366:	
367:	    for filepath in singles_paths_async:
368:	        ws_rsp = do_webservices_cmd( '/file/fsfileinfo',
369:	                                     { 'file' : filepath } )
370:	        print(ws_rsp)
371:	
372:	    # 13) Use WS fsstore to save the second directory to TAPE
373:	    # /sws/v2/file/fsstore?directory=<dirpath>
374:	
375:	    ws_rsp = do_webservices_cmd( '/file/fsstore',
376:	                                 { 'directory' : dirs_path_async,
377:	                                   'mode' : 'async' } )
378:	    check_job_status( ws_rsp)
379:	
380:	    # /sws/v2/file/fsfileinfo?directory=<dirpath>
381:	
382:	    ws_rsp = do_webservices_cmd( '/file/fsfileinfo',
383:	                                 { 'directory' : dirs_path_async } )
384:	    print(ws_rsp)
385:	
386:	    # 14) Use WS fsstore to save both files from the third directory to TAPE
387:	    # /sws/v2/file/fsstore?file=<f1>&file=<f2>
388:	
389:	    ws_rsp = do_webservices_cmd( '/file/fsstore',
390:	                                 { 'file' : [ multi_paths_async[0],
391:	                                              multi_paths_async[1] ],
392:	                                   'mode' : 'async' } )
393:	    check_job_status( ws_rsp)
394:	
395:	    # /sws/v2/file/fsfileinfo?file=<f1>&file=<f2>
396:	
397:	    ws_rsp = do_webservices_cmd( '/file/fsfileinfo',
398:	                                 { 'file' : [ multi_paths_async[0],
399:	                                              multi_paths_async[1] ] } )
400:	    print(ws_rsp)
401:	
402:	
403:	    # 15) Use WS rmdiskcopy to truncate both files in each of 3 directories
404:	    # /sws/v2/file/fsrmdiskcopy
405:	
406:	    for filepath in singles_paths_async:
407:	        ws_rsp = do_webservices_cmd( '/file/fsrmdiskcopy',
408:	                                     { 'file' : filepath } )
409:	        print(ws_rsp)
410:	
411:	    for filepath in dirs_paths_async:
412:	        ws_rsp = do_webservices_cmd( '/file/fsrmdiskcopy',
413:	                                     { 'file' : filepath } )
414:	        print(ws_rsp)
415:	
416:	    for filepath in multi_paths_async:
417:	        ws_rsp = do_webservices_cmd( '/file/fsrmdiskcopy',
418:	                                     { 'file' : filepath } )
419:	        print(ws_rsp)
420:	
421:	    # 16) Use WS fsretrieve to restore both files to first directory from TAPE
422:	    # /sws/v2/file/fsretrieve?file=<filepath>
423:	
424:	    for filepath in singles_paths_async:
425:	        ws_rsp = do_webservices_cmd( '/file/fsretrieve',
426:	                                     { 'file' : filepath,
427:	                                       'mode' : 'async' } )
428:	        check_job_status( ws_rsp)
429:	
430:	    # /sws/v2/file/fsfileinfo?file=<filepath>
431:	
432:	    for filepath in singles_paths_async:
433:	        ws_rsp = do_webservices_cmd( '/file/fsfileinfo',
434:	                                     { 'file' : filepath } )
435:	        print(ws_rsp)
436:	
437:	    # 17) Use WS fsretrieve to restore the second directory from TAPE
438:	    # /sws/v2/file/fsretrieve?directory=<dirpath>
439:	
440:	    ws_rsp = do_webservices_cmd( '/file/fsretrieve',
441:	                                 { 'directory' : dirs_path_async,
442:	                                   'mode' : 'async' } )
443:	    check_job_status( ws_rsp)
444:	
445:	    # /sws/v2/file/fsfileinfo?directory=<dirpath>
446:	
447:	    ws_rsp = do_webservices_cmd( '/file/fsfileinfo',
448:	                                 { 'directory' : dirs_path_async } )
449:	    print(ws_rsp)
450:	
451:	    # 18) Use WS fsretrieve to restore both files in the third directory from TAPE
452:	    # /sws/v2/file/fsretrieve?file=<f1>&file=<f2>
453:	
454:	    ws_rsp = do_webservices_cmd( '/file/fsretrieve',
455:	                                 { 'file' : [ multi_paths_async[0],
456:	                                              multi_paths_async[1] ],
457:	                                   'mode' : 'async' } )
458:	    check_job_status( ws_rsp)
459:	
460:	    # /sws/v2/file/fsfileinfo?file=<f1>&file=<f2>
461:	
462:	    ws_rsp = do_webservices_cmd( '/file/fsfileinfo',
463:	                                 { 'file' : [ multi_paths_async[0],
464:	                                              multi_paths_async[1] ] } )
465:	
466:	    # 19) Get policy information
467:	
468:	    ws_rsp = do_webservices_cmd( '/policy/fsdirclass',
469:	                                 { 'directory' : dirs_path_async} )
470:	    print(ws_rsp)
471:	
472:	    ws_rsp = do_webservices_cmd( '/policy/fsclassinfo',
473:	                                 { 'policy' : 'policy_min_i' } )
474:	    print(ws_rsp)
475:	
476:	    print()
477:	    print("Number of failures: %d" % (failures,))
478:	
479:	
480:	#               Copyright 2022 Quantum Corporation