mirror of
https://github.com/openfaas/faasd.git
synced 2025-06-19 20:46:40 +00:00
Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
306313ed9a | |||
ff0cccf0dc | |||
52baca9d17 | |||
f76432f60a | |||
38f26b213f |
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
/faasd
|
/faasd
|
||||||
hosts
|
hosts
|
||||||
/resolv.conf
|
/resolv.conf
|
||||||
|
.idea/
|
||||||
|
@ -65,6 +65,7 @@ func runUp(_ *cobra.Command, _ []string) error {
|
|||||||
log.Printf("Supervisor init done in: %s\n", time.Since(start).String())
|
log.Printf("Supervisor init done in: %s\n", time.Since(start).String())
|
||||||
|
|
||||||
shutdownTimeout := time.Second * 1
|
shutdownTimeout := time.Second * 1
|
||||||
|
timeout := time.Second * 60
|
||||||
|
|
||||||
wg := sync.WaitGroup{}
|
wg := sync.WaitGroup{}
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
@ -85,6 +86,12 @@ func runUp(_ *cobra.Command, _ []string) error {
|
|||||||
})
|
})
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
wd, _ := os.Getwd()
|
||||||
|
proxy := pkg.NewProxy(path.Join(wd, "hosts"), timeout)
|
||||||
|
proxy.Start()
|
||||||
|
}()
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
|
export ARCH="armv6l"
|
||||||
echo "Downloading Go"
|
echo "Downloading Go"
|
||||||
|
|
||||||
curl -SLsf https://dl.google.com/go/go1.12.14.linux-armv6l.tar.gz > go.tgz
|
curl -SLsf https://dl.google.com/go/go1.12.14.linux-$ARCH.tar.gz --output /tmp/go.tgz
|
||||||
sudo rm -rf /usr/local/go/
|
sudo rm -rf /usr/local/go/
|
||||||
sudo mkdir -p /usr/local/go/
|
sudo mkdir -p /usr/local/go/
|
||||||
sudo tar -xvf go.tgz -C /usr/local/go/ --strip-components=1
|
sudo tar -xvf /tmp/go.tgz -C /usr/local/go/ --strip-components=1
|
||||||
|
|
||||||
export GOPATH=$HOME/go/
|
export GOPATH=$HOME/go/
|
||||||
export PATH=$PATH:/usr/local/go/bin/
|
export PATH=$PATH:/usr/local/go/bin/
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
|
export ARCH="amd64"
|
||||||
echo "Downloading Go"
|
echo "Downloading Go"
|
||||||
|
|
||||||
curl -SLsf https://dl.google.com/go/go1.12.14.linux-amd64.tar.gz > go.tgz
|
curl -SLsf https://dl.google.com/go/go1.12.14.linux-$ARCH.tar.gz --output /tmp/go.tgz
|
||||||
sudo rm -rf /usr/local/go/
|
sudo rm -rf /usr/local/go/
|
||||||
sudo mkdir -p /usr/local/go/
|
sudo mkdir -p /usr/local/go/
|
||||||
sudo tar -xvf go.tgz -C /usr/local/go/ --strip-components=1
|
sudo tar -xvf /tmp/go.tgz -C /usr/local/go/ --strip-components=1
|
||||||
|
|
||||||
export GOPATH=$HOME/go/
|
export GOPATH=$HOME/go/
|
||||||
export PATH=$PATH:/usr/local/go/bin/
|
export PATH=$PATH:/usr/local/go/bin/
|
||||||
|
98
pkg/proxy.go
Normal file
98
pkg/proxy.go
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
package pkg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
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() error {
|
||||||
|
tcp := 8080
|
||||||
|
|
||||||
|
time.Sleep(3 * time.Second)
|
||||||
|
log.Printf("Starting faasd proxy on %d\n", tcp)
|
||||||
|
data := struct{ host string }{
|
||||||
|
host: "",
|
||||||
|
}
|
||||||
|
|
||||||
|
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{
|
||||||
|
Addr: fmt.Sprintf(":%d", tcp),
|
||||||
|
ReadTimeout: p.Timeout,
|
||||||
|
WriteTimeout: p.Timeout,
|
||||||
|
MaxHeaderBytes: 1 << 20, // Max header of 1MB
|
||||||
|
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
query := ""
|
||||||
|
if len(r.URL.RawQuery) > 0 {
|
||||||
|
query = "?" + r.URL.RawQuery
|
||||||
|
}
|
||||||
|
|
||||||
|
upstream := fmt.Sprintf("http://%s:8080%s%s", data.host, r.URL.Path, query)
|
||||||
|
fmt.Printf("Forward to %s %s\n", upstream, data)
|
||||||
|
|
||||||
|
if r.Body != nil {
|
||||||
|
defer r.Body.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
wrapper := ioutil.NopCloser(r.Body)
|
||||||
|
upReq, upErr := http.NewRequest(r.Method, upstream, wrapper)
|
||||||
|
|
||||||
|
if upErr != nil {
|
||||||
|
log.Println(upErr)
|
||||||
|
|
||||||
|
http.Error(w, upErr.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
upRes, upResErr := http.DefaultClient.Do(upReq)
|
||||||
|
|
||||||
|
if upResErr != nil {
|
||||||
|
log.Println(upResErr)
|
||||||
|
|
||||||
|
http.Error(w, upResErr.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range upRes.Header {
|
||||||
|
w.Header().Set(k, v[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(upRes.StatusCode)
|
||||||
|
io.Copy(w, upRes.Body)
|
||||||
|
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.ListenAndServe()
|
||||||
|
}
|
@ -43,6 +43,12 @@ func Remove(ctx context.Context, client *containerd.Client, name string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error deleting container %s, %s, %s", container.ID(), name, err)
|
return fmt.Errorf("error deleting container %s, %s, %s", container.ID(), name, err)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
service := client.SnapshotService("")
|
||||||
|
key := name + "snapshot"
|
||||||
|
if _, err := client.SnapshotService("").Stat(ctx, key); err == nil {
|
||||||
|
service.Remove(ctx, key)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -186,7 +186,6 @@ func (s *Supervisor) Start(svcs []Service) error {
|
|||||||
}
|
}
|
||||||
log.Println("Exited: ", exitStatusC)
|
log.Println("Exited: ", exitStatusC)
|
||||||
|
|
||||||
// call start on the task to execute the redis server
|
|
||||||
if err := task.Start(ctx); err != nil {
|
if err := task.Start(ctx); err != nil {
|
||||||
log.Println("Task err: ", err)
|
log.Println("Task err: ", err)
|
||||||
return err
|
return err
|
||||||
|
@ -66,7 +66,7 @@ func DaemonReload() error {
|
|||||||
|
|
||||||
func InstallUnit(name string) error {
|
func InstallUnit(name string) error {
|
||||||
tmplName := "./hack/" + name + ".service"
|
tmplName := "./hack/" + name + ".service"
|
||||||
tmpl, err := template.ParseFiles()
|
tmpl, err := template.ParseFiles(tmplName)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error loading template %s, error %s", tmplName, err)
|
return fmt.Errorf("error loading template %s, error %s", tmplName, err)
|
||||||
|
Reference in New Issue
Block a user