summaryrefslogtreecommitdiff
path: root/leap-platform-test
blob: cbae3036cd3495ab26be018d249b176fd6b51078 (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
#!/bin/bash

usage()
{
cat << EOF

usage: $0 [options] command [arguments...]

This script runs the leap platform deploy tests

OPTIONS

  -c|--config file      specify config file   
  -a|--all              run command on all nodes
  -h|--help             show help

COMMANDS
  bootstrap    <node(s)>   bootstrap node(s):
                             - leap local start
                             - leap node int
                             - sets up hostname and runs apt-get dist-upgrade
                             - leap local save
  deploy       <node(s)>   deploy node(s)
  reset_deploy <node(s)>   reset and deploy node(s)
  
EOF
}

add_nodes() {
  for i in $@
  do
    case $i in
      openvpn)
        config='openvpn.gateway_address:10.5.5.99'
        ;;
      *)
        config=''
        ;;
     esac

    leap node add --local $i $config services:$i
  done
}


bootstrap_nodes() {
  for vm in $@
  do
    leap $OPTS local start $vm
    sleep $SLEEP 
    leap $OPTS node init $vm
    # set hostname + do dist-upgrade
    leap $OPTS deploy $vm --tags site_apt::dist_upgrade,site_config::hosts
    leap $OPTS local save $vm
  done
}

compile () {
  leap $OPTS clean
  leap $OPTS compile
}


deploy() {
  LOG="$LOGDIR/deploy-$vm.log"
  ERRLOG="$LOGDIR/deploy-$vm-`date +"%F-%H%M%S"`-error.log"

  # remove colors until #1751 is fixed
  RMCOLORS='s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g'

  # we need to deploy with verbose level 2, and filter out unwanted stuff
  # until puppet errors show up in verbose level 0 +1 (#1750)
  FILTER_CLI='= read|= loading|= no change|   - executing| = executing| = applying| = ran git| = checking| = synching| = skipping file_path|   - cd .*; rsync -a|   - hiera| = created hiera/|   - cd /root/|   - rolling backexecuting|   - files/ca/client_ca.crt|\[bin,puppet\] ->'
  FILTER_PUPPET='] notice: |] No change to hostname|] Puppet apply complete|] warning: Dynamic lookup|] warning: Scope\(Class'
  FILTER_ALL="($FILTER_CLI|$FILTER_PUPPET)"

  for vm in $@
  do
    echo "Deploying \"$vm\" on `date`"|tee -a $LOG

    leap $OPTS -v 2 deploy $vm 2>&1 |  sed -r "$RMCOLORS" | tee -a $LOG | egrep -v "$FILTER_ALL" > $ERRLOG

    if [ $? -eq 0 ]
    then
      msg="From: $MAIL_FROM\nTo: $MAIL_TO\nSubject: Deploy to \"$vm\" had errors !\n\n`cat $ERRLOG`"
      echo "Deploy to \"$vm\" on `date` had errors." | tee -a $LOG
      echo
      cat $ERRLOG 
      
      if [ "$MAIL_TO" != '' ]; then
        echo "Sending this mail to $MAIL_TO:"
        printf "$msg" | sendmail -t
      fi

    else
      echo "Deploy to $vm on `date` went fine."| tee -a $LOG
    fi
  done
} 

reset_deploy() {

  compile

  echo "Starting deploy_nodes for nodes $@ as background tasks on `date`"

  for i in $@
  do
    leap $OPTS local reset $i
    # only sleep one time
    if $need_to_sleep ; then
      echo "Waiting $SLEEP secs for VM to come up..."
      sleep $SLEEP
      need_to_sleep=false
    fi
    echo $i
    deploy $i &
  done
}


config=""
all=false

if ! options=$(getopt -o ac:h -l all,config:,help -- "$@")
then 
  # something went wrong, getopt will put out an error message for us
  usage
  exit 1
fi  

eval set -- "$options"

while [ $# -gt 0 ]
do
  case $1 in
    -h|--help)     usage; exit 1;;
    -c|--config)   config=$2; shift ;;
    -a|--all)      all=true;;
    -v|--verbose)  VERBOSE=1 ;;
    (--) shift; break;;
    (-*) echo "$0: error - unrecognized option $1" 1>&2; exit 1;;
    (*) break;;
  esac
  shift
done


cmd=$1
shift
nodelist=$@

#echo "config:   $config"
#echo "cmd:      $cmd"
#echo "nodelist: $nodelist"
#echo "all:      $all"

if [ -z $config ]
then
  usage
  echo "Please provide a config file"
  exit 1
fi

source $config
cd  $ROOTDIR/$PROVIDER

if ! $all ; then
  nodes=$nodelist
else
  nodes=$NODES
fi

echo
echo "Starting $0 on `date`"

case $cmd in 
  bootstrap)    bootstrap_nodes "$nodes";;
  deploy)       deploy "$nodes";;
  reset_deploy) reset_deploy "$nodes";;
esac