Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create static (read-only) backend for demo purpose #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,24 @@ RUN go mod download
COPY ./cmd ./cmd
COPY ./pkg ./pkg
WORKDIR /go/metahub/cmd/boltdb
# static build
# static build of boltdb backend
ENV CGO_ENABLED=0 GOOS=linux
RUN go build -a -ldflags '-extldflags "-static"' .
WORKDIR /go/metahub/cmd/static
# static build of static backend
ENV CGO_ENABLED=0 GOOS=linux
RUN go build -a -ldflags '-extldflags "-static"' .
EXPOSE 8080


# Go binary serves the ui web content
FROM scratch
ARG MH_BACKEND=boltdb
ENV PORT=80
COPY --from=go /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=ui /go/metahub/static /srv/html/static
COPY --from=ui /go/metahub/templates/gen/index.html /srv/html/
COPY --from=go /go/metahub/cmd/boltdb/boltdb /usr/bin/
COPY --from=go /go/metahub/cmd/${MH_BACKEND}/${MH_BACKEND} /usr/bin/metahub
VOLUME /data/
WORKDIR /data/
ENTRYPOINT ["/usr/bin/boltdb"]
ENTRYPOINT ["/usr/bin/metahub"]
36 changes: 36 additions & 0 deletions cmd/static/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"fmt"
"log"
"metahub/pkg/daemon"
registry "metahub/pkg/registry/http/client"
"metahub/pkg/storage/static"
"net/http"
"os"

"metahub/cmd"
)

// Log allows to make http interactions visible
func Log(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s %s", r.RemoteAddr, r.Method, r.URL)
handler.ServeHTTP(w, r)
})
}

func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}

storageService := static.NewService()
registryService := registry.NewService()
daemonService := daemon.NewService(storageService, registryService)

router := cmd.RegisterRoutes(daemonService)

log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), Log(router)))
}
41 changes: 0 additions & 41 deletions pkg/storage/boltdb/dummies.go

This file was deleted.

58 changes: 5 additions & 53 deletions pkg/storage/boltdb/machinetype_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,19 @@ import (
"fmt"
"log"
"metahub/pkg/storage"

"github.com/boltdb/bolt"
"os"
)

type machineTypeService struct {
ctx context.Context
ctx context.Context
}

func formatLogin(accountName string, login string) {

}

func (s *machineTypeService) GetByID(accountName string, id int64) (mt *storage.MachineType, err error) {
log.Printf("GetByID(%s, %d)\n", accountName, id)
if _, b := os.LookupEnv("STATIC_MACHINES");b {
log.Println("Environment STATIC_MACHINES is set: Hardcoded types are served")
switch id {
case 1:
mt = &mType1
case 2:
mt = &mType2
case 3:
mt = &mType3
case 4:
mt = &mType4
}
return mt, nil
}
var foundID bool
db.View(func(tx *bolt.Tx) error {
// Assume bucket exists and has keys
Expand All @@ -53,37 +38,17 @@ func (s *machineTypeService) GetByID(accountName string, id int64) (mt *storage.
}
return err
})
if !foundID {
err = fmt.Errorf("Could not find MachineType with ID: %i", id)
if !foundID {
err = fmt.Errorf("Could not find MachineType with ID: %d", id)
}
return mt, err
}

func (s *machineTypeService) GetByUsername(username string) (mt *storage.MachineType, err error) {
log.Printf("GetByUsername(%s)\n", username)
if _, b := os.LookupEnv("STATIC_MACHINES");b {
log.Println("Environment STATIC_MACHINES is set: Serve static machine type")
switch username {
case user+"-type1":
return &mType1, nil
case user+"-type2":
return &mType2, nil
case user+"-type3":
return &mType3, nil
case user+"-type4":
return &mType4, nil
default:
panic(fmt.Errorf("Could not find username: %s", username))
}
}
return mt, nil
}

func (s *machineTypeService) Add(accountName string, mt *storage.MachineType) (err error) {
if _, b := os.LookupEnv("STATIC_MACHINES");b {
log.Println("Environment STATIC_MACHINES is set: Skip Add()")
return err
}
dbSync.Lock()
defer dbSync.Unlock()
err = db.Update(func(tx *bolt.Tx) error {
Expand All @@ -106,23 +71,14 @@ func (s *machineTypeService) Delete(accountName string, id int64) error {

func (s *machineTypeService) List(accountName string) ([]storage.MachineType, error) {
result := []storage.MachineType{}
if _, b := os.LookupEnv("STATIC_MACHINES");b {
log.Println("Environment STATIC_MACHINES is set: Serve static machine type")
return []storage.MachineType{
mType1,
mType2,
mType3,
mType4,
},nil
}
db.View(func(tx *bolt.Tx) error {
// Assume bucket exists and has keys
b := tx.Bucket([]byte("TYPES"))

c := b.Cursor()
var mt storage.MachineType
for k, v := c.First(); k != nil; k, v = c.Next() {
if err := json.Unmarshal(v, &mt); err != nil {
if err := json.Unmarshal(v, &mt); err != nil {
panic(err)
}
result = append(result, mt)
Expand All @@ -133,10 +89,6 @@ func (s *machineTypeService) List(accountName string) ([]storage.MachineType, er
}

func (s *machineTypeService) Update(accountName string, mt storage.MachineType) (err error) {
if _, b := os.LookupEnv("STATIC_MACHINES");b {
log.Println("Environment STATIC_MACHINES is set: Serve static machine type")
return
}
dbSync.Lock()
defer dbSync.Unlock()
err = db.Update(func(tx *bolt.Tx) error {
Expand Down
12 changes: 12 additions & 0 deletions pkg/storage/static/accesstoken_model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package static

import (
"time"
)

var accessTokenEntityKind = "access_token"

type accessToken struct {
AccountName string `datastore:"account,noindex"`
Expiry time.Time `datastore:"expiry,omitempty"`
}
23 changes: 23 additions & 0 deletions pkg/storage/static/accesstoken_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package static

import (
"context"
"metahub/pkg/storage"
"time"
)

type accessTokenService struct {
ctx context.Context
}

func (s *accessTokenService) Get(token string) (*storage.AccessToken, error) {
//TODO: check at.Expiry?
return &storage.AccessToken{
AccountName: token,
Expiry: time.Time{},
}, nil
}

func (s *accessTokenService) Put(token string, at storage.AccessToken) error {
return nil
}
7 changes: 7 additions & 0 deletions pkg/storage/static/account_model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package static

var accountEntityKind = "account"

type account struct {
DisplayName string `datastore:"name,noindex"`
}
20 changes: 20 additions & 0 deletions pkg/storage/static/account_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package static

import (
"context"
"metahub/pkg/storage"
)

type accountService struct {
ctx context.Context
}

func (s *accountService) Upsert(name string, a storage.Account) error {
return nil
}

func (s *accountService) Get(name string) (*storage.Account, error) {
return &storage.Account{
DisplayName: name,
}, nil
}
48 changes: 48 additions & 0 deletions pkg/storage/static/dummies.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package static

import "metahub/pkg/storage"

// Consts for protoype
const (
user = "qnib"
accountName = user
)

// Dummy MachineTypes
var (
mType1 = storage.MachineType{
ID: 1,
DisplayName: "type1",
Features: []string{"cpu:broadwell"},
Login: user + "-type1",
Password: user + "-type1",
}
mType2 = storage.MachineType{
ID: 2,
DisplayName: "type2",
Features: []string{"cpu:skylake"},
Login: user + "-type2",
Password: user + "-type2",
}
mType3 = storage.MachineType{
ID: 3,
DisplayName: "type3",
Features: []string{"cpu:coffelake"},
Login: user + "-type3",
Password: user + "-type3",
}
mType4 = storage.MachineType{
ID: 4,
DisplayName: "type4",
Features: []string{"cpu:broadwell", "nvcap:5.2"},
Login: user + "-type4",
Password: user + "-type4",
}
)

func getMachineTypes() []storage.MachineType {
return []storage.MachineType{
mType1, mType2, mType3, mType4,
}

}
10 changes: 10 additions & 0 deletions pkg/storage/static/machinetype_model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package static

var machineTypeEntityKind = "MachineType"

type machineTypeModel struct {
DisplayName string `datastore:"name,noindex"`
Features []string `datastore:"features,noindex"`
Login string `datastore:"login"`
Password string `datastore:"password,noindex"`
}
Loading