Skip to content

Commit

Permalink
Refactor repository path organization for better structure
Browse files Browse the repository at this point in the history
Signed-off-by: Quentin Guidée <[email protected]>
  • Loading branch information
quentinguidee committed Sep 6, 2023
1 parent cd34b8e commit 5d0ff76
Show file tree
Hide file tree
Showing 15 changed files with 105 additions and 91 deletions.
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ func New() Config {

func (c Config) Apply() error {
configJsContent := fmt.Sprintf("window.apiURL = \"http://%s\";", c.Host)
return os.WriteFile(path.Join(storage.PathClient, "dist", "config.js"), []byte(configJsContent), os.ModePerm)
return os.WriteFile(path.Join(storage.Path, "client", "dist", "config.js"), []byte(configJsContent), os.ModePerm)
}
59 changes: 13 additions & 46 deletions init.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package main

import (
"errors"
"flag"
"fmt"
"os"
"runtime"

"github.com/vertex-center/vertex/config"
"github.com/vertex-center/vertex/pkg/logger"
"github.com/vertex-center/vertex/pkg/storage"
"github.com/vertex-center/vertex/router"
"github.com/vertex-center/vertex/services"
"github.com/vertex-center/vertex/types"
Expand All @@ -29,37 +29,7 @@ func main() {

parseArgs()

err := os.MkdirAll(storage.PathInstances, os.ModePerm)
if err != nil && !os.IsExist(err) {
logger.Error(fmt.Errorf("failed to create directory: %v", err)).
AddKeyValue("message", "failed to create directory").
AddKeyValue("path", storage.PathInstances).
Print()

return
}

err = os.MkdirAll(storage.PathProxy, os.ModePerm)
if err != nil && !os.IsExist(err) {
logger.Error(fmt.Errorf("failed to create directory: %v", err)).
AddKeyValue("message", "failed to create directory").
AddKeyValue("path", storage.PathProxy).
Print()

return
}

err = os.MkdirAll(storage.PathSettings, os.ModePerm)
if err != nil && !os.IsExist(err) {
logger.Error(fmt.Errorf("failed to create directory: %v", err)).
AddKeyValue("message", "failed to create directory").
AddKeyValue("path", storage.PathProxy).
Print()

return
}

err = setupDependencies()
err := setupDependencies()
if err != nil {
logger.Error(fmt.Errorf("failed to setup dependencies: %v", err)).Print()
return
Expand Down Expand Up @@ -119,17 +89,14 @@ func parseArgs() {
}

func setupDependencies() error {
dependencies := []struct {
dir string
dep types.Dependency
}{
{storage.PathClient, services.DependencyClient},
{storage.PathServices, services.DependencyServices},
{storage.PathPackages, services.DependencyPackages},
dependencies := []types.Dependency{
services.DependencyClient,
services.DependencyServices,
services.DependencyPackages,
}

for _, d := range dependencies {
err := setupDependency(d.dir, d.dep)
for _, dep := range dependencies {
err := setupDependency(dep)
if err != nil {
logger.Error(err).Print()
os.Exit(1)
Expand All @@ -138,8 +105,8 @@ func setupDependencies() error {
return nil
}

func setupDependency(dir string, dependency types.Dependency) error {
err := os.Mkdir(dir, os.ModePerm)
func setupDependency(dep types.Dependency) error {
err := os.Mkdir(dep.GetPath(), os.ModePerm)
if os.IsExist(err) {
// The dependency already exists.
return nil
Expand All @@ -149,9 +116,9 @@ func setupDependency(dir string, dependency types.Dependency) error {
}

// download
_, err = dependency.CheckForUpdate()
if err != nil && err != services.ErrDependencyNotInstalled {
_, err = dep.CheckForUpdate()
if err != nil && !errors.Is(err, services.ErrDependencyNotInstalled) {
return err
}
return dependency.InstallUpdate()
return dep.InstallUpdate()
}
10 changes: 1 addition & 9 deletions pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,7 @@ import (
"github.com/vertex-center/vertex/pkg/logger"
)

const (
PathClient = "live/client"
PathPackages = "live/packages"
PathProxy = "live/proxy"
PathInstances = "live/instances"
PathServices = "live/services"
PathSettings = "live/settings"
PathUpdates = "live/updates"
)
const Path = "live"

var (
ErrNoReleasesPublished = errors.New("this repository has no existing releases")
Expand Down
25 changes: 19 additions & 6 deletions repository/instance_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,23 @@ var (
)

type InstanceFSRepository struct {
instances map[uuid.UUID]*types.Instance
instancesPath string
instances map[uuid.UUID]*types.Instance
}

func NewInstanceFSRepository() InstanceFSRepository {
r := InstanceFSRepository{
instances: map[uuid.UUID]*types.Instance{},
instancesPath: path.Join(storage.Path, "instances"),
instances: map[uuid.UUID]*types.Instance{},
}

err := os.MkdirAll(r.instancesPath, os.ModePerm)
if err != nil && !os.IsExist(err) {
logger.Error(err).
AddKeyValue("message", "failed to create directory").
AddKeyValue("path", r.instancesPath).
Print()
os.Exit(1)
}

return r
Expand All @@ -48,7 +59,7 @@ func (r *InstanceFSRepository) GetAll() map[uuid.UUID]*types.Instance {
}

func (r *InstanceFSRepository) GetPath(uuid uuid.UUID) string {
return path.Join(storage.PathInstances, uuid.String())
return path.Join(r.instancesPath, uuid.String())
}

func (r *InstanceFSRepository) Delete(uuid uuid.UUID) error {
Expand Down Expand Up @@ -169,15 +180,16 @@ func (r *InstanceFSRepository) LoadEnv(i *types.Instance) error {
}

func (r *InstanceFSRepository) Reload(load func(uuid uuid.UUID)) {
entries, err := os.ReadDir(storage.PathInstances)
entries, err := os.ReadDir(r.instancesPath)
if err != nil {
log.Fatal(err)
}

for _, entry := range entries {
info, err := entry.Info()
if err != nil {
log.Fatal(err)
logger.Error(err).Print()
continue
}

isInstance := entry.IsDir() || info.Mode()&os.ModeSymlink != 0
Expand All @@ -189,7 +201,8 @@ func (r *InstanceFSRepository) Reload(load func(uuid uuid.UUID)) {

id, err := uuid.Parse(entry.Name())
if err != nil {
log.Fatal(err)
logger.Error(err).Print()
continue
}

load(id)
Expand Down
4 changes: 2 additions & 2 deletions repository/instance_logs_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ func NewInstanceLogsFSRepository() InstanceLogsFSRepository {
}

func (r *InstanceLogsFSRepository) Open(uuid uuid.UUID) error {
dir := path.Join(storage.PathInstances, uuid.String(), ".vertex", "logs")
dir := path.Join(storage.Path, "instances", uuid.String(), ".vertex", "logs")
err := os.MkdirAll(dir, os.ModePerm)
if err != nil {
return err
}

filename := fmt.Sprintf("logs_%s.txt", time.Now().Format(time.DateOnly))
filepath := path.Join(storage.PathInstances, uuid.String(), ".vertex", "logs", filename)
filepath := path.Join(dir, filename)

file, err := os.OpenFile(filepath, os.O_RDWR|os.O_CREATE|os.O_APPEND, os.ModePerm)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion repository/package_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewPackageFSRepository(params *PackageRepositoryParams) PackageFSRepository
params = &PackageRepositoryParams{}
}
if params.dependenciesPath == "" {
params.dependenciesPath = storage.PathPackages
params.dependenciesPath = path.Join(storage.Path, "packages")
}

repo := PackageFSRepository{
Expand Down
11 changes: 10 additions & 1 deletion repository/proxy_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,16 @@ func NewProxyFSRepository(params *ProxyRepositoryParams) ProxyFSRepository {
params = &ProxyRepositoryParams{}
}
if params.proxyPath == "" {
params.proxyPath = storage.PathProxy
params.proxyPath = path.Join(storage.Path, "proxy")
}

err := os.MkdirAll(params.proxyPath, os.ModePerm)
if err != nil && !os.IsExist(err) {
logger.Error(err).
AddKeyValue("message", "failed to create directory").
AddKeyValue("path", params.proxyPath).
Print()
os.Exit(1)
}

repo := ProxyFSRepository{
Expand Down
6 changes: 5 additions & 1 deletion repository/runner_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (r RunnerDockerRepository) Start(instance *types.Instance, onLog func(msg s

setStatus(types.InstanceStatusBuilding)

instancePath := path.Join(storage.PathInstances, instance.UUID.String())
instancePath := r.getPath(*instance)

// Build
var err error
Expand Down Expand Up @@ -437,3 +437,7 @@ func (r RunnerDockerRepository) watchForLogs(containerID string, instance *types
Print()
}()
}

func (r RunnerDockerRepository) getPath(instance types.Instance) string {
return path.Join(storage.Path, "instances", instance.UUID.String())
}
4 changes: 2 additions & 2 deletions repository/runner_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (r RunnerFSRepository) Start(instance *types.Instance, onLog func(msg strin
cmd.Dir = dir
cmd.Env = os.Environ()

envFile, err := os.Open(path.Join(storage.PathInstances, instance.UUID.String(), ".env"))
envFile, err := os.Open(path.Join(r.getPath(*instance), ".env"))
if err != nil {
return err
}
Expand Down Expand Up @@ -141,5 +141,5 @@ func (r RunnerFSRepository) HasUpdateAvailable(instance types.Instance) (bool, e
}

func (r RunnerFSRepository) getPath(instance types.Instance) string {
return path.Join(storage.PathInstances, instance.UUID.String())
return path.Join(storage.Path, "instances", instance.UUID.String())
}
2 changes: 1 addition & 1 deletion repository/service_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func NewServiceFSRepository(params *ServiceRepositoryParams) ServiceFSRepository
params = &ServiceRepositoryParams{}
}
if params.servicesPath == "" {
params.servicesPath = storage.PathServices
params.servicesPath = path.Join(storage.Path, "services")
}

repo := ServiceFSRepository{
Expand Down
13 changes: 11 additions & 2 deletions repository/settings_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,23 @@ func NewSettingsFSRepository(params *SettingsRepositoryParams) SettingsFSReposit
params = &SettingsRepositoryParams{}
}
if params.settingsPath == "" {
params.settingsPath = storage.PathSettings
params.settingsPath = path.Join(storage.Path, "settings")
}

err := os.MkdirAll(params.settingsPath, os.ModePerm)
if err != nil && !os.IsExist(err) {
logger.Error(err).
AddKeyValue("message", "failed to create directory").
AddKeyValue("path", params.settingsPath).
Print()
os.Exit(1)
}

repo := SettingsFSRepository{
settingsPath: params.settingsPath,
}

err := repo.read()
err = repo.read()
if err != nil {
logger.Error(err).Print()
}
Expand Down
2 changes: 1 addition & 1 deletion router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func NewRouter(about types.About) Router {
r.Use(ginutils.Logger("MAIN"))
r.Use(gin.Recovery())
r.Use(middleware.ErrorMiddleware())
r.Use(static.Serve("/", static.LocalFile(path.Join(".", storage.PathClient, "dist"), true)))
r.Use(static.Serve("/", static.LocalFile(path.Join(".", storage.Path, "client", "dist"), true)))
r.GET("/ping", handlePing)

runnerDockerRepo = repository.NewRunnerDockerRepository()
Expand Down
4 changes: 2 additions & 2 deletions services/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func (s *InstanceService) WriteEnv(uuid uuid.UUID, environment map[string]string

func (s *InstanceService) Install(serviceID string, method string) (*types.Instance, error) {
id := uuid.New()
dir := path.Join(storage.PathInstances, id.String())
dir := s.instanceRepo.GetPath(id)

service, err := s.serviceRepo.Get(serviceID)
if err != nil {
Expand Down Expand Up @@ -470,7 +470,7 @@ func (s *InstanceService) reload() {
}

func (s *InstanceService) load(uuid uuid.UUID) error {
instancePath := path.Join(storage.PathInstances, uuid.String())
instancePath := s.instanceRepo.GetPath(uuid)

service, err := s.instanceRepo.ReadService(instancePath)
if err != nil {
Expand Down
Loading

0 comments on commit 5d0ff76

Please sign in to comment.