"Fossies" - the Fresh Open Source Software Archive

Member "solum-12.0.0/contrib/common/utils" (30 Mar 2022, 5976 Bytes) of package /linux/misc/openstack/solum-12.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bash source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #!/bin/bash
    2 # Copyright 2014 - Rackspace Hosting
    3 #
    4 #    Licensed under the Apache License, Version 2.0 (the "License");
    5 #    you may not use this file except in compliance with the License.
    6 #    You may obtain a copy of the License at
    7 #
    8 #        http://www.apache.org/licenses/LICENSE-2.0
    9 #
   10 #    Unless required by applicable law or agreed to in writing, software
   11 #    distributed under the License is distributed on an "AS IS" BASIS,
   12 #    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13 #    See the License for the specific language governing permissions and
   14 #    limitations under the License.
   15 
   16 
   17 # Common functions for build-app and unittest-app
   18 
   19 # Add a timestamp, and log a message to STDOUT and to $LOG.
   20 
   21 function TLOG () {
   22   local MESSAGE="$*"
   23   if [ ! -z "$MESSAGE" ]; then
   24 
   25     local LOGFILE=${LOG_FILE:-/dev/null}
   26     local TIMESTAMP=$(date --iso-8601=seconds)
   27     JSONMESSAGE="{ \"@timestamp\": \"$TIMESTAMP\", \"project_id\": \"$PROJECT_ID\", \"commit_id\": \"$COMMIT_ID\", \"stage_id\": \"$BUILD_ID\", \"task\": \"$TASKNAME\", \"message\": \"$MESSAGE\"}"
   28     echo $JSONMESSAGE >> $LOGFILE
   29 
   30   fi
   31 }
   32 
   33 
   34 # Overwrite TLOG to filter for user input.
   35 function TLOG_FILTERED () {
   36   local MESSAGE="$*"
   37   if [ ! -z "$MESSAGE" ]; then
   38 
   39     # This is deliberately not a local, as it's to persist between calls to TLOG.
   40     _USER=${_USER:-false}
   41 
   42     echo "$MESSAGE" | grep -i "drone build results" && _USER=false
   43 
   44     local LOGFILE=${LOG_FILE:-/dev/null}
   45     local TIMESTAMP=$(date --iso-8601=seconds)
   46     JSONMESSAGE="{ \"@timestamp\": \"$TIMESTAMP\", \"project_id\": \"$PROJECT_ID\", \"commit_id\": \"$COMMIT_ID\", \"stage_id\": \"$BUILD_ID\", \"task\": \"$TASKNAME\", \"message\": \"$MESSAGE\", \"_user\": \"$_USER\" }"
   47     echo $JSONMESSAGE >> $LOGFILE
   48 
   49     echo "$MESSAGE" | grep -i "starting build" && _USER=true
   50 
   51     export _USER
   52 
   53   fi
   54 }
   55 
   56 
   57 # Build the logfile name, and ensure it exists.
   58 function GET_LOGFILE () {
   59   local LOG_DIR=${SOLUM_TASK_DIR:-/dev/null}
   60   if [ "$LOG_DIR" != "/dev/null" ]; then
   61     sudo mkdir -p "$LOG_DIR"
   62     sudo chmod a+w "$LOG_DIR"
   63   fi
   64 
   65   local LOG_FILE=/dev/null
   66   if [ "$LOG_DIR" != "/dev/null" ]; then
   67     LOG_FILE="$LOG_DIR/$TASKNAME-$BUILD_ID.log"
   68     touch $LOG_FILE
   69   fi
   70 
   71   echo $LOG_FILE
   72 }
   73 
   74 # Get time elapsed since $1.
   75 function elapsed () {
   76   local START=$1
   77   local NOW=$(date +"%s")
   78   expr $NOW - $START
   79 }
   80 
   81 # Profile and run a command, and return its exit code.
   82 function PRUN () {
   83   # If the first argument is "silent", then set a flag and shift.
   84   local SILENT=false
   85   if [ "$1" == "silent" ]; then
   86     SILENT=true
   87     shift
   88   fi
   89 
   90   local CMD="$*"
   91   local LOGFILE=${LOG:-/dev/null}
   92 
   93   if $SILENT; then
   94     LOGFILE=/dev/null
   95   fi
   96 
   97   if ! $SILENT; then
   98     TLOG Starting: $CMD
   99   fi
  100   local EXIT_STATUS
  101   local START=$(date +"%s")
  102   if $SILENT; then
  103     $CMD 2>&1 >> /dev/null; test ${PIPESTATUS[0]} -eq 0
  104     EXIT_STATUS=$?
  105   else
  106     TLOG Starting: $CMD
  107     $CMD 2>&1 > >(while read LINE; do TLOG $LINE; done)
  108     EXIT_STATUS=$?
  109   fi
  110 
  111   local ELAPSED=$(elapsed $START)
  112   local SUCCESS
  113   [ $EXIT_STATUS -eq 0 ] && SUCCESS="Finished" || SUCCESS="FAILED"
  114 
  115   if ! $SILENT; then
  116     TLOG $SUCCESS: $CMD "[Elapsed: $ELAPSED sec] (EXIT_STATUS=$EXIT_STATUS)"
  117   fi
  118 
  119   return $EXIT_STATUS
  120 }
  121 
  122 # Register ssh private key with ssh-agent.
  123 # SSH_AUTH_SOCK env variable will be unique to this process and
  124 # it restricts other apps to access current ssh credentials.
  125 function add_ssh_creds () {
  126   local SSH_PRIVATE_KEY=$1
  127   local APP_DIR=$2
  128 
  129   if [ -n "$SSH_PRIVATE_KEY" ]; then
  130     eval `ssh-agent -s`
  131     SSH_PRIVATE_KEY_FILE=$APP_DIR/.creds
  132     echo "$SSH_PRIVATE_KEY" > $SSH_PRIVATE_KEY_FILE
  133     chmod 600 $SSH_PRIVATE_KEY_FILE
  134     ssh-add $SSH_PRIVATE_KEY_FILE ; EXIT_STATUS=$?
  135     rm -f $SSH_PRIVATE_KEY_FILE
  136     ssh -o StrictHostKeyChecking=no git@github.com
  137     return $EXIT_STATUS
  138   else
  139     return 0
  140   fi
  141 }
  142 
  143 # De-register ssh private key with ssh-agent.
  144 function remove_ssh_creds () {
  145   local SSH_PRIVATE_KEY=$1
  146 
  147   if [ -n "$SSH_PRIVATE_KEY" ]; then
  148     ssh-agent -k
  149   fi
  150 }
  151 
  152 function test_public_repo () {
  153     local GIT_REPO=$1
  154     if [[ -z $GIT_PRIVATE_KEY ]]; then
  155         curl -If ${GIT_REPO%%.git} > /dev/null 2>&1
  156         return $?
  157     fi
  158     return 0
  159 }
  160 
  161 function git_clone_with_retry () {
  162   local GIT_REPO=$1
  163   local DESTINATION=$2
  164   shift; shift
  165   local SINGLEBRANCH=$1
  166 
  167   RETRIES=0
  168   until [ $RETRIES -ge 5 ]; do
  169     rm -rf $DESTINATION
  170     PRUN git clone $SINGLEBRANCH $GIT_REPO $DESTINATION && return 0
  171     RETRIES=$[$RETRIES+1]
  172     sleep 5
  173   done
  174   return 1
  175 }
  176 
  177 function git_clone_with_commit_sha_retry () {
  178   local GIT_REPO=$1
  179   local DESTINATION=$2
  180   local COMMIT_SHA=$3
  181 
  182   RETRIES=0
  183   until [ $RETRIES -ge 5 ]; do
  184     rm -rf $DESTINATION
  185     PRUN git clone $GIT_REPO $DESTINATION;cd  $DESTINATION; git checkout $COMMIT_SHA;cd - && return 0
  186     RETRIES=$[$RETRIES+1]
  187     sleep 5
  188   done
  189   return 1
  190 }
  191 
  192 function glance_upload_with_retry () {
  193   local DU_IMG_TAG=$1
  194   shift
  195   sudo docker save "$DU_IMG_TAG" | glance --os-image-api-version 2 --os-auth-token $OS_AUTH_TOKEN image-create --container-format=docker --disk-format=raw --name "$DU_IMG_TAG" >& /dev/null  && return 0
  196   sleep 2
  197   sudo docker save "$DU_IMG_TAG" | glance --os-image-api-version 2 --os-auth-token $OS_AUTH_TOKEN image-create --container-format=docker --disk-format=raw --name "$DU_IMG_TAG" >& /dev/null
  198 }
  199 
  200 function docker_build_with_retry () {
  201   local APP_NAME=$1
  202   local DESTINATION=$2
  203   shift; shift
  204   PRUN sudo docker build --force-rm=true -t $APP_NAME $DESTINATION && return 0
  205   sleep 2
  206   PRUN sudo docker build --no-cache --force-rm=true -t $APP_NAME $DESTINATION
  207 }
  208 
  209 function docker_load_with_retry () {
  210   local FILE=$1
  211   shift
  212   sudo docker load -i $FILE && return 0
  213   sleep 1
  214   sudo docker load -i $FILE
  215 }
  216 
  217 function docker_save_with_retry () {
  218   local OUTPUT_FILE=$1
  219   shift
  220   local DOCKER_IMG=$1
  221 
  222   sudo docker save --output $OUTPUT_FILE $DOCKER_IMG && return 0
  223   sleep 1
  224   sudo docker save --output $OUTPUT_FILE $DOCKER_IMG
  225 }