Hướng dẫn Drush-Fu với aliases sẽ work across all environments năm 2015

Hướng dẫn Drush-Fu với aliases sẽ work across all environments năm 2015

Drush aliases

Have you noticed how your remote drush aliases (e.g., @my-dev-server) don't work when you're logged into the remote server? It's because aliases with the "remote-host" key specified can't work locally. Quite annoying!

In order to execute your (scripted) drush actions, you've first gotta change into the appropriate sites directory. What a hassle! Let's make life easier. Below (and in this gist) you'll find a solution that works in most hosting environments. It detects the local server name/ip, and strips the "remote-host" entry from entries that match. Skip to the bottom of the code snippet to see the real magic.

/**
 * @file
 * Example drush alias file for multihost use.
 */

// Alias settings common to all.
$aliases['base'] = array(
  'root'         => '/var/www/docroot',
  'remote-user'  => 'georgepburdell',
  'path-aliases' => array(
    '%files' => 'sites/default/files',
    '%dump'  => '/tmp/dump.sql',
  ),
);

// We explicitly include the "remote-host" key for each
// alias below. This means that the aliases won't work
// when logged in to the same box. E.g. @dev fails
// while logged into the dev server. So, at the end, we
// determine the current server and remove
// "remote-host" from all local aliases.
//
// Dev site.
$aliases['dev'] = array(
  'parent'      => '@example.base',
  'remote-host' => 'dev.example.com',
  'uri'         => 'dev.example.com',
);

// Alias entry for a "home directory" site hosted on
// the dev server. Note the parent is @dev and not
// @base.
$aliases['georgepburdell'] = array(
  'parent' => '@example.dev',
  'root'   => '/home/georgepburdell/public_html',
  'uri'    => 'georgepburdell.dev.example.com',
);

// Staging site.
$aliases['stage'] = array(
  'parent'      => '@example.base',
  'remote-host' => 'stage.example.com',
  'uri'         => 'stage.example.com',
);

// Production site.
$aliases['prod'] = array(
  'parent'      => '@example.base',
  'remote-host' => 'www.example.com',
  'uri'         => 'www.example.com',
);

// TL;DR: MAGIC STARTS HERE.
//
// Remove "remote-host" from entries that correspond
// with the current server. This allows us to use the
// same alias file in all environments.
$ip = gethostbyname(php_uname('n'));
foreach ($aliases as &$alias) {
  if (empty($alias['remote-host'])) {
    continue;
  }
  if (gethostbyname($alias['remote-host']) === $ip) {
    unset($alias['remote-host']);
  }
}

Ví dụ gọi drush

In this example, calling "drush sa @example.dev" from the dev server shows the alias record without a "remote-host" key. Voila!

$aliases["example.dev"] = array (
  '#name' => 'example.dev',
  'remote-user' => 'georgepburdell',
  'path-aliases' =>
  array (
    '%files' => 'sites/default/files',
    '%dump' => '/tmp/dump.sql',
  ),
  'root' => '/var/www/html',
  'uri' => 'http://dev.example.com',
);

 

Bạn thấy bài viết này như thế nào?: 
No votes yet
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Tìm kiếm bất động sản

 

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Facebook

Những hoạt động nào khiến chúng ta nghiện Facebook?

Theo các thống kê, Facebook hiện đang có 901 triệu tài khoản (tính đến tháng 3 năm 2012), với hơn 500 triệu tài khoản hoạt động thường xuyên. Đó là một con số ấn tượng đối với một mạng xã hội thành lập vào năm 2004.

Tối ưu hóa tìm kiếm theo địa lý, tìm kiếm video

Tối ưu hóa tìm kiếm theo địa lý, tìm kiếm video

Ảnh hưởng của tìm kiếm toàn cục và tùy biến tới kết quả tìm kiếm và tối ưu hóa tìm kiếm theo địa lý, tìm kiếm video và tìm kiếm hình ảnh.

15 sản phẩm “hot” đáng chờ tại CES 2012

15 sản phẩm “hot” đáng chờ tại CES 2012

Nhiều sản phẩm "đáng mặt anh tài" sẽ xuất hiện tại Triển lãm Điện tử tiêu dùng 2012, Las Vegas, Mỹ dựa theo những tin đồn, thông tin rò rỉ trước trước sự kiện.

Công ty diệt chuột T&C

 

Diet con trung