Compared with Service Broker, Buildpack's practical operation is much easier, the general concept alone, in fact, do not have to write a single article, but Virgo Obsessive-compulsive disorder, so still write, The two dimensions that enable the CF framework to scale out (the services that the code uses and the environment in which the code runs) are complete. This article will mainly write Buildpack basic implementation logic, and then give three need to modify the requirements of buildpack, the actual operation of the description.
Basic principle
The basic process for CF to run the application is to unpack the application package that the user publishes, then fetch all of his buildpack, match the package in the order specified, until it finds the first buildpack to run the code, and then unlocks the Buildpack. With these application code into a package (that is, droplet), in accordance with the specified operating environment parameters to build the container, the droplet thrown in, according to Buildpack specified start command, launch the application. In the above process, Buildpack implements the three-step function:
The first step,detect: Checks whether the current application package can run with this buildpack support, for example, Java Buildpack discovers Web-inf path and thinks it can run it.
The second step,compile: The application package with the Buildpack package, such as putting the Java package into the Tomcat application directory, and then replace some parameters, such as the current DEA in the random port assigned to this Tomcat instance.
Part III,release: Start droplet, such as the startup.sh that runs Tomcat.
Any buildpack has a bin path, a script with three specified names (detect, compile, release) (any script that the DEA OS can execute), and then the specific implementation logic is triggered from here. The following will take Java Buildpack as an example to introduce the development and use of buildpack through three actual requirements.
Custom Buildpack Update the default time zone and encoding requirements for Java apps
International software uses Greenwich Mean time as the default in the system, so we write the log or use the program code to get the system time (in fact, the DEA system time) are +0000, and we need +0800, of course, can be handled from the code itself. But my users are not happy, they said that my program also to local survey, too messy, you have to deal with me.
The default encoding in Tomcat should be utf8, but in some scenarios, such as the rest service running on Tomcat, the message contains Chinese, and it cannot be properly received. CF Java Buildpack contains the default tomcat, so in order to solve this problem, you can use the following command to set up, but after all, it is not cool, or directly get rid of buildpack more friendly.
cf set-env appname CATALINA_OPTS "$CATALINA_OPTS -Dfile.encoding=UTF-8"
Solution Solutions
Java Buildpack is written in Ruby, so if it's not a principled change, you can do it by opening the package, modifying the Ruby code, and then compressing it. However, WinRAR seems to have some puzzling problem, if decompression and compression will have a problem, but if you directly drag the modified name of the file into the WinRAR window to replace, you can work. In order to achieve the time zone and coding requirements, the file needs to be modified as follows:
In JAVA-BUILDPACK-OFFLINE-V#.#.#\LIB\JAVA_BUILDPACK\COMPONENT\JAVA_OPTS.RB
Add two methods to the list:
# @return [JavaOpts] +self+-Duser.timezone def add_timezone(value) self << "-Duser.timezone=#{value}" selfend# @return [JavaOpts] +self+-Dfile.encoding def add_fileencode(value) self << "-Dfile.encoding=#{value}" selfend
In Java-buildpack-offline-v#.#.#\lib\java_buildpack\jre\open_jdk_like.rb, modify the release method to add Add_timezone and add_ Fileencode Call
@droplet.java_opts .add_system_property(‘java.io.tmpdir‘, ‘$TMPDIR‘) .add_option(‘-XX:OnOutOfMemoryError‘, killjava) .add_timezone(‘GMT+08‘) .add_fileencode(‘UTF-8‘) .concat memory
Once modified, replace the two files with the original offline Buildpack package and publish them to CF to see the current buildpack situation:
cf buidlpacks
Then, create a new Buildpack, where the last parameter determines the order in which CF views Buildpack conform to the application and needs to be adjusted according to the actual situation.
cf create-buildpack java_buildpack_with_gmt0800_offline d:\somedir\java-buildpack-with-gmt0800-offline-v2.4.zip 1
If it is an update, use cf update-buildpack .
Tomcat requirements for using application packages
Some applications, especially those made by the product company, will have a certain tailoring to Tomcat, which is very painful when migrating to cf. The simple way is to make a buildpack, and provide only the JRE.
Solution Solutions
Assuming that the product's identity is Livebos, there is a Livebos folder under the first-tier path of the package:
Add a libe_bos.rb to the java-buildpack-with-livebos-offline-v#.#\lib\java_buildpack\container\:
# encoding:utf-8# Cloud Foundry Java buildpack# Copyright The original author or authors.## Licensed under the Apach E License, Version 2.0 (the "License"); # you are not a use this file except in compliance with the license.# A copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## unless required by applicable law or agreed t O in writing, software# distributed under the License are distributed on a "as is" basis,# without warranties OR CONDITION S of any KIND, either express or implied.# see the License for the specific language governing permissions and# limitation s under the License.require ' Java_buildpack/container ' require ' java_buildpack/container/dist_zip_like ' require ' Java_ Buildpack/util/dash_case ' module Javabuildpack Module Container # encapsulates the detect, compile, and release F Unctionality for +livebos+ style applications. Class Livebos < javabuildpack::container::D Istziplike # creates an instance # # @param [Hash] Context a collection of utilities used the component Def initialize (context) Super (context) end # (see Javabuildpack::component::basecomponent#compile) d EF compile Startup.chmod 0755 end # (see Javabuildpack::component::basecomponent#rele ASE) def release @droplet. Java_opts.add_system_property ' Http.port ', ' $PORT ' [ @droplet. Java_home.as_env_var, @droplet. Java_opts.as_env_var, Qualify _path (Catalina, @droplet. Root), ' Run '].flatten.compact.join (') end Protected # (see Javabuildpack::container::D istziplike#id) def ID livebos.to_s.d Ash_case End # (see Javabuildpack::container::D istziplike#supports?) DEF supports? Livebos? &&Amp Catalina.exist? End Private Def startup candidates = (root + ' bin/startup.sh '). Glob CA Ndidates.size = = 1? Candidates.first:nil End def Catalina candidates = (root + ' bin/catalina.sh '). Glob Candidates.size = = 1? Candidates.first:nil End def Livebos? (Root + ' livebos '). Exist? End End EndEnd
Add the following in the JAVA-BUILDPACK-WITH-LIVEBOS-OFFLINE-V2.4\CONFIG\COMPONENTS.YML containers:
- "JavaBuildpack::Container::LiveBOS"
This is followed by a package release.
Replace the JRE requirement in the Buildpack
Some applications can only use a specific version of the JRE, if the JRE is relatively new, but if it is old, such as 1.6, then the CF can be found in the buildpack is not supported. In addition, it is possible that the application requires the use of an Oracle JRE (the default in CF is the Open JRE).
Solution Solutions
Before you start, it is recommended that you do not try this compilation process on Windows, basically there is no possibility of success, just find a network can run Ruby Linux.
Modify the java-buildpack-offline-v2.4\config\components.yml to comment out the unused JRE:
- "JavaBuildpack::Jre::OpenJdkJRE"# - "JavaBuildpack::Jre::OracleJRE"
Modify the JAVA-BUILDPACK-OFFLINE-V2.4\CONFIG\ORACLE_JRE.YML to update the contents of the following line to a local tomcat address:
repository_root: http://localhost:8080/myapp
In the local MyApp, a index.yml file is placed with an Oracle JRE similar to the following, and if it is not already found on the Oracle Web site, it is also placed on the local tomcat.
---1.7.0_01: http://download.run.pivotal.io/openjdk/centos6/x86_64/openjdk-1.7.0_01.tar.gz......1.8.0_M6: http://download.run.pivotal.io/openjdk/centos6/x86_64/openjdk-1.8.0_M6.tar.gz
Then, you can compile the package.
bundle installbundle exec rake package OFFLINE=true...Creating build/java-buildpack-offline-cfd6b17.zip
Finally, of course, it's released to CF.
Cloud Foundry Buildpack Practical Analysis