Database Support Blog

  • Oracle Cloud
2020.06.26

OCI CLIを利用してコマンドラインからOCIを操作する方法

Oracle Cloud は管理コンソールからサービスの作成・更新・起動・停止などの操作を行います。また、管理コンソールとほぼ同じ操作を実行可能なコマンドラインでツールとして「OCI CLI」が提供されています。

「この時間にサーバを停止したい」、「サーバの稼働状況をログとして残したい」といった場合に、「OCI CLI」を使えば簡単に実現できます。

今回は「OCI CLI」を利用するための初期設定をご紹介します。


OCI CLIインストール

まずは、OCI CLIツールをインストールします。

LinuxOSの場合

1.Pythonがインストールされていて、PATHが通っていることを確認します。
※PythonはOS構築後すでにインストールされていることが多いため、ここではインストール方法は割愛します。

2.以下コマンドでインストールします。
$ bash -c "$(curl -L https://raw.githubusercontent.com/oracle/oci-cli/master/scripts/install/install.sh)"
※表示の都合上、改行を入れています。

 
 $ 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 S peed
 100 14620 100 14620 0 0 18386 0 --:--:-- --:--:-- --:--:-- 18366
 
  ******************************************************************************
  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/ oall/install.py to /tmp/oci_cli_install_tmp_Zd0l.
 ######################################################################## 100.0%
 Running install script.
 python3 /tmp/oci_cli_install_tmp_Zd0l
 -- Verifying Python version.
 -- Python version 3.6.8 okay.
 
 ===> In what directory would you like to place the install?
      (leave blank to use '/home/opc/lib/oracle-cli'):<インストール先を指定:初期状態の場合はEnter>
 -- Creating directory '/home/opc/lib/oracle-cli'.
 -- We will install at '/home/opc/lib/oracle-cli'.
 
 ===> In what directory would you like to place the 'oci' executable? 
      (leave blank to use '/home/opc/bin'):<実行ファイル配置先を指定:初期状態の場合はEnter>
 -- Creating directory '/home/opc/bin'.
 -- The executable will be in '/home/opc/bin'.
 
 ===> In what directory would you like to place the OCI scripts?
      (leave blank to use '/home/opc/bin/oci-cli-scripts'):<設定ファイル配置先を指定:初期状態の場合はEnter>
 -- Creating directory '/home/opc/bin/oci-cli-scripts'.
 -- The scripts will be in '/home/opc/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)?:<追加でインストールするオプションを指定:追加のオプションに指定がない場合はEnter>
 -- The optional packages installed will be ''.
 -- Trying to use python3 venv.
  :
 <中略>
  :
 Installing collected packages: certifi, six, pycparser, cffi, cryptography, pyOpenSSL, pytz, 
 python-dateutil, configparser, oci, click, jmespath, arrow, retrying, PyYAML, idna, terminaltables, oci-cli
 Successfully installed PyYAML-5.1.2 arrow-0.14.7 certifi-2020.4.5.1 cffi-1.14.0 click-6.7
 configparser-4.0.2 cryptography-2.8 idna-2.6 jmespath-0.9.4 oci-2.12.3 oci-cli-2.9.10 
 pyOpenSSL-18.0.0 pycparser-2.20 python-dateutil-2.8.1 pytz-2019.3 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):<PATH環境変数の更新可否:初期状態(Y)の場合はEnter>
 ===> Enter a path to an rc file to update 
      (leave blank to use '/home/opc/.bashrc'):<更新する環境変数指定ファイルを指定:初期状態の場合はEnter>
 -- Backed up '/home/opc/.bashrc' to '/home/opc/.bashrc.backup'
 -- Tab completion set up complete.
 -- If tab completion is not activated, verify that '/home/opc/.bashrc' is sourced by your shell.
 --
 -- ** Run `exec -l $SHELL` to restart your shell. **
 --
 -- Installation successful.  --正常完了
 -- Run the CLI with /home/opc/bin/oci --help
 

