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

refactor: deprecate setup-fastly.sh and update deployment instructions #136

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 3 additions & 132 deletions shopware/fastly-meta/6.6/bin/setup-fastly.sh
Original file line number Diff line number Diff line change
@@ -1,136 +1,7 @@
#!/usr/bin/env bash

# fail on error
set -e
echo "This script is deprecated and will be removed in the next major release. Please use vendor/bin/shopware-deployment-helper fastly:snippet:deploy directly."

if [[ -z "$FASTLY_API_TOKEN" ]]; then
echo "Environment variable FASTLY_API_TOKEN is not set. Skipping"
exit 0
fi
sleep 5

if [[ -z "$FASTLY_SERVICE_ID" ]]; then
echo "Environment variable FASTLY_SERVICE_ID is not set. Skipping"
exit 0
fi

CWD="$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
PROJECT_ROOT="${PROJECT_ROOT:-"$(dirname "$CWD")"}"

cd "$PROJECT_ROOT"

created_version=0

create_version_if_not_done() {
if [[ "$created_version" == "1" ]]; then
return
fi

echo "Creating version from active version"
fastly service-version clone --version=active
created_version=1
}

get_md5()
{
if builtin command -v md5 > /dev/null; then
echo "$1" | md5
elif builtin command -v md5sum > /dev/null ; then
echo "$1" | md5sum | awk '{print $1}'
else
echo "Neither md5 nor md5sum were found in the PATH"
return 1
fi

return 0
}

install_fastly_cli() {
if [[ -f "/tmp/fastly/fastly" ]]; then
export PATH="/tmp/fastly:$PATH"
return
fi

mkdir -p /tmp/fastly

arch=$(uname -m)
os="linux"
version="v10.6.4"

if [[ "$arch" == "x86_64" ]]; then
arch="amd64"
fi

if [[ "$OSTYPE" == "darwin"* ]]; then
os="darwin"
fi

echo "Detected OS: ${os} and architecture: ${arch}"

file="https://github.com/fastly/cli/releases/download/${version}/fastly_${version}_${os}-${arch}.tar.gz"

echo "Downloading ${file}"

curl -L "${file}" | tar xz -C /tmp/fastly/
export PATH="/tmp/fastly:$PATH"
}

install_fastly_cli

# Fastly tries to write into /app on platformsh and this throws an error
export HOME=/tmp

for sub in ./config/fastly/*; do
if ! find "$sub" -name '*.vcl' | grep . >/dev/null; then
# No VCL files in "$sub"; moving on to the next dir
continue
fi
for vcl in "$sub"/*.vcl; do
trigger=$(basename "$vcl" .vcl)
priority=$(echo "$trigger" | awk -F '.' '$2 ~ /^[0-9]+$/ { print $2 }')
if test -z "$priority"; then
priority="100"
else
trigger=$(basename "$trigger" ".$priority")
fi
vcl_type=$(basename "$sub")

# For backward compatibility, default VCLs doesn't include trigger in their name
if [[ "$trigger" == "default" ]]; then
name="shopware_${vcl_type}"
else
name="shopware_${vcl_type}_${trigger}"
fi

echo "Found VCL snippet $trigger of type $vcl_type with priority $priority"

if fastly vcl snippet describe --version=active "--name=$name" > /dev/null; then
# The snippet exists on remote
localContent=$(cat "$vcl")
localContentMd5=$(get_md5 "$localContent")

remoteContent=$(fastly vcl snippet describe --version=active "--name=$name" --json | jq -r '.Content')
remoteContentMd5=$(get_md5 "$remoteContent")

if [[ "$localContentMd5" != "$remoteContentMd5" ]]; then
echo "Snippet ${name} has changed. Updating"

create_version_if_not_done

fastly vcl snippet update "--name=$name" "--content=${vcl}" "--type=${vcl_type}" "--priority=${priority}" --version=latest
else
echo "Snippet ${name} is up to date"
fi
else
create_version_if_not_done

fastly vcl snippet create "--name=$name" "--content=${vcl}" "--type=${vcl_type}" "--priority=${priority}" --version=latest
fi
done
done


if [[ "$created_version" == "1" ]]; then
echo "Activating latest version"

fastly service-version activate --version latest
fi
php vendor/bin/shopware-deployment-helper fastly:snippet:deploy
4 changes: 3 additions & 1 deletion shopware/fastly-meta/6.6/post-install.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
* Add your <comment>FASTLY_API_TOKEN</comment> and <comment>FASTLY_SERVICE_ID</comment> into <comment>.env</comment>
* Run <comment>setup-fastly.sh</comment> to deploy the Fastly vcl files
* Run <comment>vendor/bin/shopware-deployment-helper fastly:snippet:deploy</comment> to deploy the Fastly snippets
* The Fastly snippets are now deployed and you can start using the Fastly CDN
* The snippets are also updated on regular <comment>vendor/bin/shopware-deployment-helper run</comment> runs
Loading