feat: initial commit

This commit is contained in:
Yukine 2021-02-04 00:25:10 +01:00
parent 8c22c2c1ee
commit 80fdeb1320
No known key found for this signature in database
GPG Key ID: 6AE0B5764F0126D1
6 changed files with 291 additions and 0 deletions

View File

@ -0,0 +1,33 @@
package main
import (
"context"
"github.com/digitalocean/godo"
)
// DigitalOceanContext is an helper struct to acess Digital Ocean actions
type DigitalOceanContext struct {
client *godo.Client
ctx context.Context
}
// GetVolume gets Volume by na
func (d DigitalOceanContext) GetVolume(id string) (*godo.Volume, *godo.Response, error) {
return d.client.Storage.GetVolume(d.ctx, id)
}
// CreateSnapshot creates a snapshot from a Volume
func (d DigitalOceanContext) CreateSnapshot(options *godo.SnapshotCreateRequest) (*godo.Snapshot, *godo.Response, error) {
return d.client.Storage.CreateSnapshot(d.ctx, options)
}
// ListSnapshots lists all snapshots from a volume
func (d DigitalOceanContext) ListSnapshots(volumeID string, opts *godo.ListOptions) ([]godo.Snapshot, *godo.Response, error) {
return d.client.Storage.ListSnapshots(d.ctx, volumeID, opts)
}
// DeleteSnapshot Deletes a snaphot by id
func (d DigitalOceanContext) DeleteSnapshot(id string) (*godo.Response, error) {
return d.client.Storage.DeleteSnapshot(d.ctx, id)
}

View File

@ -0,0 +1,146 @@
package main
import (
"context"
"fmt"
"os"
"sort"
"strconv"
"strings"
"time"
"github.com/digitalocean/godo"
log "github.com/sirupsen/logrus"
"github.com/slack-go/slack"
)
const createdAtFormat = "2006-01-02T15:04:05Z0700"
type snapshotterContext struct {
DoContext *DigitalOceanContext
SlackContext *SlackContext
}
func initLogging() {
log.SetFormatter(&log.TextFormatter{
DisableLevelTruncation: true,
})
log.SetOutput(os.Stdout)
log.SetLevel(log.WarnLevel)
}
func main() {
initLogging()
DOToken, present := os.LookupEnv("DO_TOKEN")
if present == false {
log.Fatal("Missing enviroment variable \"DO_TOKEN\"")
}
volumesEnv, present := os.LookupEnv("DO_VOLUMES")
if present == false {
log.Fatal("Missing enviroment variable \"DO_VOLUMES\"")
}
snapshotCountEnv, present := os.LookupEnv("DO_SNAPSHOT_COUNT")
if present == false {
log.Fatal("Missing enviroment variable \"DO_SNAPSHOT_COUNT\"")
}
snapshotCount, err := strconv.Atoi(snapshotCountEnv)
if err != nil {
log.Fatal("Enviroment variable \"DO_SNAPSHOT_COUNT\" is not an integer")
}
slackEnv := os.Getenv("SLACK_TOKEN")
var slackContext *SlackContext = nil
if slackEnv != "" {
channelID, present := os.LookupEnv("SLACK_CHANNEL_ID")
if present == false {
log.Fatal("Missing enviroment variable \"SLACK_CHANNEL_ID\"")
}
slackContext = &SlackContext{
client: slack.New(slackEnv),
channelID: channelID,
}
}
ctx := snapshotterContext{
DoContext: &DigitalOceanContext{
client: godo.NewFromToken(DOToken),
ctx: context.TODO(),
},
SlackContext: slackContext,
}
volumeIDs := strings.Split(volumesEnv, ",")
for _, volumeID := range volumeIDs {
volume, _, err := ctx.DoContext.GetVolume(volumeID)
if err != nil {
errorHandling(err, ctx)
return
}
_, _, err = ctx.DoContext.CreateSnapshot(&godo.SnapshotCreateRequest{
VolumeID: volume.ID,
Name: fmt.Sprintf("%s-%s", volume.Name, time.Now().Format("2006-01-02 15:04:05")),
})
if err != nil {
errorHandling(err, ctx)
return
}
snapshots, _, err := ctx.DoContext.ListSnapshots(volume.ID, nil)
snapshotLength := len(snapshots)
if snapshotLength > snapshotCount {
sort.SliceStable(snapshots, func(firstIndex, secondIndex int) bool {
firstTime, err := time.Parse(snapshots[firstIndex].Created, createdAtFormat)
if err != nil {
errorHandling(err, ctx)
}
secondTime, err := time.Parse(snapshots[firstIndex].Created, createdAtFormat)
if err != nil {
errorHandling(err, ctx)
}
return firstTime.Before(secondTime)
})
snapshotsToDelete := snapshotLength - snapshotCount
for i := 0; i < snapshotsToDelete; i++ {
_, err := ctx.DoContext.DeleteSnapshot(snapshots[i].ID)
if err != nil {
errorHandling(err, ctx)
return
}
}
}
}
}
func errorHandling(err error, ctx snapshotterContext) {
log.Error(err.Error())
if ctx.SlackContext != nil {
err = ctx.SlackContext.SendEvent(err.Error(), log.ErrorLevel)
if err != nil {
log.Error(err.Error())
}
}
}

