This repository was archived by the owner on Jan 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdaenerys-install.php
More file actions
339 lines (283 loc) · 8.68 KB
/
Copy pathdaenerys-install.php
File metadata and controls
339 lines (283 loc) · 8.68 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/php
<?php
/**
* Installer script for daenerys
*/
// @ToDo: Add check to abort this script of not cli.
/**
* @param string $text
*/
function out(string $text, int $tab = 0)
{
print(str_repeat("\t", $tab));
print($text . "\n");
}
/**
* Returns true if a command successfully runs (= should exist)
* @param $command
* @return bool
*/
function command_exists($command)
{
$return_val = 0;
$output = "";
$return = exec($command . " 2> /dev/null", $output, $return_val);
return $return_val === 0;
}
/**
* Runs a command and returns the return value and the captured output
* @param $command
* @return array
*/
function run_command($command)
{
$return_val = 0;
$output = "";
$return = exec($command . "", $output, $return_val);
return [$return_val, $output];
}
/**
* Removes a complete directory
* @param $directory
* @return bool
*/
function remove_complete_dir($directory)
{
$files = scandir($directory);
foreach ($files as $file) {
if ($file === "." or $file === "..") {
continue;
}
$file = "$directory/$file";
if (is_dir($file)) {
remove_complete_dir($file);
continue;
}
unlink($file);
}
return rmdir($directory);
}
/**
* Tries to parse cli options
* @param array $argv
* @param int $start
* @return array
*/
function get_cli_options(array $argv, int $start = 2)
{
$pos = 0;
$options = [];
$last_option = null;
foreach ($argv as $arg) {
$pos++;
if ($pos <= $start) {
continue;
}
if (substr($arg, 0, 2) == "--") {
$last_option = substr($arg, 2);
$options[$last_option] = True;
continue;
}
if ($last_option === null) {
$last_option = ".";
$options[$last_option] = [];
}
if (is_bool($options[$last_option])) {
$options[$last_option] = $arg;
} elseif (is_array($options[$last_option])) {
$options[$last_option][] = $arg;
} else {
$v = $options[$last_option];
$options[$last_option] = [$v, $arg];
}
}
return $options;
}
$argOptions = get_cli_options($argv);
$index = <<<'PHP'
<?php
use LotGD\Crate\WWW\Kernel;
use Symfony\Component\Debug\Debug;
use Symfony\Component\HttpFoundation\Request;
require __DIR__.'/vendor/autoload.php';
$_SERVER['APP_ENV'] = "prod";
$_SERVER['APP_DEBUG'] = false;
if ($_SERVER['APP_DEBUG']) {
umask(0000);
Debug::enable();
}
if ($trustedProxies = $_SERVER['TRUSTED_PROXIES'] ?? $_ENV['TRUSTED_PROXIES'] ?? false) {
Request::setTrustedProxies(explode(',', $trustedProxies), Request::HEADER_X_FORWARDED_ALL ^ Request::HEADER_X_FORWARDED_HOST);
}
if ($trustedHosts = $_SERVER['TRUSTED_HOSTS'] ?? $_ENV['TRUSTED_HOSTS'] ?? false) {
Request::setTrustedHosts([$trustedHosts]);
}
$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
PHP;
$config = <<<'YML'
database:
dsn: "sqlite:%cwd%config/daenerys-run.db3"
name: daenerys
user: root
password:
disableAutoSchemaUpdate: false
game:
epoch: 2016-07-01 00:00:00.0 -8
offsetSeconds: 0
daysPerDay: 1
logs:
path: ../logs
YML;
$composer = <<<'JSON'
{
"name": "local/test",
"require": {
"lotgd/crate-html": "{{crate-version}}",
"lotgd/core": "{{core-version}}"
},
"license": "AGPL3",
"authors": [
{
"name": "Daenerys installation script",
"email": "localhost@example.com"
}
],
"repositories": [
{
"type": "composer",
"url": "https://raw.githubusercontent.com/lotgd/packages/master/build/packages.json"
}
],
"minimum-stability": "dev",
"prefer-stable": true
}
JSON;
out("Daenerys installer, version 0.6.0");
if($argc < 2) {
out(<<<MSG
To use daenerys, you must use one of the following modes:
- check, checks the environment if daenerys is installable
- install, tries to install daenerys. Use --nointeraction to supress interactions.
MSG
);
exit(1);
}
if ($argv[1] == "check") {
out("Checking the environment");
$checks = [
"PHP" => ["8.0 or later", function () {
return version_compare("8.0", PHP_VERSION, "<=");
}],
"Directory" => ["writable", function () {
return fileperms('.') & 0x0080;
}],
"Composer" => ["available", function () {
return command_exists("composer --version");
}],
];
$faults = 0;
foreach ($checks as $what => [$adjective, $callback]) {
if ($callback()) {
out(sprintf("[+] %s is %s.", $what, $adjective), 1);
} else {
out(sprintf("[-] %s is not %s.", $what, $adjective), 1);
$faults++;
}
}
if ($faults > 0) {
print("Not all checks passed\n\n");
exit(1);
}
} elseif ($argv[1] == "install") {
out("Installation of daenerys");
// @ToDo: Let the user set some options here.
$crateVersion = "dev-master";
$coreVersion = "dev-master as 0.6.0";
$composer = str_replace("{{crate-version}}", $crateVersion, $composer);
$composer = str_replace("{{core-version}}", $coreVersion, $composer);
file_put_contents("composer.json", $composer);
$output = run_command("composer update");
if ($output[0] > 0) {
print("It looks like something went wrong with composer install.\n\n");
exit(1);
}
mkdir("config");
mkdir("logs");
mkdir("css");
mkdir("icons");
file_put_contents("index.php", $index);
file_put_contents("config/lotgd.yml", $config);
# Initialise database
`vendor/bin/daenerys database:init`;
`vendor/bin/daenerys crate:role:add ROLE_SUPERUSER`;
`vendor/bin/daenerys crate:role:add ROLE_SCENE_EDITOR`;
if (!isset($argOptions["nointeraction"])) {
$name = readline("Admin account name [admin]: ") ?: "admin";
$password = readline("Password [changeme]: ") ?: "changeme";
$email = readline("Email address for login [admin@example.com]: ") ?: "admin@example.com";
`vendor/bin/daenerys crate:user:add "$name" "$email" "$password"`;
} else {
$name = "admin";
`vendor/bin/daenerys crate:user:add admin "admin@example.com" changeme`;
}
`vendor/bin/daenerys crate:role:grant ROLE_SUPERUSER $name`;
`vendor/bin/daenerys crate:adminToolbox:add scenes "Scene Toolbox" "LotGD\\Crate\\WWW\\AdministrationToolboxes\\SceneToolbox" ROLE_SUPERUSER ROLE_SCENE_EDITOR`;
`vendor/bin/daenerys crate:adminToolbox:add users "User Toolbox" "LotGD\\Crate\\WWW\\AdministrationToolboxes\\UserToolbox" ROLE_SUPERUSER`;
`vendor/bin/daenerys crate:adminToolbox:add characters "Character Toolbox" "LotGD\\Crate\\WWW\\AdministrationToolboxes\\CharacterToolbox" ROLE_SUPERUSER`;
# Install assets
`cp vendor/lotgd/crate-html/public/css/* css`;
`cp vendor/lotgd/crate-html/public/icons/* icons`;
# Install modules if given
if (isset($argOptions["installDefaultModules"])) {
out("Install default modules.");
$moduleList = [
"lotgd/module-village",
"lotgd/module-scene-bundle",
"lotgd/module-new-day",
"lotgd/module-gender",
"lotgd/module-res-charstats",
"lotgd/module-forest",
"lotgd/module-training",
"lotgd/module-res-wealth",
"lotgd/module-dragon-kills",
];
$moduleList = implode(" ", $moduleList);
`composer require $moduleList`;
}
`vendor/bin/daenerys database:schemaUpdate`;
`vendor/bin/daenerys module:register`;
`vendor/bin/console cache:clear`;
} elseif ($argv[1] == "install-module") {
if (isset($argv[2])) {
`composer update`;
`composer require {$argv[2]}`;
`vendor/bin/daenerys database:schemaUpdate`;
`vendor/bin/daenerys module:register`;
`vendor/bin/console cache:clear`;
} else {
print("Second argument must be a module.");
}
} elseif ($argv[1] == "clean") {
@remove_complete_dir("vendor");
@remove_complete_dir("config");
@remove_complete_dir("logs");
@remove_complete_dir("css");
@remove_complete_dir("icons");
@unlink("composer.json");
@unlink("composer.lock");
@unlink("index.html");
@unlink("index.php");
@unlink("bundle.js");
@unlink("style.css");
out("Installation removed.");
} elseif ($argv[1] == "run-test") {
# Runs a test server (do not use for production!)
`php -S localhost:8000 -t .`;
} else {
out("Unknown mode.\n");
}
print("\n\n");