3.バージョンの確認をします。

 
 $ oci -v
 2.9.10
 

Windows OSの場合

1.Pythonが導入済みであり、PATHが通っていることを確認します。
※Python未導入の場合、OCI CLIのインストール時にPythonもインストールすることが可能です。

2. PowerShellを管理者権限で起動し、リモート実行ポリシーを構成します。

 
 PS C:\Windows\system32> Set-ExecutionPolicy RemoteSigned
 

3.以下コマンドでインストールします。
PS C:\Windows\system32> powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://raw.githubusercontent.com/oracle/oci-cli/master/scripts/install/install.ps1'))"
※表示の都合上、改行を入れています。

 
 PS C:\Windows\system32> powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).D
 ownloadString('https://raw.githubusercontent.com/oracle/oci-cli/master/scripts/install/install.ps1'))"
 
 ******************************************************************************
 You have started the OCI CLI Installer in interactive mode. If you do not wish
 to run this in interactive mode, please include the -AcceptAllDefaults option.
 If you have the script locally and would like to know more about
 input options for this script, then you can run:
 help .\install.ps1
 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
 
 ******************************************************************************
 VERBOSE: No valid Python installation found.
 
 Python is required to run the CLI.
 Install Python now? (Entering "n" will exit the installation script)
 [Y] Yes [N] No [?] Help (default is "Y"):<Pythonのインストール要否を指定:初期状態(Y)の場合はEnter>
 VERBOSE: Downloading Python...
 VERBOSE: Download Complete! Installer executable written to: C:\Users\opc\AppData\Local\Temp\tmpE375.tmp.exe
 VERBOSE: Installing Python to C:\Users\opc\Python...
 VERBOSE: Successfully installed Python!
 VERBOSE: Downloading install script to C:\Users\opc\AppData\Local\Temp\tmp5932.tmp
 VERBOSE: C:\Users\opc\Python False False
 VERBOSE: Using Python executable: C:\Users\opc\Python\python.exe to run install script...
 VERBOSE: Arguments to python script: "C:\Users\opc\AppData\Local\Temp\tmp5932.tmp"
 -- Verifying Python version.
 -- Python version 3.6.5 okay.
 
 ===> In what directory would you like to place the install? 
      (leave blank to use 'C:\Users\opc\lib\oracle-cli'):<インストール先を指定:初期状態の場合はEnter>
 -- Creating directory 'C:\Users\opc\lib\oracle-cli'.
 -- We will install at 'C:\Users\opc\lib\oracle-cli'.
 
 ===> In what directory would you like to place the 'oci.exe' executable?
       (leave blank to use 'C:\Users\opc\bin'):<実行ファイルの配置先を指定:初期状態の場合はEnter>
 -- Creating directory 'C:\Users\opc\bin'.
 -- The executable will be in 'C:\Users\opc\bin'.
 
 ===> In what directory would you like to place the OCI scripts? 
      (leave blank to use 'C:\Users\opc\bin\oci-cli-scripts'):<設定ファイル配置先を指定:初期状態の場合はEnter>
 -- Creating directory 'C:\Users\opc\bin\oci-cli-scripts'.
 -- The scripts will be in 'C:\Users\opc\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)?:<追加でインストールするオプションを指定:追加のオプションに指定がない場合はEnter>
 -- The optional packages installed will be ''.
 -- Trying to use python3 venv.
 :
 <中略>
 :
 3 retrying-1.3.3 six-1.14.0 terminaltables-3.1.0
 You are using pip version 9.0.3, however version 20.0.2 is available.
 You should consider upgrading via the 'python -m pip install --upgrade pip' command.
 
 ===> Modify PATH to include the CLI and enable tab completion in PowerShell now?
       (Y/n): <PATH環境変数の更新可否:初期状態(Y)の場合はEnter>
 --
 -- ** Close and re-open PowerShell to reload changes to your PATH **
 -- In order to run the autocomplete script, you may also need to set your PowerShell execution policy to allow for runni
 ng local scripts (as an Administrator run Set-ExecutionPolicy RemoteSigned in a PowerShell prompt)
 --
 -- Installation successful.  --正常完了
 -- Run the CLI with C:\Users\opc\bin\oci.exe --help
 VERBOSE: Successfully installed OCI CLI!
 
 

