Giter VIP home page Giter VIP logo

bashible's Introduction

BASHIBLE

Bashible is a deployment/automation tool written in Bash (DSL).

!!! WARNING !!!

Lots of features have been removed in the version 3.x. These features have been found unnecessary and easily replaceable.

Why?

Tools like Puppet, Chef or Ansible are sometimes too heavy to do simple things. On the other hand, plain shell scripts tend to be difficult to read and full of unhandled failures. Bashible stays somewhere in the middle: a bashible script is just a bash script, but better structured with additional features.

Features:

  • chaining of bash functions and commands
  • environment variables and script arguments can be used as usual
  • a failed command halts the process
  • skipping tasks which have already been done
  • dependencies (calling a script from a script)
  • delayed script calling (an action is done only once at the very end)
  • simple template engine (in Bash :-))
  • auto-chdir (the working directory is always the same)
  • setting variables safe (checking whether they are not empty)
  • does not re-implement The World (if you want a remote access, use ssh or pdsh, use bash for loops, etc.)
  • helper functions (files editing, etc.)

At the moment, bashible is used on Centos, Ubuntu and OpenBSD. Not tested on other platforms.

Suggestions are welcome! :-)

Example

A bashible script.ble (scripts should have the .ble extension):

@ Prerequisities
  - on host3 host4
  - i_am root
  - call ./system_base.ble
  - not empty echo $1

@ Setting up some variables
  - set_var FQDN not empty hostname
  - set_var DOMAIN2 not empty evaluate "echo $FQDN | awk -F . '{ print \$(NF-1) \".\" \$NF }' "
  # expects an ip address to be passed as the first argument to the script
  - set_var MY_IP echo $1
  # expects a public key to be passed on stdout
  - set_var PUBLIC_KEY not empty cat

@ Adding user webuser and his public key
  - may_fail useradd webuser
  - add_line "$PUBLIC_KEY" /home/webuser/.ssh/authorized_keys

@ Running bundle install
  - cd /var/www
  - bundle install

@ Install nginx unless already
  - skip_if test -x /usr/sbin/nginx
  - yum_install nginx
  - rsync -av /shared/config/webserver/ /

@ Start nginx and ensure it is running
  - service nginx start
  - timeout 20 wait_for_tcp "$MY_IP":80 up

You would then run the script by executing

echo YOUR PUBLIC KEY | bashible script.ble 10.20.30.40

By the way, @ represents a block of tasks, - represents a task. Both are bash functions, named @ and - (their behaviour is explained below).

About the same in plain bash:

basedir=`dirname $(readlink -f "$0")`

if [ "`hostname`" == host3 -o "`hostname`" == host5 ]; then
  exit 1
fi
if [ "$USER" != guest1 -a "$USER" != guest2 ]; then
  exit 1
fi
source './system_base.ble' || { echo 'error sourcing functions'; exit 1; }

echo "Setting up some variables"
FQDN=`hostname`
if [ -z "$FQDN" ]; then
  echo "FQDN is empty"; exit 1
fi
DOMAIN2=`echo $FQDN | awk -F . '{ print \$(NF-1) \".\" \$NF }' "`
if [ -z "$DOMAIN2" ]; then
  echo "DOMAIN2 is empty"; exit 1
fi
MY_IP=$1
if [ -z "$MY_IP" ]; then
  echo "MY_IP is empty"; exit 1
fi
PUBLIC_KEY=`cat`
if [ -z "$PUBLIC_KEY" ]; then
  echo "PUBLIC_KEY is empty"; exit 1
fi

echo "Adding user webuser and his public key"
useradd webuser
if ! grep "$PUBLIC_KEY" /home/webuser/.ssh/authorized_keys; then
   echo "$PUBLIC_KEY" >> /home/webuser/.ssh/authorized_keys || { echo "can't edit file"; exit 1; }
fi

echo "Running bundle install"
cd /var/www || { echo "can't chdir"; exit 1; }
bundle install || { echo "can't bundle install"; exit 1; }

cd "$basedir" || { echo "can't chdir"; exit 1; }
if [ ! -x /usr/sbin/nginx ]; then
  echo "Installing nginx unless already"
  yum install -y nginx || { echo "yum failed"; exit 1; }
  echo "Installing default vhosts.d contents unless already"
  rsync -av /shared/config/webserver/ / || { echo "rsync failed"; exit 1; }
fi

echo "Start nginx and ensure it is running"
service start nginx
timeout 20 bash -c '
  while ! netstat -ltn | grep LISTEN | grep "$MY_IP":80; do
    sleep 1; 
  done
'

Install & usage

 wget https://raw.githubusercontent.com/mig1984/bashible/master/bashible
 chmod 755 bashible
 ./bashible your-script.ble ARG1 ARG2 ...

Functions

Core functions

@ MESSAGE
- COMMAND ARG1 ARG2 ...
base_dir PATH
call PATH ARG1 ARG2 ...
delayed_call PATH
empty COMMAND ARG1 ARG2 ...
evaluate STRING
export_var NAME COMMAND ARG1 ARG2 ...
fail MESSAGE
finish MESSAGE
finish_if COMMAND ARG1 ARG2 ...
halt MESSAGE
halt_if COMMAND ARG1 ARG2 ...
force_call PATH ARG1 ARG2 ...
i_am
not COMMAND ARG1 ARG2 ...
on HOST HOST2 ...
may_fail COMMAND ARG1 ARG2 ...
print_error MSG print_info MSG print_warn MSG quiet COMMAND ARG1 ARG2 ...
reset_base_dir
set_var NAME COMMAND ARG1 ARG2 ...
skip MESSAGE
skip_if COMMAND ARG1 ARG2 ...
template TEMPLATE_PATH RESULT_PATH
toplevel
unless 'EVALUATED STRING' COMMAND ARG1 ARG2 ...
var_empty NAME
version
when 'EVALUATED STRING' COMMAND ARG1 ARG2 ...

Other functions

add_line LINE PATH
append_line LINE PATH
comment_line_matching REGEXP PATH
prepend_line LINE PATH
remove_line_matching REGEX PATH
replace_line_matching REGEXP STRING PATH
replace_matching REGEXP STRING PATH
symlink SRC DEST
timeout SECS COMMAND ARG1 ARG2 ...
uncomment_line_matching REGEXP PATH
write_to PATH COMMAND ARG1 ARG2 ...
wait_for_tcp MATCH up|down

bashible's People

Contributors

mig1984 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.