Display hostname in notifications (#102)

This commit is contained in:
CrazyMax
2020-06-22 03:02:29 +02:00
parent c0e3f7e85f
commit 5033adefd7
13 changed files with 84 additions and 23 deletions

View File

@@ -32,9 +32,14 @@ var (
) )
func main() { func main() {
var err error
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())
meta.Version = version meta.Version = version
meta.UserAgent = fmt.Sprintf("%s/%s go/%s %s", meta.ID, meta.Version, runtime.Version()[2:], strings.Title(runtime.GOOS)) meta.UserAgent = fmt.Sprintf("%s/%s go/%s %s", meta.ID, meta.Version, runtime.Version()[2:], strings.Title(runtime.GOOS))
if meta.Hostname, err = os.Hostname(); err != nil {
log.Fatal().Err(err).Msg("Cannot resolve hostname")
}
// Parse command line // Parse command line
_ = kong.Parse(&cli, _ = kong.Parse(&cli,
@@ -52,7 +57,7 @@ func main() {
// Load timezone location // Load timezone location
location, err := time.LoadLocation(cli.Timezone) location, err := time.LoadLocation(cli.Timezone)
if err != nil { if err != nil {
log.Panic().Err(err).Msgf("Cannot load timezone %s", cli.Timezone) log.Fatal().Err(err).Msgf("Cannot load timezone %s", cli.Timezone)
} }
// Init // Init

View File

@@ -43,6 +43,7 @@ The JSON response will look like this:
```json ```json
{ {
"diun_version": "0.3.0", "diun_version": "0.3.0",
"hostname": "myserver",
"status": "new", "status": "new",
"provider": "file", "provider": "file",
"image": "docker.io/crazymax/swarm-cronjob:0.2.1", "image": "docker.io/crazymax/swarm-cronjob:0.2.1",

View File

@@ -34,6 +34,7 @@ The JSON response will look like this:
```json ```json
{ {
"diun_version": "4.0.0", "diun_version": "4.0.0",
"hostname": "myserver",
"status": "new", "status": "new",
"provider": "file", "provider": "file",
"image": "docker.io/crazymax/diun:latest", "image": "docker.io/crazymax/diun:latest",

View File

@@ -10,4 +10,5 @@ type Meta struct {
Author string Author string
Version string Version string
UserAgent string UserAgent string
Hostname string
} }

View File

@@ -72,6 +72,7 @@ func (c *Client) Send(entry model.NotifEntry) error {
body, err := json.Marshal(struct { body, err := json.Marshal(struct {
Version string `json:"diun_version"` Version string `json:"diun_version"`
Hostname string `json:"hostname"`
Status string `json:"status"` Status string `json:"status"`
Provider string `json:"provider"` Provider string `json:"provider"`
Image string `json:"image"` Image string `json:"image"`
@@ -82,6 +83,7 @@ func (c *Client) Send(entry model.NotifEntry) error {
Platform string `json:"platform"` Platform string `json:"platform"`
}{ }{
Version: c.meta.Version, Version: c.meta.Version,
Hostname: c.meta.Hostname,
Status: string(entry.Status), Status: string(entry.Status),
Provider: entry.Provider, Provider: entry.Provider,
Image: entry.Image.String(), Image: entry.Image.String(),

View File

@@ -47,14 +47,20 @@ func (c *Client) Send(entry model.NotifEntry) error {
title = fmt.Sprintf("New image %s has been added", entry.Image.String()) title = fmt.Sprintf("New image %s has been added", entry.Image.String())
} }
tagTpl := "`{{ .Image.Domain }}/{{ .Image.Path }}:{{ .Image.Tag }}`" tagTpl := "`{{ .Entry.Image.Domain }}/{{ .Entry.Image.Path }}:{{ .Entry.Image.Tag }}`"
if len(entry.Image.HubLink) > 0 { if len(entry.Image.HubLink) > 0 {
tagTpl = "[`{{ .Image.Domain }}/{{ .Image.Path }}:{{ .Image.Tag }}`]({{ .Image.HubLink }})" tagTpl = "[`{{ .Entry.Image.Domain }}/{{ .Entry.Image.Path }}:{{ .Entry.Image.Tag }}`]({{ .Entry.Image.HubLink }})"
} }
var msgBuf bytes.Buffer var msgBuf bytes.Buffer
msgTpl := template.Must(template.New("gotify").Parse(fmt.Sprintf("Docker tag %s which you subscribed to through {{ .Provider }} provider has been {{ if (eq .Status \"new\") }}newly added{{ else }}updated{{ end }}.", tagTpl))) msgTpl := template.Must(template.New("gotify").Parse(fmt.Sprintf("Docker tag %s which you subscribed to through {{ .Entry.Provider }} provider has been {{ if (eq .Entry.Status \"new\") }}newly added{{ else }}updated{{ end }} on {{ .Meta.Hostname }}.", tagTpl)))
if err := msgTpl.Execute(&msgBuf, entry); err != nil { if err := msgTpl.Execute(&msgBuf, struct {
Meta model.Meta
Entry model.NotifEntry
}{
Meta: c.meta,
Entry: entry,
}); err != nil {
return err return err
} }

View File

@@ -59,24 +59,30 @@ func (c *Client) Send(entry model.NotifEntry) error {
subject = fmt.Sprintf("New image %s has been added", entry.Image.String()) subject = fmt.Sprintf("New image %s has been added", entry.Image.String())
} }
tagTpl := "**{{ .Image.Domain }}/{{ .Image.Path }}:{{ .Image.Tag }}**" tagTpl := "**{{ .Entry.Image.Domain }}/{{ .Entry.Image.Path }}:{{ .Entry.Image.Tag }}**"
if len(entry.Image.HubLink) > 0 { if len(entry.Image.HubLink) > 0 {
tagTpl = "[**{{ .Image.Domain }}/{{ .Image.Path }}:{{ .Image.Tag }}**]({{ .Image.HubLink }})" tagTpl = "[**{{ .Entry.Image.Domain }}/{{ .Entry.Image.Path }}:{{ .Entry.Image.Tag }}**]({{ .Entry.Image.HubLink }})"
} }
// Body // Body
var emailBuf bytes.Buffer var emailBuf bytes.Buffer
emailTpl := template.Must(template.New("email").Parse(fmt.Sprintf(` emailTpl := template.Must(template.New("email").Parse(fmt.Sprintf(`
Docker tag %s which you subscribed to through **{{ .Provider }}** provider has been {{ if (eq .Status "new") }}newly added{{ else }}updated{{ end }}. Docker tag %s which you subscribed to through **{{ .Entry.Provider }}** provider has been {{ if (eq .Entry.Status "new") }}newly added{{ else }}updated{{ end }} on **{{ .Meta.Hostname }}**.
This image has been {{ if (eq .Status "new") }}created{{ else }}updated{{ end }} at <code>{{ .Manifest.Created.Format "Jan 02, 2006 15:04:05 UTC" }}</code> This image has been {{ if (eq .Entry.Status "new") }}created{{ else }}updated{{ end }} at <code>{{ .Entry.Manifest.Created.Format "Jan 02, 2006 15:04:05 UTC" }}</code>
with digest <code>{{ .Manifest.Digest }}</code> for <code>{{ .Manifest.Platform }}</code> platform. with digest <code>{{ .Entry.Manifest.Digest }}</code> for <code>{{ .Entry.Manifest.Platform }}</code> platform.
Need help, or have questions? Go to https://github.com/crazy-max/diun and leave an issue. Need help, or have questions? Go to https://github.com/crazy-max/diun and leave an issue.
`, tagTpl))) `, tagTpl)))
if err := emailTpl.Execute(&emailBuf, entry); err != nil { if err := emailTpl.Execute(&emailBuf, struct {
Meta model.Meta
Entry model.NotifEntry
}{
Meta: c.meta,
Entry: entry,
}); err != nil {
return err return err
} }
email := hermes.Email{ email := hermes.Email{

View File

@@ -50,12 +50,23 @@ func (c *Client) Send(entry model.NotifEntry) error {
} }
var textBuf bytes.Buffer var textBuf bytes.Buffer
textTpl := template.Must(template.New("rocketchat").Parse(`Docker tag {{ .Image.Domain }}/{{ .Image.Path }}:{{ .Image.Tag }} which you subscribed to through {{ .Provider }} provider has been {{ if (eq .Status "new") }}newly added{{ else }}updated{{ end }}.`)) textTpl := template.Must(template.New("rocketchat").Parse(`Docker tag {{ .Entry.Image.Domain }}/{{ .Entry.Image.Path }}:{{ .Entry.Image.Tag }} which you subscribed to through {{ .Entry.Provider }} provider has been {{ if (eq .Entry.Status "new") }}newly added{{ else }}updated{{ end }}.`))
if err := textTpl.Execute(&textBuf, entry); err != nil { if err := textTpl.Execute(&textBuf, struct {
Meta model.Meta
Entry model.NotifEntry
}{
Meta: c.meta,
Entry: entry,
}); err != nil {
return err return err
} }
fields := []AttachmentField{ fields := []AttachmentField{
{
Title: "Hostname",
Value: c.meta.Hostname,
Short: false,
},
{ {
Title: "Provider", Title: "Provider",
Value: entry.Provider, Value: entry.Provider,

View File

@@ -53,6 +53,7 @@ func (c *Client) Send(entry model.NotifEntry) error {
// Set env vars // Set env vars
cmd.Env = append(os.Environ(), []string{ cmd.Env = append(os.Environ(), []string{
fmt.Sprintf("DIUN_VERSION=%s", c.meta.Version), fmt.Sprintf("DIUN_VERSION=%s", c.meta.Version),
fmt.Sprintf("DIUN_HOSTNAME=%s", c.meta.Hostname),
fmt.Sprintf("DIUN_ENTRY_STATUS=%s", string(entry.Status)), fmt.Sprintf("DIUN_ENTRY_STATUS=%s", string(entry.Status)),
fmt.Sprintf("DIUN_ENTRY_PROVIDER=%s", entry.Provider), fmt.Sprintf("DIUN_ENTRY_PROVIDER=%s", entry.Provider),
fmt.Sprintf("DIUN_ENTRY_IMAGE=%s", entry.Image.String()), fmt.Sprintf("DIUN_ENTRY_IMAGE=%s", entry.Image.String()),

View File

@@ -38,8 +38,14 @@ func (c *Client) Name() string {
// Send creates and sends a slack notification with an entry // Send creates and sends a slack notification with an entry
func (c *Client) Send(entry model.NotifEntry) error { func (c *Client) Send(entry model.NotifEntry) error {
var textBuf bytes.Buffer var textBuf bytes.Buffer
textTpl := template.Must(template.New("text").Parse("<!channel> Docker tag `{{ .Image.Domain }}/{{ .Image.Path }}:{{ .Image.Tag }}` {{ if (eq .Status \"new\") }}newly added{{ else }}updated{{ end }}.")) textTpl := template.Must(template.New("text").Parse("<!channel> Docker tag `{{ .Entry.Image.Domain }}/{{ .Entry.Image.Path }}:{{ .Entry.Image.Tag }}` {{ if (eq .Entry.Status \"new\") }}newly added{{ else }}updated{{ end }}."))
if err := textTpl.Execute(&textBuf, entry); err != nil { if err := textTpl.Execute(&textBuf, struct {
Meta model.Meta
Entry model.NotifEntry
}{
Meta: c.meta,
Entry: entry,
}); err != nil {
return err return err
} }
@@ -49,6 +55,11 @@ func (c *Client) Send(entry model.NotifEntry) error {
} }
fields := []slack.AttachmentField{ fields := []slack.AttachmentField{
{
Title: "Hostname",
Value: c.meta.Hostname,
Short: false,
},
{ {
Title: "Provider", Title: "Provider",
Value: entry.Provider, Value: entry.Provider,

View File

@@ -53,14 +53,20 @@ func (c *Client) Send(entry model.NotifEntry) error {
Timeout: time.Duration(10) * time.Second, Timeout: time.Duration(10) * time.Second,
} }
tagTpl := "`{{ .Image.Domain }}/{{ .Image.Path }}:{{ .Image.Tag }}`" tagTpl := "`{{ .Entry.Image.Domain }}/{{ .Entry.Image.Path }}:{{ .Entry.Image.Tag }}`"
if len(entry.Image.HubLink) > 0 { if len(entry.Image.HubLink) > 0 {
tagTpl = "[`{{ .Image.Domain }}/{{ .Image.Path }}:{{ .Image.Tag }}`]({{ .Image.HubLink }})" tagTpl = "[`{{ .Entry.Image.Domain }}/{{ .Entry.Image.Path }}:{{ .Entry.Image.Tag }}`]({{ .Entry.Image.HubLink }})"
} }
var textBuf bytes.Buffer var textBuf bytes.Buffer
textTpl := template.Must(template.New("text").Parse(fmt.Sprintf("Docker tag %s {{ if (eq .Status \"new\") }}newly added{{ else }}updated{{ end }}.", tagTpl))) textTpl := template.Must(template.New("text").Parse(fmt.Sprintf("Docker tag %s {{ if (eq .Entry.Status \"new\") }}newly added{{ else }}updated{{ end }}.", tagTpl)))
if err := textTpl.Execute(&textBuf, entry); err != nil { if err := textTpl.Execute(&textBuf, struct {
Meta model.Meta
Entry model.NotifEntry
}{
Meta: c.meta,
Entry: entry,
}); err != nil {
return err return err
} }
@@ -84,6 +90,8 @@ func (c *Client) Send(entry model.NotifEntry) error {
ActivityTitle: textBuf.String(), ActivityTitle: textBuf.String(),
ActivitySubtitle: "Provider: " + entry.Provider, ActivitySubtitle: "Provider: " + entry.Provider,
Facts: []Fact{ Facts: []Fact{
{"Hostname", c.meta.Hostname},
{"Provider", entry.Provider},
{"Created", entry.Manifest.Created.Format("Jan 02, 2006 15:04:05 UTC")}, {"Created", entry.Manifest.Created.Format("Jan 02, 2006 15:04:05 UTC")},
{"Digest", entry.Manifest.Digest.String()}, {"Digest", entry.Manifest.Digest.String()},
{"Platform", entry.Manifest.Platform}, {"Platform", entry.Manifest.Platform},

View File

@@ -39,14 +39,20 @@ func (c *Client) Send(entry model.NotifEntry) error {
return err return err
} }
tagTpl := "{{ .Image.Domain }}/{{ .Image.Path }}:{{ .Image.Tag }}" tagTpl := "{{ .Entry.Image.Domain }}/{{ .Entry.Image.Path }}:{{ .Entry.Image.Tag }}"
if len(entry.Image.HubLink) > 0 { if len(entry.Image.HubLink) > 0 {
tagTpl = "[{{ .Image.Domain }}/{{ .Image.Path }}:{{ .Image.Tag }}]({{ .Image.HubLink }})" tagTpl = "[{{ .Entry.Image.Domain }}/{{ .Entry.Image.Path }}:{{ .Entry.Image.Tag }}]({{ .Entry.Image.HubLink }})"
} }
var msgBuf bytes.Buffer var msgBuf bytes.Buffer
msgTpl := template.Must(template.New("email").Parse(fmt.Sprintf("Docker tag %s which you subscribed to through {{ .Provider }} provider has been {{ if (eq .Status \"new\") }}newly added{{ else }}updated{{ end }}.", tagTpl))) msgTpl := template.Must(template.New("email").Parse(fmt.Sprintf("Docker tag %s which you subscribed to through {{ .Entry.Provider }} provider has been {{ if (eq .Entry.Status \"new\") }}newly added{{ else }}updated{{ end }} on {{ .Meta.Hostname }}.", tagTpl)))
if err := msgTpl.Execute(&msgBuf, entry); err != nil { if err := msgTpl.Execute(&msgBuf, struct {
Meta model.Meta
Entry model.NotifEntry
}{
Meta: c.meta,
Entry: entry,
}); err != nil {
return err return err
} }

View File

@@ -41,6 +41,7 @@ func (c *Client) Send(entry model.NotifEntry) error {
body, err := json.Marshal(struct { body, err := json.Marshal(struct {
Version string `json:"diun_version"` Version string `json:"diun_version"`
Hostname string `json:"hostname"`
Status string `json:"status"` Status string `json:"status"`
Provider string `json:"provider"` Provider string `json:"provider"`
Image string `json:"image"` Image string `json:"image"`
@@ -51,6 +52,7 @@ func (c *Client) Send(entry model.NotifEntry) error {
Platform string `json:"platform"` Platform string `json:"platform"`
}{ }{
Version: c.meta.Version, Version: c.meta.Version,
Hostname: c.meta.Hostname,
Status: string(entry.Status), Status: string(entry.Status),
Provider: entry.Provider, Provider: entry.Provider,
Image: entry.Image.String(), Image: entry.Image.String(),