This repository has been archived by the owner on Mar 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
netstorage.module
143 lines (126 loc) · 4.1 KB
/
netstorage.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
<?php
/**
* @file
* Utilities for working with Akamai NetStorage.
*/
/**
* Implements hook_cron_queue_info().
*/
function netstorage_cron_queue_info() {
$queues['netstorage_upload'] = array(
'worker callback' => 'netstorage_upload',
'time' => 60,
'skip on cron' => TRUE,
);
return $queues;
}
/**
* Implements hook_tweetfetch().
*
* Queues upload tasks.
*/
function netstorage_tweetfetch($filepath) {
$data = new stdClass();
$data->filepath = $filepath;
$data->subdir = 'tweets';
$queue = DrupalQueue::get('netstorage_upload');
$queue->createQueue();
$queue->createItem($data);
}
/**
* Worker callback for netstorage_upload queue in hook_cron_queue_info.
*
* @param obj $upload
* - filepath, absolute path to file
* - subdir, subdirectory on netstorage
*/
function netstorage_upload($upload) {
if (!drupal_is_cli()) {
drupal_set_message('Sorry. This netstorage_upload can only be run via `drush queue-run netstorage_upload`.', 'error');
return;
}
else {
$context = drush_get_context($context);
$is_verbose = $context['DRUSH_VERBOSE'];
}
// Get upload command.
$command = netstorage_get_scp_command($upload->filepath, $upload->subdir);
$command .= ' 2>&1';
// Execute.
$message = "Running: {$command}";
watchdog('netstorage_upload', $message);
if (!$is_verbose) drupal_set_message($message);
$output = shell_exec($command);
// Report output.
$message = "Output: {$output}";
watchdog('netstorage_upload', $message);
if (!$is_verbose) drupal_set_message($message);
// @todo Once this runs, Drupal has already removed this item from the queue.
// Re-queue item here if upload fails.
}
/**
* Generate scp command to push files up to NetStorage.
*/
function netstorage_get_scp_command($filepath, $subdir = '') {
// Make sure variables aren't missing.
if (!$netstorage_credentials = netstorage_get_credentials()) {
throw new Exception('Missing netstorage_credentials.');
}
// Get SCP command.
$command = _netstorage_get_scp_command($filepath, $netstorage_credentials, $subdir);
return $command;
}
/**
* Get credentials.
*
* @return array
*/
function netstorage_get_credentials() {
// Make sure variables aren't missing.
if (!$netstorage_credentials = variable_get('netstorage_credentials', FALSE)) {
drupal_set_message('Missing netstorage_credentials. See netstorage/README.', 'error');
return;
}
else {
return $netstorage_credentials;
}
}
/**
* Call this for Drush commands updating NetStorage for individual files.
*
* scp -i /path/to/key/file /path/to/local/file user@host:/path/to/directory
*
* @param string $filepath
* Absolute path to file.
*
* @param array $netstorage_credentials
* An array of NetStorage rsync variables containing the follow key-value
* pairs:
* - "%key_file": The path to your NetStorage key file.
* - "%user": Your NetStorage username.
* - "%customer": Your NetStorage customer name.
* - "%cp_code": Your CP code provided by Akamai.
* - "%netstorage_upload_path": The Netstorage upload path.
*
* @param string $subdir
* (optional) Subdirectory inside top-level directory to scp up to.
*
* @return string
* Commands to execute to manually run NetStorage update for a single file.
*
* @see drush_netstorage_get_sync_command()
*/
function _netstorage_get_scp_command($filename, $netstorage_credentials, $subdir = '') {
$variables = array_merge($netstorage_credentials, array('%filename' => $filename,
'%subdir' => $subdir));
// Generate the scp command.
if ($destination = variable_get('netstorage_test_destination', FALSE)) {
// This is a test. We're not actually scp-ing the file up to NetStorage.
$command = "scp {$filename} {$destination}";
drupal_set_message('netstorage', 'NOTE: This is a test destination. To upload to NetStorage using credentials in settings.php run `drush netstorage-unset-test-destination`');
}
else {
$command = strtr("scp -i %key_file %filename %user@%customer.upload.akamai.com:/%cp_code/%netstorage_upload_path/%subdir", $variables);
}
return $command;
}