Skip to content

Commit

Permalink
Merge pull request #2 from limanmys/feature/discovery-logs
Browse files Browse the repository at this point in the history
Feature/discovery logs
  • Loading branch information
zekiahmetbayar authored Mar 7, 2024
2 parents 67d139b + 56707c6 commit 0bce360
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 5 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ jobs:
mkdir -p $SERVER_BIN_PATH
mkdir -p $SERVER_BIN_PATH/reports
mkdir -p $SERVER_BIN_PATH/wmi
mkdir -p $SERVER_BIN_PATH/logs
mkdir -p $SERVER_BIN_PATH/storage
mv wmi.so $SERVER_BIN_PATH/wmi/
mv storage/alternatives.csv $SERVER_BIN_PATH/storage/
Expand Down
58 changes: 54 additions & 4 deletions app/controllers/discoveries/discoveries.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package discoveries

import (
"os"
"path"

"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
"github.com/limanmys/inventory-server/app/entities"
"github.com/limanmys/inventory-server/internal/constants"
"github.com/limanmys/inventory-server/internal/database"
"github.com/limanmys/inventory-server/internal/paginator"
"github.com/limanmys/inventory-server/internal/search"
Expand Down Expand Up @@ -36,28 +40,50 @@ func Create(c *fiber.Ctx) error {
return err
}

// Set run id
runID := uuid.New().String()

// Create discovery logs record
if err := database.Connection().Create(&entities.DiscoveryLogs{
DiscoveryID: payload.ID,
Filename: runID,
}).Error; err != nil {
return err
}

// Start discovery
go discovery.Start(payload)
go discovery.Start(payload, runID)

return c.JSON(payload)
}

// Run, runs a discovery
func Run(c *fiber.Ctx) error {
// Check uuid validity
uuid, err := uuid.Parse(c.Params("id"))
uid, err := uuid.Parse(c.Params("id"))
if err != nil {
return err
}

// Get discovery
var discoveryObject entities.Discovery
if err := database.Connection().Model(&entities.Discovery{}).Where("id = ?", uuid).First(&discoveryObject).Error; err != nil {
if err := database.Connection().Model(&entities.Discovery{}).Where("id = ?", uid).First(&discoveryObject).Error; err != nil {
return err
}

// Set run id
runID := uuid.New().String()

// Create discovery logs record
if err := database.Connection().Create(&entities.DiscoveryLogs{
DiscoveryID: discoveryObject.ID,
Filename: runID,
}).Error; err != nil {
return err
}

// Start discovery
go discovery.Start(discoveryObject)
go discovery.Start(discoveryObject, runID)

return c.JSON("Discovery started successfully.")
}
Expand Down Expand Up @@ -101,3 +127,27 @@ func Delete(c *fiber.Ctx) error {

return c.JSON("Record deleted successfully.")
}

// ReadLog, Reads a latest log file
func ReadLatestLog(c *fiber.Ctx) error {
// Check uuid validity
uuid, err := uuid.Parse(c.Params("id"))
if err != nil {
return err
}

// Get logs
var log entities.DiscoveryLogs
if err := database.Connection().Model(&entities.DiscoveryLogs{}).
Where("discovery_id = ?", uuid).Order("updated_at DESC").First(&log).Error; err != nil {
return err
}

// Read content
content, err := os.ReadFile(path.Join(constants.DEFAULT_LOG_PATH, log.Filename))
if err != nil {
return err
}

return c.JSON(fiber.Map{"content": string(content)})
}
7 changes: 7 additions & 0 deletions app/entities/Discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,10 @@ func (d *Discovery) UpdateStatus(status Status, message string) {
d.Message = message
database.Connection().Model(d).Save(d)
}

type DiscoveryLogs struct {
Base
DiscoveryID *uuid.UUID `json:"discovery_id"`
Discovery *Discovery `json:"discovery"`
Filename string `json:"filename"`
}
1 change: 1 addition & 0 deletions app/entities/Result.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ type Arguments struct {
IPRange string `json:"ip_range"`
Username string `json:"string"`
Password string `json:"password"`
RunID string `json:"run_id"`
}
2 changes: 2 additions & 0 deletions app/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func Routes(app *fiber.App) {
// Discovery routes
discoveryGroup := app.Group("/discoveries")
{
// Read latest log
discoveryGroup.Get("/logs/:id", discoveries.ReadLatestLog)
// Create record
discoveryGroup.Post("/", discoveries.Create)
// Run discovery
Expand Down
1 change: 1 addition & 0 deletions internal/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ const (
REPORT_SAVE_PATH = "./reports/"
WMI_SO_PATH = "./wmi/wmi.so"
ALTERNATIVES_CSV_PATH = "./storage/alternatives.csv"
DEFAULT_LOG_PATH = "/opt/inventory-server/logs"
)
3 changes: 3 additions & 0 deletions internal/migrations/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@ func Migrate() error {
if err := database.Connection().AutoMigrate(&entities.Job{}); err != nil {
return err
}
if err := database.Connection().AutoMigrate(&entities.DiscoveryLogs{}); err != nil {
return err
}
return nil
}
3 changes: 2 additions & 1 deletion pkg/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"gorm.io/gorm/clause"
)

func Start(discovery entities.Discovery) {
func Start(discovery entities.Discovery, run_id string) {
// Open c-shared library
lib, err := dl.Open(constants.WMI_SO_PATH, 0)
if err != nil {
Expand Down Expand Up @@ -57,6 +57,7 @@ func Start(discovery entities.Discovery) {
IPRange: discovery.IPRange,
Username: profile.Username,
Password: profile.Password,
RunID: run_id,
})
if err != nil {
discovery.UpdateStatus(entities.StatusError, "error when marshalling arguments, err: "+err.Error())
Expand Down

0 comments on commit 0bce360

Please sign in to comment.