• Increase font size
  • Default font size
  • Decrease font size
Home Projects SysFunc SysFunc : Documentation

SysFunc : Documentation

E-mail Print PDF
Read : 264 times
(0 votes, average 0 out of 5)



1. Utility functions
     1.1. sf_loaded
     1.2. sf_version
     1.3. sf_exec_url
2. Temporary file management
     2.1. sf_cleanup
     2.2. sf_get_tmp
3. Error handling
     3.1. sf_fatal
     3.2. sf_unsupported
     3.3. sf_error
     3.4. sf_warning
4. User interaction
     4.1. sf_msg
     4.2. sf_trace
     4.3. sf_msg1
     4.4. sf_msg_section
     4.5. sf_banner
     4.6. sf_ask
     4.7. sf_yn_question
5. File/dir management
     5.1. sf_delete
     5.2. sf_find_executable
     5.3. sf_create_dir
     5.4. sf_save
     5.5. sf_rename_to_old
     5.6. sf_check_copy
     5.7. sf_check_block
     5.8. sf_contains_block
     5.9. sf_chown
     5.10. sf_chmod
     5.11. sf_check_link
     5.12. sf_comment_out
     5.13. sf_uncomment
     5.14. sf_check_line
6. User/group management
     6.1. sf_set_passwd
     6.2. sf_set_passwd_aix
     6.3. sf_create_group
     6.4. sf_delete_group
     6.5. sf_user_exists
     6.6. sf_delete_user
     6.7. sf_create_user
7. OS management
     7.1. sf_compute_os_id
     7.2. sf_reboot
     7.3. sf_shutdown
     7.4. sf_poweroff
8. Filesystem/Volume management
     8.1. sf_has_dedicated_fs
     8.2. sf_get_fs_mnt
     8.3. sf_get_fs_size
     8.4. sf_set_fs_space
     8.5. sf_create_fs
     8.6. sf_lv_exists
     8.7. sf_vg_exists
     8.8. sf_create_lv
     8.9. sf_create_vg
     8.10. sf_create_lv_fs
9. Service management
     9.1. sf_svc_enable
     9.2. sf_svc_disable
     9.3. sf_svc_install
     9.4. sf_svc_uninstall
     9.5. sf_svc_is_installed
     9.6. sf_svc_start
     9.7. sf_svc_stop
     9.8. sf_svc_base
     9.9. sf_svc_script
10. Network
     10.1. sf_domain_part
     10.2. sf_host_part
     10.3. sf_dns_addr_to_name
     10.4. sf_dns_name_to_addr
     10.5. sf_primary_ip_address
11. Software management
     11.1. sf_soft_exists
     11.2. sf_soft_list
     11.3. sf_soft_is_installed
     11.4. sf_soft_is_upgradeable
     11.5. sf_soft_is_up_to_date
     11.6. sf_soft_install_upgrade
     11.7. sf_soft_uninstall
     11.8. sf_soft_remove
     11.9. sf_soft_reinstall
     11.10. sf_soft_clean_cache

 


1 - Utility functions

1.1 - sf_loaded

Checks if the library is already loaded

Of course, if it can run, the library is loaded. So, it always returns 0.

Allows to support the 'official' way to load sysfunc : sf_loaded 2>/dev/null || . sysfunc.sh

 
ArgsNone
ReturnsAlways 0
DisplaysNothing
 

1.2 - sf_version

Displays library version

 
ArgsNone
ReturnsAlways 0
DisplaysLibrary version (string)
 

1.3 - sf_exec_url

Retrieves executable data through an URL and executes it.

Supports any URL accepted by wget.

By default, the 'wget' command is used. If the $WGET environment variable is set, it is used instead (use, for instance, to specify a proxy or an alternate configuration file).

 
Args
$1Url
Returnsthe return code of the executed program
Displaysdata displayed by the executed program
 

2 - Temporary file management

2.1 - sf_cleanup

Deletes all temporary files

 
ArgsNone
ReturnsAlways 0
Displaysnothing
 

2.2 - sf_get_tmp

Returns an unused temporary path

The returned path can then be used to create a directory or a file.

 
ArgsNone
ReturnsAlways 0
DisplaysAn unused temporary path
 

3 - Error handling

3.1 - sf_fatal

