README.md 13.5 KB
Newer Older
Eric Coissac's avatar
Eric Coissac committed
1
# The *LECA Bash Library*
Eric Coissac's avatar
Eric Coissac committed
2 3 4 5

The LECA Bash Library aims to provide a set of function helping the development
of bash scripts used to submit jobs on the computation grid.

6
The LECA Bash Library is constituted by a set of bash scripts that can be
Eric Coissac's avatar
Eric Coissac committed
7
sourced in your script or directly from the interactive shell if you want
8
to use the functionalities of the LECA Bash Library from your interactive
Eric Coissac's avatar
Eric Coissac committed
9 10
session.

11
For activating the *LECA Bash Library* you must source the `lecabashlib.sh` bash script.
Eric Coissac's avatar
Eric Coissac committed
12 13

```{shell}
14
	> source $LECABASHHOME/lecabashlib.sh
Eric Coissac's avatar
Eric Coissac committed
15 16
```

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
once activated modules can be loaded using the `include` command.

```{shell}
	> include timeout
```

The include command is aware about already loaded modules and takes care to not
reload several time the same module. To force the reloading of a module, use the
`reload` command.

```{shell}
	> reload timeout
```

The module is reloaded with all its dependancies. A logging message advertise the
name of each reloaded module.

34 35 36 37 38 39 40 41 42
You can specify alternative place where to look for non standard modules by setting
the `LECABASHLIB_PATH` environment variable. If several paths have to be declared, you 
must separate them using colon

```{shell}
	> export LECABASHLIB_PATH="/new/path/1:/new/path/2:${LECABASHLIB_PATH}"
```


Eric Coissac's avatar
Eric Coissac committed
43
----------------------------------------------------------
Eric Coissac's avatar
Eric Coissac committed
44

Eric Coissac's avatar
Eric Coissac committed
45
# Modules provided by the *LECA Bash Library*
Eric Coissac's avatar
Eric Coissac committed
46

