Skip to content

Commit

Permalink
BUGFIX: remove broken webdav feature in Drydock
Browse files Browse the repository at this point in the history
  • Loading branch information
skurfuerst committed Sep 27, 2024
1 parent a67415c commit 2263b75
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 238 deletions.
195 changes: 13 additions & 182 deletions cmd/xdebug.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
package cmd

import (
"fmt"
"github.com/gookit/color"
"github.com/jonhadfield/findexec"
"github.com/sandstorm/drydock/util"
"github.com/spf13/cobra"
"log"
"net"
"net/http"
"os"
"os/exec"
"os/signal"
"runtime"
"strings"
"time"
)

func convertToTopLevelExport(exportedPath string) string {
return strings.ReplaceAll(exportedPath, "/", "")
}

// phpXdebugInstallScript is running in the debugImage (NOTE: we use a different debug image here to support NFS as well.
// phpXdebugInstallScript is running in the debugImage
// - we mount the inner container to /container (should be based on some base "official" Docker PHP image)
// - we php-spx via Git inside nicolaka/netshoot (because we cannot know if git is installed inside the container)
// - then, we compile and install php-spx inside the container. This runs as root, because we use the "execroot" mechanics
// (important for the `make install` step).
// - reload the config
func phpXdebugInstallScript(pid string, extraMountFolders string) string {
func phpXdebugInstallScript(pid string) string {
// fall back to xdebug 3.1.6 for PHP 7.4 if xdebug 3.2 (the newest version) did not work
return mountSlashContainer + `
cat << EOF | chroot /container
Expand All @@ -36,6 +28,12 @@ cat << EOF | chroot /container
pecl install xdebug || pecl install xdebug-3.1.6
EOF
if [ ! -d /container$PHP_INI_DIR ]; then
echo "!!!! PHP_INI_DIR not set."
echo "!!!! please set it as env in docker compose."
exit 1
fi;
cat << EOF > /container$PHP_INI_DIR/conf.d/xdebug.ini
zend_extension=xdebug.so
Expand All @@ -50,31 +48,6 @@ pkill -USR2 php-fpm
`
}

func phpXdebugMountScript(extraMountFolders string) string {
if len(extraMountFolders) > 0 {
var davExports []string
for _, extraMountFolder := range strings.Split(extraMountFolders, ",") {
extraMountFolder = strings.Trim(extraMountFolder, "/")
davExports = append(davExports, fmt.Sprintf("/%s,/container/%s,null,null,false", convertToTopLevelExport(extraMountFolder), extraMountFolder))
}

return mountSlashContainer + `
export HTTP_PROXY=""
export HTTPS_PROXY=""
echo https://github.com/117503445/GoWebDAV/releases/download/1.9.0/gowebdav_linux_` + runtime.GOARCH + `
curl -L -o /bin/gowebdav https://github.com/117503445/GoWebDAV/releases/download/1.9.0/gowebdav_linux_` + runtime.GOARCH + `
chmod +x /bin/gowebdav
echo "Starting webdav server for extra mounts on ` + extraMountFolders + `"
/bin/gowebdav --dav "` + strings.Join(davExports, ";") + `"
`
}
return ""
}

func phpXdebugDeactivateScript() string {
return mountSlashContainer + `
rm /container$PHP_INI_DIR/conf.d/xdebug.ini
Expand All @@ -84,7 +57,6 @@ pkill -USR2 php-fpm

func buildXdebugCommand() *cobra.Command {
var debugImage string = "nicolaka/netshoot"
var extraMountFolders string = ""

var command = &cobra.Command{
Use: "xdebug [flags] SERVICE-or-CONTAINER",
Expand All @@ -97,9 +69,6 @@ the PHP Process such that the debugger is enabled.
<op=underscore;>Options:</>
--debug-image What debugger docker image to use for executing nsenter (and optionally the NFS webdav server).
By default, nicolaka/netshoot is used
--mount Extra mounts which should be mounted from the container to the host via webdav.
This is useful to be able to f.e. debug into non-mounted files (like other packages
in Neos/Flow applications)
<op=underscore;>Examples</>
Expand All @@ -109,9 +78,6 @@ the PHP Process such that the debugger is enabled.
<op=bold;>Run Xdebug in a running docker-compose service</>
drydock xdebug <op=italic;>my-docker-compose-service</>
<op=bold;>Run Xdebug a Neos/Flow Application</>
drydock xdebug <op=italic;>my-docker-compose-service</> --mount=app/Data/Temporary
<op=underscore;>Background:</>
This command installs the Xdebug PHP extension into an existing Docker container, even if the container is locked
Expand All @@ -131,11 +97,6 @@ the PHP Process such that the debugger is enabled.
color.Println("<green>=====================================</>")
color.Println("<green>Installing Xdebug into the container</>")
color.Println("<green>and reloading PHP</>")
if len(extraMountFolders) > 0 {
color.Printf("<green>Extra Mounted Folders: </><fg=green;op=bold;>%s</>\n", extraMountFolders)
} else {
color.Printf("<green>Extra Mounted Folders: </><fg=green;op=bold;>-</>\n")
}
color.Println("<green>=====================================</>")
color.Println("")

Expand Down Expand Up @@ -174,18 +135,12 @@ the PHP Process such that the debugger is enabled.
os.Exit(1)
}

if len(extraMountFolders) > 0 {
// needed for WebDav Server to extraMountFolder the extra files
extraDockerRunArgs = append(extraDockerRunArgs, "-p", "19889:80")
}
// Image: https://github.com/vgist/dockerfiles/tree/master/nfs-server

// Install XDEBUG
dockerRunCommand := dockerRunNsenterCommand(fullContainerName, debugImage, pid, extraDockerRunArgs)
dockerRunCommand = append(dockerRunCommand, "--net") // to download files now, we need to mount the network filesystem.
dockerRunCommand = append(dockerRunCommand, "/bin/bash")
dockerRunCommand = append(dockerRunCommand, "-c")
dockerRunCommand = append(dockerRunCommand, phpXdebugInstallScript(pid, extraMountFolders))
dockerRunCommand = append(dockerRunCommand, phpXdebugInstallScript(pid))

dockerRunC := exec.Command(dockerExecutablePathAndFilename, dockerRunCommand[1:]...)
dockerRunC.Env = os.Environ()
Expand All @@ -196,112 +151,10 @@ the PHP Process such that the debugger is enabled.
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)

if len(extraMountFolders) > 0 {
// Install XDEBUG
dockerRunCommand := dockerRunNsenterCommand(fullContainerName, debugImage, pid, extraDockerRunArgs)
//
dockerRunCommand = append(dockerRunCommand, "/bin/bash")
dockerRunCommand = append(dockerRunCommand, "-c")
dockerRunCommand = append(dockerRunCommand, phpXdebugMountScript(extraMountFolders))

dockerRunC := exec.Command(dockerExecutablePathAndFilename, dockerRunCommand[1:]...)
dockerRunC.Env = os.Environ()
dockerRunC.Stdout = os.Stdout
dockerRunC.Stderr = os.Stderr
dockerRunC.Start()

err = waitUntilWebdavIsRunning()
color.Println("")
color.Println("")
color.Println("<green>=====================================</>")
color.Printf("<green>Trying to mount </><fg=green;op=bold;>%s</><green> via WebDAV</>\n", extraMountFolders)
color.Println("<green>=====================================</>")
color.Println("")

if err != nil {
color.Printf("<fg=red>FATAL: Webdav not up and running: %s.</>\n", err)
color.Println("")
os.Exit(1)
}
// Webdav server up and running now :) so we can mount extraMountFolders.
for _, extraMountFolder := range strings.Split(extraMountFolders, ",") {
extraMountFolder = strings.Trim(extraMountFolder, "/")
stat, err := os.Stat(extraMountFolder)
folderExists := err == nil && stat.IsDir()
if !folderExists {
os.MkdirAll(extraMountFolder, 0755)
}

mountC := exec.Command(
"mount",
"-t",
"webdav",
fmt.Sprintf("http://127.0.0.1:19889/%s", convertToTopLevelExport(extraMountFolder)),
extraMountFolder,
)
mountC.Env = os.Environ()
mountC.Stdout = os.Stdout
mountC.Stderr = os.Stderr
err = mountC.Run()
if err != nil {
color.Printf("<fg=red>FATAL: Webdav mount did not succeed: %s.</>\n", err)
color.Println("")
os.Exit(1)
}
}

printXdebugUsage(fullContainerName)

// wait for ctrl-c
<-c

color.Println("<fg=yellow>Ctrl-C pressed. Aborting...</>")

stopC := exec.Command(
dockerExecutablePathAndFilename,
"stop",
"-t",
"0",
fullContainerName+"_DEBUG",
)
stopC.Env = os.Environ()
stopC.Stdout = os.Stdout
stopC.Stderr = os.Stderr
stopC.Run()

color.Println("")
color.Println("")
color.Println("<green>=====================================</>")
color.Printf("<green>Removing </><fg=green;op=bold;>%s</><green> mounts</>\n", extraMountFolders)
color.Println("<green>=====================================</>")
color.Println("")

for _, extraMountFolder := range strings.Split(extraMountFolders, ",") {
extraMountFolder = strings.Trim(extraMountFolder, "/")
stat, err := os.Stat(extraMountFolder)
folderExists := err == nil && stat.IsDir()
if !folderExists {
os.MkdirAll(extraMountFolder, 0755)
}

umountC := exec.Command(
"diskutil",
"umount",
"force",
extraMountFolder,
)
umountC.Env = os.Environ()
umountC.Stdout = os.Stdout
umountC.Stderr = os.Stderr
umountC.Stdin = os.Stdin
umountC.Run()
}
} else {
printXdebugUsage(fullContainerName)
// wait for ctrl-c
<-c
color.Println("<fg=yellow>Ctrl-C pressed. Aborting...</>")
}
printXdebugUsage(fullContainerName)
// wait for ctrl-c
<-c
color.Println("<fg=yellow>Ctrl-C pressed. Aborting...</>")

color.Println("<green>=====================================</>")
color.Printf("<green>Disabling Xdebug</>\n")
Expand Down Expand Up @@ -329,7 +182,6 @@ the PHP Process such that the debugger is enabled.
}

command.Flags().StringVarP(&debugImage, "debug-image", "", "nicolaka/netshoot", "What debugger docker image to use for executing nsenter. By default, gists/nfs-server is used")
command.Flags().StringVarP(&extraMountFolders, "mount", "", "", "What additional folders to mount via webdav")

return command
}
Expand Down Expand Up @@ -380,27 +232,6 @@ func printXdebugUsage(fullContainerName string) {
color.Println("<fg=yellow>To stop debugging, </><fg=yellow;op=bold>press Ctrl-C</>")
}

func waitUntilWebdavIsRunning() error {
client := http.Client{
Timeout: 200 * time.Millisecond,
}

// wait 60 seconds
for i := 0; i < 300; i++ {
resp, err := client.Get("http://127.0.0.1:19889")
if err == nil {
resp.Body.Close()
if resp.StatusCode == 200 {
return nil
}
}
time.Sleep(200 * time.Millisecond)
}

return fmt.Errorf("timeout: webdav server not running after 60 seconds")

}

func isXdebugPortOpenInIde(host string, port string) bool {
timeout := time.Second
conn, err := net.DialTimeout("tcp", net.JoinHostPort(host, port), timeout)
Expand Down
Binary file added docs/_assets/SCR-20240410-qezg-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 2263b75

Please sign in to comment.