LoginSignup
5
4

More than 5 years have passed since last update.

GCPのAEでGoの環境設定のメモ

Last updated at Posted at 2017-01-08

GCPのAEでGoの環境を作ったときのメモ。
環境はmacOS

AEのアーキテクチャ

名前 役割
プロジェクト 一つのアプリケーションの環境単位。本番、開発、ステージングなど
サービス(モジュール) それぞれのプロジェクトの構成要因。APIやWeb、Bot、バッチなど
バージョン それぞれのサービスのリビジョン番号。A/Bテストや、サービスのロールバックなどに

gcloudコマンドのインスコ

gcloudはGCPへの主要なコマンドライン、インターフェースを提供するツール

$ curl https://sdk.cloud.google.com/ | bash
$ exec -l $SHELL # 環境変数のためshellを再起動

Googleアカウントの設定

gcloud initコマンドはglocud authだけではなく、
glocud config set projectコマンドなどもやってくれる

$ gcloud auth login 
$ gcloud auth list # 認証情報の確認
$ ls -al ~/.config/gcloud/ # 認証情報保存場所

プロジェクトの設定

プロジェクトはGCPのWebサイトからつくれる

$ gcloud projects list
PROJECT_ID    NAME   PROJECT_NUMBER
onyx-day-154915  hello  242498700225
$ gcloud config set project onyx-day-154915
$ gcloud config list 

AEのコンポーネントのインスコ

ただ、GCloudにAEのGoのコンポーネントは存在せず、
かつ、それはSDKとして独立しているので、
https://cloud.google.com/appengine/downloads
の方法でインスコする

$ gcloud components install gae-go # だめだった
You have specified individual components to update.  If you are trying
 to install new components, use:
  $ gcloud components install gae-go

Do you want to run install instead (y/N)?  y

WARNING: Component [gae-go] no longer exists.
WARNING: The standalone App Engine SDKs are no longer distributed through the Cloud SDK
(however, the appcfg and dev_appserver commands remain the official and
supported way of using App Engine from the command line).  If you want to
continue using these tools, they are available for download from the official
App Engine download page here:
    https://cloud.google.com/appengine/downloads

All components are up to date.

$ open https://cloud.google.com/appengine/downloads #からDL
$ unzip go_appengine_sdk_darwin_amd64-1.9.48.zip
$ ls -al go_appengine 
$ mv go_appengine ~
$ /usr/bin/env python -V # go_appengineはpython 2.7以上を使用するので確認
$ export PATH=$PATH:$HOME/go_appengine:$GOPATH/bin # bash_profileに追加すると捗る

テストソースコードの作成

$ mkdir hello
$ cd hello
$ touch hello.go # 下記の内容を書く
$ touch app.yaml # 下記の内容を書く
hello.go
package hello

import (
        "fmt"
        "net/http"
)

func init() {
        http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
            fmt.Fprint(w, "Hello, world!")
        })
}
app.yaml
application: onyx-day-154915
version: 1
runtime: go
api_version: go1

handlers:
- url: /.*
  script: _go_app

app.yamlのapplicationの値はローカル環境では任意だが、
リモート環境にアプリケーションをデプロイする場合は、
GCEのProject IDと紐付けなければならない

ローカル環境の起動テスト

goappはAEのSDKに含まれているもの

$ pwd
/Users/os10/Desktop/Apps/hello
$ goapp serve . # ローカルで起動している
$ open http://localhost:8000/ # 管理画面
$ open http://localhost:8080/ # 開発画面

リモート環境の起動テスト

$ goapp deploy . # リモートで起動したがうまく行かなかった
Error 404: --- begin server output ---
This application does not exist (project_id=u'onyx-day-154915'). To create an App Engine application in this project, run "gcloud beta app create" in your console.
--- end server output ---
$ open https://appengine.google.com/start # 先にWebサイトでプロジェクトにAEを作成しなければならない
$ goapp deploy . # 今度は成功
...
11:15 AM Completed update of app: onyx-day-154915, version: 1
$ open http://onyx-day-154915.appspot.com # デプロイ完了したのでリモート確認

References

https://cloud.google.com/appengine/docs/go/quickstart
https://cloud.google.com/sdk/gcloud/
http://www.task-notes.com/entry/20151019/1445223600
https://cloud.google.com/sdk/gcloud/reference/
https://cloud.google.com/appengine/docs/go/microservices-on-app-engine
http://www.apps-gcp.com/gae-go-gettingstart-01/

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4