Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 361 lines (310 sloc) 7.224 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
#!/bin/bash

##############################################################################
# DO NOT MODIFY THIS FILE. Instead, modify 'helpers/custom'.
##############################################################################

COOKBOOK_NAME="$(basename $0)"
DIR="$( cd "$( dirname "$0" )" && pwd )"

#
# Run a given recipe.
#
# Arguments can be passed to the 'recipe' function. They will be accessible by
# the recipe as $2, $3, $4, etc.
#
recipe () {
  CURRENT_RECIPE_NAME=$1
  DEFAULT_ASSETS_PATH="$DIR/assets/default/$CURRENT_RECIPE_NAME"
  COOKBOOK_ASSETS_PATH="$DIR/assets/$COOKBOOK_NAME/$CURRENT_RECIPE_NAME"
  local custom_recipe="$DIR/recipes/custom/$CURRENT_RECIPE_NAME"
  local default_recipe="$DIR/recipes/default/$CURRENT_RECIPE_NAME"

  if [ -f $custom_recipe ]; then
    log "Running recipe '$custom_recipe'..." 1
    separator
    . $custom_recipe

  elif [ -f $default_recipe ]; then
    log "Running recipe '$default_recipe'..." 1
    separator
    . $default_recipe
  else
    error "Could not find recipe for '$CURRENT_RECIPE_NAME'. Fail!"
  fi

  cd $DIR
}

#
# Prints the 'finished' banner.
#
finished () {
  spacer 1
  separator "="
  echo " FINISHED: '$COOKBOOK_NAME'"
  separator "="
  spacer 1
}

#
# Writes a log line to the screen
#
# If specified, the first parameter is the number of empty lines to print
# before the log message.
#
# If specified, the second parameter is the number of empty lines to print
# before the log message.
#
log () {
  if [[ $2 -gt 0 ]]; then
    spacer $2
  fi

  echo " * $1"

  if [[ $3 -gt 0 ]]; then
    spacer $3
  fi
}

#
# Writes an error log line to the screen and exit with an error code.
#
error () {
  spacer 2
  echo " -> $1"
  spacer 2
  exit 1
}

#
# Write one or many empty lines to the screen.
#
spacer () {
  if [ $1 ]; then
    local spaces=$1
  else
    local spaces=1
  fi

  for (( i=0; i<$spaces; i++ )) do
    echo ""
  done
}

#
# noop
#
noop () {
  return 0
}

#
# Write a separator to the screen.
#
# You can optionally specify the separator character. Default is '-'.
#
separator () {
  if [ $1 ]; then
    local char=$1
  else
    local char='-'
  fi

  local width=$(tput cols)

  for (( i=0; i < $width-2; i++ )) do
    local output="$output$char"
  done
  echo $output
}

#
# Update packages in package manager.
#
package_update () {
  log "Updating package manager..." 0 1
  detect_package_manager

  if [ "$PACKAGE_MANAGER" == 'apt-get' ]; then
    apt-get update -y
  elif [ "$PACKAGE_MANAGER" == 'yum' ]; then
    yum check-update -y
  elif [ "$PACKAGE_MANAGER" == 'brew' ]; then
    brew update
  else
    error "Unknown package manager: $PACKAGE_MANAGER"
  fi

  if [ $? -ne 0 ]; then
    error "An error occured while updating packages. Fail!"
  else
    spacer 2
  fi
}

#
# Install a package through package manager
#
package () {
  log "Installing package '$1'..."
  detect_package_manager

  test_package_installed $1 > /dev/null 2>&1

  if [ $? -eq 0 ]; then
    log "Package '$1' is already installed. Skipping."
    return 0
  fi

  if [ "$PACKAGE_MANAGER" == 'apt-get' ]; then
    DEBIAN_FRONTEND=noninteractive apt-get install -y $1
  elif [ "$PACKAGE_MANAGER" == 'yum' ]; then
    yum install -y $1
  elif [ "$PACKAGE_MANAGER" == 'brew' ]; then
    brew install $1
  else
    error "Unknown package manager: $PACKAGE_MANAGER"
  fi

  if [ $? -ne 0 ]; then
    error "An error occured while installing package '$1'. Fail!"
  else
    spacer 2
  fi
}

