Junos Automation (Scripting)
Highlighted
Junos Automation (Scripting)

How to query with PyEZ whether a confirmed commit is pending, and how much time till auto-rollback?

‎07-25-2018 01:41 AM

In certain situations it may be useful to be able to detect:
- whether a confirmed commit is pending,
- how much time there is till automatic rollback (e.g. should I try still to "cancel" it (rollback 1 + commit), or should I rather wait for the auto-rollback)

When logged in to Junos (via ssh in my case) I got nice info messages basically each time I press Enter e.g.

# commit confirmed will be rolled back in 1 minute

# commit confirmed will be rolled back in less than 1 minute


And a broadcast message is visible when automatic rollback happens, e.g.

Broadcast Message from root@asdf                                               
        (no tty) at 16:54 UTC...                                               
                                                                               
Commit was not confirmed; automatic rollback complete. 


I would not like to store any state data when a confirmed commit was made to Junos, in my view it should be the responsibility of Junos to manage this state (and anyway e.g. I want to write a stateless microservice).

I could also establish a direct ssh connection, and parse these messages, but I wondered whether it can be done via PyEZ.

So far, I could only think of this algorithm:
- query the commit list (device.rpc.get_commit_information())
- get the latest commit (e.g. result.xpath("//commit-information/commit-history[sequence-number=0]")[0])
- check whether it is a confirmed commit (check whether it has <comment> and that comment text starts with 'commit confirmed, rollback in')
- if so, extract the rollback minutes from the comment text e.g. 'commit confirmed, rollback in 50mins' -> 50
- also extract the 'seconds' attribute from <date-time>
- get the current time in seconds since Epoch from uptime info: device.rpc.get_system_uptime_information(), and do e.g. int(result.xpath("//system-uptime-information/current-time/date-time/@seconds")[0])
- Now it is possible to calculate the approx seconds till rollback as:
confirmed_commit_date_time_sec + roolback_mins * 60 - current_junos_date_time_sec

(Note: this can go also a bit in the negative range, when the auto-rollback starts and it is still not finished, but it's OK.)

This seems a bit hacky way, e.g. relying on the exact text of the <comment> message, and parsing the rollback minutes from it. But I have no better idea.

Also, I cannot believe that is was not a relevant use case so far, and in case I want to have this information using PyEZ I have to do all this "ceremony"?!? Or is it *the way* to go?

(I also printed the gathered facts (device.facts), but looks like no such info there.)