blob: 0aa523096656050159b5ffeb8b1ae6720c069212 [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"
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +04006 "net/netip"
giolekva8aa73e82022-07-09 11:34:39 +04007 "os"
giolekva8aa73e82022-07-09 11:34:39 +04008
giolekva8aa73e82022-07-09 11:34:39 +04009 "github.com/spf13/cobra"
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +040010
11 "github.com/giolekva/pcloud/core/installer"
giolekva8aa73e82022-07-09 11:34:39 +040012)
13
14var bootstrapFlags struct {
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040015 envName string
16 publicIP string
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040017 chartsDir string
18 adminPubKey string
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040019 storageDir string
20 volumeDefaultReplicaCount int
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040021 fromIP string
22 toIP string
giolekva8aa73e82022-07-09 11:34:39 +040023}
24
25func bootstrapCmd() *cobra.Command {
26 cmd := &cobra.Command{
27 Use: "bootstrap",
28 RunE: bootstrapCmdRun,
29 }
30 cmd.Flags().StringVar(
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040031 &bootstrapFlags.envName,
32 "env-name",
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040033 "pcloud",
34 "",
35 )
36 cmd.Flags().StringVar(
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040037 &bootstrapFlags.envName,
38 "public-ip",
39 "",
40 "",
41 )
42 cmd.Flags().StringVar(
giolekva8aa73e82022-07-09 11:34:39 +040043 &bootstrapFlags.chartsDir,
44 "charts-dir",
45 "",
46 "",
47 )
48 cmd.Flags().StringVar(
49 &bootstrapFlags.adminPubKey,
50 "admin-pub-key",
51 "",
52 "",
53 )
54 cmd.Flags().StringVar(
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040055 &bootstrapFlags.storageDir,
56 "storage-dir",
57 "",
58 "",
59 )
60 cmd.Flags().IntVar(
61 &bootstrapFlags.volumeDefaultReplicaCount,
62 "volume-default-replica-count",
63 3,
64 "",
65 )
66 cmd.Flags().StringVar(
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040067 &bootstrapFlags.fromIP,
68 "from-ip",
69 "",
70 "",
71 )
72 cmd.Flags().StringVar(
73 &bootstrapFlags.toIP,
74 "to-ip",
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040075 "",
76 "",
77 )
giolekva8aa73e82022-07-09 11:34:39 +040078 return cmd
79}
80
81func bootstrapCmdRun(cmd *cobra.Command, args []string) error {
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040082 adminPubKey, err := os.ReadFile(bootstrapFlags.adminPubKey)
Giorgi Lekveishvili677b4572023-05-26 15:02:37 +040083 if err != nil {
84 return err
85 }
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040086 nsCreator, err := newNSCreator()
87 if err != nil {
Giorgi Lekveishvili677b4572023-05-26 15:02:37 +040088 return err
89 }
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040090 serviceIPs, err := newServiceIPs(bootstrapFlags.fromIP, bootstrapFlags.toIP)
91 if err != nil {
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040092 return err
93 }
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040094 envConfig := installer.EnvConfig{
95 Name: bootstrapFlags.envName,
96 PublicIP: bootstrapFlags.publicIP,
97 NamespacePrefix: fmt.Sprintf("%s-", bootstrapFlags.envName),
98 StorageDir: bootstrapFlags.storageDir,
99 VolumeDefaultReplicaCount: bootstrapFlags.volumeDefaultReplicaCount,
100 AdminPublicKey: adminPubKey,
101 ServiceIPs: serviceIPs,
Giorgi Lekveishvili677b4572023-05-26 15:02:37 +0400102 }
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +0400103 b := installer.NewBootstrapper(
104 installer.NewFSChartLoader(bootstrapFlags.chartsDir),
105 nsCreator,
Giorgi Lekveishvili7c427602024-01-04 00:13:55 +0400106 installer.NewActionConfigFactory(rootFlags.kubeConfig),
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +0400107 )
108 return b.Run(envConfig)
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400109}
110
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +0400111func newServiceIPs(from, to string) (installer.EnvServiceIPs, error) {
112 f, err := netip.ParseAddr(from)
113 if err != nil {
114 return installer.EnvServiceIPs{}, err
115 }
116 t, err := netip.ParseAddr(to)
117 if err != nil {
118 return installer.EnvServiceIPs{}, err
119 }
120 configRepo := f
121 ingressPublic := configRepo.Next()
122 restFrom := ingressPublic.Next()
123 return installer.EnvServiceIPs{
124 ConfigRepo: configRepo,
125 IngressPublic: ingressPublic,
126 From: restFrom,
127 To: t,
128 }, nil
129}