Commit 5fd30104 authored by Geoff Simmons's avatar Geoff Simmons

Remove Attach() from the admin package.

admin now does not depend on any packages outside the standard Go
distribution. Clients can use vsm the get the management address
and the secret file, but they are no longer forced to import it.
parent 3e241b1f
......@@ -49,17 +49,64 @@
// write the file, and then start Varnish with the -S option pointing
// to the file. Varnish reads the file anew when CLI authentication is
// necessary, so the file's contents can be changed at runtime.
//
// Clients that are running on the same host as the Varnish instance,
// and have sufficient permissions to attach to Varnish shared memory
// and read the secret file, can use the GetMgmtAddr() and
// GetSecretPath() of the vsm package to read the parameters from Varnish
// shared memory. Initiating a client connection could look like this:
//
// import "code.uplex.de/uplex-varnish/varnishapi/pkg/admin"
// import "code.uplex.de/uplex-varnish/varnishapi/pkg/vsm"
//
// // Initialize shared memory attachment
// vsm := vsm.New()
// if vsm == nil {
// // error handling
// }
// defer vsm.Destroy()
//
// // Use "" for the default instance, or use the instance name
// if err := vsm.Attach(""); err != nil {
// // error handling
// }
//
// // Get the management address
// address, err := vsm.GetMgmtAddr()
// if err != nil {
// // error handling
// }
//
// // Get the path of the secret file
// spath, err := vsm.GetSecretPath()
// if err != nil {
// // error handling
// }
// // Get the contents of the secret file
// secret, err = ioutil.ReadAll(sfile)
// if err != nil {
// // error handling
// }
//
// // Dial connects to the management address, executes the
// // authentication protocol on successful connection, and
// // returns an Admin object.
// adm, err := Dial(address, secret, time.Duration(0))
// if err != nil {
// // error handling
// }
// defer adm.Close()
//
// // The adm object can now be used to issue CLI commands to
// // the Varnish management process using the methods in this
// // package.
package admin
import (
"code.uplex.de/uplex-varnish/varnishapi/pkg/vsm"
"crypto/sha256"
"errors"
"fmt"
"io/ioutil"
"net"
"os"
"strconv"
"strings"
"time"
......@@ -305,66 +352,6 @@ func Listen(addr string) (*Admin, error) {
}, nil
}
// Attach temporarily attaches to the shared memory of a running
// Varnish process, to discover the address at which Varnish accepts
// administrative commands, and the location of the secret file. This
// can be used when Varnish was started with neither of the -T or -S
// options. The shared memory attachment is released before Attach()
// finishes.
//
// Attach uses this information to connect to the admin port and
// execute authentication. On successful completion, the Admin client
// is connected to the CLI interface, just as when Dial() completes
// successfully.
//
// name identifies the instance of Varnish to which to connect. If
// name is empty, connect to the default instance (Varnish invoked
// without the -n option). Otherwise, connect to the instance
// identified by the value of varnishd -n (see varnishd(1)).
//
// timeout sets a timeout for the connection, as for Dial().
func Attach(name string, timeout time.Duration) (*Admin, error) {
vsm := vsm.New()
if vsm == nil {
return nil, errors.New("Cannot initialize for attachment")
}
defer vsm.Destroy()
if err := vsm.Timeout(timeout); err != nil {
return nil, err
}
if err := vsm.Attach(name); err != nil {
return nil, err
}
targ, err := vsm.Get("Arg", "-T")
if err != nil {
return nil, err
}
targ = strings.TrimSpace(targ)
targ = strings.Replace(targ, " ", ":", 1)
sarg, err := vsm.Get("Arg", "-S")
if err != nil {
return nil, err
}
sarg = strings.TrimSpace(sarg)
sfile, err := os.Open(sarg)
if err != nil {
return nil, err
}
secret, err := ioutil.ReadAll(sfile)
if err != nil {
return nil, err
}
adm, err := Dial(targ, secret, timeout)
if err != nil {
return nil, err
}
return adm, nil
}
// Accept accepts a connection from the Varnish management process,
// when the Admin instance was initialized with Listen() and Varnish
// was invoked with the -M option.
......
......@@ -61,18 +61,16 @@ func TestMain(m *testing.M) {
if err := vsm.Attach(""); err != nil {
panic(err)
}
targ, err := vsm.Get("Arg", "-T")
addr, err := vsm.GetMgmtAddr()
if err != nil {
panic(err)
}
targ = strings.TrimSpace(targ)
address = strings.Replace(targ, " ", ":", 1)
address = addr
sarg, err := vsm.Get("Arg", "-S")
sarg, err := vsm.GetSecretPath()
if err != nil {
panic(err)
}
sarg = strings.TrimSpace(sarg)
sfile, err := os.Open(sarg)
if err != nil {
......@@ -86,34 +84,6 @@ func TestMain(m *testing.M) {
os.Exit(m.Run())
}
func TestAttach(t *testing.T) {
adm, err := Attach("", time.Duration(0))
if err != nil {
t.Fatal("Attach():", err)
return
}
defer adm.Close()
if adm.Banner == "" {
t.Error("Banner is empty after Attach()")
}
if !strings.Contains(adm.Banner, "Varnish Cache CLI") {
t.Error("Banner does not contain 'Varnish Cache CLI'")
}
tadm, err := Attach("gotest", time.Duration(0))
if err != nil {
t.Fatal("Attach(gotest):", err)
return
}
defer tadm.Close()
if tadm.Banner == "" {
t.Error("Banner is empty after Attach()")
}
if !strings.Contains(adm.Banner, "Varnish Cache CLI") {
t.Error("Banner does not contain 'Varnish Cache CLI'")
}
}
func TestDial(t *testing.T) {
adm, err := Dial(address, secret, time.Duration(0))
if err != nil {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment