dify官方离线安装打包插件脚本,改造成ubuntu或者debain系统可用
背景
使用docker纯内网部署的dify1.0+版本,必须进行离线安装插件。官方只提供了centos版本的,因此改造一下官方脚本,变成ubuntu或者debain系统可用。具体脚本如下:
#!/bin/bash
# author: Junjie.MGITHUB_API_URL=https://github.com
MARKETPLACE_API_URL=https://marketplace.dify.ai
PIP_MIRROR_URL=https://mirrors.aliyun.com/pypi/simpleCURR_DIR=$(dirname "$0")
cd "$CURR_DIR" || exit
CURR_DIR=$(pwd)
USER=$(whoami)market(){if [[ -z "$2" || -z "$3" || -z "$4" ]]; thenecho ""echo "Usage: $0 market [plugin author] [plugin name] [plugin version]"echo "Example:"echo " $0 market junjiem mcp_sse 0.0.1"echo " $0 market langgenius agent 0.0.9"echo ""exit 1fiecho "From the Dify Marketplace downloading ..."PLUGIN_AUTHOR=$2PLUGIN_NAME=$3PLUGIN_VERSION=$4PLUGIN_PACKAGE_PATH=${CURR_DIR}/${PLUGIN_AUTHOR}-${PLUGIN_NAME}_${PLUGIN_VERSION}.difypkgPLUGIN_DOWNLOAD_URL=${MARKETPLACE_API_URL}/api/v1/plugins/${PLUGIN_AUTHOR}/${PLUGIN_NAME}/${PLUGIN_VERSION}/downloadecho "Downloading ${PLUGIN_DOWNLOAD_URL} ..."curl -L -o "${PLUGIN_PACKAGE_PATH}" "${PLUGIN_DOWNLOAD_URL}"if [[ $? -ne 0 ]]; thenecho "Download failed, please check the plugin author, name and version."exit 1fiecho "Download success."repackage "${PLUGIN_PACKAGE_PATH}"
}github(){if [[ -z "$2" || -z "$3" || -z "$4" ]]; thenecho ""echo "Usage: $0 github [Github repo] [Release title] [Assets name (include .difypkg suffix)]"echo "Example:"echo " $0 github junjiem/dify-plugin-tools-dbquery v0.0.2 db_query.difypkg"echo " $0 github https://github.com/junjiem/dify-plugin-agent-mcp_sse 0.0.1 agent-mcp_see.difypkg"echo ""exit 1fiecho "From the Github downloading ..."GITHUB_REPO=$2if [[ "${GITHUB_REPO}" != "${GITHUB_API_URL}"* ]]; thenGITHUB_REPO="${GITHUB_API_URL}/${GITHUB_REPO}"fiRELEASE_TITLE=$3ASSETS_NAME=$4PLUGIN_NAME="${ASSETS_NAME%.difypkg}"PLUGIN_PACKAGE_PATH=${CURR_DIR}/${PLUGIN_NAME}-${RELEASE_TITLE}.difypkgPLUGIN_DOWNLOAD_URL=${GITHUB_REPO}/releases/download/${RELEASE_TITLE}/${ASSETS_NAME}echo "Downloading ${PLUGIN_DOWNLOAD_URL} ..."curl -L -o "${PLUGIN_PACKAGE_PATH}" "${PLUGIN_DOWNLOAD_URL}"if [[ $? -ne 0 ]]; thenecho "Download failed, please check the github repo, release title and assets name."exit 1fiecho "Download success."repackage "${PLUGIN_PACKAGE_PATH}"
}_local(){if [[ -z "$2" ]]; thenecho ""echo "Usage: $0 local [difypkg path]"echo "Example:"echo " $0 local ./db_query.difypkg"echo " $0 local /root/dify-plugin/db_query.difypkg"echo ""exit 1fiPLUGIN_PACKAGE_PATH=$(realpath "$2")repackage "${PLUGIN_PACKAGE_PATH}"
}repackage(){local PACKAGE_PATH=$1PACKAGE_NAME_WITH_EXTENSION=$(basename "${PACKAGE_PATH}")PACKAGE_NAME="${PACKAGE_NAME_WITH_EXTENSION%.*}"echo "Unziping ..."install_unzipunzip -o "${PACKAGE_PATH}" -d "${CURR_DIR}/${PACKAGE_NAME}"if [[ $? -ne 0 ]]; thenecho "Unzip failed."exit 1fiecho "Unzip success."echo "Repackaging ..."cd "${CURR_DIR}/${PACKAGE_NAME}" || exitpip download -r requirements.txt -d ./wheels --index-url "${PIP_MIRROR_URL}"sed -i '1i\--no-index --find-links=./wheels/' requirements.txtif [ -f .difyignore ]; thensed -i '/^wheels\//d' .difyignoreficd "${CURR_DIR}" || exitchmod 755 "${CURR_DIR}/dify-plugin-linux-amd64-5g""${CURR_DIR}/dify-plugin-linux-amd64-5g" plugin package "${CURR_DIR}/${PACKAGE_NAME}" -o "${CURR_DIR}/${PACKAGE_NAME}-offline.difypkg"echo "Repackage success."
}install_unzip(){if ! dpkg -s unzip &> /dev/null; thenecho "Installing unzip ..."sudo apt-get update && sudo apt-get install -y unzipif [ $? -ne 0 ]; thenecho "Install unzip failed."exit 1fifi
}case "$1" in'market')market "$@";;'github')github "$@";;'local')_local "$@";;*)echo "usage: $0 {market|github|local}"exit 1
esac
exit 0