@@ -24,6 +24,8 @@ type NomadServiceInterface interface {
2424 ExtractURLs () (map [string ]string , error )
2525 ExtractHostPorts () (map [string ]string , error )
2626 ExtractServicePorts () (map [string ]string , error )
27+ GetServiceStatus (service string ) (map [string ]string , error )
28+ RestartServiceAllocations (service string ) (map [string ]string , error )
2729}
2830
2931var (
@@ -42,7 +44,7 @@ func NewNomadService(nomadClient *api.Client) NomadServiceInterface {
4244 standardURLs ["photos" ] = "https://photos.dbyte.xyz"
4345 standardURLs ["drive" ] = "https://drive.dbyte.xyz"
4446 standardURLs ["plex" ] = "https://video.dbyte.xyz"
45- standardURLs ["ghost" ] = "https://admin-photo.james-hackett.ie"
47+ standardURLs ["ghost" ] = "https://admin-photo.james-hackett.ie/ghost "
4648
4749 return & NomadService {nomadClient : nomadClient }
4850}
@@ -147,6 +149,72 @@ func (s *NomadService) ExtractServicePorts() (map[string]string, error) {
147149 return servicePorts , nil
148150}
149151
152+ func (s * NomadService ) GetServiceStatus (serviceName string ) (map [string ]string , error ) {
153+ allocations , _ , err := s .nomadClient .Allocations ().List (nil )
154+ if err != nil {
155+ log .Error ().Err (err ).Msg ("Failed to list allocations" )
156+ return nil , err
157+ }
158+
159+ serviceUrls = make (map [string ]string )
160+ hostReservedPorts = make (map [string ]string )
161+ servicePorts = make (map [string ]string )
162+
163+ for _ , allocation := range allocations {
164+ s .processAllocation (allocation )
165+ }
166+
167+ s .mu .Lock ()
168+ defer s .mu .Unlock ()
169+
170+ if val , ok := serviceUrls [serviceName ]; ok {
171+ return map [string ]string {serviceName : val }, nil
172+ }
173+
174+ if val , ok := hostReservedPorts [serviceName ]; ok {
175+ return map [string ]string {serviceName : val }, nil
176+ }
177+
178+ if val , ok := servicePorts [serviceName ]; ok {
179+ return map [string ]string {serviceName : val }, nil
180+ }
181+
182+ return nil , nil
183+ }
184+
185+ // when called, this function will restart all allocations for a specific service
186+ // this is useful when a new version of a service is built and you want to restart all instances
187+ func (s * NomadService ) RestartServiceAllocations (serviceName string ) (map [string ]string , error ) {
188+ allocations , _ , err := s .nomadClient .Allocations ().List (nil )
189+ if err != nil {
190+ log .Error ().Err (err ).Msg ("Failed to list allocations" )
191+ return nil , err
192+ }
193+
194+ for _ , allocation := range allocations {
195+ job , _ , err := s .nomadClient .Jobs ().Info (allocation .JobID , nil )
196+ if err != nil {
197+ log .Error ().Err (err ).Msg ("Failed to get job info" )
198+ return nil , err
199+ }
200+
201+ if * job .Name == serviceName {
202+ allocationInfo , _ , err := s .nomadClient .Allocations ().Info (allocation .ID , nil )
203+ if err != nil {
204+ log .Error ().Err (err ).Msg ("Failed to get allocation info" )
205+ return nil , err
206+ }
207+ err = s .nomadClient .Allocations ().Restart (allocationInfo , "" , nil )
208+ if err != nil {
209+ log .Error ().Err (err ).Msg ("Failed to restart service allocations" )
210+ return nil , err
211+ }
212+ }
213+ }
214+
215+ return nil , nil
216+ }
217+
150218func (s * NomadService ) processAllocation (allocation * api.AllocationListStub ) {
151219 services := []* api.Service {}
152220
0 commit comments