-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeploy.php
152 lines (134 loc) · 5.9 KB
/
Deploy.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
<?php
use League\Flysystem\Filesystem;
use League\Flysystem\Ftp\FtpAdapter;
use League\Flysystem\Ftp\FtpConnectionOptions;
use Monolog\Handler\ErrorLogHandler;
use Monolog\Logger;
use PhpDeploy\AbstractDefaultDeploy;
require_once __DIR__.'/vendor/autoload.php';
class Deploy extends AbstractDefaultDeploy {
protected function populateFolder(): void {
$fs = new Symfony\Component\Filesystem\Filesystem();
$build_folder_path = $this->getLocalBuildFolderPath();
$fs->mirror(__DIR__, $build_folder_path);
}
protected function getFlysystemFilesystem(): Filesystem {
if ($this->target === 'cyon') {
$options = FtpConnectionOptions::fromArray([
'host' => 's007.cyon.net', // required
'root' => '/', // required
'username' => $this->username, // required
'password' => $this->password, // required
'port' => 21,
'ssl' => true,
'timeout' => 90,
]);
$adapter = new FtpAdapter($options);
return new Filesystem($adapter);
}
throw new Exception("Target must be `cyon`");
}
public function getRemotePublicPath(): string {
if ($this->target === 'cyon') {
if ($this->environment === 'staging') {
return 'public_html/staging.hatt.style';
}
if ($this->environment === 'prod') {
return 'public_html/deploy.hatt.style';
}
throw new Exception("Environment must be `staging` or `prod`");
}
throw new Exception("Target must be `cyon`");
}
public function getRemotePublicUrl(): string {
if ($this->target === 'cyon') {
if ($this->environment === 'staging') {
return "https://staging.hatt.style";
}
if ($this->environment === 'prod') {
return "https://deploy.hatt.style";
}
throw new Exception("Environment must be `staging` or `prod`");
}
throw new Exception("Target must be `cyon`");
}
public function getRemotePrivatePath(): string {
if ($this->target === 'cyon') {
if ($this->environment === 'staging') {
return 'private_files/staging';
}
if ($this->environment === 'prod') {
return 'private_files/prod';
}
throw new Exception("Environment must be `staging` or `prod`");
}
throw new Exception("Target must be `cyon`");
}
/** @return array<string, string> */
public function install(string $public_path): array {
$this->logger->info("Prepare for installation (env={$this->environment})...");
$fs = new Symfony\Component\Filesystem\Filesystem();
$fs->copy(__DIR__.'/../../.env.local', __DIR__.'/.env.local');
$public_url = $this->getRemotePublicUrl();
$staging_token = null;
$install_path = $public_path;
if ($this->environment === 'staging') {
$entries = scandir($public_path);
foreach ($entries as $entry) {
$path = "{$public_path}/{$entry}";
if ($entry[0] !== '.' && is_dir($path) && $fs->exists("{$path}/_TOKEN_DIR_WILL_BE_REMOVED.txt")) {
$fs->remove($path);
}
}
$staging_token = str_replace(['+', '/', '='], ['-', '_', ''], base64_encode(openssl_random_pseudo_bytes(18)));
$this->logger->info("--------------------------------------------");
$this->logger->info(" {$public_url}/{$staging_token}/ ");
$this->logger->info("--------------------------------------------");
$install_path = "{$public_path}/{$staging_token}";
}
$getPublicPathForSubdomain = function (string $subdomain) use ($install_path): string {
return str_replace($this->getRemotePublicPath(), "public_html/{$subdomain}", $install_path);
};
if ($this->environment === 'staging') {
// staging.hatt.style
$base_public = $getPublicPathForSubdomain('staging.hatt.style');
$this->installForSubdomain($base_public);
file_put_contents("{$install_path}/_TOKEN_DIR_WILL_BE_REMOVED.txt", '');
}
if ($this->environment === 'prod') {
// hatt.style
$base_public = $getPublicPathForSubdomain('hatt.style');
$this->installForSubdomain($base_public);
// flatastic.hatt.style
$flatastic_public = $getPublicPathForSubdomain('flatastic.hatt.style');
$this->installForSubdomain($flatastic_public);
// frame.hatt.style
$flatastic_public = $getPublicPathForSubdomain('frame.hatt.style');
$this->installForSubdomain($flatastic_public);
}
return [
'staging_token' => $staging_token,
];
}
protected function installForSubdomain(string $install_path): void {
$levels = count(explode('/', $install_path)) - count(explode('/', __DIR__)) + 4;
$fs = new Symfony\Component\Filesystem\Filesystem();
$fs->copy(__DIR__.'/public/.htaccess', "{$install_path}/.htaccess", true);
$index_path = "{$install_path}/index.php";
$index_contents = file_get_contents(__DIR__.'/public/index.php');
$updated_index_contents = str_replace(
"require_once dirname(__DIR__).'/vendor/autoload_runtime.php';",
"require_once dirname(__DIR__, {$levels}).'/private_files/{$this->environment}/deploy/live/vendor/autoload_runtime.php';",
$index_contents,
);
unlink($index_path);
file_put_contents($index_path, $updated_index_contents);
}
}
if (isset($_SERVER['argv'])) {
$deploy = new Deploy();
$logger = new Logger('deploy');
$logger->pushHandler(new ErrorLogHandler());
$deploy->setLogger($logger);
$deploy->cli();
}