Brain Phrye

code cooking diy fiction personal photos politics reviews tools


Moving home - updates to vcsh usage

A while back I switched to vcsh. I’ve written a few articles on using it but since then I’ve migrated machines a number of times.

The big issue I’ve found is having to manually install software on each machine. There are things my scripts depend on and things I just expect to have and manually creating them each time is annoying.

So the solution obviously is a script. It’s actually used all the time as I might create new dependencies or find new tools I need so I’d want that installed on all machines. So it runs in my ~/.zlogin file and by using a local stamp file (unlike for third-pary sources ) it reminds me periodically to run update now.

It first updates itself, it uses mr to update all my vim repositories, and then mostly uses native packaging tools to install software.

Switching to version control for home dirs was a lot of the battle, but getting the tools and things I need on the machines has been the next one. As it stands I can now pretty quickly get up to speed on a new machine in just a few minutes. It would be nice to have something where I could go, curl https://phrye.com/HOME | bash instead of having to remember the initial vcsh commands but that’s a further project.

  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
#!/bin/bash

set -e

stamp_dir=~/.config/update
stamp_file=~/.config/update/.last-update.stamp

if [[ ! -d $stamp_dir ]]; then
  mkdir -p $stamp_dir
fi

do_update() {
  # Update homedir.
  if [[ "$1" != "vcsh-update-done" ]]; then
    vcsh pull
    exec "$0" now vcsh-update-done
  fi
  # Update vim modules.
  cd ~/.vim
  mr update

  # Update system.
  kernel=$(uname -v)
  echo "Running specific steps for '$kernel'."
  case "$kernel" in
    FreeBSD*)
      makewhatis ~/.config/home-man
      if [ ! -f /usr/local/go/bin/go ]; then
        sudo pkg install go
      fi
      for pkg in python3 py36-sqlite3 gsed bind-tools go awscli; do
        if ! pkg list $pkg > /dev/null; then
          pkg install $pkg
        fi
      done
      ;;
    *Ubuntu*)
      if [[ ! -d /var/lib/slack ]]; then
        if [[ -d /etc/sudoers.d && ! -f "/etc/sudoers.d/$USER" ]]; then
          echo "$USER ALL=(ALL) NOPASSWD: ALL" \
            | sudo -p "Please enter password to configure sudo: " \
                  tee "/etc/sudoers.d/$USER" > /dev/null
        fi

        sudo -p "Please enter password to install packages: " \
             apt install vim-nox unzip wget tree gpg pwgen fortune \
                         perl-doc bc shellcheck yamllint clang-tidy \
                         cppcheck flawfinder gitlint lua-check \
                         python3-flake8 pylint3 screen python3-defusedxml

        go_tar=go1.10.3.linux-amd64.tar.gz
        go_tar_dir=$HOME/.config/update
        if [[ ! -d "$go_tar_dir" ]]; then
          mkdir "$go_tar_dir"
        fi
        if [[ ! -f "$go_tar_dir/$go_tar" ]]; then
          wget --quiet -O "$go_tar_dir/$go_tar" -c https://dl.google.com/go/$go_tar
          if [[ -d /usr/local/go ]]; then
            sudo -p "Please enter password to remove old go installation: " \
                 rm -rf /usr/local/go
          fi
        fi

        if [[ ! -d /usr/local/go ]]; then
          echo "Installing new go version."
          sudo tar -C /usr/local -xzf "$go_tar_dir/$go_tar"
          export PATH=${PATH}:/usr/local/go
          export GOPATH=$HOME/src/go
          export KEVIN_GOWARN=1
        fi
      fi
      ;;
  esac

  # If homebrew exists; update it too.
  if [[ -f /Users/kevin/Homebrew/bin/brew ]]; then
    brew update
    brew upgrade
  fi

  if [[ "$KEVIN_GOWARN" == 1 ]]; then
    cat << EOF
Run the following in each currently open shell (or just log out and log
in again):

export PATH=$PATH
export GOPATH=$GOPATH
EOF
  fi

  touch $stamp_file
}

do_check() {
  update_required=false
  if [[ ! -f $stamp_file ]]; then
    update_required=true
  else
    if [[ $0 -nt $stamp_file ]]; then
      update_required=true
    fi
    if find $stamp_file -mtime +21 | grep -q stamp; then
      update_required=true
    fi
  fi
  if $update_required; then
    echo "INFO: Need to run 'update now' to update home dir."
  fi
}

do_usage() {
  echo "$0 [now|check]"
}

case "$1" in
  up|update|now)
    do_update "$2"
    ;;
  check|"")
    do_check
    ;;
  *)
    do_usage
    ;;
esac