Skip to content

Instantly share code, notes, and snippets.

@a-r-g-v
Last active October 5, 2018 06:18
Show Gist options
  • Select an option

  • Save a-r-g-v/cb745842b69118c48d770b7cd69b4cb9 to your computer and use it in GitHub Desktop.

Select an option

Save a-r-g-v/cb745842b69118c48d770b7cd69b4cb9 to your computer and use it in GitHub Desktop.

Revisions

  1. a-r-g-v revised this gist Dec 24, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion docs.md
    Original file line number Diff line number Diff line change
    @@ -74,7 +74,7 @@ packer.json は下記の通り.
    "source_image_family": "debian-8",
    "zone": "asia-northeast1-a",
    "ssh_username": "packer",
    "image_family": "openresty"
    "image_family": "openresty",
    "image_name": "openresty-{{uuid}}"
    }],
    "provisioners": [
  2. a-r-g-v revised this gist Feb 13, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion docs.md
    Original file line number Diff line number Diff line change
    @@ -61,7 +61,7 @@ cat key | base64 | tr -d "\n"

    packer.json は下記の通り.

    ```
    ```json
    {
    "variables" : {
    "project_id": "{{env `GCLOUD_PROJECT`}}",
  3. a-r-g-v revised this gist Feb 13, 2017. 1 changed file with 5 additions and 42 deletions.
    47 changes: 5 additions & 42 deletions docs.md
    Original file line number Diff line number Diff line change
    @@ -60,6 +60,7 @@ cat key | base64 | tr -d "\n"
    ```

    packer.json は下記の通り.

    ```
    {
    "variables" : {
    @@ -70,10 +71,10 @@ packer.json は下記の通り.
    "type": "googlecompute",
    "account_file": "{{user `account_file_path`}}",
    "project_id": "{{user `project_id`}}",
    "source_image": "debian-8-jessie-v20161129",
    "source_image_family": "debian-8",
    "zone": "asia-northeast1-a",
    "ssh_username": "packer",
    "image_family": "openresty",
    "image_family": "openresty"
    "image_name": "openresty-{{uuid}}"
    }],
    "provisioners": [
    @@ -88,50 +89,12 @@ packer.json は下記の通り.
    }
    ```



    サンプルリポジトリがあるので,詳しくはこっちを見てほしい.
    これは,OpenRestyのイメージを生成するやつ.

    https://github.com/a-r-g-v/packer-circleci-gce
    https://circleci.com/gh/a-r-g-v/packer-circleci-gce



    ところで,Packerのマニュアルを見ていると,BuilderのGCEにはsourceのimage_familyを指定できないっぽいことがわかる. (ref: https://www.packer.io/docs/builders/googlecompute.html)

    これでは,ソースイメージが更新された度に,packer.jsonを変更する必要があって,面倒.
    特に,ベースイメージをCIで定期的に自動生成し,ベースイメージの派生イメージとして具体的なアプリケーション用のイメージを生成するようなパターンを適用したい場合,最新版のベースイメージ名を解決する必要があるので,非常にしんどい.

    どうにかならないかなーと思ってPull requestsを漁っていると,こういうのがあった.
    https://github.com/mitchellh/packer/pull/4162

    2016/12/10時点ではリリースはされていないが,`source_image_family`がサポートされるので,packer.jsonは下記のように書き直せる.

    ```
    {
    "variables" : {
    "project_id": "{{env `GCLOUD_PROJECT`}}",
    "accout_file_path": "{{env `GOOGLE_APPLICATION_CREDENTIALS`}}"
    },
    "builders": [{
    "type": "googlecompute",
    "account_file": "{{user `account_file_path`}}",
    "project_id": "{{user `project_id`}}",
    "source_image_family": "debian-8",
    "zone": "asia-northeast1-a",
    "ssh_username": "packer",
    "image_family": "openresty",
    "image_name": "openresty-{{uuid}}"
    }],
    "provisioners": [
    {
    "type": "shell",
    "execute_command": "sudo {{.Path}}",
    "scripts": [
    "openresty.sh"
    ]
    }
    ]
    }
    ```

    べんり.
  4. a-r-g-v revised this gist Dec 10, 2016. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion docs.md
    Original file line number Diff line number Diff line change
    @@ -106,7 +106,6 @@ https://github.com/mitchellh/packer/pull/4162

    2016/12/10時点ではリリースはされていないが,`source_image_family`がサポートされるので,packer.jsonは下記のように書き直せる.

    packer.json は下記の通り.
    ```
    {
    "variables" : {
  5. a-r-g-v revised this gist Dec 10, 2016. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions docs.md
    Original file line number Diff line number Diff line change
    @@ -91,14 +91,14 @@ packer.json は下記の通り.
    サンプルリポジトリがあるので,詳しくはこっちを見てほしい.
    これは,OpenRestyのイメージを生成するやつ.

    https://github.com/a-r-g-v/packer-circleci-gce
    https://circleci.com/gh/a-r-g-v/packer-circleci-gce
    https://github.com/a-r-g-v/packer-circleci-gce
    https://circleci.com/gh/a-r-g-v/packer-circleci-gce



    ところで,Packerのマニュアルを見ていると,BuilderのGCEにはsourceのimage_familyを指定できないっぽいことがわかる. (ref: https://www.packer.io/docs/builders/googlecompute.html)

    これでは,ソースイメージが更新された度に,packer.jsonを変更する必要があって,面倒.
    これでは,ソースイメージが更新された度に,packer.jsonを変更する必要があって,面倒.
    特に,ベースイメージをCIで定期的に自動生成し,ベースイメージの派生イメージとして具体的なアプリケーション用のイメージを生成するようなパターンを適用したい場合,最新版のベースイメージ名を解決する必要があるので,非常にしんどい.

    どうにかならないかなーと思ってPull requestsを漁っていると,こういうのがあった.
  6. a-r-g-v revised this gist Dec 10, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion docs.md
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ http://www.adventar.org/calendars/1530



    Google Compute Engineには,AWSではAMIに相当するImageという概念がある.  
    Google Compute Engineには,AWSではAMIに相当するImageという概念がある.
    Imageには,Image Familyというものがあって,Imageのバージョン管理を行うことができる.
    具体的には,Familyはその中に存在する一番最新のImageへのポインタを持っている.DockerfileのFROM name:latestみたいなノリ.

  7. a-r-g-v revised this gist Dec 10, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion docs.md
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ Packer+Circle CIでGoogle Compute Engineのイメージを生成する
    http://www.adventar.org/calendars/1530

    8日目: [オフィスにいる人間をSlackに通知するやつ作った - 人権真骨頂](http://blog.upamune.com/entry/2016/12/08/235349)
    10日目: (@mic_psm)[http://www.adventar.org/users/8098]
    10日目: [@mic_psm](http://www.adventar.org/users/8098)



  8. a-r-g-v revised this gist Dec 10, 2016. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions docs.md
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,13 @@
    Packer+Circle CIでGoogle Compute Engineのイメージを生成する
    ===

    これは Aizu Advent Calendar 2016 の 9日目の記事(遅刻)です.
    http://www.adventar.org/calendars/1530
    これは Aizu Advent Calendar 2016 の 9日目の記事(遅刻)です.
    http://www.adventar.org/calendars/1530

    8日目: [オフィスにいる人間をSlackに通知するやつ作った - 人権真骨頂](http://blog.upamune.com/entry/2016/12/08/235349)
    10日目: (@mic_psm)[http://www.adventar.org/users/8098]


    8日目: [オフィスにいる人間をSlackに通知するやつ作った - 人権真骨頂](http://blog.upamune.com/entry/2016/12/08/235349)
    10日目: (@mic_psm)[http://www.adventar.org/users/8098]

    Google Compute Engineには,AWSではAMIに相当するImageという概念がある.  
    Imageには,Image Familyというものがあって,Imageのバージョン管理を行うことができる.
  9. a-r-g-v revised this gist Dec 10, 2016. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion docs.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,13 @@
    Packer+Circle CIでGoogle Compute Engineのイメージを生成する
    ===

    Google Compute Engineには,AWSではAMIに相当するImageという概念がある.
    これは Aizu Advent Calendar 2016 の 9日目の記事(遅刻)です.
    http://www.adventar.org/calendars/1530

    8日目: [オフィスにいる人間をSlackに通知するやつ作った - 人権真骨頂](http://blog.upamune.com/entry/2016/12/08/235349)
    10日目: (@mic_psm)[http://www.adventar.org/users/8098]

    Google Compute Engineには,AWSではAMIに相当するImageという概念がある.  
    Imageには,Image Familyというものがあって,Imageのバージョン管理を行うことができる.
    具体的には,Familyはその中に存在する一番最新のImageへのポインタを持っている.DockerfileのFROM name:latestみたいなノリ.

    @@ -81,13 +87,15 @@ packer.json は下記の通り.
    ```

    サンプルリポジトリがあるので,詳しくはこっちを見てほしい.
    これは,OpenRestyのイメージを生成するやつ.

    https://github.com/a-r-g-v/packer-circleci-gce
    https://circleci.com/gh/a-r-g-v/packer-circleci-gce



    ところで,Packerのマニュアルを見ていると,BuilderのGCEにはsourceのimage_familyを指定できないっぽいことがわかる. (ref: https://www.packer.io/docs/builders/googlecompute.html)

    これでは,ソースイメージが更新された度に,packer.jsonを変更する必要があって,面倒.
    特に,ベースイメージをCIで定期的に自動生成し,ベースイメージの派生イメージとして具体的なアプリケーション用のイメージを生成するようなパターンを適用したい場合,最新版のベースイメージ名を解決する必要があるので,非常にしんどい.

  10. a-r-g-v revised this gist Dec 10, 2016. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions docs.md
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    Packer+Circle CIでGoogle Compute Engineのイメージを生成する
    ===

    Google Compute Engineには,Imageという概念があって,AWSではAMIに相当する.
    Imageには,Image Familyというものがあって,Imageのバージョン管理を行うことができる.
    具体的には,Familyはその中に存在する一番最新のImageへのポインタを持っている.DockerfileのFROM name:latestみたいなノリ.
    Google Compute Engineには,AWSではAMIに相当するImageという概念がある.
    Imageには,Image Familyというものがあって,Imageのバージョン管理を行うことができる.
    具体的には,Familyはその中に存在する一番最新のImageへのポインタを持っている.DockerfileのFROM name:latestみたいなノリ.

    これを使うと,インスタンスを実行する時に,デフォルトは最新のイメージであってほしいんだけれど,過去のバージョンを指定して環境を構築するシーンも想定したい,みたいなニーズを簡単に満たすことができる.

  11. a-r-g-v created this gist Dec 10, 2016.
    128 changes: 128 additions & 0 deletions docs.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,128 @@
    Packer+Circle CIでGoogle Compute Engineのイメージを生成する
    ===

    Google Compute Engineには,Imageという概念があって,AWSではAMIに相当する.
    Imageには,Image Familyというものがあって,Imageのバージョン管理を行うことができる.
    具体的には,Familyはその中に存在する一番最新のImageへのポインタを持っている.DockerfileのFROM name:latestみたいなノリ.

    これを使うと,インスタンスを実行する時に,デフォルトは最新のイメージであってほしいんだけれど,過去のバージョンを指定して環境を構築するシーンも想定したい,みたいなニーズを簡単に満たすことができる.

    circle.yml は下記の通り.

    ```yml
    # circle.yml
    machine:
    environment:
    GCLOUD_PROJECT: "argvc-jp"
    CLOUDSDK_COMPUTE_ZONE: "asia-northeast1-a"
    CLOUDSDK_COMPUTE_REGION: "asia-northeast1"
    GOOGLE_APPLICATION_CREDENTIALS: "$HOME/client-secret.json"
    post:
    - echo $CLIENT_KEYS | base64 --decode > $GOOGLE_APPLICATION_CREDENTIALS

    dependencies:
    cache_directories:
    - "~/bin"
    pre:
    - mkdir -p ~/packer
    - wget https://releases.hashicorp.com/packer/0.12.0/packer_0.12.0_linux_amd64.zip
    - unzip packer_0.12.0_linux_amd64.zip
    - mv packer ~/bin/packer

    test:
    override:
    - ~/bin/packer validate ./packer.json

    deployment:
    packer:
    branch: master
    commands:
    - gcloud auth activate-service-account --key-file $GOOGLE_APPLICATION_CREDENTIALS
    - gcloud config set project $GCLOUD_PROJECT
    - ~/bin/packer build ./packer.json

    ```

    CircleCI用にGoogle Cloud Platformのサービスアカウントを取得する必要がある.

    JSONのサービスアカウントファイルがkeyだとした時,下記コマンドで生成される文字列をCLIENT_KEYSという名前の環境変数に登録した.

    ```
    cat key | base64 | tr -d "\n"
    ```

    packer.json は下記の通り.
    ```
    {
    "variables" : {
    "project_id": "{{env `GCLOUD_PROJECT`}}",
    "accout_file_path": "{{env `GOOGLE_APPLICATION_CREDENTIALS`}}"
    },
    "builders": [{
    "type": "googlecompute",
    "account_file": "{{user `account_file_path`}}",
    "project_id": "{{user `project_id`}}",
    "source_image": "debian-8-jessie-v20161129",
    "zone": "asia-northeast1-a",
    "ssh_username": "packer",
    "image_family": "openresty",
    "image_name": "openresty-{{uuid}}"
    }],
    "provisioners": [
    {
    "type": "shell",
    "execute_command": "sudo {{.Path}}",
    "scripts": [
    "openresty.sh"
    ]
    }
    ]
    }
    ```

    サンプルリポジトリがあるので,詳しくはこっちを見てほしい.

    https://github.com/a-r-g-v/packer-circleci-gce
    https://circleci.com/gh/a-r-g-v/packer-circleci-gce



    ところで,Packerのマニュアルを見ていると,BuilderのGCEにはsourceのimage_familyを指定できないっぽいことがわかる. (ref: https://www.packer.io/docs/builders/googlecompute.html)
    これでは,ソースイメージが更新された度に,packer.jsonを変更する必要があって,面倒.
    特に,ベースイメージをCIで定期的に自動生成し,ベースイメージの派生イメージとして具体的なアプリケーション用のイメージを生成するようなパターンを適用したい場合,最新版のベースイメージ名を解決する必要があるので,非常にしんどい.

    どうにかならないかなーと思ってPull requestsを漁っていると,こういうのがあった.
    https://github.com/mitchellh/packer/pull/4162

    2016/12/10時点ではリリースはされていないが,`source_image_family`がサポートされるので,packer.jsonは下記のように書き直せる.

    packer.json は下記の通り.
    ```
    {
    "variables" : {
    "project_id": "{{env `GCLOUD_PROJECT`}}",
    "accout_file_path": "{{env `GOOGLE_APPLICATION_CREDENTIALS`}}"
    },
    "builders": [{
    "type": "googlecompute",
    "account_file": "{{user `account_file_path`}}",
    "project_id": "{{user `project_id`}}",
    "source_image_family": "debian-8",
    "zone": "asia-northeast1-a",
    "ssh_username": "packer",
    "image_family": "openresty",
    "image_name": "openresty-{{uuid}}"
    }],
    "provisioners": [
    {
    "type": "shell",
    "execute_command": "sudo {{.Path}}",
    "scripts": [
    "openresty.sh"
    ]
    }
    ]
    }
    ```

    べんり.