Skip to content

Commit 8a7fbef

Browse files
committed
Add config file and better logging
1 parent a44dd54 commit 8a7fbef

File tree

9 files changed

+162
-58
lines changed

9 files changed

+162
-58
lines changed

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ RUN CGO_ENABLED=0 go build -o bin/molecule .
99

1010
FROM gcr.io/distroless/static-debian12
1111

12-
COPY --from=builder /go/src/github.com/DistroByte/molecule/bin/molecule /bin/molecule
12+
COPY --from=builder /go/src/github.com/DistroByte/molecule/bin/molecule /app/molecule
1313
COPY --from=builder /go/src/github.com/DistroByte/molecule/web /web
1414

1515
WORKDIR /
1616

17-
ENTRYPOINT ["/bin/molecule"]
17+
ENTRYPOINT ["/app/molecule"]

config.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
standard_urls:
2+
- service: "nomad"
3+
url: "http://zeus.internal:4646"
4+
- service: "consul"
5+
url: "http://zeus.internal:8500"
6+
- service: "traefik"
7+
url: "http://hermes.internal:8081"
8+
- service: "synology-dsm"
9+
url: "https://dionysus.internal:5001"
10+
- service: "plausible"
11+
url: "https://plausible.dbyte.xyz"
12+
- service: "photos"
13+
url: "https://photos.dbyte.xyz"
14+
- service: "drive"
15+
url: "https://drive.dbyte.xyz"
16+
- service: "plex"
17+
url: "https://video.dbyte.xyz"
18+
- service: "ghost"
19+
url: "https://admin-photo.james-hackett.ie/ghost"
20+
21+
nomad:
22+
address: "http://zeus.internal:4646"
23+
24+
apikey: blahblah

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/jedib0t/go-pretty/v6 v6.6.7
1111
github.com/rs/zerolog v1.33.0
1212
github.com/stretchr/testify v1.10.0
13+
gopkg.in/yaml.v3 v3.0.1
1314
)
1415

