LoginSignup
1
0

More than 1 year has passed since last update.

WindowsにOCI CLIを導入してタスクスケジューラと組み合わせてComputeの自動起動を実装してみた①

Last updated at Posted at 2022-02-17

Information
こちらの記事はOCI CLIをWindowsにインストールを実施するまでの記事となっています。
OCI CLIインストール済みの場合は以下の記事をご覧ください。
WindowsにOCI CLIを導入してタスクスケジューラと組み合わせてComputeの自動起動を実装してみた②

はじめに

Oracle Cloud上にあるコンピュートの起動/停止などの操作はWebコンソールやモバイルアプリから簡単に利用できるが、Webコンソールやモバイルアプリの場合、スケジュールを設定した状態での自動起動/自動停止は実装できません。
そのため、運用上、夜間のみ停止し、朝には起動させておきたいなどの要件がある場合は別途ユーザ側で何かしたの仕組みを実装する必要があります。
今回はOCI CLIと呼ばれるOracle Cloudのリソースを操作するコマンドをLocalのWindows端末にインストールし、パワーシェルを作成し、それをタスクスケジューラで設定し、コンピュートの自動起動/自動停止を実装し用と思います。
これを常時稼働しているWindowsServerで実行することで運用に応用も可能です。

前提 / 流れ

⓪事前に操作するComputeは作成しています。未作成の方は別途Computeの作成をお願いします。
①OCI CLIはLocalのWindows10 proにインストールします。WindowsServerも操作,コマンドは同じです。
②起動/停止を実行する.ps1ファイルを作成します。
③タスクスケジューラを設定し、自動化を実装します。

※記事が長くなったので、今回は①のOCI CLIのインストール作業のみとなります。
 OCI CLIインストール済みの場合は以下の記事をご確認ください。
 WindowsにOCI CLIを導入してタスクスケジューラと組み合わせてComputeの自動起動を実装してみた②

OCI CLIのインストール

Windows PowerShellを管理者として起動します。
image.png

スクリプト実行エラーの発生を回避するために実行ポリシーの構成を実施します。

実行コマンド結果
PS C:\windows\system32> Set-ExecutionPolicy RemoteSigned

