zaki work log

作業ログやら生活ログやらなんやら

[Oracle Cloud] CLIツールのセットアップメモ

Oracle CloudをCLIで操作するためのツールのインストールとセットアップについて。
以前何事もなくセットアップしていたんだけど、新しい環境に入れようとしたらいろいろ(鍵設定周りで)詰まったのでメモ。
(全自動でAPIキー設定出来る方法があるかもしれないけど分からなかったので、公開鍵を手動で登録してる)

環境

[zaki@manager-dev ~]$ oci --version
2.20.0
[zaki@manager-dev ~]$ python3 --version
Python 3.6.8
[zaki@manager-dev ~]$ cat /etc/centos-release
CentOS Linux release 7.9.2009 (Core)

インストール

OCI CLIのインストールは以下。

docs.oracle.com

インストールにはPython3が必要なため、Python2しか入っていないLinux OSだとアップグレードするか?と聞かれる。事前にPython3を入れておくと無難。

bash -c "$(curl -L https://raw.githubusercontent.com/oracle/oci-cli/master/scripts/install/install.sh)"

デフォルトだと$HOME/lib$HOME/binへインストールされる。
$HOME/localとか使いたい場合はインストール時に対話的に(3か所)聞かれるのでパスを入力する。

[zaki@manager-dev ~]$ bash -c "$(curl -L https://raw.githubusercontent.com/oracle/oci-cli/master/scripts/install/install.sh)"
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 16053  100 16053    0     0  54223      0 --:--:-- --:--:-- --:--:-- 54416

    ******************************************************************************
    You have started the OCI CLI Installer in interactive mode. If you do not wish
    to run this in interactive mode, please include the --accept-all-defaults option.
    If you have the script locally and would like to know more about
    input options for this script, then you can run:
    ./install.sh -h
    If you would like to know more about input options for this script, refer to:
    https://github.com/oracle/oci-cli/blob/master/scripts/install/README.rst
    ******************************************************************************
Downloading Oracle Cloud Infrastructure CLI install script from https://raw.githubusercontent.com/oracle/oci-cli/v2.14.4/scripts/install/install.py to /tmp/oci_cli_install_tmp_zAYF.
######################################################################## 100.0%
Running install script.
python3 /tmp/oci_cli_install_tmp_zAYF 
-- Verifying Python version.
-- Python version 3.6.8 okay.

===> In what directory would you like to place the install? (leave blank to use '/home/zaki/lib/oracle-cli'): /home/zaki/local/oracle-cli/lib
-- Creating directory '/home/zaki/local/oracle-cli/lib'.
-- We will install at '/home/zaki/local/oracle-cli/lib'.

===> In what directory would you like to place the 'oci' executable? (leave blank to use '/home/zaki/bin'): /home/zaki/local/oracle-cli/bin
-- Creating directory '/home/zaki/local/oracle-cli/bin'.
-- The executable will be in '/home/zaki/local/oracle-cli/bin'.

===> In what directory would you like to place the OCI scripts? (leave blank to use '/home/zaki/bin/oci-cli-scripts'): /home/zaki/local/oracle-cli/bin/oci-cli-scripts
-- Creating directory '/home/zaki/local/oracle-cli/bin/oci-cli-scripts'.
-- The scripts will be in '/home/zaki/local/oracle-cli/bin/oci-cli-scripts'.

