Skip to content

Commit

Permalink
first spike of debugging support
Browse files Browse the repository at this point in the history
  • Loading branch information
jstrachan committed Jan 7, 2017
1 parent 95c5cb7 commit 3193523
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 26 deletions.
88 changes: 78 additions & 10 deletions cmd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,27 @@
package cmd

import (
"bufio"
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"

"github.com/pkg/browser"
"github.com/funktionio/funktion/pkg/k8sutil"
"github.com/funktionio/funktion/pkg/funktion"
"github.com/spf13/cobra"

"k8s.io/client-go/1.5/kubernetes"
"k8s.io/client-go/1.5/pkg/api"
"k8s.io/client-go/1.5/pkg/api/v1"
"k8s.io/client-go/1.5/pkg/apis/extensions/v1beta1"
"strings"
"path/filepath"
"github.com/funktionio/funktion/pkg/funktion"
"strconv"
)

const (
chromeDevToolsURLPrefix = "chrome-devtools:"
)

type debugCmd struct {
Expand Down Expand Up @@ -149,10 +155,26 @@ func (p *debugCmd) createPortText(kindText, name string) (string, error) {

debugPort := 0
if kind == functionKind {
// ensure debug mode is enabled on the function
if found.Data == nil {
found.Data = map[string]string{}
}
data := found.Data
debugMode := data[funktion.DebugProperty]
if strings.ToLower(debugMode) != "true" {
data[funktion.DebugProperty] = "true"
_, err = cms.Update(found)
if err != nil {
return "", fmt.Errorf("Failed to update Function %s to enable debug mode %v", name, err)
}
fmt.Printf("Enabled debug mode for Function %s\n", name)
}

// lets use the debug port on the runtime
runtime := ""
data := found.Labels
if data != nil {
runtime = data[funktion.RuntimeLabel]
labels := found.Labels
if labels != nil {
runtime = labels[funktion.RuntimeLabel]
}
if len(runtime) > 0 {
return p.createPortText(runtimeKind, runtime)
Expand Down Expand Up @@ -227,14 +249,60 @@ func (p *debugCmd) viewLog(pod *v1.Pod) error {
}

if p.chromeDevTools {
return p.openChromeDevTools(pod)
return p.openChromeDevTools(pod, binaryFile)
}
}
return nil
}

func (p *debugCmd) openChromeDevTools(pod *v1.Pod) error {
// TODO
func (p *debugCmd) openChromeDevTools(pod *v1.Pod, binaryFile string) error {
name := pod.Name

args := []string{"logs", "-f", name}
cmd := exec.Command(binaryFile, args...)

cmdReader, err := cmd.StdoutPipe()
if err != nil {
return fmt.Errorf("failed to get stdout for command %s %s: %v", filepath.Base(binaryFile), strings.Join(args, " "), err)
}

scanner := bufio.NewScanner(cmdReader)
line := 0
go func() {
for scanner.Scan() {
if line++; line > 50 {
fmt.Printf("No log line found starting with `%s` in the first %d lines. Maybe debug is not really enabled in this pod?\n", chromeDevToolsURLPrefix, line)
cmdReader.Close()
killCmd(cmd)
}
text := strings.TrimSpace(scanner.Text())
if strings.HasPrefix(text, chromeDevToolsURLPrefix) {
fmt.Printf("\nTo Debug open: %s\n\n", text)
cmdReader.Close()
killCmd(cmd)
browser.OpenURL(text)
}
}
}()

if err := cmd.Start(); err != nil {
killCmd(cmd)
return fmt.Errorf("failed to start command %s %s: %v", filepath.Base(binaryFile), strings.Join(args, " "), err)
}

err = cmd.Wait()
if err != nil {
killCmd(cmd)
return fmt.Errorf("failed to wait for command %s %s: %v", filepath.Base(binaryFile), strings.Join(args, " "), err)
}
return nil
}

func killCmd(cmd *exec.Cmd) {
if cmd != nil {
p := cmd.Process
if p != nil {
p.Kill()
}
}
}
4 changes: 2 additions & 2 deletions cmd/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/funktionio/funktion/pkg/k8sutil"
"github.com/spf13/cobra"
Expand All @@ -26,8 +28,6 @@ import (
"k8s.io/client-go/1.5/pkg/api"
"k8s.io/client-go/1.5/pkg/api/v1"
"k8s.io/client-go/1.5/pkg/apis/extensions/v1beta1"
"strings"
"path/filepath"
)

type logCmd struct {
Expand Down
27 changes: 13 additions & 14 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ import:
- package: github.com/magiconair/properties
- package: github.com/fsnotify/fsnotify
- package: github.com/kardianos/osext
- package: github.com/pkg/browser

0 comments on commit 3193523

Please sign in to comment.