From ddf95d87bd67a4bbad6c6f3e55026cc368a36168 Mon Sep 17 00:00:00 2001 From: Mauricio Siu <47042324+Siumauricio@users.noreply.github.com> Date: Sat, 7 Dec 2024 21:20:31 -0600 Subject: [PATCH 1/3] refactor: add multiple OS --- packages/server/src/setup/server-setup.ts | 75 +++++++++++++++++++---- 1 file changed, 63 insertions(+), 12 deletions(-) diff --git a/packages/server/src/setup/server-setup.ts b/packages/server/src/setup/server-setup.ts index f2abd3c5..0026d6c0 100644 --- a/packages/server/src/setup/server-setup.ts +++ b/packages/server/src/setup/server-setup.ts @@ -74,7 +74,43 @@ const installRequirements = async (serverId: string, logPath: string) => { client .once("ready", () => { const bashCommand = ` + set -e; + # Thanks to coolify <3 + + OS_TYPE=$(grep -w "ID" /etc/os-release | cut -d "=" -f 2 | tr -d '"') + CURRENT_USER=$USER + + echo "Installing requirements for: OS: $OS_TYPE" + if [ $EUID != 0 ]; then + echo "Please run this script as root or with sudo ❌" + exit + fi + # Check if the OS is manjaro, if so, change it to arch + if [ "$OS_TYPE" = "manjaro" ] || [ "$OS_TYPE" = "manjaro-arm" ]; then + OS_TYPE="arch" + fi + + # Check if the OS is Asahi Linux, if so, change it to fedora + if [ "$OS_TYPE" = "fedora-asahi-remix" ]; then + OS_TYPE="fedora" + fi + + # Check if the OS is popOS, if so, change it to ubuntu + if [ "$OS_TYPE" = "pop" ]; then + OS_TYPE="ubuntu" + fi + + # Check if the OS is linuxmint, if so, change it to ubuntu + if [ "$OS_TYPE" = "linuxmint" ]; then + OS_TYPE="ubuntu" + fi + + #Check if the OS is zorin, if so, change it to ubuntu + if [ "$OS_TYPE" = "zorin" ]; then + OS_TYPE="ubuntu" + fi + ${validatePorts()} command_exists() { @@ -83,16 +119,17 @@ const installRequirements = async (serverId: string, logPath: string) => { ${installRClone()} ${installDocker()} ${setupSwarm()} - ${setupNetwork()} - ${setupMainDirectory()} - ${setupDirectories()} - ${createTraefikConfig()} - ${createDefaultMiddlewares()} - ${createTraefikInstance()} - ${installNixpacks()} - ${installBuildpacks()} + `; + // ${setupNetwork()} + // ${setupMainDirectory()} + // ${setupDirectories()} + // ${createTraefikConfig()} + // ${createDefaultMiddlewares()} + // ${createTraefikInstance()} + // ${installNixpacks()} + // ${installBuildpacks()} client.exec(bashCommand, (err, stream) => { if (err) { writeStream.write(err); @@ -204,8 +241,12 @@ const setupNetwork = () => ` echo "Network dokploy-network already exists ✅" else # Create the dokploy-network if it doesn't exist - docker network create --driver overlay --attachable dokploy-network - echo "Network created ✅" + if docker network create --driver overlay --attachable dokploy-network; then + echo "Network created ✅" + else + echo "Failed to create dokploy-network ❌" >&2 + exit 1 + fi fi `; @@ -214,7 +255,12 @@ const installDocker = () => ` echo "Docker already installed ✅" else echo "Installing Docker ✅" - curl -sSL https://get.docker.com | sh -s -- --version 27.2.0 + if curl -sSL https://get.docker.com | sh -s -- --version 27.2.0; then + echo "Docker installed successfully ✅" + else + echo "Failed to install Docker ❌" >&2 + exit 1 + fi fi `; @@ -260,7 +306,12 @@ const createDefaultMiddlewares = () => { }; export const installRClone = () => ` -curl https://rclone.org/install.sh | sudo bash + if command_exists rclone; then + echo "RClone already installed ✅" + else + curl https://rclone.org/install.sh | sudo bash + echo "RClone installed successfully ✅" + fi `; export const createTraefikInstance = () => { From e03aef8e3713938bfc4051a643ab8ac0f2a7c9d8 Mon Sep 17 00:00:00 2001 From: Mauricio Siu <47042324+Siumauricio@users.noreply.github.com> Date: Sun, 8 Dec 2024 17:09:28 -0600 Subject: [PATCH 2/3] refactor: improve script to support more OS --- packages/server/src/setup/server-setup.ts | 231 +++++++++++++++++++--- 1 file changed, 204 insertions(+), 27 deletions(-) diff --git a/packages/server/src/setup/server-setup.ts b/packages/server/src/setup/server-setup.ts index 0026d6c0..73adec43 100644 --- a/packages/server/src/setup/server-setup.ts +++ b/packages/server/src/setup/server-setup.ts @@ -77,6 +77,7 @@ const installRequirements = async (serverId: string, logPath: string) => { set -e; # Thanks to coolify <3 + DOCKER_VERSION=27.0.3 OS_TYPE=$(grep -w "ID" /etc/os-release | cut -d "=" -f 2 | tr -d '"') CURRENT_USER=$USER @@ -111,25 +112,66 @@ const installRequirements = async (serverId: string, logPath: string) => { OS_TYPE="ubuntu" fi + if [ "$OS_TYPE" = "arch" ] || [ "$OS_TYPE" = "archarm" ]; then + OS_VERSION="rolling" + else + OS_VERSION=$(grep -w "VERSION_ID" /etc/os-release | cut -d "=" -f 2 | tr -d '"') + fi + + case "$OS_TYPE" in + arch | ubuntu | debian | raspbian | centos | fedora | rhel | ol | rocky | sles | opensuse-leap | opensuse-tumbleweed | almalinux | amzn | alpine) ;; + *) + echo "This script only supports Debian, Redhat, Arch Linux, Alpine Linux, or SLES based operating systems for now." + exit + ;; + esac + + echo -e "---------------------------------------------" + echo "| Operating System | $OS_TYPE $OS_VERSION" + echo "| Docker | $DOCKER_VERSION" + echo -e "---------------------------------------------\n" + echo -e "1. Installing required packages (curl, wget, git, jq, openssl). " + + ${installUtilities()} + + echo -e "2. Validating ports. " ${validatePorts()} command_exists() { command -v "$@" > /dev/null 2>&1 } - ${installRClone()} - ${installDocker()} - ${setupSwarm()} - - `; - // ${setupNetwork()} - // ${setupMainDirectory()} - // ${setupDirectories()} - // ${createTraefikConfig()} - // ${createDefaultMiddlewares()} - // ${createTraefikInstance()} - // ${installNixpacks()} - // ${installBuildpacks()} + echo -e "3. Installing RClone. " + ${installRClone()} + + echo -e "4. Installing Docker. " + ${installDocker()} + + echo -e "5. Setting up Docker Swarm" + ${setupSwarm()} + + echo -e "6. Setting up Network" + ${setupNetwork()} + + echo -e "7. Setting up Directories" + ${setupMainDirectory()} + ${setupDirectories()} + + echo -e "8. Setting up Traefik" + ${createTraefikConfig()} + + echo -e "9. Setting up Middlewares" + ${createDefaultMiddlewares()} + + echo -e "10. Setting up Traefik Instance" + ${createTraefikInstance()} + + echo -e "11. Installing Nixpacks" + ${installNixpacks()} + + echo -e "12. Installing Buildpacks" + ${installBuildpacks()} + `; client.exec(bashCommand, (err, stream) => { if (err) { writeStream.write(err); @@ -250,20 +292,6 @@ const setupNetwork = () => ` fi `; -const installDocker = () => ` - if command_exists docker; then - echo "Docker already installed ✅" - else - echo "Installing Docker ✅" - if curl -sSL https://get.docker.com | sh -s -- --version 27.2.0; then - echo "Docker installed successfully ✅" - else - echo "Failed to install Docker ❌" >&2 - exit 1 - fi - fi -`; - const validatePorts = () => ` # check if something is running on port 80 if ss -tulnp | grep ':80 ' >/dev/null; then @@ -276,6 +304,155 @@ const validatePorts = () => ` fi `; +const installUtilities = () => ` + + case "$OS_TYPE" in + arch) + pacman -Sy --noconfirm --needed curl wget git jq openssl >/dev/null || true + ;; + alpine) + sed -i '/^#.*\/community/s/^#//' /etc/apk/repositories + apk update >/dev/null + apk add curl wget git jq openssl >/dev/null + ;; + ubuntu | debian | raspbian) + apt-get update -y >/dev/null + apt-get install -y curl wget git jq openssl >/dev/null + ;; + centos | fedora | rhel | ol | rocky | almalinux | amzn) + if [ "$OS_TYPE" = "amzn" ]; then + dnf install -y wget git jq openssl >/dev/null + else + if ! command -v dnf >/dev/null; then + yum install -y dnf >/dev/null + fi + if ! command -v curl >/dev/null; then + dnf install -y curl >/dev/null + fi + dnf install -y wget git jq openssl unzip >/dev/null + fi + ;; + sles | opensuse-leap | opensuse-tumbleweed) + zypper refresh >/dev/null + zypper install -y curl wget git jq openssl >/dev/null + ;; + *) + echo "This script only supports Debian, Redhat, Arch Linux, or SLES based operating systems for now." + exit + ;; + esac +`; + +const installDocker = () => ` + +# Detect if docker is installed via snap +if [ -x "$(command -v snap)" ]; then + SNAP_DOCKER_INSTALLED=$(snap list docker >/dev/null 2>&1 && echo "true" || echo "false") + if [ "$SNAP_DOCKER_INSTALLED" = "true" ]; then + echo " - Docker is installed via snap." + echo " Please note that Dokploy does not support Docker installed via snap." + echo " Please remove Docker with snap (snap remove docker) and reexecute this script." + exit 1 + fi +fi + +echo -e "3. Check Docker Installation. " +if ! [ -x "$(command -v docker)" ]; then + echo " - Docker is not installed. Installing Docker. It may take a while." + case "$OS_TYPE" in + "almalinux") + dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo >/dev/null 2>&1 + dnf install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin >/dev/null 2>&1 + if ! [ -x "$(command -v docker)" ]; then + echo " - Docker could not be installed automatically. Please visit https://docs.docker.com/engine/install/ and install Docker manually to continue." + exit 1 + fi + systemctl start docker >/dev/null 2>&1 + systemctl enable docker >/dev/null 2>&1 + ;; + "alpine") + apk add docker docker-cli-compose >/dev/null 2>&1 + rc-update add docker default >/dev/null 2>&1 + service docker start >/dev/null 2>&1 + if ! [ -x "$(command -v docker)" ]; then + echo " - Failed to install Docker with apk. Try to install it manually." + echo " Please visit https://wiki.alpinelinux.org/wiki/Docker for more information." + exit 1 + fi + ;; + "arch") + pacman -Sy docker docker-compose --noconfirm >/dev/null 2>&1 + systemctl enable docker.service >/dev/null 2>&1 + if ! [ -x "$(command -v docker)" ]; then + echo " - Failed to install Docker with pacman. Try to install it manually." + echo " Please visit https://wiki.archlinux.org/title/docker for more information." + exit 1 + fi + ;; + "amzn") + dnf install docker -y >/dev/null 2>&1 + DOCKER_CONFIG=/usr/local/lib/docker + mkdir -p $DOCKER_CONFIG/cli-plugins >/dev/null 2>&1 + curl -sL https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o $DOCKER_CONFIG/cli-plugins/docker-compose >/dev/null 2>&1 + chmod +x $DOCKER_CONFIG/cli-plugins/docker-compose >/dev/null 2>&1 + systemctl start docker >/dev/null 2>&1 + systemctl enable docker >/dev/null 2>&1 + if ! [ -x "$(command -v docker)" ]; then + echo " - Failed to install Docker with dnf. Try to install it manually." + echo " Please visit https://www.cyberciti.biz/faq/how-to-install-docker-on-amazon-linux-2/ for more information." + exit 1 + fi + ;; + "fedora") + if [ -x "$(command -v dnf5)" ]; then + # dnf5 is available + dnf config-manager addrepo --from-repofile=https://download.docker.com/linux/fedora/docker-ce.repo --overwrite >/dev/null 2>&1 + else + # dnf5 is not available, use dnf + dnf config-manager --add-repo=https://download.docker.com/linux/fedora/docker-ce.repo >/dev/null 2>&1 + fi + dnf install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin >/dev/null 2>&1 + if ! [ -x "$(command -v docker)" ]; then + echo " - Docker could not be installed automatically. Please visit https://docs.docker.com/engine/install/ and install Docker manually to continue." + exit 1 + fi + systemctl start docker >/dev/null 2>&1 + systemctl enable docker >/dev/null 2>&1 + ;; + *) + if [ "$OS_TYPE" = "ubuntu" ] && [ "$OS_VERSION" = "24.10" ]; then + echo "Docker automated installation is not supported on Ubuntu 24.10 (non-LTS release)." + echo "Please install Docker manually." + exit 1 + fi + curl -s https://releases.rancher.com/install-docker/$DOCKER_VERSION.sh | sh 2>&1 + if ! [ -x "$(command -v docker)" ]; then + curl -s https://get.docker.com | sh -s -- --version $DOCKER_VERSION 2>&1 + if ! [ -x "$(command -v docker)" ]; then + echo " - Docker installation failed." + echo " Maybe your OS is not supported?" + echo " - Please visit https://docs.docker.com/engine/install/ and install Docker manually to continue." + exit 1 + fi + fi + if [ "$OS_TYPE" = "rocky" ]; then + systemctl start docker >/dev/null 2>&1 + systemctl enable docker >/dev/null 2>&1 + fi + + if [ "$OS_TYPE" = "centos" ]; then + systemctl start docker >/dev/null 2>&1 + systemctl enable docker >/dev/null 2>&1 + fi + + + esac + echo " - Docker installed successfully." +else + echo " - Docker is installed." +fi +`; + const createTraefikConfig = () => { const config = getDefaultServerTraefikConfig(); From ce0e9ccddc9e42481e0e170078608b60a8bac0de Mon Sep 17 00:00:00 2001 From: Mauricio Siu <47042324+Siumauricio@users.noreply.github.com> Date: Sun, 8 Dec 2024 17:30:32 -0600 Subject: [PATCH 3/3] refactor: add preview deployments for cloud version --- apps/api/src/schema.ts | 10 ++++++++++ apps/api/src/utils.ts | 20 +++++++++++++++++++ .../server/queues/deployments-queue.ts | 2 -- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/apps/api/src/schema.ts b/apps/api/src/schema.ts index 5f26e018..609289bf 100644 --- a/apps/api/src/schema.ts +++ b/apps/api/src/schema.ts @@ -19,6 +19,16 @@ export const deployJobSchema = z.discriminatedUnion("applicationType", [ applicationType: z.literal("compose"), serverId: z.string().min(1), }), + z.object({ + applicationId: z.string(), + previewDeploymentId: z.string(), + titleLog: z.string(), + descriptionLog: z.string(), + server: z.boolean().optional(), + type: z.enum(["deploy"]), + applicationType: z.literal("application-preview"), + serverId: z.string().min(1), + }), ]); export type DeployJob = z.infer; diff --git a/apps/api/src/utils.ts b/apps/api/src/utils.ts index 2654487f..d919f29e 100644 --- a/apps/api/src/utils.ts +++ b/apps/api/src/utils.ts @@ -1,10 +1,12 @@ import { deployRemoteApplication, deployRemoteCompose, + deployRemotePreviewApplication, rebuildRemoteApplication, rebuildRemoteCompose, updateApplicationStatus, updateCompose, + updatePreviewDeployment, } from "@dokploy/server"; import type { DeployJob } from "./schema"; @@ -47,6 +49,20 @@ export const deploy = async (job: DeployJob) => { }); } } + } else if (job.applicationType === "application-preview") { + await updatePreviewDeployment(job.previewDeploymentId, { + previewStatus: "running", + }); + if (job.server) { + if (job.type === "deploy") { + await deployRemotePreviewApplication({ + applicationId: job.applicationId, + titleLog: job.titleLog, + descriptionLog: job.descriptionLog, + previewDeploymentId: job.previewDeploymentId, + }); + } + } } } catch (error) { if (job.applicationType === "application") { @@ -55,6 +71,10 @@ export const deploy = async (job: DeployJob) => { await updateCompose(job.composeId, { composeStatus: "error", }); + } else if (job.applicationType === "application-preview") { + await updatePreviewDeployment(job.previewDeploymentId, { + previewStatus: "error", + }); } } diff --git a/apps/dokploy/server/queues/deployments-queue.ts b/apps/dokploy/server/queues/deployments-queue.ts index 9ff8a157..b8dfb8cd 100644 --- a/apps/dokploy/server/queues/deployments-queue.ts +++ b/apps/dokploy/server/queues/deployments-queue.ts @@ -21,8 +21,6 @@ export const deploymentWorker = new Worker( "deployments", async (job: Job) => { try { - console.log(job.data); - if (job.data.applicationType === "application") { await updateApplicationStatus(job.data.applicationId, "running");