Changes between Initial Version and Version 1 of AptitudeUpdateScript


Ignore:
Timestamp:
Jun 9, 2015, 7:40:45 PM (3 years ago)
Author:
chris
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • AptitudeUpdateScript

    v1 v1  
     1These two scripts are used to update the Debian packages on the servers and to record the updates in `/root/Changelog`.
     2
     3== a-up ==
     4
     5{{{
     6#!/usr/bin/env bash
     7
     8# This script runs "aptitude full-upgrade" and updates the Changelog
     9
     10# location of the logchange script, we assume it has been
     11# installed int he same directory as this script is in
     12#DIR="/usr/local/bin"
     13DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
     14LOGCHANGE="$DIR/logchange"
     15
     16# check that the script is being run by root
     17if [[ "$(id -u)" != "0" ]] ; then
     18  echo "You must run '$0' as root or via sudo"
     19  exit 1
     20fi
     21
     22# check that the logchange script is installed
     23if [[ ! -f "${LOGCHANGE}" ]] ; then
     24  echo "You need to install the '${LOGCHANGE}' script before you can run $0"
     25  exit 2
     26fi
     27
     28# For Ubuntu Touch
     29UBUNTU_TOUCH=$( uname -a | grep 'ubuntu-phablet' )
     30if [[ "${UBUNTU_TOUCH}" ]]; then
     31  echo "Ubuntu touch so skipping kernel update"
     32  export FLASH_KERNEL_SKIP="true"
     33fi
     34
     35# check that packages are installed and if not then install, this depends on the
     36# package name matching the binary in /usr/bin, this check is done first because
     37# it's faster
     38DEPENDENCIES="apt-show-versions aptitude"
     39for d in ${DEPENDENCIES}; do
     40  if [[ ! -f "/usr/bin/${d}" ]] ; then
     41    DEP_CHECK=$(dpkg -s ${d} 2>&1 | grep "Status: install ok installed")
     42    if [[ ! "${DEP_CHECK}" ]]; then
     43       echo "Installing '${d}'"
     44       apt-get install ${d}
     45    fi
     46  fi
     47done
     48
     49# get updates
     50apt-get -qq update
     51# get a list of updates
     52UPDATES=$(apt-show-versions -b -u | xargs)
     53# if we have updates then install them and write the list of updates to the
     54# Changelog
     55if [[ "${UPDATES}" ]]; then
     56  echo "About to upgrade '${UPDATES}'"
     57  ${LOGCHANGE} "${UPDATES} : updated"
     58  aptitude full-upgrade
     59  # Delete dowloaded packages to free up space
     60  if [[ "${UBUNTU_TOUCH}" ]]; then
     61    apt-get clean
     62  else
     63    aptitude autoclean
     64    echo "If ownCloud has been updated run the following commands:"
     65    echo "su - owncloud -s /bin/bash"
     66    echo "php console.php upgrade"
     67  fi
     68  exit 0
     69else
     70  echo "No updates today"
     71  exit 0
     72fi
     73}}}
     74
     75== logchange ==
     76
     77{{{
     78#!/usr/bin/env bash
     79
     80# This is a script to update the /root/Changelog
     81
     82# File name of the Changelog
     83CHANGELOG_NAME="Changelog"
     84
     85DATESTAMP="$(date +%Y-%m-%d)"
     86TIMESTAMP="$(date +%Y-%m-%d-%H-%M-%S)"
     87
     88# check that the script is being run by root
     89if [[ "$(id -u)" != "0" ]] ; then
     90  echo "You must run $0 as root or via sudo"
     91  exit 2
     92fi
     93
     94# check that $HOME is set
     95if [[ ${HOME} ]]; then
     96  CHANGELOG_DIR="${HOME}"
     97  CHANGELOG="${CHANGELOG_DIR}/${CHANGELOG_NAME}"
     98  BACKUP_DIR="${CHANGELOG_DIR}/.${CHANGELOG_NAME}"
     99  CHANGELOG_BACKUP="${BACKUP_DIR}/${CHANGELOG_NAME}.${TIMESTAMP}"
     100else
     101  echo "You are homeless?"
     102  exit 1
     103fi
     104
     105# get a username and if we can't use root
     106if [[ ${SUDO_USER} ]]; then
     107  CHANGELOG_EDITOR="${SUDO_USER}"
     108elif [[ ${USER} ]]; then
     109  CHANGELOG_EDITOR="${USER}"
     110else
     111  CHANGELOG_EDITOR="root"
     112fi
     113
     114# check for some changes on standard input
     115if [[ $1 ]]; then
     116  CHANGES="$1"
     117elif [[ ! "$1" ]]; then
     118  echo "Type the changes you would like recorded then [ENTER]:"
     119  read changes
     120  CHANGES=${changes}
     121fi
     122
     123# create the backup directory if it doesn't exist
     124if [[ ! -d "${BACKUP_DIR}" ]] ; then
     125  mkdir ${BACKUP_DIR}
     126fi
     127
     128# backup the CHANGELOG if it exists
     129if [[ -f "${CHANGELOG}" ]] ; then
     130  cp -a ${CHANGELOG} ${CHANGELOG_BACKUP}
     131fi
     132
     133# write the date and user to the CHANGELOG
     134echo -e "${DATESTAMP}\t${CHANGELOG_EDITOR}" > ${CHANGELOG}
     135
     136# write what we have done, taken from standard in
     137echo -e "\t*\t${CHANGES}" >> ${CHANGELOG}
     138
     139# write a blank line
     140echo >> ${CHANGELOG}
     141
     142# write the backup back to the $CHANGELOG
     143if [[ -f "${CHANGELOG_BACKUP}" ]] ; then
     144  cat ${CHANGELOG_BACKUP} >> ${CHANGELOG}
     145fi
     146
     147# gz the backup if it exists
     148if [[ -f "${CHANGELOG_BACKUP}" ]] ; then
     149  gzip ${CHANGELOG_BACKUP}
     150fi
     151
     152exit 0
     153}}}