How I Automated My Job
medium.com
medium.com
a) They help in applying forethought to a process. Maybe you can even dry-run the thing.
b) They help documenting what has been done, and how, which can be invaluable in a team setting.
c) They train you into getting incrementally better at automating, and at sensing when to automate and when not to†. If it's painful, you're not doing enough of it.
† I find that for the ones that would take ages to properly script, more often than not a combination of manual (like preparing a normalised input for the script using vim) and automated subtasks can do wonders, leveraging the best of wetware and software worlds.
If you go by the old mantra of treating servers like cattle and not pets, you have to automate everything. My VMs (EC2 instances) can be destroyed, replicated, auto scaled, etc. at any time. I have to automate no matter how long it takes to figure out.
Ubuntu and Bash had been somewhat of a mystery to me previously, but I eventually got a virtual dev environment setup and was comfortable getting in with navigating to my Vagrant folder, then executing 'vagrant up', 'vagrant ssh', etc. and then navigating to my project.
That comfort turned to annoyance as I realized how unnecessary those few keystrokes were. And despite how little time they took, I found myself getting angry at the repetition of having to enter them.
So I decided to conquer my Bash scripting fears, and wrote what many would consider to be a trivial script to navigate to the Vagrant folder on my host machine, boot up the VM, and then navigate to the appropriate project folder in the VM. Along the way I learned more about aliases, and also how to pass commands from a host to a guest machine which was one of the roadblocks I encountered.
I now can boot up and get into my project folder with a simple three character alias from within any directory on my host machine.
Again--any experienced developer would be able to script this in minutes most likely. Back then it took hours. And now those are the most satisfying three characters I type, as every time I enter them, I get reminded of the progress I'm making and that even trivial things may be candidates for automation.
In any case, I think it's more than just the time payoff. Getting repetitive tasks automated allows you to reallocate that cognitive overhead to other details, and that's a payoff that's hard to quantify, but is critical for leaving your mind free to focus on more important complexity.
If for example it only took 15 minutes of manual work it's highly unlikely that the payback of automation would occur any time soon. That's especially true because the 2nd or 3rd time there may be different special cases and you'd have to go back and change your automation.
Automating a task usually means each important parameter is entered only once, that all steps are completed, and that the results are checked.
Spend 10 hours writing a script to automate 50x10 minute jobs. You spend an extra hour, but you end up with 50 consistent results, rather than 40 successful jobs and 10 with minor mistakes.
From a maintenance perspective, a well-written script can serve as a reference after the original task was long forgotten. A poor substitute for documentation, but when performing software archaeology on legacy systems you really need everything you can get.
But yeah... got to put some effort into it. Like all docs, script-docs worn't write themselves.
(A recent example of much simpler automation going horribly wrong was a company losing their top-level domain name because it was set to auto-renew using an ex-employee's personal credit card, and their email address as contact. This worked for years until that card was cancelled and the warning emails bounced off the closed account.)
I think correctness is the most important one. Even if a manual task is quick, ensuring that you do it correctly and exactly the same way for all 30-50 things is extremely tedious. If one out of those 50 subdomains was behaving differently and you needed to investigate exactly why, that could quickly erode any time savings.
Also, automation work is much more interesting than doing stupid, repetitive tasks.
or go on holidays and your colleagues just gotta run the script.
the big benefits in scripts that is often overlooked: Given the same conditions and input, the output will always meet your expectations. There's no chance for human error.
Maybe partial automation would have been a good roi.
Or maybe being deep into manual is also neat for your brain.
First do it manually, then automate it.
Doing manual tasks is frustrating enough to motivate me to automate it. Plus I have a much better understanding of the inputs, the required outputs and constraints for the script to be more useful.
Let your scripts evolve.
Don't force everything in the first go. Simplicity is the key. Let them be rigid, readable and in a single file at first. Then, introduce flexibility as required. Abstract out core functions into individual scripts/functions/modules. This builds a certain sense of familiarity with the code that is just too endearing and eventually very productive.
Reuse your mini-scripts.
Each of my scripts does one thing. There are super-scripts that combine many of those for a task that I am performing at the moment. DRY principle. The more simple, atomic and mutually agreeable your scripts are the more powerful they get. This is just the unix way of doing things. Nothing new.
Write consistent code comments
You will write. You will forget. Make the code comments personal and consistent enough for you to immediately recognize them.
Use version control
Absolutely use it. There will be many versions of your core scripts/functions that dont agree with each other. My personal preference is a version number (semver) suffix on each file to identify which core script is used by which task script.
Reap the rewards
The rewards of putting in the effort to me have been akin to compound interest on an early investment. I have reused core scripts so heavily that prototyping any new idea is pretty easy. If there is something that a script does not do, I know what I need to read over the next weekend :)
Perhaps the majority of content read, but that’s because it is marketed.
I’m not sure how you would measure. Count all the bytes on .com vs .org?
Why is this unfortunate?
A recent approach I’ve been taking is just parsing args with something like minimist or zeit/arg and then handling the commands passed in.
It leads to a lot simpler code especially for small tasks like what is described in the parent article.
Another tip, you could also use pkg to create binaries for your new cli application so people don’t need node installed on their machine to run them.
Commander leads to incredibly simple code (as it just sets a list of options) combined with helpful CLI help output.
Abstractions can be good, but I feel as though something like commander is really just an unneccessary abstraction.
I’ll take simplicity first anyday.
It would be hard to be more explicit
"Since February 2015, the SRE (site reliability engineering) team at Stack Overflow has switched from a mixture of Python and Bash to Go. Even though Go isn't a scripting language, for small programs it compiles and runs nearly as fast as Python takes to start. At Stack Overflow we tend to prefer compiled, type-checked languages for large programs, especially when multiple people are collaborating, and, therefore, no one person is familiar with every line of code. Our policy was that Bash scripts couldn't be larger than 100 lines and Python programs couldn't be larger than 1,000 lines. Those seemed like reasonable limits. Rewriting scripts when they grew beyond the limit, however, was a lot of work. It was better to start in Go and avoid the conversion."
Not Golang, per se, but over time you are limited by your tools. Big bash scripts get unwieldy. Nodejs changes underlying dependencies (coincidentally wrestling with 4 yr old nodejs this week).
https://www.techworm.net/2016/06/programmer-automates-job-si...
This article is slightly better as it links to the original advice thread:
https://boingboing.net/2016/06/08/coder-fired-after-6-years-...
American Dream: https://github.com/bibanon/bibanon/blob/master/Stories/Ameri...
If I was better at Python, I might try my hand at writing a better Python cli lib myself. I'm surprised nobody else has yet.
https://opensource.com/article/17/5/4-practical-python-libra...
My usual go-to for that kind of stuff is Escort for Ruby. Everything is defined at a place and level that makes sense IMO.
If you mean sub-commands like version control software has, argparse can do it. A bit clunky, but doable.
I reference these regularly to keep me on track doing things that actually matter, rather than tinkering endlessly to save me having to do things that are less interesting, but would take less time.
FTFY
I have been looking at FFMPEG for automating video post-production, and recommend you do the same.
Yeah, I'm sure there is an API :-) I just haven't gotten around to looking into it yet, it's one of those annoying things that never gets to the point of seriousness, but gets a few choice words from me when I have to use Google Admin.
Git Bash. The only requirement is installing Git For Windows, which is an easy install.
how original. Saw this on reddit earlier this week, and didn't read it then, either.