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-06-19 00:01:12 +02:00
|
|
|
HostVars map[string]*AnsibleHostVars `json:"hostvars,omitempty"`
|
2016-05-14 13:21:10 +02:00
|
|
|
} `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"`
|
2016-07-09 10:58:06 +02:00
|
|
|
Owner string `json:"owner,omitempty"`
|
2016-06-19 00:01:12 +02:00
|
|
|
Channel24 uint32 `json:"radio24_channel,omitempty"`
|
|
|
|
TxPower24 uint32 `json:"radio24_txpower,omitempty"`
|
|
|
|
Channel5 uint32 `json:"radio5_channel,omitempty"`
|
|
|
|
TxPower5 uint32 `json:"radio5_txpower,omitempty"`
|
2016-05-29 22:49:40 +02:00
|
|
|
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)}
|
2016-06-19 00:01:12 +02:00
|
|
|
ansible.Meta.HostVars = make(map[string]*AnsibleHostVars)
|
2016-05-29 21:41:58 +02:00
|
|
|
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
|
|
|
Hostname: alias.Hostname,
|
2016-07-09 10:58:06 +02:00
|
|
|
Owner: alias.Owner,
|
2016-05-29 22:49:40 +02:00
|
|
|
}
|
2016-07-09 19:14:04 +02:00
|
|
|
if node.Nodeinfo.Network.Addresses != nil {
|
|
|
|
vars.Address = node.Nodeinfo.Network.Addresses[0]
|
|
|
|
}
|
2016-06-29 00:04:33 +02:00
|
|
|
if alias.Wireless != nil {
|
|
|
|
vars.Channel24 = alias.Wireless.Channel24
|
|
|
|
vars.TxPower24 = alias.Wireless.TxPower24
|
|
|
|
vars.Channel5 = alias.Wireless.Channel5
|
|
|
|
vars.TxPower5 = alias.Wireless.TxPower5
|
2016-05-29 22:49:40 +02:00
|
|
|
}
|
|
|
|
if alias.Location != nil {
|
|
|
|
vars.GeoLatitude = alias.Location.Latitude
|
|
|
|
vars.GeoLongitude = alias.Location.Longtitude
|
2016-05-14 13:21:10 +02:00
|
|
|
}
|
2016-06-19 00:00:18 +02:00
|
|
|
ansible.Meta.HostVars[nodeid] = 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
|
|
|
}
|