-
Notifications
You must be signed in to change notification settings - Fork 2
/
mediaserver.go
223 lines (201 loc) · 4.84 KB
/
mediaserver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package main
import (
"fmt"
"io"
"log"
"os"
"os/exec"
"os/user"
"strconv"
)
type environment struct {
username string
basePort string
uid string
gid string
storage string
}
type command interface {
execute() error
}
type start struct{}
type stop struct{}
func (s start) execute() error {
username, basePort, storage, err := loadStartArgs()
if err != nil {
return err
}
uid, gid, err := getUser()
if err != nil {
return err
}
env := environment{username, basePort, uid, gid, storage}
err = composeUp(env)
if err != nil {
fmt.Printf("error executing docker-compose up: %s", err.Error())
fmt.Printf("\ntrying to revert...\n")
return stop{}.execute()
}
err = changeOwner(env)
if err != nil {
return fmt.Errorf("impossible to change owner of storage folder")
}
return nil
}
func (s stop) execute() error {
username, err := loadStopArgs()
if err != nil {
return err
}
uid, gid, err := getUser()
if err != nil {
return err
}
cmd := exec.Command("docker-compose", "down")
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, fmt.Sprintf("COMPOSE_PROJECT_NAME=%s", username))
cmd.Env = append(cmd.Env, fmt.Sprintf("MEDIA_UID=%s", uid))
cmd.Env = append(cmd.Env, fmt.Sprintf("MEDIA_GID=%s", gid))
cmd.Env = append(cmd.Env, fmt.Sprintf("MEDIA_USERNAME=%s", username))
cmd.Env = append(cmd.Env, fmt.Sprintf("MEDIA_STORAGE=%s", ""))
cmd.Env = append(cmd.Env, fmt.Sprintf("MEDIA_BASE_PORT=%s", ""))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func main() {
err := checkSudo()
if err != nil {
log.Fatal(err)
}
cmd, err := getCommand()
if err != nil {
fmt.Printf("\n%s\n", err)
printHelp(os.Stdout)
os.Exit(0)
}
err = cmd.execute()
if err != nil {
log.Fatal(err)
}
}
func getCommand() (command, error) {
if len(os.Args) <= 1 {
return nil, fmt.Errorf("not command")
}
if os.Args[1] != "start" && os.Args[1] != "stop" {
errFormat := "%s is not a valid command, must be either %q or %q"
return nil, fmt.Errorf(errFormat, os.Args[1], "start", "stop")
}
if os.Args[1] == "start" {
return start{}, nil
}
return stop{}, nil
}
func changeOwner(e environment) error {
cmd := exec.Command(
"chown",
fmt.Sprintf("%s:%s", e.uid, e.gid),
fmt.Sprintf("%s/%s", e.storage, e.username),
"-R",
)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func composeUp(e environment) error {
cmd := exec.Command("docker-compose", "up", "-d")
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, fmt.Sprintf("COMPOSE_PROJECT_NAME=%s", e.username))
cmd.Env = append(cmd.Env, fmt.Sprintf("MEDIA_USERNAME=%s", e.username))
cmd.Env = append(cmd.Env, fmt.Sprintf("MEDIA_BASE_PORT=%s", e.basePort))
cmd.Env = append(cmd.Env, fmt.Sprintf("MEDIA_UID=%s", e.uid))
cmd.Env = append(cmd.Env, fmt.Sprintf("MEDIA_GID=%s", e.gid))
cmd.Env = append(cmd.Env, fmt.Sprintf("MEDIA_STORAGE=%s", e.storage))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return err
}
fmt.Printf(`
Applications Listening on:
Plex (Go to app.plex.tv)
Tranmission %[1]s01
Sonarr %[1]s02
Radarr %[1]s03
Jackett %[1]s04
`, e.basePort)
return nil
}
func checkSudo() error {
u, err := user.Current()
if err != nil {
return fmt.Errorf("cannot take user from environment")
}
if u.Uid != "0" {
return fmt.Errorf("this script must be executed with sudo")
}
return nil
}
func getUser() (uid string, gid string, err error) {
uid = os.Getenv("SUDO_UID")
gid = os.Getenv("SUDO_GID")
if uid == "0" {
return "", "", fmt.Errorf("this script cannot be executed as root")
}
return
}
func loadStartArgs() (username string, basePort string, storage string, err error) {
args := os.Args
if len(args) <= 4 {
printHelp(os.Stdout)
os.Exit(0)
}
username = args[2]
basePort = args[3]
storage = args[4]
port, err := strconv.Atoi(basePort)
if err != nil {
return "", "", "", fmt.Errorf("basePort must be a number, %s is not", basePort)
}
if port < 10 || port > 99 {
return "", "", "", fmt.Errorf("basePort must be a number between 10 and 99")
}
if storage[0] != '/' {
return "", "", "", fmt.Errorf("storage cannot be a relative path")
}
if storage[len(storage)-1] == '/' {
storage = storage[:len(storage)-1]
}
return
}
func loadStopArgs() (username string, err error) {
if len(os.Args) <= 2 {
return "", fmt.Errorf("no user to stop")
}
return os.Args[2], nil
}
func printHelp(w io.Writer) {
text := fmt.Sprintf(`
Start o stop the desired user media server
Commands:
start username basePort storage
stop username
Arguments:
username Name of the user owner of the media server
basePort Number between 10 and 99 used as port prefix
storage Path where all will be stored
Example:
sudo mediaserver ivandelabeldad 42 /media
Storage:
/media/ivandelabeldad
Ports:
Plex 4200
Tranmission 4201
Sonarr 4202
Radarr 4203
Jackett 4204
`)
w.Write([]byte(text))
}