-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwf.php
239 lines (179 loc) · 6.85 KB
/
wf.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
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
<?php
/**
* Created by PhpStorm.
* User: qunabu
* Date: 11.08.17
* Time: 18:09
*/
if (PHP_SAPI === 'cli') {
} else {
die('nope');
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
include(__DIR__ . '/../vendor/autoload.php');
} else if (is_file(__DIR__ . '/vendor/autoload.php')) {
include(__DIR__ . '/vendor/autoload.php');
}
function console_log($msg) {
echo $msg."\n";
}
function eoltrim($string) {
return trim(str_replace(array("\n","\r"), '', $string));
}
function generateRandomPassword() {
//Initialize the random password
$password = '';
//Initialize a random desired length
$desired_length = rand(8, 12);
for($length = 0; $length < $desired_length; $length++) {
//Append a random ASCII character (including symbols)
$password .= chr(rand(32, 126));
}
return $password;
}
use FortyTwoStudio\WebFactionPHP\WebFactionClient;
use FortyTwoStudio\WebFactionPHP\WebFactionException;
use phpseclib\Net;
try
{
$login = readline("login: ");
readline_add_history($login);
$password = readline("password: ");
readline_add_history($password);
$projectname = readline("project name: ");
readline_add_history($projectname);
if ($login && $password && $projectname) {
console_log("logging in.... please wait");
} else {
console_log('must provide $login && $password && $projectname');
}
//** loggin with SSH and trying to clone repo */
$ssh = new Net\SSH2('qunabu.webfactional.com');
if (!$ssh->login($login, $password)) {
console_log('Login Failed');
}
// create a connection to the API, use your own credentials here, obvs
$wf = new WebFactionClient($login, $password);
$ip = '185.119.174.181';
//$db_pass = WebFactionClient::generatePassword(21); //only in php7
$db_pass = generateRandomPassword();
$exists = array(
'app'=>false,
'db_user'=>false,
'db'=>false,
'website'=>false,
'domain'=>false,
);
$apps = $wf->listApps();
foreach($apps as $app) {
if ($app['name'] == $projectname) {
$exists['app'] = true;
}
}
$websites = $wf->listWebsites();
foreach($websites as $website) {
if ($website['name'] == $projectname) {
$exists['website'] = true;
}
}
$domains = $wf->listDomains();
foreach($domains as $domain) {
if ($domain['domain'] == 'qunabu.com') {
$exists['domain'] = in_array($projectname, $domain['subdomains']);
}
}
$db_users = $wf->listDbUsers();
foreach($db_users as $db_user) {
if ($db_user['username'] == $projectname) {
$exists['db_user'] = true;
}
}
$dbs = $wf->listDbs();
foreach($dbs as $db) {
if ($db['name'] == $projectname) {
$exists['db'] = true;
}
}
//DB USER
if (!$exists['db_user']) {
console_log('user doesn`t $exists, creating one....');
$user = $wf->createDbUser($projectname,$db_pass,'mysql');
} else {
console_log('user exists, setting up new password one....');
$user = $wf->changeDbUserPassword($projectname,$db_pass,'mysql');
}
//DB
if (!$exists['db']) {
console_log('db doesn`t $exists, creating one....');
$database = $wf->createDb($projectname, 'mysql', '', $projectname);
} else {
$user = $wf->grantDbPermissions($projectname,$projectname,'mysql');
console_log('db exists, Grant full database permissions to a user' );
}
//APP
if (!$exists['app']) {
console_log('app doesn`t $exists, creating one....');
$app = $wf->createApp($projectname,'static_php56');
} else {
console_log('app exists, nothing to do' );
}
//DOMAIN
if (!$exists['domain']) {
console_log('domain doesn`t $exists, creating qunabu subdomain....');
$domain = $wf->createDomain('qunabu.com', [$projectname]);
} else {
console_log('domain exists, nothing to do' );
}
//WEBSITE
if (!$exists['website']) {
console_log('website doesn`t $exists, creating one....');
$website = $wf->createWebsite($projectname, $ip, FALSE, [$projectname . '.qunabu.com'], [
$projectname,
'/'
]);
} else {
/*
console_log('website $exists, updating one....');
$website = $wf->updateWebsite($projectname, $ip, FALSE, [$projectname . '.qunabu.com'], [
[$projectname, '/'],
[$projectname, '/']
])
*/;
console_log('website $exists, nothing to do ');
}
$isGit = eoltrim($ssh->exec("[ -d /home/qunabu/webapps/$projectname/.git ] && echo GIT || echo SHIT"));
$isIndex = eoltrim($ssh->exec("[ -e /home/qunabu/webapps/$projectname/index.html ] && echo GIT || echo SHIT"));
if ($isIndex == 'GIT') {
echo $ssh->exec("rm /home/qunabu/webapps/$projectname/index.html");
}
if ($isGit == 'SHIT') {
echo $ssh->exec("git clone -b develop git@git.qunabu.com:qunabuinteractive/$projectname.git /home/qunabu/webapps/$projectname");
}
echo $ssh->exec("cd /home/qunabu/webapps/$projectname && /usr/local/bin/php56 /home/qunabu/composer.phar update");
//setting build.sh
$build_sh = file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'help-files'.DIRECTORY_SEPARATOR.'build.sh');
$build_sh = str_replace('PROJECTNAME', $projectname, $build_sh);
file_put_contents(__DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'build.sh',$build_sh);
//setting .gitlab-ci.yml
$gitlab_ci = file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'help-files'.DIRECTORY_SEPARATOR.'.gitlab-ci.yml');
$gitlab_ci = str_replace('PROJECTNAME', $projectname, $gitlab_ci);
file_put_contents(__DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'.gitlab-ci.yml',$gitlab_ci);
echo "PROJECT DETAILS:\n";
echo "projectname: $projectname \n";
echo "application static_php56 name : $projectname \n";
echo "domain : $projectname.qunabu.com \n";
echo "mysql host : localhost \n";
echo "mysql username : $projectname \n";
echo "mysql database : $projectname \n";
echo "mysql passwrod : $db_pass \n";
echo "ip: $ip \n";
echo "path: /home/qunabu/webapps/$projectname \n";
echo "visit http://$projectname.qunabu.com/install.php to install Silverstripe \n";
echo "once installation is ready visit to set up _env file \n";
echo "visit http://$projectname.qunabu.com/dev/tasks/SetEnvironmentTask \n";
} catch (WebFactionException $e)
{
// Something went wrong, find out what with $e->getMessage() but be warned, WebFaction exception messages are often
// vague and unhelpful!
echo "rut roh, this went wrong: " . $e->getMessage() . "\n\n";
}