blob: dc85470c954f8a606251c4f81a209285ac1fafad [file] [log] [blame]
giolekva8aa73e82022-07-09 11:34:39 +04001package main
2
3import (
giolekva8aa73e82022-07-09 11:34:39 +04004 _ "embed"
giolekva8aa73e82022-07-09 11:34:39 +04005 "fmt"
gio3cdee592024-04-17 10:15:56 +04006 "net"
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +04007 "net/netip"
giolekva8aa73e82022-07-09 11:34:39 +04008 "os"
gio3cdee592024-04-17 10:15:56 +04009 "strings"
giolekva8aa73e82022-07-09 11:34:39 +040010
giolekva8aa73e82022-07-09 11:34:39 +040011 "github.com/spf13/cobra"
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +040012
13 "github.com/giolekva/pcloud/core/installer"
giolekva8aa73e82022-07-09 11:34:39 +040014)
15
16var bootstrapFlags struct {
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040017 envName string
18 publicIP string
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040019 chartsDir string
20 adminPubKey string
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040021 storageDir string
22 volumeDefaultReplicaCount int
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040023 fromIP string
24 toIP string
giolekva8aa73e82022-07-09 11:34:39 +040025}
26
27func bootstrapCmd() *cobra.Command {
28 cmd := &cobra.Command{
29 Use: "bootstrap",
30 RunE: bootstrapCmdRun,
31 }
32 cmd.Flags().StringVar(
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040033 &bootstrapFlags.envName,
34 "env-name",
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040035 "pcloud",
36 "",
37 )
38 cmd.Flags().StringVar(
gio3cdee592024-04-17 10:15:56 +040039 &bootstrapFlags.publicIP,
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040040 "public-ip",
41 "",
42 "",
43 )
44 cmd.Flags().StringVar(
giolekva8aa73e82022-07-09 11:34:39 +040045 &bootstrapFlags.chartsDir,
46 "charts-dir",
47 "",
48 "",
49 )
50 cmd.Flags().StringVar(
51 &bootstrapFlags.adminPubKey,
52 "admin-pub-key",
53 "",
54 "",
55 )
56 cmd.Flags().StringVar(
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040057 &bootstrapFlags.storageDir,
58 "storage-dir",
59 "",
60 "",
61 )
62 cmd.Flags().IntVar(
63 &bootstrapFlags.volumeDefaultReplicaCount,
64 "volume-default-replica-count",
65 3,
66 "",
67 )
68 cmd.Flags().StringVar(
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040069 &bootstrapFlags.fromIP,
70 "from-ip",
71 "",
72 "",
73 )
74 cmd.Flags().StringVar(
75 &bootstrapFlags.toIP,
76 "to-ip",
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040077 "",
78 "",
79 )
giolekva8aa73e82022-07-09 11:34:39 +040080 return cmd
81}
82
83func bootstrapCmdRun(cmd *cobra.Command, args []string) error {
Giorgi Lekveishvili0c6b3242024-03-14 15:31:08 +040084 // TODO(gio): remove
85 installer.CreateAllApps()
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040086 adminPubKey, err := os.ReadFile(bootstrapFlags.adminPubKey)
Giorgi Lekveishvili677b4572023-05-26 15:02:37 +040087 if err != nil {
88 return err
89 }
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040090 nsCreator, err := newNSCreator()
91 if err != nil {
Giorgi Lekveishvili677b4572023-05-26 15:02:37 +040092 return err
93 }
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040094 serviceIPs, err := newServiceIPs(bootstrapFlags.fromIP, bootstrapFlags.toIP)
95 if err != nil {
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040096 return err
97 }
gio3cdee592024-04-17 10:15:56 +040098 publicIPs, err := parseIPs(bootstrapFlags.publicIP)
99 envConfig := installer.BootstrapConfig{
100 InfraName: bootstrapFlags.envName,
101 PublicIP: publicIPs,
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +0400102 NamespacePrefix: fmt.Sprintf("%s-", bootstrapFlags.envName),
103 StorageDir: bootstrapFlags.storageDir,
104 VolumeDefaultReplicaCount: bootstrapFlags.volumeDefaultReplicaCount,
105 AdminPublicKey: adminPubKey,
106 ServiceIPs: serviceIPs,
Giorgi Lekveishvili677b4572023-05-26 15:02:37 +0400107 }
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +0400108 b := installer.NewBootstrapper(
109 installer.NewFSChartLoader(bootstrapFlags.chartsDir),
110 nsCreator,
Giorgi Lekveishvili7c427602024-01-04 00:13:55 +0400111 installer.NewActionConfigFactory(rootFlags.kubeConfig),
gio3af43942024-04-16 08:13:50 +0400112 installer.NewInMemoryAppRepository(installer.CreateAllApps()),
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +0400113 )
114 return b.Run(envConfig)
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400115}
116
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +0400117func newServiceIPs(from, to string) (installer.EnvServiceIPs, error) {
118 f, err := netip.ParseAddr(from)
119 if err != nil {
120 return installer.EnvServiceIPs{}, err
121 }
122 t, err := netip.ParseAddr(to)
123 if err != nil {
124 return installer.EnvServiceIPs{}, err
125 }
126 configRepo := f
127 ingressPublic := configRepo.Next()
128 restFrom := ingressPublic.Next()
129 return installer.EnvServiceIPs{
130 ConfigRepo: configRepo,
131 IngressPublic: ingressPublic,
132 From: restFrom,
133 To: t,
134 }, nil
135}
gio3cdee592024-04-17 10:15:56 +0400136
137func parseIPs(ip string) ([]net.IP, error) {
138 ret := make([]net.IP, 0)
139 for _, i := range strings.Split(ip, ",") {
140 ip := net.ParseIP(i)
141 if ip == nil {
142 return nil, fmt.Errorf("invalid ip: %s", i)
143 }
144 ret = append(ret, ip)
145 }
146 return ret, nil
147}