blob: a57c361ac82846cdbbbdef4392728be7c82ed869 [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"
gioe72b54f2024-04-22 10:44:41 +040014 "github.com/giolekva/pcloud/core/installer/soft"
giolekva8aa73e82022-07-09 11:34:39 +040015)
16
17var bootstrapFlags struct {
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040018 envName string
19 publicIP string
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040020 chartsDir string
21 adminPubKey string
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040022 storageDir string
23 volumeDefaultReplicaCount int
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040024 fromIP string
25 toIP string
giolekva8aa73e82022-07-09 11:34:39 +040026}
27
28func bootstrapCmd() *cobra.Command {
29 cmd := &cobra.Command{
30 Use: "bootstrap",
31 RunE: bootstrapCmdRun,
32 }
33 cmd.Flags().StringVar(
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040034 &bootstrapFlags.envName,
35 "env-name",
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040036 "pcloud",
37 "",
38 )
39 cmd.Flags().StringVar(
gio3cdee592024-04-17 10:15:56 +040040 &bootstrapFlags.publicIP,
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040041 "public-ip",
42 "",
43 "",
44 )
45 cmd.Flags().StringVar(
giolekva8aa73e82022-07-09 11:34:39 +040046 &bootstrapFlags.chartsDir,
47 "charts-dir",
48 "",
49 "",
50 )
51 cmd.Flags().StringVar(
52 &bootstrapFlags.adminPubKey,
53 "admin-pub-key",
54 "",
55 "",
56 )
57 cmd.Flags().StringVar(
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040058 &bootstrapFlags.storageDir,
59 "storage-dir",
60 "",
61 "",
62 )
63 cmd.Flags().IntVar(
64 &bootstrapFlags.volumeDefaultReplicaCount,
65 "volume-default-replica-count",
66 3,
67 "",
68 )
69 cmd.Flags().StringVar(
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040070 &bootstrapFlags.fromIP,
71 "from-ip",
72 "",
73 "",
74 )
75 cmd.Flags().StringVar(
76 &bootstrapFlags.toIP,
77 "to-ip",
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040078 "",
79 "",
80 )
giolekva8aa73e82022-07-09 11:34:39 +040081 return cmd
82}
83
84func bootstrapCmdRun(cmd *cobra.Command, args []string) error {
Giorgi Lekveishvili0c6b3242024-03-14 15:31:08 +040085 // TODO(gio): remove
86 installer.CreateAllApps()
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040087 adminPubKey, err := os.ReadFile(bootstrapFlags.adminPubKey)
Giorgi Lekveishvili677b4572023-05-26 15:02:37 +040088 if err != nil {
89 return err
90 }
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040091 nsCreator, err := newNSCreator()
92 if err != nil {
Giorgi Lekveishvili677b4572023-05-26 15:02:37 +040093 return err
94 }
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040095 serviceIPs, err := newServiceIPs(bootstrapFlags.fromIP, bootstrapFlags.toIP)
96 if err != nil {
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040097 return err
98 }
gio3cdee592024-04-17 10:15:56 +040099 publicIPs, err := parseIPs(bootstrapFlags.publicIP)
100 envConfig := installer.BootstrapConfig{
101 InfraName: bootstrapFlags.envName,
102 PublicIP: publicIPs,
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +0400103 NamespacePrefix: fmt.Sprintf("%s-", bootstrapFlags.envName),
104 StorageDir: bootstrapFlags.storageDir,
105 VolumeDefaultReplicaCount: bootstrapFlags.volumeDefaultReplicaCount,
106 AdminPublicKey: adminPubKey,
107 ServiceIPs: serviceIPs,
Giorgi Lekveishvili677b4572023-05-26 15:02:37 +0400108 }
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +0400109 b := installer.NewBootstrapper(
110 installer.NewFSChartLoader(bootstrapFlags.chartsDir),
111 nsCreator,
Giorgi Lekveishvili7c427602024-01-04 00:13:55 +0400112 installer.NewActionConfigFactory(rootFlags.kubeConfig),
gio3af43942024-04-16 08:13:50 +0400113 installer.NewInMemoryAppRepository(installer.CreateAllApps()),
gioe72b54f2024-04-22 10:44:41 +0400114 soft.RealClientGetter{},
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +0400115 )
116 return b.Run(envConfig)
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400117}
118
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +0400119func newServiceIPs(from, to string) (installer.EnvServiceIPs, error) {
120 f, err := netip.ParseAddr(from)
121 if err != nil {
122 return installer.EnvServiceIPs{}, err
123 }
124 t, err := netip.ParseAddr(to)
125 if err != nil {
126 return installer.EnvServiceIPs{}, err
127 }
128 configRepo := f
129 ingressPublic := configRepo.Next()
130 restFrom := ingressPublic.Next()
131 return installer.EnvServiceIPs{
132 ConfigRepo: configRepo,
133 IngressPublic: ingressPublic,
134 From: restFrom,
135 To: t,
136 }, nil
137}
gio3cdee592024-04-17 10:15:56 +0400138
139func parseIPs(ip string) ([]net.IP, error) {
140 ret := make([]net.IP, 0)
141 for _, i := range strings.Split(ip, ",") {
142 ip := net.ParseIP(i)
143 if ip == nil {
144 return nil, fmt.Errorf("invalid ip: %s", i)
145 }
146 ret = append(ret, ip)
147 }
148 return ret, nil
149}