4
4
"context"
5
5
"fmt"
6
6
"os"
7
+ "os/exec"
7
8
"os/signal"
9
+ "runtime"
8
10
"syscall"
9
11
"time"
10
12
@@ -16,31 +18,23 @@ import (
16
18
)
17
19
18
20
var gatewayCmd = & cobra.Command {
19
- Example : `infisical gateway` ,
20
- Short : "Used to infisical gateway" ,
21
21
Use : "gateway" ,
22
+ Short : "Run the Infisical gateway or manage its systemd service" ,
23
+ Long : "Run the Infisical gateway in the foreground or manage its systemd service installation. Use 'gateway install' to set up the systemd service." ,
24
+ Example : `infisical gateway --token=<token>
25
+ sudo infisical gateway install --token=<token> --domain=<domain>` ,
22
26
DisableFlagsInUseLine : true ,
23
27
Args : cobra .NoArgs ,
24
28
Run : func (cmd * cobra.Command , args []string ) {
25
29
token , err := util .GetInfisicalToken (cmd )
26
30
if err != nil {
27
- util .HandleError (err , "Unable to parse flag" )
31
+ util .HandleError (err , "Unable to parse token flag" )
28
32
}
29
33
30
34
if token == nil {
31
35
util .HandleError (fmt .Errorf ("Token not found" ))
32
36
}
33
37
34
- domain , err := cmd .Flags ().GetString ("domain" )
35
- if err != nil {
36
- util .HandleError (err , "Unable to parse domain flag" )
37
- }
38
-
39
- // Try to install systemd service if possible
40
- if err := gateway .InstallGatewaySystemdService (token .Token , domain ); err != nil {
41
- log .Warn ().Msgf ("Failed to install systemd service: %v" , err )
42
- }
43
-
44
38
Telemetry .CaptureEvent ("cli-command:gateway" , posthog .NewProperties ().Set ("version" , util .CLI_VERSION ))
45
39
46
40
sigCh := make (chan os.Signal , 1 )
@@ -110,6 +104,50 @@ var gatewayCmd = &cobra.Command{
110
104
},
111
105
}
112
106
107
+ var gatewayInstallCmd = & cobra.Command {
108
+ Use : "install" ,
109
+ Short : "Install and enable systemd service for the gateway (requires sudo)" ,
110
+ Long : "Install and enable systemd service for the gateway. Must be run with sudo on Linux." ,
111
+ Example : "sudo infisical gateway install --token=<token> --domain=<domain>" ,
112
+ DisableFlagsInUseLine : true ,
113
+ Args : cobra .NoArgs ,
114
+ Run : func (cmd * cobra.Command , args []string ) {
115
+ if runtime .GOOS != "linux" {
116
+ util .HandleError (fmt .Errorf ("systemd service installation is only supported on Linux" ))
117
+ }
118
+
119
+ if os .Geteuid () != 0 {
120
+ util .HandleError (fmt .Errorf ("systemd service installation requires root/sudo privileges" ))
121
+ }
122
+
123
+ token , err := util .GetInfisicalToken (cmd )
124
+ if err != nil {
125
+ util .HandleError (err , "Unable to parse flag" )
126
+ }
127
+
128
+ if token == nil {
129
+ util .HandleError (fmt .Errorf ("Token not found" ))
130
+ }
131
+
132
+ domain , err := cmd .Flags ().GetString ("domain" )
133
+ if err != nil {
134
+ util .HandleError (err , "Unable to parse domain flag" )
135
+ }
136
+
137
+ if err := gateway .InstallGatewaySystemdService (token .Token , domain ); err != nil {
138
+ util .HandleError (err , "Failed to install systemd service" )
139
+ }
140
+
141
+ enableCmd := exec .Command ("systemctl" , "enable" , "infisical-gateway" )
142
+ if err := enableCmd .Run (); err != nil {
143
+ util .HandleError (err , "Failed to enable systemd service" )
144
+ }
145
+
146
+ log .Info ().Msg ("Successfully installed and enabled infisical-gateway service" )
147
+ log .Info ().Msg ("To start the service, run: sudo systemctl start infisical-gateway" )
148
+ },
149
+ }
150
+
113
151
var gatewayRelayCmd = & cobra.Command {
114
152
Example : `infisical gateway relay` ,
115
153
Short : "Used to run infisical gateway relay" ,
@@ -139,9 +177,12 @@ var gatewayRelayCmd = &cobra.Command{
139
177
140
178
func init () {
141
179
gatewayCmd .Flags ().String ("token" , "" , "Connect with Infisical using machine identity access token" )
180
+ gatewayInstallCmd .Flags ().String ("token" , "" , "Connect with Infisical using machine identity access token" )
181
+ gatewayInstallCmd .Flags ().String ("domain" , "" , "Domain of your self-hosted Infisical instance" )
142
182
143
183
gatewayRelayCmd .Flags ().String ("config" , "" , "Relay config yaml file path" )
144
184
185
+ gatewayCmd .AddCommand (gatewayInstallCmd )
145
186
gatewayCmd .AddCommand (gatewayRelayCmd )
146
187
rootCmd .AddCommand (gatewayCmd )
147
188
}
0 commit comments