Rails + Cloud SQL のチュートリアルやってみた
準備
Google Developers Console
プロジェクト作成する
お支払い方法を設定する
Google Cloud SDK をインストール
インストール
$ ./google-cloud-sdk/install.sh
- Initialize the SDK
$ ./google-cloud-sdk/bin/gcloud init
To continue, you must log in. Would you like to log in (Y/n)? Y
Pick cloud project to use:
[1] [demo-bookshelf]
Please enter your numeric choice: 1
利用するAPIの有効
Google Cloud Datastore API を有効にする
service accountの作成
- 「IAMと管理」 => 「サービスアカウント」で鍵を作成する
Cloud SQL
インスタンス作成
IPv4 addressをリクエスト
- 「アクセス制御」 => 「IPアドレス」
承認済みネットワークに追加する
- 「アクセス制御」 => 「承認」
ユーザー追加
- 「アクセス制御」 => 「ユーザー」
RailsのCRUDアプリ
ダウンロード
$ ghq get https://github.com/GoogleCloudPlatform/getting-started-ruby.git
$ cd 2-cloud-sql
$ bundle install
database
config
$ vi config/database.yml
mysql_settings: &mysql_settings
adapter: mysql2
encoding: utf8
pool: 5
username: rubyapp
password: password
host: 173.194.251.242
database: bookshelf
migrate
$ bundle exec rake db:create
$ bundle exec rake db:migrate
データベースが作成される
ローカル起動
$ bundle exec rails s
$ http://localhost:3000
デプロイ
$ gcloud preview app deploy
You are about to deploy the following services:
- glossy-champion-131810/default (from [/Users/takahiro/.ghq/github.com/GoogleCloudPlatform/getting-started-ruby/2-cloud-sql/app.yaml])
Deployed URL: [https://glossy-champion-131810.appspot.com]
Do you want to continue (Y/n)? Y
Beginning deployment...
If this is your first deployment, this may take a while...done.
Verifying that Managed VMs are enabled and ready.
Building and pushing image for service [default]
Started cloud build [8e7694ed-7dea-4d52-bac2-9d68065af32a].
To see logs in the Cloud Console: https://console.developers.google.com/logs?project=glossy-champion-131810&service=cloudbuild.googleapis.com&key1=8e7694ed-7dea-4d52-bac2-9d68065af32a&logName=projects/glossy-champion-131810/logs/cloudbuild
------------------------------------------------------------------------------------------- REMOTE BUILD OUTPUT -------------------------------------------------------------------------------------------
starting build "8e7694ed-7dea-4d52-bac2-9d68065af32a"
FETCHSOURCE
Fetching storage object: gs://staging.glossy-champion-131810.appspot.com/us.gcr.io/glossy-champion-131810/appengine/default.20160521t203824:latest#1463830791735000
Copying gs://staging.glossy-champion-131810.appspot.com/us.gcr.io/glossy-champion-131810/appengine/default.20160521t203824:latest#1463830791735000...
Downloading file:///tmp/source-archive.tgz: 203.89 KiB/203.89 KiB
FETCHBUILDER
BUILD
+ docker version
Client:
Version: 1.9.1
API version: 1.21
Go version: go1.4.3
Git commit: a34a1d5
Built: Fri Nov 20 17:56:04 UTC 2015
OS/Arch: linux/amd64
Server:
Version: 1.9.1
API version: 1.21
Go version: go1.4.2
Git commit: a34a1d5
Built: Fri Nov 20 13:12:04 UTC 2015
OS/Arch: linux/amd64
# This Dockerfile for a Ruby application was generated by gcloud.
# The base Dockerfile installs:
# * A number of packages needed by the Ruby runtime and by gems
# commonly used in Ruby web apps (such as libsqlite3)
# * A recent version of NodeJS
# * A recent version of the standard Ruby runtime to use by default
# * The bundler gem
FROM gcr.io/google_appengine/ruby
# Install 2.2.3 if not already preinstalled by the base image
RUN cd /rbenv/plugins/ruby-build && \
git pull && \
rbenv install -s 2.2.3 && \
rbenv global 2.2.3 && \
gem install -q --no-rdoc --no-ri bundler --version 1.11.2
ENV RBENV_VERSION 2.2.3
# Copy the application files.
COPY . /app/
# Install required gems if Gemfile.lock is present.
RUN if test -f Gemfile.lock; then \
bundle install --deployment && rbenv rehash; \
fi
# Temporary. Will be moved to base image later.
ENV RACK_ENV=production \
RAILS_ENV=production \
RAILS_SERVE_STATIC_FILES=true
# Run asset pipeline if we're in a Rails app.
RUN if test -d app/assets -a -f config/application.rb; then \
bundle exec rake assets:precompile; \
fi
# BUG: Reset entrypoint to override base image.
ENTRYPOINT []
# Start application on port $PORT.
CMD bundle exec rackup -p 8080 -E production config.ru
+ cat Dockerfile
+ docker build --tag us.gcr.io/glossy-champion-131810/appengine/default.20160521t203824:latest /workspace
Sending build context to Docker daemon 548.9 kB
Step 1 : FROM gcr.io/google_appengine/ruby
---> 3d07cecaa948
Step 2 : RUN cd /rbenv/plugins/ruby-build && git pull && rbenv install -s 2.2.3 && rbenv global 2.2.3 && gem install -q --no-rdoc --no-ri bundler --version 1.11.2
---> Running in 25c119c65752
From https://github.com/sstephenson/ruby-build
2bd59bd..3dbbf90 master -> origin/master
Updating 2bd59bd..3dbbf90
Fast-forward
share/ruby-build/1.8.5-p113 | 3 +++
share/ruby-build/1.8.5-p114 | 3 +++
share/ruby-build/1.8.5-p115 | 3 +++
share/ruby-build/1.8.5-p231 | 3 +++
share/ruby-build/1.8.5-p52 | 3 +++
share/ruby-build/1.8.6 | 3 +++
share/ruby-build/1.8.6-p110 | 3 +++
share/ruby-build/1.8.6-p111 | 3 +++
share/ruby-build/1.8.6-p114 | 3 +++
share/ruby-build/1.8.6-p230 | 3 +++
share/ruby-build/1.8.6-p286 | 3 +++
share/ruby-build/1.8.6-p287 | 3 +++
share/ruby-build/1.8.6-p36 | 3 +++
share/ruby-build/1.8.6-p368 | 3 +++
share/ruby-build/1.8.6-p369 | 3 +++
share/ruby-build/1.8.6-p388 | 3 +++
share/ruby-build/1.8.6-p398 | 3 +++
share/ruby-build/1.8.6-p399 | 3 +++
share/ruby-build/1.8.7 | 3 +++
share/ruby-build/1.8.7-p160 | 3 +++
share/ruby-build/1.8.7-p17 | 3 +++
share/ruby-build/1.8.7-p173 | 3 +++
share/ruby-build/1.8.7-p174 | 3 +++
share/ruby-build/1.8.7-p22 | 3 +++
share/ruby-build/1.8.7-p248 | 3 +++
share/ruby-build/1.8.7-p299 | 3 +++
share/ruby-build/1.8.7-p301 | 3 +++
share/ruby-build/1.8.7-p330 | 3 +++
share/ruby-build/1.8.7-p373 | 3 +++
share/ruby-build/1.8.7-p71 | 3 +++
share/ruby-build/1.8.7-p72 | 3 +++
share/ruby-build/1.8.7-preview1 | 3 +++
share/ruby-build/1.8.7-preview2 | 3 +++
share/ruby-build/1.8.7-preview3 | 3 +++
share/ruby-build/1.8.7-preview4 | 3 +++
share/ruby-build/1.9.0-0 | 4 ++++
share/ruby-build/1.9.0-1 | 4 ++++
share/ruby-build/1.9.0-2 | 4 ++++
share/ruby-build/1.9.0-3 | 4 ++++
share/ruby-build/1.9.0-4 | 4 ++++
share/ruby-build/1.9.0-5 | 4 ++++
share/ruby-build/1.9.1-p0 | 4 ++++
share/ruby-build/1.9.1-p129 | 4 ++++
share/ruby-build/1.9.1-p243 | 4 ++++
share/ruby-build/1.9.1-p376 | 4 ++++
share/ruby-build/1.9.1-p429 | 4 ++++
share/ruby-build/1.9.1-p431 | 4 ++++
share/ruby-build/1.9.1-preview1 | 4 ++++
share/ruby-build/1.9.1-preview2 | 4 ++++
share/ruby-build/1.9.1-rc1 | 4 ++++
share/ruby-build/1.9.1-rc2 | 4 ++++
share/ruby-build/1.9.2-p136 | 4 ++++
share/ruby-build/1.9.2-preview1 | 4 ++++
share/ruby-build/1.9.2-preview3 | 4 ++++
share/ruby-build/1.9.2-rc1 | 4 ++++
share/ruby-build/1.9.2-rc2 | 4 ++++
share/ruby-build/1.9.3-p105 | 5 +++++
share/ruby-build/1.9.3-p426 | 3 +++
share/ruby-build/jruby-9.1.1.0 | 2 ++
share/ruby-build/rbx-3.30 | 3 +++
60 files changed, 202 insertions(+)
create mode 100644 share/ruby-build/1.8.5-p113
create mode 100644 share/ruby-build/1.8.5-p114
create mode 100644 share/ruby-build/1.8.5-p115
create mode 100644 share/ruby-build/1.8.5-p231
create mode 100644 share/ruby-build/1.8.5-p52
create mode 100644 share/ruby-build/1.8.6
create mode 100644 share/ruby-build/1.8.6-p110
create mode 100644 share/ruby-build/1.8.6-p111
create mode 100644 share/ruby-build/1.8.6-p114
create mode 100644 share/ruby-build/1.8.6-p230
create mode 100644 share/ruby-build/1.8.6-p286
create mode 100644 share/ruby-build/1.8.6-p287
create mode 100644 share/ruby-build/1.8.6-p36
create mode 100644 share/ruby-build/1.8.6-p368
create mode 100644 share/ruby-build/1.8.6-p369
create mode 100644 share/ruby-build/1.8.6-p388
create mode 100644 share/ruby-build/1.8.6-p398
create mode 100644 share/ruby-build/1.8.6-p399
create mode 100644 share/ruby-build/1.8.7
create mode 100644 share/ruby-build/1.8.7-p160
create mode 100644 share/ruby-build/1.8.7-p17
create mode 100644 share/ruby-build/1.8.7-p173
create mode 100644 share/ruby-build/1.8.7-p174
create mode 100644 share/ruby-build/1.8.7-p22
create mode 100644 share/ruby-build/1.8.7-p248
create mode 100644 share/ruby-build/1.8.7-p299
create mode 100644 share/ruby-build/1.8.7-p301
create mode 100644 share/ruby-build/1.8.7-p330
create mode 100644 share/ruby-build/1.8.7-p373
create mode 100644 share/ruby-build/1.8.7-p71
create mode 100644 share/ruby-build/1.8.7-p72
create mode 100644 share/ruby-build/1.8.7-preview1
create mode 100644 share/ruby-build/1.8.7-preview2
create mode 100644 share/ruby-build/1.8.7-preview3
create mode 100644 share/ruby-build/1.8.7-preview4
create mode 100644 share/ruby-build/1.9.0-0
create mode 100644 share/ruby-build/1.9.0-1
create mode 100644 share/ruby-build/1.9.0-2
create mode 100644 share/ruby-build/1.9.0-3
create mode 100644 share/ruby-build/1.9.0-4
create mode 100644 share/ruby-build/1.9.0-5
create mode 100644 share/ruby-build/1.9.1-p0
create mode 100644 share/ruby-build/1.9.1-p129
create mode 100644 share/ruby-build/1.9.1-p243
create mode 100644 share/ruby-build/1.9.1-p376
create mode 100644 share/ruby-build/1.9.1-p429
create mode 100644 share/ruby-build/1.9.1-p431
create mode 100644 share/ruby-build/1.9.1-preview1
create mode 100644 share/ruby-build/1.9.1-preview2
create mode 100644 share/ruby-build/1.9.1-rc1
create mode 100644 share/ruby-build/1.9.1-rc2
create mode 100644 share/ruby-build/1.9.2-p136
create mode 100644 share/ruby-build/1.9.2-preview1
create mode 100644 share/ruby-build/1.9.2-preview3
create mode 100644 share/ruby-build/1.9.2-rc1
create mode 100644 share/ruby-build/1.9.2-rc2
create mode 100644 share/ruby-build/1.9.3-p105
create mode 100644 share/ruby-build/1.9.3-p426
create mode 100644 share/ruby-build/jruby-9.1.1.0
create mode 100644 share/ruby-build/rbx-3.30
Downloading ruby-2.2.3.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.3.tar.bz2
Installing ruby-2.2.3...
Installed ruby-2.2.3 to /rbenv/versions/2.2.3
Successfully installed bundler-1.11.2
1 gem installed
---> f78d5ca2d7d7
Removing intermediate container 25c119c65752
Step 3 : ENV RBENV_VERSION 2.2.3
---> Running in 0b601904e482
---> ef76ec7b785b
Removing intermediate container 0b601904e482
Step 4 : COPY . /app/
---> e8ff4eacbbf2
Removing intermediate container a9e4e6e1bf5b
Step 5 : RUN if test -f Gemfile.lock; then bundle install --deployment && rbenv rehash; fi
---> Running in 0ad27781a277
Don't run Bundler as root. Bundler can ask for sudo if it is needed, and
installing your bundle as root will break this application for all non-root
users on this machine.
Warning: the running version of Bundler is older than the version that created the lockfile. We suggest you upgrade to the latest version of Bundler by running `gem install bundler`.
Fetching gem metadata from https://rubygems.org/...........
Fetching version metadata from https://rubygems.org/...
Fetching dependency metadata from https://rubygems.org/..
Installing rake 11.1.2
Installing i18n 0.7.0
Installing json 1.8.3 with native extensions
Installing minitest 5.8.4
Installing thread_safe 0.3.5
Installing builder 3.2.2
Installing erubis 2.7.0
Installing mini_portile2 2.0.0
Installing rack 1.6.4
Installing mime-types-data 3.2016.0221
Installing arel 6.0.3
Installing addressable 2.4.0
Installing cliver 0.3.2
Installing concurrent-ruby 1.0.2
Installing diff-lcs 1.2.5
Installing thor 0.19.1
Installing multi_json 1.12.0
Installing mysql2 0.3.20 with native extensions
Installing websocket-extensions 0.1.2
Using bundler 1.11.2
Installing rspec-support 3.4.1
Installing sqlite3 1.3.11 with native extensions
Installing tzinfo 1.2.2
Installing nokogiri 1.6.7.2 with native extensions
Installing rack-test 0.6.3
Installing mime-types 3.0
Installing sprockets 3.6.0
Installing websocket-driver 0.6.3 with native extensions
Installing rspec-core 3.4.4
Installing rspec-expectations 3.4.0
Installing rspec-mocks 3.4.1
Installing activesupport 4.2.6
Installing tzinfo-data 1.2016.4
Installing loofah 2.0.3
Installing xpath 2.0.0
Installing mail 2.6.4
Installing rails-deprecated_sanitizer 1.0.3
Installing globalid 0.3.6
Installing activemodel 4.2.6
Installing rails-html-sanitizer 1.0.3
Installing capybara 2.7.1
Installing rails-dom-testing 1.0.7
Installing activejob 4.2.6
Installing activerecord 4.2.6
Installing poltergeist 1.9.0
Installing actionview 4.2.6
Installing actionpack 4.2.6
Installing actionmailer 4.2.6
Installing railties 4.2.6
Installing sprockets-rails 3.0.4
Installing jquery-rails 4.1.1
Installing rspec-rails 3.4.2
Installing rails 4.2.6
Bundle complete! 9 Gemfile dependencies, 53 gems now installed.
Bundled gems are installed into ./vendor/bundle.
---> 6e3f7bd87f9a
Removing intermediate container 0ad27781a277
Step 6 : ENV RACK_ENV production RAILS_ENV production RAILS_SERVE_STATIC_FILES true
---> Running in b0679bc93d64
---> 0d9a83bc7a3b
Removing intermediate container b0679bc93d64
Step 7 : RUN if test -d app/assets -a -f config/application.rb; then bundle exec rake assets:precompile; fi
---> Running in 745b52a1128a
---> 54e5b440ac49
Removing intermediate container 745b52a1128a
Step 8 : ENTRYPOINT
---> Running in 799b7e51b53c
---> 902f440effe2
Removing intermediate container 799b7e51b53c
Step 9 : CMD bundle exec rackup -p 8080 -E production config.ru
---> Running in e91280844a17
---> 564709cc773f
Removing intermediate container e91280844a17
Successfully built 564709cc773f
PUSH
The push refers to a repository [us.gcr.io/glossy-champion-131810/appengine/default.20160521t203824] (len: 1)
564709cc773f: Preparing
564709cc773f: Pushing
564709cc773f: Pushed
902f440effe2: Preparing
902f440effe2: Pushing
902f440effe2: Pushed
54e5b440ac49: Preparing
54e5b440ac49: Pushing
54e5b440ac49: Pushed
0d9a83bc7a3b: Preparing
0d9a83bc7a3b: Pushing
0d9a83bc7a3b: Pushed
6e3f7bd87f9a: Preparing
6e3f7bd87f9a: Pushing
6e3f7bd87f9a: Pushed
e8ff4eacbbf2: Preparing
e8ff4eacbbf2: Pushing
e8ff4eacbbf2: Pushed
ef76ec7b785b: Preparing
ef76ec7b785b: Pushing
ef76ec7b785b: Pushed
f78d5ca2d7d7: Preparing
f78d5ca2d7d7: Pushing
f78d5ca2d7d7: Pushed
58a8561a458e: Image already exists
ef866b7acceb: Image already exists
b5af07c62198: Image already exists
b3388c4bd40b: Image already exists
437045486895: Image already exists
58d9f8b0627d: Image already exists
f8f0a08927ad: Image already exists
502312a1defd: Image already exists
f28dbda953c1: Image already exists
096d9403d234: Image already exists
latest: digest: sha256:4af2c910ff637c71943ff243192c4fcc9854e6615c027a418ec3337ec20143eb size: 75409
DONE
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Updating service [default]...done.
Deployed service [default] to [https://glossy-champion-131810.appspot.com]
- 動いた!