Skip to content

Commit

Permalink
chore: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
vranystepan committed Dec 19, 2022
0 parents commit dd4705a
Show file tree
Hide file tree
Showing 10 changed files with 282 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
main

25 changes: 25 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <https://unlicense.org>

35 changes: 35 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"context"
"log"

"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/coreos/go-systemd/dbus"
"github.com/vranystepan/unit-reloader/internal/reloader"
"github.com/vranystepan/unit-reloader/pkg/config"
)

func main() {
cfg, err := config.New()
if err != nil {
log.Fatalf("could not load configuration: %s", err)
}

// initialize dbus client
dbusClient, err := dbus.New()
if err != nil {
log.Fatalf("could not initialize dbus client: %s", err)
}

// initialize SQS client
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
sqsClient := sqs.New(sess)

// start the main loop
ctx := context.Background()
reloader.Run(ctx, cfg, sqsClient, dbusClient)
}
15 changes: 15 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module github.com/vranystepan/unit-reloader

go 1.19

require (
github.com/aws/aws-sdk-go v1.44.162
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf
)

require (
github.com/godbus/dbus/v5 v5.0.4 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
)

replace github.com/coreos/go-systemd => github.com/coreos/go-systemd/v22 v22.5.0
46 changes: 46 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
github.com/aws/aws-sdk-go v1.44.162 h1:hKAd+X+/BLxVMzH+4zKxbQcQQGrk2UhFX0OTu1Mhon8=
github.com/aws/aws-sdk-go v1.44.162/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
github.com/coreos/go-systemd/v22 v22.5.0 h1:RrqgGjYQKalulkV8NGVIfkXQf6YYmOyiJKk8iXXhfZs=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
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/godbus/dbus/v5 v5.0.4 h1:9349emZab16e7zQvpmsbtjc18ykshndd8y2PG3sgJbA=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/pkg/errors v0.9.1/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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/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-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
48 changes: 48 additions & 0 deletions internal/reloader/reloader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package reloader

import (
"context"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/coreos/go-systemd/dbus"
"github.com/vranystepan/unit-reloader/pkg/config"
dbusImpl "github.com/vranystepan/unit-reloader/pkg/dbus"
sqsImpl "github.com/vranystepan/unit-reloader/pkg/sqs"
)

func Run(ctx context.Context, cfg *config.Config, sqsClient *sqs.SQS, dbusClient *dbus.Conn) {

// consume messages in loop
for {

output, err := sqsClient.ReceiveMessage(&sqs.ReceiveMessageInput{
QueueUrl: &cfg.QueueURL,
MaxNumberOfMessages: aws.Int64(int64(cfg.QueueMaxMessages)),
WaitTimeSeconds: aws.Int64(int64(cfg.QueueWaitTime)),
})

if err != nil {
log.Printf("could not receive SQS messages: %s", err)
}

for _, message := range output.Messages {
log.Printf("processing SQS message %s: %s", *message.MessageId, *message.Body)
// TODO: filter messages

// perform systemd action
err = dbusImpl.Do(ctx, dbusClient, cfg.UnitName, cfg.UnitAction)
if err != nil {
log.Printf("could not perform dbus action: %s", err)
}

// delete message
err = sqsImpl.Delete(sqsClient, cfg.QueueURL, message)
if err != nil {
log.Printf("could not delete message: %s", err)
}
}

}
}
50 changes: 50 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package config

import (
"flag"
"fmt"

"github.com/vranystepan/unit-reloader/pkg/util"
)

type Config struct {
QueueURL string
UnitName string
UnitAction string
QueueMaxMessages int
QueueWaitTime int
}

func New() (*Config, error) {
queueURL := flag.String("queue-url", "", "SQS queue URL")
unit := flag.String("unit", "", "systemd unit name")
action := flag.String("action", "", "systemd unit action")

flag.Parse()

// perform basic sanity checks
if *queueURL == "" {
return nil, fmt.Errorf("queue-url can't be emtpy")
}

if *unit == "" {
return nil, fmt.Errorf("unit can't be emtpy")
}

if *action == "" {
return nil, fmt.Errorf("action can't be emtpy")
}

if !util.StringInSlice(*action, []string{"start", "stop", "restart"}) {
return nil, fmt.Errorf("action can be only start, stop or restart")
}

// get the config
return &Config{
QueueURL: *queueURL,
UnitName: *unit,
UnitAction: *action,
QueueMaxMessages: 1,
QueueWaitTime: 20,
}, nil
}
36 changes: 36 additions & 0 deletions pkg/dbus/dbus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package dbus

import (
"context"
"fmt"

"github.com/coreos/go-systemd/dbus"
)

func Do(ctx context.Context, dbusClient *dbus.Conn, unit string, action string) error {
switch action {
case "start":
return Start(ctx, dbusClient, unit)
case "stop":
return Stop(ctx, dbusClient, unit)
case "restart":
return Restart(ctx, dbusClient, unit)
default:
return fmt.Errorf("action %s is not defined", action)
}
}

func Restart(ctx context.Context, dbusClient *dbus.Conn, unit string) error {
_, err := dbusClient.RestartUnitContext(ctx, unit, "replace", nil)
return err
}

func Start(ctx context.Context, dbusClient *dbus.Conn, unit string) error {
_, err := dbusClient.StartUnitContext(ctx, unit, "replace", nil)
return err
}

func Stop(ctx context.Context, dbusClient *dbus.Conn, unit string) error {
_, err := dbusClient.StopUnitContext(ctx, unit, "replace", nil)
return err
}
15 changes: 15 additions & 0 deletions pkg/sqs/ack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package sqs

import (
"github.com/aws/aws-sdk-go/service/sqs"
)

func Delete(sqsClient *sqs.SQS, queue string, message *sqs.Message) error {
request := &sqs.DeleteMessageInput{
QueueUrl: &queue,
ReceiptHandle: message.ReceiptHandle,
}

_, err := sqsClient.DeleteMessage(request)
return err
}
10 changes: 10 additions & 0 deletions pkg/util/slice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package util

func StringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}

0 comments on commit dd4705a

Please sign in to comment.