Skip to content

Internal functions

Internal functions include internal bash methods, and builtin operations.

Internal methods

Internal bash methods can be called in your bash scripts.

Wrappers for display

In order to unify the presentation of messages on the “standard” and “error” outputs, I have created 3 methods:

MethodsUseOutput usedDisplay example
_info_info "Your message"stdout”[INFO] Your message”
_warn_warn "Your message"stdout”[WARN] Your message”
_error_error "Your message"stderr”[ERROR] Your message”

System state changes

It can be useful to define and obtain a summary of changes made by a shell on a system.

To define a change, follow the command that impacts the environment with the “_change” instruction.

Example of use: The script creates a file on the system which de facto changes the system status. The number of changes made is indicated in the execution report, while the details appear in the log file.

Fenêtre de terminal
touch /tmp/myfile
_change

After execution, you get a similar result:

display changes

The trace file details all the changes made:

Example traces (unrelated to the previous screenshot) :

[...]
shellStdOut: #[C]: Changes made during the script execution:
shellStdOut: #[C]: Change #0: /usr/bin/etcdctl --cacert=/etc/kubernetes/pki/etcd/ca.crt --cert=/etc/kubernetes/pki/etcd/server.crt --endpoints=https://127.0.0.1:2379 --key=/etc/kubernetes/pki/etcd/server.key snapshot save "$DIRBACKUP/etcd-snapshot-$(date +%Y-%m-%d_%H:%M:%S_%Z).db" > /dev/null 2>&1
shellStdOut: #[C]: Change #1: rm -f "$F"
shellStdOut: #[C][TOTAL NUMBER OF CHANGES]:2
[INFO] Shell execution completed - 10/19/2024, 12:00:05 (1729332005998 in milliseconds) - Elapsed Time: 3375 ms

debInstall

Idempotent** method for installing Debian packages. This method is called automatically when an operation manifest specifies a list of values for the attribute: **“dependencies ”**.

Example:

The operation manifest indicates a dependency linked to the Debian package “jq”, “automation-cli” will proceed with the installation as a priority, only if the package is not installed:

[...]
dependencies:
- "jq"
[...]

checkIsServerProtected

This method, systematically executed by automation-cli, is responsible for checking the presence of a specific marker on a server. If this marker is present, no operation can be executed on this server.

Since operations are carried out by “root”, we need to find a way of temporarily preventing operations from being carried out (a compromised server, a server undergoing prolonged maintenance, etc.),

The marker is the file “/mytinydc-runtime-protection.lock ”.

Protecting a server

To protect a server, run :

Fenêtre de terminal
sudo automation-cli run -h "localhost" -c "touch /mytinydc-runtime-protection.lock"

To test, run the command “ls /”.

Fenêtre de terminal
automation-cli run -h "localhost" -c "ls -l"

The command displays an error message and exits with the exit code: “2”.

Remove protection

To remove the protection, connect via SSH to the protected server, and remove the marker by running:

Fenêtre de terminal
sudo rm -f "/mytinydc-runtime-protection.lock"

Built-in operations

These operations are performed by the control node. The operation name begins with “#”. A list of these special operations is available by typing the command: automation-cli builtin

#waitForServerRestart

In an “operationBook”, a step stipulates that the server must be restarted, by executing the “reboot” command. The problem with network connections is the “timeout”. On an SSH connection, this value is very short.

To overcome this, I’ve created an internal operation whose function is to detect server shutdown and restart. After restart, operations continue as normal.

Example of “operationBook” :

operations:
- command: "reboot"
- operation: "#waitForServerRestart"
- command: "ls /"

#isCidrNetwork

Tests whether the specified value is in CIDR network format.

**Although this test checks only one value, the built-in operations all use the same framework. “values” is of type Array.

operations:
[...]
- operation: "#isCidrNetwork"
values: [ "172.28.0.0/24" ]
[...]

#isPrivateCidrNetwork

Test whether the specified value is in CIDR network format and whether the network is private.

**Although this test checks only one value, the built-in operations all use the same framework. “values” is of type ‘Array’.

operations:
[...]
- operation: "#isPrivateCidrNetwork"
values: [ "172.28.0.0/24" ]
[...]

#confirm

This operation only works for a single host and inserts a breakpoint asking the user to confirm execution. Another implementation is currently being studied, which would allow confirmation for all hosts in the list.

operations:
[...]
- operation: "#confirm"
[...]