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
1717type 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
5246func (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) {
9185func (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) {
114108func (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) {
133127func (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) {
152146func (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,
187181func (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
0 commit comments