From c47324b07beabd825f1418ae6c1d0e327d2fcc6b Mon Sep 17 00:00:00 2001 From: mundotv789123 Date: Mon, 5 Sep 2022 09:19:29 -0300 Subject: [PATCH 1/3] Creating 'wings service-install' command --- cmd/root.go | 5 +-- cmd/service_install.go | 70 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 cmd/service_install.go diff --git a/cmd/root.go b/cmd/root.go index 12d26115..381fd097 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -5,8 +5,6 @@ import ( "crypto/tls" "errors" "fmt" - "github.com/pterodactyl/wings/internal/cron" - "github.com/pterodactyl/wings/internal/database" log2 "log" "net/http" _ "net/http/pprof" @@ -30,6 +28,8 @@ import ( "github.com/pterodactyl/wings/config" "github.com/pterodactyl/wings/environment" + "github.com/pterodactyl/wings/internal/cron" + "github.com/pterodactyl/wings/internal/database" "github.com/pterodactyl/wings/loggers/cli" "github.com/pterodactyl/wings/remote" "github.com/pterodactyl/wings/router" @@ -87,6 +87,7 @@ func init() { rootCommand.AddCommand(versionCommand) rootCommand.AddCommand(configureCmd) + rootCommand.AddCommand(serviceCmd) rootCommand.AddCommand(newDiagnosticsCommand()) } diff --git a/cmd/service_install.go b/cmd/service_install.go new file mode 100644 index 00000000..aa6de624 --- /dev/null +++ b/cmd/service_install.go @@ -0,0 +1,70 @@ +package cmd + +import ( + "fmt" + "os" + "os/exec" + + "github.com/apex/log" + "github.com/spf13/cobra" +) + +var ( + serviceFile = "/etc/systemd/system/wings.service" + serviceContent = `[Unit] +Description=Pterodactyl Wings Daemon +After=docker.service +Requires=docker.service +PartOf=docker.service + +[Service] +User=root +WorkingDirectory=/etc/pterodactyl +LimitNOFILE=4096 +PIDFile=/var/run/wings/daemon.pid +ExecStart=/usr/local/bin/wings +Restart=on-failure +StartLimitInterval=180 +StartLimitBurst=30 +RestartSec=5s + +[Install] +WantedBy=multi-user.target` + serviceCmd = &cobra.Command{ + Use: "service-install", + Short: "Use to install wings.service automatically", + Run: installService, + } +) + +func installService(cmd *cobra.Command, args []string) { + if _, err := os.Stat(serviceFile); err == nil { + log.WithField("error", "service file exists").Fatal("service aready installed") + return + } + + f, cf_err := os.Create(serviceFile) + + if cf_err != nil { + log.WithField("error", cf_err).Fatal("error while creating service file") + return + } + + content := []byte(serviceContent) + + _, wf_err := f.Write(content) + + if wf_err != nil { + log.WithField("error", wf_err).Fatal("error while write service file") + return + } + + command := exec.Command("systemctl", "enable", "--now", serviceFile) + cmd_err := command.Start() + + if cmd_err != nil { + log.WithField("error", wf_err).Fatal("error while enabling service") + return + } + fmt.Println("service created success!") +} From 8654e63ba800a8c61b389309cc9f63601018b699 Mon Sep 17 00:00:00 2001 From: Michael Fernandes <48911860+mundotv789123@users.noreply.github.com> Date: Tue, 6 Sep 2022 10:39:01 -0300 Subject: [PATCH 2/3] Adding 'systemctl daemon-reload' command after service is installed. --- cmd/service_install.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/cmd/service_install.go b/cmd/service_install.go index aa6de624..e8c1efb1 100644 --- a/cmd/service_install.go +++ b/cmd/service_install.go @@ -59,12 +59,21 @@ func installService(cmd *cobra.Command, args []string) { return } - command := exec.Command("systemctl", "enable", "--now", serviceFile) - cmd_err := command.Start() + enable_command := exec.Command("systemctl", "enable", "--now", serviceFile) + cmd_enable_err := enable_command.Start() - if cmd_err != nil { + if cmd_enable_err != nil { log.WithField("error", wf_err).Fatal("error while enabling service") return } + + daemon_reload_command := exec.Command("systemctl", "daemon-reload") + cmd_reload_err := daemon_reload_command.Start() + + if cmd_reload_err != nil { + log.WithField("error", wf_err).Fatal("error while reloading daemon") + return + } + fmt.Println("service created success!") } From 84ec566a42958367ab0da521ee7c486262325248 Mon Sep 17 00:00:00 2001 From: Michael Fernandes <48911860+mundotv789123@users.noreply.github.com> Date: Tue, 6 Sep 2022 10:41:28 -0300 Subject: [PATCH 3/3] fix errors references on messages. --- cmd/service_install.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/service_install.go b/cmd/service_install.go index e8c1efb1..c16e5835 100644 --- a/cmd/service_install.go +++ b/cmd/service_install.go @@ -63,7 +63,7 @@ func installService(cmd *cobra.Command, args []string) { cmd_enable_err := enable_command.Start() if cmd_enable_err != nil { - log.WithField("error", wf_err).Fatal("error while enabling service") + log.WithField("error", cmd_enable_err).Fatal("error while enabling service") return } @@ -71,7 +71,7 @@ func installService(cmd *cobra.Command, args []string) { cmd_reload_err := daemon_reload_command.Start() if cmd_reload_err != nil { - log.WithField("error", wf_err).Fatal("error while reloading daemon") + log.WithField("error", cmd_reload_err).Fatal("error while reloading daemon") return }