aboutsummaryrefslogtreecommitdiff
path: root/lib/hydra/misc
blob: 67d2e311d76bb1102cce06faf8a4fe35743ffae3 (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
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
#!/bin/bash

# Set needed environment variables and do basic checks
function hydra_set_env {
  export BASEREPOS="config doc bootless dns git/public git/private puppet ansible"
  export OSVERSION="`cut -d . -f 1 /etc/debian_version`"
  export COMMIT="`( cd $APP_BASE && git log -n 1 --pretty=oneline 2> /dev/null | cut -d " " -f 1 )`"
  export CONFIG="$HOME/.hydra/config"

  if [ "`whoami`" != 'root' ]; then
    SUDO="sudo"
  fi

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

  if [ "$NAME" == "hydra" ]; then
    if [ -z "$HYDRA" ]; then
      hydra_action_usage
      exit 1
    elif [ "$ACTION" != "register" ] && [ "$ACTION" != "init" ]; then
      if [ -z "$HYDRA_FOLDER" ] || [ ! -d "$HYDRA_FOLDER" ]; then
        echo "hydra $HYDRA not configured, aborting"
        exit 1
      fi
    fi
  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
}

# 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 $SUDO apt-get install $1 -y || exit 1
  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 $*
}

# Run a command using sudo and abort on error
function hydra_sudo_run {
  if [ -z "$SUDO" ]; then
    $@
  else
    $SUDO "$@"
  fi

  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"
  elif [ "$release" == "stretch" ]; then
    echo "buster"
  elif [ "$release" == "buster" ]; then
    echo "bullseye"
  elif [ "$release" == "bullseye" ]; then
    echo "bookworm"
  elif [ "$release" == "bookworm" ]; then
    echo "trixie"
  elif [ "$release" == "trixie" ]; then
    echo "sid"
  else
    echo "Unsupported release"
    exit 1
  fi
}

# Get fqdn from nodename
function hydra_get_fqdn_from_nodename {
  local node="$1"

  if [ -z "$node" ]; then
    return
  fi

  if [ "$node" == "localhost" ]; then
    facter fqdn
  elif echo $node | grep -q '\.'; then
    echo $node
  else
    domain=`hydra $HYDRA config domain`
    echo $node.$domain
  fi
}

function hydra_hiera_query {
  local node="$1"
  local param="$2"

  # Build basic query command
  # Hiera 3 version
  # There might be weird hiera errors, so that's why we're doing a "2> /dev/null" redirection
  #lookup="hiera --config $HYDRA_FOLDER/puppet/hiera.yaml"
  #lookup_params="settings::confdir=$HYDRA_FOLDER/puppet ::clientcert=$node"

  # Build basic query command
  # Hiera 5 version
  lookup="puppet lookup --confdir $HYDRA_FOLDER/puppet/ --node $node nodo --render-as s"

  # Hiera 3
  #role="`$lookup $nodo::role $lookup_params 2> /dev/null`"
  #location="`$lookup nodo::location $lookup_params 2> /dev/null`"

  # Hiera 5
  role="`$lookup $nodo::role 2> /dev/null`"
  location="`$lookup nodo::location 2> /dev/null`"

  #role="$(grep "^nodo::role: " $FACTS/${node}.yaml | cut -d ':' -f 2 | sed -e 's/ //g' -e 's/"//g')"
  #location="$(grep "^nodo::location: " $FACTS/${node}.yaml   | cut -d ':' -f 2 | sed -e 's/ //g' -e 's/"//g')"
  virtual="$(grep "^virtual: " $FACTS/${node}.yaml | cut -d ':' -f 2 | sed -e 's/ //g' -e 's/"//g')"
  domain="$(grep "^domain: " $FACTS/${node}.yaml   | cut -d ':' -f 2 | sed -e 's/ //g' -e 's/"//g')"

  if [ ! -z "$virtual" ]; then
    # Hiera 3
    #lookup_params="$lookup_params ::virtual=$virtual"

    # Hiera 5
    export FACTER_virtual="$virtual"
  fi

  if [ ! -z "$role" ]; then
    # Hiera 3
    #lookup_params="$lookup_params ::nodo::role=$role"

    # Hiera 5
    export FACTER_role="$role"
  fi

  if [ ! -z "$location" ]; then
    # Hiera 3
    #lookup_params="$lookup_params ::nodo::location=$location"

    # Hiera 5
    export FACTER_location="$location"
  fi

  if [ ! -z "$domain" ]; then
    # Hiera 3
    #lookup_params="$lookup_params ::domain=$domain"

    # Hiera 5
    export FACTER_domain="$domain"
  fi

  # Hiera 3
  #$lookup $param $lookup_params 2> /dev/null

  # Hiera 5
  $lookup $param 2> /dev/null
}