Created
December 9, 2015 17:35
-
-
Save jeffbrl/eb961b9111c98750545b to your computer and use it in GitHub Desktop.
An example in slax of merging set-style configuration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| version 1.0; | |
| ns junos = "http://xml.juniper.net/junos/*/junos"; | |
| ns xnm = "http://xml.juniper.net/xnm/1.1/xnm"; | |
| ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0"; | |
| ns exsl extension = "http://exslt.org/common"; | |
| ns func extension = "http://exslt.org/functions"; | |
| ns automation = "http://xml.juniper.net/automation"; | |
| import "../import/junos.xsl"; | |
| var $syslog-level = "external.notice"; | |
| var $filename = "/var/tmp/config.txt"; | |
| match / { | |
| var $connection = jcs:open(); | |
| /* need to insert code to verify file exists here */ | |
| var $file-contents = automation:get-file-contents($filename, $connection); | |
| expr jcs:output($file-contents); | |
| var $commit-success = { call do-commit($connection, $file-contents=$file-contents, $format="text", $action="set"); } | |
| if($commit-success == "false") { | |
| expr jcs:output("failed"); | |
| } | |
| else { | |
| expr jcs:output("success"); | |
| } | |
| } | |
| /* Returns unescaped contents of $filename */ | |
| <func:function name="automation:get-file-contents"> { | |
| param $filename; | |
| param $connection; | |
| var $fileget = { | |
| <file-get> { | |
| <filename>$filename; | |
| <encoding>"ascii"; | |
| } | |
| } | |
| var $file-get-results = jcs:execute($connection, $fileget); | |
| var $file-contents = | |
| <xsl:value-of disable-output-escaping="yes" select="$file-get-results//file-contents">; | |
| <func:result select="$file-contents">; | |
| } | |
| /* | |
| * Attempts an exclusive commit. If it succeeds then return "true", otherwise return "false" | |
| * Code based on Curtis Call's template in time-based-filters.slax | |
| */ | |
| template do-commit( $connection, $file-contents, $format, $action) { | |
| /* Lock the configuration database */ | |
| var $lock-config = <lock-configuration>; | |
| var $lock-results = jcs:execute( $connection, $lock-config ); | |
| /* Was there an error locking? Then we cannot go on, return false */ | |
| if( not( jcs:empty( $lock-results/..//xnm:error ) ) ) { | |
| var $message = $script _ " unable to lock configuration database."; | |
| expr jcs:syslog( $syslog-level, $message ); | |
| expr "false"; | |
| } | |
| else { | |
| /* Load the configuration changes */ | |
| /* only set syntax supported currently */ | |
| var $load-configuration = { | |
| if(not(jcs:empty($file-contents))) { | |
| <load-configuration action = $action format = $format> { | |
| <configuration-set> { | |
| expr $file-contents; | |
| } | |
| } | |
| } | |
| else { | |
| /* submit null config */ | |
| <load-configuration> { | |
| <configuration>; | |
| } | |
| } | |
| } | |
| var $load-results = jcs:execute( $connection, $load-configuration ); | |
| /* Was there an error loading? */ | |
| if( not( jcs:empty( $load-results/..//xnm:error ) ) ) { | |
| var $message = $script _ " unable to load exclusive configuration changes."; | |
| expr jcs:syslog( $syslog-level, $message ); | |
| expr "false"; | |
| } | |
| else { | |
| /* Commit the configuration */ | |
| var $commit-configuration = <commit-configuration> { | |
| <log> "configuration committed by " _ $script; | |
| }; | |
| var $commit-results = jcs:execute( $connection, $commit-configuration ); | |
| /* Was there an error committing? */ | |
| if( not( jcs:empty( $commit-results/..//xnm:error ) ) ) { | |
| var $message = $script _ " unable to commit exclusive changes."; | |
| expr jcs:syslog( $syslog-level, $message ); | |
| expr "false"; | |
| } | |
| else { | |
| /* Unlock the database */ | |
| var $unlock-results = jcs:execute( $connection, "unlock-configuration" ); | |
| /* Was there an error unlocking? The commit already was successful, so just syslog about it */ | |
| if( not( jcs:empty( $unlock-results/..//xnm:error ) ) ) { | |
| var $message = $script _ " unable to unlock configuration database."; | |
| expr jcs:syslog( $syslog-level, $message ); | |
| } | |
| /* Close the connection */ | |
| var $close-results = jcs:close( $connection ); | |
| /* Was there an error closing the connection? The commit already was successful, so just syslog about it */ | |
| if( not( jcs:empty( $close-results/..//xnm:error ) ) ) { | |
| var $message = $script _ " unable to close connection."; | |
| expr jcs:syslog( $syslog-level, $message ); | |
| } | |
| /* Report success */ | |
| expr "true"; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment