forked from ganggo/ganggo
/
updater.go
293 lines (273 loc) · 8.56 KB
/
updater.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package main
//
// GangGo Application Server
// Copyright (C) 2017 Lukas Matt <lukas@zauberstuhl.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
import (
"io/ioutil"
"time"
"fmt"
"strings"
"flag"
"github.com/jpillora/overseer"
"github.com/jpillora/overseer/fetcher"
"os"
"os/exec"
"github.com/revel/config"
"runtime"
"gopkg.in/AlecAivazis/survey.v1"
)
var (
mode string = "prod"
updateChannel string = "alpha"
version string // version will be defined on compile time
packageName = "github.com/ganggo/ganggo"
srcDir = "src/" + packageName
configPath = srcDir + "/conf/app.conf"
userConfigPath = "app.conf"
interval = 10 * time.Minute
)
var answers = struct {
Protocol string
Tld string
DatabaseDriver string
DatabaseUsername string
DatabasePassword string
DatabaseHost string
DatabaseName string
DatabaseDSN string
Relay bool
RelayTags string
}{}
var questions =[]*survey.Question{
{
Name: "protocol",
Prompt: &survey.Select{
Message: "Which protocol will be used for the webpage?",
Options: []string{"http://", "https://"},
Default: "http://",
},
Validate: survey.Required,
},
{
Name: "tld",
Prompt: &survey.Input{
Message: "Which top level domain will be used?",
Help: "This could be example.com, pod.example.com or example.com:8080",
Default: "localhost:9000",
},
Validate: survey.Required,
},
{
Name: "databaseDriver",
Prompt: &survey.Select{
Message: "What database do you want to use?",
Options: []string{"mssql", "mysql", "postgres", "sqlite"},
},
Validate: func (val interface{}) error {
if str, ok := val.(string) ; ok {
switch str {
case "mysql":
answers.DatabaseDSN = "%s:%s@tcp(%s)/%s?parseTime=true"
case "postgres":
answers.DatabaseDSN = "user=%s password=%s host=%s dbname=%s sslmode=disable"
case "sqlite":
prompt := &survey.Input{
Message: "Please specify the database file path:",
Default: "./ganggo.db",
}
survey.AskOne(prompt, &answers.DatabaseDSN, nil)
default:
prompt := &survey.Input{
Message: "Please specify your database DSN (Data Source Name):",
Help: "e.g. [username[:password]@][protocol[(address)]]/dbname[?param1=value1&...¶mN=valueN]",
}
survey.AskOne(prompt, &answers.DatabaseDSN, nil)
}
}
return nil
},
},
{
Name: "databaseUsername",
Prompt: &survey.Input{Message: "What is the database username?"},
Validate: survey.Required,
},
{
Name: "databasePassword",
Prompt: &survey.Password{Message: "What is the database password?"},
Validate: survey.Required,
},
{
Name: "databaseHost",
Prompt: &survey.Input{
Message: "What is the database hostname?",
Default: "localhost",
},
Validate: survey.Required,
},
{
Name: "databaseName",
Prompt: &survey.Input{
Message: "What is the database name?",
Default: "ganggo",
},
Validate: survey.Required,
},
{
Name: "relay",
Prompt: &survey.Confirm{
Help: "The relay is disabled on default in respect of privacy matters!\n" +
"ⓘ You should think about enabling it! The software will register at\n" +
"ⓘ the-federation.info and start using the social-relay. After using it you\n" +
"ⓘ will benefit from public posts and comments from the relay and fill your\n" +
"ⓘ pod up more quicklier then just using the diaspora protocol!",
Message: "Enable the relay for fetching public entities?",
Default: false,
},
Validate: func (val interface{}) error {
var relayType string
if enabled, ok := val.(bool); ok && enabled {
prompt := &survey.Select{
Message: "Do you want to send/receive all public entities or only tags?",
Options: []string{"tags", "all"},
Default: "all",
}
survey.AskOne(prompt, &relayType, nil)
if relayType == "tags" {
prompt := &survey.Input{
Message: "Which tags do you want to be relayed?",
Help: "e.g. social,network,politics",
}
survey.AskOne(prompt, &answers.RelayTags, nil)
}
}
return nil
},
},
}
func init() {
flag.StringVar(&mode, "mode", mode, "Start revel server in production or development mode")
flag.StringVar(&updateChannel, "channel", updateChannel, "Specify an update channel e.g. stable, beta or alpha.\n" +
"\tYou can disable auto-updates with '-channel disable'")
flag.StringVar(&userConfigPath, "config", userConfigPath, "Set a different config path")
}
func main() {
// Parse command-line flags
flag.Parse()
if updateChannel != "alpha" {
interval = 1 * time.Hour
}
// Delete old files
os.RemoveAll("src")
// Extract assets and golang libraries
err := RestoreAssets("", "src")
if err != nil {
panic(err)
}
// if app.conf is not in the current
// directory start the wizard and create it
if _, err := os.Stat(userConfigPath); os.IsNotExist(err) {
if err := survey.Ask(questions, &answers); err != nil {
panic(err)
}
// generate new configuration
var section = "ganggo"
config, err := config.ReadDefault(configPath + ".example")
if err != nil { panic(err) }
config.AddOption(section, "proto", answers.Protocol)
config.AddOption(section, "address", answers.Tld)
config.AddOption(section, "db.driver", answers.DatabaseDriver)
config.AddOption(section, "db.dsn", answers.DatabaseDSN)
config.AddOption(section, "db.user", answers.DatabaseUsername)
config.AddOption(section, "db.password", answers.DatabasePassword)
config.AddOption(section, "db.host", answers.DatabaseHost)
config.AddOption(section, "db.database", answers.DatabaseName)
if answers.Relay {
config.AddOption(section, "relay.subscribe", "true")
} else {
config.AddOption(section, "relay.subscribe", "false")
}
if answers.RelayTags == "" {
config.AddOption(section, "relay.scope", "all")
} else {
config.AddOption(section, "relay.scope", "tags")
config.AddOption(section, "relay.tags", answers.RelayTags)
}
err = config.WriteFile(userConfigPath, 0644, "AUTO-GENERATED BY GANGGO WIZARD")
if err != nil { panic(err) }
}
overseer.Run(overseer.Config{
Program: prog,
Fetcher: &fetcher.Github{
User: "ganggo",
Repo: "ganggo",
Asset: AssetValidator,
Interval: interval,
},
Debug: true,
})
}
// Add the ability to use multiple update channel via Github-fetcher
func AssetValidator(filename string) bool {
defaultAsset := strings.Contains(filename, runtime.GOOS) && strings.Contains(filename, runtime.GOARCH)
if defaultAsset && updateChannel == "stable" {
return true
}
return defaultAsset && strings.Contains(filename, updateChannel)
}
func prog(state overseer.State) {
buildID := int32(time.Now().Unix())
binFile := fmt.Sprintf("./ganggo.%s.%d", version, buildID)
// clean-up binaries
defer func() {
os.Remove(binFile)
}()
// restore app binary
asset, err := ganggoBytes()
if err != nil {
panic(err)
}
err = ioutil.WriteFile(binFile, asset, 0755)
if err != nil {
panic(err)
}
// read config file from wizard and copy it to src folder
config, err := config.ReadDefault(userConfigPath)
if err != nil {
panic(err)
}
err = config.WriteFile(configPath, 0644, "AUTO-GENERATED BY GANGGO WIZARD")
if err != nil {
panic(err)
}
cmd := exec.Command(binFile, "-importPath", packageName,
"-srcPath", "src", "-runMode", mode)
if mode != "prod" {
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
fmt.Printf("app#%s %s listening...\n", version, state.ID)
if err := cmd.Start(); err != nil {
panic(err)
}
// wait for overseer to restart or terminate
<-state.GracefulShutdown
// send kill signal to old process
cmd.Process.Kill()
fmt.Printf("app#%s %s exiting...\n", version, state.ID)
}