本帖最后由 xioaxu790 于 2014-6-19 14:07 编辑
问题导读:
1、如何编写自己的APP?
2、如何运行自己的APP?
Cloud Foundry(简称CF)是一个大型可扩展性的APP引擎平台。CF可以帮助开发者快速的运行并延展新创建的APP,缩短与用户的反馈周期,同时使网络资源管理简单化自动化。
对于一名软件开发员,他只需要知道几个基本的命令来与CF平台互动。客户端的命令行工具cf(小写)可以以'gem install cf'来安装ruby版本,或者参照说明安装Go版本:http://docs.gopivotal.com/pivota ... install-go-cli.html
我写了一个简单的ruby sinatra app来介绍几个最常用的命令。大家可以仿效Appendix里的命令行输入/输出来运行自己的APP。
网页的基本功能就是记录用户点击了页面多少次。大家现在便可以登录clicks.cfapps.io来尝试我的APP。
大家可以来https://console.run.pivotal.io/register注册一个免费测试账户。
(以下运用了Go cf)
A. 连接并登入cloudfoundry
> cf api [api.mycloudfoundry.xip.io]
设置CF云端的服务接口
> cf login
验证并登陆CF
B. 上传并运行APP
> cf push APP
在程序根目录下运行这个命令后,cf就会把当前目录包括源代码的所有文件,打包并上传到CF上面,之后CF就会自动分析出APP的语言,根据源代码下载所需的依赖,编译程序并将其重新打包成一个“露珠”(dropplet,可以反复运行并延展的独立程序包)。这一切都是后台自动完成的,无需任何设置。这时候在你设置网段的任何人便可以去浏览我们的APP了。
开发员可以定义APP的名称(name),运行实例的数量(number of instances),内存(Memory Limit),子域名(subdomain),域名(domain),还有可选择绑定的服务(services,会在另一篇文章深入介绍),等等。
C. 监控和调试APP
> cf apps
显示已经上传的APP以及它们的运行状态。
> cf logs APP --recent
显示我们上传APP的log,包括env.log, staging_task.log, stderr.log, 和stdout.log。我们的log一般会在stderr或stdout找到,另外两个为系统log。在下面的示例,stderr.log反应了在代码里留下的log,并显示出session函数的值。
如果去掉'--recent',cf则会对log进行'tail -f'的功能。
> cf files APP
显示我们上传APP的根目录文件夹结构,一般会反应我们在本地上传的目录结构。我们还可以运用这个命令查看已上传文件的内容。
Appendix
--------------------------------Command Line Outputs--------------------------------
~/workspace/clicks $ cat *
- ./Gemfile
- source "http://rubygems.org"
- gem 'sinatra
-
- ./config.ru
- require "./main"
- run Sinatra::Application
-
- ./main.rb
- require 'sinatra'
- use Rack::Logger
-
- enable :sessions
-
- get '/' do
- session['counter'] ||= 0
- session['counter'] += 1
- logger.info("hello world, the session variable is #{session.inspect}")
-
- "<h1>Welcome to my site!</h1><br / >
- You've hit this page #{session['counter']} times!"
- end
-
- helpers do
- def logger
- request.logger
- end
- end
复制代码
~/workspace/clicks $ bundle
- Using rack (1.5.2)
- Using rack-protection (1.5.2)
- Using tilt (1.4.1)
- Using sinatra (1.4.4)
- Using bundler (1.5.3)
- Updating files in vendor/cache
- Your bundle is complete!
- Use `bundle show [gemname]` to see where a bundled gem is installed.
复制代码
~/workspace/clicks $ cf api api.run.pivotal.io
- Setting api endpoint to api.run.pivotal.io...
- OK
-
- API endpoint: [url]https://api.run.pivotal.io[/url] (API version: 2.2.0)
- Not logged in. Use 'cf login' to log in.
复制代码
~/workspace/clicks $ cf login
- API endpoint: [url]https://api.run.pivotal.io[/url]
-
- Username> [email]jda@gopivotal.com[/email]
-
- Password>
- Authenticating...
- OK
-
- Targeted org my-pivotal-org
-
- Targeted space development
-
- API endpoint: [url]https://api.run.pivotal.io[/url] (API version: 2.2.0)
- User: [email]jda@gopivotal.com[/email]
- Org: my-pivotal-org
- Space: development
复制代码
~/workspace/clicks $ cf push clicks
- Creating app clicks in org my-pivotal-org / space development as [email]jda@gopivotal.com[/email]...
- OK
-
- Using route clicks.cfapps.io
- Binding clicks.cfapps.io to clicks...
- OK
-
- Uploading clicks...
- Uploading from: /User/daj/workspace/clicks
- 60.7K, 8 files
- OK
-
- Starting app clicks in org my-pivotal-org / space development as [email]jda@gopivotal.com[/email]...
- -----> Downloaded app package (584K)
- OK
- -----> Using Ruby version: ruby-1.9.3
- -----> Installing dependencies using Bundler version 1.3.2
- Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin --deployment
- Installing rack (1.5.2)
- Installing rack-protection (1.5.2)
- Installing tilt (1.4.1)
- Installing sinatra (1.4.4)
- Using bundler (1.3.2)
- Updating files in vendor/cache
- Your bundle is complete! It was installed into ./vendor/bundle
- Cleaning up the bundler cache.
- -----> WARNINGS:
- You have not declared a Ruby version in your Gemfile.
- To set your Ruby version add this line to your Gemfile:"
- ruby '1.9.3'"
- # See [url]https://devcenter.heroku.com/articles/ruby-versions[/url] for more information."
- -----> Uploading droplet (23M)
-
- 1 of 1 instances running
-
- App started
-
- Showing health and status for app clicks in org my-pivotal-org / space development as [email]jda@gopivotal.com[/email]...
- OK
-
- requested state: started
- instances: 1/1
- usage: 1G x 1 instances
- urls: clicks.cfapps.io
-
- state since cpu memory disk
- #0 running 2014-04-08 12:48:08 PM 0.0% 69.7M of 1G 52.7M of 1G
复制代码
~/workspace/clicks $ cf apps
- Getting apps in org my-pivotal-org / space development as [email]jda@gopivotal.com[/email]...
- OK
-
- name requested state instances memory disk urls
- clicks started 1/1 1G 1G clicks.cfapps.io
复制代码
~/workspace/clicks $ cf logs clicks --recent
- Connected, dumping recent logs for app clicks in org my-pivotal-org / space development as [email]jda@gopivotal.com[/email]...
-
- 2014-04-08T12:47:03.37+0800 [API] OUT Created app with guid a011b680-af31-4a91-b12d-a72a3c544479
- 2014-04-08T12:47:22.40+0800 [API] OUT Updated app with guid a011b680-af31-4a91-b12d-a72a3c544479 ({"route"=>"9fd1610e-460a-4241-9317-713633d4469c"})
- 2014-04-08T12:47:49.01+0800 [DEA] OUT Got staging request for app with id a011b680-af31-4a91-b12d-a72a3c544479
- 2014-04-08T12:47:50.12+0800 [API] OUT Updated app with guid a011b680-af31-4a91-b12d-a72a3c544479 ({"state"=>"STARTED"})
- 2014-04-08T12:47:50.28+0800 [STG] OUT -----> Downloaded app package (584K)
- 2014-04-08T12:47:51.95+0800 [STG] OUT -----> Using Ruby version: ruby-1.9.3
- 2014-04-08T12:47:52.20+0800 [STG] OUT -----> Installing dependencies using Bundler version 1.3.2
- 2014-04-08T12:47:52.38+0800 [STG] OUT Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin --deployment
- 2014-04-08T12:47:52.84+0800 [STG] OUT Installing rack (1.5.2)
- 2014-04-08T12:47:52.86+0800 [STG] OUT Installing rack-protection (1.5.2)
- 2014-04-08T12:47:52.90+0800 [STG] OUT Installing tilt (1.4.1)
- 2014-04-08T12:47:52.96+0800 [STG] OUT Installing sinatra (1.4.4)
- 2014-04-08T12:47:52.96+0800 [STG] OUT Using bundler (1.3.2)
- 2014-04-08T12:47:52.97+0800 [STG] OUT Updating files in vendor/cache
- 2014-04-08T12:47:53.06+0800 [STG] OUT Your bundle is complete! It was installed into ./vendor/bundle
- 2014-04-08T12:47:53.08+0800 [STG] OUT Cleaning up the bundler cache.
- 2014-04-08T12:47:53.87+0800 [STG] OUT -----> WARNINGS:
- 2014-04-08T12:51:39.29+0800 [RTR] OUT clicks.cfapps.io - [08/04/2014:04:51:39 +0000] "GET / HTTP/1.1" 200 67 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.149 Safari/537.36 AppEngine-Google; (+[url]http://code.google.com/appengine;[/url] appid: s~gateway-sanmateo3)" 10.10.2.122:14151 vcap_request_id:183229a95ec62ccd1d27fc538c40d4a1 response_time:0.027762402 app_id:a011b680-af31-4a91-b12d-a72a3c544479
- 2014-04-08T12:47:53.87+0800 [STG] OUT You have not declared a Ruby version in your Gemfile.
- 2014-04-08T12:47:53.87+0800 [STG] OUT To set your Ruby version add this line to your Gemfile:"
- 2014-04-08T12:47:53.87+0800 [STG] OUT ruby '1.9.3'"
- 2014-04-08T12:47:53.87+0800 [STG] OUT # See [url]https://devcenter.heroku.com/articles/ruby-versions[/url] for more information."
- 2014-04-08T12:47:58.25+0800 [STG] OUT -----> Uploading droplet (23M)
- 2014-04-08T12:48:05.54+0800 [DEA] OUT Starting app instance (index 0) with guid a011b680-af31-4a91-b12d-a72a3c544479
- 2014-04-08T12:48:07.87+0800 [App/0] ERR [2014-04-08 04:48:07] INFO WEBrick 1.3.1
- 2014-04-08T12:48:07.87+0800 [App/0] ERR [2014-04-08 04:48:07] INFO ruby 1.9.3 (2013-11-22) [x86_64-linux]
- 2014-04-08T12:48:07.87+0800 [App/0] ERR [2014-04-08 04:48:07] INFO WEBrick::HTTPServer#start: pid=31 port=63085
- <strong> 2014-04-08T12:51:39.27+0800 [App/0] ERR I, [2014-04-08T04:51:39.271861 #31] INFO -- : hello world, the session variable is {"session_id"=>"d77ce523a0b928f613c55e73b96cf9d728da3757576c2f7e08bbb4151ec4b246", "csrf"=>"964520f5aa2fe6a1b70fcaa4b276acf9", "tracking"=>{"HTTP_USER_AGENT"=>"1565a324ffc00535d703dcdc07bce753b1085adc", "HTTP_ACCEPT_LANGUAGE"=>"28e0451b50d874ecd59df027dd2a40d490bc65f4"}, "counter"=>1}</strong>
- 2014-04-08T12:51:39.27+0800 [App/0] ERR 8.35.200.32, 10.10.2.122 - - [08/Apr/2014 04:51:39] "GET / HTTP/1.1" 200 67 0.0038
复制代码
~/workspace/clicks $ cf files clicks
- Getting files for app clicks in org my-pivotal-org / space development as [email]jda@gopivotal.com[/email]...
- OK
-
- .bash_logout 220B
- .bashrc 3.0K
- .profile 675B
- app/ -
- logs/ -
- run.pid 3B
- staging_info.yml 87B
- tmp/ -
复制代码
~/workspace/clicks $ cf files clicks app/
- Getting files for app clicks in org my-pivotal-org / space development as jda@gopivotal.com...
- OK
-
- .bundle/ -
- .java-buildpack.log 14.7K
- .profile.d/ -
- .ruby-version 11B
- Gemfile 43B
- Gemfile.lock 260B
- bin/ -
- config.ru 42B
- main.rb 343B
- vendor/ -
复制代码
|