diff --git a/README.md b/README.md index 7d705a8..5dbfed0 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,9 @@ Packing any application with a tiny/bare minimal base image sounds like an aweso This repo/docker image tries to solve this problem by having a different image with all possible tools needed to debug majority of the problems in a production environment. This image also includes a very small web application for testing/debugging purposes. +### Packages +For Linux, see file: [package/Dockerfile](package/Dockerfile) + ## Running ``` diff --git a/main.go b/main.go index f4e14ad..fa7858c 100644 --- a/main.go +++ b/main.go @@ -14,11 +14,9 @@ import ( var VERSION = "v0.0.0-dev" const ( - appName = "swiss-army-knife" - portArg = "port" - useMetadataArg = "use-rancher-metadata" - metadataAddressArg = "metadata-address" - alphabetArg = "alphabet" + appName = "swiss-army-knife" + portArg = "port" + alphabetArg = "alphabet" ) func main() { @@ -26,11 +24,6 @@ func main() { app.Name = appName app.Version = VERSION app.Flags = []cli.Flag{ - cli.StringFlag{ - Name: metadataAddressArg, - Value: server.DefaultMetadataAddress, - EnvVar: "RANCHER_METADATA_ADDRESS", - }, cli.StringFlag{ Name: portArg, Value: server.DefaultServerPort, @@ -41,11 +34,6 @@ func main() { Usage: "Run the web server with the given alphabet", EnvVar: "ALPHABET", }, - cli.BoolFlag{ - Name: useMetadataArg, - Usage: "Use Rancher metadata for querying information about self", - EnvVar: "USE_RANCHER_METADATA", - }, cli.BoolFlag{ Name: "debug", Usage: "Turn on debug logging", @@ -73,9 +61,7 @@ func run(c *cli.Context) error { } s, err := server.NewServer( c.String(portArg), - c.String(metadataAddressArg), c.String(alphabetArg), - c.Bool(useMetadataArg), ) if err != nil { log.Errorf("Error creating new server") diff --git a/package/Dockerfile b/package/Dockerfile index 4c71c1c..2d27b0d 100644 --- a/package/Dockerfile +++ b/package/Dockerfile @@ -29,6 +29,7 @@ RUN apt-get update && \ netcat \ netcat-openbsd \ openssl \ + openssh-client \ psmisc \ socat \ tcpdump \ diff --git a/server/server.go b/server/server.go index b169be8..8b30c4f 100644 --- a/server/server.go +++ b/server/server.go @@ -2,26 +2,22 @@ package server import ( "fmt" + "math/rand" "net" "net/http" "os" "strconv" "strings" "text/template" + "time" "github.com/leodotcloud/log" - "github.com/rancher/go-rancher-metadata/metadata" ) const ( - metadataURLTemplate = "http://%v/2016-07-29" - // DefaultServerPort ... DefaultServerPort = "80" defaultServerPort = 80 - - // DefaultMetadataAddress specifies the default value to use if nothing is specified - DefaultMetadataAddress = "169.254.169.250" ) type alphabet struct { @@ -113,12 +109,10 @@ var homePageTmpl = ` // Server ... type Server struct { - port int - exitCh chan int - mc metadata.Client - l net.Listener - useMetadata bool - alphabets []alphabet + port int + exitCh chan int + l net.Listener + alphabets []alphabet } // ErrorResponse ... @@ -131,29 +125,13 @@ type ErrorResponse struct { } // NewServer ... -func NewServer(portStr, metadataAddress, inputAlphabet string, useMetadata bool) (*Server, error) { - var ( - mc metadata.Client - err error - ) - - if useMetadata { - log.Debugf("Waiting for metadata") - metadataURL := fmt.Sprintf(metadataURLTemplate, metadataAddress) - mc, err = metadata.NewClientAndWait(metadataURL) - if err != nil { - return nil, fmt.Errorf("Error creating metadata client: %v", err) - } - } - +func NewServer(portStr, inputAlphabet string) (*Server, error) { exitCh := make(chan int) return &Server{ - port: getServerPortToRun(portStr), - exitCh: exitCh, - mc: mc, - useMetadata: useMetadata, - alphabets: getAlphabetsToUse(inputAlphabet), + port: getServerPortToRun(portStr), + exitCh: exitCh, + alphabets: getAlphabetsToUse(inputAlphabet), }, nil } @@ -220,13 +198,6 @@ func (s *Server) homePageHandler(w http.ResponseWriter, r *http.Request) { log.Debugf("localIP: %v", localIP) p.IPAddress = localIP - if s.useMetadata { - self, err := s.mc.GetSelfContainer() - if err == nil { - p.DockerID = self.ExternalId - p.RancherID = self.UUID - } - } var t *template.Template ua := r.UserAgent() @@ -280,6 +251,11 @@ func getServerPortToRun(portStr string) int { return port } +func getRandomAlphabetIndex() int { + rand.Seed(time.Now().Unix()) + return rand.Intn(len(allAlphabets)) +} + func getAlphabetIndex(alphabet string) int { index := rune(-1) if alphabet != "" { @@ -295,7 +271,14 @@ func getAlphabetIndex(alphabet string) int { func getAlphabetsToUse(inputAlphabet string) []alphabet { var alphabets []alphabet - alphabetIndex := getAlphabetIndex(inputAlphabet) + var alphabetIndex int + + if inputAlphabet == "random" || inputAlphabet == "RANDOM" { + log.Infof("picking a random alphabet") + alphabetIndex = getRandomAlphabetIndex() + } else { + alphabetIndex = getAlphabetIndex(inputAlphabet) + } if alphabetIndex >= 0 { alphabets = allAlphabets[alphabetIndex : alphabetIndex+1] log.Infof("using alphabet: %v", alphabets)