Sunday, 20 September 2015

python and aem

Use Case:
Sometimes it is required to bind/unbind the configurations, restart/refresh the bundles afterwards .

Solution: We can use the below script wit some modifications according to your project setup.
def checkbundle():
          retries = 15
     response = 0
     while retries > 0 and response != '1':
          time.sleep(10)
          response = commands.getoutput('curl -silent -u admin:admin http://'+host+':'+port+'/system/console/bundles/org.apache.felix.webconsole.json | grep Active | wc
 -l')
          response = response.strip()
          retries = retries - 1
     if (response == '1'):
         bindConfig();
         startBundles();
     else:
         print "Felix console not ready :-("

def getBundleState():
        response = commands.getoutput('curl -silent -u admin:admin http://'+host+':'+port+'/system/console/config/Bundlelist.nfo | grep Kosa_CMS*')
    var = response.split('<br/>\n')
    var3 = ""
    for i in var:
        m1=re.search('&nbsp;\[(.+),&nbsp;', i)
        m2=re.search('(.+)&nbsp;\(', i)
        m3=re.search('&nbsp;\((.+)\)&nbsp;', i)
        if m1 or m2 or m3:
            var3 = var3 + m1.group(1)+";"+ m2.group(1)+";"+m3.group(1) +"\n"
    print var3.strip()
    return var3.strip()
def startBundles():
    bund2 = getBundleState();
    bund = bund2.split('\n')
       for i in bund:
        if i.split(';')[0] != 'active':
           print "Starting bundle "+i.split(';')[1]+""
           res = commands.getoutput('curl -silent -u admin:admin http://'+host+':'+port+'/system/console/bundles/'+i.split(';')[1]+' -Faction=start')
           print res
        else:
           print "The bundle "+i.split(';')[1]+" is already active"

def getConfigState():
        response = commands.getoutput('curl -silent -u admin:admin http://'+host+':'+port+'/system/console/config/Configurations.nfo |grep -e PID -e BundleLocation | grep kosa')
    var = response.split('PID')
    var3 = ""
    for i in var:
        m1=re.search('&nbsp;=&nbsp;(.+)<br/>', i)
        m2=re.search('BundleLocation&nbsp;=&nbsp;(.+)<br/>', i)
        if m2:
           loc = m2.group(1)
        else:
           loc = "UnBound"
        if m1:
           var3 = var3 +m1.group(1)+";"+loc + "\n"
    return var3.strip()
def bindConfig():
    bund2 = getConfigState();
 bund = bund2.split('\n')
       for i in bund:
        print "unbinding the config before restarting Kosa-common bundle"
        resp = commands.getoutput('curl -silent -u admin:admin -X POST "http://'+host+':'+port+'/system/console/configMgr/'+i.split(';')[0]+'?unbind=1"')
        print resp
        if i.split(';')[1] == 'UnBound':
           print "Binding the conf "+i.split(';')[0]+""
           res = commands.getoutput('curl -silent -u admin:admin -F action=refresh http://'+host+':'+port+'/system/console/bundles/com.adobe.nimish.cms.nimish-cms-common')
           print res
        else:
           print "The config "+i.split(';')[0]+" is already binded to "+i.split(';')[1]+""
    res = commands.getoutput('curl -silent -u admin:admin -F action=refresh http://'+host+':'+port+'/system/console/bundles/com.adobe.nimish.cms.nimish-cms-common')
    print "starting common bundle"
    print res

te = open('/var/CMSDump/AmsTools/log.txt','w')  # File where you need to keep the logs

class Unbuffered:

   def __init__(self, stream):

       self.stream = stream

   def write(self, data):

       self.stream.write(data)
       self.stream.flush()
       te.write(data)    # Write the data of stdout here to a text file as well


sys.stdout=Unbuffered(sys.stdout)
if __name__=='__main__':
     checkbundle();

No comments:

Post a Comment