Statistics
| Branch: | Tag: | Revision:

root / doc / hooks.rst @ cd46f3b4

History | View | Annotate | Download (13.6 kB)

1
Ganeti customisation using hooks
2
================================
3

    
4
Documents ganeti version 2.0
5

    
6
.. contents::
7

    
8
Introduction
9
------------
10

    
11

    
12
In order to allow customisation of operations, ganeti runs scripts
13
under ``/etc/ganeti/hooks`` based on certain rules.
14

    
15

    
16
This is similar to the ``/etc/network/`` structure present in Debian
17
for network interface handling.
18

    
19
Organisation
20
------------
21

    
22
For every operation, two sets of scripts are run:
23

    
24
- pre phase (for authorization/checking)
25
- post phase (for logging)
26

    
27
Also, for each operation, the scripts are run on one or more nodes,
28
depending on the operation type.
29

    
30
Note that, even though we call them scripts, we are actually talking
31
about any executable.
32

    
33
*pre* scripts
34
~~~~~~~~~~~~~
35

    
36
The *pre* scripts have a definite target: to check that the operation
37
is allowed given the site-specific constraints. You could have, for
38
example, a rule that says every new instance is required to exists in
39
a database; to implement this, you could write a script that checks
40
the new instance parameters against your database.
41

    
42
The objective of these scripts should be their return code (zero or
43
non-zero for success and failure). However, if they modify the
44
environment in any way, they should be idempotent, as failed
45
executions could be restarted and thus the script(s) run again with
46
exactly the same parameters.
47

    
48
Note that if a node is unreachable at the time a hooks is run, this
49
will not be interpreted as a deny for the execution. In other words,
50
only an actual error returned from a script will cause abort, and not
51
an unreachable node.
52

    
53
Therefore, if you want to guarantee that a hook script is run and
54
denies an action, it's best to put it on the master node.
55

    
56
*post* scripts
57
~~~~~~~~~~~~~~
58

    
59
These scripts should do whatever you need as a reaction to the
60
completion of an operation. Their return code is not checked (but
61
logged), and they should not depend on the fact that the *pre* scripts
62
have been run.
63

    
64
Naming
65
~~~~~~
66

    
67
The allowed names for the scripts consist of (similar to *run-parts* )
68
upper and lower case, digits, underscores and hyphens. In other words,
69
the regexp ``^[a-zA-Z0-9_-]+$``. Also, non-executable scripts will be
70
ignored.
71

    
72

    
73
Order of execution
74
~~~~~~~~~~~~~~~~~~
75

    
76
On a single node, the scripts in a directory are run in lexicographic
77
order (more exactly, the python string comparison order). It is
78
advisable to implement the usual *NN-name* convention where *NN* is a
79
two digit number.
80

    
81
For an operation whose hooks are run on multiple nodes, there is no
82
specific ordering of nodes with regard to hooks execution; you should
83
assume that the scripts are run in parallel on the target nodes
84
(keeping on each node the above specified ordering).  If you need any
85
kind of inter-node synchronisation, you have to implement it yourself
86
in the scripts.
87

    
88
Execution environment
89
~~~~~~~~~~~~~~~~~~~~~
90

    
91
The scripts will be run as follows:
92

    
93
- no command line arguments
94

    
95
- no controlling *tty*
96

    
97
- stdin is actually */dev/null*
98

    
99
- stdout and stderr are directed to files
100

    
101
- PATH is reset to ``/sbin:/bin:/usr/sbin:/usr/bin``
102

    
103
- the environment is cleared, and only ganeti-specific variables will
104
  be left
