Skip to content

Commit

Permalink
Merge pull request #19 from leodotcloud/fixes
Browse files Browse the repository at this point in the history
Multiple fixes
  • Loading branch information
leodotcloud authored Jul 8, 2021
2 parents 04a3f91 + 8dc6ee5 commit 19df188
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 57 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

```
Expand Down
20 changes: 3 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,16 @@ 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() {
app := cli.NewApp()
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,
Expand All @@ -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",
Expand Down Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions package/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ RUN apt-get update && \
netcat \
netcat-openbsd \
openssl \
openssh-client \
psmisc \
socat \
tcpdump \
Expand Down
63 changes: 23 additions & 40 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -113,12 +109,10 @@ var homePageTmpl = `<html>

// 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 ...
Expand All @@ -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
}

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 != "" {
Expand All @@ -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)
Expand Down

0 comments on commit 19df188

Please sign in to comment.