Skip to content

Commit

Permalink
Merge pull request #12 from oxyc/source
Browse files Browse the repository at this point in the history
Issue #11: Install XHProf from source
  • Loading branch information
geerlingguy committed Mar 1, 2016
2 parents 41da17c + 9aef8af commit eab43ed
Show file tree
Hide file tree
Showing 18 changed files with 246 additions and 57 deletions.
62 changes: 43 additions & 19 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,39 +1,63 @@
---
language: python
python: "2.7"
sudo: required

env:
- SITE=test.yml
- distribution: centos
version: 6
init: /sbin/init
run_opts: ""
SITE: source
- distribution: centos
version: 7
init: /usr/lib/systemd/systemd
run_opts: "--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
SITE: source
- distribution: ubuntu
version: 14.04
init: /sbin/init
run_opts: ""
SITE: source
- distribution: ubuntu
version: 12.04
init: /sbin/init
run_opts: ""
SITE: source

services:
- docker

before_install:
- sudo apt-get update -qq
# Pull container
- 'sudo docker pull ${distribution}:${version}'
# Customize container
- 'sudo docker build --rm=true --file=tests/Dockerfile.${distribution}-${version} --tag=${distribution}-${version}:ansible tests'

install:
# Install Ansible.
- pip install ansible

# Add ansible.cfg to pick up roles path.
- "{ echo '[defaults]'; echo 'roles_path = ../'; } >> ansible.cfg"
script:
- container_id=$(mktemp)
# Run container in detached state
- 'sudo docker run --detach --volume="${PWD}":/etc/ansible/roles/role_under_test:ro ${run_opts} ${distribution}-${version}:ansible "${init}" > "${container_id}"'

# Install dependencies.
- ansible-galaxy install geerlingguy.php geerlingguy.php-pecl
- 'sudo docker exec "$(cat ${container_id})" ansible-galaxy install geerlingguy.php geerlingguy.repo-remi'

script:
# Check the role/playbook's syntax.
- "ansible-playbook -i tests/inventory tests/$SITE --syntax-check"
# Ansible syntax check.
- 'sudo docker exec --tty "$(cat ${container_id})" env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/test-${SITE}.yml --syntax-check'

# Run the role/playbook with ansible-playbook.
- "ansible-playbook -i tests/inventory tests/$SITE --connection=local --sudo"
# Test role.
- 'sudo docker exec --tty "$(cat ${container_id})" env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/test-${SITE}.yml'

# Run the role/playbook again, checking to make sure it's idempotent.
# Test role idempotence.
- >
ansible-playbook -i tests/inventory tests/$SITE --connection=local --sudo
sudo docker exec "$(cat ${container_id})" ansible-playbook /etc/ansible/roles/role_under_test/tests/test-${SITE}.yml
| grep -q 'changed=0.*failed=0'
&& (echo 'Idempotence test: pass' && exit 0)
|| (echo 'Idempotence test: fail' && exit 1)
# Run script to test if XHProf is available and working.
- php tests/xhprof-test.php
- 'sudo docker exec --tty "$(cat ${container_id})" env TERM=xterm php /etc/ansible/roles/role_under_test/tests/xhprof-test.php'

# Clean up
- 'sudo docker stop "$(cat ${container_id})"'

notifications:
webhooks: https://galaxy.ansible.com/api/v1/notifications/
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,25 @@ None.

Available variables are listed below, along with default values (see `defaults/main.yml`):

workspace: /root

Where XHProf setup files will be downloaded and built.

xhprof_output_dir: /var/tmp/xhprof

Directory where XHProf runs are stored.

php_xhprof_lib_dir: /usr/share/php/xhprof_lib

Directory where the XHProf PHP library is stored.

php_xhprof_html_dir: /usr/share/php/xhprof_html

Directory where the XHProf UI is stored.

## Dependencies

- geerlingguy.php
- geerlingguy.php-pecl

## Example Playbook

Expand Down
5 changes: 5 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
---
workspace: /root

xhprof_output_dir: /var/tmp/xhprof

php_xhprof_lib_dir: /usr/share/php/xhprof_lib
php_xhprof_html_dir: /usr/share/php/xhprof_html
1 change: 0 additions & 1 deletion meta/main.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
dependencies:
- { role: geerlingguy.php }
- { role: geerlingguy.php-pecl }

galaxy_info:
author: geerlingguy
Expand Down
18 changes: 18 additions & 0 deletions tasks/configure.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
- name: Ensure PHP configuration directories exist.
file:
path: "{{ item }}"
state: directory
mode: 0755
follow: true
with_items: "{{ php_xhprof_config_dirs }}"