Displays an error message and aborts execution

 
Args
$1message
$2Optional. Exit code.
ReturnsDoes not return. Exits with the provided exit code if arg 2 set, with 1 if not.
DisplaysError and abort messages
 

3.2 - sf_unsupported

Fatal error on unsupported feature

Call this function when a feature is not available on the current operating system (yet ?)

 
Args
$1feature name
ReturnsDoes not return. Exits with code 2.
DisplaysError and abort messages
 

3.3 - sf_error

Displays an error message

If the ERRLOG environment variable is set, it is supposed to contain a path. The error message will be appnded to the file at this path. If the file does not exist, it will be created.

 
Args
$1Message
ReturnsAlways 0
DisplaysError message
 

3.4 - sf_warning

Displays a warning message

 
Args
$1message
ReturnsAlways 0
DisplaysWarning message
 

4 - User interaction

4.1 - sf_msg

Displays a message (string)

If the $sf_noexec environment variable is set, prefix the message with '(n)'

 
Args
$1message
ReturnsAlways 0
DisplaysMessage
 

4.2 - sf_trace

Display trace message

If the $sf_verbose environment variable is set, displays the message. If not, does not display anything.

 
Args
$1message
ReturnsAlways 0
Displaysmessage if verbose mode is active, nothing if not
 

4.3 - sf_msg1

Displays a message prefixed with spaces

 
Args
$1message
ReturnsAlways 0
Displaysmessage prefixed with spaces
 

4.4 - sf_msg_section

Displays a 'section' message

This is a message prefixed with a linefeed and some hyphens. To be used as paragraph/section title.

 
Args
$1message
ReturnsAlways 0
DisplaysMessage
 

4.5 - sf_banner

Displays a 'banner' message

The message is displayed with an horizontal separator line above and below

 
Args
$1message
ReturnsAlways 0
Displaysmessage
 

4.6 - sf_ask

Ask a question to the user

Due to compatibility problems, don't use 'no newline' echo options anymore.

 
Args
$1message
ReturnsAlways 0
Displaysmessage
 

4.7 - sf_yn_question

Asks a 'yes/no' question, gets answer, and return yes/no code

Works at least for questions in english, french, and german : Accepts 'Y', 'O', and 'J' for 'yes' (upper or lowercase), and anything different is considered as 'no'

If the $sf_forceyes environment variable is set, the user is not asked and the 'yes' code is returned.

 
Args
$1Question string
Returns0 for 'yes', 1 for 'no'
DisplaysQuestion and typed answer if $sf_forceyes not set, nothing if $sf_forceyes is set.
 

5 - File/dir management

5.1 - sf_delete

Recursively deletes a file or a directory

Returns without error if arg is a non-existent path

 
Args
$1Path to delete
ReturnsAlways 0
DisplaysInfo msg
 

5.2 - sf_find_executable

Find an executable file

 
Args
$1Executable name
$2Optional. List of directories to search. If not search, use PATH
ReturnsAlways 0
DisplaysAbsolute path if found, nothing if not found
 

5.3 - sf_create_dir

Creates a directory

If the given path argument corresponds to an already existing file (any type except directory or symbolic link to a directory), the program aborts with a fatal error. If you want to aAlways 0 this (if you want to create the directory, even if somathing else is already existing in this path), call sf_delete first. If the path given as arg contains a symbolic link pointing to an existing directory, it is left as-is.

 
Args
$1Path
$2Optional. Directory owner[:group]. Default: root
$3Optional. Directory mode in a format accepted by chmod. Default: 755
ReturnsAlways 0
DisplaysInfo msg
 

5.4 - sf_save

Saves a file

No action if the 'sf_nosave' environment variable is set to a non-empty string. If the input arg is the path of an existing regular file, the file is copied to '$path.orig' TODO: improve save features (multiple numbered saved versions,...)

 
Args
$1Path
ReturnsAlways 0
DisplaysInfo msg
 

5.5 - sf_rename_to_old

Renames a file to '<dir>/old.<filename>

 
Args
$1Path
ReturnsAlways 0
DisplaysInfo msg
 

5.6 - sf_check_copy

Copy a file or the content of function's standard input to a target file

The copy takes place only if the source and target files are different. If the target file is already existing, it is saved before being overwritten. If the target path directory does not exist, it is created.

 
Args
$1Source path. Special value: '-' means that data to copy is read from stdin, allowing to produce dynamic content without a temp file.
$2Target path
$3Optional. File creation mode. Default=644
ReturnsAlways 0
DisplaysInfo msg
 