1516
require (
@@ -37,5 +38,4 @@ require (
3738
github.com/rivo/uniseg v0.4.7 // indirect
3839
golang.org/x/sys v0.31.0 // indirect
3940
golang.org/x/text v0.23.0 // indirect
40-
gopkg.in/yaml.v3 v3.0.1 // indirect
4141
)

internal/api/v1/nomad_service.go

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import (
99
"strings"
1010
"sync"
1111

12+
"github.com/DistroByte/molecule/logger"
1213
"github.com/hashicorp/nomad/api"
1314
"github.com/jedib0t/go-pretty/v6/table"
14-
"github.com/rs/zerolog/log"
1515
)
1616

1717
type NomadService struct {
@@ -35,24 +35,18 @@ var (
3535
servicePorts = make(map[string]string)
3636
)
3737

38-
func NewNomadService(nomadClient *api.Client) NomadServiceInterface {
39-
standardURLs["nomad"] = "http://zeus.internal:4646"
40-
standardURLs["consul"] = "http://zeus.internal:8500"
41-
standardURLs["traefik"] = "http://hermes.internal:8081"
42-
standardURLs["synology-dsm"] = "https://dionysus.internal:5001"
43-
standardURLs["plausible"] = "https://plausible.dbyte.xyz"
44-
standardURLs["photos"] = "https://photos.dbyte.xyz"
45-
standardURLs["drive"] = "https://drive.dbyte.xyz"
46-
standardURLs["plex"] = "https://video.dbyte.xyz"
47-
standardURLs["ghost"] = "https://admin-photo.james-hackett.ie/ghost"
38+
func NewNomadService(nomadClient *api.Client, staticUrls map[string]string) NomadServiceInterface {
39+
for key, value := range staticUrls {
40+
standardURLs[key] = value
41+
}
4842

4943
return &NomadService{nomadClient: nomadClient}
5044
}
5145

5246
func (s *NomadService) ExtractAll(print bool) (map[string]string, error) {
5347
allocations, _, err := s.nomadClient.Allocations().List(nil)
5448
if err != nil {
55-
log.Error().Err(err).Msg("Failed to list allocations")
49+
logger.Log.Error().Err(err).Msg("Failed to list allocations")
5650
return nil, err
5751
}
5852

@@ -91,7 +85,7 @@ func (s *NomadService) ExtractAll(print bool) (map[string]string, error) {
9185
func (s *NomadService) ExtractURLs() (map[string]string, error) {
9286
allocations, _, err := s.nomadClient.Allocations().List(nil)
9387
if err != nil {
94-
log.Error().Err(err).Msg("Failed to list allocations")
88+
logger.Log.Error().Err(err).Msg("Failed to list allocations")
9589
return nil, err
9690
}
9791

@@ -114,7 +108,7 @@ func (s *NomadService) ExtractURLs() (map[string]string, error) {
114108
func (s *NomadService) ExtractHostPorts() (map[string]string, error) {
115109
allocations, _, err := s.nomadClient.Allocations().List(nil)
116110
if err != nil {
117-
log.Error().Err(err).Msg("Failed to list allocations")
111+
logger.Log.Error().Err(err).Msg("Failed to list allocations")
118112
return nil, err
119113
}
120114

@@ -133,7 +127,7 @@ func (s *NomadService) ExtractHostPorts() (map[string]string, error) {
133127
func (s *NomadService) ExtractServicePorts() (map[string]string, error) {
134128
allocations, _, err := s.nomadClient.Allocations().List(nil)
135129
if err != nil {
136-
log.Error().Err(err).Msg("Failed to list allocations")
130+
logger.Log.Error().Err(err).Msg("Failed to list allocations")
137131
return nil, err
138132
}
139133

@@ -152,7 +146,7 @@ func (s *NomadService) ExtractServicePorts() (map[string]string, error) {
152146
func (s *NomadService) GetServiceStatus(serviceName string) (map[string]string, error) {
153147
allocations, _, err := s.nomadClient.Allocations().List(nil)
154148
if err != nil {
155-
log.Error().Err(err).Msg("Failed to list allocations")
149+
logger.Log.Error().Err(err).Msg("Failed to list allocations")
156150
return nil, err
157151
}
158152

@@ -187,26 +181,26 @@ func (s *NomadService) GetServiceStatus(serviceName string) (map[string]string,
187181
func (s *NomadService) RestartServiceAllocations(serviceName string) (map[string]string, error) {
188182
allocations, _, err := s.nomadClient.Allocations().List(nil)
189183
if err != nil {
190-
log.Error().Err(err).Msg("Failed to list allocations")
184+
logger.Log.Error().Err(err).Msg("Failed to list allocations")
191185
return nil, err
192186
}
193187

194188
for _, allocation := range allocations {
195189
job, _, err := s.nomadClient.Jobs().Info(allocation.JobID, nil)
196190
if err != nil {
197-
log.Error().Err(err).Msg("Failed to get job info")
191+
logger.Log.Error().Err(err).Msg("Failed to get job info")
198192
return nil, err
199193
}
200194

201195
if *job.Name == serviceName {
202196
allocationInfo, _, err := s.nomadClient.Allocations().Info(allocation.ID, nil)
203197
if err != nil {
204-
log.Error().Err(err).Msg("Failed to get allocation info")
198+
logger.Log.Error().Err(err).Msg("Failed to get allocation info")
205199
return nil, err
206200
}
207201
err = s.nomadClient.Allocations().Restart(allocationInfo, "", nil)
208202
if err != nil {
209-
log.Error().Err(err).Msg("Failed to restart service allocations")
203+
logger.Log.Error().Err(err).Msg("Failed to restart service allocations")
210204
return nil, err
211205
}
212206
}
@@ -220,19 +214,19 @@ func (s *NomadService) processAllocation(allocation *api.AllocationListStub) {
220214

221215
allocationInfo, _, err := s.nomadClient.Allocations().Info(allocation.ID, nil)
222216
if err != nil {
223-
log.Error().Err(err).Msg("Failed to get allocation info")
217+
logger.Log.Error().Err(err).Msg("Failed to get allocation info")
224218
return
225219
}
226220

227221
node, _, err := s.nomadClient.Nodes().Info(allocation.NodeID, nil)
228222
if err != nil {
229-
log.Error().Err(err).Msg("Failed to get node info")
223+
logger.Log.Error().Err(err).Msg("Failed to get node info")
230224
return
231225
}
232226

233227
job, _, err := s.nomadClient.Jobs().Info(allocation.JobID, nil)
234228
if err != nil {
235-
log.Error().Err(err).Msg("Failed to get job info")
229+
logger.Log.Error().Err(err).Msg("Failed to get job info")
236230
return
237231
}
238232

internal/api/v1/nomad_service_mock.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package v1
22

3-
import "github.com/rs/zerolog/log"
3+
import (
4+
"github.com/DistroByte/molecule/logger"
5+
)
46

57
type MockNomadService struct{}
68

@@ -16,31 +18,31 @@ func NewMockNomadService() NomadServiceInterface {
1618
}
1719

1820
func (m *MockNomadService) ExtractAll(print bool) (map[string]string, error) {
19-
log.Debug().Msg("Mock: ExtractAll called")
21+
logger.Log.Debug().Msg("Mock: ExtractAll called")
2022
return urls, nil
2123
}
2224

2325
func (m *MockNomadService) ExtractURLs() (map[string]string, error) {
24-
log.Debug().Msg("Mock: ExtractURLs called")
26+
logger.Log.Debug().Msg("Mock: ExtractURLs called")
2527
return urls, nil
2628
}
2729

2830
func (m *MockNomadService) ExtractHostPorts() (map[string]string, error) {
29-
log.Debug().Msg("Mock: ExtractHostPorts called")
31+
logger.Log.Debug().Msg("Mock: ExtractHostPorts called")
3032
return urls, nil
3133
}
3234

3335
func (m *MockNomadService) ExtractServicePorts() (map[string]string, error) {
34-
log.Debug().Msg("Mock: ExtractServicePorts called")
36+
logger.Log.Debug().Msg("Mock: ExtractServicePorts called")
3537
return urls, nil
3638
}
3739

3840
func (m *MockNomadService) GetServiceStatus(service string) (map[string]string, error) {
39-
log.Debug().Msg("Mock: GetServiceStatus called")
41+
logger.Log.Debug().Msg("Mock: GetServiceStatus called")
4042
return urls, nil
4143
}
4244

4345
func (m *MockNomadService) RestartServiceAllocations(service string) (map[string]string, error) {
44-
log.Debug().Msg("Mock: RestartServiceAllocations called")
46+
logger.Log.Debug().Msg("Mock: RestartServiceAllocations called")
4547
return urls, nil
4648
}

logger/logger.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package logger
2+
3+
import (
4+
"os"
5+
6+
"github.com/rs/zerolog"
7+
"github.com/rs/zerolog/log"
8+
)
9+
10+
var Log zerolog.Logger
11+
12+
func InitLogger(level zerolog.Level) {
13+
zerolog.SetGlobalLevel(level)
14+
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
15+
16+
if level == zerolog.DebugLevel {
17+
Log = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
18+
Log.Info().Msg("running locally in debug mode")
19+
} else {
20+
Log = zerolog.New(os.Stderr).With().Timestamp().Logger()
21+
}
22+
}

0 commit comments

Comments
 (0)