View File

@ -0,0 +1,30 @@
package main
import (
log "github.com/sirupsen/logrus"
"github.com/slack-go/slack"
)
// OutputSource is an abstraction for outputting specific events to other services (e.g. Discord, Slack or Whatsapp)
type OutputSource interface {
SendEvent(string, log.Level) error
}
// SendEvent forwards event to Slack
func (s SlackContext) SendEvent(content string, level log.Level) error {
color := "#00FF00"
if level == log.ErrorLevel {
color = "#FF0000"
}
return s.SendMessageWithEmbed(slack.Attachment{
Color: color,
AuthorName: "DigitalOceanSnapshotter",
AuthorIcon: "https://cdn.top.gg/icons/DO_Logo_icon_blue.png",
Text: content,
Title: "DigitalOceanSnapshotter",
TitleLink: "https://github.com/top-gg/DigitalOceanSnapshotter",
})
}

View File

@ -0,0 +1,21 @@
package main
import "github.com/slack-go/slack"
// SlackContext is an helper struct to acess slack actions
type SlackContext struct {
client *slack.Client
channelID string
}
// SendMessageWithContent sends a message with content to the pre defined channel
func (s SlackContext) SendMessageWithContent(content string) error {
_, _, _, err := s.client.SendMessage(s.channelID, slack.MsgOptionText(content, true))
return err
}
// SendMessageWithEmbed sends a message with a Rich Embed to the pre defined channel
func (s SlackContext) SendMessageWithEmbed(attachment slack.Attachment) error {
_, _, _, err := s.client.SendMessage(s.channelID, slack.MsgOptionAttachments(attachment))
return err
}

9
go.mod Normal file
View File

@ -0,0 +1,9 @@
module github.com/top-gg/DigitalOceanSnapshotter
go 1.15
require (
github.com/digitalocean/godo v1.57.0
github.com/sirupsen/logrus v1.7.0
github.com/slack-go/slack v0.8.0
)

52
go.sum Normal file
View File

@ -0,0 +1,52 @@
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/digitalocean/godo v1.57.0 h1:uCpe0sRIZ/sJWxWDsJyBPBjUfSvxop+WHkHiSf+tjjM=
github.com/digitalocean/godo v1.57.0/go.mod h1:p7dOjjtSBqCTUksqtA5Fd3uaKs9kyTq2xcz76ulEJRU=
github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho=
github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.5 h1:F768QJ1E9tib+q5Sc8MkdJi1RxLTbRcTf8LJV56aRls=
github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/slack-go/slack v0.8.0 h1:ANyLY5KHLV+MxLJDQum2IuHTLwbCbDtaWY405X1EU9U=
github.com/slack-go/slack v0.8.0/go.mod h1:FGqNzJBmxIsZURAxh2a8D21AnOVvvXZvGligs4npPUM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e h1:3G+cUijn7XD+S4eJFddp53Pv7+slrESplyjG25HgL+k=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d h1:TzXSXBo42m9gQenoE3b9BGiEpg5IG2JkU5FkPIawgtw=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.6.5 h1:tycE03LOZYQNhDpS27tcQdAzLCVMaj7QT2SXxebnpCM=
google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=