|
| 1 | +package gateway |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "path/filepath" |
| 8 | + "runtime" |
| 9 | + |
| 10 | + "github.com/rs/zerolog/log" |
| 11 | +) |
| 12 | + |
| 13 | +const systemdServiceTemplate = `[Unit] |
| 14 | +Description=Infisical Gateway Service |
| 15 | +After=network.target |
| 16 | +
|
| 17 | +[Service] |
| 18 | +Type=simple |
| 19 | +EnvironmentFile=/etc/infisical/gateway.conf |
| 20 | +ExecStart=/usr/local/bin/infisical gateway |
| 21 | +Restart=on-failure |
| 22 | +InaccessibleDirectories=/home |
| 23 | +PrivateTmp=yes |
| 24 | +LimitCORE=infinity |
| 25 | +LimitNOFILE=1000000 |
| 26 | +LimitNPROC=60000 |
| 27 | +LimitRTPRIO=infinity |
| 28 | +LimitRTTIME=7000000 |
| 29 | +
|
| 30 | +[Install] |
| 31 | +WantedBy=multi-user.target |
| 32 | +` |
| 33 | + |
| 34 | +func InstallGatewaySystemdService(token string, domain string) error { |
| 35 | + if runtime.GOOS != "linux" { |
| 36 | + log.Info().Msg("Skipping systemd service installation - not on Linux") |
| 37 | + return nil |
| 38 | + } |
| 39 | + |
| 40 | + if os.Geteuid() != 0 { |
| 41 | + log.Info().Msg("Skipping systemd service installation - not running as root/sudo") |
| 42 | + return nil |
| 43 | + } |
| 44 | + |
| 45 | + configDir := "/etc/infisical" |
| 46 | + if err := os.MkdirAll(configDir, 0755); err != nil { |
| 47 | + return fmt.Errorf("failed to create config directory: %v", err) |
| 48 | + } |
| 49 | + |
| 50 | + configContent := fmt.Sprintf("INFISICAL_UNIVERSAL_AUTH_ACCESS_TOKEN=%s\n", token) |
| 51 | + if domain != "" { |
| 52 | + configContent += fmt.Sprintf("INFISICAL_API_URL=%s\n", domain) |
| 53 | + } else { |
| 54 | + configContent += "INFISICAL_API_URL=\n" |
| 55 | + } |
| 56 | + |
| 57 | + configPath := filepath.Join(configDir, "gateway.conf") |
| 58 | + if err := os.WriteFile(configPath, []byte(configContent), 0600); err != nil { |
| 59 | + return fmt.Errorf("failed to write config file: %v", err) |
| 60 | + } |
| 61 | + |
| 62 | + servicePath := "/etc/systemd/system/infisical-gateway.service" |
| 63 | + if _, err := os.Stat(servicePath); err == nil { |
| 64 | + log.Info().Msg("Systemd service file already exists") |
| 65 | + return nil |
| 66 | + } |
| 67 | + |
| 68 | + if err := os.WriteFile(servicePath, []byte(systemdServiceTemplate), 0644); err != nil { |
| 69 | + return fmt.Errorf("failed to write systemd service file: %v", err) |
| 70 | + } |
| 71 | + |
| 72 | + reloadCmd := exec.Command("systemctl", "daemon-reload") |
| 73 | + if err := reloadCmd.Run(); err != nil { |
| 74 | + return fmt.Errorf("failed to reload systemd: %v", err) |
| 75 | + } |
| 76 | + |
| 77 | + log.Info().Msg("Successfully installed systemd service") |
| 78 | + log.Info().Msg("To start the service, run: sudo systemctl start infisical-gateway") |
| 79 | + log.Info().Msg("To enable the service on boot, run: sudo systemctl enable infisical-gateway") |
| 80 | + |
| 81 | + return nil |
| 82 | +} |
0 commit comments