5.7 - sf_check_block

Replaces or prepends/appends a data block in a file

The block is composed of entire lines and is surrounded by special comment lines when inserted in the target file. These comment lines identify the data block and allow the function to be called several times on the same target file with different data blocks. The block identifier is the base name of the source path.

If the given block is not present in the target file, it is appended or prepended, depending on the flag argument. If the block is already present in the file (was inserted by a previous run of this function), its content is compared with the new data, and replaced if different. In this case, it is replaced at the exact place where the previous block lied.

If the target file exists, it is saved before being overwritten.

If the target path directory does not exist, it is created.

 
Args
$1If this arg starts with the '-' char, the data is to be read from stdin and the string after the '-' is the block identifier.

If it does not start with '-', it is the path to the source file (containing the data to insert).

$2Target path
$3Optional. Target file mode.

Default=644

$4Optional. Flag. Set to 'prepend' to add data at the beginning of the file.

Default mode: Append.

Used only if data block is not already present in the file.

Can be set to '' (empty string) to mean 'default mode'.

$5Optional. Comment character.

This argument, if set, must contain only one character. This character will be used as first char when building the 'identifier' lines surrounding the data block.

Default: '#'.

ReturnsAlways 0
DisplaysInfo msg
 

5.8 - sf_contains_block

Checks if a file contains a block inserted by sf_check_block

 
Args
$1The block identifier or source path
$2File path
Returns0 if the block is in the file, !=0 if not.
DisplaysNothing
 

5.9 - sf_chown

Change the owner of a set of files/dirs

 
Args
$1owner[:group]
$2+List of paths
Returnschown status code
DisplaysNothing
 

5.10 - sf_chmod

Change the mode of a set of files/dirs

 
Args
$1mode as accepted by chmod
$2+List of paths
Returnschmod status code
DisplaysNothing
 

5.11 - sf_check_link

Creates or modifies a symbolic link

The target is saved before being modified. Note: Don't use 'test -h' (not portable) If the target path directory does not exist, it is created.

 
Args
$1Link target
$2Link path
ReturnsAlways 0
DisplaysInfo msg
 

5.12 - sf_comment_out

Comment one line in a file

The first line containing the (grep) pattern given in argument will be commented out by prefixing it with the comment character. If the pattern is not contained in the file, the file is left unchanged.

 
ArgsNone
ReturnsAlways 0
DisplaysInfo msg
 

5.13 - sf_uncomment

Uncomment one line in a file

The first commented line containing the (grep) pattern given in argument will be uncommented by removing the comment character. If the pattern is not contained in the file, the file is left unchanged.

 
ArgsNone
ReturnsAlways 0
DisplaysInfo msg
 

5.14 - sf_check_line

Checks if a given line is contained in a file

Takes a pattern and a string as arguments. The first line matching the pattern is compared with the string. If they are different, the string argument replaces the line in the file. If they are the same, the file is left unchanged. If the pattern is not found, the string arg is appended to the file. If the file does not exist, it is created.

 
Args
$1File path
$2Pattern to search
$3Line string
ReturnsAlways 0
DisplaysInfo msg
 

6 - User/group management

6.1 - sf_set_passwd

Change a user's password

Works on HP-UX, Solaris, and Linux. Replaces an encrypted passwd in /etc/passwd or /etc/shadow. TODO: Unify with AIX and autodetect the file to use (passwd/shadow)

 
Args
$1Username
$2Encrypted password
$3File path
ReturnsAlways 0
DisplaysNothing
 

6.2 - sf_set_passwd_aix

Set an AIX password

TODO: Unify with other supported OS

 
Args
$1Username
$2Encrypted password
ReturnsAlways 0
Displaysnothing
 

6.3 - sf_create_group

Create a user group

 
ArgsNone
ReturnsAlways 0
DisplaysInfo msg
 

6.4 - sf_delete_group

Remove a group

 
Args
$1Group name
ReturnsStatus from system command
Displaysnothing
 

6.5 - sf_user_exists

Checks if a given user exists on the system

 
Args
$1User name to check
Returns0 if user exists; != 0 if not
Displaysnothing
 

6.6 - sf_delete_user

