blob: 6e0b247a1022146e0f4c4705951fe0ff6e8e5f36 [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 Lekveishvilie009a5d2024-01-05 14:10:11 +040082 // TODO(gio): remove installer.CreateAllApps()
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040083 adminPubKey, err := os.ReadFile(bootstrapFlags.adminPubKey)
Giorgi Lekveishvili677b4572023-05-26 15:02:37 +040084 if err != nil {
85 return err
86 }
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040087 nsCreator, err := newNSCreator()
88 if err != nil {
Giorgi Lekveishvili677b4572023-05-26 15:02:37 +040089 return err
90 }
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040091 serviceIPs, err := newServiceIPs(bootstrapFlags.fromIP, bootstrapFlags.toIP)
92 if err != nil {
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040093 return err
94 }
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040095 envConfig := installer.EnvConfig{
96 Name: bootstrapFlags.envName,
97 PublicIP: bootstrapFlags.publicIP,
98 NamespacePrefix: fmt.Sprintf("%s-", bootstrapFlags.envName),
99 StorageDir: bootstrapFlags.storageDir,
100 VolumeDefaultReplicaCount: bootstrapFlags.volumeDefaultReplicaCount,
101 AdminPublicKey: adminPubKey,
102 ServiceIPs: serviceIPs,
Giorgi Lekveishvili677b4572023-05-26 15:02:37 +0400103 }
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +0400104 b := installer.NewBootstrapper(
105 installer.NewFSChartLoader(bootstrapFlags.chartsDir),
106 nsCreator,
Giorgi Lekveishvili7c427602024-01-04 00:13:55 +0400107 installer.NewActionConfigFactory(rootFlags.kubeConfig),
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +0400108 )
109 return b.Run(envConfig)
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400110}
111
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +0400112func newServiceIPs(from, to string) (installer.EnvServiceIPs, error) {
113 f, err := netip.ParseAddr(from)
114 if err != nil {
115 return installer.EnvServiceIPs{}, err
116 }
117 t, err := netip.ParseAddr(to)
118 if err != nil {
119 return installer.EnvServiceIPs{}, err
120 }
121 configRepo := f
122 ingressPublic := configRepo.Next()
123 restFrom := ingressPublic.Next()
124 return installer.EnvServiceIPs{
125 ConfigRepo: configRepo,
126 IngressPublic: ingressPublic,
127 From: restFrom,
128 To: t,
129 }, nil
130}