Compare commits

..

2 Commits

Author SHA1 Message Date
409fa5e642 Alteration for version passing
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
2019-12-31 12:27:21 +00:00
03fe48db99 Pass version from main
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
2019-12-31 12:22:21 +00:00
3 changed files with 53 additions and 74 deletions

View File

@ -2,18 +2,16 @@ package cmd
import (
"fmt"
"github.com/pkg/errors"
"io/ioutil"
"log"
"os"
"os/signal"
"path"
"strings"
"sync"
"syscall"
"time"
"github.com/pkg/errors"
"github.com/alexellis/faasd/pkg"
"github.com/alexellis/k3sup/pkg/env"
"github.com/sethvargo/go-password/password"
@ -96,30 +94,10 @@ func runUp(_ *cobra.Command, _ []string) error {
})
}()
gatewayURLChan := make(chan string, 1)
proxy := pkg.NewProxy(timeout)
go proxy.Start(gatewayURLChan)
go func() {
wd, _ := os.Getwd()
time.Sleep(3 * time.Second)
fileData, fileErr := ioutil.ReadFile(path.Join(wd, "hosts"))
if fileErr != nil {
log.Println(fileErr)
return
}
host := ""
lines := strings.Split(string(fileData), "\n")
for _, line := range lines {
if strings.Index(line, "gateway") > -1 {
host = line[:strings.Index(line, "\t")]
}
}
log.Printf("[up] Sending %s to proxy\n", host)
gatewayURLChan <- host
close(gatewayURLChan)
proxy := pkg.NewProxy(path.Join(wd, "hosts"), timeout)
proxy.Start()
}()
wg.Wait()
@ -152,10 +130,10 @@ func makeBasicAuthFiles() error {
func makeFile(filePath, fileContents string) error {
_, err := os.Stat(filePath)
if err == nil {
log.Printf("File exists: %q\n", filePath)
log.Printf("File exists: %q, Using data from this file",filePath)
return nil
} else if os.IsNotExist(err) {
log.Printf("Writing to: %q\n", filePath)
log.Printf("Writing file: %q", filePath)
return ioutil.WriteFile(filePath, []byte(fileContents), 0644)
} else {
return err
@ -165,12 +143,12 @@ func makeFile(filePath, fileContents string) error {
func makeServiceDefinitions(archSuffix string) []pkg.Service {
wd, _ := os.Getwd()
secretMountDir := "/run/secrets"
secretMountDir := "/run/secrets/"
return []pkg.Service{
pkg.Service{
Name: "basic-auth-plugin",
Image: "docker.io/openfaas/basic-auth-plugin:0.18.10" + archSuffix,
Name: "basic-auth-plugin",
Image: "docker.io/openfaas/basic-auth-plugin:0.18.10" + archSuffix,
Env: []string{
"port=8080",
"secret_mount_path=" + secretMountDir,
@ -179,16 +157,16 @@ func makeServiceDefinitions(archSuffix string) []pkg.Service {
},
Mounts: []pkg.Mount{
pkg.Mount{
Src: path.Join(wd, "basic-auth-password"),
Dest: path.Join(secretMountDir, "basic-auth-password"),
Src:path.Join(wd, "/basic-auth-password"),
Dest:secretMountDir + "basic-auth-password",
},
pkg.Mount{
Src: path.Join(wd, "basic-auth-user"),
Dest: path.Join(secretMountDir, "basic-auth-user"),
Src: path.Join(wd, "/basic-auth-user"),
Dest: secretMountDir + "basic-auth-user",
},
},
Caps: []string{"CAP_NET_RAW"},
Args: nil,
Args: nil,
},
pkg.Service{
Name: "nats",
@ -224,18 +202,18 @@ func makeServiceDefinitions(archSuffix string) []pkg.Service {
"auth_proxy_pass_body=false",
"secret_mount_path=" + secretMountDir,
},
Image: "docker.io/openfaas/gateway:0.18.8" + archSuffix,
Image: "docker.io/openfaas/gateway:0.18.8" + archSuffix,
Mounts: []pkg.Mount{
pkg.Mount{
Src: path.Join(wd, "basic-auth-password"),
Dest: path.Join(secretMountDir, "basic-auth-password"),
Src:path.Join(wd, "/basic-auth-password"),
Dest:secretMountDir + "basic-auth-password",
},
pkg.Mount{
Src: path.Join(wd, "basic-auth-user"),
Dest: path.Join(secretMountDir, "basic-auth-user"),
Src: path.Join(wd, "/basic-auth-user"),
Dest: secretMountDir + "basic-auth-user",
},
},
Caps: []string{"CAP_NET_RAW"},
Caps: []string{"CAP_NET_RAW"},
},
pkg.Service{
Name: "queue-worker",
@ -250,18 +228,18 @@ func makeServiceDefinitions(archSuffix string) []pkg.Service {
"basic_auth=true",
"secret_mount_path=" + secretMountDir,
},
Image: "docker.io/openfaas/queue-worker:0.9.0",
Image: "docker.io/openfaas/queue-worker:0.9.0",
Mounts: []pkg.Mount{
pkg.Mount{
Src: path.Join(wd, "basic-auth-password"),
Dest: path.Join(secretMountDir, "basic-auth-password"),
Src:path.Join(wd, "/basic-auth-password"),
Dest:secretMountDir + "basic-auth-password",
},
pkg.Mount{
Src: path.Join(wd, "basic-auth-user"),
Dest: path.Join(secretMountDir, "basic-auth-user"),
Src: path.Join(wd, "/basic-auth-user"),
Dest: secretMountDir + "basic-auth-user",
},
},
Caps: []string{"CAP_NET_RAW"},
Caps: []string{"CAP_NET_RAW"},
},
}
}

View File

@ -6,36 +6,48 @@ import (
"io/ioutil"
"log"
"net/http"
"strings"
"time"
)
func NewProxy(timeout time.Duration) *Proxy {
func NewProxy(hosts string, timeout time.Duration) *Proxy {
return &Proxy{
Hosts: hosts,
Timeout: timeout,
}
}
type Proxy struct {
Hosts string
Timeout time.Duration
}
func (p *Proxy) Start(gatewayChan chan string) error {
func (p *Proxy) Start() error {
tcp := 8080
http.DefaultClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
time.Sleep(3 * time.Second)
log.Printf("Starting faasd proxy on %d\n", tcp)
data := struct{ host string }{
host: "",
}
data.host = <-gatewayChan
log.Printf("Starting faasd proxy on %d\n", tcp)
fileData, fileErr := ioutil.ReadFile(p.Hosts)
if fileErr != nil {
return fileErr
}
lines := strings.Split(string(fileData), "\n")
for _, line := range lines {
if strings.Index(line, "gateway") > -1 {
data.host = line[:strings.Index(line, "\t")]
}
}
fmt.Printf("Gateway: %s\n", data.host)
s := &http.Server{
@ -60,8 +72,6 @@ func (p *Proxy) Start(gatewayChan chan string) error {
wrapper := ioutil.NopCloser(r.Body)
upReq, upErr := http.NewRequest(r.Method, upstream, wrapper)
copyHeaders(upReq.Header, &r.Header)
if upErr != nil {
log.Println(upErr)
@ -78,7 +88,9 @@ func (p *Proxy) Start(gatewayChan chan string) error {
return
}
copyHeaders(w.Header(), &upRes.Header)
for k, v := range upRes.Header {
w.Header().Set(k, v[0])
}
w.WriteHeader(upRes.StatusCode)
io.Copy(w, upRes.Body)
@ -88,12 +100,3 @@ func (p *Proxy) Start(gatewayChan chan string) error {
return s.ListenAndServe()
}
// copyHeaders clones the header values from the source into the destination.
func copyHeaders(destination http.Header, source *http.Header) {
for k, v := range *source {
vClone := make([]string, len(v))
copy(vClone, v)
destination[k] = vClone
}
}

View File

@ -153,7 +153,7 @@ func (s *Supervisor) Start(svcs []Service) error {
)
if containerCreateErr != nil {
log.Printf("Error creating container %s\n", containerCreateErr)
log.Println(containerCreateErr)
return containerCreateErr
}
@ -161,7 +161,7 @@ func (s *Supervisor) Start(svcs []Service) error {
task, err := newContainer.NewTask(ctx, cio.NewCreator(cio.WithStdio))
if err != nil {
log.Printf("Error creating task: %s\n", err)
log.Println(err)
return err
}
@ -175,21 +175,19 @@ func (s *Supervisor) Start(svcs []Service) error {
writeErr := ioutil.WriteFile("hosts", hosts, 0644)
if writeErr != nil {
log.Printf("Error writing file %s %s\n", "hosts", writeErr)
log.Println("Error writing hosts file")
}
// os.Chown("hosts", 101, 101)
_, err = task.Wait(ctx)
exitStatusC, err := task.Wait(ctx)
if err != nil {
log.Printf("Wait err: %s\n", err)
log.Println(err)
return err
}
log.Println("Exited: ", exitStatusC)
log.Printf("Task: %s\tContainer: %s\n", task.ID(), newContainer.ID())
// log.Println("Exited: ", exitStatusC)
if err = task.Start(ctx); err != nil {
log.Printf("Task err: %s\n", err)
if err := task.Start(ctx); err != nil {
log.Println("Task err: ", err)
return err
}
}