问题
We're looking to deploy a few Laravel4 based PHP apps on amazon with OpsWorks, this requires a few things:
- Grab code from git
- Download composer.phar from getcomposer.com
- Run
php composer.phar install
- Change permissions on a few specific folders
I'm completely fresh with it comes to chef, so initially looking for a place to get to grips with the basics of chef, and then how to achieve the tasks above, would appreciate any pointers.
回答1:
I'm no Chef guru (I usually use Puppet) but try the following:
Grab code from git
You may want to execute a wget command (see examples below).
If you want something more sophisticated see http://docs.opscode.com/resource_deploy.html
deploy_revision "/path/to/application" do
repo 'ssh://name-of-git-repo/repos/repo.git'
migrate false
purge_before_symlink %w{one two folder/three}
create_dirs_before_symlink []
symlinks(
"one" => "one",
"two" => "two",
"three" => "folder/three"
)
before_restart do
# some Ruby code
end
notifies :restart, "service[foo]"
notifies :restart, "service[bar]"
end
Download composer.phar from getcomposer.com
I would execute a wget.
I lifted some code from here: http://cookingclouds.com/2012/06/23/chef-simple-cookbook-example/
It's basically just doing a wget in a specific folder, extracting the contents of the tar & updating some permissions on the new files. It only does this if the folder doesn't already exist.
# Run a bash shell - download and extract composer
bash "install_composer" do
user "root"
cwd "/folder/to/extact/to"
code <<-EOH
wget http://getcomposer.com/composer.tar.gz
tar -xzf composer.tar.gz
chown -R user:group /folder/to/extact/to
EOH
not_if "test -d /folder/to/extact/to"
end
Run php composer.phar install
http://docs.opscode.com/resource_execute.html
execute "composer install" do
command "php composer.phar install && touch /var/log/.php_composer_installed"
creates "/var/log/.php_composer_installed"
action :run
end
This will only run it once, otherwise you can remove the "creates" and it will run it each time.
Change permissions on a few specific folders
http://docs.opscode.com/resource.html
directory "/tmp/folder" do
owner "root"
group "root"
mode 0755
action :create
end
If the directory already exists, nothing will happen. If the directory was changed in any way, the resource is marked as updated.
Finally
I find the search handy, browsing stuff on the Chef site seems to be hopeless (too much stuff to dig through). http://docs.opscode.com/search.html
回答2:
I would go pretty much with Drew Khoury's answer, with one change. To download compose.phar, you can use the remote_file resource instead of doing a wget in a bash script.
回答3:
As composer.phar
is already an executable, you could simply put it to a dir in your $PATH
:
remote_file '/usr/bin/composer' do
source 'http://getcomposer.org/composer.phar'
mode '0755'
action :create_if_missing
end
回答4:
You can configure your git hook post-receive to something like that:
GIT_WORK_TREE=/path/to/your/site
cd /path/to/your/site
curl -sS https://getcomposer.org/installer | php
php composer.phar install
# do your stuff here
And make sure to give executable permissions to the post-receive file.
回答5:
Or you can just commit your vendor directory. We have a couple projects running on Laravel 4 and OpsWorks.
回答6:
I'm in the same position with Laravel and OpsWorks. I was looking for a solution but then...
What happens if someone injects a security flaw into one of the sub modules? Does that mean we trust X number of external code-bases to be 100% secure all the time?
Either there is some fundamental flaw in my understanding or running composer at all on a production server is about the most serious WTF there is.
I've now made sure that the code in my project repo will be deployed 100% as is. No downloading external modules on the production server ever.
来源:https://stackoverflow.com/questions/16072524/getting-started-with-chef-and-running-composer-install-on-deploy