首先感谢 @lgn21st 送的邀请码。
得到邀请码后,昨天就把自己刚开始开发的一个项目部署上去了,总体而言,非常的简单,按官方的文档操作就是了。我这里把它综合一下,主要是减少初次接触 Cloud Foundry 的兄弟的 google 时间。
官方文档中有这么一段:
# For Ruby 1.9 Cloud Foundry requires a tweak to the jquery-rails gem.
# gem 'jquery-rails'
gem 'cloudfoundry-jquery-rails'
# For Ruby 1.9 Cloud Foundry requires a tweak to devise.
# Uncomment next line if you plan to use devise.
# gem 'cloudfoundry-devise', :require => 'devise'
但是 cloudfoundry-jquery-rails
和 cloudfoundry-devise
都差不多有一年没有维护了,咋整?
其实是文档中没有把 require a tweak
说清楚,事实上只不过就是 jquery-rails 和 devise 的 gemspec 中的作者姓名中包含有非 ASCII 的 UTF 字符,把这些字符去掉就可以了。如果你嫌自己 fork 代码麻烦,也可以直接用我 fork 的:
gem 'jquery-rails', :git => 'git://github.com/xhj/jquery-rails.git'
gem 'devise', :git => 'git://github.com/xhj/devise.git'
据说 Cloud Foundry 是支持自动化配置 MySQL 的,但我没有试,因为手工配置也是很简单的,如下:
首先在 Gemfile 中加入gem 'cf-runtime'
然后在 database.yml 中这么配置:
adapter: mysql2
encoding: utf8
<% require 'cfruntime/properties' %>
<% db_svc = CFRuntime::CloudApp.service_props('mysql') %>
database: <%= db_svc[:database] rescue 'xxx_production' %>
username: <%= db_svc[:username] rescue 'xhj' %>
password: <%= db_svc[:password] rescue 'xhj_love_xxx' %>
host: <%= db_svc[:host] rescue 'localhost' %>
port: <%= db_svc[:port] rescue '3306' %>
最后再新建一个初始化文件cf_db_migrate.rb
,内容如下:
require 'cfruntime/properties'
# Run the equivalent of rake db:migrate on startup
if CFRuntime::CloudApp.running_in_cloud?
migrations = Rails.root.join('db','migrate')
if migrations.directory?
ActiveRecord::Migrator.migrate(migrations)
end
end
这些基本上都是官方文档中有的,算不上坑,只是我找这些文档时花了点时间,所以还是记在这里。
然后我就开始部署了:
$ vmc push --runtime ruby19
Would you like to deploy from the current directory? [Yn]: y
Application Name: xxx
Detected a Rails Application, is this correct? [Yn]: y
Application Deployed URL [xxx.scfapp.com]:
Memory reservation (128M, 256M, 512M, 1G, 2G) [256M]:
How many instances? [1]:
Create services to bind to 'xxx'? [yN]: y
1: mongodb
2: mysql
3: rabbitmq
4: redis
What kind of service?: 2
Specify the name of the service [mysql-ef6f0]: mysql-xxx
Create another? [yN]: N
Would you like to save this configuration? [yN]: y
Manifest written to manifest.yml.
Creating Application: OK
Creating Service [mysql-xxx]: OK
Binding Service [mysql-xxx]: OK
Uploading Application:
Checking for available resources: OK
Processing resources: OK
Packing application: OK
Uploading (24M): OK
Push Status: OK
Staging Application 'xxx': ...............................Error (JSON 504): <html>
<head><title>504 Gatewa...
看着一个个绿色的 OK,心想不对啊,TMD 顺利得一塌糊涂的,怎么也应该出点错啥?
然后马上就如愿以偿了,来了个红色的 504 错误,啥玩意儿?
答案:内存整小了,把默认的 256M 改成 512M 就过了。
end.