wp-nginx-map.php 1.35 KB
Newer Older
Joe Oblivian's avatar
Joe Oblivian committed
1
2
3
4
5
<?php

// wp-nginx-map.php
//
// Stampa una mappa delle assegnazioni blog -> backend, per NGINX.
Joe's avatar
Joe committed
6
7
// IP interno del master.
define('NOBLOGS_MASTER', '172.16.1.10');
Joe Oblivian's avatar
Joe Oblivian committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

// Load wordpress api.
define('WP_CACHE',false);
require_once('/opt/noblogs/www/wp-load.php');

// Return all blogs.
function get_blogs() {
  global $wpdb;
  $sql = "SELECT blog_id, domain FROM $wpdb->blogs WHERE deleted = 0 AND archived = '0' ORDER BY domain ASC";
  $result = $wpdb->get_results($sql);
  return ($result);  
}

function printline($s) {
  echo $s . "\n";
}

function backend_to_http_endpoint($backend) {
  if (substr($backend, 0, 8) != 'backend_') {
    error_log('diamine, di questo backend non so che farmene: ' . $backend);
Joe's avatar
Joe committed
28
    return NOBLOGS_MASTER . ':82';
Joe Oblivian's avatar
Joe Oblivian committed
29
30
31
32
33
34
35
36
37
38
39
40
41
  }
  $id = substr($backend, 8);
  return '172.16.1.' . $id . ':82';
}

// Print the blog -> backend map.
function generate_map() {
  global $wpdb;
  $wpdb_hash = &$wpdb->hash_map;

  $blogs = get_blogs();

  printline('map $http_host $backend_noblogs {');
Joe's avatar
Joe committed
42
  printline(' default http://' . NOBLOGS_MASTER . ':82;');
Joe Oblivian's avatar
Joe Oblivian committed
43
44
  foreach ($blogs as $blog) {
    $blog_id = $blog->blog_id;
45
46
    if ($blog_id == 1)
      continue;
Joe Oblivian's avatar
Joe Oblivian committed
47
48
49
50
51
52
53
54
55
56
    $backend_id = $wpdb_hash->lookup($blog_id);
    $backend_http = backend_to_http_endpoint($backend_id);
    printline(' ' . $blog->domain . ' http://' . $backend_http . ';');
  }
  printline('}');
}


generate_map();