README.md 12.2 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.

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

```{shell}
14
	> source $LECABASHHOME/utils.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.

Eric Coissac's avatar
Eric Coissac committed
34
----------------------------------------------------------
Eric Coissac's avatar
Eric Coissac committed
35

Eric Coissac's avatar
Eric Coissac committed
36
# Modules provided by the *LECA Bash Library*
Eric Coissac's avatar
Eric Coissac committed
37

Eric Coissac's avatar
Eric Coissac committed
38 39 40 41 42 43 44
- **[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*
- **[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
45
- **[sets](#sets)**         : manages *set* of values
Eric Coissac's avatar
Eric Coissac committed
46 47 48
- **[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
49
- **[irods](#irods)**       : provide functions wrapping the iRods iget and iput icommands.
Eric Coissac's avatar
Eric Coissac committed
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

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

# Detailed description of modules

## atexit

### Activating the module

```{shell}
	> source $LECABASHHOME/utils.sh
	> 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
78 79 80

## clobber

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

83 84 85 86 87 88 89 90 91
When the **noclobber** mode is `on` you cannot substitute an existing file by a
empty one using a UNIX redirection.

### Activating the module

```{shell}
	> source $LECABASHHOME/utils.sh
	> include clobber
```
Eric Coissac's avatar
Eric Coissac committed
92 93 94 95 96 97


### Functions provided:

#### setnoclobber

98
Sets the **noclobber** mode on.
Eric Coissac's avatar
Eric Coissac committed
99 100 101

You cannot erase an existing file by a redirection

102 103 104 105 106 107 108 109
```{shell}
	> include clobber
	> setnoclobber
	> getnoclobber
	on
```


Eric Coissac's avatar
Eric Coissac committed
110 111
#### setclobber

112
Sets the **noclobber** mode off.
Eric Coissac's avatar
Eric Coissac committed
113 114 115

You can erase an existing file by a redirection

116 117 118 119 120 121 122
```{shell}
	> include clobber
	> setclobber
	> getnoclobber
	off
```

Eric Coissac's avatar
Eric Coissac committed
123 124
#### getnoclobber

125
```{shell}
Eric Coissac's avatar
Eric Coissac committed
126 127 128
    getnoclobber
```

Eric Coissac's avatar
Eric Coissac committed
129 130
Returns the noclobber status as a string

131
-  `on`: the noclobber mode is on
132
-  `off`: the noclobber mode is off
Eric Coissac's avatar
Eric Coissac committed
133

134 135 136 137 138
```{shell}
	> include clobber
	> getnoclobber
	off
```
Eric Coissac's avatar
Eric Coissac committed
139

Eric Coissac's avatar
Eric Coissac committed
140
#### pushclobber
Eric Coissac's avatar
Eric Coissac committed
141

142
```{shell}
Eric Coissac's avatar
Eric Coissac committed
143 144 145
    pushclobber
```

Eric Coissac's avatar
Eric Coissac committed
146 147 148
Saves the current noclobber mode on a stack and sets it to 'off'

#### pushnoclobber
149 150

```{shell}
Eric Coissac's avatar
Eric Coissac committed
151 152 153
    pushnoclobber
```

Eric Coissac's avatar
Eric Coissac committed
154 155 156
Saves the current noclobber mode on a stack and sets it to 'on'

#### popclobber
157 158

```{shell}
Eric Coissac's avatar
Eric Coissac committed
159 160 161
    popclobber
```

Eric Coissac's avatar
Eric Coissac committed
162 163 164 165
Restores the last pushed noclobber mode

#### popnoclobber

166
```{shell}
Eric Coissac's avatar
Eric Coissac committed
167 168
    popnoclobber
```
Eric Coissac's avatar
Eric Coissac committed
169

Eric Coissac's avatar
Eric Coissac committed
170 171 172
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
173 174 175

## download

Eric Coissac's avatar
Eric Coissac committed
176 177 178 179 180 181 182 183 184 185 186
### Activating the module

```{shell}
	> source $LECABASHHOME/utils.sh
	> include download
```

### Functions provided:

#### download

187
```{shell}
Eric Coissac's avatar
Eric Coissac committed
188 189 190 191 192 193 194 195 196 197
    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

198
```{shell}
Eric Coissac's avatar
Eric Coissac committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
    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

216
```{shell}
Eric Coissac's avatar
Eric Coissac committed
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
    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}
	> source $LECABASHHOME/utils.sh
	> include ifs
```

### Functions provided:

#### pushifs

242
```{shell}
Eric Coissac's avatar
Eric Coissac committed
243 244 245 246 247
    pushifs <NEWIFS>
```

#### popifs

248
```{shell}
Eric Coissac's avatar
Eric Coissac committed
249 250 251 252 253 254 255
    popifs
```



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

Eric Coissac's avatar
Eric Coissac committed
256 257
## lecaluke

Eric Coissac's avatar
Eric Coissac committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
The lecaluke module provides a set of variable defining emplacement
where LECA ressources are stored on the LUKE cluster.


### Activating the module

```{shell}
	> source $LECABASHHOME/utils.sh
	> include lecaluke
```

### Functions provided:

#### latesttaxonomy

273
```{shell}
Eric Coissac's avatar
Eric Coissac committed
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
    latesttaxonomy
```

### Defined variables

#### LECA_ENVIRONEMENT