#
# Determine if a package is installed or not.
#
# If package is installed, function will return 0. If not, it will return 1.
#
test_package_installed () {
  detect_package_manager

  # When many packages are specified, skip test.
  if [ $# -gt 1 ]; then
    return 1
  fi

  if [ "$PACKAGE_MANAGER" == 'apt-get' ]; then
    dpkg -l $1
    return $?
  fi

  # Don't know how to detect if a package is installed with other package managers.
  return 1
}

#
# Determine which package manager is in use on the system.
#
detect_package_manager () {
  if [ "$PACKAGE_MANAGER" != "" ]; then
    return 0
  fi

  if command_exist apt-get; then
    PACKAGE_MANAGER='apt-get'
  elif command_exist yum; then
    PACKAGE_MANAGER='yum'
  elif command_exist brew; then
    PACKAGE_MANAGER='brew'
  else
    error "Could not find a package manager. Fail!"
  fi

  log "Detected package manager: $PACKAGE_MANAGER"
  return 0
}

#
# Determines if a command exist on the system.
#
command_exist () {
  command -v "$1" > /dev/null 2>&1;
}

#
# Copy a file from the assets folder to the specified location.
#
copy () {
  local cookbook_assets_source="$COOKBOOK_ASSETS_PATH/$1"
  local default_assets_source="$DEFAULT_ASSETS_PATH/$1"
  local target=$2

  if [ -f $cookbook_assets_source ]; then
    log "Copying $cookbook_assets_source to $target..."
    cp $cookbook_assets_source $target
  elif [ -f $default_assets_source ]; then
    log "Copying $default_assets_source to $target..."
    cp $default_assets_source $target
  else
    error "Could not find '$1' to copy. Fail!"
  fi
}

#
# Add a user to the system.
#
add_user () {
  local user=$1
  local pass=$2
  local args=$3

  id $user > /dev/null 2>&1

  if [ $? -eq 0 ]; then
    log "User $user already exists. Skipped creation."
  else
    log "Adding user $user..."
    [ "$pass" == "" ] && pass=generate_password

    if [[ "$args" != *nohome* ]]; then
      /usr/sbin/useradd --password `openssl passwd -crypt $pass` --create-home $user
    else
      /usr/sbin/useradd --password `openssl passwd -crypt $pass` $user
    fi
  fi
}

#
# Generate a random password.
#
generate_password() {
  local l=$1
  [ "$l" == "" ] && l=8
  tr -dc A-Za-z0-9_ < /dev/urandom | head -c ${l} | xargs
}

#
# Run a command as another user
#
run_as () {
  local user=$1
  local cmd=$2
  log "Running command as '$user'..."
  log "$cmd"
  # sudo -u $user -H -s /bin/bash -c "$cmd"
  # sudo -u $user -s /bin/bash -i "$cmd"
  su -c "$cmd" -s /bin/bash $user
}

#
# Add line to a file if line is not already present
#
add_line () {
  local line=$1
  local file=$2
  grep "$line" $file > /dev/null 2>&1

  if [ $? -ne 0 ]; then
    log "Adding '$line' to '$file'..."
    echo "$line" >> $file
  else
    log "'$line' already in '$file'. Skipping."
  fi
}

#
# Write a warning if user is not root.
#
warn_if_not_root () {
  uid=`id -u` && [ "$uid" = "0" ] ||
  { echo "WARNING: You are NOT running this script as 'root'. You might want to consider that..."; }
}

#
# Stops the execution of the script if user is not root.
#
fail_if_not_root () {
  uid=`id -u` && [ "$uid" = "0" ] ||
  { echo "You must run this as 'root'. Exiting."; exit 1; }
}

#
# Checks if a certain element has already been installed.
#
function is_installed () {
  if [ $# -gt 1 ]; then
    local args=$*
    local name=${args// /-}
  else
    local name=$1
  fi

  if [[ -f ~/.shoestrap/installed/$name ]]; then
    log "'$name' is already installed."
    return 0
  else
    log "'$name' is not installed."
    return 1
  fi
}

#
# Sets an element as installed.
#
function set_installed () {
  if [ $# -gt 1 ]; then
    local args=$*
    local name=${args// /-}
  else
    local name=$1
  fi

  mkdir -p ~/.shoestrap/installed
  touch ~/.shoestrap/installed/$name
}

Something went wrong with that request. Please try again.