- name: Copy XHProf INI into various other conf folders.
template:
src: xhprof.ini.j2
dest: "{{ item }}/{{ php_xhprof_config_filename }}"
owner: root
group: root
mode: 0644
with_items: "{{ php_xhprof_config_dirs }}"
notify: restart webserver
62 changes: 42 additions & 20 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,56 @@
- include: setup-Debian.yml
when: ansible_os_family == 'Debian'

# Install and configure XHProf.
- name: Install XHProf using PECL.
command: "pecl install {{ item }}"
register: pecl_result
changed_when: "pecl_result.rc == 0"
failed_when: "not (('already installed' in pecl_result.stdout) or ('install ok:' in pecl_result.stdout))"
- name: Download XHProf.
get_url:
url: "https://github.com/phacility/xhprof/archive/master.tar.gz"
dest: "{{ workspace }}/xhprof-master.tar.gz"

# TODO: In 2.0, we can set the 'src' to the URL from the get_url task above and
# cut out one extra task :)
- name: Untar XHProf.
unarchive:
src: "{{ workspace }}/xhprof-master.tar.gz"
dest: "{{ workspace }}"
copy: no

- name: Build XHProf.
shell: >
{{ item }}
chdir={{ workspace }}/xhprof-master/extension
creates={{ workspace }}/xhprof-master/extension/modules/xhprof.so
with_items:
# Manual channel installation required for certain PEAR versions.
- "channel://pecl.php.net/xhprof-0.9.4"
- xhprof
- phpize
- ./configure
- make
notify: restart webserver

- name: Ensure PHP configuration directories exist.
- name: Ensure XHProf module path exists.
file:
path: "{{ item }}"
path: "{{ php_xhprof_module_path }}"
state: directory
mode: 0755
with_items: php_xhprof_config_dirs

- name: Copy XHProf INI into various other conf folders.
template:
src: xhprof.ini.j2
dest: "{{ item }}/{{ php_xhprof_config_filename }}"
owner: root
group: root
mode: 0644
with_items: php_xhprof_config_dirs
mode: 0755

- name: Move XHProf module into place.
shell: >
cp {{ workspace }}/xhprof-master/extension/modules/xhprof.so {{ php_xhprof_module_path }}/xhprof.so
creates={{ php_xhprof_module_path }}/xhprof.so
notify: restart webserver

- name: Move XHProf PHP library into place.
shell: >
cp -r {{ workspace }}/xhprof-master/xhprof_lib {{ php_xhprof_lib_dir }}
creates={{ php_xhprof_lib_dir }}/utils/xhprof_lib.php
- name: Move XHProf UI into place.
shell: >
cp -r {{ workspace }}/xhprof-master/xhprof_html {{ php_xhprof_html_dir }}
creates={{ php_xhprof_html_dir }}/index.php
- include: configure.yml

- name: Ensure XHProf output directory exists.
file:
path: "{{ xhprof_output_dir }}"
Expand Down
5 changes: 5 additions & 0 deletions tasks/setup-Debian.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
---
- name: Ensure dependencies for installing from source are installed.
apt: "pkg={{ item }} state=installed"
with_items:
- make

- name: Ensure GraphViz is installed.
apt: pkg=graphviz state=installed
5 changes: 5 additions & 0 deletions tasks/setup-RedHat.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
---
- name: Ensure dependencies for installing from source are installed.
yum: "pkg={{ item }} state=installed"
with_items:
- make

- name: Ensure GraphViz is installed.
yum: pkg=graphviz state=installed
2 changes: 1 addition & 1 deletion templates/xhprof.ini.j2
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[xhprof]
extension=xhprof.so
extension="{{ php_xhprof_module_path }}/xhprof.so"
xhprof.output_dir="{{ xhprof_output_dir }}"
19 changes: 19 additions & 0 deletions tests/Dockerfile.centos-6
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM centos:6

# Install Ansible
RUN yum -y update; yum clean all;
RUN yum -y install epel-release
RUN yum update openssl
RUN yum -y install git python-setuptools gcc sudo libffi-devel python-devel openssl-devel
RUN yum clean all
RUN easy_install pip
RUN pip install ansible

# Disable requiretty
RUN sed -i -e 's/^\(Defaults\s*requiretty\)/#--- \1/' /etc/sudoers

# Install Ansible inventory file
RUN mkdir - p /etc/ansible
RUN echo -e '[local]\nlocalhost ansible_connection=local' > /etc/ansible/hosts