4.PowerShellを再起動します。

5.バージョンの確認をします。

 
 PS C:\Windows\system32> oci -v
 2.9.10
 

OCI CLIコマンド利用設定

インストールが完了したら、OCI CLIを利用してOracle Cloudへ接続するための設定を行います。この設定が完了すると、OCI CLIが利用可能になります。
※以下では、Oracle Cloud Infrastracture - Compute VMで構築したWindows OSでopcユーザにて設定をした場合を例にご紹介しています。

1.事前確認

セットアップの前に以下2点を確認しておきます。
 (1). Oracle Cloudへ接続する際に利用する、Oracle CloudユーザーのOCID
 (2). 接続するOracle CloudのテナントのOCID

事前確認 (1). Oracle Cloudへ接続する際に利用する、Oracle CloudユーザーのOCID

管理コンソールの「メニュー >> アイデンティティ >> ユーザー」よりユーザ一覧を表示します。

OCI CLIでOracle Cloudへ接続するユーザーの「名前」を選択します。

OCIDを確認します。「コピー」を選択してテキスト等に残しておきます。

事前確認(2). 接続するOracle CloudのテナントのOCID

管理コンソールの「メニュー >> 管理 >> テナンシ詳細」よりテナンシ情報を表示します。

OCIDを確認します。「コピー」を選択してテキスト等に残しておきます。


2.セットアップ

