Slide 1

Slide 1 text

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | OCIスキルアップセミナー第2回 OCI CLIを使い倒す 2019年9月27日 日本オラクル株式会社 丸川 祐考 @mmarukaw #ocijp 1

Slide 2

Slide 2 text

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | Safe Harbor Statement 以下の事項は、弊社の一般的な製品の方向性に関する概要を説明するものです。 また、情報提供を唯一の目的とするものであり、いかなる契約にも組み込むこと はできません。以下の事項は、マテリアルやコード、機能を提供することをコ ミットメント(確約)するものではないため、購買決定を行う際の判断材料になさ らないで下さい。 オラクル製品に関して記載されている機能の開発、リリースおよび時期について は、弊社の裁量により決定されます。 2

Slide 3

Slide 3 text

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | OCI CLI の概要 CLI は、OCI リソースを管理するための重要なツールです。 Webコンソールにあるほとんどの機能を提供します。 さらにスクリプトを使用した拡張機能も可能です。 主な機能 • Python SDK で 構築 • Python 2.7.5 + または 3.5 + • Mac、Windows、および Linux • OCI API インタラクション 3

Slide 4

Slide 4 text

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | CLI コマンド構成 OCI CLI は、単一のコマンドを使用してほとんどのサービスとの対話を可能にする 統合ツールです。プログラムコマンドを入力した後、サービス、アクション、お よび追加のオプションを指定します。 4

Slide 5

Slide 5 text

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | CLI のインストール スクリプトを使用インストール or 手動 Windows 1. 管理者としてPowerShell 実行 > Set-ExecutionPolicy RemoteSigned > powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://raw.githubusercontent.com/oracle/ocicli/master/scri pts/install/install.ps1’))” Linux / Mac 1. ターミナルプロンプトで実行: $ bash -c "$(curl -L https://raw.githubusercontent.com/oracle/oci- cli/master/scripts/install/install.sh)" 5

Slide 6

Slide 6 text

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | configファイルの作成 アクセスするためのIAM資格証明を ~/.oci/config に格納 手動入力 or setup コマンドで指定 $ oci setup config 設定例 $ cat ~/.oci/config [DEFAULT] user=ocid1.user.oc1..aaaaaaaaja3b5st7wd5lkj245jlk235lk23sdf9sdfdhwlndbm6mf7pznq fingerprint=8b:cc:27:61:dd:92:22:45:c1:24:f1:3d:84:5f:ee:b4 key_file=/home/opc/.oci/oci_api_key.pem tenancy=ocid1.tenancy.oc1..aaaaaaaaxy6bh46cdnlfpwerljwelrkjwer23cxtfxhhva2hna region=us-phoenix-1 6

Slide 7

Slide 7 text

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | CLI プロファイル プロファイルを使用すると、configファイルに複数の資格証明が設定可能で、CLI 実行時に適切なプロファイルを参照できる $ oci compute image list --profile dev_compartment [DEFAULT] … [dev_compartment] user=ocid1.user.oc1..aaaaaaaaja3b5st7wd5lkj245jlk235lk23sdf9sdfdhwlndbm6mf7pznq fingerprint=8b:cc:27:61:dd:92:22:45:c1:24:f1:3d:84:5f:ee:b4 key_file=/home/opc/.oci/oci_api_key.pem tenancy=ocid1.tenancy.oc1..aaaaaaaaxy6bh46cdnlfpwerljwelrkjwer23cxtfxhhva2hna region=us-phoenix-1 compartment-id=ocid1.compartment.oc1..aaaaaaaa8asdf6lkj7wer5lh3aer0asdv9j 7

Slide 8

Slide 8 text

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | oci-cli-rc ファイル 8 • 資格証明以外の情報は、~/.oci/oci_cli_rc ファイルに格納 • 以下の設定が可能 – プロファイル毎のデフォルトパラメータ値 (compartment-idなど) – コマンドの別名 (例 : “ls”をlistの別名として登録) – オプションの別名 (例 : “--ad” を --availability-domain の別名として登録) – --query オプションに渡される名前付き問合せ • 以下のコマンドで作成可能 – oci setup oci-cli-rc --file path/to/target/file

Slide 9

Slide 9 text

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | CLIのオプション 9 • --output [json | table] – 出力を json形式、表形式から選択 • --query – JMESPath クエリを用いてコマンド出力を絞り込み • --generate-full-command-json-input – コマンドで使用可能なすべての入力オプションを含むJSONの出力 • --generate-param-json-input <パラメータ名> – 指定したパラメータの入力オプションを含むJSONの出力 • --from-json – JSONで定義されているパラメータを入力に使用

Slide 10

Slide 10 text

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | --query オプションの使用 $ oci compute image list --output table 10

