wp-nginx-map.php 2.1 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
// IP interno del master.
ale's avatar
ale committed
7 8
$master_url = file_get_contents('/etc/noblogs/master');
$noblogs_master = parse_url($master_url)['host'];
Joe Oblivian's avatar
Joe Oblivian committed
9 10 11 12 13

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

ale's avatar
ale committed
14 15 16
// Port that the noblogs apache2 instance is listening on (as a string).
define('NOBLOGS_PORT','92');

Joe Oblivian's avatar
Joe Oblivian committed
17 18 19 20 21
// 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);
Joe's avatar
Joe committed
22
  return ($result);
Joe Oblivian's avatar
Joe Oblivian committed
23 24 25 26 27 28 29 30 31
}

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);
ale's avatar
ale committed
32
    return $noblogs_master . ':' . NOBLOGS_PORT;
Joe Oblivian's avatar
Joe Oblivian committed
33 34
  }
  $id = substr($backend, 8);
ale's avatar
ale committed
35
  return '172.16.1.' . $id . ':' . NOBLOGS_PORT;
Joe Oblivian's avatar
Joe Oblivian committed
36 37 38
}

// Print the blog -> backend map.
shammash's avatar
shammash committed
39
function generate_backend_map($blogs) {
Joe Oblivian's avatar
Joe Oblivian committed
40
  global $wpdb;
Joe's avatar
Joe committed
41
  global $noblogs_master;
Joe Oblivian's avatar
Joe Oblivian committed
42 43 44
  $wpdb_hash = &$wpdb->hash_map;

  printline('map $http_host $backend_noblogs {');
ale's avatar
ale committed
45
  printline(' default http://' . $noblogs_master . ':' . NOBLOGS_PORT . ';');
Joe Oblivian's avatar
Joe Oblivian committed
46 47
  foreach ($blogs as $blog) {
    $blog_id = $blog->blog_id;
48 49
    if ($blog_id == 1)
      continue;
Joe Oblivian's avatar
Joe Oblivian committed
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('}');
}

shammash's avatar
shammash committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
// Print the blog -> site map.
// This is used to send apache the right site name for logging purposes.
function generate_site_map($blogs) {
  printline('map $http_host $site_noblogs {');
  printline(' default noblogs.org;');
  foreach ($blogs as $blog) {
    $blog_id = $blog->blog_id;
    if ($blog_id == 1)
      continue;
    printline(' ' . $blog->domain . ' ' . $blog->domain . ';');
  }
  printline('}');
}

function generate_maps() {
  $all_blogs = get_blogs();
  generate_backend_map($all_blogs);
  generate_site_map($all_blogs);
}
Joe Oblivian's avatar
Joe Oblivian committed
76

shammash's avatar
shammash committed
77
generate_maps();