cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
995
Views
1
Helpful
2
Replies

"Service Upgrade" with Python available?

mleske
Cisco Employee
Cisco Employee

Hello folks,

I am wondering whether there is a chance to have python code supporting Service Upgrade phase, e.g. after the YANG model has changed and existing services need to be corrected. Example "developing-with-ncs/14-upgrade-service" only shows this with Java code.

According to Roque, I want to know if there is “setUseForCdbUpgrade” in Python API.

If possible getting an example comparable to 14-upgrade-service would be great.

Regards

Mike

1 Accepted Solution

Accepted Solutions

Jan Lindblad
Cisco Employee
Cisco Employee

Here's a small example to illustrate this.

I made an upgrade function that rounds down the 'val' leaf to the nearest multiple of 4. I did not write any code that checks whether the package was launched due to a software upgrade, or checks if the conversion has been done already. A real upgrade package would typically need something to control that. The exact details may vary.

Anyway, here I have a tiny service with a single leaf 'val'. In this example, there are three instances as shown in the CLI output. Then I reload the package which runs the upgrade code, and ensures all values are multiples of 4. Then I show the configured values again.

admin@ncs# show running-config upp

upp abba

val 14

!

upp dep

val 16

!

upp enya

val 29

!

admin@ncs# packages reload

reload-result {

    package upp

    result true

}

admin@ncs#

System message at 2017-09-01 16:26:07...

Commit performed by admin via tcp using python.

admin@ncs# show running-config upp

upp abba

val 12

!

upp dep

val 16

!

upp enya

val 28

!


To make this happen, I have two components in package-meta-data.xml . One for the service itself, and one for the upgrade function.

  <component>

    <name>main</name>

    <application>

      <python-class-name>upp.main.Main</python-class-name>

    </application>

  </component>

  <component>

    <name>upgrade</name>

    <application>

      <python-class-name>upgrade.main.Main</python-class-name>

    </application>

  </component>

</ncs-package>

My packages/upp/python/upgrade/main.py look like this:

# -*- mode: python; python-indent: 4 -*-

import ncs

from ncs.application import Service


# ---------------------------------------------

# COMPONENT THREAD THAT WILL BE STARTED BY NCS.

# ---------------------------------------------

class Main(ncs.application.Application):

    def setup(self):

       self.log.info('Upgrade Main RUNNING')

        with ncs.maapi.Maapi() as m:

            with ncs.maapi.Session(m, 'admin', 'python'):

                with m.start_write_trans() as t:

                    root = ncs.maagic.get_root(t)

                    for upp in root.upp:

                     self.log.info('Found upp instace with val=',upp.val)

                      upp.val = upp.val & ~3

                     self.log.info('Rouinding down to multiple of 4, new val=',upp.val)

                    t.apply(keep_open=False)


    def teardown(self):

       self.log.info('Upgrade Main FINISHED')

I'll figure out how to post the complete mini-example if anyone asks for it.

View solution in original post

2 Replies 2

mleske
Cisco Employee
Cisco Employee

Any idea or example already available?

Jan Lindblad
Cisco Employee
Cisco Employee

Here's a small example to illustrate this.

I made an upgrade function that rounds down the 'val' leaf to the nearest multiple of 4. I did not write any code that checks whether the package was launched due to a software upgrade, or checks if the conversion has been done already. A real upgrade package would typically need something to control that. The exact details may vary.

Anyway, here I have a tiny service with a single leaf 'val'. In this example, there are three instances as shown in the CLI output. Then I reload the package which runs the upgrade code, and ensures all values are multiples of 4. Then I show the configured values again.

admin@ncs# show running-config upp

upp abba

val 14

!

upp dep

val 16

!

upp enya

val 29

!

admin@ncs# packages reload

reload-result {

    package upp

    result true

}

admin@ncs#

System message at 2017-09-01 16:26:07...

Commit performed by admin via tcp using python.

admin@ncs# show running-config upp

upp abba

val 12

!

upp dep

val 16

!

upp enya

val 28

!


To make this happen, I have two components in package-meta-data.xml . One for the service itself, and one for the upgrade function.

  <component>

    <name>main</name>

    <application>

      <python-class-name>upp.main.Main</python-class-name>

    </application>

  </component>

  <component>

    <name>upgrade</name>

    <application>

      <python-class-name>upgrade.main.Main</python-class-name>

    </application>

  </component>

</ncs-package>

My packages/upp/python/upgrade/main.py look like this:

# -*- mode: python; python-indent: 4 -*-

import ncs

from ncs.application import Service


# ---------------------------------------------

# COMPONENT THREAD THAT WILL BE STARTED BY NCS.

# ---------------------------------------------

class Main(ncs.application.Application):

    def setup(self):

       self.log.info('Upgrade Main RUNNING')

        with ncs.maapi.Maapi() as m:

            with ncs.maapi.Session(m, 'admin', 'python'):

                with m.start_write_trans() as t:

                    root = ncs.maagic.get_root(t)

                    for upp in root.upp:

                     self.log.info('Found upp instace with val=',upp.val)

                      upp.val = upp.val & ~3

                     self.log.info('Rouinding down to multiple of 4, new val=',upp.val)

                    t.apply(keep_open=False)


    def teardown(self):

       self.log.info('Upgrade Main FINISHED')

I'll figure out how to post the complete mini-example if anyone asks for it.

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the NSO Developer community: