github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/worker/introspection/script.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package introspection
     5  
     6  import (
     7  	"io/ioutil"
     8  	"path"
     9  	"runtime"
    10  
    11  	"github.com/juju/errors"
    12  )
    13  
    14  var (
    15  	profileDir        = "/etc/profile.d"
    16  	bashFuncsFilename = "juju-introspection.sh"
    17  )
    18  
    19  // WriteProfileFunctions writes the bashFuncs below to a file in the
    20  // /etc/profile.d directory so all bash terminals can easily access the
    21  // introspection worker.
    22  func WriteProfileFunctions() error {
    23  	if runtime.GOOS != "linux" {
    24  		logger.Debugf("skipping profile funcs install")
    25  		return nil
    26  	}
    27  	filename := profileFilename()
    28  	if err := ioutil.WriteFile(filename, []byte(bashFuncs), 0644); err != nil {
    29  		return errors.Annotate(err, "writing introspection bash funcs")
    30  	}
    31  	return nil
    32  }
    33  
    34  func profileFilename() string {
    35  	return path.Join(profileDir, bashFuncsFilename)
    36  }
    37  
    38  const bashFuncs = `
    39  jujuAgentCall () {
    40    local agent=$1
    41    shift
    42    local path=
    43    for i in "$@"; do
    44      path="$path/$i"
    45    done
    46    echo -e "GET $path HTTP/1.0\r\n" | socat abstract-connect:jujud-$agent STDIO
    47  }
    48  
    49  jujuMachineAgentName () {
    50    local machine=` + "`ls -d /var/lib/juju/agents/machine*`" + `
    51    machine=` + "`basename $machine`" + `
    52    echo $machine
    53  }
    54  
    55  jujuMachineOrUnit () {
    56    # First arg is the path, second is optional agent name.
    57    if [ "$#" -gt 2 ]; then
    58      echo "expected no args (for machine agent) or one (unit agent)"
    59      return 1
    60    fi
    61    local agent=$(jujuMachineAgentName)
    62    if [ "$#" -eq 2 ]; then
    63      agent=$2
    64    fi
    65    jujuAgentCall $agent $1
    66  }
    67  
    68  juju-goroutines () {
    69    jujuMachineOrUnit debug/pprof/goroutine?debug=1 $@
    70  }
    71  
    72  juju-heap-profile () {
    73    jujuMachineOrUnit debug/pprof/heap?debug=1 $@
    74  }
    75  
    76  juju-engine-report () {
    77    jujuMachineOrUnit depengine/ $@
    78  }
    79  
    80  export -f jujuAgentCall
    81  export -f jujuMachineAgentName
    82  export -f jujuMachineOrUnit
    83  export -f juju-goroutines
    84  export -f juju-heap-profile
    85  export -f juju-engine-report
    86  `