Salt States by Example | Part 5 | Install WP-CLI using Salt

By: rahul On: Fri 10 November 2017
In: Devops
Tags: #devops #configuration-management #linux #saltstack

Greetings Wanderer!

This is a continuation on our attempt to write a complete Salt State. Before proceeding, if you haven't already, please check the links/articles listed below:

> Saltstack By Example | Part 1 | Installation
> Saltstack By Example | Part 2 | Configuration
> Saltstack By Example | Part 3 | Basic Commands
> Saltstack By Example | Part 4 | Salt State Examples

> Salt States by Example | Part 1 | Install a Salt Minion on CentOS or Debian
> Salt States by Example | Part 2 | Install Nginx using Saltstack
> Salt States by Example | Part 3 | Install MySQL, PHP and PHP-FPM using Saltstack
> Salt States by Example | Part 4 | Create and Configure an Nginx Virtual Host
>> Salt States by Example | Part 5 | Install WP-CLI using Salt (<== We are here)

In the previous article, we saw how to create and configure an Nginx Virtual host on a Salt Minion. In this post, we'll see how to go about installing wp-cli using saltstack on the same Salt Minion.

Prerequisites
  • A Salt Master
  • A Salt Minion that runs a Debian minimal install or a CentOS minimal install - the installation is outlined in Part 1
  • This is what our directory structure looks like:
.
|-- common    <=== COMPLETED
|-- nginx     <=== COMPLETED
|   `-- repofiles
|-- mysql     <=== COMPLETED
|-- php       <=== COMPLETED
|   `-- files
|-- wordpress 
|   `-- files
|-- wpcli
|   `-- files
`-- wp_user   <=== COMPLETED
    `-- files
        |-- templates
        `-- wptest
            |-- logs
            `-- public_html

1. Install WP-CLI

1a. As usual, we'll begin by declaring a few variables, using the map.jinja file (wpcli/map.jinja):

{%  set wpcli = salt['grains.filter_by']({
    'Debian': {
        'url': 'https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar',
        'service': 'apache2',
        'docroot': '/var/www/html/',
    },
    'RedHat': {
        'url': 'https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar',
        'service': 'httpd',
        'docroot': '/var/www/html/',
    }
}) %}

1b. Here, I've used a hacky method to install wp-cli using a shell script that is copied and run on the Minion. I'm sure there are much better ways to accomplish this. If you do come across a better method, please drop me a line and I'll edit and update the salt state.

This is the shell script we'll use (located at wpcli/files/install-wpcli.sh):

1
2
3
4
5
6
7
#!/bin/bash

cd /tmp
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
mv wp-cli.phar /usr/local/bin/wp
cd

1c. And we'll run the script above using the install.sls file (wpcli/install.sls):

{%- from "wordpresser/wpcli/map.jinja" import wpcli %}

install_wpcli:
  cmd.script:
    - source: salt://wordpresser/wpcli/files/install-wpcli.sh
    - cwd: {{ wpcli.docroot }}
- user: root

NOTE: this will install wp-cli globally on the server.

Here's what the completed wpcli directory structure should look like:

wpcli/
|-- files
|   `-- install-wpcli.sh
|-- install.sls
`-- map.jinja

1d. Edit the init.sls file to include the wpcli files:

include:
  - wordpresser.common.packages
  - wordpresser.nginx.repo
  - wordpresser.nginx.packages
  - wordpresser.nginx.firewalld
  - wordpresser.mysql.packages
  - wordpresser.mysql.firewalld
  - wordpresser.mysql.mysql_secure
  - wordpresser.php.packages
  - wordpresser.wp_user.create
  - wordpresser.wp_user.configure
  - wordpresser.wpcli.install

1e. Run salt command - salt '*' state.apply
If it runs successfully, wp-cli will be istall on the Minions.

In the next post, we'll see how Wordpress is install and configured within the Salt Minions using salt.

A complete version of the code is available at this GitHub repository: Wordpresser


If you found the article helpful, please share or cite the article, and spread the word:


For any feedback or corrections, please write in to: rahul [at] muchbits [dot] com