mirror of
https://github.com/hanzokms/cli.git
synced 2026-10-30 20:30:00 +00:00
Infisical->KMS across all .go (identifiers + env-var keys + brand strings), e2e env vars + e2e/packages/infisical->kms dir. Only external published deps remain: github.com/infisical/go-sdk + infisical-kmip API symbols (kmsSdk.NewInfisicalClient etc.) and the forked keyring's INFISICAL_VAULT_FILE_PASSPHRASE env contract — these need upstream forks to rename.
111 lines
3.3 KiB
Go
111 lines
3.3 KiB
Go
package gatewayv2
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
|
|
"github.com/hanzokms/cli/packages/util"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
func InstallGatewaySystemdService(token string, domain string, name string, relayName string, serviceLogFile string) error {
|
|
if runtime.GOOS != "linux" {
|
|
log.Info().Msg("Skipping systemd service installation - not on Linux")
|
|
return nil
|
|
}
|
|
|
|
if os.Geteuid() != 0 {
|
|
log.Info().Msg("Skipping systemd service installation - not running as root/sudo")
|
|
return nil
|
|
}
|
|
|
|
configDir := "/etc/kms"
|
|
if err := os.MkdirAll(configDir, 0755); err != nil {
|
|
return fmt.Errorf("failed to create config directory: %v", err)
|
|
}
|
|
|
|
configContent := fmt.Sprintf("KMS_TOKEN=%s\n", token)
|
|
if domain != "" {
|
|
configContent += fmt.Sprintf("KMS_API_URL=%s\n", domain)
|
|
}
|
|
|
|
if name != "" {
|
|
configContent += fmt.Sprintf("%s=%s\n", GATEWAY_NAME_ENV_NAME, name)
|
|
}
|
|
if relayName != "" {
|
|
configContent += fmt.Sprintf("%s=%s\n", RELAY_NAME_ENV_NAME, relayName)
|
|
}
|
|
|
|
environmentFilePath := filepath.Join(configDir, "gateway.conf")
|
|
if err := os.WriteFile(environmentFilePath, []byte(configContent), 0600); err != nil {
|
|
return fmt.Errorf("failed to write environment file: %v", err)
|
|
}
|
|
|
|
if err := util.WriteSystemdServiceFile(serviceLogFile, environmentFilePath, "kms-gateway", "gateway", "Hanzo KMS Gateway Service"); err != nil {
|
|
return fmt.Errorf("failed to write systemd service file: %v", err)
|
|
}
|
|
|
|
if err := util.WriteLogrotateFile(serviceLogFile, "kms-gateway"); err != nil {
|
|
return fmt.Errorf("failed to write logrotate file: %v", err)
|
|
}
|
|
|
|
reloadCmd := exec.Command("systemctl", "daemon-reload")
|
|
if err := reloadCmd.Run(); err != nil {
|
|
return fmt.Errorf("failed to reload systemd: %v", err)
|
|
}
|
|
|
|
log.Info().Msg("Successfully installed systemd service")
|
|
log.Info().Msg("To start the service, run: sudo systemctl start kms-gateway")
|
|
log.Info().Msg("To enable the service on boot, run: sudo systemctl enable kms-gateway")
|
|
|
|
return nil
|
|
}
|
|
|
|
func UninstallGatewaySystemdService() error {
|
|
if runtime.GOOS != "linux" {
|
|
log.Info().Msg("Skipping systemd service uninstallation - not on Linux")
|
|
return nil
|
|
}
|
|
|
|
if os.Geteuid() != 0 {
|
|
log.Info().Msg("Skipping systemd service uninstallation - not running as root/sudo")
|
|
return nil
|
|
}
|
|
|
|
// Stop the service if it's running
|
|
stopCmd := exec.Command("systemctl", "stop", "kms-gateway")
|
|
if err := stopCmd.Run(); err != nil {
|
|
log.Warn().Msgf("Failed to stop service: %v", err)
|
|
}
|
|
|
|
// Disable the service
|
|
disableCmd := exec.Command("systemctl", "disable", "kms-gateway")
|
|
if err := disableCmd.Run(); err != nil {
|
|
log.Warn().Msgf("Failed to disable service: %v", err)
|
|
}
|
|
|
|
// Remove the service file
|
|
servicePath := "/etc/systemd/system/kms-gateway.service"
|
|
if err := os.Remove(servicePath); err != nil && !os.IsNotExist(err) {
|
|
return fmt.Errorf("failed to remove systemd service file: %v", err)
|
|
}
|
|
|
|
// Remove the configuration file
|
|
configPath := "/etc/kms/gateway.conf"
|
|
if err := os.Remove(configPath); err != nil && !os.IsNotExist(err) {
|
|
return fmt.Errorf("failed to remove config file: %v", err)
|
|
}
|
|
|
|
// Reload systemd to apply changes
|
|
reloadCmd := exec.Command("systemctl", "daemon-reload")
|
|
if err := reloadCmd.Run(); err != nil {
|
|
return fmt.Errorf("failed to reload systemd: %v", err)
|
|
}
|
|
|
|
log.Info().Msg("Successfully uninstalled Hanzo KMS Gateway systemd service")
|
|
return nil
|
|
}
|