-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessProduct.php
More file actions
276 lines (208 loc) · 10.9 KB
/
ProcessProduct.php
File metadata and controls
276 lines (208 loc) · 10.9 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
<?php
/**
* Created by PhpStorm.
* User: Jackson Meires
* Date: 03/02/2018
* Time: 09:05
*/
namespace Util;
ini_set('display_errors', 1);
ini_set('display_startup_erros', 1);
error_reporting(E_ALL);
require __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/lib/Functions.php';
require_once __DIR__ . '/lib/Git.php';
require_once(__DIR__ . '/lib/git-php-master/src/GitRepository.php');
use GitWrapper\GitWrapper;
use phpseclib\Net\SSH2;
use Session\Session;
class ProcessProduct
{
private $ssh;
private $objConfigSSH;
private $objConfigDB;
private $objConfigProduct;
private static $msg;
private static $host;
private static $sudo;
private static $setTime;
public function init($jsonProduct)
{
$jsonProduct = json_decode($jsonProduct);
self::$setTime = 600;
self::$msg = '';
set_time_limit(self::$setTime);
ini_set('max_execution_time', self::$setTime);
ini_set("default_socket_timeout", self::$setTime);
define('NET_SSH2_LOGGING', SSH2::LOG_COMPLEX);
$this->objConfigDB = Functions::getConfigDB();
$this->objConfigProduct = Functions::getConfigProduct($jsonProduct->id);
Functions::createConfigDBiniProduct((array)$this->objConfigDB);
$this->connSSH();
}
function connSSH()
{
try {
$this->objConfigSSH = Functions::getConfigSSH();
$this->ssh = new SSH2("" . $this->objConfigSSH->host . "");
self::$host = $this->objConfigSSH->host;
self::$sudo = "echo " . $this->objConfigSSH->password . " | sudo -S ";
if (!$this->ssh->login("" . $this->objConfigSSH->user . "", "" . $this->objConfigSSH->password . "")) {
throw new \Exception('Login Failed in SSH');
}
$this->cloneProduct();
} catch (\Exception $e) {
self::$msg .= $e->getMessage();
}
}
static function getHost()
{
$objConfigSSH = Functions::getConfigSSH();
return $objConfigSSH->host;
}
static function getDBType()
{
$objConfigSSH = Functions::getConfigDB();
return $objConfigSSH->type;
}
function cloneProduct()
{
try {
$log = '';
$wrapper = new GitWrapper();
$wrapper->setTimeout(self::$setTime);
$this->objConfigProduct->name = strtolower($this->objConfigProduct->name);
$createLog = 'cd ' . $this->objConfigSSH->dir . ' && ' . self::$sudo . ' chmod -R 777 wizard_lps/log';
$log .= $this->run($createLog);
$copy = 'cd ' . $this->objConfigSSH->dir . ' && mkdir ' . $this->objConfigProduct->name . ' && ' . self::$sudo . ' chmod -R 777 ' . $this->objConfigProduct->name;
$log .= $this->run($copy);
$log .= $wrapper->cloneRepository("" . $this->objConfigProduct->url_repository . "", '' . $this->objConfigSSH->dir . $this->objConfigProduct->name . '');
if (file_exists("log/git.log")) {
unlink("log/git.log");
}
if (file_exists("log/restore_db.log")) {
unlink("log/restore_db.log");
}
if (!empty($this->objConfigProduct->name_file_database)) {
$this->dumpDB();
}
$objSession = Session::getInstance();
if ($objSession->__get('code_generator') == 2) {
$this->copyFilesGenerator();
}
clearstatcache();
file_put_contents("log/git.log", $log);
} catch (Exception $e) {
self::$msg .= $e->getMessage();
}
}
function copyFilesGenerator()
{
$log = '';
##### copy page #####
if ($this->objConfigProduct->name == "bloglps") {
$dir_app_product = $this->objConfigSSH->dir . $this->objConfigProduct->name . '/admin/app/';
} else {
$dir_app_product = $this->objConfigSSH->dir . $this->objConfigProduct->name . '/app/';
}
$db_conn = 'cd ' . $this->objConfigSSH->dir . ' && ' . self::$sudo . ' cp -ar wizard_lps/code_generator/app/files/* ' . $dir_app_product;
$log .= $this->run($db_conn);
##### copy page #####
clearstatcache();
file_put_contents("log/copy_files_generator.log", $log);
}
function dumpDB()
{
try {
if (!empty($this->objConfigDB->name)) {
$log = '';
$sqlFileNameDump = $this->objConfigProduct->name_file_database; // dump_db_name.sql
if ($this->objConfigDB->type == 'pgsql') {
if (empty($this->objConfigDB->docker_container)) {
############# Commands PSQL #############
$pgsqlPassword = " export PGPASSWORD=" . $this->objConfigDB->pass . "";
$psqlCommand = $pgsqlPassword . " && psql -h " . $this->objConfigDB->host . " -U " . $this->objConfigDB->user . " -d " . $this->objConfigDB->name . " -f ";
$dirFileWizard = "cd " . $this->objConfigSSH->dir . "wizard_lps" . DIRECTORY_SEPARATOR . "database" . DIRECTORY_SEPARATOR . "scripts" . DIRECTORY_SEPARATOR . "";
$dirFileProduct = "cd " . $this->objConfigSSH->dir . $this->objConfigProduct->name . DIRECTORY_SEPARATOR . $this->objConfigProduct->dir_file_database;
$commandDump = $psqlCommand . $sqlFileNameDump . "";
############# Commands PSQL #############
$db_restore = $dirFileProduct . " && " . self::$sudo . " chmod 777 " . $sqlFileNameDump . " && " . $commandDump;
$log .= $this->run($db_restore);
$db_create = $dirFileWizard . " && " . self::$sudo . " chmod 777 create_db_config_product_pgsql.sql | " . $psqlCommand . " create_db_config_product_pgsql.sql";
$log .= $this->run($db_create);
$db_insert = $dirFileWizard . " && " . self::$sudo . " chmod 777 insert_config_product_pgsql.sql | " . $psqlCommand . " insert_config_product_pgsql.sql";
$log .= $this->run($db_insert);
$db_update = $dirFileWizard . " && " . self::$sudo . " chmod 777 enable_pages_config_product_pgsql_ceres.sql | " . $psqlCommand . " enable_pages_config_product_pgsql_ceres.sql";
$log .= $this->run($db_update);
} else {
############# Docker Commands PSQL #############
$psqlCommand = " docker exec -i " . $this->objConfigDB->docker_container . " psql -U " . $this->objConfigDB->user . " -d " . $this->objConfigDB->name . "";
$commandDump = " cat " . $sqlFileNameDump . " | " . $psqlCommand;
$dirFileWizard = "cd " . $this->objConfigSSH->dir . "wizard_lps" . DIRECTORY_SEPARATOR . "database" . DIRECTORY_SEPARATOR . "scripts" . DIRECTORY_SEPARATOR . "";
$dirFileProduct = "cd " . $this->objConfigSSH->dir . $this->objConfigProduct->name . DIRECTORY_SEPARATOR . $this->objConfigProduct->dir_file_database;
############# Docker Commands PSQL #############
$db_restore = $dirFileProduct . " && " . self::$sudo . " chmod 777 " . $sqlFileNameDump . " && " . $commandDump;
$log .= $this->run($db_restore);
$db_create = $dirFileWizard . " && " . self::$sudo . " chmod 777 create_db_config_product_pgsql.sql && cat create_db_config_product_pgsql.sql | " . $psqlCommand;
$log .= $this->run($db_create);
$db_insert = $dirFileWizard . " && " . self::$sudo . " chmod 777 insert_config_product_pgsql.sql && cat insert_config_product_pgsql.sql | " . $psqlCommand;
$log .= $this->run($db_insert);
$db_update = $dirFileWizard . " && " . self::$sudo . " chmod 777 enable_pages_config_product_pgsql_ceres.sql && cat enable_pages_config_product_pgsql_ceres.sql | " . $psqlCommand;
$log .= $this->run($db_update);
}
} else if ($this->objConfigDB->type == 'mysql') {
echo $this->ssh->exec("cd " . $this->objConfigSSH->dir . $this->objConfigProduct->name . "/database/ && sudo mysql -h localhost -U " . $this->objConfigDB->user . " " . $this->objConfigDB->name . " -f db_teste.sql");
} else if ($this->objConfigDB->type == 'sqlite') {
############# SQlite Commands #############
$dirFileWizard = $this->objConfigSSH->dir . "wizard_lps" . DIRECTORY_SEPARATOR . "database" . DIRECTORY_SEPARATOR . "scripts" . DIRECTORY_SEPARATOR . "";
$dirFileProduct = $this->objConfigSSH->dir . $this->objConfigProduct->name . DIRECTORY_SEPARATOR . $this->objConfigProduct->dir_file_database;
$sqLiteCommand = " sqlite3 " . $dirFileProduct . $sqlFileNameDump;
############# SQlite Commands #############
$db_restore = "cd " . $dirFileProduct . " && " . self::$sudo . " chmod 777 " . $sqlFileNameDump;
$log .= $this->run($db_restore);
$db_create = "cd " . $dirFileWizard . " && " . self::$sudo . " chmod 777 create_db_config_product_sqlite.sql && cat create_db_config_product_sqlite.sql | " . $sqLiteCommand;
$log .= $this->run($db_create);
$db_insert = "cd " . $dirFileWizard . " && " . self::$sudo . " chmod 777 insert_config_product_sqlite.sql && cat insert_config_product_sqlite.sql | " . $sqLiteCommand;
$log .= $this->run($db_insert);
}
if ($this->objConfigProduct->name == "cereslps") {
##### copy page #####
$dir_config_product = $this->objConfigSSH->dir . $this->objConfigProduct->name . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR;
$db_conn = 'cd ' . $this->objConfigSSH->dir . ' && ' . self::$sudo . ' mv -f wizard_lps' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'pg_ceres.ini ' . $dir_config_product;
$log .= $this->run($db_conn);
##### copy page #####
}
clearstatcache();
file_put_contents("log/restore_db.log", $log);
}
} catch (Exception $e) {
self::$msg .= $e->getMessage();
}
}
function run($command)
{
$this->ssh->write($command . "\n");
$log = $this->ssh->read();
return $log;
}
/**
* @return string
*/
public static function getMsg()
{
return self::$msg;
}
/**
* set string
*/
public function setMsg($msg)
{
return self::$msg = $msg;
}
}
$objPP = new ProcessProduct();
if (!empty($_GET['json'])) {
$objPP->init($_GET['json']);
} else {
$objPP->setMsg('JSON is null');
}