-
Notifications
You must be signed in to change notification settings - Fork 0
/
post-renamed.slax
392 lines (347 loc) · 13.9 KB
/
post-renamed.slax
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
Perform a limited replace pattern with type action on the following configuration sections:
groups/system/hostname
system/hostname
interfaces/interfcace/description
interfaces/interfcace/unit/description
interfaces/interfcace/unit/family/ethernet-switching/storm-control-profile
interfaces/interfcace/unit/family/ethernet-switching/vlan/members
snmp/location
forwarding-optinos/storm-control-profiles
vlans/vlan
vlans/vlan/description
system/login/user
access/profile
protocols/dot1x/authenticator/authentication-profile-name
protocols/oam/ethernet/link-fault-management/action-profile
protocols/oam/ethernet/link-fault-management/interace/apply-action-profile
This is a case sensitive operation.
Author: Ken Cope [email protected]
Last modified: 07.17.2023
Known bugs:
- None ATT
Todo:
*/
version 1.1;
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 str = "http://exslt.org/strings";
ns func extension = "http://exslt.org/functions";
ns replacepat extension = "http://xml.juniper.net/replacepat";
import "../import/junos.xsl";
param $pattern;
param $replace;
var $arguments = {
<argument> {
<name> "pattern";
<description> "Pattern to match";
}
<argument> {
<name> "replace";
<description> "Text to replace pattern with";
}
}
/* Junos Space annotations: https://www.juniper.net/documentation/us/en/software/junos-space21.2/junos-space-workspaces/topics/concept/scripts-annotations.html */
/* @NAME = "post-renamed" */
/* @DESCRIPTION = "Update fields that have a post name in them that must be updated as a result of a name change. The pattern and replace text are case sensitive */
/* ISLOCAL: This annotation is used to define whether the script is to be executed locally or staged on the device. */
/* @ISLOCAL = "false" */
/* connection to the local system that we are executing this script on*/
param $local = jcs:open();
var $usage = "pattern <pattern to match> replace <text to update>";
/* Controls for sections if they are updated or not - false() to skip, anything else to proceed*/
var $update_user_names = false();
/*
var $update_user_names = "YES";
/*
/* Main script */
match / {
<op-script-results> {
/* Check that the required parameters have been entered */
if (jcs:empty($pattern) or jcs:empty($replace)){
expr jcs:output($usage);
expr jcs:output("Note: arguments are case sensitive!");
terminate("pattern and replace arguments needed");
}
/* Get the config and look for the pattern in it */
var $get-config-rpc = <get-configuration database="commited">;
var $config = jcs:invoke($get-config-rpc);
var $matched = $config//*[text()[contains(., $pattern )]];
if (not($matched)) {
terminate concat("Pattern ", $pattern, " did not match anything in the interesting config sections");
}
expr jcs:output("Interesting config sections contains pattern " _ $pattern, " : replacing with ", $replace);
/* Update the host-name if needed */
var $new-hostname = replacepat:replace-pattern($config/system/host-name, $pattern, $replace);
var $new-hostname-re0 = replacepat:replace-pattern($config/groups[name="re0"]/system/host-name, $pattern, $replace);
var $new-hostname-re1 = replacepat:replace-pattern($config/groups[name="re0"]/system/host-name, $pattern, $replace);
/* Update the snmp location if needed */
var $new-snmp-location = replacepat:replace-pattern($config/snmp/location, $pattern, $replace);
/* Updated config
* Check each section to see if any updates are needed in each section to prevent empty stanzas from being
* propagated to the config and leading to warnings about empty config sections if nothing matches
*/
var $config-updated = {
<configuration> {
/* Check hostnames in re0 and re1 groups for dual RE systems like the 9200 series */
if ($new-hostname-re0) {
<groups> {
<name>re0;
<system>{
<host-name>$new-hostname-re0;
}
}
}
if ($new-hostname-re1) {
<groups> {
<name>re1;
<system>{
<host-name>$new-hostname-re1;
}
}
}
/* Update hostname */
if ($new-hostname) {
<system> {
<host-name> $new-hostname;
}
}
/* Update the user name */
if ($config/system/login/user/name[contains(.,$pattern)] and $update_user_names) {
/* Iterate through the users */
<system> {
<login> {
for-each ($config/system/login/user[name[contains(., $pattern)]]) {
<user rename="rename" name=replacepat:replace-pattern(./name, $pattern, $replace)> {
<name>./name;
}
}
}
}
}
/* 802.1x */
if ($config/access/profile/name[contains(.,$pattern)]) {
/* First the access profiles */
<access> {
/* Iterate through the access profiles */
for-each ($config/access/profile[name[contains(.,$pattern)]]) {
<profile rename="rename" name=replacepat:replace-pattern(./name, $pattern, $replace)>{
<name>./name;
}
}
}
/* Then if it is applied under protocols */
if ($config/protocols/dot1x/authenticator/authentication-profile-name[contains(.,$pattern)]){
<protocols> {
<dot1x> {
<authenticator>{
<authentication-profile-name>replacepat:replace-pattern($config/protocols/dot1x/authenticator/authentication-profile-name, $pattern, $replace);
}
}
}
}
}
/* Ethernet LFM */
if ($config/protocols/oam/ethernet/link-fault-management[contains(.,$pattern)]) {
<protocols> {
<oam> {
<ethernet> {
<link-fault-management> {
/* Check any action profiles */
if ($config/protocols/oam/ethernet/link-fault-management/action-profile[name[contains(., $pattern)]]){
/* iterate through the action profiles */
for-each ($config/protocols/oam/ethernet/link-fault-management/action-profile[name[contains(., $pattern)]]) {
<action-profile rename="rename" name=replacepat:replace-pattern(./name, $pattern, $replace)> {
<name>./name;
}
}
/* iterate through any interface that might have a matching action profile applied
* This has to be done as a delete, and add rather than a rename or you get a nasty commit error that the
* profile doesn't exist yet
*/
for-each ($config/protocols/oam/ethernet/link-fault-management/interface[apply-action-profile[contains(., $pattern)]]){
<interface> {
<name>./name;
<apply-action-profile delete="delete">./apply-action-profile;
<apply-action-profile>replacepat:replace-pattern(./apply-action-profile, $pattern, $replace);
}
}
}
}
}
}
}
}
/* Interfaces section - check if anything needs updating*/
if ($config/interfaces/interface[contains(., $pattern)]) {
<interfaces> {
/* Iterate through interfaces that have a change needed */
for-each ($config/interfaces/interface[contains(., $pattern)]) {
<interface> {
<name>./name;
/* update IFD description */
if (replacepat:replace-pattern(./description, $pattern, $replace)) {
<description>replacepat:replace-pattern(./description, $pattern, $replace);
}
/* Iterate through each IFL */
for-each (./unit[contains(., $pattern)]) {
<unit> {
<name>./name;
/* update IFL description */
if (replacepat:replace-pattern(./description, $pattern, $replace)) {
<description>replacepat:replace-pattern(./description, $pattern, $replace);
}
/* check ethernet switching family */
if (./family/ethernet-switching[contains(., $pattern)]) {
<family> {
<ethernet-switching> {
/* update applied storm control profiles */
if (replacepat:replace-pattern(./family/ethernet-switching/storm-control/profile-name, $pattern, $replace)){
<storm-control>{
<profile-name>replacepat:replace-pattern(./family/ethernet-switching/storm-control/profile-name, $pattern, $replace);
}
}
/* update any named vlans if needed */
if (./family/ethernet-switching/vlan[contains(., $pattern)]) {
<vlan> {
for-each (./family/ethernet-switching/vlan/members) {
if (replacepat:replace-pattern(., $pattern, $replace)) {
/* delete the old name and add the new name */
<members delete="delete">.;
<members>replacepat:replace-pattern(., $pattern, $replace);
}
}
}
}
}
}
}
}
}
}
}
}
}
/* Update storm control profiles */
if ($config/forwarding-options/storm-control-profiles[contains(., $pattern)]){
<forwarding-options> {
for-each ($config/forwarding-options/storm-control-profiles) {
if (replacepat:replace-pattern(./name, $pattern, $replace)) {
<storm-control-profiles rename="rename" name=replacepat:replace-pattern(./name, $pattern, $replace)> {
<name>./name;
}
}
}
}
}
/* check if snmp location needs updating */
if ($new-snmp-location) {
<snmp> {
<location> $new-snmp-location;
}
}
/* check if any vlan info needs changing */
if ($config/vlans[contains(., $pattern)]){
<vlans> {
/* iterate throuh all vlans */
for-each ($config/vlans/vlan) {
/* Rename a VLAN */
if (replacepat:replace-pattern(./name, $pattern, $replace)){
<vlan rename="rename" name=replacepat:replace-pattern(./name, $pattern, $replace)> {
<name>./name;
}
/* Update description for a renamed VLAN */
if (replacepat:replace-pattern(./description, $pattern, $replace)) {
<vlan> {
<name>replacepat:replace-pattern(./name, $pattern, $replace);
<description>replacepat:replace-pattern(./description, $pattern, $replace);
}
}
}
/* Update VLAN desription field only*/
else if (./description[(contains(., $pattern))]){
<vlan> {
<name> ./name;
if (replacepat:replace-pattern(./description, $pattern, $replace)) {
<description>replacepat:replace-pattern(./description, $pattern, $replace);
}
}
}
}
}
}
}
}
expr jcs:output("Updating config");
copy-of $config-updated;
/* Configuration commit parameters */
var $load-action = "merge";
var $log-message = concat("Post rename ", $pattern, " to ", $replace);
var $options := {
<commit-options>{
<log> $log-message;
}
}
/* Commit the config */
var $commit-results := {
call jcs:load-configuration(
$action=$load-action,
$commit-options = $options,
$configuration = $config-updated,
$connection = $local
);
}
/* Check the results */
if ($commit-results//xnm:error) {
for-each ($commit-results//xnm:error) {
<output> message;
}
}
if ($commit-results//xnm:warning) {
for-each ($commit-results//xnm:warning) {
<output> message;
copy-of .;
}
}
/* Display commit status */
if ($commit-results//commit-success) {
expr jcs:output("Sucessfully committed changes");
}
else {
terminate "Exiting with errors.";
}
/* Display results and changes */
var $get-config-compare-rpc = <get-configuration compare="rollback" rollback="1">;
var $compare-config-results = jcs:invoke($get-config-compare-rpc);
var $changed-lines = jcs:break-lines($compare-config-results/configuration-output);
<output> "Changed configuration items:";
for-each ($changed-lines) {
<output> .;
}
}
}
/* function to replace a pattern in a string
* sets a False boolean if the pattern was not contained in the text
* to be searched
*/
function replacepat:replace-pattern ( $line, $pattern, $replace) {
/* set a mutable variable to keep track of sucess */
mvar $result = 0;
/* Check that the line contains the pattern */
if (not (jcs:regex($pattern, $line))) {
set $result = -1;
}
else {
/* Repace the string occurrances */
set $result = str:replace($line, $pattern, $replace);
}
/* return results */
if ($result == -1 ){
/* false boolean */
<func:result select="false()">;
}
else {
result $result;
}
}