/[drupal]/contributions/modules/deploy/deploy.module
ViewVC logotype

Contents of /contributions/modules/deploy/deploy.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download) (as text)
Thu Mar 6 21:13:19 2008 UTC (20 months, 3 weeks ago) by heyrocker
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
Changes since 1.1: +14 -5 lines
File MIME type: text/x-php
adding basic user/menu perms
1 <?php
2
3 // $Id$
4
5 /**
6 * Implementation of hook_help().
7 */
8 function deploy_help($section='') {
9 $output='';
10
11 switch ($section) {
12 case 'admin/help#deploy':
13 $output = t('Allows users to deploy objects between servers.');
14 }
15
16 return $output;
17 }
18
19 /**
20 * Implementation of hook_perm().
21 */
22 function deploy_perm() {
23 return array(
24 'administer deployment',
25 'add items to deployment plan',
26 'deploy items'
27 );
28 }
29
30 /**
31 * Implementation of hook_menu().
32 */
33 function deploy_menu() {
34
35 $items = array();
36
37 $items[] = array(
38 'path' => 'admin/deploy',
39 'title' => t('Deployment'),
40 'callback' => 'deploy_overview',
41 'access' => user_access('administer deployment'),
42 'description' => t('Deployment'),
43 );
44 $items[] = array(
45 'path' => 'admin/deploy/plans',
46 'title' => t('Plans'),
47 'type' => MENU_DEFAULT_LOCAL_TASK,
48 'access' => user_access('administer deployment'),
49 );
50 $items[] = array(
51 'path' => 'admin/deploy/servers',
52 'title' => t('Servers'),
53 'description' => t('Manage deployment servers'),
54 'callback' => 'deploy_server_overview',
55 'access' => user_access('administer deployment'),
56 'type' => MENU_LOCAL_TASK,
57 'weight' => 3
58 );
59 $items[] = array(
60 'path' => 'admin/deploy/add',
61 'title' => t('Add a deployment plan'),
62 'description' => t('Add a deployment plan.'),
63 'callback' => 'drupal_get_form',
64 'callback arguments' => array('deploy_plan_form'),
65 'access' => user_access('administer deployment'),
66 'type' => MENU_CALLBACK,
67 'weight' => 2
68 );
69 $items[] = array(
70 'path' => 'admin/deploy/plan',
71 'title' => t('Edit plan'),
72 'callback' => 'drupal_get_form',
73 'callback arguments' => array('deploy_plan_form'),
74 'access' => user_access('administer deployment'),
75 'type' => MENU_CALLBACK,
76 );
77 $items[] = array(
78 'path' => 'admin/deploy/server/add',
79 'title' => t('Add server'),
80 'description' => t('Add a deployment server.'),
81 'callback' => 'drupal_get_form',
82 'callback arguments' => array('deploy_server_form'),
83 'access' => user_access('administer deployment'),
84 'type' => MENU_CALLBACK,
85 'weight' => 2
86 );
87 $items[] = array(
88 'path' => 'admin/deploy/server',
89 'title' => t('Edit server'),
90 'description' => t('Edit a deployment server.'),
91 'callback' => 'drupal_get_form',
92 'callback arguments' => array('deploy_server_form'),
93 'access' => user_access('administer deployment'),
94 'type' => MENU_CALLBACK,
95 );
96 $items[] = array(
97 'path' => 'admin/deploy/push',
98 'title' => t('Push a plan live'),
99 'callback' => 'deploy_push',
100 'type' => MENU_CALLBACK,
101 'access' => user_access('deploy items'),
102 'weight' => 1,
103 );
104 $items[] = array(
105 'path' => 'admin/deploy/list',
106 'title' => t('View deployment plan items'),
107 'callback' => 'deploy_list',
108 'type' => MENU_CALLBACK,
109 'access' => user_access('administer deployment'),
110 'weight' => 1,
111 );
112 $items[] = array(
113 'path' => 'admin/deploy/delete/item',
114 'title' => t('Delete a deployment plan item'),
115 'callback' => 'deploy_delete_item',
116 'type' => MENU_CALLBACK,
117 'access' => user_access('administer deployment'),
118 'weight' => 1,
119 );
120 $items[] = array(
121 'path' => 'admin/deploy/delete/plan',
122 'title' => t('Delete a deployment plan'),
123 'callback' => 'deploy_delete_plan',
124 'type' => MENU_CALLBACK,
125 'access' => user_access('administer deployment'),
126 'weight' => 1,
127 );
128 $items[] = array(
129 'path' => 'admin/deploy/delete/server',
130 'title' => t('Delete a server'),
131 'callback' => 'deploy_delete_server',
132 'type' => MENU_CALLBACK,
133 'access' => user_access('administer deployment'),
134 'weight' => 1,
135 );
136
137 return $items;
138 }
139
140
141 /**
142 * deployment overview page menu callback
143 */
144 function deploy_overview() {
145 $header = array(t('Name'), array('data' => t('Operations'), 'colspan' => 2));
146
147 $result = db_query("select * from {deploy_plan}");
148 while ($row = db_fetch_array($result)) {
149 $row = array(l($row['name'], 'admin/deploy/list/'. $row['pid']), l('edit', 'admin/deploy/plan/'. $row['pid']), l('delete', 'admin/deploy/delete/plan/'. $row['pid']));
150 $rows[] = $row;
151 }
152
153 if (empty($rows)) {
154 $rows[] = array(array('data' => t('No deployment plans available.'), 'colspan' => '4', 'class' => 'message'));
155 }
156
157 $output = theme('table', $header, $rows);
158
159 $output .= l("Add a New Deployment Plan", "/admin/deploy/add", array());
160
161 return $output;
162 }
163
164 /**
165 * add/edit deployment plan form
166 */
167 function deploy_plan_form($pid = NULL) {
168 $plan = NULL;
169 if (!is_null($pid)) {
170 $plan = deploy_get_plan($pid);
171 $form['pid'] = array(
172 '#type' => 'hidden',
173 '#default_value' => $pid,
174 );
175 }
176
177 $form['plan_name'] = array(
178 '#title' => t('Name'),
179 '#type' => 'textfield',
180 '#size' => 30,
181 '#maxlength' => 50,
182 '#default_value' => $plan['name'],
183 '#description' => t('A unique name for this deployment plan.'),
184 );
185 $form['submit'] = array(
186 '#type' => 'submit',
187 '#value' => t('Submit'),
188 );
189
190 return $form;
191 }
192
193 /**
194 * deployment plan form validator
195 */
196 function deploy_plan_form_validate($form_id, $form_values) {
197 if ($form_values['plan_name'] == '') {
198 form_set_error('plan_name', t('Plan name is required'));
199 }
200 }
201
202 /**
203 * add deployment plan form submit function
204 */
205 function deploy_plan_form_submit($form_id, $form_values) {
206 $name = $form_values['plan_name'];
207
208 // if form_values has 'pid', then this is an update. Otherwise it is an
209 // insert
210 if (array_key_exists('pid', $form_values)) {
211 $pid = $form_values['pid'];
212 db_query("update {deploy_plan} set name = '%s' where pid = %d", $name, $pid);
213 drupal_set_message(t('Plan updated'));
214 }
215 else {
216 $pid = db_next_id('{deploy_plan}_pid');
217 db_query("insert into {deploy_plan} (pid, name) values (%d, '%s')", $pid, $name);
218 drupal_set_message(t('Plan added'));
219 }
220
221 return 'admin/deploy';
222 }
223
224 /**
225 * deployment servers overview page menu callback
226 */
227 function deploy_server_overview() {
228 $header = array(t('Name'), t('URL'), array('data' => t('Operations'), 'colspan' => 2));
229
230 $result = db_query("select * from {deploy_servers}");
231 while ($row = db_fetch_array($result)) {
232 $row = array($row['description'], $row['url'], l('edit', 'admin/deploy/server/'. $row['sid']), l('delete', 'admin/deploy/delete/server/'. $row['sid']));
233 $rows[] = $row;
234 }
235
236 if (empty($rows)) {
237 $rows[] = array(array('data' => t('No deployment servers available.'), 'colspan' => '4', 'class' => 'message'));
238 }
239
240 $output = theme('table', $header, $rows);
241
242 $output .= l("Add a New Server", "/admin/deploy/server/add", array());
243
244 return $output;
245 }
246
247 /**
248 * add deployment server form
249 */
250 function deploy_server_form($sid = NULL) {
251 $server = NULL;
252 if (!is_null($sid)) {
253 $server = deploy_get_server($sid);
254 $form['sid'] = array(
255 '#type' => 'hidden',
256 '#default_value' => $sid,
257 );
258 }
259
260 $form['description'] = array(
261 '#title' => t('Name'),
262 '#type' => 'textfield',
263 '#size' => 50,
264 '#maxlength' => 100,
265 '#default_value' => $server['description'],
266 '#description' => t('Description of this server.'),
267 );
268 $form['url'] = array(
269 '#title' => t('URL'),
270 '#type' => 'textfield',
271 '#size' => 50,
272 '#maxlength' => 100,
273 '#default_value' => $server['url'],
274 '#description' => t('Domain name and path to xmlrpc service.'),
275 );
276 $form['api_key'] = array(
277 '#title' => t('API key'),
278 '#type' => 'textfield',
279 '#size' => 32,
280 '#maxlength' => 32,
281 '#default_value' => $server['api_key'],
282 '#description' => t('API key (if needed)'),
283 );
284 $form['submit'] = array(
285 '#type' => 'submit',
286 '#value' => t('Submit'),
287 );
288
289 return $form;
290 }
291
292 /**
293 * add deployment server form validator
294 */
295 function deploy_server_form_validate($form_id, $form_values) {
296 if ($form_values['description'] == '') {
297 form_set_error('description', t('Server description is required'));
298 }
299
300 if ($form_values['url'] == '') {
301 form_set_error('url', t('URL is required'));
302 }
303 }
304
305 /**
306 * add deployment server form submit function
307 */
308 function deploy_server_form_submit($form_id, $form_values) {
309 $url = $form_values['url'];
310 $description = $form_values['description'];
311 $api_key = $form_values['api_key'];
312
313 if (array_key_exists('sid', $form_values)) {
314 $sid = $form_values['sid'];
315 db_query("update {deploy_servers} set description = '%s', url = '%s', api_key = '%s' where sid = %d", $description, $url, $api_key, $sid);
316 drupal_set_message(t('Deployment server updated'));
317 }
318 else {
319 $sid = db_next_id('{deploy_servers}_sid');
320 db_query("insert into {deploy_servers} (sid, description, url, api_key) values (%d, '%s', '%s', '%s')", $sid, $description, $url, $api_key);
321 drupal_set_message(t('Deployment server added'));
322 }
323
324 return 'admin/deploy/servers';
325 }
326
327 /**
328 * list deployment plan items menu callback
329 */
330 function deploy_list($pid = NULL) {
331 if (is_null($pid)) {
332 drupal_goto('/admin/deploy');
333 }
334
335 $output = "We will want to add item weighting here as well, since order of execution can matter";
336
337 $header = array(t('Module'), t('Description'), t('Operations'));
338 $result = db_query("select module, description, iid from {deploy_plan_items} where pid = %d", $pid);
339 while ($row = db_fetch_array($result)) {
340 $row = array($row['module'], $row['description'], l(t('Delete From Plan'), "/admin/deploy/delete/item/$pid/" . $row['iid'] ));
341 $rows[] = $row;
342 }
343
344 if (empty($rows)) {
345 $rows[] = array(array('data' => t('No items have been added to this deployment.'), 'colspan' => '3', 'class' => 'message'));
346 }
347
348 $output .= theme('table', $header, $rows);
349
350 $output .= l('Push this plan live', 'admin/deploy/push/'. $pid);
351
352 return $output;
353 }
354
355
356 /**
357 * return an assoc array of deployment plans of format pid => name
358 */
359 function deploy_get_plans() {
360 $result = db_query("select * from {deploy_plan} order by name");
361 $plans = array();
362 while ($plan = db_fetch_array($result)) {
363 $plans[$plan['pid']] = $plan['name'];
364 }
365
366 return $plans;
367 }
368
369 /**
370 * delete a deployment plan.
371 * should probably add a confirmation here
372 */
373 function deploy_delete_plan($pid = NULL) {
374 if (is_null($pid)) {
375 drupal_goto('/admin/deploy');
376 }
377
378 db_query("delete from {deploy_plan_items} where pid = %d", $pid);
379 db_query("delete from {deploy_plan} where pid = %d", $pid);
380
381 drupal_set_message('Plan Deleted');
382
383 drupal_goto("/admin/deploy/");
384 }
385
386 /**
387 * delete a deployment plan.
388 * should probably add a confirmation here
389 */
390 function deploy_delete_server($sid = NULL) {
391 if (is_null($sid)) {
392 drupal_goto('/admin/deploy/servers');
393 }
394
395 db_query("delete from {deploy_servers} where sid = %d", $sid);
396
397 drupal_set_message('Server Deleted');
398
399 drupal_goto("/admin/deploy/servers");
400 }
401
402 /**
403 * delete an item from a deployment plan.
404 * should probably add a confirmation here
405 */
406 function deploy_delete_item($pid = NULL, $iid = NULL) {
407 if (is_null($iid) || is_null($pid)) {
408 drupal_goto('/admin/deploy');
409 }
410
411 db_query("delete from {deploy_plan_items} where iid = %d", $iid);
412
413 drupal_set_message('Item Deleted');
414
415 drupal_goto("/admin/deploy/list/$pid");
416 }
417
418 /**
419 * hook_xmlrpc deploy.install callback
420 */
421 function deploy_install_items($module, $form_id, $data) {
422 watchdog('install', $module);
423 if (module_exists($module)) {
424 $data = unserialize($data);
425 drupal_execute($form_id, $data);
426 }
427 else {
428 return "Module not enabled: $module";
429 }
430 }
431
432 /**
433 * add data to a deployment plan
434 */
435 function deploy_add_to_plan($pid, $module, $description, $data) {
436 $iid = db_next_id('{deploy_plan_items}_iid');
437 $result = db_query("insert into deploy_plan_items (iid, pid, module, description, data) values (%d, %d, '%s', '%s', '%s')", $iid, $pid, $module, $description, $data);
438 if (!$result) {
439 drupal_set_message("an error occured");
440 }
441 else {
442 drupal_set_message("$description successfully added to plan");
443 }
444 }
445
446 /**
447 * push a deployment plan live
448 */
449 function deploy_push($pid) {
450 if (is_null($pid)) {
451 drupal_goto('/admin/deploy');
452 }
453
454 $output = 'Choose a deployment server';
455 $output .= drupal_get_form('deploy_pushform', $pid);
456
457 return $output;
458 }
459
460 function deploy_pushform($pid) {
461 $servers = _deploy_get_servers();
462
463 $form['pid'] = array(
464 '#type' => 'hidden',
465 '#value' => $pid,
466 );
467 $form['sid'] = array(
468 '#title' => t('Server'),
469 '#type' => 'select',
470 '#options' => $servers,
471 '#description' => t('The server you want to deploy to'),
472 );
473 $form['submit'] = array(
474 '#type' => 'submit',
475 '#value' => t('Submit'),
476 );
477
478 return $form;
479 }
480
481 /**
482 * submit handler for deployment push form
483 */
484 function deploy_pushform_submit($form_id, $form_values) {
485 $sid = $form_values['sid'];
486 $pid = $form_values['pid'];
487 $result = db_query("select * from deploy_servers where sid = %d", $sid);
488 $row = db_fetch_array($result);
489 $url = $row['url'];
490 $api_key = $row['api_key'];
491
492 $result = db_query("select * from deploy_plan_items where pid = %d", $pid);
493 while ($row = db_fetch_array($result)) {
494 call_user_func($row['module'] .'_deploy', $url, $api_key, $row['data']);
495 }
496
497 return "/admin/deploy/list/$pid";
498 }
499
500 /**
501 * return an assoc array of servers of format sid => description
502 */
503 function _deploy_get_servers() {
504 $result = db_query("select * from {deploy_servers} order by description");
505 $servers = array();
506 while ($server = db_fetch_array($result)) {
507 $servers[$server['sid']] = $server['description'];
508 }
509
510 return $servers;
511 }
512
513 /**
514 * update a single item in a deployment plan with new data
515 */
516 function deploy_update_item($iid, $data) {
517 db_query("update {deploy_plan_items} set data = '%s' where iid = %d", $data, $iid);
518 }
519
520 /**
521 * retrieve an array of a deployment plan
522 */
523 function deploy_get_plan($pid) {
524 $result = db_query("select * from {deploy_plan} where pid = %d", $pid);
525 return db_fetch_array($result);
526 }
527
528 /**
529 * retrieve an array of a deployment plan server
530 */
531 function deploy_get_server($sid) {
532 $result = db_query("select * from {deploy_servers} where sid = %d", $sid);
533 return db_fetch_array($result);
534 }

  ViewVC Help
Powered by ViewVC 1.1.2