122 lines
3.3 KiB
Go
122 lines
3.3 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"gc-infra/config"
|
|
"gc-infra/utils"
|
|
"net"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/pulumi/pulumi-command/sdk/go/command/remote"
|
|
"github.com/pulumi/pulumi-hcloud/sdk/go/hcloud"
|
|
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
|
|
)
|
|
|
|
type Infrastructure struct {
|
|
placementGroup *hcloud.PlacementGroup
|
|
networkID *pulumi.IDOutput
|
|
managerNodes []*hcloud.Server
|
|
workerNodes []*hcloud.Server
|
|
}
|
|
|
|
func main() {
|
|
infra := &Infrastructure{}
|
|
|
|
pulumi.Run(func(ctx *pulumi.Context) error {
|
|
var err error
|
|
|
|
cfg := config.InfraConfig{
|
|
StackReference: strings.Join(
|
|
[]string{ctx.Organization(), ctx.Project(), ctx.Stack()},
|
|
"/"),
|
|
SwarmNetworkName: "gc-swarmnet",
|
|
SwarmIpRange: "10.0.0.0/16",
|
|
SwarmSubnetIpRange: "10.0.1.0/24",
|
|
}
|
|
pk, err := utils.CreateSshKey(ctx)
|
|
if err != nil {
|
|
panic(err.Error())
|
|
}
|
|
ctx.Export("privKey", pk.PrivateKeyOpenssh)
|
|
|
|
hkey, err := hcloud.NewSshKey(ctx, strings.Join([]string{cfg.StackReference, "key"}, "-"), &hcloud.SshKeyArgs{PublicKey: pk.PublicKeyOpenssh})
|
|
if err != nil {
|
|
panic(err.Error())
|
|
}
|
|
|
|
infra.placementGroup, err = utils.CreatePlacementGroup(ctx, cfg.StackReference)
|
|
if err != nil {
|
|
panic(err.Error())
|
|
}
|
|
|
|
infra.networkID, err = utils.CreateClusterNet(ctx, cfg)
|
|
if err != nil {
|
|
panic(err.Error())
|
|
}
|
|
|
|
infra.managerNodes, err = utils.CreateServer(ctx, utils.CreateServerArgs{
|
|
PlacementGroupId: infra.placementGroup.ID(),
|
|
NetworkId: infra.networkID,
|
|
NetworkFirstIP: string(utils.IncrementIP(net.ParseIP("10.0.1.0"))),
|
|
Basename: "manager-node",
|
|
Count: 3,
|
|
SshKey: hkey,
|
|
ServerType: "ccx23",
|
|
})
|
|
if err != nil {
|
|
panic(err.Error())
|
|
}
|
|
// infra.workerNodes, err = utils.CreateServer(ctx, utils.CreateServerArgs{
|
|
// PlacementGroupId: infra.placementGroup.ID(),
|
|
// NetworkId: infra.networkID,
|
|
// NetworkFirstIP: string(utils.IncrementIP(net.ParseIP("10.0.1.20"))),
|
|
// Basename: "worker-node",
|
|
// Count: 2,
|
|
// SshKey: hkey,
|
|
// })
|
|
// if err != nil {
|
|
// panic(err.Error())
|
|
// }
|
|
|
|
for idx, s := range slices.Concat(infra.managerNodes, infra.workerNodes) {
|
|
err := utils.InstallAnsibleDependencies(ctx, remote.ConnectionArgs{
|
|
Host: s.Ipv4Address,
|
|
User: pulumi.String("root"),
|
|
PrivateKey: pk.PrivateKeyOpenssh,
|
|
}, fmt.Sprintf("%d", idx))
|
|
if err != nil {
|
|
panic(err.Error())
|
|
}
|
|
}
|
|
|
|
// var advAddr = infra.managerNodes[0].Networks.ApplyT(func(net []hcloud.ServerNetworkType) string {
|
|
// return *net[0].Ip
|
|
// }).(pulumi.StringOutput)
|
|
|
|
// tokens, err := utils.InitDockerSwarm(ctx, remote.ConnectionArgs{
|
|
// Host: infra.managerNodes[0].Ipv4Address,
|
|
// User: pulumi.String("root"),
|
|
// PrivateKey: pk.PrivateKeyOpenssh}, advAddr)
|
|
// if err != nil {
|
|
// panic(err.Error())
|
|
// }
|
|
|
|
// ctx.Export("SwarmTokens", tokens)
|
|
|
|
// inventory, err := utils.CreateAnsibleInventory(infra.managerNodes, infra.workerNodes)
|
|
// if err != nil {
|
|
// panic(err.Error())
|
|
// }
|
|
// ctx.Export("inventory", inventory)
|
|
|
|
sm := map[string]pulumi.Input{}
|
|
for idx, s := range slices.Concat(infra.managerNodes, infra.workerNodes) {
|
|
sm[fmt.Sprintf("node-%d-ip", idx)] = s.Ipv4Address
|
|
}
|
|
ctx.Export("server-ips", pulumi.Map(sm))
|
|
|
|
return nil
|
|
})
|
|
}
|