Browse files

Enhancement: recipe to install the AWS ElastiCache client for PHP (#1092

)

* Enhancement: recipe to install the AWS ElastiCache client for PHP

Related: easybib/ops#10

* Refactor: move shellout to Php::Config

 * allows us to mock this in a test
 * moves logic from "untestable" LWRP to library

* Update: add copy action

 * allows us to copy modules around
 * leverages internals from php_pecl/Php::Config, etc.
  • Loading branch information...
1 parent 10eefcc commit 707dc0e8b5f89ae69416ac3ed455d2bcf772d766 @till committed with fh Aug 24, 2016
View
4 php/attributes/default.rb
@@ -55,3 +55,7 @@
'require_hash' => 'Off'
}
default['php-phar']['load_priority'] = nil
+
+default['amazon-elasticache-cluster-client'] = {
+ 'php_version' => '7.0'
+}
View
BIN php/files/default/5.6-amazon-elasticache-cluster-client.so
Binary file not shown.
View
BIN php/files/default/7.0-amazon-elasticache-cluster-client.so
Binary file not shown.
View
21 php/libraries/config.rb
@@ -1,3 +1,6 @@
+require 'chef/mixin/shell_out'
+include Chef::Mixin::ShellOut
+
module Php
class Config
def initialize(ext_name, directives)
@@ -26,5 +29,23 @@ def get_directives
keep
end
+
+ def get_extension_dir(prefix)
+ @extension_dir ||= begin
+ p = shell_out("#{prefix}/bin/php-config --extension-dir")
+ p.stdout.strip
+ end
+ end
+
+ def get_extension_files
+ files = []
+
+ p = shell_out("pecl list-files #{@ext_name}")
+ p.stdout.each_line.grep(/^src\s+.*\.so$/i).each do |line|
+ files << line.split[1]
+ end
+
+ files
+ end
end
end
View
4 php/libraries/matchers.rb
@@ -42,6 +42,10 @@ def install_php_pecl(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:php_pecl, :install, resource_name)
end
+ def copy_php_pecl(resource_name)
+ ChefSpec::Matchers::ResourceMatcher.new(:php_pecl, :copy, resource_name)
+ end
+
def compile_php_pecl(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:php_pecl, :compile, resource_name)
end
View
33 php/providers/pecl.rb
@@ -1,11 +1,10 @@
-require 'chef/mixin/shell_out'
include Chef::Mixin::ShellOut
action :install do
extension = new_resource.name
version = new_resource.version
- ext_dir = get_extension_dir
+ ext_dir = get_extension_dir(new_resource.prefix)
ext_dir << ::File::SEPARATOR if ext_dir[-1].chr != ::File::SEPARATOR
so_file = "#{ext_dir}/#{extension}.so"
@@ -23,6 +22,16 @@
end
+action :copy do
+ cbf = cookbook_file "#{get_extension_dir(new_resource.prefix)}/#{new_resource.name}" do
+ cookbook 'php'
+ source new_resource.ext_file.to_s
+ mode 0644
+ end
+
+ new_resource.updated_by_last_action(cbf.updated_by_last_action?)
+end
+
action :compile do
if new_resource.source_dir.empty?
@@ -47,7 +56,7 @@
'phpize',
configure,
'make',
- "cp modules/#{extension}.so #{get_extension_dir}"
+ "cp modules/#{extension}.so #{get_extension_dir(new_resource.prefix)}"
]
commands.each do |command|
@@ -61,20 +70,12 @@
end
-def get_extension_dir
- @extension_dir ||= begin
- p = shell_out("#{new_resource.prefix}/bin/php-config --extension-dir")
- p.stdout.strip
- end
+def get_extension_dir(prefix)
+ config = ::Php::Config.new('', {})
+ config.get_extension_dir(prefix)
end
def get_extension_files(name)
- files = []
-
- p = shell_out("pecl list-files #{name}")
- p.stdout.each_line.grep(/^src\s+.*\.so$/i).each do |line|
- files << line.split[1]
- end
-
- files
+ config = ::Php::Config.new(new_resource.name, {})
+ config.get_extension_files
end
View
21 php/recipes/module-aws_elasticache_cluster_client.rb
@@ -0,0 +1,21 @@
+include_recipe 'php::dependencies-ppa'
+
+ext = 'amazon-elasticache-cluster-client.so'
+
+source_version = node['amazon-elasticache-cluster-client']['php_version']
+
+php_pecl ext do
+ prefix node['php-fpm']['exec_prefix']
+ ext_file "#{source_version}-#{ext}"
+ action :copy
+end
+
+php_config File.basename(ext, '.so') do
+ config {}
+ config_dir node['php']['extensions']['config_dir']
+ extension_path ext
+ load_extension true
+ load_priority 10
+ suffix ''
+ notifies :reload, 'service[php-fpm]', :delayed
+end
View
3 php/resources/pecl.rb
@@ -1,4 +1,4 @@
-actions :install, :compile
+actions :install, :copy, :compile
def initialize(*args)
super
@@ -12,3 +12,4 @@ def initialize(*args)
attribute :config, :kind_of => Hash
attribute :source_dir, :kind_of => String, :default => ''
attribute :cflags, :kind_of => String
+attribute :ext_file, :kind_of => String, :default => nil
View
63 php/spec/module-aws_elasticache_cluster_client_spec.rb
@@ -0,0 +1,63 @@
+require_relative 'spec_helper.rb'
+require_relative '../libraries/config'
+
+describe 'php::module-aws_elasticache_cluster_client' do
+
+ let(:runner) { ChefSpec::Runner.new(:step_into => %w(php_pecl php_config)) }
+ let(:chef_run) { runner.converge(described_recipe) }
+ let(:node) { runner.node }
+
+ let(:ext) { 'amazon-elasticache-cluster-client.so' }
+
+ let(:ext_dir) { '/usr/lib/php/20151012' }
+
+ let(:my_config) { double(Php::Config) }
+
+ before do
+ node.set['php']['extensions']['config_dir'] = 'etc/php/7.0/mods-available'
+ node.set['php']['ppa'] = {
+ 'name' => 'ondrejphp',
+ 'uri' => 'ppa:ondrej/php',
+ 'package_prefix' => 'php7.0'
+ }
+ node.set['php-fpm'] = {
+ 'prefix' => '',
+ 'exec_prefix' => '/usr',
+ 'fpm_config' => 'etc/php/7.0/fpm/php.ini',
+ 'cli_config' => 'etc/php/7.0/cli/php.ini',
+ 'pool_dir' => 'etc/php/7.0/fpm/pool.d',
+ 'socketdir' => '/var/run/php',
+ 'pid' => '/var/run/php/php7.0-fpm.pid',
+ 'packages' => 'php7.0-fpm,php7.0-cli'
+ }
+
+ Php::Config.stub(:new).and_return(my_config)
+ my_config.stub(:get_extension_dir).with('/usr').and_return(ext_dir)
+ my_config.stub(:get_directives).and_return({})
+ end
+
+ it 'uses php_pecl to copy the file' do
+ expect(chef_run).to copy_php_pecl(ext)
+ end
+
+ it 'actually copies the file in place' do
+ expect(chef_run).to create_cookbook_file("#{ext_dir}/#{ext}")
+ end
+
+ it 'configures the extension' do
+ expect(chef_run).to generate_php_config(File.basename(ext, '.so'))
+ end
+
+ it 'renders the file' do
+ ini = '/etc/php/7.0/mods-available/10-amazon-elasticache-cluster-client.ini'
+ expect(chef_run).to create_template(ini)
+ expect(chef_run).to render_file(ini)
+ .with_content('extension=amazon-elasticache-cluster-client.so')
+ end
+
+ it 'creates links the ini file' do
+ %w(cli fpm).each do |sapi|
+ expect(chef_run).to create_link("enable_module_#{sapi}_amazon-elasticache-cluster-client")
+ end
+ end
+end
View
6 vagrant-test/php7/Vagrantfile
@@ -0,0 +1,6 @@
+my_runlist = %w(stack-easybib::role-phpapp php::module-aws_elasticache_cluster_client)
+
+ENV['recipe_runlist'] = my_runlist.join(',')
+ENV['chef_json'] = "#{File.dirname(__FILE__)}/deploy.json"
+
+require_relative '../vagrant-provision'
View
25 vagrant-test/php7/deploy.json
@@ -0,0 +1,25 @@
+{
+ "php": {
+ "extensions": {
+ "config_dir": "etc/php/7.0/mods-available"
+ },
+ "ppa": {
+ "name": "ondrejphp",
+ "uri": "ppa:ondrej/php",
+ "package_prefix": "php7.0"
+ }
+ },
+ "php-apc": {
+ "package_prefix": "php"
+ },
+ "php-fpm": {
+ "prefix": "",
+ "exec_prefix": "/usr",
+ "fpm_config": "etc/php/7.0/fpm/php.ini",
+ "cli_config": "etc/php/7.0/cli/php.ini",
+ "pool_dir": "etc/php/7.0/fpm/pool.d",
+ "socketdir": "/var/run/php",
+ "pid": "/var/run/php/php7.0-fpm.pid",
+ "packages": "php7.0-fpm,php7.0-cli,php7.0-pdo-mysql,php7.0-curl"
+ }
+}

0 comments on commit 707dc0e

Please sign in to comment.