• Stars
    star
    142
  • Rank 250,271 (Top 6 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created over 10 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Cloud Foundry Buildpack for PHP.

Cloud Foundry PHP Buildpack

CF Slack Join us on Slack

A buildpack to deploy PHP applications to Cloud Foundry based systems, such as a cloud provider or your own instance.

Buildpack User Documentation

Official buildpack documentation can be found here: php buildpack docs.

Building the Buildpack

Option 1: Using the package.sh script

  1. Run ./scripts/package.sh [ --uncached | --cached ] [ --stack=STACK ]

This requires that you have docker installed on your local machine, as it will run packaging setup within a ruby image.

Option 2: Manually use the buildpack-packager

  1. Make sure you have fetched submodules

    git submodule update --init
  2. Check out a tagged release. It is not recommended to bundle buildpacks based on master or develop as these are moving targets.

    git checkout v4.4.2  # or whatever version you want, see releases page for available versions
  3. Get latest buildpack dependencies, this will require having Ruby 3.0 or running in a Ruby 3.0 container image

    BUNDLE_GEMFILE=cf.Gemfile bundle
  4. Build the buildpack. Please note that the PHP buildpack still uses the older Ruby based buildpack packager. This is different than most of the other buildpacks which use a newer Golang based buildpack packager. You must use the Ruby based buildpack packager with the PHP buildpack.

    BUNDLE_GEMFILE=cf.Gemfile bundle exec buildpack-packager [ --uncached | --cached ] [ --any-stack | --stack=STACK ]
  5. Use in Cloud Foundry

    Upload the buildpack to your Cloud Foundry instance and optionally specify it by name

    cf create-buildpack custom_php_buildpack php_buildpack-cached-custom.zip 1
    cf push my_app -b custom_php_buildpack

Contributing

Find our guidelines here.

Integration Tests

Buildpacks use the Cutlass framework for running integration tests.

To run integration tests, run the following command:

./scripts/integration.sh

Unit Tests

To run unit tests, run the following command:

./scripts/unit

Requirements

  1. PyEnv - This will allow you to easily install Python 2.6.6, which is the same version available through the staging environment of CloudFoundry.
  2. virtualenv & pip - The buildpack uses virtualenv and pip to setup the required packages. These are used by the unit test and not required by the buildpack itself.

Setup

git clone https://github.com/cloudfoundry/php-buildpack
cd php-buildpack
python -V  # should report 2.6.6, if not fix PyEnv before creating the virtualenv
virtualenv `pwd`/env
. ./env/bin/activate
pip install -r requirements.txt

Project Structure

The project is broken down into the following directories:

  • bin contains executable scripts, including compile, release and detect
  • defaults contains the default configuration
  • docs contains project documentation
  • extensions contains non-core extensions
  • env virtualenv environment
  • lib contains core extensions, helper code and the buildpack utils
  • scripts contains the Python scripts that run on compile, release and detect
  • tests contains test scripts and test data
  • run_tests.sh a convenience script for running the full suite of tests

Understanding the Buildpack

The easiest way to understand the buildpack is to trace the flow of the scripts. The buildpack system calls the compile, release and detect scripts provided by the buildpack. These are located under the bin directory and are generic. They simply redirect to the corresponding Python script under the scripts directory.

Of these, the detect and release scripts are straightforward, providing the minimal functionality required by a buildpack. The compile script is more complicated but works like this.

  • load configuration
  • setup the WEBDIR directory
  • install the buildpack utils and the core extensions (HTTPD, Nginx & PHP)
  • install other extensions
  • install the rewrite and start scripts
  • setup the runtime environment and process manager
  • generate a startup.sh script

Extensions

The buildpack relies heavily on extensions. An extension is simply a set of Python methods that will get called at various times during the staging process.

Included non-core extensions:

Adding extensions

In general, you shouldn't need to modify the buildpack itself. Instead creating your own extension should be the way to go.

To create an extension, simply create a folder. The name of the folder will be the name of the extension. Inside that folder, create a file called extension.py. That file will contain your code. Inside that file, put your extension methods and any additional required code.

It's not necessary to fork the buildpack to add extensions for your app. The buildpack will notice and use extensions if you place them in a .extensions folder at your application root. See the extensions directory in the this example for a sample.

Methods

Here is an explanation of the methods offered to an extension developer. All of them are optional and if a method is not implemented, it is simply skipped.

def configure(ctx):
    pass

The configure method gives extension authors a chance to adjust the configuration of the buildpack prior to any extensions running. The method is called very early on in the lifecycle of the buildpack, so keep this in mind when using this method. The purpose of this method is to allow an extension author the opportunity to modify the configuration for PHP, the web server or another extension prior to those components being installed.

An example of when to use this method would be to adjust the list of PHP extensions that are going to be installed.

The method takes one argument, which is the buildpack context. You can edit the context to update the state of the buildpack. Return value is ignore / not necessary.

def preprocess_commands(ctx):
    return ()

The preprocess_commands method gives extension authors the ability to contribute a list of commands that should be run prior to the services. These commands are run in the execution environment, not the staging environment and should execute and complete quickly. The purpose of these commands is to give extension authors the chance to run any last-minute code to adjust to the environment.

As an example, this is used by the core extensions rewrite configuration files with information that is specific to the runtime environment.

The method takes the context as an argument and should return a tuple of tuples (i.e. list of commands to run).

def service_commands(ctx):
    return {}

The service_commands method gives extension authors the ability to contribute a set of services that need to be run. These commands are run and should continue to run. If any service exits, the process manager will halt all of the other services and the application will be restarted by Cloud Foundry.

The method takes the context as an argument and should return a dictionary of services to run. The key should be the service name and the value should be a tuple which is the command and arguments.

def service_environment(ctx):
    return {}

The service_environment method gives extension authors the ability to contribute environment variables that will be set and available to the services.

The method takes the buildpack context as its argument and should return a dictionary of the environment variables to be added to the environment where services (see service_commands) are executed.

The key should be the variable name and the value should be the value. The value can either be a string, in which case the environment variable will be set with the value of the string or it can be a list.

If it's a list, the contents will be combined into a string and separated by the path separation character (i.e. ':' on Unix / Linux or ';' on Windows). Keys that are set multiple times by the same or different extensions are automatically combined into one environment variable using the same path separation character. This is helpful when two extensions both want to contribute to the same variable, for example LD_LIBRARY_PATH.

Please note that environment variables are not evaluated as they are set. This would not work because they are set in the staging environment which is different than the execution environment. This means you cannot do things like PATH=$PATH:/new/path or NEWPATH=$HOME/some/path. To work around this, the buildpack will rewrite the environment variable file before it's processed. This process will replace any @<env-var> markers with the value of the environment variable from the execution environment. Thus if you do PATH=@PATH:/new/path or NEWPATH=@HOME/some/path, the service end up with a correctly set PATH or NEWPATH.

def compile(install):
    return 0

The compile method is the main method and where extension authors should perform the bulk of their logic. This method is called by the buildpack while it's installing extensions.

The method is given one argument which is an Installer builder object. The object can be used to install packages, configuration files or access the context (for examples of all this, see the core extensions like HTTPD, Nginx, PHP, Dynatrace and NewRelic). The method should return 0 when successful or any other number when it fails. Optionally, the extension can raise an exception. This will also signal a failure and it can provide more details about why something failed.

Method Order

It is sometimes useful to know what order the buildpack will use to call the methods in an extension. They are called in the following order.

  1. configure
  2. compile
  3. service_environment
  4. service_commands
  5. preprocess_commands

Example

Here is an example extension. While technically correct, it doesn't actually do anything.

Here's the directory.

$ ls -lRh
total 0
drwxr-xr-x  3 daniel  staff   102B Mar  3 10:57 testextn

./testextn:
total 8
-rw-r--r--  1 daniel  staff   321B Mar  3 11:03 extension.py

Here's the code.

import logging

_log = logging.getLogger('textextn')

# Extension Methods
def configure(ctx):
    pass

def preprocess_commands(ctx):
    return ()

def service_commands(ctx):
    return {}

def service_environment(ctx):
    return {}

def compile(install):
    return 0

Tips

  1. To be consistent with the rest of the buildpack, extensions should import and use the standard logging module. This will allow extension output to be incorporated into the output for the rest of the buildpack.
  2. The buildpack will run every extension that is included with the buildpack and the application. There is no mechanism to disable specific extensions. Thus, when you write an extension, you should make some way for the user to enable / disable it's functionality. See the NewRelic extension for an example of this.
  3. If an extension requires configuration, it should be included with the extension. The defaults/options.json file is for the buildpack and its core extensions. See the NewRelic buildpack for an example of this.
  4. Extensions should have their own test module. This generally takes the form tests/test_<extension_name>.py.
  5. Run bosh-lite. It'll speed up testing and allow you to inspect the environment manually, if needed.
  6. Run a local web server for your binaries. It'll seriously speed up download times.
  7. Test, test and test again. Create unit and integration tests for your code and extensions. This gives you quick and accurate feedback on your code. It also makes it easier for you to make changes in the future and be confident that you're not breaking stuff.
  8. Check your code with flake8. This linting tool can help to detect problems quickly.

Help and Support

Join the #buildpacks channel in our Slack community

Reporting Issues

This project is managed through GitHub. If you encounter any issues, bug or problems with the buildpack please open an issue.

Active Development

The project backlog is on Pivotal Tracker

More Repositories

1

bosh

Cloud Foundry BOSH is an open source tool chain for release engineering, deployment and lifecycle management of large scale distributed services.
Ruby
2,010
star
2

cli

The official command line client for Cloud Foundry
Go
1,733
star
3

uaa

CloudFoundry User Account and Authentication (UAA) Server
Java
1,541
star
4

java-buildpack-memory-calculator

Cloud Foundry JVM Memory Calculator
Go
602
star
5

gosigar

A Golang implementation of the Sigar API
Go
453
star
6

gorouter

CF Router
Go
429
star
7

java-buildpack

Cloud Foundry buildpack for running Java applications
Ruby
425
star
8

go-diodes

Diodes are ring buffers manipulated via atomics.
Go
411
star
9

cf-java-client

Java Client Library for Cloud Foundry
Java
318
star
10

cf-for-k8s

The open source deployment manifest for Cloud Foundry on Kubernetes
Shell
302
star
11

korifi

Cloud Foundry on Kubernetes
Go
284
star
12

cf-deployment

The canonical open source deployment manifest for Cloud Foundry
Go
279
star
13

stratos

Stratos: Web-based Management UI for Cloud Foundry and Kubernetes
TypeScript
241
star
14

credhub

CredHub centralizes and secures credential generation, storage, lifecycle management, and access
Java
225
star
15

garden

Go Warden
Go
223
star
16

java-buildpack-auto-reconfiguration

Auto-reconfiguration functionality for the Java Buildpack
Java
219
star
17

loggregator-release

Cloud Native Logging
Go
217
star
18

bytefmt

Human readable byte formatter
Go
208
star
19

diego-release

BOSH Release for Diego
HTML
199
star
20

staticfile-buildpack

Deploy static HTML/JS/CSS apps to Cloud Foundry
Go
199
star
21

cloud_controller_ng

Cloud Foundry Cloud Controller
Ruby
181
star
22

bosh-bootloader

Command line utility for standing up a BOSH director on an IAAS of your choice.
Go
176
star
23

bosh-cli

BOSH CLI v2+
Go
174
star
24

nodejs-buildpack

Cloud Foundry buildpack for Node.js
Go
161
star
25

diego-design-notes

Diego Architectural Design Musings and Explications
HTML
142
star
26

bosh-deployment

Collection of BOSH manifests referenced by cloudfoundry/docs-bosh
Shell
125
star
27

python-buildpack

Cloud Foundry buildpack for the Python Language
Go
118
star
28

eirini

Pluggable container orchestration for Cloud Foundry, and a Kubernetes backend
Go
115
star
29

go-buildpack

Cloud Foundry buildpack for the Go Language
Go
80
star
30

multiapps-cli-plugin

A CLI plugin for Multi-Target Application (MTA) operations in Cloud Foundry
Go
77
star
31

cloud-service-broker

OSBAPI service broker that uses Terraform to provision and bind services. Derived from https://github.com/GoogleCloudPlatform/gcp-service-broker
Go
76
star
32

guardian

containers4life
Go
75
star
33

lager

An opinionated logger for Go.
Go
73
star
34

app-autoscaler

Auto Scaling for CF Applications
Go
73
star
35

ibm-websphere-liberty-buildpack

IBM WebSphere Application Server Liberty Buildpack
Ruby
71
star
36

summit-training-classes

Opensourced content for cloud foundry training classes: zero to hero (beginner), bosh/operator, and microservices
JavaScript
69
star
37

cf-acceptance-tests

CF Acceptance tests
Go
68
star
38

cf-networking-release

Container Networking for CloudFoundry
Go
68
star
39

ruby-buildpack

Cloud Foundry buildpack for Ruby, Sinatra and Rails
Go
63
star
40

garden-runc-release

Shell
63
star
41

bosh-google-cpi-release

BOSH Google CPI
Go
62
star
42

bosh-azure-cpi-release

BOSH Azure CPI
Ruby
61
star
43

loggregator

Archived: Now bundled in https://github.com/cloudfoundry/loggregator-release
Go
60
star
44

cf-mysql-release

Cloud Foundry MySQL Release
Go
58
star
45

go-pubsub

Tree based pubsub library for Go.
Go
56
star
46

bosh-agent

BOSH Agent runs on each BOSH deployed VM
Go
56
star
47

docs-book-cloudfoundry

The bookbinder repository for open source Cloud Foundry documentation
HTML
55
star
48

homebrew-tap

Cloud Foundry Homebrew packages
Ruby
53
star
49

multiapps-controller

The server side component (controller) for Multi-Target Application (MTA) for Cloud Foundry
Java
52
star
50

socks5-proxy

This is a go library for starting a socks5 proxy server via SSH
Go
44
star
51

cf-uaac

Ruby
41
star
52

docs-cloudfoundry-concepts

A place for architecture and concept docs
HTML
41
star
53

buildpacks-ci

Concourse CI pipelines for the buildpacks team
HTML
41
star
54

service-fabrik-broker

Cloud Foundry service broker which provisions service instances as Docker containers and BOSH deployments.
JavaScript
40
star
55

grootfs

Garden root file system
Go
40
star
56

routing-release

This is the BOSH release for cloud foundry routers
Ruby
39
star
57

docs-dev-guide

Documentation for application developers who want to deploy their applications to Cloud Foundry
HTML
39
star
58

community

Governance and contact information for Cloud Foundry
Python
38
star
59

cf-smoke-tests

Smoke tests for CloudFoundry that are safe to run in a production environment
Go
38
star
60

credhub-cli

CredHub CLI provides a command line interface to interact with CredHub servers
Go
38
star
61

bosh-linux-stemcell-builder

BOSH Ubuntu Linux stemcells
Ruby
37
star
62

haproxy-boshrelease

A BOSH release for haproxy (based on cf-release's haproxy job)
Ruby
37
star
63

pmc-notes

Agendas and Notes for Cloud Foundry Project Management Committee Meetings
36
star
64

eirini-release

Helm release for Project Eirini
Shell
36
star
65

bosh-s3cli

Go CLI for S3
Go
36
star
66

bpm-release

isolated bosh jobs
Go
35
star
67

cfdot

A command-line tool to interact with a Cloud Foundry Diego deployment.
Go
34
star
68

libbuildpack

A library for writing buildpacks
Go
34
star
69

bosh-openstack-cpi-release

BOSH OpenStack CPI
Ruby
33
star
70

java-test-applications

Applications used for testing the Java buildpack
Java
33
star
71

switchboard

Golang TCP Proxy
JavaScript
33
star
72

docs-bosh

The docs repo for BOSH
HTML
32
star
73

cf-k8s-networking

building a cloud foundry without gorouter....
Go
32
star
74

cflinuxfs2

The official Cloud Foundry app container rootfs
Ruby
31
star
75

pxc-release

BOSH release of Percona Xtradb Cluster
JavaScript
30
star
76

clock

time provider & rich fake for Go
Go
30
star
77

bosh-vsphere-cpi-release

BOSH vSphere CPI
Ruby
30
star
78

os-conf-release

Additional Linux OS configuration release
Go
30
star
79

binary-buildpack

Deploy binaries to Cloud Foundry
Shell
28
star
80

bbs

Internal API to access the database for Diego.
Go
28
star
81

nginx-buildpack

Cloud Foundry buildpack that provides NGINX
Go
28
star
82

jumpbox-deployment

Deploy single vanilla jumpbox machine with BOSH
Shell
28
star
83

bosh-aws-cpi-release

BOSH AWS CPI
Ruby
27
star
84

uaa-release

Bosh Release for the UAA
Ruby
27
star
85

app-autoscaler-release

Automated scaling for apps running on Cloud Foundry
Go
26
star
86

archiver

Utilities for extracting and compressing tgz and zip files.
Go
26
star
87

bosh-backup-and-restore

Go
26
star
88

exemplar-release

Shell
25
star
89

apt-buildpack

Go
25
star
90

diego-notes

Diego Notes
23
star
91

capi-release

Bosh Release for Cloud Controller and friends
HTML
23
star
92

noaa

NOAA is a client library to consume metric and log messages from Doppler.
Go
23
star
93

metric-store-release

Metric Store: A Cloud-Native Time Series Database for Cloud Foundry
Go
23
star
94

cli-plugin-repo

Public repository for community created CF CLI plugins.
Go
23
star
95

cf-deployment-concourse-tasks

Shell
23
star
96

buildpack-packager

Buildpack Packager
Ruby
23
star
97

uaa-cli

CLI for UAA written in Go
Go
22
star
98

galera-healthcheck

A lightweight web server written in Golang to check the health of a node in a Galera cluster
Go
21
star
99

docs-buildpacks

HTML
21
star
100

winc

CLI tool for spawning and running containers on Windows according to the OCI specification
Go
21
star