Shark/vShark update via REST API

Let me share a quick and dirty script I wrote and used recently to update a few sharks(I did ~50 of them at once) from version 10.9.1 to 10.9.2a.  The script uses the shark REST API to upload the iso image, then initialize the image, then trigger the install to run.

NOTE:  There is no error checking happening in this script, so use at own risk.

 

Put the ISO image(in my case was "rbt_csk_update-10.9.2a-2d41.iso"(don't forget to update line 37 in File 2 with the correct update image file name.) in the same directory as the 2x script files.

 

File1:  Sharkvars.py  :used to define all you sharks/vsharks (I use this file as a master list for all my sharks/vsharks.  I organize them by goups and update the "SHARKLIST" with the grouping of sharks that I want to update.)

 

 

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# <nbformat>3.0</nbformat>
# <codecell>

def SetSharksGlobalParameters():
        global SITE1DMZvSharks
        SITE1DMZvSharks = ['dmzvs1','dmzvs2','dmzvs3']
        global Site1ProdvSharks
        Site1ProdvSharks = ['prodvs1','prodvs2','prodvs3','prodvs4','prodvs5','prodvs6','prodvs7','prodvs8','prodvs9']
        SHARKLIST = SITE1DMZvSharks+Site1ProdvSharks



 

File2: update-sharks.py

 

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# <nbformat>3.0</nbformat>
# <codecell>
# Description: Script to update Riverbed Sharks via REST APO


import Sharkvars
Sharkvars.SetSharksGlobalParameters()
import pycurl, argparse, cStringIO, json, pprint, re, sys
from urllib import urlencode


parser = argparse.ArgumentParser(description='Parameter options')
parser.add_argument('-u','--username',help="username",required=True)
parser.add_argument('-p','--password',help="password",required=True)
rargs = parser.parse_args()


METHODE = 'https://'
URL1='/api/shark/5.1/system/update/iso'
URL2='/api/shark/5.1/system/update/state'
URL3='/api/shark/5.1/system/update'
DOMNAME = '.localdomain.com'




def POSTSystemUpdateISO(URL):
        c = pycurl.Curl()
        c.setopt(pycurl.URL, METHODE+SHARK+DOMNAME+URL)
        c.setopt(pycurl.SSL_VERIFYPEER, 0)
        c.setopt(pycurl.SSL_VERIFYHOST, 0)
        c.setopt(pycurl.SSL_VERIFYHOST, 0)
        c.setopt(pycurl.VERBOSE, 0)
        c.setopt(pycurl.USERPWD, rargs.username+':'+rargs.password)
        c.setopt(pycurl.HTTPPOST, [("iso-file", (pycurl.FORM_FILE, "rbt_csk_update-10.9.2a-2d41.iso"))])
        c.setopt(pycurl.HTTPHEADER, ['Content-Type: multipart/form-data'])
        c.setopt(pycurl.HTTPHEADER, ['Content-Disposition: form-data; name="iso-file"; filename="rbt_csk_update-10.9.2a-2d41.iso"'])
        c.perform()
        c.close()




def PUTSystemUpdateSTATE(URL):
        c = pycurl.Curl()
        c.setopt(pycurl.URL, METHODE+SHARK+DOMNAME+URL)
        c.setopt(pycurl.URL, METHODE+SHARK+DOMNAME+URL)
        c.setopt(pycurl.SSL_VERIFYPEER, 0)
        c.setopt(pycurl.SSL_VERIFYHOST, 0)
        c.setopt(pycurl.SSL_VERIFYHOST, 0)
        c.setopt(pycurl.VERBOSE, 0)
        c.setopt(pycurl.USERPWD, rargs.username+':'+rargs.password)
        c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json'])
        c.setopt(pycurl.CUSTOMREQUEST, "PUT")
        data = {'reset': True, 'state': 'INITIALIZED'}
        post_data = json.dumps(data)
        print post_data
        c.setopt(c.POSTFIELDS, post_data)
        c.perform()
        c.close()


def getUserDATA(URL):
        c = pycurl.Curl()
        c.setopt(pycurl.URL, METHODE+SHARK+DOMNAME+URL)
        c.setopt(pycurl.URL, METHODE+SHARK+DOMNAME+URL)
        c.setopt(pycurl.SSL_VERIFYPEER, 0)
        c.setopt(pycurl.SSL_VERIFYHOST, 0)
        c.setopt(pycurl.SSL_VERIFYHOST, 0)
        c.setopt(pycurl.VERBOSE, 0)
        c.setopt(pycurl.USERPWD, rargs.username+':'+rargs.password)
        c.setopt(pycurl.HTTPHEADER, ['Accept: application/json'])
        c.setopt(pycurl.WRITEFUNCTION, b1.write)
        c.perform()
        c.close()


def runUPdate(URL, init_ID):
       c = pycurl.Curl()
       c.setopt(pycurl.URL, METHODE+SHARK+DOMNAME+URL)
       c.setopt(pycurl.URL, METHODE+SHARK+DOMNAME+URL)
       c.setopt(pycurl.SSL_VERIFYPEER, 0)
       c.setopt(pycurl.SSL_VERIFYHOST, 0)
       c.setopt(pycurl.SSL_VERIFYHOST, 0)
       c.setopt(pycurl.VERBOSE, 0)
       c.setopt(pycurl.USERPWD, rargs.username+':'+rargs.password)
       c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json'])
       c.setopt(pycurl.CUSTOMREQUEST, "PUT")
       data = {'init_id': init_ID, 'state': 'RUNNING'}
       post_data = json.dumps(data)
       print post_data
       c.setopt(c.POSTFIELDS, post_data)
       c.perform()
       c.close()


for SHARK in Sharkvars.SHARKLIST:
        print "UPloading File to %s" % (SHARK)
        POSTSystemUpdateISO(URL1)
        print "Upload file completed initializing update"
        PUTSystemUpdateSTATE(URL2)
        print "update initialized"
        b1 = cStringIO.StringIO()
        contents = getUserDATA(URL3)
        init_ID = str(re.findall("init_id\": \"([^\"]+)\"",b1.getvalue()))[2 : -2]
        state = re.findall("state\": \"([^\"]+)\"",b1.getvalue())
        estimate = re.findall("estimate\": \"([^\"]+)\"",b1.getvalue())
        print "INIT: %s, STATE: %s, Time Rmaining: %s" % (init_ID, state, estimate)
        print init_ID
        print "Starting updates avaialbe"
        runUPdate(URL2, init_ID)