Skip to content

Instantly share code, notes, and snippets.

@mark-kubacki
Last active September 21, 2023 08:02
Show Gist options
  • Save mark-kubacki/4a921d2bdf50fc986cdfc7d0edeb13ef to your computer and use it in GitHub Desktop.
Save mark-kubacki/4a921d2bdf50fc986cdfc7d0edeb13ef to your computer and use it in GitHub Desktop.
notify me to running servers on Scaleway
package main
import (
"fmt"
"os"
notify "github.com/TheCreeper/go-notify"
instance "github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
yaml "gopkg.in/yaml.v3"
)
// Format of the file stored as ~/.config/scw/config.yaml
type ScwConfig struct {
AccessKey string `yaml:"access_key,omitempty"`
SecretKey string `yaml:"secret_key,omitempty"`
DefaultOrganizationID string `yaml:"default_organization_id,omitempty"`
DefaultProjectID string `yaml:"default_project_id"`
DefaultRegion string `yaml:"default_region,omitempty"`
DefaultZone string `yaml:"default_zone,omitempty"`
ApiUrl string `yaml:"api_url,omitempty"`
Insecure bool `yaml:"insecure,omitempty"`
ActiveProfile string `yaml:"active_profile,omitempty"`
SendTelemetry bool `yaml:"send_telemetry,omitempty"`
}
func ReadConfig() (*ScwConfig, error) {
configPath := scw.GetConfigPath()
f, err := os.Open(configPath)
if err != nil {
return nil, err
}
defer f.Close()
cfg := new(ScwConfig)
dec := yaml.NewDecoder(f)
err = dec.Decode(cfg)
return cfg, err
}
func main() {
cfg, err := ReadConfig()
if err != nil {
panic(err)
}
client, err := scw.NewClient(
// Get your credentials at https://console.scaleway.com/project/credentials
scw.WithDefaultOrganizationID(cfg.DefaultOrganizationID),
scw.WithDefaultProjectID(cfg.DefaultProjectID),
scw.WithAuth(cfg.AccessKey, cfg.SecretKey),
)
if err != nil {
panic(err)
}
instanceApi := instance.NewAPI(client)
zone := scw.ZoneNlAms1
if z, err := scw.ParseZone(cfg.DefaultZone); err == nil {
zone = z
} else {
fmt.Printf("WARN: Zone '%s' is not recognized - did you mean '%s-1'?",
cfg.DefaultZone, cfg.DefaultRegion)
}
response, err := instanceApi.ListServers(&instance.ListServersRequest{
Zone: zone,
})
if err != nil {
panic(err)
}
if len(response.Servers) == 0 {
fmt.Printf("Exiting. No servers in zone: %s\n", zone)
return
}
n := notify.NewNotification(
"Servers on Scaleway",
fmt.Sprintf("Number of servers that are running: %d",
len(response.Servers)),
)
n.AppName = "hygienic.fun/homeoffice/server-notifier"
n.AppIcon = "network-workgroup"
n.Hints = make(map[string]interface{}, 1)
n.Hints[notify.HintUrgency] = notify.UrgencyNormal
n.Show()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment