GAE for Ruby チュートリアルやってみた

[RubyがGoogle App Engineで利用可能](https://cloudplatform.googleblog.com/2016/05/Ruby-on-Google-App-Engine-goes-betaruntime.html)になったので、チュートリアルやってみた

#　準備

## Google Developers Console

+ [Google Developers Console](https://console.cloud.google.com/)

## プロジェクト作成する

![1.png](https://qiita-image-store.s3.amazonaws.com/0/7086/f163dfa3-5726-6cea-3be9-7ebf1cb09b53.png)

## お支払い方法を設定する

![3.png](https://qiita-image-store.s3.amazonaws.com/0/7086/d78a7093-a834-cccc-e036-d4bb8d878366.png)

## Google Cloud SDK をインストール

+ [SDKダウンロード](https://cloud.google.com/sdk/docs/quickstart-mac-os-x)

+ インストール

```shell-session
$ ./google-cloud-sdk/install.sh
```

+ Initialize the SDK

```shell-session
$ ./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:
	[3] [gae-ruby-hello-world]
Please enter your numeric choice:  3
```

+ ヘルプ

```shell-session
$ gcloud -h
```
```shell-session
Usage: gcloud [optional flags] <group | command>
  group may be           auth | components | compute | config | container |
                         dataproc | deployment-manager | dns | preview |
                         projects | source | sql | topic
  command may be         docker | feedback | help | info | init | version

The *gcloud* CLI manages authentication, local configuration, developer
workflow, and interactions with the Google Cloud Platform APIs.

commonly used flags:
  --account ACCOUNT      Google Cloud Platform user account to use for
                         invocation.
  --configuration CONFIGURATION
                         The configuration to use for this command invocation.
  --format FORMAT        The format for printing command output resources.
  --help                 Display detailed help.
  --project PROJECT_ID   Google Cloud Platform project ID to use for this
                         invocation.
  --quiet, -q            Disable all interactive prompts.
  --verbosity VERBOSITY  Override the default verbosity for this command.  This
                         must be a standard logging verbosity level: [debug,
                         info, warning, error, critical, none] (Default:
                         [warning]).
  -h                     Print a summary help and exit.
  -v, --version          Print version information and exit. This flag is only
                         available at the global level.

other flags:
  Run `gcloud --help`
  for the full list of available flags for this command.

command groups:
  auth                   Manage oauth2 credentials for the Google Cloud SDK.
  components             List, install, update, or remove Google Cloud SDK
                         components.
  compute                Read and manipulate Google Compute Engine resources.
  config                 View and edit Cloud SDK properties.
  container              Deploy and manage clusters of machines for running
                         containers.
  dataproc               Create and manage Google Cloud Dataproc clusters and
                         jobs.
  deployment-manager     Manage deployments of cloud resources.
  dns                    Manage your Cloud DNS managed-zones and record-sets.
  preview                Manage Preview CLI command groups.
  projects               Manage your projects.
  source                 Cloud git repository commands.
  sql                    Manage Cloud SQL databases.
  topic                  gcloud supplementary help.

commands:
  docker                 Provides the docker CLI access to the Google Container
                         Registry.
  feedback               Provide feedback to the Google Cloud SDK team.
  help                   Prints detailed help messages for the specified
                         commands.
  info                   Display information about the current gcloud
                         environment.
  init                   Initialize or reinitialize gcloud.
  version                Print version information for Cloud SDK components.


For more detailed information on this command and its flags, run:
  gcloud --help
```



# Hello World アプリ

## ダウンロード

```shell-session
$ ghq get https://github.com/GoogleCloudPlatform/ruby-docs-samples
$ cd ruby-docs-samples/appengine/hello_world
```

## ローカル起動

```shell-session
$ bundle install
$ bundle exec ruby app.rb -p 8080
$ http://localhost:8080
```

# デプロイ

```shell-session
$ gcloud preview app deploy
```
```shell-session
WARNING: The `gcloud preview app` surface is rapidly improving. Look out for
changing flags and new commands before the transition out of the `preview`
component. These changes will be documented in the Cloud SDK release notes
<https://goo.gl/X8apDJ> and via deprecation notices for changing commands.

If you would like to avoid changing behavior, please pin to a fixed version of
the Google Cloud SDK as described under the "Alternative Methods" section of the
Cloud SDK web site: <https://cloud.google.com/sdk/#alternative>.

You are about to deploy the following services:
 - gae-ruby-hello-world/default (from [/Users/takahiro/.ghq/github.com/GoogleCloudPlatform/ruby-docs-samples/appengine/hello_world/app.yaml])
     Deployed URL: [https://gae-ruby-hello-world.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 [99999999-8303-4766-85ea-f673d4aaef0d].
To see logs in the Cloud Console: https://console.developers.google.com/logs?project=gae-ruby-hello-world&service=cloudbuild.googleapis.com&key1=99999999-8303-4766-85ea-f673d4aaef0d&logName=projects/gae-ruby-hello-world/logs/cloudbuild
------------------------------------------------------------------------------------------- REMOTE BUILD OUTPUT -------------------------------------------------------------------------------------------
starting build "99999999-8303-4766-85ea-f673d4aaef0d"

FETCHSOURCE
Fetching storage object: gs://staging.gae-ruby-hello-world.appspot.com/us.gcr.io/gae-ruby-hello-world/appengine/default.20160515t164733:latest#1463298550804000
Copying gs://staging.gae-ruby-hello-world.appspot.com/us.gcr.io/gae-ruby-hello-world/appengine/default.20160515t164733:latest#1463298550804000...
Downloading file:///tmp/source-archive.tgz:                      2.28 KiB/2.28 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
+ cat Dockerfile
# 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.3.0 if not already preinstalled by the base image
RUN cd /rbenv/plugins/ruby-build && \
    git pull && \
    rbenv install -s 2.3.0 && \
    rbenv global 2.3.0 && \
    gem install -q --no-rdoc --no-ri bundler --version 1.11.2
ENV RBENV_VERSION 2.3.0

# 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 ruby app.rb -p $PORT
+ docker build --tag us.gcr.io/gae-ruby-hello-world/appengine/default.20160515t164733:latest /workspace
Sending build context to Docker daemon 16.38 kB
Step 1 : FROM gcr.io/google_appengine/ruby
 ---> 3d07cecaa948
Step 2 : RUN cd /rbenv/plugins/ruby-build &&     git pull &&     rbenv install -s 2.3.0 &&     rbenv global 2.3.0 &&     gem install -q --no-rdoc --no-ri bundler --version 1.11.2
 ---> Running in 8d1e9140e644
From https://github.com/sstephenson/ruby-build
   2bd59bd..f03f7f8  master     -> origin/master
Updating 2bd59bd..f03f7f8
Fast-forward
 share/ruby-build/rbx-3.30 | 3 +++
 1 file changed, 3 insertions(+)
 create mode 100644 share/ruby-build/rbx-3.30
Successfully installed bundler-1.11.2
1 gem installed
 ---> 00eeedd7bd1f
Removing intermediate container 8d1e9140e644
Step 3 : ENV RBENV_VERSION 2.3.0
 ---> Running in 65bfb649dd5a
 ---> d5e81e06f4e2
Removing intermediate container 65bfb649dd5a
Step 4 : COPY . /app/
 ---> bfda7fe62fb9
Removing intermediate container a37794a58e58
Step 5 : RUN if test -f Gemfile.lock; then     bundle install --deployment && rbenv rehash;     fi
 ---> Running in 1f06fb69b4e0
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.
Fetching gem metadata from https://rubygems.org/.........
Fetching version metadata from https://rubygems.org/..
Installing rake 10.4.2
Installing ast 2.1.0
Installing diff-lcs 1.2.5
Installing powerpack 0.1.1
Installing rack 1.6.4
Installing rainbow 2.0.0
Installing rspec-support 3.4.1
Installing ruby-progressbar 1.7.5
Installing tins 1.6.0
Installing tilt 2.0.2
Using bundler 1.11.2
Installing parser 2.2.3.0
Installing rack-protection 1.5.3
Installing rspec-core 3.4.1
Installing rspec-expectations 3.4.0
Installing rspec-mocks 3.4.0
Installing astrolabe 1.3.1
Installing sinatra 1.4.7
Installing rspec 3.4.0
Installing rubocop 0.35.1
Bundle complete! 4 Gemfile dependencies, 20 gems now installed.
Bundled gems are installed into ./vendor/bundle.
 ---> 3c74294903f2
Removing intermediate container 1f06fb69b4e0
Step 6 : ENV RACK_ENV production RAILS_ENV production RAILS_SERVE_STATIC_FILES true
 ---> Running in 7b607b631515
 ---> 61ac9e67fa23
Removing intermediate container 7b607b631515
Step 7 : RUN if test -d app/assets -a -f config/application.rb; then     bundle exec rake assets:precompile;     fi
 ---> Running in 52b6c74169f1
 ---> 4b9109d9a860
Removing intermediate container 52b6c74169f1
Step 8 : ENTRYPOINT
 ---> Running in b8a2b35e07be
 ---> 3eb8a86b6283
Removing intermediate container b8a2b35e07be
Step 9 : CMD bundle exec ruby app.rb -p $PORT
 ---> Running in 4ed7bb9961fa
 ---> b2c9403ed037
Removing intermediate container 4ed7bb9961fa
Successfully built b2c9403ed037
PUSH
The push refers to a repository [us.gcr.io/gae-ruby-hello-world/appengine/default.20160515t164733] (len: 1)
b2c9403ed037: Preparing
b2c9403ed037: Pushing
b2c9403ed037: Pushed
3eb8a86b6283: Preparing
3eb8a86b6283: Pushing
3eb8a86b6283: Pushed
4b9109d9a860: Preparing
4b9109d9a860: Pushing
4b9109d9a860: Pushed
61ac9e67fa23: Preparing
61ac9e67fa23: Pushing
61ac9e67fa23: Pushed
3c74294903f2: Preparing
3c74294903f2: Pushing
3c74294903f2: Pushed
bfda7fe62fb9: Preparing
bfda7fe62fb9: Pushing
bfda7fe62fb9: Pushed
d5e81e06f4e2: Preparing
d5e81e06f4e2: Pushing
d5e81e06f4e2: Pushed
00eeedd7bd1f: Preparing
00eeedd7bd1f: Pushing
00eeedd7bd1f: 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:64dc345056b18ef4618d891e8fc3cf318f8bf5733ee160f9c74764fb842917d1 size: 75353
DONE
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Updating service [default]...done.
Deployed service [default] to [https://gae-ruby-hello-world.appspot.com]
```

+ https://your-project-id.appspot.com