This repository has been archived by the owner on May 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
janitor.go
127 lines (105 loc) · 3.96 KB
/
janitor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"crypto/tls"
"flag"
"log"
"net/http"
"net/url"
"strconv"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/tbruyelle/hipchat-go/hipchat"
)
var (
tokenFlag = flag.String("token", "", "The HipChat API token.")
urlFlag = flag.String("url", "", "The HipChat server URL.")
intervalFlag = flag.Int("interval", 24, "How often cleanups are attempted, in hours.")
maxFlag = flag.Int("max", 30, "The maximum amount of time to keep a room, in days.")
insecureFlag = flag.Bool("insecure", false, "Skip certificate verification for HTTPS requests.")
)
func main() {
flag.Parse()
// validate flags
if *tokenFlag == "" {
log.Fatal("The 'token' flag is required for communications with HipChat.")
}
if *urlFlag == "" {
log.Fatal("The 'url' flag must be set to a valid HipChat server URL.")
}
if *intervalFlag <= 0 {
log.Fatal("The 'interval' flag must be set to a number greater than zero.")
}
if *maxFlag <= 0 {
log.Fatal("The 'max' flag must be set to a number greater than zero.")
}
// ensure that we can parse the provided URL flag
hipchatURL, err := url.Parse(*urlFlag)
if err != nil {
log.Fatalf("Fatal: could not parse URL flag: %v", err)
}
// setup health server
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) })
go http.ListenAndServe(":3000", nil)
// setup metrics server
http.Handle("/metrics", promhttp.Handler())
go http.ListenAndServe(":3001", nil)
// create a HipChat client
client := hipchat.NewClient(*tokenFlag)
client.BaseURL = hipchatURL
// if the insecure flag is set, customize the underlying HTTP client to skip TLS verification
if *insecureFlag {
client.SetHTTPClient(&http.Client{Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}})
}
// create a new ticker based on the interval configuration
ticker := time.NewTicker(time.Duration(*intervalFlag) * time.Hour)
log.Printf("HipChat Janitor Started. Archiving any rooms untouched for %d days every %d hours.", *maxFlag, *intervalFlag)
for {
// get rooms
rooms, _, err := client.Room.List(&hipchat.RoomsListOptions{IncludePrivate: true, IncludeArchived: false})
if err != nil {
log.Fatalf("Fatal: Could not get rooms list from HipChat: %v", err)
}
// loop over each room and check for archivability
for _, item := range rooms.Items {
// the item ID needs to be converted to a string
idString := strconv.Itoa(item.ID)
// get room details
room, _, err := client.Room.Get(idString)
if err != nil {
log.Printf("Error: Could not retrieve room '%s': %v", item.Name, err)
break
}
// ensure that we only archive private rooms
if room.Privacy == "private" {
// get room statistics
stats, _, err := client.Room.GetStatistics(idString)
if err != nil {
log.Printf("Error: Could not retrieve statistics for room '%s': %v", room.Name, err)
break
}
// get lastactive date as a time
lastActiveTime, err := time.Parse(time.RFC3339, stats.LastActive)
if err != nil {
log.Printf("Error: Could not parse LastActive time '%s' for room '%s': %s", stats.LastActive, room.Name, err)
break
}
// if the last update was > max days ago, archive the room
if time.Now().Sub(lastActiveTime).Hours()/24 >= float64(*maxFlag) {
log.Printf("Archiving room '%s', not touched in >= %d days.", room.Name, *maxFlag)
_, err := client.Room.Update(idString, &hipchat.UpdateRoomRequest{
Name: room.Name,
IsArchived: true,
IsGuestAccess: room.IsGuestAccessible,
Owner: hipchat.ID{ID: strconv.Itoa(room.Owner.ID)},
Privacy: room.Privacy,
Topic: room.Topic,
})
if err != nil {
log.Printf("Error: Could not archive room '%s': %v", idString, err)
}
}
}
}
<-ticker.C
}
}