blob: 9b685292e8a901ef6970d5dbd67c7dc9aac7eece [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 Lekveishvili0c6b3242024-03-14 15:31:08 +040082 // TODO(gio): remove
83 installer.CreateAllApps()
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040084 adminPubKey, err := os.ReadFile(bootstrapFlags.adminPubKey)
Giorgi Lekveishvili677b4572023-05-26 15:02:37 +040085 if err != nil {
86 return err
87 }
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040088 nsCreator, err := newNSCreator()
89 if err != nil {
Giorgi Lekveishvili677b4572023-05-26 15:02:37 +040090 return err
91 }
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040092 serviceIPs, err := newServiceIPs(bootstrapFlags.fromIP, bootstrapFlags.toIP)
93 if err != nil {
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040094 return err
95 }
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040096 envConfig := installer.EnvConfig{
97 Name: bootstrapFlags.envName,
98 PublicIP: bootstrapFlags.publicIP,
99 NamespacePrefix: fmt.Sprintf("%s-", bootstrapFlags.envName),
100 StorageDir: bootstrapFlags.storageDir,
101 VolumeDefaultReplicaCount: bootstrapFlags.volumeDefaultReplicaCount,
102 AdminPublicKey: adminPubKey,
103 ServiceIPs: serviceIPs,
Giorgi Lekveishvili677b4572023-05-26 15:02:37 +0400104 }
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +0400105 b := installer.NewBootstrapper(
106 installer.NewFSChartLoader(bootstrapFlags.chartsDir),
107 nsCreator,
Giorgi Lekveishvili7c427602024-01-04 00:13:55 +0400108 installer.NewActionConfigFactory(rootFlags.kubeConfig),
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +0400109 )
110 return b.Run(envConfig)
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400111}
112
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +0400113func newServiceIPs(from, to string) (installer.EnvServiceIPs, error) {
114 f, err := netip.ParseAddr(from)
115 if err != nil {
116 return installer.EnvServiceIPs{}, err
117 }
118 t, err := netip.ParseAddr(to)
119 if err != nil {
120 return installer.EnvServiceIPs{}, err
121 }
122 configRepo := f
123 ingressPublic := configRepo.Next()
124 restFrom := ingressPublic.Next()
125 return installer.EnvServiceIPs{
126 ConfigRepo: configRepo,
127 IngressPublic: ingressPublic,
128 From: restFrom,
129 To: t,
130 }, nil
131}