Remove a user account

 
Args
$1User name
ReturnsStatus from system command
Displaysnothing
 

6.7 - sf_create_user

Create a user

To set the login shell, initialize the CREATE_USER_SHELL variable before calling the function. For accounts with no access allowed (blocked accounts), $7, $8, and $9 are not set.

 
ArgsNone
ReturnsAlways 0
DisplaysInfo msg
 

7 - OS management

7.1 - sf_compute_os_id

Computes and displays a string defining the curent system environment

The displayed string is a combination of the OS name, version, system architecture and may also depend on other parameters like, for instance, the RedHat ES/AS branches. It is an equivalent of the 'channel' concept used by RedHat. I personnally call it 'OS ID' for 'OS IDentifier' and use it in every script where I need a single string to identify the system environment the script is currently running on. If the current system is not recognized, the program aborts. By convention, environments recognized by this function must support the rest of the library. Contributors welcome ! Feel free to enhance this function with additional recognized systems, especially with other Linux distros, and send your patches.

 
ArgsNone
ReturnsAlways 0
DisplaysOS ID
 

7.2 - sf_reboot

Shutdown and restart the host

 
ArgsNone
Returnsdoes not return
Displaysnothing
 

7.3 - sf_shutdown

Shutdown and halt the host

 
ArgsNone
Returnsdoes not return
Displaysnothing
 

7.4 - sf_poweroff

Shutdown and poweroff the host

 
ArgsNone
Returnsdoes not return
Displaysnothing
 

8 - Filesystem/Volume management

8.1 - sf_has_dedicated_fs

Checks if a directory is a file system mount point

 
Args
$1Directory to check
Returns0 if true, !=0 if false
Displaysnothing
 

8.2 - sf_get_fs_mnt

Gets the mount point of the filesystem containing a given path

 
Args
$1Path (must correspond to an existing element)
ReturnsAlways 0
DisplaysThe mount directory of the filesystem containing the element
 

8.3 - sf_get_fs_size

Get the size of the filesystem containing a given path

 
Args
$1Path (must correspond to an existing element)
ReturnsAlways 0
DisplaysFS size in Mbytes
 

8.4 - sf_set_fs_space

Extend a file system to a given size

 
Args
$1A path contained in the file system to extend
$2The new size in Mbytes, or the size to add if prefixed with a '+'
ReturnsAlways 0
DisplaysInfo msg
 

8.5 - sf_create_fs

Create a file system, mount it, and set system configuration to mount it

at system start Refuses existing directory as mount point (security)

 
Args
$1Mount point
$2device path
$3FS type
$4Optional. Mount point directory owner[:group]
Returns0 if no error, 1 on error
DisplaysInfo msg
 

8.6 - sf_lv_exists

Checks if a given logical volume exists

 
Args
$1VG name
$2LV name
Returns0 if it exists, 1 if not
DisplaysNothing
 

8.7 - sf_vg_exists

Checks if a given volume group exists

 
Args
$1VG name
Returns0 if it exists, 1 if not
DisplaysNothing
 

8.8 - sf_create_lv

Create a logical volume

 
Args
$1Logical volume name
$2Volume group name
$3Size in Mbytes
Returns0: OK, !=0: Error
DisplaysInfo msg
 

8.9 - sf_create_vg

Create a volume group

 
Args
$1volume group name
$2PE size (including optional unit, default=Mb)
$3Device path, without the /dev prefix
Returns0: OK, !=0: Error
DisplaysInfo msg
 

8.10 - sf_create_lv_fs

Create a logical volume and a filesystem on it

Combines sf_create_lv and sf_create_fs

 
Args
$1Mount point (directory)
$2Logical volume name
$3Volume group name
$4File system type
$5Size in Mbytes
$6Optional. Directory owner[:group]
Returns0: OK, !=0: Error
DisplaysInfo msg
 

9 - Service management

9.1 - sf_svc_enable

Enable service start/stop at system boot/shutdown

 
Args
$*Service names
ReturnsAlways 0
DisplaysInfo msg
 

9.2 - sf_svc_disable

Disable service start/stop at system boot/shutdown

 
Args
$*Service names
ReturnsAlways 0
DisplaysInfo msg
 

9.3 - sf_svc_install

Install a service script (script to start/stop a service)

 
Args
$1Source script
$2Service name
ReturnsAlways 0
DisplaysInfo msg
 