Slide 11

Slide 11 text

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | --query オプションの使用 $ oci compute image list --query "data [*].{ImageName:¥"display-name¥", OCID:id}" --output table 11

Slide 12

Slide 12 text

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | 修飾子を指定した--query オプションの使用 $ oci compute image list --query ¥ "data [?contains(¥"display-name¥", 'Oracle-Linux')].{ImageName:¥"display-name¥", OCID:id}" –output table 12

Slide 13

Slide 13 text

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | JSON 入力テンプレートの操作 コマンド・パラメータ --generate-full-command-json-input は特定の値を設定できる テンプレートシェルを作成するために使用できます。これは、開発用コンピュー トインスタンスなどのリソーステンプレートを作成するのに最適です。 13 $ oci compute instance launch --generate-full-command-json-input > compute_template.json

Slide 14

Slide 14 text

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | JSON テンプレートを使用したコンピュートインスタン スの起動 $ oci compute instance launch --from-jsonfile://compute_template.json 14

Slide 15

Slide 15 text

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | --query句の活用 • --query 句を活用することで、オブジェクトの作成した際のOCIDを返 り値で取得できる • 例 – VCNID=$(oci network vcn create --cidr-block 10.0.0.0/16 --display-name $VCNNAME --query 'data.id' --raw-output) 15

Slide 16

Slide 16 text

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | 非同期リクエスト結果の待機 16 #!/bin/bash instance_id=$(oci compute instance launch --from-json file://compute_template.json ¥ --query “data.id” | sed –e ‘s/^”//’ –e s/”$//’ pub_ip=$(oci compute instance list-vnics --instance-id $instance_id --query ¥ “data [*].¥”public-ip¥”” | grep -o '[0-9]¥{1,3¥}¥.[0-9]¥{1,3¥}¥.[0-9]¥{1,3¥}¥.[0-9]¥{1,3¥}’) # verify SSH connectivity ssh -qi ~/.ssh/id_rsa opc@$pub_ip while [ $? -ne 0 ]; do echo "Checking SSH connectivity“ && sleep 10 ssh -qi ~/.ssh/id_rsa opc@$pub_ip done echo "SSH is up - lets move on!“ && sleep 3 #run a simple test curl http://$pub_ip/testpage.html • インスタンス作成などの非同期リクエストは、waitForStateというパラメーター を設定することでバックグラウンドの処理実行結果を待機できる

Slide 17

Slide 17 text

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | インスタンス・プリンシパルの活用 • インスタンス・プリンシパルを利用すると、OCI内のインスタンスか ら資格情報なしで CLI/API が利用できる – インスタンス・プリンシパル = 特定のルールに合致するインスタンスに対し てIAMを利用したアクセス権を付与する仕組み • 管理者権限を持つユーザーに対してAPI鍵を発行する必要がなくなる ため、セキュリティが向上する – ただしインスタンスへのアクセスは厳重に管理が必要 – インスタンスを停止するとAPIアクセスを遮断できる 17

Slide 18

Slide 18 text

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | CLI ドキュメント • OracleCLI ドキュメント(日本語) https://docs.oracle.com/cd/E97706_01/Content/API/Concepts/cliconcepts.htm • Oracle Cloud Infrastructure CLI Command Reference https://docs.cloud.oracle.com/iaas/tools/oci-cli/latest/oci_cli_docs/ • コマンドライン(CLI)でOCIを操作する - Oracle Cloud Infrastructureチュートリアル https://community.oracle.com/docs/DOC-1019624 • JMESpath情報 http://jmespath.org/ • OCI-CLI GitHub Repositry https://github.com/oracle/oci-cli 18

Slide 19

Slide 19 text

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | (参考) • Oracle Cloud Infrastructure CLI用のfish補完を作ってみた https://qiita.com/sugimount/items/fb71920f846175633d68 Ocicliの高速なコマンド補完をやってくれる。ただしfish限定 • OCI CLI のプロファイルからテナンシの OCID を得る Bash スクリプト http://neos21.hatenablog.com/entry/2019/07/12/080000 OcicliにはTenancy OCIDを返してくれるクエリがないので、プロファイルから取 得する方法 19

Slide 20

Slide 20 text

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | シリーズ : OCIスキルアップセミナー • Oracle Cloud Infrastructure (OCI) の技術トピックを取り上げてディープに学習する 勉強会シリーズです。月に1回のペースで、業務時間後に開催しています。 • 時々ミートアップイベントも予定しています • Connpass の Oracle Code Night グループで活動しています https://oracle-code-tokyo-dev.connpass.com/ 20

Slide 21

Slide 21 text

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | 21

Slide 22

Slide 22 text

No content