CMD ["/usr/sbin/init"]
29 changes: 29 additions & 0 deletions tests/Dockerfile.centos-7
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
FROM centos:7

# Install systemd -- See https://hub.docker.com/_/centos/
RUN yum -y swap -- remove fakesystemd -- install systemd systemd-libs
RUN yum -y update; yum clean all; \
(cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/multi-user.target.wants/*; \
rm -f /etc/systemd/system/*.wants/*; \
rm -f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f /lib/systemd/system/basic.target.wants/*; \
rm -f /lib/systemd/system/anaconda.target.wants/*;

# Install Ansible
RUN yum -y install git python-setuptools gcc sudo libffi-devel python-devel openssl-devel
RUN yum clean all
RUN easy_install pip
RUN pip install ansible

# Disable requiretty
RUN sed -i -e 's/^\(Defaults\s*requiretty\)/#--- \1/' /etc/sudoers

# Install Ansible inventory file
RUN mkdir - p /etc/ansible
RUN echo -e '[local]\nlocalhost ansible_connection=local' > /etc/ansible/hosts

VOLUME ["/sys/fs/cgroup"]
CMD ["/usr/sbin/init"]
11 changes: 11 additions & 0 deletions tests/Dockerfile.ubuntu-12.04
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM ubuntu:12.04
RUN apt-get update

# Install Ansible
RUN apt-get install -y software-properties-common python-software-properties git
RUN apt-add-repository -y ppa:ansible/ansible
RUN apt-get update
RUN apt-get install -y ansible

# Install Ansible inventory file
RUN echo "[local]\nlocalhost ansible_connection=local" > /etc/ansible/hosts
11 changes: 11 additions & 0 deletions tests/Dockerfile.ubuntu-14.04
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM ubuntu:14.04
RUN apt-get update

# Install Ansible
RUN apt-get install -y software-properties-common git
RUN apt-add-repository -y ppa:ansible/ansible
RUN apt-get update
RUN apt-get install -y ansible

# Install Ansible inventory file
RUN echo "[local]\nlocalhost ansible_connection=local" > /etc/ansible/hosts
21 changes: 21 additions & 0 deletions tests/test-source.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
- hosts: all

vars:
php_enable_webserver: false
php_version: 5.6
php_enablerepo: "remi,remi-php56"

pre_tasks:
- name: Ensure build dependencies are installed (RedHat).
yum: name=which state=present
when: ansible_os_family == 'RedHat'

- name: Add repository for PHP 5.6.
apt_repository: repo='ppa:ondrej/php5-5.6'
when: ansible_os_family == 'Debian'

roles:
- { role: geerlingguy.repo-remi, when: ansible_os_family == 'RedHat' }
- geerlingguy.php
- role_under_test
8 changes: 0 additions & 8 deletions tests/test.yml

This file was deleted.

25 changes: 22 additions & 3 deletions tests/xhprof-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Test if XHProf is available and working.
*/

$xhprof_root_dir = '/usr/share/php';
$success = TRUE;

xhprof_enable();
Expand All @@ -18,12 +19,30 @@
$success = FALSE;
}

include $xhprof_root_dir . '/xhprof_lib/utils/xhprof_lib.php';
include $xhprof_root_dir . '/xhprof_lib/utils/xhprof_runs.php';

$xhprof_runs = new XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($data, "xhprof_testing");
$output_dir = ini_get("xhprof.output_dir");
if (!empty($output_dir) && is_writable($output_dir)) {
print "XHProf output directory writable.\r\n";
$filename = "$output_dir/$run_id.xhprof_testing.xhprof";

if (file_exists($filename)) {
print "XHProf PHP library writing to output directory.\r\n";
}
else {
print "XHProf PHP library not working.\r\n";
$success = FALSE;
}

ob_start();
include $xhprof_root_dir . '/xhprof_html/index.php';
$html = ob_get_clean();
if (strpos($html, "?run=$run_id") !== FALSE) {
print "XHProf UI working.\r\n";
}
else {
print "XHProf output directory not writable.\r\n";
print "XHProf UI not working.\r\n";
$success = FALSE;
}

Expand Down
3 changes: 1 addition & 2 deletions vars/Debian.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
php_xhprof_lib_dir: /usr/share/php/xhprof_lib
php_xhprof_html_dir: /usr/share/php/xhprof_html
php_xhprof_module_path: /usr/lib/php5/modules

php_xhprof_config_filename: 20-xhprof.ini
php_xhprof_config_dirs:
Expand Down
Loading

0 comments on commit eab43ed

Please sign in to comment.