hoaproject / Infrastructure

Everything related to the infrastructure of Hoa.
http://hoa-project.net/
4 stars 2 forks source link

Provisioner: Draft Debian prelude and nginx #6

Closed Hywan closed 8 years ago

Hywan commented 8 years ago

Related to #4.

So far, the skeleton lands, with Debian prelude and nginx skeleton.

Hywan commented 8 years ago

Lets' talk a moment about the Debian prelude.

We introduce the following function:

These ones are quite easy to understand and to write. Nothing fancy. arch::error is very important since using STDERR correctly is the basis. arch::info is visually helpful and arch::echo is here for consistency.

The prelude sets -euf -o pipefail for safety concerns. This is the basis to fail early and thus avoiding a crazy script to continue its execution. However, having set -e is sometimes really annoying. Let's say we would like to check if git exists with which git: If it returns > 0, then the script will exit. This is not what we want. So, just like in Rust, I have introduced the arch::unsafe function that executes a portion of code in an unsafe “block”/state. Basically, we set +e at the beginning and then set -e after. But things get complicated from here. We set -e before returning, and we must return the status of the portion of code. If which git returns > 0, then arch::unsafe returns > 0 and then the script will exit. So arch::unsafe always returns 0 and puts the portion of code status inside the $unsafe_status variable. The portion of code is executed inside a sub-shell to protect the current one. Finally, to get the output of the portion of code, arch::unsafe takes as a first argument a variable name. Thus:

arch::unsafe "git" "which git"
echo $unsafe_status
echo $git

is strictly equivalent to:

git="$(which git)"
echo $?
echo $git

if we do not consider set -e.

Formalizing this, ensuring all constraints (sub-shell, API…), took me some hours, but the concept of an “unsafe block” is pretty solid and it allows to preserve “Shell safety” rules (like set -e).

Thoughts @hoaproject/hoackers?