Skip to content

Commit 0bce360

Browse files
Merge pull request #2 from limanmys/feature/discovery-logs
Feature/discovery logs
2 parents 67d139b + 56707c6 commit 0bce360

File tree

8 files changed

+71
-5
lines changed

8 files changed

+71
-5
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ jobs:
7171
mkdir -p $SERVER_BIN_PATH
7272
mkdir -p $SERVER_BIN_PATH/reports
7373
mkdir -p $SERVER_BIN_PATH/wmi
74+
mkdir -p $SERVER_BIN_PATH/logs
7475
mkdir -p $SERVER_BIN_PATH/storage
7576
mv wmi.so $SERVER_BIN_PATH/wmi/
7677
mv storage/alternatives.csv $SERVER_BIN_PATH/storage/

app/controllers/discoveries/discoveries.go

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package discoveries
22

33
import (
4+
"os"
5+
"path"
6+
47
"github.com/gofiber/fiber/v2"
58
"github.com/google/uuid"
69
"github.com/limanmys/inventory-server/app/entities"
10+
"github.com/limanmys/inventory-server/internal/constants"
711
"github.com/limanmys/inventory-server/internal/database"
812
"github.com/limanmys/inventory-server/internal/paginator"
913
"github.com/limanmys/inventory-server/internal/search"
@@ -36,28 +40,50 @@ func Create(c *fiber.Ctx) error {
3640
return err
3741
}
3842

43+
// Set run id
44+
runID := uuid.New().String()
45+
46+
// Create discovery logs record
47+
if err := database.Connection().Create(&entities.DiscoveryLogs{
48+
DiscoveryID: payload.ID,
49+
Filename: runID,
50+
}).Error; err != nil {
51+
return err
52+
}
53+
3954
// Start discovery
40-
go discovery.Start(payload)
55+
go discovery.Start(payload, runID)
4156

4257
return c.JSON(payload)
4358
}
4459

4560
// Run, runs a discovery
4661
func Run(c *fiber.Ctx) error {
4762
// Check uuid validity
48-
uuid, err := uuid.Parse(c.Params("id"))
63+
uid, err := uuid.Parse(c.Params("id"))
4964
if err != nil {
5065
return err
5166
}
5267

5368
// Get discovery
5469
var discoveryObject entities.Discovery
55-
if err := database.Connection().Model(&entities.Discovery{}).Where("id = ?", uuid).First(&discoveryObject).Error; err != nil {
70+
if err := database.Connection().Model(&entities.Discovery{}).Where("id = ?", uid).First(&discoveryObject).Error; err != nil {
71+
return err
72+
}
73+
74+
// Set run id
75+
runID := uuid.New().String()
76+
77+
// Create discovery logs record
78+
if err := database.Connection().Create(&entities.DiscoveryLogs{
79+
DiscoveryID: discoveryObject.ID,
80+
Filename: runID,
81+
}).Error; err != nil {
5682
return err
5783
}
5884

5985
// Start discovery
60-
go discovery.Start(discoveryObject)
86+
go discovery.Start(discoveryObject, runID)
6187

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

102128
return c.JSON("Record deleted successfully.")
103129
}
130+
131+
// ReadLog, Reads a latest log file
132+
func ReadLatestLog(c *fiber.Ctx) error {
133+
// Check uuid validity
134+
uuid, err := uuid.Parse(c.Params("id"))
135+
if err != nil {
136+
return err
137+
}
138+
139+
// Get logs
140+
var log entities.DiscoveryLogs
141+
if err := database.Connection().Model(&entities.DiscoveryLogs{}).
142+
Where("discovery_id = ?", uuid).Order("updated_at DESC").First(&log).Error; err != nil {
143+
return err
144+
}
145+
146+
// Read content
147+
content, err := os.ReadFile(path.Join(constants.DEFAULT_LOG_PATH, log.Filename))
148+
if err != nil {
149+
return err
150+
}
151+
152+
return c.JSON(fiber.Map{"content": string(content)})
153+
}

app/entities/Discovery.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,10 @@ func (d *Discovery) UpdateStatus(status Status, message string) {
2929
d.Message = message
3030
database.Connection().Model(d).Save(d)
3131
}
32+
33+
type DiscoveryLogs struct {
34+
Base
35+
DiscoveryID *uuid.UUID `json:"discovery_id"`
36+
Discovery *Discovery `json:"discovery"`
37+
Filename string `json:"filename"`
38+
}

app/entities/Result.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ type Arguments struct {
99
IPRange string `json:"ip_range"`
1010
Username string `json:"string"`
1111
Password string `json:"password"`
12+
RunID string `json:"run_id"`
1213
}

app/routes/routes.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ func Routes(app *fiber.App) {
2727
// Discovery routes
2828
discoveryGroup := app.Group("/discoveries")
2929
{
30+
// Read latest log
31+
discoveryGroup.Get("/logs/:id", discoveries.ReadLatestLog)
3032
// Create record
3133
discoveryGroup.Post("/", discoveries.Create)
3234
// Run discovery

internal/constants/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ const (
44
REPORT_SAVE_PATH = "./reports/"
55
WMI_SO_PATH = "./wmi/wmi.so"
66
ALTERNATIVES_CSV_PATH = "./storage/alternatives.csv"
7+
DEFAULT_LOG_PATH = "/opt/inventory-server/logs"
78
)

internal/migrations/migrate.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,8 @@ func Migrate() error {
2424
if err := database.Connection().AutoMigrate(&entities.Job{}); err != nil {
2525
return err
2626
}
27+
if err := database.Connection().AutoMigrate(&entities.DiscoveryLogs{}); err != nil {
28+
return err
29+
}
2730
return nil
2831
}

pkg/discovery/discovery.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"gorm.io/gorm/clause"
1515
)
1616

17-
func Start(discovery entities.Discovery) {
17+
func Start(discovery entities.Discovery, run_id string) {
1818
// Open c-shared library
1919
lib, err := dl.Open(constants.WMI_SO_PATH, 0)
2020
if err != nil {
@@ -57,6 +57,7 @@ func Start(discovery entities.Discovery) {
5757
IPRange: discovery.IPRange,
5858
Username: profile.Username,
5959
Password: profile.Password,
60+
RunID: run_id,
6061
})
6162
if err != nil {
6263
discovery.UpdateStatus(entities.StatusError, "error when marshalling arguments, err: "+err.Error())

0 commit comments

Comments
 (0)