はじめに
Oracle Cloud Infractructure(以下OCI) を使っていくと、どのリージョンでどれくらいのリソースを使っているのか一覧化したくなります。Webブラウザでコンソールをぽちぽち見ていくことも出来ますが、多数のリージョン・コンパートメントがあるとなかなか一苦労です。そこで、GitHub で公開されている showoci という名前の Python ツールを使うことで、簡単に一覧化が出来ます。csv・json・標準出力の3種類に一覧を出力できます。
今回は、showoci をインストールして、全リソースを csv で出力する方法を確認していきます。
Cloud Shell の起動
showoci は、Python で動かすツールです。手元の環境にインストールしてもいいのですが、Cloud Shell を使うのが一番簡単です。OCI Console を開いて、右上のメニューから Cloud Shell を起動します。
showoci の実行
venv を使って、Python 環境の仮想環境を作ります。
python3 -m venv python_venv
source python_venv/bin/activate
実行例
suguru_sug@cloudshell:~ (ap-tokyo-1)$ python3 -m venv python_venv
suguru_sug@cloudshell:~ (ap-tokyo-1)$ source python_venv/bin/activate
(python_venv) suguru_sug@cloudshell:~ (ap-tokyo-1)$
Python の OCI SDK を pip 経由でインストールします。
pip install oci
実行例
(python_venv) suguru_sug@cloudshell:~ (ap-tokyo-1)$ pip install oci
Collecting oci
Downloading https://files.pythonhosted.org/packages/f0/0b/ccc2d83c7fd1d45c7ec3ccfe9c9463a7855e7f8f0fe1221fecbab528b2e9/oci-2.14.0-py2.py3-none-any.whl (4.2MB)
100% |████████████████████████████████| 4.2MB 276kB/s
Collecting pytz>=2016.10 (from oci)
Downloading https://files.pythonhosted.org/packages/4f/a4/879454d49688e2fad93e59d7d4efda580b783c745fd2ec2a3adf87b0808d/pytz-2020.1-py2.py3-none-any.whl (510kB)
100% |████████████████████████████████| 512kB 2.3MB/s
Collecting configparser==4.0.2 (from oci)
Downloading https://files.pythonhosted.org/packages/7a/2a/95ed0501cf5d8709490b1d3a3f9b5cf340da6c433f896bbe9ce08dbe6785/configparser-4.0.2-py2.py3-none-any.whl
Collecting pyOpenSSL>=17.5.0 (from oci)
Downloading https://files.pythonhosted.org/packages/9e/de/f8342b68fa9e981d348039954657bdf681b2ab93de27443be51865ffa310/pyOpenSSL-19.1.0-py2.py3-none-any.whl (53kB)
100% |████████████████████████████████| 61kB 7.9MB/s
Collecting certifi (from oci)
Downloading https://files.pythonhosted.org/packages/57/2b/26e37a4b034800c960a00c4e1b3d9ca5d7014e983e6e729e33ea2f36426c/certifi-2020.4.5.1-py2.py3-none-any.whl (157kB)
100% |████████████████████████████████| 163kB 5.9MB/s
Collecting python-dateutil<3.0.0,>=2.5.3 (from oci)
Downloading https://files.pythonhosted.org/packages/d4/70/d60450c3dd48ef87586924207ae8907090de0b306af2bce5d134d78615cb/python_dateutil-2.8.1-py2.py3-none-any.whl (227kB)
100% |████████████████████████████████| 235kB 4.3MB/s
Collecting cryptography==2.8 (from oci)
Downloading https://files.pythonhosted.org/packages/45/73/d18a8884de8bffdcda475728008b5b13be7fbef40a2acc81a0d5d524175d/cryptography-2.8-cp34-abi3-manylinux1_x86_64.whl (2.3MB)
100% |████████████████████████████████| 2.3MB 494kB/s
Collecting six>=1.5.2 (from pyOpenSSL>=17.5.0->oci)
Downloading https://files.pythonhosted.org/packages/65/eb/1f97cb97bfc2390a276969c6fae16075da282f5058082d4cb10c6c5c1dba/six-1.14.0-py2.py3-none-any.whl
Collecting cffi!=1.11.3,>=1.8 (from cryptography==2.8->oci)
Downloading https://files.pythonhosted.org/packages/f1/c7/72abda280893609e1ddfff90f8064568bd8bcb2c1770a9d5bb5edb2d1fea/cffi-1.14.0-cp36-cp36m-manylinux1_x86_64.whl (399kB)
100% |████████████████████████████████| 399kB 2.7MB/s
Collecting pycparser (from cffi!=1.11.3,>=1.8->cryptography==2.8->oci)
Downloading https://files.pythonhosted.org/packages/ae/e7/d9c3a176ca4b02024debf82342dab36efadfc5776f9c8db077e8f6e71821/pycparser-2.20-py2.py3-none-any.whl (112kB)
100% |████████████████████████████████| 112kB 7.0MB/s
Installing collected packages: pytz, configparser, six, pycparser, cffi, cryptography, pyOpenSSL, certifi, python-dateutil, oci
Successfully installed certifi-2020.4.5.1 cffi-1.14.0 configparser-4.0.2 cryptography-2.8 oci-2.14.0 pyOpenSSL-19.1.0 pycparser-2.20 python-dateutil-2.8.1 pytz-2020.1 six-1.14.0
You are using pip version 9.0.3, however version 20.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
(python_venv) suguru_sug@cloudshell:~ (ap-tokyo-1)$
oci-python-sdk を GitHub からクローンします。
git clone https://github.com/oracle/oci-python-sdk
showoci のディレクトリへ移動します。
cd oci-python-sdk/examples/showoci
実行します。まずは、Identity 情報を除外して 標準出力に Print します。標準出力を見ると、Compute Instance を含めたリソース群の情報が確認できます。
python showoci.py -dt -ani
これで、実行確認が出来たので、次にCSV 出力をします。まずは、出力用のディレクトリを作成します。
mkdir ~/output
全てのリソースを対象に、ディレクトリを指定して出力します。
cd ~/oci-python-sdk/examples/showoci
python showoci.py -dt -a -csv ~/output/output.csv
末尾に、出力されたファイルの場所や、正常終了した内容が確認できます。
##########################################################################################
# Processing CSV Files #
##########################################################################################
CSV: identity_compartments --> /home/suguru_sug/output/output.csv_identity_compartments.csv
CSV: identity_users --> /home/suguru_sug/output/output.csv_identity_users.csv
CSV: identity_policy --> /home/suguru_sug/output/output.csv_identity_policy.csv
CSV: identity_groups --> /home/suguru_sug/output/output.csv_identity_groups.csv
CSV: compute --> /home/suguru_sug/output/output.csv_compute.csv
CSV: network_subnet --> /home/suguru_sug/output/output.csv_network_subnet.csv
CSV: network_routes --> /home/suguru_sug/output/output.csv_network_routes.csv
CSV: network_security_list --> /home/suguru_sug/output/output.csv_network_security_list.csv
CSV: network_security_group --> /home/suguru_sug/output/output.csv_network_security_group.csv
CSV: network_dhcp_options --> /home/suguru_sug/output/output.csv_network_dhcp_options.csv
CSV: load_balancer_listeners --> /home/suguru_sug/output/output.csv_load_balancer_listeners.csv
CSV: load_balancer_backendset --> /home/suguru_sug/output/output.csv_load_balancer_backendset.csv
CSV: limits --> /home/suguru_sug/output/output.csv_limits.csv
##########################################################################################
# Completed Successfully at 2020-05-01 10:53:42 #
##########################################################################################
(python_venv) suguru_sug@cloudshell:showoci (ap-tokyo-1)$
Object Storage 経由でダウンロード
Cloud Shell の内部に出力された csv ファイルを取得するために、Object Storage へ格納します。まず、いくつかのファイルが出力されているので、zip で一つのまとめます (tar.gz でもいいです)
cd ~
zip -r outout.zip output
Object Storage へアップロード
oci os object put --bucket-name inputbucket --file outout.zip
Console からダウンロードします
こんな感じのcsvファイルを確認できます
参考URL