2016-05-14 10:31:43 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
type Ansible struct {
|
2016-05-14 11:21:10 +00:00
|
|
|
Nodes []string `json:"nodes"`
|
2016-05-29 19:41:58 +00:00
|
|
|
Meta struct {
|
2016-06-18 22:01:12 +00:00
|
|
|
HostVars map[string]*AnsibleHostVars `json:"hostvars,omitempty"`
|
2016-05-14 11:21:10 +00:00
|
|
|
} `json:"_meta"`
|
2016-05-14 10:31:43 +00:00
|
|
|
}
|
|
|
|
type AnsibleHostVars struct {
|
2016-05-29 19:41:58 +00:00
|
|
|
Address string `json:"ansible_ssh_host"`
|
2016-05-29 20:49:40 +00:00
|
|
|
Hostname string `json:"node_name,omitempty"`
|
2016-07-09 08:58:06 +00:00
|
|
|
Owner string `json:"owner,omitempty"`
|
2016-06-18 22:01:12 +00: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 20:49:40 +00:00
|
|
|
GeoLatitude float64 `json:"geo_latitude,omitempty"`
|
|
|
|
GeoLongitude float64 `json:"geo_longitude,omitempty"`
|
2016-05-14 10:31:43 +00:00
|
|
|
}
|
|
|
|
|
2016-05-29 19:41:58 +00:00
|
|
|
func GenerateAnsible(nodes *Nodes, aliases map[string]*Alias) *Ansible {
|
|
|
|
ansible := &Ansible{Nodes: make([]string, 0)}
|
2016-06-18 22:01:12 +00:00
|
|
|
ansible.Meta.HostVars = make(map[string]*AnsibleHostVars)
|
2016-05-29 19:41:58 +00:00
|
|
|
for nodeid, alias := range aliases {
|
2016-05-14 11:21:10 +00:00
|
|
|
if node := nodes.List[nodeid]; node != nil {
|
2016-05-14 10:31:43 +00:00
|
|
|
|
2016-05-29 19:41:58 +00:00
|
|
|
ansible.Nodes = append(ansible.Nodes, nodeid)
|
2016-05-14 10:31:43 +00:00
|
|
|
|
2016-05-14 11:21:10 +00:00
|
|
|
vars := &AnsibleHostVars{
|
2016-05-29 20:49:40 +00:00
|
|
|
Hostname: alias.Hostname,
|
2016-07-09 08:58:06 +00:00
|
|
|
Owner: alias.Owner,
|
2016-05-29 20:49:40 +00:00
|
|
|
}
|
2016-07-09 17:14:04 +00:00
|
|
|
if node.Nodeinfo.Network.Addresses != nil {
|
|
|
|
vars.Address = node.Nodeinfo.Network.Addresses[0]
|
|
|
|
}
|
2016-06-28 22:04:33 +00: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 20:49:40 +00:00
|
|
|
}
|
|
|
|
if alias.Location != nil {
|
|
|
|
vars.GeoLatitude = alias.Location.Latitude
|
|
|
|
vars.GeoLongitude = alias.Location.Longtitude
|
2016-05-14 11:21:10 +00:00
|
|
|
}
|
2016-06-18 22:00:18 +00:00
|
|
|
ansible.Meta.HostVars[nodeid] = vars
|
2016-05-14 10:31:43 +00:00
|
|
|
|
2016-05-14 11:21:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ansible
|
2016-05-14 10:31:43 +00:00
|
|
|
}
|