Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
agalitsyn committed Jan 23, 2023
1 parent 57ea22c commit 2447ade
Show file tree
Hide file tree
Showing 49 changed files with 585 additions and 674 deletions.
5 changes: 2 additions & 3 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ import (
"soldr/pkg/app/api/worker"
"soldr/pkg/app/api/worker/events"
"soldr/pkg/log"
"soldr/pkg/mysql"
"soldr/pkg/observability"
"soldr/pkg/secret"
"soldr/pkg/storage"
"soldr/pkg/storage/mysql"
"soldr/pkg/system"
"soldr/pkg/version"
)
Expand Down Expand Up @@ -254,7 +253,7 @@ func main() {
)

gormMeter := meterProvider.Meter("vxapi-meter")
if err = storage.InitGormMetrics(gormMeter); err != nil {
if err = mysql.InitGormMetrics(gormMeter); err != nil {
logrus.WithError(err).Error("could not initialize vxapi-meter")
return
}
Expand Down
31 changes: 16 additions & 15 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ import (
"soldr/pkg/app/server/config"
"soldr/pkg/app/server/mmodule"
"soldr/pkg/controller"
"soldr/pkg/db"
"soldr/pkg/observability"
"soldr/pkg/storage"
"soldr/pkg/s3"
"soldr/pkg/system"
"soldr/pkg/utils"
"soldr/pkg/vxproto"
Expand Down Expand Up @@ -78,7 +79,7 @@ func (s *Server) Init(env svc.Environment) (err error) {
if err != nil {
return fmt.Errorf("failed to compose a DSN from config: %w", err)
}
gdb, err := storage.New(dsn)
gdb, err := db.New(dsn)
if err != nil {
return fmt.Errorf("failed to initialize a connection to DB: %w", err)
}
Expand All @@ -103,15 +104,15 @@ func (s *Server) Init(env svc.Environment) (err error) {
case loaderTypeFS:
cl, err = controller.NewConfigFromFS(s.config.Base)
case loaderTypeS3:
var s3ConnParams *storage.S3ConnParams
var s3ConnParams *s3.RemoteStorageConfig
s3ConnParams, err = s3ConnParamsFromConfig(&s.config.S3)
if err != nil {
err = fmt.Errorf("failed to compose s3 connection params from config: %w", err)
break
}
cl, err = controller.NewConfigFromS3(s3ConnParams)
case loaderTypeDB:
var dsn *storage.DSN
var dsn *db.DSN
dsn, err = dsnFromConfig(&s.config.DB)
if err != nil {
err = fmt.Errorf("failed to compose a DSN from config: %w", err)
Expand All @@ -132,7 +133,7 @@ func (s *Server) Init(env svc.Environment) (err error) {
case loaderTypeFS:
fl, err = controller.NewFilesFromFS(s.config.Base)
case loaderTypeS3:
var s3ConnParams *storage.S3ConnParams
var s3ConnParams *s3.RemoteStorageConfig
s3ConnParams, err = s3ConnParamsFromConfig(&s.config.S3)
if err != nil {
err = fmt.Errorf("failed to compose s3 connection params from config: %w", err)
Expand All @@ -149,7 +150,7 @@ func (s *Server) Init(env svc.Environment) (err error) {
logger.Info("modules files loader was created")

utils.RemoveUnusedTempDir()
store, err := storage.NewS3(&storage.S3ConnParams{
store, err := s3.NewRemoteStorage(&s3.RemoteStorageConfig{
Endpoint: s.config.S3.Endpoint,
AccessKey: s.config.S3.AccessKey,
SecretKey: s.config.S3.SecretKey,
Expand Down Expand Up @@ -185,7 +186,7 @@ func (s *Server) Init(env svc.Environment) (err error) {
return
}

func dsnFromConfig(c *config.DB) (*storage.DSN, error) {
func dsnFromConfig(c *config.DB) (*db.DSN, error) {
if c == nil {
return nil, fmt.Errorf("passed config is nil")
}
Expand All @@ -204,7 +205,7 @@ func dsnFromConfig(c *config.DB) (*storage.DSN, error) {
if len(c.Name) == 0 {
return nil, fmt.Errorf("db name is empty")
}
return &storage.DSN{
return &db.DSN{
Host: c.Host,
Port: c.Port,
User: c.User,
Expand All @@ -213,7 +214,7 @@ func dsnFromConfig(c *config.DB) (*storage.DSN, error) {
}, nil
}

func s3ConnParamsFromConfig(c *config.S3) (*storage.S3ConnParams, error) {
func s3ConnParamsFromConfig(c *config.S3) (*s3.RemoteStorageConfig, error) {
if c == nil {
return nil, fmt.Errorf("passed config is nil")
}
Expand All @@ -229,19 +230,19 @@ func s3ConnParamsFromConfig(c *config.S3) (*storage.S3ConnParams, error) {
if len(c.BucketName) == 0 {
return nil, fmt.Errorf("bucket name is empty")
}
return &storage.S3ConnParams{
return &s3.RemoteStorageConfig{
Endpoint: c.Endpoint,
AccessKey: c.AccessKey,
SecretKey: c.SecretKey,
BucketName: c.BucketName,
}, nil
}

func initCertProvider(c *config.CertsConfig, s3 storage.IFileReader) (certs.Provider, error) {
func initCertProvider(c *config.CertsConfig, s3FileReader s3.FileReader) (certs.Provider, error) {
if c == nil {
return nil, fmt.Errorf("passed config object is nil")
}
createFileProvider := func(store storage.IFileReader, base string) (certs.Provider, error) {
createFileProvider := func(store s3.FileReader, base string) (certs.Provider, error) {
conf := &certsConfig.Config{
StaticProvider: &certsConfig.StaticProvider{
Reader: store,
Expand All @@ -256,19 +257,19 @@ func initCertProvider(c *config.CertsConfig, s3 storage.IFileReader) (certs.Prov
}
switch c.Type {
case loaderTypeFS:
store, err := storage.NewFS()
store, err := s3.NewLocalStorage()
if err != nil {
return nil, fmt.Errorf("failed to initialize a file store: %w", err)
}
return createFileProvider(store, c.Base)
case loaderTypeS3:
return createFileProvider(s3, c.Base)
return createFileProvider(s3FileReader, c.Base)
default:
return nil, fmt.Errorf("store type %s is not available for certificate providers", c.Type)
}
}

func initGorm(dsn *storage.DSN, logDir string) (*gorm.DB, error) {
func initGorm(dsn *db.DSN, logDir string) (*gorm.DB, error) {
addr := fmt.Sprintf(
"%s:%s@%s/%s?parseTime=true",
dsn.User,
Expand Down
14 changes: 7 additions & 7 deletions pkg/app/api/client/agent_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (

"soldr/pkg/app/api/models"
"soldr/pkg/app/api/storage/mem"
"soldr/pkg/mysql"
"soldr/pkg/s3"
"soldr/pkg/secret"
"soldr/pkg/storage"
"soldr/pkg/storage/mysql"
)

type AgentServerClient struct {
Expand Down Expand Up @@ -70,20 +70,20 @@ func (c *AgentServerClient) GetDB(ctx context.Context, hash string) (*gorm.DB, e
return dbWithORM, nil
}

func (c *AgentServerClient) GetS3(hash string) (storage.IStorage, error) {
s3, err := c.s3Conns.Get(hash)
func (c *AgentServerClient) GetS3(hash string) (s3.FileStorage, error) {
s3Conn, err := c.s3Conns.Get(hash)
if err == nil {
return s3, nil
return s3Conn, nil
}

var service models.Service
if err = c.db.Take(&service, "hash = ?", hash).Error; err != nil {
return nil, fmt.Errorf("could not get service by hash '%s': %w", hash, err)
}

s3Conn, err := storage.NewS3(service.Info.S3.ToS3ConnParams())
s3Conn, err = s3.NewRemoteStorage(service.Info.S3.ToS3ConnParams())
if err != nil {
return nil, fmt.Errorf("could not create S3 client: %w", err)
return nil, fmt.Errorf("could not create RemoteStorage client: %w", err)
}
c.s3Conns.Set(hash, s3Conn)

Expand Down
10 changes: 5 additions & 5 deletions pkg/app/api/models/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"database/sql/driver"
"encoding/json"

"soldr/pkg/storage"

"github.com/jinzhu/gorm"

"soldr/pkg/s3"
)

// ServiceInfoDB is model to contain service external config to connetion to DB
Expand All @@ -23,7 +23,7 @@ func (sidb ServiceInfoDB) Valid() error {
return validate.Struct(sidb)
}

// ServiceInfoS3 is model to contain service external config to connetion to S3
// ServiceInfoS3 is model to contain service external config to connetion to RemoteStorage
type ServiceInfoS3 struct {
Endpoint string `form:"endpoint" json:"endpoint" validate:"max=100,required"`
AccessKey string `form:"access_key" json:"access_key" validate:"max=50,required"`
Expand All @@ -32,8 +32,8 @@ type ServiceInfoS3 struct {
}

// ToS3ConnParams is a helper function to convert the structure to the vxcommon version one
func (sis3 *ServiceInfoS3) ToS3ConnParams() *storage.S3ConnParams {
return &storage.S3ConnParams{
func (sis3 *ServiceInfoS3) ToS3ConnParams() *s3.RemoteStorageConfig {
return &s3.RemoteStorageConfig{
Endpoint: sis3.Endpoint,
AccessKey: sis3.AccessKey,
SecretKey: sis3.SecretKey,
Expand Down
34 changes: 17 additions & 17 deletions pkg/app/api/modules/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"soldr/pkg/app/api/models"
"soldr/pkg/app/api/utils"
"soldr/pkg/crypto"
"soldr/pkg/storage"
"soldr/pkg/s3"
)

type agentModuleDetails struct {
Expand Down Expand Up @@ -50,9 +50,9 @@ func joinPath(args ...string) string {
}

func CopyModuleAFilesToInstanceS3(mi *models.ModuleInfo, sv *models.Service) error {
gS3, err := storage.NewS3(nil)
gS3, err := s3.NewRemoteStorage(nil)
if err != nil {
return errors.New("failed to initialize global S3 driver: " + err.Error())
return errors.New("failed to initialize global RemoteStorage driver: " + err.Error())
}

mfiles, err := gS3.ReadDirRec(joinPath(mi.Name, mi.Version.String()))
Expand All @@ -67,24 +67,24 @@ func CopyModuleAFilesToInstanceS3(mi *models.ModuleInfo, sv *models.Service) err
return errors.New("failed to read utils files: " + err.Error())
}

iS3, err := storage.NewS3(sv.Info.S3.ToS3ConnParams())
iS3, err := s3.NewRemoteStorage(sv.Info.S3.ToS3ConnParams())
if err != nil {
return errors.New("failed to initialize instance S3 driver: " + err.Error())
return errors.New("failed to initialize instance RemoteStorage driver: " + err.Error())
}

if iS3.RemoveDir(joinPath(mi.Name, mi.Version.String())); err != nil {
return errors.New("failed to remove module directory from instance S3: " + err.Error())
return errors.New("failed to remove module directory from instance RemoteStorage: " + err.Error())
}

for fpath, fdata := range mfiles {
if err := iS3.WriteFile(joinPath(mi.Name, mi.Version.String(), fpath), fdata); err != nil {
return errors.New("failed to write system module file to S3: " + err.Error())
return errors.New("failed to write system module file to RemoteStorage: " + err.Error())
}
}

for fpath, fdata := range ufiles {
if err := iS3.WriteFile(joinPath("utils", fpath), fdata); err != nil {
return errors.New("failed to write utils file to S3: " + err.Error())
return errors.New("failed to write utils file to RemoteStorage: " + err.Error())
}
}

Expand Down Expand Up @@ -156,8 +156,8 @@ func clearMapKeysList(curMap, defMap map[string]interface{}) map[string]interfac

// args:
//
// cc is Current module Config from old module version which we tried to keep
// dc is Default module Config from actual module version which we are using as a reference
// cc is Current module RemoteStorageConfig from old module version which we tried to keep
// dc is Default module RemoteStorageConfig from actual module version which we are using as a reference
// sh is JSON Schema structure from actual module version which wa are using to check result document
func mergeModuleACurrentConfig(cc, dc models.ModuleConfig, sh models.Schema) models.ModuleConfig {
// add new config values from default
Expand Down Expand Up @@ -204,8 +204,8 @@ func mergeModuleASecureCurrentConfig(cc, dc models.ModuleSecureConfig, sh models

// args:
//
// cac is Current Action Config from old module version which we tried to keep
// dac is Default Action Config from actual module version which we are using as a reference
// cac is Current Action RemoteStorageConfig from old module version which we tried to keep
// dac is Default Action RemoteStorageConfig from actual module version which we are using as a reference
// sh is JSON Schema structure from actual module version which wa are using to check result document
func mergeModuleACurrentActionConfig(cac, dac models.ActionConfig, sh models.Schema) models.ActionConfig {
for acn, daci := range dac {
Expand Down Expand Up @@ -239,8 +239,8 @@ func mergeModuleACurrentActionConfig(cac, dac models.ActionConfig, sh models.Sch

// args:
//
// ceci is Current Event Config Item from old module version which we tried to keep
// deci is Default Event Config Item from actual module version which we are using as a reference
// ceci is Current Event RemoteStorageConfig Item from old module version which we tried to keep
// deci is Default Event RemoteStorageConfig Item from actual module version which we are using as a reference
// sh is JSON Schema structure from actual module version which wa are using to check result document
func mergeModuleAEventConfigItem(ceci, deci models.EventConfigItem, sh models.Schema) models.EventConfigItem {
reci := models.EventConfigItem{}
Expand All @@ -256,8 +256,8 @@ func mergeModuleAEventConfigItem(ceci, deci models.EventConfigItem, sh models.Sc

// args:
//
// cec is Current Event Config from old module version which we tried to keep
// dec is Default Event Config from actual module version which we are using as a reference
// cec is Current Event RemoteStorageConfig from old module version which we tried to keep
// dec is Default Event RemoteStorageConfig from actual module version which we are using as a reference
// sh is JSON Schema structure from actual module version which wa are using to check result document
func mergeModuleACurrentEventConfig(cec, dec models.EventConfig, sh models.Schema) models.EventConfig {
ecsh := copySchema(&sh.Type, models.GetECSDefinitions(sh.Definitions))
Expand Down Expand Up @@ -291,7 +291,7 @@ func mergeModuleACurrentEventConfig(cec, dec models.EventConfig, sh models.Schem
// args:
//
// dd is Dynamic Dependencies from old module version which we tried to keep
// cec is Current Event Config which was got after merging to default (result Current Event Config)
// cec is Current Event RemoteStorageConfig which was got after merging to default (result Current Event RemoteStorageConfig)
func clearModuleADynamicDependencies(dd models.Dependencies, cec models.EventConfig) models.Dependencies {
rdd := models.Dependencies{}
checkDepInActions := func(ec models.EventConfigItem, moduleName string) bool {
Expand Down
47 changes: 0 additions & 47 deletions pkg/app/api/server/context/context.go

This file was deleted.

Loading

0 comments on commit 2447ade

Please sign in to comment.