Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trellis update trellis #135

Open
pixeline opened this issue Sep 21, 2020 · 2 comments
Open

trellis update trellis #135

pixeline opened this issue Sep 21, 2020 · 2 comments

Comments

@pixeline
Copy link

pixeline commented Sep 21, 2020

Summary

Enable a command like: trellis update trellis that would make it easier to update Trellis to the next version

Motivation

These instructions are great, but could they be automated as much as possible ?
https://discourse.roots.io/t/best-practices-to-update-trellis/5386/32

@swalkinshaw
Copy link
Member

Completely agreed. This would be a great feature for trellis-cli 👍

I'm familiar with the Rails upgrade task and it could work like that: https://guides.rubyonrails.org/upgrading_ruby_on_rails.html#the-update-task

$ rails app:update
   identical  config/boot.rb
       exist  config
    conflict  config/routes.rb
Overwrite /myapp/config/routes.rb? (enter "h" for help) [Ynaqdh]
       force  config/routes.rb
    conflict  config/application.rb
Overwrite /myapp/config/application.rb? (enter "h" for help) [Ynaqdh]
       force  config/application.rb
    conflict  config/environment.rb
...

@fkohrt
Copy link

fkohrt commented May 24, 2022

Related: #883

Also, for people that need something right now, I'll share my current approach. Given a directory structure as created with trellis new example.org, I'm using a bash script update-trellis.sh along with two files TRELLIS_COMMON_VERSION and TRELLIS_CUSTOM_VERSION to integrate changes at github.com/roots/trellis into the trellis/ directory:

    example.org
    ├── site/
    │   ├── config/
    │   ├── web/
    │   └── ...
    ├── trellis/
    │   ├── deploy-hooks/
    │   ├── group_vars/
    │   └── ...
    │── .git/
    │── update-trellis.sh
    │── TRELLIS_COMMON_VERSION
    └── TRELLIS_CUSTOM_VERSION

While TRELLIS_COMMON_VERSION tracks the version of files that are likely to be the same across Trellis instances, TRELLIS_CUSTOM_VERSION tracks the version of files that are likely to be customized as part of a Trellis instance. This allows for a quick update of common files and a separate, more cautious update of customized files:

# Only once: create version files
#   Identify the version of your "trellis/" directory,
#   e.g. look into "trellis/CHANGELOG.md"
#   or whatever you passed to `trellis new` via `--trellis-version`.
#   Then write the corresponding version tag
#   from the roots/trellis GitHub repo to the version files.
echo "COMMON=v1.14.0" > TRELLIS_COMMON_VERSION
echo "CUSTOM=v1.14.0" > TRELLIS_CUSTOM_VERSION

# If a new version is released in the roots/trellis GitHub repo
# issue the update command for common files.
# Note that you can also choose a specific commit, e.g. `f1f8ff1`.
# If you want the latest commit don't use `master`,
# instead look into the roots/trellis GitHub repo which commit
# is the latest and use that.
./update-trellis.sh common v1.15.0

# For the customized files, first have a look what files changed.
./update-trellis.sh custom v1.15.0 --stat

# If none of the files that you customized are affected
# (you might know because you just set it up)
# give it a go.
./update-trellis.sh custom v1.15.0
# But if files that you customized are affected,
# resort to a manual copy & paste
# (note that you can still try to update with the
# script, it might just not work). Afterwards,
# update your version tracker file.
echo "CUSTOM=v1.15.0" > TRELLIS_CUSTOM_VERSION

# You can now make the changes known to git.
git add .
git commit -m "Bump trellis to v1.15.0"
git pull
git push

Feedback welcome!

update-trellis.sh

Warning: Always do a backup of your files before using this code.

#!/bin/bash

# SPDX-FileCopyrightText: 2024 Florian Kohrt
#
# SPDX-License-Identifier: CC0-1.0

# Update Script for Trellis
#   ./update-trellis.sh common v1.14.0
#   ./update-trellis.sh custom 74b4f5e --stat
#
# List of custom files from https://discourse.roots.io/t/lets-encrypt-certificate-expired/7171/23

