Creating patch for thin clients

All the features that are in TKControl are implemented on the basis of SaltStack, therefore, to write your own developments and use the proposed solutions, you need to familiarize with the SaltStack documentation.

Main components of SaltStack

All the basic logic for interacting with clients is contained on the SaltMaster server in the directory /srv/salt and in file /etc/salt/master.d/reactor.conf:

  1. /etc/salt/master.d/reactor.conf is file declaring the reactions that should occur when certain events;
  2. /srv/salt/pillar is directory containing variables that are set on the client before performing any action;
  3. /srv/salt/states is directory containing states that can be applied on the client;
  4. /srv/salt/reactor is directory containing states of reactions to certain events;
  5. /srv/salt/_grains is directory containing scripts for adding custom grains on the client.

Files of updates

Most important system components are the files associated with updating thin clients.

  1. Go to the SaltMaster’s server;
  2. Go to directory /srv/salt/states;
  3. This directory contains 4 files for updating clients:
  • request-update.sls is file that calls window in the GUI to obtain user rights and start the update;
  • update.sls is file that first calls patch.sls and then update-last-stateapply;
  • patch.sls is file that contains the main logic for updating the thin client;
  • update-last-stateapply.sls is file that updates the date of the last successful update.

Client upgrade scripts

Updating after starting the client (basic)

  1. Client connects to the salt-master (for example, after restarting the salt-minion service);
  2. Event appears that minion has started;
  3. Reactor calls highstate for the client in response to the previous event - minion applies top.sls state;
  4. top.sls applies request-update.sls state;
  5. request-update.sls asks for the credentials of the user if he lacks rights and applies update.sls.

Update via web-application

On one client:

  1. Go to the page TKControl;
  2. Go to the tab /hosts;
  3. Select the required minion and press the button “Upgrade”;
  4. Client applies request-update.sls state.

On group of clients:

  1. Go to the page TKControl;
  2. Go to the tab /groups;
  3. Select the required group and press the button “Upgrade”;
  4. Clients belonging to the group apply request-update.sls.

Update via salt-master

  1. Go to salt-master;

  2. Execute the command:

    sudo salt 'minion_id' state.apply # applied `top.sls`
    sudo salt 'minion_id' state.apply update.sls # applied update directly, without asking for rights.
    

Example of creating patch

You can find out about all the functionality when writing states in the SaltStack documentation.

To create patch, add the following block to the file patch.sls:

patch-2:
  cmd.run:
    - name: "touch /tmp/readme.txt"

This patch creates file readme.txt файл in /tmp directory.

Start applying the patch via salt-master:

sudo salt 'skupov-stagin-tkcontrol-client' state.apply

Fill out the form on the client and click OK.

Result of the execution can be seen in the “Recent Actions” tab (/actions).

Add one more block to the file patch.sls and deliberately call an error:

patch-3:
  cmd.run:
    - name: "exit 1"

Start applying the patch via salt-master:

sudo salt 'skupov-stagin-tkcontrol-client' state.apply

Fill out the form on the client and click OK.

Result of the execution can be seen in the “Recent Actions” tab (/actions).

Since the update ended with an error, it is saved as an unsuccessful update.

Result of failed update can be seen in the “Failed Updates” tab (/failed-updates).

Click on the “Information” button. Detailed and consistent progress of the update will be displayed.

“Problem” value in the “Status” column means that the last update run on this host was unsuccessful.

Fix the problem and start applying the update again.

Replace command “exit 1” to “exit 0” in the block patch-3:

patch-3:
  cmd.run:
    - name: "exit 0"

Start applying the patch via salt-master:

sudo salt 'skupov-stagin-tkcontrol-client' state.apply

Fill out the form on the client and click OK.

Result of failed update can be seen in the “Recent Actions” tab (/actions).

Since the update completed successfully, the update will not appear in the Failed Updates tab. But now, for the last failed update, the value in the “Status” column has changed from “Problem” to “Fixed”. This means that the last update performed on the client was successful.

We have now covered creating small patch, but often in practice you have to create many separate patches and it is best to store and modify them separately.

For this:

# Delete the current patch
sudo rm patch.sls

# Create directory for patches
sudo mkdir patch

# Add the direct patch file
cat <<EOF | sudo tee patch/patch-1.sls
patch-1:
  cmd.run:
    - name: "echo Multifiles Patch!"
EOF

# Add init file
cat <<EOF | sudo tee patch/init.sls
include:
  - patch/patch-1
EOF

Start applying the patch via salt-master:

sudo salt 'skupov-stagin-tkcontrol-client' state.apply

Fill out the form on the client and click OK.

Result of failed update can be seen in the “Recent Actions” tab (/actions).