Apache User Homepages Automatically – Salt Package-File-Service Example

These are example daemon configuration files for salt. Package-file-service is the most common way to configure daemons.

If you’re new, start with salt master-slave installation and a commented example of SSH Server install.

Where the Configuration Files Are?

First, install everything manually. You can only automate things you know how to do.
Find out which configuration files were modified. If you edited them with sudoedit, it’s obvious. But what if you used a command or a GUI to create the files?
You can modify the files and then look which files were modified. For example, you could run ‘sudo a2enmod userdir’, then create a timeline of /etc/apache2/. The timeline command is from Linux forensics article.

$ cd /etc/apache2/
$ sudo find -printf '%T+ M %p\n%A+ A %p\n%C+ C %p\n'|sort
# ...
2018-04-03+13:38:25.3595187980 M ./mods-enabled/userdir.conf
2018-04-03+13:38:25.3595187980 M ./mods-enabled/userdir.load

To find out more about these files, you can use ‘cat’, ‘less’ and ‘ls -l’. In this case, ‘ls -l’ reveals they are symlinks, symbolic links.
An easier to remember format for printing modification times with find could be

$ find -printf "%T+ %p\n"|sort
# ...
2018-04-03+13:38:25.3595187980 ./mods-enabled/userdir.conf
2018-04-03+13:38:25.3595187980 ./mods-enabled/userdir.load

Where

  • “%T+” is the modification time
  • “%p” is the filename with path
  • “\n” is newline

Quick Way with a Command

When using a command in a state, you must make the command idempotent. Here, we use “creates” parameter to only run the command if a file does not exist.

$ cat /srv/salt/apache/init.sls
apache2:
 pkg.installed
/var/www/html/index.html:
 file.managed:
   - source: salt://apache/default-index.html
a2enmod userdir:
 cmd.run:
   - creates: /etc/apache2/mods-enabled/userdir.conf
apache2service:
 service.running:
   - name: apache2
   - watch:
     - cmd: 'a2enmod userdir'

Better Way with Files

We can convert the previous example to use files. This is more reliable than running shell commands from states.

$ cat /srv/salt/apache/init.sls
apache2:
 pkg.installed
/var/www/html/index.html:
 file.managed:
   - source: salt://apache/default-index.html
/etc/apache2/mods-enabled/userdir.conf:
 file.symlink:
   - target: ../mods-available/userdir.conf
/etc/apache2/mods-enabled/userdir.load:
 file.symlink:
   - target: ../mods-available/userdir.load
apache2service:
 service.running:
   - name: apache2
   - watch:
     - file: /etc/apache2/mods-enabled/userdir.conf
     - file: /etc/apache2/mods-enabled/userdir.load

Contents of the Template

$ cat /srv/salt/apache/default-index.html
See you at TeroKarvinen.com

Applying the Manifest

Over master-server

$ sudo salt '*' state.apply apache

Testing it

To simplify the example, a regular user creates his homepage manually.

$ whoami
tero
$ cd /home/tero/
$ mkdir public_html
$ echo "Teros homepage" |tee public_html/index.html
Teros homepage

Depending on your setup, you might need to fix a 403 forbidden by giving some permissions: ‘chmod ugo+x $HOME $HOME/public_html/; chmod ug+r  $HOME/public_html/index.html”.
Test the homepage by opening your web browser and browsing to http://localhost/~tero/
Do you see Tero’s homepage? Well done, your Package-File-Service works.
Updated: This article has been improved multiple times.

Posted in Uncategorized | Tagged , , , , , , , , , , , , , | Comments Off on Apache User Homepages Automatically – Salt Package-File-Service Example

Comments are closed.