105

    
106

    
107
All information about the cluster is passed using environment
108
variables. Different operations will have sligthly different
109
environments, but most of the variables are common.
110

    
111
Operation list
112
--------------
113

    
114
Node operations
115
~~~~~~~~~~~~~~~
116

    
117
OP_ADD_NODE
118
+++++++++++
119

    
120
Adds a node to the cluster.
121

    
122
:directory: node-add
123
:env. vars: NODE_NAME, NODE_PIP, NODE_SIP
124
:pre-execution: all existing nodes
125
:post-execution: all nodes plus the new node
126

    
127

    
128
OP_REMOVE_NODE
129
++++++++++++++
130

    
131
Removes a node from the cluster. On the removed node the hooks are called
132
during the execution of the operation and not after its completion.
133

    
134
:directory: node-remove
135
:env. vars: NODE_NAME
136
:pre-execution: all existing nodes except the removed node
137
:post-execution: all existing nodes
138

    
139
OP_NODE_SET_PARAMS
140
++++++++++++++++++
141

    
142
Changes a node's parameters.
143

    
144
:directory: node-modify
145
:env. vars: MASTER_CANDIDATE, OFFLINE, DRAINED
146
:pre-execution: master node, the target node
147
:post-execution: master node, the target node
148

    
149
OP_NODE_EVACUATE
150
++++++++++++++++
151

    
152
Relocate secondary instances from a node.
153

    
154
:directory: node-evacuate
155
:env. vars: NEW_SECONDARY, NODE_NAME
156
:pre-execution: master node, target node
157
:post-execution: master node, target node
158

    
159

    
160
Instance operations
161
~~~~~~~~~~~~~~~~~~~
162

    
163
All instance operations take at least the following variables:
164
INSTANCE_NAME, INSTANCE_PRIMARY, INSTANCE_SECONDARIES,
165
INSTANCE_OS_TYPE, INSTANCE_DISK_TEMPLATE, INSTANCE_MEMORY,
166
INSTANCE_DISK_SIZES, INSTANCE_VCPUS, INSTANCE_NIC_COUNT,
167
INSTANCE_NICn_IP, INSTANCE_NICn_BRIDGE, INSTANCE_NICn_MAC,
168
INSTANCE_DISK_COUNT, INSTANCE_DISKn_SIZE, INSTANCE_DISKn_MODE.
169

    
170
The INSTANCE_NICn_* and INSTANCE_DISKn_* variables represent the
171
properties of the *n* -th NIC and disk, and are zero-indexed.
172

    
173

    
174
OP_INSTANCE_ADD
175
+++++++++++++++
176

    
177
Creates a new instance.
178

    
179
:directory: instance-add
180
:env. vars: ADD_MODE, SRC_NODE, SRC_PATH, SRC_IMAGES
181
:pre-execution: master node, primary and secondary nodes
182
:post-execution: master node, primary and secondary nodes
183

    
184
OP_INSTANCE_REINSTALL
185
+++++++++++++++++++++
186

    
187
Reinstalls an instance.
188

    
189
:directory: instance-reinstall
190
:env. vars: only the standard instance vars
191
:pre-execution: master node, primary and secondary nodes
192
:post-execution: master node, primary and secondary nodes
193

    
194
OP_BACKUP_EXPORT
195
++++++++++++++++
196

    
197
Exports the instance.
198

    
199

    
200
:directory: instance-export
201
:env. vars: EXPORT_NODE, EXPORT_DO_SHUTDOWN
202
:pre-execution: master node, primary and secondary nodes
203
:post-execution: master node, primary and secondary nodes
204

    
205
OP_INSTANCE_START
206
+++++++++++++++++
207

    
208
Starts an instance.
209

    
210
:directory: instance-start
211
:env. vars: INSTANCE_NAME, INSTANCE_PRIMARY, INSTANCE_SECONDARIES, FORCE
212
:pre-execution: master node, primary and secondary nodes
213
:post-execution: master node, primary and secondary nodes
214

    
215
OP_INSTANCE_SHUTDOWN
216
++++++++++++++++++++
217

    
218
Stops an instance.
219

    
220
:directory: instance-shutdown
221
:env. vars: INSTANCE_NAME, INSTANCE_PRIMARY, INSTANCE_SECONDARIES
222
:pre-execution: master node, primary and secondary nodes
223
:post-execution: master node, primary and secondary nodes
224

    
225
OP_INSTANCE_REBOOT
226
++++++++++++++++++
227

    
228
Reboots an instance.
229

    
230
:directory: instance-reboot
231
:env. vars: IGNORE_SECONDARIES, REBOOT_TYPE
232
:pre-execution: master node, primary and secondary nodes
233
:post-execution: master node, primary and secondary nodes
234

    
235
OP_INSTANCE_MODIFY
236
++++++++++++++++++
237

    
238
Modifies the instance parameters.
239

    
240
:directory: instance-modify
241
:env. vars: INSTANCE_NAME, MEM_SIZE, VCPUS, INSTANCE_IP
242
:pre-execution: master node, primary and secondary nodes
243
:post-execution: master node, primary and secondary nodes
244

    
245
OP_INSTANCE_FAILOVER
246
++++++++++++++++++++
247

    
248
Failovers an instance.
249

    
250
:directory: instance-failover
251
:env. vars: IGNORE_CONSISTENCY
252
:pre-execution: master node, secondary node
253
:post-execution: master node, secondary node
254

    
255
OP_INSTANCE_MIGRATE
256
++++++++++++++++++++
257

    
258
Migrates an instance.
259

    
260
:directory: instance-failover
261
:env. vars: INSTANCE_MIGRATE_LIVE, INSTANCE_MIGRATE_CLEANUP
262
:pre-execution: master node, secondary node
263
:post-execution: master node, secondary node
264

    
265

    
266
OP_INSTANCE_REMOVE
267
++++++++++++++++++
268

    
269
Remove an instance.
270

    
271
:directory: instance-remove
272
:env. vars: INSTANCE_NAME, INSTANCE_PRIMARY, INSTANCE_SECONDARIES
273
:pre-execution: master node
274
:post-execution: master node
275

    
276
OP_INSTANCE_REPLACE_DISKS
277
+++++++++++++++++++++++++
278

    
279
Replace an instance's disks.
280

    
281
:directory: mirror-replace
282
:env. vars: MODE, NEW_SECONDARY, OLD_SECONDARY
283
:pre-execution: master node, primary and secondary nodes
284
:post-execution: master node, primary and secondary nodes
285

    
286
OP_INSTANCE_GROW_DISK
287
+++++++++++++++++++++
288

    
289
Grows the disk of an instance.
290

    
291
:directory: disk-grow
292
:env. vars: DISK, AMOUNT
293
:pre-execution: master node, primary node
294
:post-execution: master node, primary node
295

    
296
OP_INSTANCE_RENAME
297
++++++++++++++++++
298

    
299
Renames an instance.
300

    
301
:directory: instance-rename
302
:env. vars: INSTANCE_NEW_NAME
303
:pre-execution: master node, primary and secondary nodes
304
:post-execution: master node, primary and secondary nodes
305

    
306
Cluster operations
307
~~~~~~~~~~~~~~~~~~
308

    
309
OP_CLUSTER_VERIFY
310
+++++++++++++++++
311

    
312
Verifies the cluster status. This is a special LU with regard to
313
hooks, as the result of the opcode will be combined with the result of
314
post-execution hooks, in order to allow administrators to enhance the
315
cluster verification procedure.
316

    
317
:directory: cluster-verify
318
:env. vars: CLUSTER, MASTER, CLUSTER_TAGS, NODE_TAGS_<name>
319
:pre-execution: none
320
:post-execution: all nodes
321

    
322
OP_CLUSTER_RENAME
323
+++++++++++++++++
324

    
325
Renames the cluster.
326

    
327
:directory: cluster-rename
328
:env. vars: NEW_NAME
329
:pre-execution: master-node
330
:post-execution: master-node
331

    
332
OP_CLUSTER_SET_PARAMS
333
+++++++++++++++++++++
334

    
335
Modifies the cluster parameters.
336

    
337
:directory: cluster-modify
338
:env. vars: NEW_VG_NAME
339
:pre-execution: master node
340
:post-execution: master node
341

    
342

    
343
Obsolete operations
344
~~~~~~~~~~~~~~~~~~~
345

    
346
The following operations are no longer present or don't execute hooks
347
anymore in Ganeti 2.0:
348

    
349
- OP_INIT_CLUSTER
350
- OP_MASTER_FAILOVER
351
- OP_INSTANCE_ADD_MDDRBD
352
- OP_INSTANCE_REMOVE_MDDRBD
353

    
354

    
355
Environment variables
356
---------------------
357

    
358
Note that all variables listed here are actually prefixed with
359
*GANETI_* in order to provide a clear namespace.
360

    
361
Common variables
362
~~~~~~~~~~~~~~~~
363

    
364
This is the list of environment variables supported by all operations:
365

    
366
HOOKS_VERSION
367
  Documents the hooks interface version. In case this doesnt match
