-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSimpleSamlInstaller.php
182 lines (150 loc) · 5.41 KB
/
SimpleSamlInstaller.php
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
<?php
namespace UoN\SimpleSamlInstaller;
use Dotenv;
use Symfony\Component\Filesystem\Filesystem;
class SimpleSamlInstaller
{
/**
* @var Filesystem
*/
private $filesystem;
/**
* @var string
*/
private $env = 'production';
/**
*
*/
public function __construct()
{
$this->filesystem = new Filesystem();
$this->say("\n\n===== SimpleSaml Installer START =====\n");
$this->setEnvironment();
$this->setPaths();
}
/**
* Creates new instance of installer and runs it
* @return mixed
*/
public static function run()
{
return (new static)->install();
}
/**
*
*/
private function install()
{
// check for essentials
$this->checkEssentials();
// config templates
$this->copy('config');
// metadata templates
$this->copy('metadata');
// certificates
$this->copy('cert');
// copy modules
$this->copy('modules');
// enable modules
$this->enableModules(['cron', 'metarefresh', 'discopower']);
}
/**
* @param string $what
*/
private function say($what)
{
print $what . "\n";
}
/**
* @param string $what
*/
private function sayLastWords($what)
{
$this->say("\nERROR: " . $what);
$this->say("Quitting...\n\n");
exit();
}
/**
* Does some basic checks for folder existence, etc
*/
private function checkEssentials()
{
// check folders
if (!$this->filesystem->exists(SIMPLESAML_INSTALLER_DATA_DIR)) $this->sayLastWords('Install dir not found: ' . SIMPLESAML_INSTALLER_DATA_DIR);
$foldersToCheck = array('config', 'metadata', 'modules', 'cert');
foreach ($foldersToCheck as $folder) {
if (!$this->filesystem->exists(SIMPLESAML_INSTALLER_DATA_DIR . DIRECTORY_SEPARATOR . $folder)) {
$this->sayLastWords(sprintf('%s dir not found: %s',
ucfirst($folder),
SIMPLESAML_INSTALLER_DATA_DIR . DIRECTORY_SEPARATOR . $folder));
}
}
if (!$this->filesystem->exists(SIMPLESAMLPHP_PLUGIN_DIR)) $this->sayLastWords('Simplesamlphp plugin dir not found: ' . SIMPLESAMLPHP_PLUGIN_DIR);
}
/**
* Looks for APP_ENV env variable and sets the environment to that if present
*/
private function setEnvironment()
{
$this->say('Setting environment...');
try {
$dotenv = new Dotenv();
$dotenv->load(realpath(__DIR__ . '/../../../'));
} catch (\InvalidArgumentException $e) {
//
}
if ($env = getenv('APP_ENV')) {
$this->env = $env;
}
$this->say('Environment: ' . $this->env);
}
/**
* TODO: set path correctly, allow override from arguments (--data_path --simplesamlphp_path ?)
*/
private function setPaths()
{
$this->say('Setting paths...');
// expects to be in /vendor/uon/simplesamlinstaller
define('SIMPLESAML_INSTALLER_DATA_DIR', realpath(__DIR__ . '/../../../docs/install/' . $this->env . '/simplesaml/') . DIRECTORY_SEPARATOR);
define('SIMPLESAMLPHP_PLUGIN_DIR', realpath(__DIR__ . '/../../simplesamlphp/simplesamlphp/') . DIRECTORY_SEPARATOR);
}
/**
* Copies contents of the specified folder
* @param $folder
*/
private function copy($folder)
{
$this->say('Copying ' . $folder . '...');
$source = SIMPLESAML_INSTALLER_DATA_DIR . $folder;
$target = SIMPLESAMLPHP_PLUGIN_DIR . $folder;
$directoryIterator = new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS);
$iterator = new \RecursiveIteratorIterator($directoryIterator, \RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $item) {
if ($item->isDir()) {
$this->filesystem->mkdir($target . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
$this->say(' Make dir: ' . $target . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
} else {
$this->filesystem->copy($item, $target . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
$this->say(' Copy ' . basename($item) . ' to ' . $target . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
}
}
}
/**
* Creates 'enable' file in specified module(s) to enable them
* @param array|string $modules
*/
private function enableModules($modules)
{
if (!is_array($modules)) $modules = array($modules);
foreach ($modules as $module) {
if (!$this->filesystem->exists(SIMPLESAMLPHP_PLUGIN_DIR . 'modules' . DIRECTORY_SEPARATOR . $module)) {
$this->say(sprintf('SKIPPING: %s module not found in: %s',
ucfirst($module),
SIMPLESAMLPHP_PLUGIN_DIR . 'modules' . DIRECTORY_SEPARATOR . $module));
continue;
}
$this->filesystem->touch(SIMPLESAMLPHP_PLUGIN_DIR . 'modules' . DIRECTORY_SEPARATOR . $module . DIRECTORY_SEPARATOR . 'enable');
$this->say('Module "' . $module . '" enabled.');
}
}
}