mirror of
https://github.com/hanzozt/zt.git
synced 2026-10-30 15:04:55 +00:00
- Repo names: ziti-* → zt-* - Go modules: github.com/hanzozt/ziti → github.com/hanzozt/zt - Docker images: hanzozt/ziti-* → hanzozt/zt-* - C/Java/JS symbols: ziti_* → zt_* - File and directory names updated
127 lines
2.9 KiB
Go
127 lines
2.9 KiB
Go
package ztlab
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/hanzozt/fablab/kernel/model"
|
|
"github.com/hanzozt/storage/boltz"
|
|
"github.com/hanzozt/zt/v2/controller/command"
|
|
"github.com/hanzozt/zt/v2/controller/db"
|
|
"github.com/pkg/errors"
|
|
"go.etcd.io/bbolt"
|
|
)
|
|
|
|
type ZitiDbBuilderStrategy interface {
|
|
GetDbFile(m *model.Model) string
|
|
ProcessDbModel(tx *bbolt.Tx, m *model.Model, builder *ZitiDbBuilder) error
|
|
}
|
|
|
|
type ZitiEdgeRouterStrategy interface {
|
|
GetSite(router *db.EdgeRouter) (string, bool)
|
|
PostProcess(router *db.EdgeRouter, c *model.Component)
|
|
}
|
|
|
|
type ZitiDbBuilder struct {
|
|
Strategy ZitiDbBuilderStrategy
|
|
ztDb boltz.Db
|
|
stores *db.Stores
|
|
}
|
|
|
|
func (self *ZitiDbBuilder) GetDb() boltz.Db {
|
|
return self.ztDb
|
|
}
|
|
|
|
func (self *ZitiDbBuilder) GetStores() *db.Stores {
|
|
return self.stores
|
|
}
|
|
|
|
func (self *ZitiDbBuilder) Build(m *model.Model) error {
|
|
dbFile := self.Strategy.GetDbFile(m)
|
|
|
|
var err error
|
|
self.ztDb, err = db.Open(dbFile)
|
|
if err != nil {
|
|
return errors.Wrapf(err, "unable to open zt bbolt db [%v]", dbFile)
|
|
}
|
|
|
|
defer func() {
|
|
if err = self.ztDb.Close(); err != nil {
|
|
panic(err)
|
|
}
|
|
}()
|
|
|
|
self.stores, err = db.InitStores(self.ztDb, command.NoOpRateLimiter{}, nil)
|
|
if err != nil {
|
|
return errors.Wrapf(err, "unable to init fabric stores using db [%v]", dbFile)
|
|
}
|
|
|
|
return self.ztDb.View(func(tx *bbolt.Tx) error {
|
|
return self.Strategy.ProcessDbModel(tx, m, self)
|
|
})
|
|
}
|
|
|
|
func (self *ZitiDbBuilder) CreateEdgeRouterHosts(tx *bbolt.Tx, m *model.Model, erStrategy ZitiEdgeRouterStrategy) error {
|
|
ids, _, err := self.stores.EdgeRouter.QueryIds(tx, "true limit none")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, id := range ids {
|
|
er, err := self.stores.EdgeRouter.LoadById(tx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if site, useEdgeRouter := erStrategy.GetSite(er); useEdgeRouter {
|
|
regionId := site[:len(site)-1]
|
|
|
|
var region *model.Region
|
|
for _, r := range m.Regions {
|
|
if r.Site == site {
|
|
region = r
|
|
break
|
|
}
|
|
}
|
|
|
|
if region == nil {
|
|
if _, found := m.Regions[site]; found {
|
|
return errors.Errorf("trying to add region for site %v, but one exists, with different site", site)
|
|
}
|
|
region = &model.Region{
|
|
Scope: model.Scope{Tags: model.Tags{}},
|
|
Region: regionId,
|
|
Site: site,
|
|
Hosts: model.Hosts{},
|
|
}
|
|
m.Regions[site] = region
|
|
}
|
|
|
|
host := &model.Host{
|
|
Scope: model.Scope{Tags: model.Tags{}},
|
|
Region: region,
|
|
Components: model.Components{},
|
|
}
|
|
id = strings.ReplaceAll(er.Id, ".", "_")
|
|
region.Hosts["router_"+id] = host
|
|
|
|
component := &model.Component{
|
|
Scope: model.Scope{Tags: model.Tags{}},
|
|
Type: &RouterType{},
|
|
Host: host,
|
|
}
|
|
|
|
host.Components[er.Id] = component
|
|
erStrategy.PostProcess(er, component)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (self *ZitiDbBuilder) DefaultGetSite(er *db.EdgeRouter) (string, bool) {
|
|
if val, found := er.Tags["fablab.site"]; found {
|
|
return fmt.Sprintf("%v", val), true
|
|
}
|
|
return "", false
|
|
}
|