source TRELLIS_COMMON_VERSION
source TRELLIS_CUSTOM_VERSION
if [ -z "$COMMON" ] || \
   [ -z "$CUSTOM" ] || \
   [ -z "$1" ] || \
   [ -z "$2" ]
then
  exit 1
fi

if [ "$1" = "custom" ]
then
  curl -fsSL -o- https://github.com/roots/trellis/compare/"$CUSTOM".."$2".diff \
    | git apply --verbose -p1 --directory='trellis' \
                --include='trellis/ansible.cfg' \
                --include='trellis/group_vars/*' \
                --include='trellis/deploy-hooks/*' \
                --include='trellis/hosts/*' \
                --include='trellis/nginx-includes/*' \
                $3 -
  RESULT=$?
  if [ $RESULT -eq 0 ] && \
     [ "$3" != "--stat" ] && \
     [ "$3" != "--numstat" ] && \
     [ "$3" != "--summary" ] && \
     [ "$3" != "--check" ]
  then
    echo 'CUSTOM='"$2" > TRELLIS_CUSTOM_VERSION
  fi
else
  curl -fsSL -o- https://github.com/roots/trellis/compare/"$COMMON".."$2".diff \
    | git apply --verbose -p1 --directory='trellis' \
                --exclude='trellis/ansible.cfg' \
                --exclude='trellis/group_vars/*' \
                --exclude='trellis/deploy-hooks/*' \
                --exclude='trellis/hosts/*' \
                --exclude='trellis/nginx-includes/*' \
                --exclude='trellis/.vault_pass' \
                --exclude='trellis/.github/*' \
                $3 -
  RESULT=$?
  if [ $RESULT -eq 0 ] && \
     [ "$3" != "--stat" ] && \
     [ "$3" != "--numstat" ] && \
     [ "$3" != "--summary" ] && \
     [ "$3" != "--check" ]
  then
    echo 'COMMON='"$2" > TRELLIS_COMMON_VERSION
  fi
fi

exit $RESULT
Implementation notes

I also tried it with patch instead of git apply, but there were issues with some git diff markup unknown to filterdiff (see "Using filterdiff to exclude a diff").

#!/bin/bash

# SPDX-FileCopyrightText: 2022 Florian Kohrt
#
# SPDX-License-Identifier: CC0-1.0

# Update Script for Trellis
#   ./update-trellis.sh common v1.14.0
#   ./update-trellis.sh custom 74b4f5e --dry-run
#
# List of custom files from https://discourse.roots.io/t/lets-encrypt-certificate-expired/7171/23

source TRELLIS_COMMON_VERSION
source TRELLIS_CUSTOM_VERSION
if [ -z "$COMMON" ] || \
   [ -z "$CUSTOM" ] || \
   [ -z "$1" ] || \
   [ -z "$2" ]
then
  exit 1
fi

if [ "$1" = "custom" ]
then
  curl -fsSL -o- https://github.com/roots/trellis/compare/"$CUSTOM".."$2".diff \
    | filterdiff --strip-match=1 \
                 --include='ansible.cfg' \
                 --include='group_vars/*' \
                 --include='deploy-hooks/*' \
                 --include='hosts/*' \
                 --include='nginx-includes/*' \
    | patch --forward --reject-file=- --no-backup-if-mismatch --strip=1 --directory='trellis' $3 \
    && if [ "$3" != "--dry-run" ]; then echo 'CUSTOM='"$2" > TRELLIS_CUSTOM_VERSION; RESULT=0; fi || RESULT=1
else
  curl -fsSL -o- https://github.com/roots/trellis/compare/"$COMMON".."$2".diff \
    | filterdiff --strip-match=1 \
                 --exclude='ansible.cfg' \
                 --exclude='group_vars/*' \
                 --exclude='deploy-hooks/*' \
                 --exclude='hosts/*' \
                 --exclude='nginx-includes/*' \
                 --exclude='.vault_pass' \
                 --exclude='.github/*' \
    | patch --forward --reject-file=- --no-backup-if-mismatch --strip=1 --directory='trellis' $3 \
    && if [ "$3" != "--dry-run" ]; then echo 'COMMON='"$2" > TRELLIS_COMMON_VERSION; RESULT=0; fi || RESULT=1
fi

exit $RESULT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants