Skip to content

Feat/ui changes for manifestlist #7

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

Open
wants to merge 4 commits 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)))
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ require (
github.com/gorilla/mux v1.7.1
github.com/opencontainers/go-digest v1.0.0-rc1
github.com/opencontainers/image-spec v1.0.1 // indirect
//github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/qnib/image-spec v2.0.0-feature+incompatible
github.com/sirupsen/logrus v1.4.1 // indirect
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ github.com/prometheus/common v0.2.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y8
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.0-20190117184657-bf6a532e95b1 h1:/K3IL0Z1quvmJ7X0A1AwNEK7CRkVK3YwfOU/QAL4WGg=
github.com/prometheus/procfs v0.0.0-20190117184657-bf6a532e95b1/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/qnib/image-spec v2.0.0-feature+incompatible h1:Tkei126H6GlnhaTj1ipFEGRWnDxQ4C1KeaJC/5O8DOE=
github.com/qnib/image-spec v2.0.0-feature+incompatible/go.mod h1:KDRMn7EhYdZqS2LTyV+C1uWDYMd4kJ2Bw9iBu3U1ftk=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.1 h1:GL2rEmy6nsikmW0r8opw9JIRScdMF5hA8cOYLH7In1k=
Expand Down
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
3 changes: 2 additions & 1 deletion pkg/storage/machinetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ type MachineType struct {
Password string `json:"password"`
}

// ToBytes creates a Byte array
func (mtm MachineType) ToBytes() []byte {
reqBodyBytes := new(bytes.Buffer)
json.NewEncoder(reqBodyBytes).Encode(mtm)
return reqBodyBytes.Bytes()
}
}
76 changes: 76 additions & 0 deletions pkg/storage/manifestlist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package storage

import (
"bytes"
"encoding/json"
"fmt"

v1 "github.com/qnib/image-spec/specs-go/v2"
)

// repoName represents the full path without the registry component.
// https://docs.docker.com/registry/spec/api/
/******
Classically, repository names have always been two path components where each path component
is less than 30 characters. The V2 registry API does not enforce this.
The rules for a repository name are as follows:
1. A repository name is broken up into path components. A component of a
repository name must be at least one lowercase, alpha-numeric characters, optionally separated by periods,
dashes or underscores. More strictly, it must match the regular expression [a-z0-9]+(?:[._-][a-z0-9]+)*.
2. If a repository name has two or more path components, they must be separated by a forward slash (“/”).
3. The total length of a repository name, including slashes, must be less than 256 characters.
*/

// ManifestList describes the metahub-internal definitions of a ManifestList
type ManifestList struct {
ID int64 `json:"id"`
RepoName string `json:"repo"`
TagName string `json:"tag"`
Manifests []Manifest `json:"manifests,omitempty"`
// TODO: Do we need user/password to safeguard ManifestLists - might be just safeguarded vai Manifest
}

// ToBytes returns a bytearray
func (mtm ManifestList) ToBytes() []byte {
reqBodyBytes := new(bytes.Buffer)
json.NewEncoder(reqBodyBytes).Encode(mtm)
return reqBodyBytes.Bytes()
}

// NewManifestList generates a new Manifest list
func NewManifestList(repo, tag string, mfs ...Manifest) (ml ManifestList, err error) {
ml.RepoName = repo
ml.TagName = tag
ml.Manifests = mfs
return
}

// Manifest describes a single Image (aka Manifest)
type Manifest struct {
ID int64 `json:"id"`
RepoName string `json:"image"`
TagName string `json:"tag"`
Platform v1.Platform `json:"features,omitempty"`
}

// ToBytes returns a bytearray
func (mtm Manifest) ToBytes() []byte {
reqBodyBytes := new(bytes.Buffer)
json.NewEncoder(reqBodyBytes).Encode(mtm)
return reqBodyBytes.Bytes()
}

// NewManifest generates a new Manifest objects checking for correctness
func NewManifest(repo, tag, os, arch string, features ...string) (mf Manifest, err error) {
mf.RepoName = repo
mf.TagName = tag
if arch != "amd64" && os != "linux" {
return mf, fmt.Errorf("Let's first concentrate on 'linux' and 'amd64' - keep it simple")
}
mf.Platform = v1.Platform{
Architecture: arch,
OS: os,
Features: features,
}
return
}
11 changes: 11 additions & 0 deletions pkg/storage/manifestlist_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package storage

// ManifestListService provides an internal storage for ManifestLists defined within MetaHub
// to complement backend ManifestList or backends without ManifestList implementation
type ManifestListService interface {
HasManifestList(repoName string) (result bool, err error)
Add(ml *ManifestList) (err error)
Delete(repoName, tagName string, id int64) error
List() ([]*ManifestList, error)
Update(ml *ManifestList) error
}
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"`
}
Loading