368
  what the script expects, it should not run. The documents conforms
369
  to the version 2.
370

    
371
HOOKS_PHASE
372
  One of *PRE* or *POST* denoting which phase are we in.
373

    
374
CLUSTER
375
  The cluster name.
376

    
377
MASTER
378
  The master node.
379

    
380
OP_CODE
381
  One of the *OP_* values from the list of operations.
382

    
383
OBJECT_TYPE
384
  One of ``INSTANCE``, ``NODE``, ``CLUSTER``.
385

    
386
DATA_DIR
387
  The path to the Ganeti configuration directory (to read, for
388
  example, the *ssconf* files).
389

    
390

    
391
Specialised variables
392
~~~~~~~~~~~~~~~~~~~~~
393

    
394
This is the list of variables which are specific to one or more
395
operations.
396

    
397
INSTANCE_NAME
398
  The name of the instance which is the target of the operation.
399

    
400
INSTANCE_DISK_TEMPLATE
401
  The disk type for the instance.
402

    
403
INSTANCE_DISK_COUNT
404
  The number of disks for the instance.
405

    
406
INSTANCE_DISKn_SIZE
407
  The size of disk *n* for the instance.
408

    
409
INSTANCE_DISKn_MODE
410
  Either *rw* for a read-write disk or *ro* for a read-only one.
411

    
412
INSTANCE_NIC_COUNT
413
  The number of NICs for the instance.
