-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.go
149 lines (124 loc) · 2.93 KB
/
functions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package ssh
import (
"bytes"
"context"
"fmt"
"github.com/digitalocean/godo"
"github.com/kamushadenes/swayit/common"
"github.com/kamushadenes/swayit/config"
"github.com/kevinburke/ssh_config"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"path"
"sort"
"strings"
)
func DropletList(ctx context.Context, client *godo.Client) ([]godo.Droplet, error) {
// create a list to hold our droplets
var list []godo.Droplet
// create options. initially, these will be blank
opt := &godo.ListOptions{}
for {
droplets, resp, err := client.Droplets.List(ctx, opt)
if err != nil {
return nil, err
}
// append the current page's droplets to our list
list = append(list, droplets...)
// if we are at the last page, break out the for loop
if resp.Links == nil || resp.Links.IsLastPage() {
break
}
page, err := resp.Links.CurrentPage()
if err != nil {
return nil, err
}
// set the page we want for the next request
opt.Page = page + 1
}
return list, nil
}
func UpdateHosts() error {
err := UpdateDigitalOceanHosts()
if err != nil {
return err
}
return nil
}
func UpdateDigitalOceanHosts() error {
home, err := os.UserHomeDir()
if err != nil {
return err
}
var byRegion = make(map[string][]godo.Droplet)
for _, v := range config.SwayItConfig.SSH.DigitalOcean.Contexts {
client := godo.NewFromToken(v.Token)
droplets, err := DropletList(context.Background(), client)
if err != nil {
return err
}
for k := range droplets {
byRegion[droplets[k].Region.Slug] = append(byRegion[droplets[k].Region.Slug], droplets[k])
}
}
for region := range byRegion {
var hostsFile HostsFile
hostsFile.Hosts = make(map[string]*HostEntry)
for _, droplet := range byRegion[region] {
ip, err := droplet.PublicIPv4()
if err != nil {
common.Logger.Error().Err(err).Msg("an error has occurred")
}
hostsFile.Hosts[droplet.Name] = &HostEntry{
Hostname: ip,
User: "root",
}
}
b, err := yaml.Marshal(&hostsFile)
if err != nil {
return err
}
err = ioutil.WriteFile(path.Join(home, ".ssh", "digitalocean", fmt.Sprintf("%s.yml", region)), b, 0644)
if err != nil {
return err
}
}
return nil
}
func GetHosts() ([]string, error) {
home, err := os.UserHomeDir()
if err != nil {
return nil, err
}
b, err := ioutil.ReadFile(path.Join(home, ".ssh", "config"))
if err != nil {
return nil, err
}
cnf, err := ssh_config.Decode(bytes.NewReader(b))
if err != nil {
return nil, err
}
var hosts = make(map[string]bool)
for _, v := range cnf.Hosts {
for _, p := range v.Patterns {
host := p.String()
if !strings.Contains(host, "*") {
if strings.Contains(host, "@") {
host = strings.Split(host, "@")[1]
}
if host != "github.com" {
hosts[host] = true
}
}
}
}
var hostsStr []string
for k := range hosts {
hostsStr = append(hostsStr, k)
}
sort.Slice(hostsStr, func(i, j int) bool {
return hostsStr[i] < hostsStr[j]
})
return hostsStr, nil
}