-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhosting_git.module
352 lines (309 loc) · 10.4 KB
/
hosting_git.module
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
/**
* @file
* Provides Git integration
*/
define('HOSTING_GIT_WEBHOOK_STATUS_OK', 1);
define('HOSTING_GIT_WEBHOOK_STATUS_ACCESS_DENIED', 2);
define('HOSTING_GIT_WEBHOOK_STATUS_INVALID_CODE', 3);
define('HOSTING_GIT_WEBHOOK_STATUS_OTHER_REF', 4);
// These are github's Webhook callback IPs.
// This list grows occaisonally, update it as needed.
define('HOSTING_GIT_WEBHOOK_DEFAULT_ALLOWED_IPS', "
204.232.175.64/27
192.30.252.0/22
104.192.143.192/28
104.192.143.208/28
");
/**
* Implements hook_menu().
*/
function hosting_git_menu() {
$items['admin/hosting/git'] = array(
'title' => 'Git',
'description' => 'Configure Git integration',
'file' => 'hosting_git.admin.inc',
'page callback' => 'drupal_get_form',
'page arguments' => array('hosting_git_settings_form'),
'access arguments' => array('administer hosting settings'),
'tab_parent' => 'admin/hosting',
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Implements hook_form_alter().
*/
function hosting_git_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'platform_node_form' || $form_id == 'site_node_form') {
// Get node object.
$node = $form['#node'];
// Only add git info if allowed by settings.
if (!variable_get("hosting_git_allow_deploy_" . $node->type, TRUE)) {
return;
}
// Skip Git forms for exsisting sites without a repo URL.
// Adding one later is currently not supported.
if (isset($node->nid) && $node->verified && empty($node->git['repo_url'])) {
return;
}
if ($form_id == 'platform_node_form') {
$node_type = 'platform';
$deploy_description = t('You may deploy this platform from a Git repository. This functionality cannot be combined with a Makefile.');
// Default collapse 'from makefile' fieldset.
$form['frommakefile']['#collapsed'] = TRUE;
}
if ($form_id == 'site_node_form') {
$node_type = 'site';
$deploy_description = t('You may deploy this site from a Git repository.');
}
$form['git'] = array(
'#type' => 'fieldset',
'#tree' => TRUE,
'#title' => t('Deploy from Git'),
'#collapsible' => TRUE,
'#description' => $deploy_description,
'#weight' => -1,
);
if (!isset($node->nid) || (!$node->verified && !isset($node->git['repo_url']))) {
$form['git']['repo_url'] = array(
'#type' => 'textfield',
'#title' => 'Repository URL',
'#description' => t('The full URL that you would pass to <em>git clone</em>. Example: [email protected]:username/project.git or https://github.com/username/project.git. Note that this repository must be accessible by the Aegir user.'),
'#default_value' => isset($node->git['repo_url']) ? $node->git['repo_url'] : '',
);
$form['git']['repo_docroot'] = array(
'#type' => 'textfield',
'#title' => 'Repository docroot',
'#description' => t('If Drupal is not in the root of your repository, enter the relative path within the repository that points to the correct subfolder.'),
'#default_value' => isset($node->git['repo_docroot']) ? $node->git['repo_docroot'] : '',
'#access' => $node_type == 'platform',
);
$form['git']['git_ref'] = array(
'#type' => 'textfield',
'#title' => 'Branch or tag name',
'#description' => t('If a branch or tag name is not supplied, the @node_type will be deployed from <em>master</em>. This must match the Git branch or tag name exactly.', array('@node_type' => $node_type)),
'#default_value' => isset($node->git['git_ref']) ? $node->git['git_ref'] : 'master',
);
}
else {
// Display it.
$form['git']['repo_url_display'] = array(
'#type' => 'item',
'#title' => t('Repo URL'),
'#markup' => $node->git['repo_url'],
);
$form['git']['repo_path_display'] = array(
'#type' => 'item',
'#title' => t('Repo Path'),
'#markup' => $node->git['repo_path'],
);
$form['git']['git_ref_display'] = array(
'#type' => 'item',
'#title' => 'Branch or tag name',
'#markup' => isset($node->git['git_ref']) ? $node->git['git_ref'] : 'master',
'#description' => 'Use a checkout task to change this value.',
);
// Don't lose values
$form['git']['repo_url'] = array(
'#type' => 'value',
'#default_value' => isset($node->git['repo_url']) ? $node->git['repo_url'] : '',
);
// Don't lose values
$form['git']['repo_path'] = array(
'#type' => 'value',
'#default_value' => isset($node->git['repo_path']) ? $node->git['repo_path'] : '',
);
}
// Default collapse one fieldset based on the current node.
if (empty($node->frommakefile['makefile'])) {
$form['frommakefile']['#collapsed'] = TRUE;
}
elseif (empty($node->git['repo_url'])) {
$form['git']['#collapsed'] = TRUE;
}
}
}
/**
* Implements hook_node_submit().
*/
function hosting_git_node_submit($node, $form, &$form_state) {
if (!empty($form_state['values']['git']['repo_docroot'])) {
$form_state['values']['git']['repo_path'] = $node->git['repo_path'] = $node->publish_path;
$form_state['values']['publish_path'] = $node->publish_path = $node->git['repo_path'] . '/' . $node->git['repo_docroot'];
}
}
/**
* Implements hook_node_insert().
*/
function hosting_git_node_insert($node) {
return hosting_git_node_update($node);
}
/**
* Implements hook_node_update().
*/
function hosting_git_node_update($node) {
if ($node->type == 'platform' || $node->type == 'site') {
// Only add git info if allowed by settings.
if (!variable_get("hosting_git_allow_deploy_" . $node->type, TRUE)) {
return;
}
if (empty($node->git['repo_url']) || empty($node->git['git_ref']) || empty($node->git['git_docroot'])) {
_hosting_git_node_load_defaults($node);
}
db_merge('hosting_git')
->key(array('nid' => $node->nid))
->fields(array(
'repo_url' => $node->git['repo_url'],
'repo_path' => $node->git['repo_path'],
'repo_docroot' => $node->git['repo_docroot'],
'git_ref' => $node->git['git_ref'],
))
->execute();
}
}
/**
* Implements hook_node_load().
*/
function hosting_git_node_load($nodes, $types) {
foreach ($nodes as $node) {
if ($node->type == 'platform' || $node->type == 'site') {
// Only add git info if allowed by settings.
if (!variable_get("hosting_git_allow_deploy_" . $node->type, TRUE)) {
return;
}
$r = db_query('SELECT * FROM {hosting_git} WHERE nid = :nid', array(':nid' => $node->nid));
if ($result = $r->fetchObject()) {
if (!isset($node->git)) {
$node->git = array();
}
$node->git['repo_url'] = $result->repo_url;
$node->git['git_ref'] = $result->git_ref;
$node->git['repo_path'] = $result->repo_path;
$node->git['repo_docroot'] = $result->repo_docroot;
// If platform repo path is empty, then load the publish_path in it's place.
if ( $node->type == 'platform' && empty($node->git['repo_path'])) {
$node->git['repo_path'] = $node->publish_path;
}
}
else {
_hosting_git_node_load_defaults($node);
}
}
}
}
/**
* Private function to initialize default values for a node object.
*/
function _hosting_git_node_load_defaults(&$node) {
if (!isset($node->git)) {
$node->git = array();
}
if (!isset($node->git['repo_url'])) {
$node->git['repo_url'] = '';
}
if (!isset($node->git['repo_path'])) {
$node->git['repo_path'] = '';
}
if (!isset($node->git['repo_docroot'])) {
$node->git['repo_docroot'] = '';
}
if (!isset($node->git['git_ref'])) {
$node->git['git_ref'] = 'master';
}
}
/**
* Implements hook_node_delete().
*/
function hosting_git_node_delete($node) {
if ($node->type == 'platform' || $node->type == 'site') {
db_delete('hosting_git')
->condition('nid', $node->nid)
->execute();
}
}
/**
* Implements hook_node_type_view().
*/
function hosting_git_node_view($node) {
if (!empty($node->git['repo_url'])) {
$node->content['info']['repo_url'] = array(
'#type' => 'item',
'#title' => t('Git URL'),
'#weight' => 100,
'#markup' => check_plain($node->git['repo_url']),
);
if (!empty($node->git['repo_path'])) {
$node->content['info']['repo_path'] = array(
'#type' => 'item',
'#title' => t('Git repository path'),
'#weight' => 100,
'#markup' => check_plain($node->git['repo_path']),
);
}
if (!empty($node->git['repo_docroot'])) {
$node->content['info']['repo_docroot'] = array(
'#type' => 'item',
'#title' => t('Git repository docroot'),
'#weight' => 100,
'#markup' => check_plain($node->git['repo_docroot']),
);
}
$ref = $node->git['git_ref'] ? $node->git['git_ref'] : t('Pending Verification');
$node->content['info']['git_ref'] = array(
'#type' => 'item',
'#title' => t('Git reference'),
'#weight' => 100,
'#markup' => $ref,
);
}
}
/* Private functions */
/**
* Check to see if the target dir is empty.
*/
function _hosting_git_isdirempty($dir) {
if ($handle = @opendir($dir)) {
// Open directory.
while (($file = readdir($handle)) !== FALSE) {
if ($file != "." && $file != "..") {
closedir($handle); // Close directory.
return FALSE;
}
}
closedir($handle); // Close directory.
}
return TRUE;
}
/**
* Get the path to the site specific directory.
*
* e.g. /var/aegir/platforms/drupal-7.x/sites/example.com
*/
function _hosting_git_getsitepath($node) {
// Select the platform publish_path for $node->platform.
$platform_publishpath = db_query("SELECT publish_path FROM {hosting_platform} WHERE nid = :nid", array(':nid' => $node->platform))->fetchField();
if ($platform_publishpath) {
return $platform_publishpath . '/sites/' . $node->title;
}
else {
return FALSE;
}
}
/**
* Test to see if a platform or site node has an enabled status.
*
* @param object $node
* Node object
*
* @return bool
*/
function _hosting_git_site_or_platform_enabled($node) {
return (
isset($node->nid)
&& (
($node->type == 'site' && $node->site_status == HOSTING_SITE_ENABLED)
|| ($node->type == 'platform' && ($node->platform_status == HOSTING_PLATFORM_ENABLED || $node->platform_status == HOSTING_PLATFORM_LOCKED))
)
);
}