-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathterminatur.code.inc
92 lines (87 loc) · 3.99 KB
/
terminatur.code.inc
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
<?php
/**
* @file
* code.inc: Helper functions to grab code
*/
/**
* Gets the codebase for the Pantheon site with git
*/
function _terminatur_code_git($site, $destination) {
// Check if the codebase already exists
if (file_exists($destination . $site['machine-name'] . "/.git/config")) {
// @todo get this out of drush_shell_exec and into PHP
drush_shell_exec("cat " . $destination . $site['machine-name'] . "/.git/config | grep url > " . TERMINATUR_TMP_DIR . "gitconfig" . $site['machine-name']);
$gitconfig_string = trim(file_get_contents(TERMINATUR_TMP_DIR . "gitconfig" . $site['machine-name']));
drush_shell_exec("rm /tmp/gitconfig" . $site['machine-name']);
// Codebase does exist, just refresh that code
if ($gitconfig_string == "url = " . $site['terminatur']['code']['git-connect']) {
// We need to use this environmental variable bullshit to ensure we actually get relevant output
if (!drush_shell_exec("git --git-dir=" . $destination . $site['machine-name'] . "/.git pull")) {
return drush_set_error('TERMINATUR_ERROR', 'Could not git pull your code. Check that your SSH key is loaded and the codeserver is reachable.');
}
return TRUE;
}
else {
// Do some checking so we don't delete the whole webroot!
if (isset($site['machine-name'])) {
if (!drush_shell_exec("rm -rf " . $destination . $site['machine-name'])) {
return drush_set_error('TERMINATUR_ERROR', 'Could not remove the existing codebase. Please remove manually and then try again.');
}
}
}
}
// Git clone for the first time
mkdir($destination . $site['machine-name'], 0777, TRUE);
// We need to use this environmental variable bullshit to ensure we actually get relevant output
if (!drush_shell_exec("cd " . $destination . " && git clone " . $site['terminatur']['code']['git-connect'] . " " . $site['machine-name'])) {
return drush_set_error('TERMINATUR_ERROR', 'Could not clone code from the remote repository. Check that your SSH key is loaded and the codeserver is reachable.');
}
return TRUE;
}
/**
* Gets the codebase for the Pantheon site via S3 download
*/
function _terminatur_code_download($site, $destination) {
// Get the download link data
if (!$site['terminatur']['code']['download-data'] = _terminatur_get_download_link($site, 'code')) {
return FALSE;
}
// Parse download data and begin the download
$result = $site['terminatur']['code']['download-data'];
$data = json_decode($result['json']);
$filename = strstr(basename($data->url), '?', '_');
$dir = strstr($filename, '.', true);
$cache_duration = 86400*365;
$path = _drush_download_file($data->url, TERMINATUR_TMP_DIR . $filename, $cache_duration);
if ($path || drush_get_context('DRUSH_SIMULATE')) {
drush_tarball_extract($path);
drush_shell_exec("rm -rf " . $destination . $site['machine-name']);
rename(TERMINATUR_TMP_DIR . $dir, $destination . $site['machine-name']);
return TRUE;
}
else {
return drush_set_error('TERMINATUR_ERROR', 'Download failed.');
}
}
/**
* Removes the code
*/
function _terminatur_code_remove($site, $destination) {
// Remove Code but first make sure we have a machine name before we
// inadvertently destroy the entire webroot
if (isset($site['machine-name'])) {
$web_root = $destination . $site['machine-name'];
// Make write-protected directory and files writable so we can remove them.
if (is_dir($web_root . TERMINATUR_DEFAULT_DEFAULT_DIR)) {
chmod($web_root . TERMINATUR_DEFAULT_DEFAULT_DIR, 0755);
chmod($web_root . TERMINATUR_DEFAULT_DEFAULT_DIR . 'default.settings.php', 0644);
chmod($web_root . TERMINATUR_DEFAULT_DEFAULT_DIR . 'settings.php', 0644);
}
// Remove the webroot and everything in it.
// Need to run shell command because PHP isnt great at complete destruction
if (!drush_shell_exec("rm -rf " . $web_root)) {
return drush_set_error('TERMINATUR_ERROR', 'Could not remove webroot. Please remove manually.');
}
}
return TRUE;
}