-
Notifications
You must be signed in to change notification settings - Fork 49
/
installer.php
164 lines (134 loc) · 3.99 KB
/
installer.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
<?php
/**
* MongoGento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE_AFL.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade MongoGento to newer
* versions in the future.
*/
/**
* MongoGento install script.
*
* @author Aurelien FOUCRET <[email protected]>
* @copyright 2013 Smile (http://www.smile-oss.com/)
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/
// Git Repository
const GITHUB_REPO_OWNER = 'Smile-SA';
const GITHUB_REPO_NAME = 'mongogento';
const GITHUB_API_BASE_URL = 'https://api.github.com';
/**
* API Call to GitHub
*
* @param string $uri URI of the REST API to be called
* @param string $method Http method to use : GET / POST
* @param mixed $postData Data of the POST to use
*
* @return mixed
*/
function callGitHubApi($uri, $method = 'GET', $postData = null)
{
if (!isset($handle)) {
$handle = curl_init();
}
$options = array(
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10,
CURLOPT_URL => GITHUB_API_BASE_URL . $uri,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_USERAGENT => 'MongoGento Installer'
);
if ($postData !== null && $method == 'POST') {
$options[CURLOPT_POSTFIELDS] = '$postData';
}
curl_setopt_array($handle, $options);
$response = curl_exec($handle);
if (!$response) {
trigger_error(curl_error($handle));
}
$response = json_decode($response, true);
return $response;
}
/**
* Retrieve release list and release tarball URL from GitHub
*
* @return array
*/
function getReleases()
{
$result = array('master' => 'https://github.com/' . GITHUB_REPO_OWNER . '/' . GITHUB_REPO_NAME . '/archive/master.tar.gz');
$uri = '/repos/' . GITHUB_REPO_OWNER . '/' . GITHUB_REPO_NAME . '/releases';
$releasesListResponse = callGitHubApi($uri);
foreach ($releasesListResponse as $currentRelease) {
$result[$currentRelease['tag_name']] = $currentRelease['tarball_url'];
}
return $result;
}
/**
* Download the archive file
*
* @param string $downloadUri URL the file has to be downloaded from
*
* @return string
*/
function downloadRelease($downloadUri)
{
$outFile = dirname(__FILE__) . '/var/mongogento.tar.gz';
echo "Start downloading mongogento archive at $downloadUri into $outFile ...\n";
$fp = fopen($outFile, 'w');
if ($fp === false) {
trigger_error("Unable to write file $outFile");
die;
}
$options = array(
CURLOPT_USERAGENT => 'MongoGento Installer',
CURLOPT_TIMEOUT => 50,
CURLOPT_FILE => $fp,
CURLOPT_FOLLOWLOCATION => 1
);
$handle = curl_init($downloadUri);
curl_setopt_array($handle, $options);
$data = curl_exec($handle);
curl_close($handle);
fclose($fp);
echo "File downloaded.\n";
return $outFile;
}
/**
* Unpack the archive
*
* @param string $archiveFile The path of the archive to be extracted
*
* @return void
*/
function extractArchive($archiveFile)
{
echo "Unpacking archive ...\n";
$cmd = "tar -zxf $archiveFile --strip 2 --wildcards */src";
$test = shell_exec($cmd);
echo "Unpacked finished.";
}
$releases = getReleases();
// Use master release by default
$selectedRelease = 'master';
// Check if a release as been selected manually
if (isset($argv[1])) {
$selectedRelease = $argv[1];
}
// Fails if selected release does not exists
if (!isset($releases[$selectedRelease])) {
echo "Error : Release $selectedRelease does not exist\n";
die;
}
$archiveFile = downloadRelease($releases[$selectedRelease]);
extractArchive($archiveFile);
echo "MongoGento sucessfully installed.\n";