未加星标

Shell Script Suggestions for Speedy Setups

字体大小 | |
[系统(linux) 所属分类 系统(linux) | 发布者 店小二03 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

We aim to rotate onto new projects every 2-4 months. This gives us the benefit of fresh eyes on each project every so often. Every time we rotate someone new onto a project, they bring new energy and make a positive impact on aspects that the current team have become complacent to.

Getting a new developer setup quickly is important for making sure they feel equipped to succeed. If getting a laptop setup with your codebase takes more then 30 minutes on average then there is room to improve!

Ideally, if I already have all the application’s dependencies this should take closer to 5 minutes. Thirty minutes is attainable for most products and any longer is going to directly affect my first impressions of the codebase. I always want a new team member’s introduction to the product to be a joyful one.

We’ve written before about having a bin/setup script to automate this process. There’s even a default bin/setup script shipped as part of all new rails applications. Here are some tips I’ve recently learnt to improve first time setups.

Be respectful of everyone’s time and attention. The rule of silence is simply that “when a program has nothing surprising, interesting or useful to say, it should say nothing”. I want as little information as possible when everything is working as expected. I aim for the following examples to fulfil on this ideal.

Every app has dependencies, be it Elasticsearch, Redis or even a certain version of Node.js. Let us start making sure these are installed and prompting developers to get them:

#!/bin/sh # Exit if any subcommand fails set -e RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[0;33m' NO_COLOR='\033[0m' CLEAR_LINE='\r\033[K' printf "[1/6]:mag_right: checking dependencies" if ! command -v node > /dev/null; then printf "${CLEAR_LINE}:skull:${RED} You must install node on your system before setup can continue${NO_COLOR}\n" printf ":information_source: On macOS:apple: you should 'brew install node'\n" exit -1 fi if [[ $(node --version) != "v4.6.0" ]]; then printf "${CLEAR_LINE}:warning:${YELLOW} You are not using a known working version of node.${NO_COLOR}\n" printf ":information_source: This might not be a problem but if you're having issues, try installing 4.6.0\n" printf "[1/6]:mag_right: checking dependencies" fi

Checkout this excellent description of why we use command -v instead of which . This script checks if you have node installed, if not you’re prompted to get it. If you have it installed it will then check the version and leave a helpful warning.

You’ll notice two elements in this script to improve readability.

The first is colors! Colors are a great way to indicate which output I need to care about. Green generally means all is well. Yellow is a potential issue and should be avoided at your own peril. And finally, Red states that there’s a problem.

The second is the careful and thoughtful application of emoji. These powerful characters have the ability to punctuate the message your trying to convey. Notice the lack of color on lines that are purely informational. By tactfully prefixing these lines with :information_source: we allow users to quickly understand the essence of the message.

A word of warning: despite popular opinion, emoji can be overused. I can give no advice when that line is crossed but I suggest you follow your :heart:.

Installing Packages/Gems/Addons

Once we’ve determined that all dependencies are installed we can automate installing packages:

#!/bin/sh # Exit if any subcommand fails set -e CLEAR_LINE='\r\033[K' printf "${CLEAR_LINE}[2/6]:hourglass_flowing_sand: Installing yarn packages" yarn > /dev/null

There are a few things here I’d like to highlight. The first is set +e and set -e . These set if the script should stop executing if a none 0 exit code occurs. By setting this manually we can mark parts of our script where we’d like to manually handle errors and make a decision if the script should continue or not.

There are many tools that do not follow the rule of silence. To fix this we can redirect the output to /dev/null . This is to keep our setup as noiseless as possible. This pattern should be applied on a case by case basis. It is sometimes desirable to allow the tool you’re using to give feedback but often there is a lot more being output then I’m interested in. > /dev/null will only redirect output to stdout, program errors are written to stderr and so will still be output.

Your Special Snowflake Specific Steps

Let’s say you have something very specific to your application such as cloning internal dependencies. You might write a script to pull down all the required repos:

#!/bin/sh # Exit if any subcommand fails set -e DIR=$PWD . ./bin/colors CLEAR_LINE='\r\033[K' function clone { printf ":hourglass_flowing_sand: $1 is being cloned" cd $DIR/.. if [ ! -d $1 ]; then # don't stop the script if there is an error cloning from github set +e git clone $2 > /dev/null # if the last command gives a non-zero exit code we should warn the user if [[ $? != 0 ]]; then printf "${CLEAR_LINE}:x:${RED} $1 failed to clone! You might not have permissions.${NO_COLOR}\n" printf ":hourglass_flowing_sand: $1 is being cloned" fi set -e fi } clone dependency_one [email protected]:my_org/dependency_one.git clone dependency_two [email protected]:my_org/dependency_two.git cd $DIR

In this case I don’t want to stop the script from completing, but I do want the user to know they do not have a complete setup. We’re using set +e to make sure the script doesn’t stop if it hits and error and then manually checking for a non-zero exit code.

Pulling it all together

I like to continue this pattern of having scripts with a single responsibility and calling them all together in bin/setup :

#!/bin/sh # Exit if any subcommand fails set -e CLEAR_LINE='\r\033[K' . ./bin/colors ./bin/setup_steps/check_dependencies ./bin/setup_steps/install_plugins ./bin/setup_steps/setup_ssl ./bin/setup_steps/pull_environment_vars_from_heroku echo "" echo -e "${CLEAR_LINE}[6/6]:tada:${GREEN} Finished!${NO_COLOR}"

本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统

主题: RedisNode.jsGRE
分页:12
转载请注明
本文标题:Shell Script Suggestions for Speedy Setups
本站链接:http://www.codesec.net/view/523411.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(linux) | 评论(0) | 阅读(83)