9.4 - sf_svc_uninstall

Uninstall a service script (script to start/stop a service)

 
Args
$1Service name
ReturnsAlways 0
DisplaysInfo msg
 

9.5 - sf_svc_is_installed

Check if a service script is installed

 
Args
$1Service name
Returns0 is installed, 1 if not
DisplaysNothing
 

9.6 - sf_svc_start

Start a service

 
Args
$1Service name
ReturnsReturn code from script execution
DisplaysOutput from service script
 

9.7 - sf_svc_stop

Stop a service

 
Args
$1Service name
ReturnsReturn code from script execution
DisplaysOutput from service script
 

9.8 - sf_svc_base

Display the base directory of service scripts

 
ArgsNone
ReturnsAlways 0
DisplaysString
 

9.9 - sf_svc_script

Display the full path of the script corresponding to a given service

 
Args
$1Service name
ReturnsAlways 0
DisplaysScript path
 

10 - Network

10.1 - sf_domain_part

Suppresses the host name part from a FQDN

Displays the input string without the beginning up to and including the first '.'.

 
Args
$1Input FQDN
ReturnsAlways 0
Displaystruncated string
 

10.2 - sf_host_part

Extracts a hostname from an FQDN

Removes everything from the first dot up to the end of the string

 
Args
$1Input FQDN
ReturnsAlways 0
Displaystruncated string
 

10.3 - sf_dns_addr_to_name

Resolves an IP address through the DNS

If the address cannot be resolved, displays nothing

 
Args
$1IP address
$2Optional. DNS server to ask
ReturnsAlways 0
DisplaysHost name as returned by the DNS or nothing if address could not be resolved.
 

10.4 - sf_dns_name_to_addr

Resolves a host name through the DNS

If the name cannot be resolved, displays nothing

 
Args
$1Host name to resolve
$2Optional. DNS server to ask
ReturnsAlways 0
DisplaysIP address as returned by the DNS or nothing if name could not be resolved.
 

10.5 - sf_primary_ip_address

Get the primary address of the system

This is an arbitrary choice, such as the address assigned to the first network nterface. Feel free to improve !

 
ArgsNone
ReturnsAlways 0
DisplaysIP address or nothing if no address was found
 

11 - Software management

11.1 - sf_soft_exists

Check if software exist (installed or available for installation)

 
ArgsNone
Returns0 if every software exists, !=0 if not
DisplaysNothing
 

11.2 - sf_soft_list

List installed software

Returns a sorted list of installed software Linux output: (name-version-release.arch)

 
ArgsNone
ReturnsAlways 0
Displayssoftware list
 

11.3 - sf_soft_is_installed

Check if software is installed

 
Args
$*software name(s)
Returns0 if every argument software are installed. 1 if at least one of them is not.
DisplaysNothing
 

11.4 - sf_soft_is_upgradeable

Check if a newer version of a software is available

Note : if the software is not installed, it is not considered as updateable Note : yum returns 0 if no software are available for update

 
Args
$*software name(s)
Returns0 if at least one of the given software(s) can be updated.
DisplaysNothing
 

11.5 - sf_soft_is_up_to_date

Check if the installed version of a software is installed and the latest

version

 
Args
$*software name(s)
Returns0 if every argument software are installed and up to date (for yum)
DisplaysNothing
 

11.6 - sf_soft_install_upgrade

Install or upgrade a software

Install or updates a software depending on its presence on the host If the software is up to date, no action.

 
Args
$*software name(s)
ReturnsAlways 0
DisplaysInfo msg
 

11.7 - sf_soft_uninstall

Uninstall a software (including dependencies)

Return without error if the software is not installed

 
Args
$*software name(s)
ReturnsAlways 0
DisplaysInfo msg
 

11.8 - sf_soft_remove

Uninstall a software (ignoring spendencies)

Return without error if the software is not installed

 
Args
$*software name(s)
ReturnsAlways 0
DisplaysInfo msg
 

11.9 - sf_soft_reinstall

Reinstall a software, even at same version

 
Args
$*software name(s)
ReturnsAlways 0
DisplaysInfo msg
 

11.10 - sf_soft_clean_cache

Clean the software installation cache

 
ArgsNone
ReturnsAlways 0
DisplaysNothing