-
Notifications
You must be signed in to change notification settings - Fork 4
/
upgrade-ex.slax
292 lines (228 loc) · 9.22 KB
/
upgrade-ex.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
#!/usr/libexec/ui/cscript
version 1.0;
/* Junos standard namespaces */
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";
/* EXSLT extensions and app specific namespaces */
ns exsl extension = "http://exslt.org/common";
ns func extension = "http://exslt.org/functions";
ns ztp = "http://xml.juniper.net/junos/ztp";
import "/usr/libdata/cscript/import/junos.xsl";
/* ------------------------------------------------------------------ */
/* Constants */
/* ------------------------------------------------------------------ */
var $SERVER = '172.28.202.27';
var $OS_PACKAGE = 'images/os-package.tgz';
var $HOOKS_PACKAGE = 'images/package-hooks-ex.tgz';
var $JUNOS_IMAGE = 'junos-arm-32-19.4R1.10.tgz';
var $JUNOS_CONF = 'config/default-ex2300.conf';
var $CRAPPY_VERSION = '18.1R3.3';
var $WANTED_VERSION = '19.4R1.10';
var $APPNAME = 'tingztp[' _ $junos-context/pid _ ']';
var $SYSLOG = 'user.info';
var $TMPDIR = '/var/tmp';
/* ------------------------------------------------------------------ */
/* MAIN */
/* ------------------------------------------------------------------ */
match / {
<op-script-results> {
/* Open a connection to the device */
var $jnx = jcs:open();
/* Get version number */
var $version = ztp:version-number( $jnx );
if( $version == $CRAPPY_VERSION ) {
expr ztp:rescue-upgrade-procedure( $jnx );
} else if ( $version != $WANTED_VERSION ) {
expr ztp:normal-upgrade-procedure( $jnx );
} else {
expr ztp:load-configuration( $jnx );
expr ztp:delete-packages( $jnx );
expr ztp:poweroff( $jnx );
expr ztp:end-script( $jnx );
}
expr ztp:reboot( $jnx );
expr ztp:save-rescue( $jnx );
expr ztp:end-script( $jnx );
}
}
/* ------------------------------------------------- */
/* this function is used get the version number */
/* ------------------------------------------------- */
<func:function name="ztp:version-number">
{
param $jnx;
var $cmd = <get-software-information>;
var $sn = jcs:execute( $jnx, $cmd )//junos-version;
expr jcs:output("The version we are running is " _ $sn);
<func:result select="$sn">;
}
/* ------------------------------------------------------ */
/* this function is used as the rescue-upgrade-procedure */
/* ------------------------------------------------------ */
<func:function name="ztp:rescue-upgrade-procedure">
{
param $jnx;
var $local_image = $TMPDIR _ '/' _ $JUNOS_IMAGE;
/* request system storage cleanup */
expr jcs:output('Cleaning up storage');
var $do_cleanup = <request-system-storage-cleanup> {
<no-confirm>;
}
var $cleanup-rsp = jcs:execute( $jnx, $do_cleanup );
/* request system software add http://172.28.202.27/images/os-package.tgz */
expr jcs:output('Adding OS package');
var $do_os_package = <request-package-add> {
<package-name> 'http://' _ $SERVER _ '/' _ $OS_PACKAGE;
}
var $osp-rsp = jcs:execute( $jnx, $do_os_package );
/* request system software add http://172.28.202.27/images/package-hooks-ex.tgz */
expr jcs:output('Adding EX hooks package');
var $do_hooks_package = <request-package-add> {
<package-name> 'http://' _ $SERVER _ '/' _ $HOOKS_PACKAGE;
}
var $hooks-rsp = jcs:execute( $jnx, $do_hooks_package );
if( ztp:file-exists( $jnx, $local_image )) {
expr jcs:output("junos image exists, no download needed");
} else {
/* file copy .... */
expr jcs:output("downloading junos image");
var $do_copy = <file-copy> {
<source> 'http://' _ $SERVER _ '/images/' _ $JUNOS_IMAGE;
<destination> $TMPDIR;
<staging-directory> $TMPDIR;
};
var $did_copy = jcs:execute( $jnx, $do_copy );
expr jcs:output($did_copy);
/* trap error here */
if( not(ztp:file-exists( $jnx, $local_image )) ) {
expr jcs:output("ERROR: unable to download junos image" );
expr ztp:end-script();
}
}
/* request system software add http://172.28.202.27/images/junos-arm-32-19.4R1.10.tgz force unlink no-copy reboot */
expr jcs:output('Installing software upgrade');
var $do_upgrade = <request-package-add> {
<package-name> $local_image;
<force>;
<no-copy>;
<unlink>;
<no-validate>;
}
var $upgrade-rsp = jcs:execute( $jnx, $do_upgrade );
expr jcs:output($upgrade-rsp);
expr jcs:output("Completed installing junos image");
<func:result select="true()">;
}
/* ------------------------------------------------------ */
/* this function is used as the normal-upgrade-procedure */
/* ------------------------------------------------------ */
<func:function name="ztp:normal-upgrade-procedure">
{
param $jnx;
/* request system software add http://172.28.202.27/images/junos-arm-32-19.4R1.10.tgz force unlink no-copy */
expr jcs:output('Installing software upgrade');
var $do_upgrade = <request-package-add> {
<package-name> 'http://' _ $SERVER _ '/images/' _ $JUNOS_IMAGE;
<force>;
<no-copy>;
<unlink>;
<no-validate>;
}
var $upgrade-rsp = jcs:execute( $jnx, $do_upgrade );
<func:result select="$upgrade-rsp">;
}
/* ------------------------------------------------- */
/* this function is to find if a package is installed */
/* ------------------------------------------------- */
<func:function name="ztp:package-installed">
{
param $jnx;
param $package;
var $cmd = <get-software-information>;
var $search = jcs:execute( $jnx, $cmd )//name[contains(text(), $package)];
expr jcs:output("Looking for " _ $package _ " and found " _ $search);
<func:result select="$search">;
}
/* ------------------------------------------------------ */
/* this function is used to delete packages */
/* ------------------------------------------------------ */
<func:function name="ztp:delete-packages">
{
param $jnx;
expr jcs:output('Deleting packages');
if( ztp:package-installed( $jnx, 'os-package' )) {
var $do_delete_os = <request-package-delete> { <package-name> 'os-package'; }
var $did_delete_os = jcs:execute( $jnx, $do_delete_os );
}
if( ztp:package-installed( $jnx, 'package-hooks-platform' )) {
var $do_delete = <request-package-delete> { <package-name> 'package-hooks-platform'; }
var $did_delete = jcs:execute( $jnx, $do_delete );
}
<func:result select="true()">;
}
/* ------------------------------------------------------ */
/* this function is used to check if a file exists */
/* ------------------------------------------------------ */
<func:function name="ztp:file-exists">
{
param $jnx;
param $filename;
var $ls_file = <file-list> { <path> $filename; };
var $ls_got = jcs:execute( $jnx, $ls_file );
var $retval = boolean( $ls_got//file-information );
<func:result select="$retval">;
}
/* ------------------------------------------------------ */
/* this function is used to load the configuration */
/* ------------------------------------------------------ */
<func:function name="ztp:load-configuration">
{
param $jnx;
var $do_load = <load-configuration action="override" url="http://" _ $SERVER _ "/" _ $JUNOS_CONF format="text">;
var $did_load = jcs:execute( $jnx, $do_load );
var $complete_commit = jcs:execute( $jnx, 'commit-configuration' );
<func:result select="$complete_commit">;
}
/* --------------------------------------------------------------------- */
/* this function is used save the configuration to the rescue partition */
/* --------------------------------------------------------------------- */
<func:function name="ztp:save-rescue">
{
param $jnx;
var $do_rescue = <request-save-rescue-configuration>;
var $did_rescue = jcs:execute( $jnx, $do_rescue );
<func:result select="true()">;
}
/* ------------------------------------------------------ */
/* this function is used to reboot the device */
/* ------------------------------------------------------ */
<func:function name="ztp:reboot">
{
param $jnx;
expr jcs:output("Rebooting in 60 seconds");
var $do_reboot = <request-reboot> { <in> 1; }
var $reboot-rsp = jcs:execute( $jnx, $do_reboot );
<func:result select="$reboot-rsp">;
}
/* ------------------------------------------------------ */
/* this function is used to poweroff the device */
/* ------------------------------------------------------ */
<func:function name="ztp:poweroff">
{
param $jnx;
expr jcs:output("Shutting down in 60 seconds");
var $do_poweroff = <request-power-off> { <in> 1; }
var $did_poweroff = jcs:execute( $jnx, $do_poweroff );
<func:result select="$did_poweroff">;
}
/* ------------------------------------------------- */
/* this function is used to terminate script properly */
/* ------------------------------------------------- */
<func:function name="ztp:end-script">
{
param $jnx;
expr jcs:close( $jnx );
<xsl:message terminate="yes">;
<func:result select="true()">;
}