Skip to content

Commit

Permalink
installing service under systemd draft
Browse files Browse the repository at this point in the history
  • Loading branch information
f1mishutka committed Nov 17, 2022
1 parent 08a87d1 commit ce52a32
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"type": "go",
"request": "launch",
"program": "main.go",
"args": ["info"]
"args": ["install"]
}
]
}
10 changes: 10 additions & 0 deletions app/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package app

import (
_ "embed"
"runtime"
)

Expand All @@ -10,6 +11,9 @@ var BuildCommit = "DEV"

const DefaultSettingsFilename = ".bot.yml"

//go:embed mt-tgadmin.service.template
var ServiceUnitFileTemplate string

var Global struct {
AppName string
Version string
Expand All @@ -21,6 +25,8 @@ var Global struct {

BotInfo string
ChatInfo string

ServiceAutostart bool
}

func init() {
Expand All @@ -36,6 +42,10 @@ func init() {
Global.Settings.GuiHostname = "localhost"
Global.Settings.GuiPort = 15080

Global.Settings.ServiceName = Global.AppName
Global.Settings.ServiceUser = "www-data"
Global.Settings.ServiceGroup = "www-data"

Global.BotInfo = "[undefined]"
Global.ChatInfo = "[undefined]"
}
17 changes: 17 additions & 0 deletions app/mt-tgadmin.service.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[Unit]
Description={{ .Name }}
After=network.target
StartLimitIntervalSec=60
StartLimitBurst=5

[Service]
RestartSec=2s
Type=simple
User={{ .User }}
Group={{ .Group }}
WorkingDirectory={{ .WorkingDir }}
ExecStart={{ .Executable }} run
Restart=always

[Install]
WantedBy=multi-user.target
4 changes: 4 additions & 0 deletions app/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ type Settings struct {
GuiPassword string `yaml:"gui_password" yaml_comment:"GUI access password"`
GuiHostname string `yaml:"gui_hostname" yaml_comment:"Web GUI hostname"`
GuiPort uint16 `yaml:"gui_port" yaml_comment:"Web GUI port number"`

ServiceName string `yaml:"service_name" yaml_comment:"Service name for 'install' command"`
ServiceUser string `yaml:"service_user" yaml_comment:"User for 'install' command"`
ServiceGroup string `yaml:"service_group" yaml_comment:"Group for 'install' command"`
}
102 changes: 102 additions & 0 deletions cmd/install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package cmd

import (
"log"
"os"
"path"
"text/template"

"github.com/mitoteam/mt-tgadmin/app"
"github.com/mitoteam/mt-tgadmin/mttools"

"github.com/spf13/cobra"
)

const systemdServiceDirPath = "/usr/lib/systemd"

func init() {
cmd := &cobra.Command{
Use: "install",
Short: "Creates system service to run " + app.Global.AppName,

RunE: func(cmd *cobra.Command, args []string) error {
//log.Printf("Autostart %t\n", app.Global.ServiceAutostart)

if mttools.IsDirExists(systemdServiceDirPath) {
systemdServiceInstall()
} else {
log.Fatalf(
"Directory %s does not exists. Only systemd based services supported for now.\n",
systemdServiceDirPath,
)
}

return nil
},
}

cmd.PersistentFlags().BoolVar(
&app.Global.ServiceAutostart,
"autostart",
false,
"Set service to be auto started after boot. Please note this does not auto starts service after installation.",
)

rootCmd.AddCommand(cmd)
}

type serviceUnitData struct {
Name string
User string
Group string
Executable string
WorkingDir string
}

func systemdServiceInstall() {
filename := path.Join(systemdServiceDirPath, app.Global.Settings.ServiceName+".service")

if mttools.IsFileExists(filename) {
log.Fatalf("File %s already exists. Use 'uninstall' command or remove file manually.\n", filename)
}

t := template.New("service")
if _, err := t.Parse(app.ServiceUnitFileTemplate); err != nil {
log.Fatalln(err)
}

executable, err := os.Executable()

if err != nil {
log.Fatalln(err)
}

workingDir, err := os.Getwd()

if err != nil {
log.Println(err)
}

data := &serviceUnitData{
Name: app.Global.Settings.ServiceName,
User: app.Global.Settings.ServiceUser,
Group: app.Global.Settings.ServiceGroup,
Executable: executable,
WorkingDir: workingDir,
}

file, err := os.Create(filename)
if err != nil {
log.Fatalln(err)
}

if err := t.Execute(file, data); err != nil {
log.Fatalln(err)
}

file.Close()

if err := file.Chmod(0644); err != nil {
log.Fatal(err)
}
}

0 comments on commit ce52a32

Please sign in to comment.