===> Currently supported optional packages are: ['db (will install cx_Oracle)']
What optional CLI packages would you like to be installed (comma separated names; press enter if you don't need any optional packages)?: 
-- The optional packages installed will be ''.
-- Trying to use python3 venv.
-- Executing: ['/usr/bin/python3', '-m', 'venv', '/home/zaki/local/oracle-cli/lib']
-- Executing: ['/home/zaki/local/oracle-cli/lib/bin/pip', 'install', '--upgrade', 'pip']
Cache entry deserialization failed, entry ignored
Collecting pip
  Downloading https://files.pythonhosted.org/packages/de/47/58b9f3e6f611dfd17fb8bd9ed3e6f93b7ee662fb85bdfee3565e8979ddf7/pip-21.0-py3-none-any.whl (1.5MB)
    100% |████████████████████████████████| 1.5MB 1.3MB/s 
Installing collected packages: pip
  Found existing installation: pip 9.0.3
    Uninstalling pip-9.0.3:
      Successfully uninstalled pip-9.0.3
Successfully installed pip-21.0

  [snip]

Successfully built PyYAML retrying terminaltables
Installing collected packages: pycparser, six, cffi, cryptography, pytz, python-dateutil, pyOpenSSL, configparser, certifi, terminaltables, retrying, PyYAML, oci, jmespath, click, arrow, oci-cli
Successfully installed PyYAML-5.1.2 arrow-0.17.0 certifi-2020.12.5 cffi-1.14.4 click-6.7 configparser-4.0.2 cryptography-3.2.1 jmespath-0.10.0 oci-2.29.0 oci-cli-2.20.0 pyOpenSSL-19.1.0 pycparser-2.20 python-dateutil-2.8.1 pytz-2020.5 retrying-1.3.3 six-1.14.0 terminaltables-3.1.0

===> Modify profile to update your $PATH and enable shell/tab completion now? (Y/n): y

===> Enter a path to an rc file to update (file will be created if it does not exist) (leave blank to use '/home/zaki/.bashrc'): 
-- Backed up '/home/zaki/.bashrc' to '/home/zaki/.bashrc.backup'
-- Tab completion set up complete.
-- If tab completion is not activated, verify that '/home/zaki/.bashrc' is sourced by your shell.
-- 
-- ** Run `exec -l $SHELL` to restart your shell. **
-- 
-- Installation successful.
-- Run the CLI with /home/zaki/local/oracle-cli/bin/oci --help
[zaki@manager-dev ~]$

.bashrcにPATH設定が追加されるのでsourceとかすればociが使えるようになる。
インストールできたら初期設定を行う。

初期設定

CLIをインストールしたら設定。
途中入力する各値は以下で確認できる。

ユーザーのOCIDを確認

f:id:zaki-hmkc:20210131141044p:plain

f:id:zaki-hmkc:20210131141606p:plain

テナンシのOCIDを確認

f:id:zaki-hmkc:20210131141843p:plain

f:id:zaki-hmkc:20210131141937p:plain

リージョンは

f:id:zaki-hmkc:20210131142120p:plain

f:id:zaki-hmkc:20210131142216p:plain

この辺で確認。

[zaki@manager-dev ~]$ oci setup config
    This command provides a walkthrough of creating a valid CLI config file.

    The following links explain where to find the information required by this
    script:

    User API Signing Key, OCID and Tenancy OCID:

        https://docs.cloud.oracle.com/Content/API/Concepts/apisigningkey.htm#Other

    Region:

        https://docs.cloud.oracle.com/Content/General/Concepts/regions.htm

    General config documentation:

        https://docs.cloud.oracle.com/Content/API/Concepts/sdkconfig.htm


Enter a location for your config [/home/zaki/.oci/config]: 
Enter a user OCID: ★ユーザーのOCIDを入力
Enter a tenancy OCID: ★テナンシのOCIDを入力
Enter a region by index or name(e.g.
1: ap-chiyoda-1, 2: ap-chuncheon-1, 3: ap-hyderabad-1, 4: ap-melbourne-1, 5: ap-mumbai-1,
6: ap-osaka-1, 7: ap-seoul-1, 8: ap-sydney-1, 9: ap-tokyo-1, 10: ca-montreal-1,
11: ca-toronto-1, 12: eu-amsterdam-1, 13: eu-frankfurt-1, 14: eu-zurich-1, 15: me-dubai-1,
16: me-jeddah-1, 17: sa-santiago-1, 18: sa-saopaulo-1, 19: uk-cardiff-1, 20: uk-gov-cardiff-1,
21: uk-gov-london-1, 22: uk-london-1, 23: us-ashburn-1, 24: us-gov-ashburn-1, 25: us-gov-chicago-1,
26: us-gov-phoenix-1, 27: us-langley-1, 28: us-luke-1, 29: us-phoenix-1, 30: us-sanjose-1): ★使用しているリージョンを入力
Do you want to generate a new API Signing RSA key pair? (If you decline you will be asked to supply the path to an existing key.) [Y/n]: y
Enter a directory for your keys to be created [/home/zaki/.oci]: 
Enter a name for your key [oci_api_key]: 
Public key written to: /home/zaki/.oci/oci_api_key_public.pem
Enter a passphrase for your private key (empty for no passphrase): 
Private key written to: /home/zaki/.oci/oci_api_key.pem
Fingerprint: ........
Config written to /home/zaki/.oci/config


    If you haven't already uploaded your API Signing public key through the
    console, follow the instructions on the page linked below in the section
    'How to upload the public key':

        https://docs.cloud.oracle.com/Content/API/Concepts/apisigningkey.htm#How2


[zaki@manager-dev ~]$ 

これでキーペアが作成される。

[zaki@manager-dev ~]$ ls -l ~/.oci/
合計 12
-rw-------. 1 zaki zaki  301  1月 31 11:02 config
-rw-------. 1 zaki zaki 1679  1月 31 11:02 oci_api_key.pem
-rw-------. 1 zaki zaki  451  1月 31 11:02 oci_api_key_public.pem

次は、作成された公開鍵(oci_api_key_public.pem)をクラウドに登録する。

公開鍵設定

docs.oracle.com

webの管理コンソールを開き、右上の「ユーザー設定」からユーザーのOCIDを確認したのと同じ画面下部にある「APIキー」の「APIキーの追加」で、作成した公開鍵(oci_api_key_public.pem)の中身(catなどで確認)を貼り付ける。

f:id:zaki-hmkc:20210131141128p:plain

f:id:zaki-hmkc:20210131141210p:plain

貼り付けるとプレビュー画面になるので、ダイアログを閉じれば公開鍵が登録される。
登録した公開鍵が有効になるまで若干のタイムラグがあるけど、認証できるようになれば、

[zaki@manager-dev ~]$ oci os ns get
{
  "data": "........"
}

という感じで、認証エラーにならず正常にレスポンスを得られる。

コマンド例

コンパートメントの一覧

$ oci iam compartment list

コンパートメント一覧から(1件目の)compartment-idをピックアップ

$ oci iam compartment list --query 'data[0]."compartment-id"' 

可用性ゾーン一覧

oci iam availability-domain list

使用可能イメージ一覧

$ oci compute image list -c $(oci iam compartment list --query 'data[0]."compartment-id"' | sed -e 's/"//g')

これは~/.oci/configに設定されてるコンパートメントID見てくれなくて(?)、-cオプションが必須みたい。

docs.oracle.com

docs.oracle.com