07-17-2017 01:55 PM - edited 03-01-2019 03:55 AM
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
Solved! Go to Solution.
09-01-2017 07:47 AM
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>
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.
08-15-2017 11:13 PM
Any idea or example already available?
09-01-2017 07:47 AM
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>
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.
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: