feat: implement server init

This commit is contained in:
decentral1se 2021-08-02 01:03:27 +02:00
parent 6be54c670a
commit 19d435c5e5
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
2 changed files with 40 additions and 1 deletions

View File

@ -12,7 +12,7 @@
- [ ] `capsul`
- [ ] `hetzner`
- [x] `rm`
- [ ] `init`
- [x] `init`
- [ ] `abra app`
- [x] `ls`
- [x] `new`

View File

@ -1,10 +1,14 @@
package cli
import (
"context"
"fmt"
"net"
"coopcloud.tech/abra/client"
"coopcloud.tech/abra/config"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/swarm"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
@ -111,6 +115,41 @@ the default IPv4 address as the advertising address. This can be re-configured
later for more advanced use cases.
`,
Action: func(c *cli.Context) error {
host := c.Args().First()
if host == "" {
cli.ShowSubcommandHelp(c)
return nil
}
cl, err := client.NewClientWithContext(host)
if err != nil {
return err
}
var ipv4 net.IP
ips, _ := net.LookupIP(host)
for _, ip := range ips {
ipv4 = ip.To4()
}
if string(ipv4) == "" {
return fmt.Errorf("Unable to retrieve ipv4 address for %s", host)
}
ctx := context.Background()
initReq := swarm.InitRequest{
ListenAddr: "0.0.0.0:2377",
AdvertiseAddr: string(ipv4),
}
if _, err := cl.SwarmInit(ctx, initReq); err != nil {
return err
}
netOpts := types.NetworkCreate{Driver: "overlay", Scope: "swarm"}
if _, err := cl.NetworkCreate(ctx, "proxy", netOpts); err != nil {
return err
}
return nil
},
}