実行ポリシーの変更
実行ポリシーは、信頼されていないスクリプトからの保護に役立ちます。実行ポリシーを変更すると、about_Execution_Policies のヘルプ トピック (https://go.microsoft.com/fwlink/?LinkID=135170)
で説明されているセキュリティ上の危険にさらされる可能性があります。実行ポリシーを変更しますか?
[Y] はい(Y)  [A] すべて続行(A)  [N] いいえ(N)  [L] すべて無視(L)  [S] 中断(S)  [?] ヘルプ (既定値は "N"): Y

PowerShellにTLS 1.2を使用するように強制するため以下コマンドを実行します。

実行コマンド
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

スクリプト・インストーラをダウンロードします。

実行コマンド
Invoke-WebRequest https://raw.githubusercontent.com/oracle/oci-cli/master/scripts/install/install.ps1 -OutFile install.ps1

デフォルト設定でインストーラ・スクリプトを実行します。これによりOCI-CLIに必要なパッケージが自動的にインストールされます。

実行コマンド
install.ps1 -AcceptAllDefaults

なお、依存関係の都合によりPythonも自動的にインストールされるため、途中でPythonのSetup画面が表示されますが、Cancelしないでください。
image.png

以下実行結果ですが、最終行に"Successfully installed OCI CLI!"が確認できれば問題ないと思います。

実行コマンド結果
PS C:\windows\system32> install.ps1 -AcceptAllDefaults
詳細: No valid Python installation found.
詳細: Downloading Python...
詳細: Download Complete! Installer executable written to: C:\Users\kazunish\AppData\Local\Temp\tmpFC38.tmp.exe
詳細: Installing Python to C:\Users\kazunish\Python...
詳細: Successfully installed Python!
詳細: Downloading install script to C:\Users\kazunish\AppData\Local\Temp\tmpBE12.tmp
詳細: C:\Users\kazunish\Python True    False
詳細: Using Python executable: C:\Users\kazunish\Python\python.exe to run install script...
詳細: Arguments to python script: "C:\Users\kazunish\AppData\Local\Temp\tmpBE12.tmp" --accept-all-defaults
-- Verifying Python version.
-- Python version 3.8.5 okay.
-- Creating directory 'C:\Users\kazunish\lib\oracle-cli'.
-- We will install at 'C:\Users\kazunish\lib\oracle-cli'.
-- The executable will be in 'C:\Users\kazunish\bin'.
-- Creating directory 'C:\Users\kazunish\bin\oci-cli-scripts'.
-- The scripts will be in 'C:\Users\kazunish\bin\oci-cli-scripts'.
-- The optional packages installed will be ''.
-- Trying to use python3 venv.
-- Executing: ['C:\\Users\\kazunish\\Python\\python.exe', '-m', 'venv', 'C:\\Users\\kazunish\\lib\\oracle-cli']
-- Executing: ['C:\\Users\\kazunish\\lib\\oracle-cli\\Scripts\\python.exe', '-m', 'pip', 'install', '--upgrade', 'pip']
Collecting pip
  Downloading pip-22.0.3-py3-none-any.whl (2.1 MB)
     |████████████████████████████████| 2.1 MB 6.4 MB/s
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 20.1.1
    Uninstalling pip-20.1.1:
      Successfully uninstalled pip-20.1.1
Successfully installed pip-22.0.3
-- Executing: ['C:\\Users\\kazunish\\lib\\oracle-cli\\Scripts\\pip', 'install', '--cache-dir', 'C:\\Users\\kazunish\\AppData\\Local\\Temp\\tmpovppc_0d', 'wheel', '--upgrade']
Collecting wheel                                                                                                                                                                                                                                                                 Downloading wheel-0.37.1-py2.py3-none-any.whl (35 kB)                                                                                                                                                                                                                        Installing collected packages: wheel                                                                                                                                                                                                                                           Successfully installed wheel-0.37.1                                                                                                                                                                                                                                            -- Executing: ['C:\\Users\\kazunish\\lib\\oracle-cli\\Scripts\\pip', 'install', '--cache-dir', 'C:\\Users\\kazunish\\AppData\\Local\\Temp\\tmpovppc_0d', 'oci_cli', '--upgrade']
Collecting oci_cli
  Downloading oci_cli-3.5.0-py3-none-any.whl (24.3 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 24.3/24.3 MB 15.2 MB/s eta 0:00:00
Collecting cryptography<=3.4.7,>=3.2.1
  Downloading cryptography-3.4.7-cp36-abi3-win_amd64.whl (1.6 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.6/1.6 MB 20.5 MB/s eta 0:00:00
Collecting click==7.1.2
  Downloading click-7.1.2-py2.py3-none-any.whl (82 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 82.8/82.8 KB ? eta 0:00:00
Collecting PyYAML<6,>=5.4
  Downloading PyYAML-5.4.1-cp38-cp38-win_amd64.whl (213 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 213.6/213.6 KB 12.7 MB/s eta 0:00:00
Collecting arrow>=1.0.0
  Downloading arrow-1.2.2-py3-none-any.whl (64 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 64.0/64.0 KB 3.4 MB/s eta 0:00:00
Collecting pyOpenSSL==19.1.0
  Downloading pyOpenSSL-19.1.0-py2.py3-none-any.whl (53 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 53.7/53.7 KB 2.9 MB/s eta 0:00:00
Collecting jmespath==0.10.0
  Downloading jmespath-0.10.0-py2.py3-none-any.whl (24 kB)
Collecting python-dateutil<3.0.0,>=2.5.3
  Downloading python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 247.7/247.7 KB 15.8 MB/s eta 0:00:00
Collecting pytz>=2016.10
  Downloading pytz-2021.3-py2.py3-none-any.whl (503 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 503.5/503.5 KB 30.8 MB/s eta 0:00:00
Collecting oci==2.56.0
  Downloading oci-2.56.0-py2.py3-none-any.whl (12.5 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 12.5/12.5 MB 18.2 MB/s eta 0:00:00
Collecting six>=1.15.0
  Downloading six-1.16.0-py2.py3-none-any.whl (11 kB)
Collecting certifi
  Downloading certifi-2021.10.8-py2.py3-none-any.whl (149 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 149.2/149.2 KB 8.7 MB/s eta 0:00:00
Collecting terminaltables==3.1.0
  Downloading terminaltables-3.1.0.tar.gz (12 kB)
  Preparing metadata (setup.py) ... done
Collecting circuitbreaker<2.0.0,>=1.3.1
  Downloading circuitbreaker-1.3.2.tar.gz (7.9 kB)
  Preparing metadata (setup.py) ... done
Collecting cffi>=1.12
  Downloading cffi-1.15.0-cp38-cp38-win_amd64.whl (179 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 180.0/180.0 KB 10.6 MB/s eta 0:00:00
Collecting pycparser
  Downloading pycparser-2.21-py2.py3-none-any.whl (118 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 118.7/118.7 KB ? eta 0:00:00
Building wheels for collected packages: terminaltables, circuitbreaker
  Building wheel for terminaltables (setup.py) ... done
  Created wheel for terminaltables: filename=terminaltables-3.1.0-py3-none-any.whl size=15355 sha256=dfddda669130c54e3916453c2f148200359b42f76ebf3b6151db1dbab853afdb
  Stored in directory: c:\users\kazunish\appdata\local\temp\tmpovppc_0d\wheels\08\8f\5f\253d0105a55bd84ee61ef0d37dbf70421e61e0cd70cef7c5e1
  Building wheel for circuitbreaker (setup.py) ... done
  Created wheel for circuitbreaker: filename=circuitbreaker-1.3.2-py3-none-any.whl size=6017 sha256=10903f380196b9d3706c53d73e69640a490cd67af11310571301e7786338d634
  Stored in directory: c:\users\kazunish\appdata\local\temp\tmpovppc_0d\wheels\97\d2\3d\8ad7bff00d846a770cdc0ed208f0fae657c983e675d658c1d5
Successfully built terminaltables circuitbreaker
Installing collected packages: terminaltables, pytz, circuitbreaker, certifi, six, PyYAML, pycparser, jmespath, click, python-dateutil, cffi, cryptography, arrow, pyOpenSSL, oci, oci_cli
Successfully installed PyYAML-5.4.1 arrow-1.2.2 certifi-2021.10.8 cffi-1.15.0 circuitbreaker-1.3.2 click-7.1.2 cryptography-3.4.7 jmespath-0.10.0 oci-2.56.0 oci_cli-3.5.0 pyOpenSSL-19.1.0 pycparser-2.21 python-dateutil-2.8.2 pytz-2021.3 six-1.16.0 terminaltables-3.1.0
--
-- ** 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 running local scripts (as an Administrator run Set-ExecutionPolicy RemoteSigned in a PowerShell prompt)
--
-- Installation successful.
-- Run the CLI with C:\Users\kazunish\bin\oci.exe --help
詳細: Successfully installed OCI CLI! 

無事にインストールが完了したことを確認するためバージョン確認を実施します。

実行コマンド結果
PS C:\windows\system32> oci -v
3.5.0

OCI CLI Config設定のための事前準備

次にインストールしたWindows端末、サーバからユーザのOCI環境にアクセスするため、APIキーの設定を実施します。
まずはOCI WebコンソールにてAPI認証に必要な以下情報を確認します。

●ユーザOCID
●テナンシOCID

image.png

テナンシOCIDをコピーして手元にメモしておきます。
image.png

ユーザOCIDをコピーして手元にメモしておきます。
image.png

OCI CLI Config設定

OCI CLIインストール後、config設定を実施します。
対話式で情報入力する流れとなります。このタイミングでユーザOCID、テナンシーOCIDが必要となるため事前にコピー&ペーストの準備をしておいてください。

実行コマンド結果
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\kazunish\.oci\config]: <configの格納場所、デフォルトで良い場合はEnter>
Enter a user OCID: <事前に確認したユーザOCIDを入力>
Enter a tenancy OCID: <事前に確認したテナンシーOCIDを入力>
Enter a region by index or name(e.g.
1: af-johannesburg-1, 2: ap-chiyoda-1, 3: ap-chuncheon-1, 4: ap-dcc-canberra-1, 5: ap-hyderabad-1,
6: ap-ibaraki-1, 7: ap-melbourne-1, 8: ap-mumbai-1, 9: ap-osaka-1, 10: ap-seoul-1,
11: ap-singapore-1, 12: ap-sydney-1, 13: ap-tokyo-1, 14: ca-montreal-1, 15: ca-toronto-1,
16: eu-amsterdam-1, 17: eu-frankfurt-1, 18: eu-marseille-1, 19: eu-milan-1, 20: eu-stockholm-1,
21: eu-zurich-1, 22: il-jerusalem-1, 23: me-abudhabi-1, 24: me-dcc-muscat-1, 25: me-dubai-1,
26: me-jeddah-1, 27: sa-santiago-1, 28: sa-saopaulo-1, 29: sa-vinhedo-1, 30: uk-cardiff-1,
31: uk-gov-cardiff-1, 32: uk-gov-london-1, 33: uk-london-1, 34: us-ashburn-1, 35: us-gov-ashburn-1,
36: us-gov-chicago-1, 37: us-gov-phoenix-1, 38: us-langley-1, 39: us-luke-1, 40: us-phoenix-1,
41: us-sanjose-1): <利用リージョンの数値を入力してEnter(東京:13 / 大阪:9)>
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]: <APIキーの新規作成の要否(用意していない場合はY)>
Enter a directory for your keys to be created [C:\Users\kazunish\.oci]:キーの格納場所、デフォルトで良い場合はEnter
Enter a name for your key [oci_api_key]:<キー名、デフォルトで良い場合はEnter>
Public key written to: C:\Users\kazunish\.oci\oci_api_key_public.pem
Enter a passphrase for your private key (empty for no passphrase):<パスワード設定、不要の場合はEnter>
Private key written to: C:\Users\kazunish\.oci\oci_api_key.pem
Fingerprint: a7:75:15:bb:9a:34:15:47:f6:e3:1a:fa:ab:29:07:aa
Config written to C:\Users\kazunish\.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

 
 
Configファイル、秘密鍵,公開鍵が作成されたことを確認します。
image.png

APIキー設定

OCI Webコンソールのユーザ詳細画面から"APIキーの追加"をクリックします。
image.png

先ほど作成した公開鍵をアップロードして"追加"をクリックします。
image.png

フィンガープリントのレコードを追加されたことを確認します。
image.png

OCI CLI動作確認

上記手順実施後に問題なくOCI CLIが使用できるか確認するため以下を実行します。

(OCIで利用できるリージョンリストの表示)

問題なく表示されればOCI CLIのインストール作業は完了となります。

コマンド実行結果
PS C:\windows\system32> oci iam region list --output table
+-----+-------------------+
| key | name              |
+-----+-------------------+
| AMS | eu-amsterdam-1    |
| ARN | eu-stockholm-1    |
| AUH | me-abudhabi-1     |
| BOM | ap-mumbai-1       |
| CWL | uk-cardiff-1      |
| DXB | me-dubai-1        |
| FRA | eu-frankfurt-1    |
| GRU | sa-saopaulo-1     |
| HYD | ap-hyderabad-1    |
| IAD | us-ashburn-1      |
| ICN | ap-seoul-1        |
| JED | me-jeddah-1       |
| JNB | af-johannesburg-1 |
| KIX | ap-osaka-1        |
| LHR | uk-london-1       |
| LIN | eu-milan-1        |
| MEL | ap-melbourne-1    |
| MRS | eu-marseille-1    |
| MTZ | il-jerusalem-1    |
| NRT | ap-tokyo-1        |
| PHX | us-phoenix-1      |
| SCL | sa-santiago-1     |
| SIN | ap-singapore-1    |
| SJC | us-sanjose-1      |
| SYD | ap-sydney-1       |
| VCP | sa-vinhedo-1      |
| YNY | ap-chuncheon-1    |
| YUL | ca-montreal-1     |
| YYZ | ca-toronto-1      |
| ZRH | eu-zurich-1       |
+-----+-------------------+

参考

OCI CLIインストールマニュアル
WindowsにOCI CLIを導入してタスクスケジューラと組み合わせてComputeの自動起動を実装してみた②

最後に

今回はWindowsにOCI CLIをインストールする手順を実施しました。
以前はPythonは自身で別途インストールしたりする必要があったのですが、現在のインストーラーはそこも含めて実施してくれるので大変簡単でスムーズに作業ができました。
次回は今回インストールしたOCI CLIを利用してComputeの自動起動/停止を実施しようと思います。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0