From f3a804f098602ed914096dc38ae9cdeab465ee1b Mon Sep 17 00:00:00 2001 From: Dalton Hubble Date: Sun, 13 Dec 2015 17:44:39 -0800 Subject: [PATCH] api: Add flags to configure address and image asset path --- Dockerfile | 2 +- Godeps/_workspace/src/github.com/coreos/pkg | 1 + Makefile | 8 +--- acifile | 13 ------- api/server.go | 18 +++++++-- build | 1 + cmd/main.go | 42 ++++++++++++++++++--- docker-build | 2 +- docker-push | 4 +- docker-run | 3 ++ run | 2 +- scripts/pixiecore | 2 +- 12 files changed, 62 insertions(+), 36 deletions(-) create mode 160000 Godeps/_workspace/src/github.com/coreos/pkg delete mode 100755 acifile create mode 100755 docker-run diff --git a/Dockerfile b/Dockerfile index 1614d0af..ac8f51d9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,4 +2,4 @@ FROM alpine:latest MAINTAINER Dalton Hubble COPY bin/server /server EXPOSE 8080 -CMD ["./server"] +ENTRYPOINT ["./server"] diff --git a/Godeps/_workspace/src/github.com/coreos/pkg b/Godeps/_workspace/src/github.com/coreos/pkg new file mode 160000 index 00000000..2c77715c --- /dev/null +++ b/Godeps/_workspace/src/github.com/coreos/pkg @@ -0,0 +1 @@ +Subproject commit 2c77715c4df99b5420ffcae14ead08f52104065d diff --git a/Makefile b/Makefile index 7a856284..868dcde8 100644 --- a/Makefile +++ b/Makefile @@ -7,14 +7,8 @@ build: build-docker: ./docker-build -build-aci: - ./acifile - run-docker: - docker run -p 8080:8080 --name=bcs --rm -v $(shell echo $$PWD)/static:/static dghubble/bcs:latest - -run-rkt: - rkt --insecure-options=image run --no-overlay bin/bcs-0.0.1-linux-amd64.aci + ./docker-run run-pixiecore: ./scripts/pixiecore diff --git a/acifile b/acifile deleted file mode 100755 index 9daf8199..00000000 --- a/acifile +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/bash -e - -acbuild begin -acbuild set-name dghubble/bcs -acbuild copy bin/server /server -acbuild set-exec /server -acbuild port add www tcp 8080 -acbuild label add version 0.0.1 -acbuild label add arch amd64 -acbuild label add os linux -acbuild annotation add authors "Dalton Hubble " -acbuild write bin/bcs-0.0.1-linux-amd64.aci -acbuild end \ No newline at end of file diff --git a/api/server.go b/api/server.go index 24dff827..2be0eb59 100644 --- a/api/server.go +++ b/api/server.go @@ -4,15 +4,25 @@ import ( "net/http" ) +// Config configures the api Server. +type Config struct { + // Path to static image assets + ImagePath string + // Adapter which provides BootConfigs + BootAdapter BootAdapter +} + // Server serves iPXE/Pixiecore boot configs and hosts images. type Server struct { + imagePath string bootConfigs BootAdapter } // NewServer returns a new Server which uses the given BootAdapter. -func NewServer(bootConfigs BootAdapter) *Server { +func NewServer(config *Config) *Server { return &Server{ - bootConfigs: bootConfigs, + imagePath: config.ImagePath, + bootConfigs: config.BootAdapter, } } @@ -21,9 +31,9 @@ func (s *Server) HTTPHandler() http.Handler { mux := http.NewServeMux() // iPXE mux.Handle("/ipxe/", ipxeMux(s.bootConfigs)) - // Pixiecore API Server + // Pixiecore mux.Handle(pixiecorePath, pixiecoreHandler(s.bootConfigs)) // Kernel and Initrd Images - mux.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.Dir("static")))) + mux.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.Dir(s.imagePath)))) return mux } diff --git a/build b/build index 379fdfd3..9ff23e6e 100755 --- a/build +++ b/build @@ -3,6 +3,7 @@ # allow builds outside the standard GOPATH via symlink export GOPATH=${PWD}/Godeps/_workspace export GOBIN=${PWD}/bin + mkdir -p $GOPATH/src/github.com/coreos # directory or symlink must be present [ -d $GOPATH/src/github.com/coreos/coreos-baremetal ] || ln -s ${PWD} $GOPATH/src/github.com/coreos/coreos-baremetal diff --git a/cmd/main.go b/cmd/main.go index a92d2795..12ca8208 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -3,12 +3,15 @@ package main import ( "log" "net/http" + "flag" + "os" + "fmt" + "net/url" "github.com/coreos/coreos-baremetal/api" + "github.com/coreos/pkg/flagutil" ) -const address = ":8080" - // Example Boot Configs var CoreOSStable = &api.BootConfig{ @@ -44,14 +47,41 @@ var CoreOSLocalAutoLogin = &api.BootConfig{ } func main() { + flags := flag.NewFlagSet("bootcfg", flag.ExitOnError) + address := flags.String("address", "127.0.0.1:8080", "HTTP listen address") + imagesPath := flags.String("images-path", "./images", "Path to static image assets") + + if err := flags.Parse(os.Args[1:]); err != nil { + fmt.Println(err.Error()) + os.Exit(1) + } + if err := flagutil.SetFlagsFromEnv(flags, "BOOTCFG"); err != nil { + fmt.Println(err.Error()) + os.Exit(1) + } + + // validate arguments + if url, err := url.Parse(*address); err != nil || url.String() == "" { + log.Fatal("A valid HTTP listen address is required") + } + if finfo, err := os.Stat(*imagesPath); err != nil || !finfo.IsDir() { + log.Fatal("A path to an image assets directory is required") + } + // load some boot configs bootAdapter := api.NewMapBootAdapter() bootAdapter.AddUUID("8a549bf5-075c-4372-8b0d-ce7844faa48c", CoreOSLocalAutoLogin ) bootAdapter.SetDefault(CoreOSLocal) - // api server - server := api.NewServer(bootAdapter) - log.Printf("Starting boot config server") - err := http.ListenAndServe(address, server.HTTPHandler()) + + config := &api.Config{ + ImagePath: *imagesPath, + BootAdapter: bootAdapter, + } + + // API server + server := api.NewServer(config) + log.Printf("Starting boot config server on %s", *address) + err := http.ListenAndServe(*address, server.HTTPHandler()) if err != nil { log.Fatal("ListenAndServe: ", err) } diff --git a/docker-build b/docker-build index ed7bc14f..78ec68fe 100755 --- a/docker-build +++ b/docker-build @@ -1,6 +1,6 @@ #!/bin/bash -e -REPO=dghubble/bcs +REPO=dghubble/bootcfg GIT_SHA=$(git rev-parse HEAD) docker build -q --rm=true -t $REPO:$GIT_SHA . diff --git a/docker-push b/docker-push index a54e4e40..a1b43c11 100755 --- a/docker-push +++ b/docker-push @@ -1,7 +1,7 @@ #!/bin/bash -e -REPO=dghubble/bcs -QUAY_REPO=quay.io/dghubble/bcs +REPO=dghubble/bootcfg +QUAY_REPO=quay.io/dghubble/bootcfg GIT_SHA=$(git rev-parse HEAD) # quay.io diff --git a/docker-run b/docker-run new file mode 100755 index 00000000..59eee295 --- /dev/null +++ b/docker-run @@ -0,0 +1,3 @@ +#!/bin/bash -e + +docker run -p 8080:8080 --name=bootcfg --rm -v $PWD/images:/images dghubble/bootcfg:latest -address=0.0.0.0:8080 \ No newline at end of file diff --git a/run b/run index fc8914aa..5b19c8df 100755 --- a/run +++ b/run @@ -8,4 +8,4 @@ mkdir -p $GOPATH/src/github.com/coreos # directory or symlink must be present [ -d $GOPATH/src/github.com/coreos/coreos-baremetal ] || ln -s ${PWD} $GOPATH/src/github.com/coreos/coreos-baremetal -go run cmd/main.go +go run cmd/main.go "$@" diff --git a/scripts/pixiecore b/scripts/pixiecore index 6d933ec3..0ca5fc2b 100755 --- a/scripts/pixiecore +++ b/scripts/pixiecore @@ -1,6 +1,6 @@ #!/bin/bash -e -CONFIG_SERVICE=bcs +CONFIG_SERVICE=bootcfg CONFIG_SERVICE_IP=$(docker inspect --format {{.NetworkSettings.IPAddress}} ${CONFIG_SERVICE}) CONFIG_SERVICE_PORT=$(docker inspect --format '{{ (index (index .NetworkSettings.Ports "8080/tcp") 0).HostPort }}' ${CONFIG_SERVICE})