$30 off During Our Annual Pro Sale. View Details »

Customazed CLI: カスタムVMイメージ作成支援ツール

YAEGASHI Takeshi
December 16, 2020
270

Customazed CLI: カスタムVMイメージ作成支援ツール

第30回 Tokyo Jazug Night (Online)
https://jazug.connpass.com/event/197139/

GitHub: Customazed CLI - Azure VM Custom Script Helper
https://github.com/yaegashi/customazed

Qiita: Azure Image Builder と Customazed CLI による簡単 VM イメージ構築
https://qiita.com/yaegashi/items/5216a9b37c2041b93a3a

YAEGASHI Takeshi

December 16, 2020
Tweet

More Decks by YAEGASHI Takeshi

Transcript

  1. Customazed CLI:
    カスタムVMイメージ作成支援ツール
    2020-12-16 第30回 Tokyo Jazug Night (Online)
    八重樫 剛史 Takeshi Yaegashi

    View Slide

  2. 自己紹介
    2
    八重樫 剛史 Takeshi Yaegashi
    株式会社バンダイナムコスタジオ所属
    Linux・Unix・OSS・Go 言語が好きなエンジニア
    組み込みシステム開発、ゲームサーバ開発、CI/CD インフラ開発、
    開発環境のクラウドシフトなどの業務に従事
    活動場所
    ホームページ・ブログ https://l0w.dev
    GitHub https://github.com/yaegashi
    GitLab https://gitlab.com/yaegashi
    Twitter https://twitter.com/hogegashi

    View Slide

  3. 今日の話題 - Customazed CLI
    3
    https://github.com/yaegashi/customazed
    ▪ CustomAZed: Customized + Azure な造語
    よみ:かすためいずど?
    ▪ ファイルのアップロードが必要になる
    Azure リソース作りを支援してくれる CLI ツール
    ▪ Windows / macOS / Linux で動く (powered by Go)
    ▪ Qiita の紹介記事
    Azure Advent Calendar 2020 14日目
    https://qiita.com/yaegashi/items/5216a9b37c2041b93a3a

    View Slide

  4. こういうのでイラッとした経験ないですか?
    4
    ARM Template などで頻出する VM CustomScript Extension のリソース
    実行するスクリプトを非公開 Blob ストレージに置いて SAS つき URL を渡している
    "properties": {
    "publisher": "Microsoft.Compute",
    "type": "CustomScriptExtension",
    "typeHandlerVersion": "1.8",
    "autoUpgradeMinorVersion": true,
    "settings": {
    "fileUris": [
    "[uri(parameters('_artifactsLocation'), concat(variables('ScriptFileName'), parameters('_art
    ifactsLocationSasToken')))]"
    ],
    "commandToExecute": "[concat('powershell -ExecutionPolicy Unrestricted -File ', variables('Scr
    iptFolder'), '/', variables('ScriptFileName'))]"
    }
    }

    View Slide

  5. こういうのでイラッとした経験ないですか?
    5
    Azure Image Builder のカスタマイザでも外部ファイル参照は出てくる
    公開可能なファイルであれば GitHub に置くのでもよいが…
    "customize": [
    {
    "type": "PowerShell",
    "name": "CreateBuildPath",
    "runElevated": false,
    "scriptUri": "https://raw.githubusercontent.com/danielsollondon/azvmimagebuilder/master/testPs
    Script.ps1"
    },
    {
    "type": "File",
    "name": "downloadBuildArtifacts",
    "sourceUri": "https://raw.githubusercontent.com/danielsollondon/azvmimagebuilder/master/quickq
    uickstarts/exampleArtifacts/buildArtifacts/index.html",
    "destination": "c:¥¥buildArtifacts¥¥index.html"
    },

    View Slide

  6. いま編集中のファイルを持っていってほしい!
    6

    View Slide

  7. Customazed CLI を使えば楽になります!
    7
    {{upload `script/hello.ps1`}} のように書くと、ファイルを適切なストレージ
    アカウントにアップロードした上で、ダウンロード URL を埋め込んでくれます
    {
    "fileUris": [
    "{{upload `scripts/hello.ps1`}}"
    ],
    "commandToExecute": "powershell -ExecutionPolicy Bypass -Command {{prefix}}/scripts/hello.ps1"
    }
    {
    "fileUris": [
    "https://customazed12345.blob.core.windows.net/customazed/9a85b47c-2e6e-508f-9c80-9dc6fb2d15
    f3/scripts/hello.ps1"
    ],
    "commandToExecute": "powershell -ExecutionPolicy Bypass -Command 9a85b47c-2e6e-508f-9c80-9dc6f
    b2d15f3/scripts/hello.ps1"
    }

    View Slide

  8. Customazed CLI Example: CustomScript Extension
    8
    設定ファイル customazed.json を用意します (Go Template 記法)
    {
    "tenantId": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
    "subscriptionId": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
    "storage": {
    "location": "westus2",
    "resourceGroup": "CustomazedRG",
    "accountName": "customazed12345",
    "containerName": "customazed",
    "prefix": "{{hash (cfg `machine.resourceGroup`) (cfg `machine.machineName`)}}"
    },
    "machine": {
    "resourceGroup": "MachineRG",
    "machineName": "windowsMachine"
    }
    }

    View Slide

  9. Customazed CLI Example: CustomScript Extension
    9
    同じ場所に customazed_machine.json と scripts/hello.ps1 を用意します
    {
    "fileUris": [
    "{{upload `scripts/hello.ps1`}}"
    ],
    "commandToExecute": "powershell -ExecutionPolicy Bypass -Command {{prefix}}/scripts/hello.ps1"
    }
    Write-Host "hello, world"

    View Slide

  10. Customazed CLI Example: CustomScript Extension
    10
    最初に認証を行い、機能・プロバイダの登録を行います (一度だけ必要)
    $ customazed login
    2020/12/16 16:40:02 Loading config file customazed.json
    2020/12/16 16:40:02 To sign in, use a web browser to open the page https://microsoft.com/devic
    elogin and enter the code HN54K2H9F to authenticate.
    2020/12/16 16:41:04 Saving auth-dev token in .customazed/auth_dev.json
    $ customazed feature register
    2020/12/16 16:42:06 Loading config file customazed.json
    2020/12/16 16:42:06 Loading auth-dev token in .customazed/auth_dev.json
    2020/12/16 16:42:06 Feature: registering Microsoft.VirtualMachineImages/VirtualMachineTemplate
    Preview
    2020/12/16 16:42:09 Provider: registering Microsoft.VirtualMachineImages
    2020/12/16 16:42:11 Provider: registering Microsoft.KeyVault
    2020/12/16 16:42:13 Provider: registering Microsoft.Compute
    2020/12/16 16:42:14 Provider: registering Microsoft.Storage

    View Slide

  11. Customazed CLI Example: CustomScript Extension
    11
    ストレージアカウントの作成と必要なアクセス権限設定をしてくれます
    $ customazed setup
    2020/12/16 16:44:05 Loading config file customazed.json
    2020/12/16 16:44:05 Loading auth-dev token in .customazed/auth_dev.json
    2020/12/16 16:44:05 Loading auth-dev token in .customazed/auth_dev.json
    2020/12/16 16:44:05 Storage: creating resource group: CustomazedRG
    2020/12/16 16:44:08 Storage: creating storage account: customazed12345
    2020/12/16 16:44:10 Storage: creating blob container: customazed
    2020/12/16 16:44:10 Identity: missing configuration
    2020/12/16 16:44:10 Machine: enabling system assigned identity
    2020/12/16 16:45:12 Image: missing configuration
    2020/12/16 16:45:12 Gallery: missing configuration
    2020/12/16 16:45:12 Builder: missing configuration
    2020/12/16 16:45:12 Identity: missing configuration
    2020/12/16 16:45:12 Image: missing configuration
    2020/12/16 16:45:12 Gallery: missing configuration
    2020/12/16 16:45:12 Role: assign role to user for blob container
    2020/12/16 16:45:14 Role: assign role to machine for blob container

    View Slide

  12. Customazed CLI Example: CustomScript Extension
    12
    実行すると処理された customazed_machine.json を表示して確認待ちになります
    $ customazed machine run
    2020/12/16 16:47:11 Loading config file customazed.json
    2020/12/16 16:47:11 Loading auth-dev token in .customazed/auth_dev.json
    2020/12/16 16:47:11 Loading custom script settings customazed_machine.json
    2020/12/16 16:47:11 Blob: adding scripts/hello.ps1
    2020/12/16 16:47:11
    {
    "fileUris": [
    "https://customazed12345.blob.core.windows.net/customazed/9a85b47c-2e6e-508f-9c80-9dc6f
    b2d15f3/scripts/hello.ps1"
    ],
    "commandToExecute": "powershell -ExecutionPolicy Bypass -Command 9a85b47c-2e6e-508f-9c80-
    9dc6fb2d15f3/scripts/hello.ps1",
    "timestamp": 1608104831
    }
    2020/12/16 16:47:11 Files to upload: 1
    Press ENTER to proceed:

    View Slide

  13. Customazed CLI Example: CustomScript Extension
    13
    実行完了後、出力結果を確認します
    Press ENTER to proceed: (ENTER 押した)
    2020/12/16 16:47:27 Loading auth-dev token in .customazed/auth_dev.json
    2020/12/16 16:47:28 Blob: destination https://customazed12345.blob.core.windows.net/custom
    azed/9a85b47c-2e6e-508f-9c80-9dc6fb2d15f3
    2020/12/16 16:47:28 Blob: uploading scripts/hello.ps1
    2020/12/16 16:47:30 Executing VM extension...
    2020/12/16 16:48:30 Success: use "customazed machine show-status" to see the output
    $ customazed machine show-status
    2020/12/16 16:50:41 Loading config file customazed.json
    2020/12/16 16:50:41 Loading auth-dev token in .customazed/auth_dev.json
    2020/12/16 16:50:41 ProvisioningState/succeeded: Provisioning succeeded
    Command execution finished
    2020/12/16 16:50:41 StdOut:
    hello, world
    2020/12/16 16:50:41 StdErr:

    View Slide

  14. Customazed CLI にできること (v0.3.1)
    14
    ▪ Azure VM CustomScript Extension のサポート(この資料で解説)
    − 手元にあるファイルを VM に送り込み、コマンド実行と結果取得が手軽にできる
    − Windows Linux 両方の VM に対応
    ▪ Azure Image Builder のサポート(Qiita 記事で解説)
    − AIB に必要なユーザー割り当て ID や共有イメージギャラリーなども自動的に作ってくれる
    − Windows Virtual Desktop 向け VM イメージカスタマイズの CI/CD に活用
    ▪ 任意テキストの処理と、それに含まれるファイルのアップロード
    − ARM Template などのプリプロセスに利用可能

    View Slide

  15. Customazed CLI 今後の計画
    15
    ▪ Azure VM RunCommand Extension サポート
    − CustomScript とは異なる VM コマンド実行手段
    ▪ ARM Template サポート
    − ARM Template を直接デプロイできる
    − ARM TemplateSpec リソースを作成できる
    ▪ ZIP アップロード機能
    − 複数のフォルダやファイルを自動的に ZIP ファイルにまとめてアップロード
    ▪ ドキュメントなどの充実

    View Slide

  16. おわり
    16
    ▪ ご清聴ありがとうございました

    View Slide