414

    
415
INSTANCE_NICn_BRIDGE
416
  The bridge to which the *n* -th NIC of the instance is attached.
417

    
418
INSTANCE_NICn_IP
419
  The IP (if any) of the *n* -th NIC of the instance.
420

    
421
INSTANCE_NICn_MAC
422
  The MAC address of the *n* -th NIC of the instance.
423

    
424
INSTANCE_OS_TYPE
425
  The name of the instance OS.
426

    
427
INSTANCE_PRIMARY
428
  The name of the node which is the primary for the instance.
429

    
430
INSTANCE_SECONDARIES
431
  Space-separated list of secondary nodes for the instance.
432

    
433
INSTANCE_MEMORY
434
  The memory size (in MiBs) of the instance.
435

    
436
INSTANCE_VCPUS
437
  The number of virtual CPUs for the instance.
438

    
439
INSTANCE_STATUS
440
  The run status of the instance.
441

    
442
NODE_NAME
443
  The target node of this operation (not the node on which the hook
444
  runs).
445

    
446
NODE_PIP
447
  The primary IP of the target node (the one over which inter-node
448
  communication is done).
449

    
450
NODE_SIP
451
  The secondary IP of the target node (the one over which drbd
452
  replication is done). This can be equal to the primary ip, in case
453
  the cluster is not dual-homed.
454

    
455
FORCE
456
  This is provided by some operations when the user gave this flag.
457

    
458
IGNORE_CONSISTENCY
459
  The user has specified this flag. It is used when failing over
460
  instances in case the primary node is down.
461

    
462
ADD_MODE
463
  The mode of the instance create: either *create* for create from
464
  scratch or *import* for restoring from an exported image.
465

    
466
SRC_NODE, SRC_PATH, SRC_IMAGE
467
  In case the instance has been added by import, these variables are
468
  defined and point to the source node, source path (the directory
469
  containing the image and the config file) and the source disk image
470
  file.
471

    
472
NEW_SECONDARY
473
  The name of the node on which the new mirror component is being
474
  added. This can be the name of the current secondary, if the new
475
  mirror is on the same secondary.
476

    
477
OLD_SECONDARY
478
  The name of the old secondary in the replace-disks command Note that
479
  this can be equal to the new secondary if the secondary node hasn't
480
  actually changed.
481

    
482
EXPORT_NODE
483
  The node on which the exported image of the instance was done.
484

    
485
EXPORT_DO_SHUTDOWN
486
  This variable tells if the instance has been shutdown or not while
487
  doing the export. In the "was shutdown" case, it's likely that the
488
  filesystem is consistent, whereas in the "did not shutdown" case,
489
  the filesystem would need a check (journal replay or full fsck) in
490
  order to guarantee consistency.
491

    
492
CLUSTER_TAGS
493
  The list of cluster tags, space separated.
494

    
495
NODE_TAGS_<name>
496
  The list of tags for node *<name>*, space separated.
497

    
498
Examples
499
--------
500

    
501
The startup of an instance will pass this environment to the hook
502
script::
503

    
504
  GANETI_CLUSTER=cluster1.example.com
505
  GANETI_DATA_DIR=/var/lib/ganeti
506
  GANETI_FORCE=False
507
  GANETI_HOOKS_PATH=instance-start
508
  GANETI_HOOKS_PHASE=post
509
  GANETI_HOOKS_VERSION=2
510
  GANETI_INSTANCE_DISK0_MODE=rw
511
  GANETI_INSTANCE_DISK0_SIZE=128
512
  GANETI_INSTANCE_DISK_COUNT=1
513
  GANETI_INSTANCE_DISK_TEMPLATE=drbd
514
  GANETI_INSTANCE_MEMORY=128
515
  GANETI_INSTANCE_NAME=instance2.example.com
516
  GANETI_INSTANCE_NIC0_BRIDGE=xen-br0
517
  GANETI_INSTANCE_NIC0_IP=
518
  GANETI_INSTANCE_NIC0_MAC=aa:00:00:a5:91:58
519
  GANETI_INSTANCE_NIC_COUNT=1
520
  GANETI_INSTANCE_OS_TYPE=debootstrap
521
  GANETI_INSTANCE_PRIMARY=node3.example.com
522
  GANETI_INSTANCE_SECONDARIES=node5.example.com
523
  GANETI_INSTANCE_STATUS=down
524
  GANETI_INSTANCE_VCPUS=1
525
  GANETI_MASTER=node1.example.com
526
  GANETI_OBJECT_TYPE=INSTANCE
527
  GANETI_OP_CODE=OP_INSTANCE_STARTUP
528
  GANETI_OP_TARGET=instance2.example.com