Eric Coissac's avatar
Eric Coissac committed
47 48 49 50
- **[atexit](#atexit)**     : for automatically execute task at the end of the execution
- **[clobber](#clobber)**   : for managing the clobber/noclobber status
- **[download](#download)** : for managing download from FTP and Web including MD5 checks
- **[ifs](#ifs)**           : for managing the *input field separator*
51
- **[irods](#irods)**       : provide functions wrapping the iRods iget and iput icommands.
Eric Coissac's avatar
Eric Coissac committed
52 53 54
- **[lecaluke](#lecaluke)** : declare variable related for the LECA computers in LUKE
- **[logging](#logging)**   : adds functionnalities for managing log files and log levels
- **[mutex](#mutex)**       : manages concurent access to resources through locks and semaphores
55
- **[sets](#sets)**         : manages *set* of values
56
- **[signal](#signal)**     : manages signal handler functions
Eric Coissac's avatar
Eric Coissac committed
57 58 59
- **[stacks](#stacks)**     : manages *stacks* of values
- **[tempdir](#tempdir)**   : manages the creation of temporary directories and their erasements
- **[timeout](#timeout)**   : provides a function for limiting the maximum execution time of a command
60
- **[xml](#xml)**           : provides a set of functions to write basic XML files
Eric Coissac's avatar
Eric Coissac committed
61 62 63 64 65 66 67 68 69 70

----------------------------------------------------------

# Detailed description of modules

## atexit

### Activating the module

```{shell}
71
	> source $LECABASHHOME/lecabashlib.sh
Eric Coissac's avatar
Eric Coissac committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
	> include atexit
```

### Functions provided:


#### registeratexit

Allows to register commands that will be automatically executed at the end
of the script execution.

```{shell}
	> include atexit
	> registeratexit "echo this is the end of the script"
```

----------------------------------------------------------
Eric Coissac's avatar
Eric Coissac committed
89 90 91

## clobber

92
Manages the **clobber**/**noclobber** property of the bash shell
Eric Coissac's avatar
Eric Coissac committed
93

94 95 96 97 98 99
When the **noclobber** mode is `on` you cannot substitute an existing file by a
empty one using a UNIX redirection.

### Activating the module

```{shell}
100
	> source $LECABASHHOME/lecabashlib.sh
101 102
	> include clobber
```
Eric Coissac's avatar
Eric Coissac committed
103 104 105 106 107 108


### Functions provided:

#### setnoclobber

109
Sets the **noclobber** mode on.
Eric Coissac's avatar
Eric Coissac committed
110 111 112

You cannot erase an existing file by a redirection

113 114 115 116 117 118 119 120
```{shell}
	> include clobber
	> setnoclobber
	> getnoclobber
	on
```


Eric Coissac's avatar
Eric Coissac committed
121 122
#### setclobber

123
Sets the **noclobber** mode off.
Eric Coissac's avatar
Eric Coissac committed
124 125 126

You can erase an existing file by a redirection

127 128 129 130 131 132 133
```{shell}
	> include clobber
	> setclobber
	> getnoclobber
	off
```

Eric Coissac's avatar
Eric Coissac committed
134 135
#### getnoclobber

136
```{shell}
Eric Coissac's avatar
Eric Coissac committed
137 138 139
    getnoclobber
```

Eric Coissac's avatar
Eric Coissac committed
140 141
Returns the noclobber status as a string

142
-  `on`: the noclobber mode is on
143
-  `off`: the noclobber mode is off
Eric Coissac's avatar
Eric Coissac committed
144

145 146 147 148 149
```{shell}
	> include clobber
	> getnoclobber
	off
```
Eric Coissac's avatar
Eric Coissac committed
150

Eric Coissac's avatar
Eric Coissac committed
151
#### pushclobber
Eric Coissac's avatar
Eric Coissac committed
152

153
```{shell}
Eric Coissac's avatar
Eric Coissac committed
154 155 156
    pushclobber
```

Eric Coissac's avatar
Eric Coissac committed
157 158 159
Saves the current noclobber mode on a stack and sets it to 'off'

#### pushnoclobber
160 161

```{shell}
Eric Coissac's avatar
Eric Coissac committed
162 163 164
    pushnoclobber
```

Eric Coissac's avatar
Eric Coissac committed
165 166 167
Saves the current noclobber mode on a stack and sets it to 'on'

#### popclobber
168 169

```{shell}
Eric Coissac's avatar
Eric Coissac committed
170 171 172
    popclobber
```

Eric Coissac's avatar
Eric Coissac committed
173 174 175 176
Restores the last pushed noclobber mode

#### popnoclobber

177
```{shell}
Eric Coissac's avatar
Eric Coissac committed
178 179
    popnoclobber
```
Eric Coissac's avatar
Eric Coissac committed
180

Eric Coissac's avatar
Eric Coissac committed
181 182 183
Alias for the `popclobber` function. Using one of the both commands `popnoclobber` or `popclobber` leads to the same action, restoring the last pushed noclober mode

----------------------------------------------------------
Eric Coissac's avatar
Eric Coissac committed
184 185 186

## download

Eric Coissac's avatar
Eric Coissac committed
187 188 189
### Activating the module

```{shell}
190
	> source $LECABASHHOME/lecabashlib.sh
Eric Coissac's avatar
Eric Coissac committed
191 192 193 194 195 196 197
	> include download
```

### Functions provided:

#### download

198
```{shell}
Eric Coissac's avatar
Eric Coissac committed
199 200 201 202 203 204 205 206 207 208
    download <URL> [USERNAME] [PASSWORD]
```

Downloads the file specified by `URL`.
If specifed the second (`USERNAME`) and third (`PASSWORD`) parametters allow
to indicate a login and password for a protected connection


#### downloadandcheck

209
```{shell}
Eric Coissac's avatar
Eric Coissac committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
    downloadandcheck <URL> <MD5URL> <DESTINATION> [USERNAME] [PASSWORD]
```

Downloads the file specified by `URL` and check file integrity.
The MD5 checksum file URL is indicated using the `MD5URL` parametters.
You must specified a destination filename to store the downloaded data
using the `DESTINATION` parametter. The MD5 file is downloaded under the
name DESTINATION.md5
If specifed the fourth (`USERNAME`) and fifth (`PASSWORD`) parametters allow
to indicate a login and password for a protected connection

On error the function has a return status set to *1*, *0* otherwise.



#### setmaxretry

227
```{shell}
Eric Coissac's avatar
Eric Coissac committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
    setmaxretry <MAXRETRY>
```

If downloading failed the downloadandcheck function can retry to download
the file several times before a complete failure. This function allows to
define the maximum of time the downloadandcheck tries to redownload before
exiting in error.

----------------------------------------------------------



## ifs

### Activating the module

```{shell}
245
	> source $LECABASHHOME/lecabashlib.sh
Eric Coissac's avatar
Eric Coissac committed
246 247 248 249 250 251 252
	> include ifs
```

### Functions provided:

#### pushifs

253
```{shell}
Eric Coissac's avatar
Eric Coissac committed
254 255 256 257 258
    pushifs <NEWIFS>
```

#### popifs

259
```{shell}
Eric Coissac's avatar
Eric Coissac committed
260 261 262 263 264 265 266
    popifs
```



----------------------------------------------------------

Eric Coissac's avatar
Eric Coissac committed
267 268
## lecaluke

Eric Coissac's avatar
Eric Coissac committed
269 270 271 272 273 274 275
The lecaluke module provides a set of variable defining emplacement
where LECA ressources are stored on the LUKE cluster.


### Activating the module

```{shell}
276
	> source $LECABASHHOME/lecabashlib.sh
Eric Coissac's avatar
Eric Coissac committed
277 278 279 280 281 282 283
	> include lecaluke
```

### Functions provided:

#### latesttaxonomy

284
```{shell}
Eric Coissac's avatar
Eric Coissac committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
    latesttaxonomy
```

### Defined variables

#### LECA_ENVIRONEMENT

#### LOCK_DIR

#### DATA_DIR

#### BIODATA_DIR

#### TAXONOMY_DIR

#### TREMBL_DIR

#### SWISSPROT_DIR

----------------------------------------------------------

Eric Coissac's avatar
Eric Coissac committed
306 307
## logging

Eric Coissac's avatar
Eric Coissac committed
308 309 310 311 312 313 314 315 316 317 318 319
The *logging* module define a set of function allowing to log on the standard error
or into a file messages.

All the log will be done on file descriptor 3
By default file descriptor 3 is redirected to stderr (2)
By default the logging level is set to `WARNING`.
Therefore only `WARNING` and `ERROR` messages will be actually logged.


### Activating the module

```{shell}
320
	> source $LECABASHHOME/lecabashlib.sh
Eric Coissac's avatar
Eric Coissac committed
321 322 323 324 325 326 327
	> include logging
```

### Functions provided:

#### logdebug

328
```{shell}
Eric Coissac's avatar
Eric Coissac committed
329 330 331 332 333 334 335
    logdebug <MESSAGE>
```

Writes `MESSAGE` as a debug level log to the current log file.

#### loginfo

336
```{shell}
Eric Coissac's avatar
Eric Coissac committed
337 338 339 340 341 342 343 344
    loginfo <MESSAGE>
```

Writes `MESSAGE` as a info level log to the current log file.


#### logerror

345
```{shell}
Eric Coissac's avatar
Eric Coissac committed
346 347 348 349 350 351 352 353
    logerror <MESSAGE>
```

Writes `MESSAGE` as a error level log to the current log file.


#### logwarning

354
```{shell}
Eric Coissac's avatar
Eric Coissac committed
355 356 357 358 359 360 361 362
    logwarning <MESSAGE>
```

Writes `MESSAGE` as a warning level log to the current log file.


#### setloglevel

363
```{shell}
Eric Coissac's avatar
Eric Coissac committed
364 365 366 367 368 369 370 371 372 373
    setloglevel <LEVEL>
```

Set the current logging level to the level specified by `LEVEL`.
Level is `DEBUG`, `INFO`, `WARNING` or `ERROR`.
A log is recorded only when it is submitted with a level
greater or equal to the current logging level.

#### openlogfile

374
```{shell}
Eric Coissac's avatar
Eric Coissac committed
375 376 377 378 379 380 381 382 383
    openlogfile <FILENAME>
```

Redirected all logging to the file specified by `FILENAME`.
If the file already exists, new logs are appened at the end of the file.
If the file does not exist it is created

#### closelogfile

384
```{shell}
Eric Coissac's avatar
Eric Coissac committed
385 386 387 388 389 390 391 392
    closelogfile
```

Closes the current logfile and redirect the logging to stderr.

----------------------------------------------------------


Eric Coissac's avatar
Eric Coissac committed
393 394
## mutex

Eric Coissac's avatar
Eric Coissac committed
395 396 397
### Activating the module

```{shell}
398
	> source $LECABASHHOME/lecabashlib.sh
Eric Coissac's avatar
Eric Coissac committed
399 400 401 402 403 404 405
	> include mutex
```

### Functions provided:

#### getlock

406
```{shell}
Eric Coissac's avatar
Eric Coissac committed
407 408 409 410 411
    getlock <LOCKNAME> [global]
```

#### releaselock

412
```{shell}
Eric Coissac's avatar
Eric Coissac committed
413 414 415 416 417 418
    releaselock <LOCKNAME> [global]
```


#### getsemaphore

419
```{shell}
Eric Coissac's avatar
Eric Coissac committed
420 421 422 423 424
    getsemaphore <SEMAPHORENAME> <MAXCOUNT> [global]
```

#### releasesemaphore

425
```{shell}
Eric Coissac's avatar
Eric Coissac committed
426 427 428 429 430
    releasesemaphore <SEMAPHORENAME> [global]
```

#### releaseoldlock

431
```{shell}
Eric Coissac's avatar
Eric Coissac committed
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
    releaseoldlock [MINUTES]
```

### Variable provided

#### LOCK_DIR

#### LECA_LOCK_DELAY

#### LECA_LOCK_MAXRETRY

----------------------------------------------------------

## sets

### Activating the module

```{shell}
450
	> source $LECABASHHOME/lecabashlib.sh
Eric Coissac's avatar
Eric Coissac committed
451 452 453 454 455 456 457
	> include sets
```

### Functions provided:

#### newset

458
```{shell}
Eric Coissac's avatar
Eric Coissac committed
459 460 461 462 463
    newset <SETNAME>
```

#### setsize

464
```{shell}
Eric Coissac's avatar
Eric Coissac committed
465 466 467 468 469
    setsize <SETNAME>
```

#### setaddvalue

470
```{shell}
Eric Coissac's avatar
Eric Coissac committed
471 472 473 474 475
    setsize <SETNAME> <VALUE>
```

#### setdelvalue

476
```{shell}
Eric Coissac's avatar
Eric Coissac committed
477 478 479 480 481
    setdelvalue <SETNAME> <VALUE>
```

#### setcontains

482
```{shell}
Eric Coissac's avatar
Eric Coissac committed
483 484 485 486 487
    setcontains <SETNAME> <VALUE>
```

#### setunion

488
```{shell}
Eric Coissac's avatar
Eric Coissac committed
489 490 491 492 493 494
    setunion <SETNAME1> <SETNAME2> <DESTINATION>
```


#### setintersec

495
```{shell}
Eric Coissac's avatar
Eric Coissac committed
496 497 498
    setintersec <SETNAME1> <SETNAME2> <DESTINATION>
```

499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
----------------------------------------------------------

## signal

### Activating the module

```{shell}
	> source $LECABASHHOME/lecabashlib.sh
	> include signal
```

### Functions provided:

#### registertrap

```{shell}
    registertrap <SIGNAME> <COMMAND>
```

#### run

```{shell}
    run <SIGNAME1> <SIGNAME1> <COMMAND>
```


Eric Coissac's avatar
Eric Coissac committed
525 526 527
----------------------------------------------------------


528
## stacks
Eric Coissac's avatar
Eric Coissac committed
529 530 531 532

### Activating the module

```{shell}
533
	> source $LECABASHHOME/lecabashlib.sh
534
	> include stacks
Eric Coissac's avatar
Eric Coissac committed
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
```

### Functions provided:

#### newstack

```{shell}
    newstack <STACKNAME>
```

Creates a new stack named `STACKNAME`.

#### stacksize

```{shell}
    stacksize <STACKNAME>
```

Returns the count of element stored in the stack.

#### pushvalue

```{shell}
    pushvalue <STACKNAME> <VALUE>
```

Adds the value VALUE at the end of the stack `STACKNAME`.


#### popvalue

```{shell}
    popvalue <STACKNAME> <VARIABLE>
```

Pops out the last pushed value of the stack and store it into the `VARIABLE`.
If the stack is already empty the function returns nothing
and the return status is set to *1* instead of *0* usually.



----------------------------------------------------------


## tempdir

### Activating the module

```{shell}
584
	> source $LECABASHHOME/lecabashlib.sh
Eric Coissac's avatar
Eric Coissac committed
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
	> include tempdir
```

### Functions provided:

#### availabledisk

```{shell}
    availabledisk <DIRPATH>
```

#### registertmpdir

```{shell}
    registertmpdir <DIRPATH>
```

602 603 604 605 606 607
#### getbasetmpdir

```{shell}
    getbasetmpdir
```

Eric Coissac's avatar
Eric Coissac committed
608 609 610 611 612 613 614 615 616 617 618 619 620 621
#### tempdirectory

```{shell}
    tempdirectory <VARIABLE> [MINSIZE]
```

----------------------------------------------------------


## timeout

### Activating the module

```{shell}
622
	> source $LECABASHHOME/lecabashlib.sh
Eric Coissac's avatar
Eric Coissac committed
623 624 625 626 627 628 629 630 631 632 633
	> include timeout
```

### Functions provided:

#### timeoutcmd

```{shell}
    timeoutcmd <SECONDE> <UNIX COMMAND>
```

634 635 636 637 638
----------------------------------------------------------
## path

### Activating the module

639
    	> source $LECABASHHOME/lecabashlib.sh
640 641 642 643 644 645 646 647 648 649 650 651 652 653
    	> include path

### Functions provided:

#### relative2absolute

        relative2absolute <PATH>
#### absolute2relative

```bash
    absolute2relative <PATH> <RELATIVE_TO>
```


654 655 656 657 658 659 660 661 662 663
----------------------------------------------------------

## irods

The *irods* module define a set of function wrapping the classical `iput` and
`iget` commands.

### Activating the module

```{shell}
664
	> source $LECABASHHOME/lecabashlib.sh
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
	> include irods
```

### Functions provided:

#### isetressource

```{shell}
    isetresource <RESOURCE>
```

Set the default resource used by `iput` to store data to `RESOURCE`.

----------------------------------------------------------

```{shell}
    iresourceexists <IROD_SOURCE>
```

Function returning an error status according if the specified iRods
resource exists or not.

- Returns an error status set to 0 if the resource exists,
- Returns an error status set to 1 otherwise.

ex:
```{shell}
        if iresourceexists $ipath ; then
              echo $ipath exists
        else
              echo $ipath is absent
        fi
```

----------------------------------------------------------


#### igetwithmd5

```{shell}
    igetwithmd5 <IROD_SOURCE> [LOCAL_DEST]
```

Gets an iRods resource and check if the uploaded file is compatible with the
MD5 checksum stored on iRods to. The MD5 file must have the same name than the
downloaded resource with the `.md5` extension added.

The `LOCAL_DEST` parameter is optional and without it the downloaded file is
saved in the current working directory.
Eric Coissac's avatar
Eric Coissac committed
714

715 716
The function download the file until the MD5 check is ok or the maximum retrial
count is reached.
Eric Coissac's avatar
Eric Coissac committed
717

718
`igetwithmd5` is not taking care of protecting is access by a lock or a semaphore.
719 720 721 722 723 724

## xml

### Activating the module

```{shell}
725
	> source $LECABASHHOME/lecabashlib.sh
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
	> include xml
```

### Functions provided:

#### XML_header

```{shell}
    XML_header
```

#### XML_push

```{shell}
    XML_push <TAG>
```

#### XML_pop

```{shell}
    XML_pop 
```

#### XML_tag_data

```{shell}
    XML_tag_data <TAG> <VALUES>
```

755 756 757 758 759 760
#### XML_tag_attr_data

```{shell}
    XML_tag_attr_data <TAG> <ATTRIBUTE 1> <VALUE 1> -- <VALUES>
```

761 762 763 764 765 766
#### XML_empty_tag

```{shell}
    XML_empty_tag <TAG>
```

767 768 769 770 771 772
#### XML_data

```{shell}
    XML_data <VALUES>
```