53 points · by schizoidboy · 9y ago · 53 comments
sup on the other hand has a very small surface area of things needed to learn if you're familiar with shell scripting. I would definitely recommend it for smaller projects.
I was annoyed by all the structure Ansible, Chef, etc impose, so now I use Fabric [1]. It's for a few personal servers, and for that it works just fine.
With that said, i still prefer tools like Sup that just stick to bash & unix. As much as i hate bash & unix, not using it requires learning yet another API that probably will end up with either missing edge cases, or complexity due to edge cases. Unix is the way it is because the "problem" is complex, not because the devs involved were incompetent. An abstraction ontop of Unix is just another API, for me at least.
Declarative: can first be tested, verified, dry-run, and implemented in any language. salt (my preferred CM) has yaml data files that can be analyzed and verified long before any system command occurs, for example.
Imperitive: You don't know what it does unless you run it. You don't even know if it will execute without syntax error, especially in a dynamic language like bash (posixcube) or ruby (chef, et al).
# Loop through the list of servers passed in
for cubevar_app_server in ${cubevar_app_servers}; do
# Get the IP address of the server
cubevar_app_server_ip="$(dig +short ${cubevar_app_server} || cube_check_return)" || cube_check_return
# Skip ourselves by comparing to our hostname
if [ "${cubevar_app_server}" != "$(cube_hostname)" ]; then
if ! firewall-cmd -q --zone=trusted --query-source=${cubevar_app_server_ip} ; then
cube_echo "Adding ${cubevar_app_server}'s IP ${cubevar_app_server_ip} to trusted whitelist"
firewall-cmd --zone=trusted --add-source=${cubevar_app_server_ip} || cube_check_return
firewall-cmd --permanent --zone=trusted --add-source=${cubevar_app_server_ip} || cube_check_return
fi
fi
done
This would be run as follows (where the -o parameter is expanded from ~/.ssh/known_hosts): posixcube.sh -h $TARGETSERVER -o cubevar_app_servers=*.mydomain.com -c firewall_whitelistAn argument might be made that this would live better in a (templated or not) config file in version control - and then the action become: fill in template, push config, (re)load config.
In this case, for Ansible, you'd probably try to use command (there's also she'll, but simple is better - when possible): http://docs.ansible.com/ansible/command_module.html
* Idempotent file templating with variable substitution
* Plain-text and encrypted variable files
* Logical packaging of recipes
* Roles and a way to check for roles in scripts
* Repeatable sets of actions for certain types of servers
So beyond the basics in posixcube.sh of uploading itself to the remote host and providing a consistent API, the above requirements were solved with: cube_set_file_contents, gpg for encryption, "cubes" for logical packaging (although loose scripts and straight commands are also supported), -r ROLE, and cubespecs.ini, respectively.After having migrated an entire Ruby on Rails architecture (https://github.com/myplaceonline/myplaceonline_posixcubes) to posixcube, I thought it went very well, although I certainly see some of the downsides of shell scripting such as checking for error codes in a pipeline, strange HEREDOC interpolation, etc. I also see some of the benefits of complete idempotency, but I didn't find the additional procedural checks to emulate idempotency (outside of file templates) that difficult. For the simplest cube example from the previous link which configures an NFS server, see https://github.com/myplaceonline/myplaceonline_posixcubes/bl... and for a more complex nginx+Rails cube, see https://github.com/myplaceonline/myplaceonline_posixcubes/bl...
You can test it as simply as:
git clone https://github.com/myplaceonline/posixcube
cd posixcube
./posixcube.sh -h $SERVER "cube_echo Hello World"
For more complex examples, see the usage. I'm open to any feedback and any philosophical debate on this approach versus Ansible, Docker, etc.This is merely pushing some dumb shell scripts (units) that we write to be idempotent, and "dependency" order is handled by a flat file (groups) that references the scripts (units) or other flat files (groups), running each one in sequence (with silent+logs, or verbosity) in its own shell (so that each unit can't leak env vars to the next one) that sources a very small library of tool shell functions (currently three functions that tally up to 41 lines atop the 189 mentioned above).
The whole thing is so small, simple and approachable that it just flies in the face of puppet/chef/ansible in terms of productivity at our scale and for our problem domain.
I didn't even consider opening it given how small it is but I guess it could turn out to be useful to some nonetheless.
And frankly, after having some very sensible patches rejected (this was a few years ago, before the acquisition), I got fatigued of the entire project.
- YAML quoting.
- Quasi Python expressions from Jinja 2.
- Which fields are expressions and which ones are strings is not always clear.
[1] https://github.com/myplaceonline/posixcube/issues/1 [2] https://github.com/myplaceonline/posixcube/issues/2 [3] https://github.com/myplaceonline/posixcube/issues/3
And shell script is a fickle language -- I'd at least consider using "trap" and/or "set -e", "set -u" -- maybe "set -p" rather than "echo 'what I am about to do'; what I am about to do". See eg: http://redsymbol.net/articles/unofficial-bash-strict-mode/
I agree that ansible is far from perfect - but I'm not convinced posixcube is a great alternative right now.
From a brief look at the code, it does look nicely structured and commented, though!
I've played a bit with the idea of doing something similar (mostly because I'm not convinced all the complexity present in ansible is necessary for my use case -- which appear to be similar to your use-case -- a few small simple server configs).
I've seen people here on HN recommending some makefiles for deployment (along with simple shell scripts) -- and I've played a bit with using m4 for templates rather than here-documents -- and it looks like that might work fine. Does mean your base-installs/bootstraps/images will need m4 (or "compile" the templates on the workstation you run your automation from).
But right now, I feel GNU guix is the right approach: have a small, minimal, vm/container host (right now I'm playing with Ubuntu and LXD/LXC - Xen and KVM also works nicely), and have all images be managed by something like guix.
That reduces the need for additional management and automation to the install of hosts - possibly pxe-installs of guix if you need/want to run some hosts on bare metal -- and should simply management by having system level rollback etc.
What are some bashisms that you find particularly useful? I wonder if I can just reproduce them in POSIX. So far, I haven't found POSIX too bad, although I don't have much experience making huge and complicated shell scripts. posixcube does support some optional Bashisms already like tab auto-completion and `caller`.
> And shell script is a fickle language -- I'd at least consider using "trap" and/or "set -e"
I didn't like `set -e` because it disallows using "benign" error codes. For example, one of the most commonly used APIs in posixcube is cube_set_file_contents. You pass the file you want to update and the source file. If the file is updated, it returns 0; otherwise, it returns 1. This is commonly used like this:
if cube_set_file_contents "/etc/npt.conf" "templates/ntp.conf"; then
cube_service restart ntpd
fi
I hadn't considered `set -u` and it's part of POSIX. My initial reaction is that it's a nice feature, so thanks for pointing it out, and I might set it.pipefail is a Bashism, and I'm torn on this. Handling errors in a pipeline is one of the worst parts of POSIX shell scripts. However, using something like pipefail means exiting with an error without a stack. I think it's always useful to have a stack to quickly jump to where the error is in which script. So I still prefer this guideline:
There are non-standard mechanisms like pipefail and PIPESTATUS, but the standardized
approach is to run each command separately and check the status. For example:
cube_app_result1="$(command1 || cube_check_return)" || cube_check_return
cube_app_result2="$(printf '%s' "${cube_app_result1}" | command2 || cube_check_return)" || cube_check_return
> I agree that ansible is far from perfect - but I'm not convinced posixcube is a great alternative right now.Please see my reply to another comment in this thread, asking how a simple firewall whitelist configuration would be done. I think this would be a useful example to debate around.
> From a brief look at the code, it does look nicely structured and commented, though!
That's very nice to hear, thanks! This was my first time doing anything hardcore in shell scripting.
> I've seen people here on HN recommending some makefiles for deployment (along with simple shell scripts) -- and I've played a bit with using m4 for templates rather than here-documents -- and it looks like that might work fine. Does mean your base-installs/bootstraps/images will need m4 (or "compile" the templates on the workstation you run your automation from).
I really like that posixcube has 0 requirements, but I'm not philosophically opposed to requirements, so I'm certainly curious what a make-based system would look like. Does make have imperative programming like shell scripts?
> But right now, I feel GNU guix is the right approach: have a small, minimal, vm/container host (right now I'm playing with Ubuntu and LXD/LXC - Xen and KVM also works nicely), and have all images be managed by something like guix.
I'm not aware of that, I'll check it out.
Thanks for the comments.
Mostly [[ vs old [/test, advanced parameter expansion/substitution, arrays and maybe local variables.
It's been rather long since I attempted to get some dark magic working in posix shell - so I'm not entirely sure.
Either way, any proper scripting language will be more ergonomic to work in when you've reached beyond trivial (which posixcube already has).
There are simply too many anti-patterns to check for - too easy to miss quoting etc.
Python isn't great for (posix) system automation - but it's pretty good. And now I can trust all my systems to have python3. Then I feel it a bit hard to use shell beyond simple "command1, command2, command3"-type simple automation.
That said, it can be great fun to weave pipes and inputs in she'll, when you get it working. But as you've pointed out error handling with complex pipes isn't very good - and as always it's better to keep things simple, when possible.
Sorry to break it to you, but you have an agent anyway, except it's the same fragile channel for debugging, administrative access, and running scripts, so once you start to need to configure it, you can break all the things at the same time.
Oh, and you just dropped proper configuration management. Now you need to add a task queue (another thing that can break and overflow and what not) to re-send configuration commands when some server is down, which would not be a problem in the first place with appropriate architecture of the system.
Unless I'm not understanding what you mean by configuration management, I don't see what prevents the "agent-less" setup from having this. Store the config in VCS, or if it has secrets, some ACL'd storage system that's accessible by your laptop/whatever is actually running the deploy.
Agent-less here, at least to me, means not having a server that the hosts phone back to, and instead having the deploying machine call out to the servers with SSH. It's just connecting in the opposite direction, but allows you to connect from wherever is most convenient. SSH is built for running commands, and a lot of the existing stuff seems to just reinvent that.
> Now you need to add a task queue (another thing that can break and overflow and what not) to re-send configuration commands when some server is down,
If a server is oddly down that I expect up, I'd much rather the task failed outright, as I've clearly expected the machine to be up (by attempting to have it execute something) when it is not. Time to for a human to re-evaluate the situation, not retry.
It also isn't clear to me that all deployment tasks are idempotent, and safe to retry. Most that I've dealt with are, but only most, not all.
So instead of a single place where your machine gets configs from you have potentially multiple machines that appear and disappear in an ad-hoc manner. Such setup is harder to maintain (and harder to defend security-wise) than a setup that has designated master.
> SSH is built for running commands, and a lot of the existing stuff seems to just reinvent that.
You're mistaken. SSH was not built for running commands, it was built for providing interactive shell access -- and for this works good.
Running administrative commands through SSH is just silly. With the tooling we have there's not even proper error reporting (how to tell that a command reached its target server but failed there? you can use heuristics of exit code at best).
>> Now you need to add a task queue (another thing that can break and overflow and what not) to re-send configuration commands when some server is down,
> If a server is oddly down that I expect up,
And if it was planned downtime? "Tough luck"?
> I'd much rather the task failed outright, as I've clearly expected the machine to be up (by attempting to have it execute something) when it is not.
You'd much rather the server be monitored, with other systems being resilient than failing every now and then for random reasons and needing special additional actions to be taken to recover them.
By agent, I meant something more narrow: a prerequisite that must be installed on a server before it works. In that sense, the agent here is sshd, but that can be presumed to always exist.
> so once you start to need to configure it, you can break all the things at the same time.
Do you mean if the script updates sshd configuration?
> Oh, and you just dropped proper configuration management. Now you need to add a task queue (another thing that can break and overflow and what not) to re-send configuration commands when some server is down
I'm not sure what you mean, can you please elaborate on a use case of a task queue?
Thanks for the comments!
Answering my own question by re-reading your comment: I'm not familiar with systems automatically re-trying commands. What if there is some fatal error on the server? It seems like this is something that requires a human, not a task queue. Relatedly, if the system has the `caller` built-in, when there's an error, a stack trace is dumped, including the line number of the failing cube for someone to quickly jump to the failing line in the script.
With that said, a retry mechanism could be built-in if it's a big use case.