aboutsummaryrefslogtreecommitdiff
path: root/lib/hydra/misc
blob: 42750219690edc8eb8fd12075fce683554483e14 (plain)
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
#!/bin/bash

# Set needed environment variables and do basic checks
function hydra_set_env {
  export COMMIT="`( cd $APP_BASE && git log -n 1 --pretty=oneline | cut -d " " -f 1 )`"
  export CONFIG="$HOME/.hydra/config"
  export ACTION="$1"

  if [ ! -z "$HYDRA" ]; then
    export HYDRA_FOLDER="`hydra_eval_parameter $HYDRA`"
    export PREFERENCES="$HOME/.hydra/$HYDRA"
  fi
}

# Read a parameter from user
function hydra_user_input {
  local input
  local param="$1"
  local default="$2"
  shift 2

  if echo $param | grep -q 'passwd'; then
    read -s -rep "$* (defaults to $default): " input
  else
    read -rep "$* (defaults to $default): " input
  fi

  if [ -z "$input" ]; then
    export $param="$default"
  else
    export $param="$input"
  fi
}

# Get a configuration parameter if not previously defined by a sourced file
function hydra_user_config {
  local param="$1"
  local default="$2"
  shift 2

  if [ -z "`eval echo '$'$param`" ]; then
    hydra_user_input $param $default $*
  fi
}

# Install a package
function hydra_install_package {
  if [ -z "$1" ]; then
    return
  fi

  dpkg -s $1 &> /dev/null

  if [ "$?" == "1" ]; then
    echo "Installing package $1..."
    DEBIAN_FRONTEND=noninteractive apt-get install $1 -y
  fi
}

# Truncate a database
function hydra_truncate_database {
  if [ ! -z "$1" ]; then
    mysql $1 -e "drop database $1; create database $1;"
  fi
}

# Check for a command
function hydra_check_command {
  if [ -z "$1" ]; then
    return
  fi

  if ! which $1 &> /dev/null; then
    echo "Please install a package for $1 to run this action"
    exit 1
  fi
}

# Check for an user
function hydra_check_user {
  if [ -z "$1" ]; then
    return 1
  fi

  grep -qe "^$1:" /etc/passwd
}

# Check for a group
function hydra_check_group {
  if [ -z "$1" ]; then
    return 1
  fi

  grep -qe "^$1:" /etc/group
}

# Abort on error
function hydra_exit_on_error {
  if [ "$?" != "0" ]; then
    echo "Error: $*"
    exit 1
  fi
}

# Run a command or abort
function hydra_safe_run {
  $*
  hydra_exit_on_error $*
}

# Determine the next debian release
function hydra_next_debian_release {
  local release="$1"

  if [ "$release" == "lenny" ]; then
    echo "squeeze"
  elif [ "$release" == "squeeze" ]; then
    echo "wheezy"
  elif [ "$release" == "wheezy" ]; then
    echo "jessie"
  elif [ "$release" == "jessie" ]; then
    echo "stretch"
  else
    echo "Unsupported release"
    exit 1
  fi
}