Skip to content

Instantly share code, notes, and snippets.

@bakins
Created January 6, 2014 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bakins/8286996 to your computer and use it in GitHub Desktop.
Save bakins/8286996 to your computer and use it in GitHub Desktop.
Silly worker to create a push queue for each chef role and add each node in that role as a subscriber. Just learning golang and Iron.IO
package main
import (
"fmt"
"github.com/bakins/chef-golang"
"github.com/bakins/iron_go/mq"
"github.com/iron-io/iron_go/config"
"os"
"encoding/json"
"io/ioutil"
)
func getPayload()(map[string]interface{}) {
payloadIndex := 0
for index, arg := range(os.Args) {
if arg == "-payload" {
payloadIndex = index + 1
}
}
if payloadIndex >= len(os.Args) {
panic("No payload value.")
}
payload := os.Args[payloadIndex]
var data map[string]interface{}
raw, err := ioutil.ReadFile(payload)
if err != nil {
panic(err.Error())
}
err = json.Unmarshal(raw, &data)
if err != nil {
panic(err.Error())
}
return data
}
func main() {
var roles = make(map[string][]string)
data := getPayload()
c, err := chef.ConnectUrl(data["chef_server_url"].(string), "11.4.0", data["chef_client_name"].(string), data["chef_client_key"].(string))
if err != nil {
panic(err)
}
config := config.Config("iron_mq")
config.ProjectId = data["project_id"].(string)
config.Token = data["token"].(string)
nodes, err := c.GetNodes()
if err != nil {
panic(err)
}
for name := range nodes {
node, ok, err := c.GetNode(name)
if err != nil {
panic(err)
}
if ok {
for _,role := range node.Info.Roles {
r := roles[role]
roles[role] = append(r, name)
}
}
}
for key, val := range roles {
q := mq.New(fmt.Sprintf("role-%s", key))
qi := mq.QueueInfo{ PushType: "multicast" }
fmt.Println(val)
for _, node := range val {
fmt.Println(node)
qi.Subscribers = append(qi.Subscribers, mq.QueueSubscriber{ URL: fmt.Sprintf("ironmq:///node-%s", node) } )
}
fmt.Println(qi)
_, err := q.Update(qi)
if err != nil {
panic(err)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment