Skip to content

Commit

Permalink
oem/packet: Add Packet report status implementation
Browse files Browse the repository at this point in the history
packet.go POST igntion status
messages to Packet.net via Packet's api url.
  • Loading branch information
Bubblemelon committed Jul 30, 2018
1 parent 1204433 commit b7856d2
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 2 deletions.
5 changes: 3 additions & 2 deletions internal/oem/oem.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ func init() {
fetch: noop.FetchConfig,
})
configs.Register(Config{
name: "packet",
fetch: packet.FetchConfig,
name: "packet",
fetch: packet.FetchConfig,
status: packet.PostStatus,
})
configs.Register(Config{
name: "pxe",
Expand Down
86 changes: 86 additions & 0 deletions internal/providers/packet/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,23 @@
package packet

import (
"bytes"
"encoding/json"
"errors"
"net/http"
"net/url"
"strings"

"github.com/coreos/ignition/config/validate/report"
"github.com/coreos/ignition/internal/config/types"
"github.com/coreos/ignition/internal/providers/util"
"github.com/coreos/ignition/internal/resource"
)

var (
ErrValidFetchEmptyData = errors.New("fetch successful but fetched data empty")
)

var (
userdataUrl = url.URL{
Scheme: "https",
Expand All @@ -34,6 +43,14 @@ var (
}
)

var (
metadataUrl = url.URL{
Scheme: "https",
Host: "metadata.packet.net",
Path: "metadata",
}
)

func FetchConfig(f resource.Fetcher) (types.Config, report.Report, error) {
// Packet's metadata service returns "Not Acceptable" when queried
// with the default Accept header.
Expand All @@ -48,3 +65,72 @@ func FetchConfig(f resource.Fetcher) (types.Config, report.Report, error) {

return util.ParseConfig(f.Logger, data)
}

// PostStatus posts a message that will show on the Packet Instance Timeline
func PostStatus(stageName string, f resource.Fetcher, errMsg error) error {
f.Logger.Info("POST message to Packet Timeline")
// fetch JSON from https://metadata.packet.net/metadata
data, err := f.FetchToBuffer(metadataUrl, resource.FetchOptions{
Headers: nil,
})
if err != nil {
return err
}
if data == nil {
return ErrValidFetchEmptyData
}
metadata := struct {
PhoneHomeURL string `json:"phone_home_url"`
}{}
err = json.Unmarshal(data, &metadata)
if err != nil {
return err
}
phonehomeURL := metadata.PhoneHomeURL
// to get phonehome IPv4
phonehomeURL = strings.TrimSuffix(phonehomeURL, "/phone-home")
// POST Message to phonehome IP
postMessageURL := phonehomeURL + "/events"

return postMessage(stageName, errMsg, postMessageURL)
}

// postMessage makes a post request with the supplied message to the url
func postMessage(stageName string, e error, url string) error {

stageName = "[" + stageName + "]"

type mStruct struct {
State string `json:"state"`
Message string `json:"message"`
}
m := mStruct{}
if e != nil {
m = mStruct{
State: "failed",
Message: stageName + " Ignition error: " + e.Error(),
}

} else {
m = mStruct{
State: "succeeded",
Message: stageName + " Ignition status: finished successfully",
}
}
messageJSON, err := json.Marshal(m)
if err != nil {
return err
}
postReq, err := http.NewRequest("POST", url, bytes.NewBuffer(messageJSON))
if err != nil {
return err
}
postReq.Header.Set("Content-Type", "application/json")
client := &http.Client{}
respPost, err := client.Do(postReq)
if err != nil {
return err
}
defer respPost.Body.Close()
return err
}

0 comments on commit b7856d2

Please sign in to comment.