#### LOCK_DIR

#### DATA_DIR

#### BIODATA_DIR

#### TAXONOMY_DIR

#### TREMBL_DIR

#### SWISSPROT_DIR

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

Eric Coissac's avatar
Eric Coissac committed
295 296
## logging

Eric Coissac's avatar
Eric Coissac committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
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}
	> source $LECABASHHOME/utils.sh
	> include logging
```

### Functions provided:

#### logdebug

317
```{shell}
Eric Coissac's avatar
Eric Coissac committed
318 319 320 321 322 323 324
    logdebug <MESSAGE>
```

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

#### loginfo

325
```{shell}
Eric Coissac's avatar
Eric Coissac committed
326 327 328 329 330 331 332 333
    loginfo <MESSAGE>
```

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


#### logerror

334
```{shell}
Eric Coissac's avatar
Eric Coissac committed
335 336 337 338 339 340 341 342
    logerror <MESSAGE>
```

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


#### logwarning

343
```{shell}
Eric Coissac's avatar
Eric Coissac committed
344 345 346 347 348 349 350 351
    logwarning <MESSAGE>
```

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


#### setloglevel

352
```{shell}
Eric Coissac's avatar
Eric Coissac committed
353 354 355 356 357 358 359 360 361 362
    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

363
```{shell}
Eric Coissac's avatar
Eric Coissac committed
364 365 366 367 368 369 370 371 372
    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

373
```{shell}
Eric Coissac's avatar
Eric Coissac committed
374 375 376 377 378 379 380 381
    closelogfile
```

Closes the current logfile and redirect the logging to stderr.

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


Eric Coissac's avatar
Eric Coissac committed
382 383
## mutex

Eric Coissac's avatar
Eric Coissac committed
384 385 386 387 388 389 390 391 392 393 394
### Activating the module

```{shell}
	> source $LECABASHHOME/utils.sh
	> include mutex
```

### Functions provided:

#### getlock

395
```{shell}
Eric Coissac's avatar
Eric Coissac committed
396 397 398 399 400
    getlock <LOCKNAME> [global]
```

#### releaselock

401
```{shell}
Eric Coissac's avatar
Eric Coissac committed
402 403 404 405 406 407
    releaselock <LOCKNAME> [global]
```


#### getsemaphore

408
```{shell}
Eric Coissac's avatar
Eric Coissac committed
409 410 411 412 413
    getsemaphore <SEMAPHORENAME> <MAXCOUNT> [global]
```

#### releasesemaphore

414
```{shell}
Eric Coissac's avatar
Eric Coissac committed
415 416 417 418 419
    releasesemaphore <SEMAPHORENAME> [global]
```

#### releaseoldlock

420
```{shell}
Eric Coissac's avatar
Eric Coissac committed
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
    releaseoldlock [MINUTES]
```

### Variable provided

#### LOCK_DIR

#### LECA_LOCK_DELAY

#### LECA_LOCK_MAXRETRY

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

## sets

### Activating the module

```{shell}
	> source $LECABASHHOME/utils.sh
	> include sets
```

### Functions provided:

#### newset

447
```{shell}
Eric Coissac's avatar
Eric Coissac committed
448 449 450 451 452
    newset <SETNAME>
```

#### setsize

453
```{shell}
Eric Coissac's avatar
Eric Coissac committed
454 455 456 457 458
    setsize <SETNAME>
```

#### setaddvalue

459
```{shell}
Eric Coissac's avatar
Eric Coissac committed
460 461 462 463 464
    setsize <SETNAME> <VALUE>
```

#### setdelvalue

465
```{shell}
Eric Coissac's avatar
Eric Coissac committed
466 467 468 469 470
    setdelvalue <SETNAME> <VALUE>
```

#### setcontains

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

#### setunion

477
```{shell}
Eric Coissac's avatar
Eric Coissac committed
478 479 480 481 482 483
    setunion <SETNAME1> <SETNAME2> <DESTINATION>
```


#### setintersec

484
```{shell}
Eric Coissac's avatar
Eric Coissac committed
485 486 487 488 489 490 491 492 493 494 495 496 497 498 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 525 526 527 528 529 530 531 532 533 534 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
    setintersec <SETNAME1> <SETNAME2> <DESTINATION>
```

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


## stack

### Activating the module

```{shell}
	> source $LECABASHHOME/utils.sh
	> include stack
```

### 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}
	> source $LECABASHHOME/utils.sh
	> include tempdir
```

### Functions provided:

#### availabledisk

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

#### registertmpdir

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

565 566 567 568 569 570
#### getbasetmpdir

```{shell}
    getbasetmpdir
```

Eric Coissac's avatar
Eric Coissac committed
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
#### tempdirectory

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

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


## timeout

### Activating the module

```{shell}
	> source $LECABASHHOME/utils.sh
	> include timeout
```

### Functions provided:

#### timeoutcmd

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

597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
----------------------------------------------------------
## path

### Activating the module

    	> source $LECABASHHOME/utils.sh
    	> include path

### Functions provided:

#### relative2absolute

        relative2absolute <PATH>
#### absolute2relative

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


617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
----------------------------------------------------------

## irods

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

### Activating the module

```{shell}
	> source $LECABASHHOME/utils.sh
	> 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
677

678 679
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
680

681
`igetwithmd5` is not taking care of protecting is access by a lock or a semaphore.