yanic/models/ansible.go

49 lines
1.4 KiB
Go
Raw Normal View History

2016-05-14 12:31:43 +02:00
package models
type Ansible struct {
2016-05-14 13:21:10 +02:00
Nodes []string `json:"nodes"`
2016-05-29 21:41:58 +02:00
Meta struct {
2016-05-14 13:21:10 +02:00
HostVars []*AnsibleHostVars `json:"hostvars"`
} `json:"_meta"`
2016-05-14 12:31:43 +02:00
}
type AnsibleHostVars struct {
2016-05-29 21:41:58 +02:00
Address string `json:"ansible_ssh_host"`
2016-05-29 22:49:40 +02:00
Hostname string `json:"node_name,omitempty"`
Channel24 int `json:"radio24_channel,omitempty"`
TxPower24 int `json:"radio24_txpower,omitempty"`
Channel5 int `json:"radio5_channel,omitempty"`
TxPower5 int `json:"radio5_txpower,omitempty"`
GeoLatitude float64 `json:"geo_latitude,omitempty"`
GeoLongitude float64 `json:"geo_longitude,omitempty"`
2016-05-14 12:31:43 +02:00
}
2016-05-29 21:41:58 +02:00
func GenerateAnsible(nodes *Nodes, aliases map[string]*Alias) *Ansible {
ansible := &Ansible{Nodes: make([]string, 0)}
for nodeid, alias := range aliases {
2016-05-14 13:21:10 +02:00
if node := nodes.List[nodeid]; node != nil {
2016-05-14 12:31:43 +02:00
2016-05-29 21:41:58 +02:00
ansible.Nodes = append(ansible.Nodes, nodeid)
2016-05-14 12:31:43 +02:00
2016-05-14 13:21:10 +02:00
vars := &AnsibleHostVars{
2016-05-29 22:49:40 +02:00
Address: node.Nodeinfo.Network.Addresses[0],
Hostname: alias.Hostname,
}
if alias.Freq24 != nil {
vars.Channel24 = alias.Freq24.Channel
vars.TxPower24 = alias.Freq24.TxPower
}
if alias.Freq5 != nil {
vars.Channel5 = alias.Freq5.Channel
vars.TxPower5 = alias.Freq5.TxPower
}
if alias.Location != nil {
vars.GeoLatitude = alias.Location.Latitude
vars.GeoLongitude = alias.Location.Longtitude
2016-05-14 13:21:10 +02:00
}
2016-05-29 21:41:58 +02:00
ansible.Meta.HostVars = append(ansible.Meta.HostVars, vars)
2016-05-14 12:31:43 +02:00
2016-05-14 13:21:10 +02:00
}
}
return ansible
2016-05-14 12:31:43 +02:00
}