セットアップコマンドを実行します。
※PowerShellを管理者として実行し、操作します。
※表示の都合上、改行を入れています。

 
 PS C:\Windows\system32> 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 [C:\Users\opc\.oci\config]:<設定ファイル配置先:初期状態の場合はEnter>
 Enter a user OCID: <事前に確認したユーザーのOCID>
 Enter a tenancy OCID: <事前に確認したテナンシのOCID>
 Enter a region (e.g. ap-melbourne-1, ap-mumbai-1, ap-osaka-1, ap-seoul-1, ap-sydney-1, 
  ap-tokyo-1, ca-montreal-1, ca-toronto-1, eu-amsterdam-1, eu-frankfurt-1, eu-zurich-1, 
  me-jeddah-1, sa-saopaulo-1, uk-gov-london-1, uk-london-1, us-ashburn-1, us-gov-ashburn-1, 
  us-gov-chicago-1, us-gov-phoenix-1, us-langley-1, us-luke-1, us-phoenix-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 [C:\Users\opc\.oci]:<鍵ファイル配置先:初期状態の場合はEnter>
 Enter a name for your key [oci_api_key]:<鍵ファイル名:初期状態の場合はEnter>
 Public key written to: C:\Users\opc\.oci\oci_api_key_public.pem
 Enter a passphrase for your private key (empty for no passphrase):<パスワード:パスワード無しの場合はEnter>
 Private key written to: C:\Users\opc\.oci\oci_api_key.pem
 Fingerprint: b6:cb:f4:f0:6d:ae:78:9e:ba:90:7a:72:ad:7f:6d:a1
 Config written to C:\Users\opc\.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
 
 
 PS C:\Windows\system32>
 

”n”を指定した場合

 
 Enter the location of your private key file: <登録した公開鍵と対になる秘密鍵の配置先>
 : Fingerprint: b6:cb:f4:f0:6d:ae:78:9e:ba:90:7a:72:ad:7f:6d:a1
 Config written to C:\Users\opc\.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
 
 
 PS C:\Windows\system32>
 

3.WebコンソールへのAPIキー登録

管理コンソールの「メニュー >> アイデンティティ >> ユーザー」よりユーザ一覧を表示します。

OCI CLIでOracle Cloudへ接続するユーザーの「名前」を選択します。

ユーザ詳細画面のリソース”APIキー”より「公開キーの追加」を選択します。

公開キーを追加します。
公開キーの追加は、ファイルを選択する方法と直接キーを入力する方法があります。
※以下では、ファイルを選択する方法をご紹介します。

保存した公開鍵ファイルを選択します。

選択したファイルが画面に表示されたら「追加」を選択します。

APIキーにフィンガープリントが表示されます。

OCI CLI利用確認

設定したら確認してみましょう。
ここでは、以下のコマンドでリージョンのリストを表示します。

 
 PS C:\Windows\system32> oci iam region list --output table
 +-----+----------------+
 | key | name           |
 +-----+----------------+
 | AMS | eu-amsterdam-1 |
 | BOM | ap-mumbai-1    |
 | FRA | eu-frankfurt-1 |
 | GRU | sa-saopaulo-1  |
 | IAD | us-ashburn-1   |
 | ICN | ap-seoul-1     |
 | JED | me-jeddah-1    |
 | KIX | ap-osaka-1     |
 | LHR | uk-london-1    |
 | MEL | ap-melbourne-1 |
 | NRT | ap-tokyo-1     |
 | PHX | us-phoenix-1   |
 | SYD | ap-sydney-1    |
 | YUL | ca-montreal-1  |
 | YYZ | ca-toronto-1   |
 | ZRH | eu-zurich-1    |
 +-----+----------------+
 

まとめ

Oracle Cloudは管理コンソールからの操作がメインになりますが、Webコンソールを開いて操作ができない場合もありますよね。
OCI CLIを利用できるようにしておくと、管理コンソールへ接続せずともOracle Cloudへの仮想マシンを作成したり起動や停止の操作をすることが可能です。
コマンドをスクリプト化することで、スケジューリングによる実行も可能になります。

管理コンソールと併せてOCI CLIも利用できれば、運用も楽になるのではないでしょうか。

「実際にどのようなコマンドで操作するのか?」は今後お伝えしたいと思います。
まずは、OCI CLIを利用できる環境の設定を実施してみてください!

参考情報:コマンド・ライン・インタフェース (CLI)


執筆者情報

いとう さやね プロフィール画像

2012年アシスト入社後、Oracle Databaseフィールドエンジニアに従事。2017年からはOracle Cloudを中心としたプリセールスおよびフィールド技術を担当。 ...show more


■本記事の内容について
 本記事に示した定義及び条件は変更される場合があります。あらかじめご了承ください。

■商標に関して
 ・Oracle®、Java、MySQL及びNetSuiteは、Oracle、その子会社及び関連会社の米国及びその他の国における登録商標です。
 ・Amazon Web Services、AWS、Powered by AWS ロゴ、[およびかかる資料で使用されるその他の AWS 商標] は、Amazon.com, Inc. またはその関連会社の商標です。
  文中の社名、商品名等は各社の商標または登録商標である場合があります。

関連している記事

  • Oracle Cloud
  • Oracle Database
2024.02.02

OCIにおけるOracle Database 11g R2、12g R1、12g R2の新規プロビジョニング終了とその影響

Oracle Databaseのバージョン11g R2、12g.R1、12g.R2は既にすべてのメーカーサポートが終了しています。OCIのBase Database Serviceでも2024年1月中旬ころから11g R2、12g R1、12g R2での新規プロビジョニングができなくなりました。

  • Oracle Cloud
2024.01.19

Oracle Cloudのサービスリミットを理解し、適切に対処する方法

Oracle Cloudでクラウドアカウントごとに設定されているサービスの利用上限値「サービス制限(サービスリミット)」の引上げ方法とその注意点をご紹介します。

  • Oracle Database
  • Oracle Cloud
2023.12.15

ライセンスの観点から考えるOracle Cloudのススメ

オラクル社が提供しているクラウドサービス「Oracle Cloud」は、Oracle Databaseライセンス観点でも様々な効果があることはご存じでしょうか? ここでは「ライセンス」に焦点をあて、Oracle Cloudがおススメできるポイントを説明します。

ページの先頭へ戻る