OCI-CLI(with jq) cook book

Oracle Cloudの操作に役立ちそうなコマンドをcook bookとしてメモ（自分用）を記載してみました。
必要に応じて少しずつ成長させていく予定です。

# 前提
以下の環境を前提としています。 （環境準備は[参考情報](https://qiita.com/T_Amakawa/private/572ef394c33d7eccd9eb#%E5%8F%82%E8%80%83%E6%83%85%E5%A0%B1)等を参照くださいませ）

- oci cliのコマンドが実行できる
  → この記事の大前提

- [fish shell](https://qiita.com/ryotako/items/31f9c9153bece58f2d98)を使っている
   → OCIcliのコマンド等の補完機能がとっても便利

- jqコマンドが実行できる
   → jsonの戻り値をハンドリングするための必須ツール

# Cook Book



## Identity and Access Management Service ( iam )

### availability domain : ドメイン一覧

```oci iam availability-domain list -c $compartment_id```

```
oci iam availability-domain list -c $compartment_id 
{
  "data": [
    {
      "compartment-id": "ocid1.compartment.oc1..******",
      "id": "ocid1.availabilitydomain.oc1..******",
      "name": "TGjA:US-ASHBURN-AD-1"
    },
    {
      "compartment-id": "ocid1.compartment.oc1..******",
      "id": "ocid1.availabilitydomain.oc1..******",
      "name": "TGjA:US-ASHBURN-AD-2"
    },
    {
      "compartment-id": "ocid1.compartment.oc1..******",
      "id": "ocid1.availabilitydomain.oc1..******",
      "name": "TGjA:US-ASHBURN-AD-3"
    }
  ]
}
```

### api-key : 情報取得

SSH用公開鍵、fingerprintを確認

``` oci iam user api-key list --user-id $user_id```


## Compute Service

### compute image : 情報取得

使用可能なcompute imageのリストを取得します。

```oci compute image list -c $compartment_id```

（例）該当するCompartmentで利用できるOracle Linux Imageの一覧取得

```
$ oci compute image list -c $compartment_id \
                          |jq -r '.data[]|select(."create-image-allowed" == true) |select(."operating-system" | contains("Linux")) | ."display-name" + "," + ."id" + "," +  ."operating-system" + "," + ."operating-system-version"'
Oracle-Linux-7.6-Gen2-GPU-2019.01.17-0,ocid1.image.oc1.iad.aaaaaaaakzhli25jz7qb7papgx7cn2ssmfq347s23uqfxwhimz7wx7ewti6q,Oracle Linux,7.6
Oracle-Linux-7.6-Gen2-GPU-2018.12.19-0,ocid1.image.oc1.iad.aaaaaaaayvmxa5eshwxn6psn2bpkuemhpo5hplhibhaw7gxeilnjdqhgdzwa,Oracle Linux,7.6
Oracle-Linux-7.6-Gen2-GPU-2018.11.19-0,ocid1.image.oc1.iad.aaaaaaaa5ghgpkurpzdybudyhghmanedksqy5k4l7txwgkdntp3q5p5xziua,Oracle Linux,7.6
Oracle-Linux-7.6-2019.01.17-0,ocid1.image.oc1.iad.aaaaaaaawufnve5jxze4xf7orejupw5iq3pms6cuadzjc7klojix6vmk42va,Oracle Linux,7.6
Oracle-Linux-7.6-2018.12.19-0,ocid1.image.oc1.iad.aaaaaaaawiur3bi46qsb6egmfqnfhsn66kj74bnvnfxrr7o72wiyuhzy2fba,Oracle Linux,7.6
Oracle-Linux-7.6-2018.11.19-0,ocid1.image.oc1.iad.aaaaaaaa2mnepqp7wn3ej2axm2nkoxwwcdwf7uc246tcltg4li67z6mktdiq,Oracle Linux,7.6
Oracle-Linux-6.10-2019.01.17-0,ocid1.image.oc1.iad.aaaaaaaal4ngds7b5pddq35wxkyacbevzyammxgr6gfab3rydsx22qer3tia,Oracle Linux,6.10
Oracle-Linux-6.10-2018.12.18-0,ocid1.image.oc1.iad.aaaaaaaaawnepxjhz4aue42hv56ydl6txdolmke4i53kiavasmnwiu6fc4gq,Oracle Linux,6.10
Oracle-Linux-6.10-2018.11.19-0,ocid1.image.oc1.iad.aaaaaaaa63dxj6hspkwqz37tajxkrpdb4xr7cdpy6dyspcufqg32m65pj4sq,Oracle Linux,6.10 
```

### Compute Instance ： Instanceの一覧取得

特定のComaprtmentに所属するCompute Instanceの一覧を取得します。

```oci compute instance list -c $compartment_id```
```oci compute instance list-vnics --instance-id $compute_id```


（例）インスタンス情報の一覧を取得

``` 
$ oci compute instance list -c $compartment_id \
                                            |jq -r '.data[]' \
                                            |jq -r '(."Domain")=(."region" + "_" + ."availability-domain" + "_" + ."fault-domain")' \
                                            |jq -r '{"Domain" : ."Domain", "display-name": ."display-name", "id": ."id", "shape" : ."shape", "lifecycle-state" : ."lifecycle-state"}'

{
  "Domain": "iad_TGjA:US-ASHBURN-AD-1_FAULT-DOMAIN-2",
  "display-name": "test01Instance0",
  "id": (instance_id),
  "shape": "VM.Standard2.2",
  "lifecycle-state": "STOPPED"
}
{
  "Domain": "iad_TGjA:US-ASHBURN-AD-2_FAULT-DOMAIN-3",
  "display-name": "instance-20190127-1249",
  "id": (instance_id),
  "shape": "VM.Standard2.2",
  "lifecycle-state": "RUNNING"
}
```

（例）インスタンスごとのVNIC一覧を取得

```
$ set -x compute_ids (oci compute instance list -c $compartment_id |jq -r '.data[]|."id"')
$ for compute_id in $compute_ids
            oci compute instance list-vnics --instance-id $compute_id \
            |jq -r '.data[]|{"hostname-label": ."hostname-label", "public-ip": ."public-ip", "private-ip": ."private-ip"}'
        end
{
  "hostname-label": "test01hst01",
  "public-ip": "x.x.x.x",
  "private-ip": "10.0.7.5"
}
{
  "hostname-label": "instance-20190127-1249",
  "public-ip": "x.x.x.x",
  "private-ip": "10.0.8.2"
}

```


### Compute Instance ： Instanceの起動

```oci compute instance action --instance-id $instance_id --action "START"```

（例）特定インスタンスを指定して起動

```
$ oci compute instance action --instance-id $instance_id --action "START" | jq '.data|{"display-name": ."display-name", "lifecycle-state": ."lifecycle-state"}' 
{
  "display-name": "instance-20190127-1249",
  "lifecycle-state": "STARTING"
}
```

（例）停止中のインスタンスをすべて起動

```
# set variables regarding all information of stopped compute instances

$ set -x stopped_instances ( \
                     oci compute instance list -c $compartment_id \
                     | jq -r '.data[]|select(."lifecycle-state" == "STOPPED")')

# Check the name of compute instance

$ echo $stopped_instances | jq -r '."display-name"'
test01Instance0
instance-20190127-1249

# set variables regarding id of stopped compute instances

$ set -x instace_ids (echo $stopped_instances | jq -r '."id"')

# stop all instances above mentioned

$ for instance_id in $instace_ids 
                         oci compute instance action --instance-id $instance_id --action "START" | jq '.data|{"display-name": ."display-name", "lifecycle-state": ."lifecycle-state"}'
                     end
{
  "display-name": "test01Instance0",
  "lifecycle-state": "STOPPING"
}
{
  "display-name": "instance-20190127-1249",
  "lifecycle-state": "STOPPING"
}

```

（例）起動ノード一括起動スクリプト

```
#!/bin/fish

set -e compute_list
set -e compute_list_stopped
set -e compute_list_displayname
set -e compute_list_id
set -e return

### Gather information about compute name and id
set -x compute_list (oci compute instance list -c $compartment_id )
set -x compute_list_stopped (echo $compute_list | jq -r '.data[]|select(."lifecycle-state" == "STOPPED")')
set -x compute_list_displayname (echo $compute_list_stopped  | jq -r '.|."display-name"')
set -x compute_list_id (echo $compute_list_stopped  | jq -r '.|."id"')
echo "compute display name is" (echo $compute_list_displayname)
echo "compute display id is" (echo $compute_list_id)

### Ask operators to see if start instances or not for just in case
echo "below is currently stopped compute list."
for compute in $compute_list_displayname; echo "$compute is stopped" ; end
read -p 'echo "are you sure to start compute instances? (y/N)"' yn
switch "$yn"
    case y Y
       echo "it'll beginning to start compute instance"
       echo $compute_list_displayname
       for compute_id in $compute_list_id; set -x return $return  (oci compute instance action --instance-id "$compute_id" --action "START"); end
       echo $return | jq -r '.data|{"display-name": ."display-name", "lifecycle-state": ."lifecycle-state"}'
    case '*'
       echo "abort"
end
```

### Compute Instance ： Instanceの停止

```oci compute instance action --instance-id $instance_id --action "SOFTSTOP"```
SOFTSTOP ・・・ shutdownコマンドによる停止

（例）特定インスタンスを指定して停止

```
$ oci compute instance action --instance-id $instance_id --action "SOFTSTOP" | jq '.data|{"display-name": ."display-name", "lifecycle-state": ."lifecycle-state"}' 
{
  "display-name": "instance-20190127-1249",
  "lifecycle-state": "STOPPING"
}
```

（例）稼働中のインスタンスをすべて停止 

```
# set variables regarding all information of running compute instances

$ set -x running_instances ( \
                     oci compute instance list -c $compartment_id \
                     | jq -r '.data[]|select(."lifecycle-state" == "RUNNING")')

# Check the name of compute instance

$ echo $running_instances | jq -r '."display-name"'
test01Instance0
instance-20190127-1249

# set variables regarding id of running compute instances

$ set -x instace_ids (echo $running_instances | jq -r '."id"')

# stop all instances above men

$ for instance_id in $instace_ids 
                         oci compute instance action --instance-id $instance_id --action "SOFTSTOP" | jq '.data|{"display-name": ."display-name", "lifecycle-state": ."lifecycle-state"}'
                     end
{
  "display-name": "test01Instance0",
  "lifecycle-state": "STOPPING"
}
{
  "display-name": "instance-20190127-1249",
  "lifecycle-state": "STOPPING"
}

```
（例）起動ノード一括停止スクリプト

```
#!/bin/fish

set -e compute_list
set -e compute_list_stopped
set -e compute_list_displayname
set -e compute_list_id
set -e return

### Gather information about compute name and id
set -x compute_list (oci compute instance list -c $compartment_id )
set -x compute_list_running (echo $compute_list | jq -r '.data[]|select(."lifecycle-state" == "RUNNING")')
set -x compute_list_displayname (echo $compute_list_running  | jq -r '.|."display-name"')
set -x compute_list_id (echo $compute_list_running  | jq -r '.|."id"')
echo "compute display name is" (echo $compute_list_displayname)
echo "compute display id is" (echo $compute_list_id)

### Ask operators to see if stop instances or not for just in case
echo "below is currently running compute list."
for compute in $compute_list_displayname; echo "$compute is running" ; end
read -p 'echo "are you sure to shutdown compute instances? (y/N)"' yn
switch "$yn"
    case y Y
       echo "it'll start stopping compute node"
       echo $compute_list_displayname
       for compute_id in $compute_list_id; set -x return $return  (oci compute instance action --instance-id "$compute_id" --action "SOFTSTOP"); end
       echo $return | jq -r '.data|{"display-name": ."display-name", "lifecycle-state": ."lifecycle-state"}'
    case '*'
       echo "abort"
end
```


## Network
### VCN：情報取得

特定のcompartmentに属するVCNの一覧（表示名、vcn-id、CIDR）

```
$ oci network vcn list -c $comaprment_id | \
jq -r '.data[] | select(."lifecycle-state" == "AVAILABLE") | ."display-name" + "\t" + ."id" + "\t" + ."cidr-block"'
test02VCN	ocid1.vcn.oc1.iad.xxxxxx	10.0.0.0/16
test01VCN	ocid1.vcn.oc1.iad.xxxxxx 10.0.0/16
```
特定のcompartmentに属するVCNの一覧（JSON出力：vcn-id、表示名、domain-name、CIDR、状態）

```
oci network vcn list -c ocid1.compartment.xxxxxxx  | \
jq '[.data[] | {"vcnid" : ."id","display-name" : ."display-name", "domain_name" : ."vcn-domain-name", "cidr-block" : ."cidr-block" , "lifecycle-state" : ."lifecycle-state"} ]| select(."display-name" == "test01VCN")'
[
{
  "vcnid": "ocid1.vcn.oc1.iad.xxxxxxxx",
  "display-name": "test01VCN",
  "domain_name": "xxxx.oraclevcn.com",
  "cidr-block": "10.0.0.0/16",
  "lifecycle-state": "AVAILABLE"
}
]
```

Global-ip address割当情報取得

```
oci network virtual-circuit-public-prefix list --virtual-circuit-id $vcn-id
```


### Subnet：情報取得
特定のVCNに属するsubnet一覧（表示名、SubnetID、AD、CIDR）
※ 表示名で「Sort」＆「特定の文字列（Bastion）を含むものに限定」

```
$ oci network subnet list -c $comaprment_id  --vcn-id $vcn-id | \
jq -r '.data[] | {"display-name" : ."display-name", "id" : ."id", "availability-domain" : ."availability-domain",  "cidr-block" : ."cidr-block"} |select(."display-name" |contains("Bastion"))' | \
jq -s 'sort_by(."display-name")'
[
  {
    "display-name": "BastionSubnetAD1",
    "id": ($subnet_id)
    "availability-domain": "TGjA:US-ASHBURN-AD-1",
    "cidr-block": "10.0.7.0/24"
  }
]

```

### Security Filter ( Firewall )：情報取得

```oci network security-list list -c $compartment_id -vcn-id $vcn-id```

表示するSecurityList(display-nameとID)の一覧を取得

```
$ oci network security-list list -c $compartment_id -vcn-id $vcn-id \ 
  jq -r '.data[] |."display-name" + "\t" +  ."id"'
Bastion	($security_list_id)
```

```oci network security-list get --security-list-id "hoge" ```
該当するIDを条件としてSecurity-listの情報を取得

```
$ oci network security-list get --security-list-id "hoge" \
  | jq '.data[]|select(."id" == "hoge")|del(."compartment-id")|del(."id")|del(."vcn-id")'"
  "defined-tags": {},
  "display-name": "Default Security List for test01VCN",
  "egress-security-rules": [
    {
      "destination": "0.0.0.0/0",
      "destination-type": "CIDR_BLOCK",
      "icmp-options": null,
      "is-stateless": false,
      "protocol": "all",
      "tcp-options": null,
      "udp-options": null
    }
  ],
  "freeform-tags": {},
  "ingress-security-rules": [
    {
      "icmp-options": null,
      "is-stateless": false,
      "protocol": "6",
      "source": "0.0.0.0/0",
      "source-type": "CIDR_BLOCK",
      "tcp-options": {
        "destination-port-range": {
          "max": 22,
          "min": 22
        },
        "source-port-range": null
      },
      "udp-options": null
    },
    {
      "icmp-options": {
        "code": 4,
        "type": 3
      },
      "is-stateless": false,
      "protocol": "1",
      "source": "0.0.0.0/0",
      "source-type": "CIDR_BLOCK",
      "tcp-options": null,
      "udp-options": null
    },
    {
      "icmp-options": {
        "code": null,
        "type": 3
      },
      "is-stateless": false,
      "protocol": "1",
      "source": "10.0.0.0/16",
      "source-type": "CIDR_BLOCK",
      "tcp-options": null,
      "udp-options": null
    }
  ],
  "lifecycle-state": "AVAILABLE",
  "time-created": "2019-01-24T02:04:16.671000+00:00"
}
```

### Security Filter ( Firewall )：設定（リスト変更）
※※※
*Security-listのegress-security-rules or ingress-security-rules単位での変更となります。
１エントリだけの変更はできないため、事前に既存のリストを取得 → 変更する箇所を変更 → 反映の手順が必要になります。*
※※※

```
$ oci network security-list update --security-list-id $security_list_id --egress-security-rules \
                        '[
                                {
                                  "destination": "0.0.0.0/0",
                                  "destination-type": "CIDR_BLOCK",
                                  "icmp-options": null,
                                  "is-stateless": false,
                                  "protocol": "6",
                                  "tcp-options": null,
                                  "udp-options": null
                                },
                                {
                                  "destination": "oci-iad-objectstorage",
                                  "destination-type": "SERVICE_CIDR_BLOCK",
                                  "icmp-options": null,
                                  "is-stateless": false,
                                  "protocol": "all",
                                  "tcp-options": null,
                                  "udp-options": null
                                }
                          ]'
```



## Database(DBaaS)

### DBaaS: DB Shapeの一覧を取得

```oci db system-shape list --availability-domain $ad_names[1] -c $compartment_id```

（例）取得可能なshape名を取得

```
$ oci db system-shape list --availability-domain $ad_names[1] -c $compartment_id | jq -r '.data[]|."shape"'
VM.Standard1.4
VM.Standard2.2
BM.DenseIO1.36
VM.Standard1.8
Exadata.Full1.336
VM.Standard2.1
VM.Standard2.8
BM.DenseIO2.52
Exadata.Quarter1.84
VM.Standard1.2
Exadata.Quarter2.92
Exadata.Full2.368
VM.Standard2.24
VM.Standard1.16
Exadata.Half2.184
Exadata.Half1.168
VM.Standard1.1
VM.Standard2.4
VM.Standard2.16
```

### DBaaS: DB Versionの一覧を取得

```oci db version list -c $comartment_id```

```
$ oci db version list -c $comartment_id
{
  "data": [
    {
      "is-latest-for-major-version": true,
      "supports-pdb": false,
      "version": "11.2.0.4"
    },
    {
      "is-latest-for-major-version": true,
      "supports-pdb": true,
      "version": "12.1.0.2"
    }
  ]
}
```


### DBaaS: DBaaSの一覧を取得

```oci db system list -c $compartment_id ```

（例）一般的によく参照される情報を収集

```
$ oci db system list -c $compartment_id \
             |jq -r '.data[]' \
             |jq -r '{"display-name": ."display-name", "id": ."id", "FQDN": (."hostname" + "." + ."domain"), "lifecycle-state": ."lifecycle-state", "shape": ."shape", "database-edition": ."database-edition", "version": ."version" }'
{
  "display-name": "DBforODI",
  "id": "ocid1.dbsystem.oc1.iad.******",
  "FQDN": "hoge.oraclevcn.com",
  "lifecycle-state": "AVAILABLE",
  "shape": "VM.Standard2.2",
  "database-edition": "ENTERPRISE_EDITION",
  "version": "12.2.0.1.180417"
}
```

### DBaaS: Databaseの情報収集

```oci db database list -c $compartment_id --db-system-id $db_system_id```

```
$ oci db database list -c $compartment_id --db-system-id $db_system_id 
{
  "data": [
    {
      "character-set": "AL32UTF8",
      "compartment-id": "ocid1.compartment.oc1..******",
      "db-backup-config": {
        "auto-backup-enabled": false
      },
      "db-home-id": "ocid1.dbhome.oc1.iad.******",
      "db-name": "myODIDB",
      "db-unique-name": "myODIDB_iad1cx",
      "db-workload": "OLTP",
      "defined-tags": {},
      "freeform-tags": {},
      "id": "ocid1.database.oc1.iad.******",
      "lifecycle-details": null,
      "lifecycle-state": "AVAILABLE",
      "ncharacter-set": "AL16UTF16",
      "pdb-name": "pdb1",
      "time-created": "2019-01-24T08:30:20.937000+00:00"
    }
  ]
}
```
### DBaaS: Database Nodeの情報収集

```oci db node list -c $compartment_id --db-system-id $db_system_id```

```
$ oci db node list -c $compartment_id --db-system-id $db_system_id 
{
  "data": [
    {
      "backup-vnic-id": null,
      "db-system-id": "ocid1.dbsystem.oc1.iad.******",
      "hostname": "db01forodi01",
      "id": "ocid1.dbnode.oc1.iad.******",
      "lifecycle-state": "AVAILABLE",
      "software-storage-size-in-gb": 200,
      "time-created": "2019-01-24T08:30:20.936000+00:00",
      "vnic-id": "ocid1.vnic.oc1.iad.abuwc******"
    }
  ]
}
```

### DBaaS: DBaaSの接続情報を取得

```
set -x db_node_id (oci db node list -c $compartment_id --db-system-id $db_system_id | jq -r '.data[]|."id"')
set -x vnic_id (oci db node list -c $compartment_id --db-system-id $db_system_id | jq -r '.data[]|."vnic-id"')
set -x domain_name (oci db system list -c $compartment_id |jq -r '.data[]|."domain"')
set -x pdb_name (oci db database list -c $compartment_id --db-system-id $db_system_id | jq -r '.data[]|."pdb-name"')
set -x db_name (oci db database list -c $compartment_id --db-system-id $db_system_id | jq -r '.data[]|."db-name"')
set -x global_ip (oci network vnic get --vnic-id $vnic_id |jq -r '.data|."public-ip"')

echo DB_name : $db_name\n\
Service Name : $pdb_name.$domain_name\n\
Global IP : $global_ip
    
DB_name : myODIDB 
 Service Name : xxx.xxx.oraclevcn.com 
 Global IP : xxx.xxx.xxx.xxx

```

### DBaaS: DB nodeの停止

（例）db_node_idを指定してノードを停止

```
$ oci db node stop --db-node-id $db_node_id --wait-for-state STOPPED
```

（例）可動しているDBを一括停止
db_sytem_idを取得 → 取得したidを使ってDB nodeを停止
※ 複数DBでの検証は未実施

```
# make sure if running db system's are the right ones or not.
oci db system list -c $compartment_id | jq -r '.data[]|select(."lifecycle-state" == "AVAILABLE")|."hostname"'

# get db system ids
set -x db_system_ids (oci db system list -c $compartment_id | jq -r '.data[]|select(."lifecycle-state" == "AVAILABLE")|."id"')

# make sure if running db node's hostname are the right ones or not.
oci db node list -c $compartment_id --db-system-id $db_system_id | jq -r '.data[]|select(."lifecycle-state" == "AVAILABLE")|."hostname"'

# get db node ids
set -x db_node_ids \
                     (for db_system_id in $db_system_ids ; oci db node list -c $compartment_id --db-system-id $db_system_id | jq -r '.data[]|select(."lifecycle-state" == "AVAILABLE")|."id"' ;end)

# stop db node ids by using the ids gotten above
for db_node_id in $db_node_ids; oci db node stop --db-node-id $db_node_id --wait-for-state STOPPED ;end
```

### DBaaS: DB nodeの起動

（例）db_node_idを指定してノードを起動

```
oci db node start --db-node-id $db_node_id --wait-for-state AVAILABLE
```

（例）停止しているDBを一括起動

```
# make sure if stopped db system's are the right ones or not.
$ oci db system list -c $compartment_id | jq -r '.data[]|select(."lifecycle-state" == "AVAILABLE")|."hostname"'

# get db system ids
set -x db_system_ids (oci db system list -c $compartment_id | jq -r '.data[]|select(."lifecycle-state" == "AVAILABLE")|."id"')

# make sure if running db node's hostname are the right ones or not.
oci db node list -c $compartment_id --db-system-id $db_system_id | jq -r '.data[]|select(."lifecycle-state" == "STOPPED")|."hostname"'

# get db node ids
set -x db_node_ids \
                     (for db_system_id in $db_system_ids ; oci db node list -c $compartment_id --db-system-id $db_system_id | jq -r '.data[]|select(."lifecycle-state" == "STOPPED")|."id"' ;end)

# stop db node ids by using the ids gotten above
for db_node_id in $db_node_ids; oci db node start --db-node-id $db_node_id --wait-for-state AVAILABLE ;end
```


## Database(ADWC)

### ADWC: ADWCの一覧を取得

```oci db autonomous-data-warehouse list -c $compartment_id```

（例）一般的によく参照される情報を収集

```
$ oci db autonomous-data-warehouse list -c $compartment_id \
|jq -r '.data[]|{"db-name": ."db-name", "lifecycle-state": ."lifecycle-state", "id": ."id", "connection-strings": ."connection-strings", "db-version": ."db-version", "service-console-url": ."service-console-url"}'
{
  "db-name": "tamakawa4oac1",
  "lifecycle-state": "AVAILABLE",
  "id": "ocid1.autonomousdwdatabase.oc1.iad.*****",
  "connection-strings": {
    "all-connection-strings": {
      "HIGH": "adb.us-ashburn-1.oraclecloud.com:1522/*****_tamakawa4oac1_high.adwc.oraclecloud.com",
      "LOW": "adb.us-ashburn-1.oraclecloud.com:1522/*****_tamakawa4oac1_low.adwc.oraclecloud.com",
      "MEDIUM": "adb.us-ashburn-1.oraclecloud.com:1522/*****_tamakawa4oac1_medium.adwc.oraclecloud.com"
    },
    "high": "adb.us-ashburn-1.oraclecloud.com:1522/*****_tamakawa4oac1_high.adwc.oraclecloud.com",
    "low": "adb.us-ashburn-1.oraclecloud.com:1522/*****_tamakawa4oac1_low.adwc.oraclecloud.com",
    "medium": "adb.us-ashburn-1.oraclecloud.com:1522/*****_tamakawa4oac1_medium.adwc.oraclecloud.com"
  },
  "db-version": "18.4.0.0",
  "service-console-url": "https://adb.us-ashburn-1.oraclecloud.com/console/index.html?tenant_name=OCID1.TENANCY.OC1..*****"
}

```

### ADWC: 指定したADWCインスタンスのコネクション情報の取得

```oci db autonomous-data-warehouse get --autonomous-data-warehouse-id $adwc_id \
|jq -r '.data|{"service-console-url": ."service-console-url"}' ```

（例）

```
$ oci db autonomous-data-warehouse get --autonomous-data-warehouse-id $adwc_id \
                     |jq -r '.data|{"service-console-url": ."service-console-url", "connection-strings": ."connection-strings"."all-connection-strings"}'
{
  "service-console-url": "https://adb.us-ashburn-1.oraclecloud.com/console/index.html?tenant_name=OCID1.TENANCY.OC1..*****",
  "connection-strings": {
    "HIGH": "adb.us-ashburn-1.oraclecloud.com:1522/*****_adwdb1_high.adwc.oraclecloud.com",
    "LOW": "adb.us-ashburn-1.oraclecloud.com:1522/*****_adwdb1_low.adwc.oraclecloud.com",
    "MEDIUM": "adb.us-ashburn-1.oraclecloud.com:1522/*****_adwdb1_medium.adwc.oraclecloud.com"
  }
}
```

### ADWC: DB作成

```oci db autonomous-data-warehouse create -c $compartment_id  --db-name $db_name --display-name $db_name --admin-password $adwc_admin_password --cpu-core-count $cpu_core_count --data-storage-size-in-tbs $storage_size_in_tb```

（例）以下前提でADWCを作成
CPU Core ： 2
Storage size : 1(TB)

```
 $ oci db autonomous-data-warehouse create -c $compartment_id  --db-name $db_name --display-name $db_name --admin-password $adwc_admin_password --cpu-core-count $cpu_core_count --data-storage-size-in-tbs $storage_size_in_tb
{
  "data": {
    "compartment-id": "ocid1.compartment.oc1..*****",
    "connection-strings": null,
    "cpu-core-count": 2,
    "data-storage-size-in-tbs": 1,
    "db-name": "tamakawa4oac1",
    "db-version": null,
    "defined-tags": {},
    "display-name": "tamakawa4oac1",
    "freeform-tags": {},
    "id": "ocid1.autonomousdwdatabase.oc1.iad.*****",
    "license-model": "BRING_YOUR_OWN_LICENSE",
    "lifecycle-details": null,
    "lifecycle-state": "PROVISIONING",
    "service-console-url": null,
    "time-created": "2019-01-30T07:22:57.821000+00:00"
  },
  "etag": "*****"
}
```

### ADWC: Credentialファイルの取得

```oci db autonomous-data-warehouse generate-wallet --autonomous-data-warehouse-id $adwc_id --password $adwc_admin_password --file "wallet_adwc.zip"```


### ADWC: DBの停止

```oci db autonomous-data-warehouse stop --autonomous-data-warehouse-id $adwc_id ```

（例）稼働中のADWC IDを取得 → 一括停止

稼働中のADWCを確認

```
$ oci db autonomous-data-warehouse list -c $compartment_id \
                     |jq '.data[]' \
                     |jq 'select(."lifecycle-state" == "AVAILABLE")' \
                     |jq -r '{"display-name": ."display-name", "lifecycle-state": ."lifecycle-state"}'
```

IDを変数に代入

```
$ set -x LIST_ADWC_ID (oci db autonomous-data-warehouse list -c $compartment_id \
                     |jq '.data[]' \
                     |jq 'select(."lifecycle-state" == "AVAILABLE")' \
                     |jq -r '."id"' ) 
```

DB停止

```
$ for i in $LIST_ADWC_ID; oci db autonomous-data-warehouse stop --autonomous-data-warehouse-id $i; end
```


### ADWC：DBの起動

```oci db autonomous-data-warehouse start --autonomous-data-warehouse-id $adwc_id ```

（例）停止中のADWC IDを取得 → 一括起動

停止中のADWCを確認

```
$ oci db autonomous-data-warehouse list -c $compartment_id \
                     |jq '.data[]' \
                     |jq 'select(."lifecycle-state" == "STOPPED")' \
                     |jq -r '{"display-name": ."display-name", "lifecycle-state": ."lifecycle-state"}'
{
  "display-name": "tamakawa4oac1",
  "lifecycle-state": "STOPPED"
}
{
  "display-name": "example_autonomous_data_warehouse",
  "lifecycle-state": "STOPPED"
}
```

IDを変数に代入

```
$ set -x LIST_ADWC_ID (oci db autonomous-data-warehouse list -c $compartment_id \
                     |jq '.data[]' \
                     |jq 'select(."lifecycle-state" == "STOPPED")' \
                     |jq -r '."id"' ) 
```

DB起動

```
$ for i in $LIST_ADWC_ID; oci db autonomous-data-warehouse start --autonomous-data-warehouse-id $i; end
```




## Object Storage

### bucket：一覧の取得

```oci os bucket list -c $compartment_id```

（例）bucket一覧の取得

```
oci os bucket list -c $compartment_id | jq -r '.data[]|."name"'
test01-odics01
test01-odics02
test01Bucket01
test01bdc01
test01oac01
```
### bucket：bucketの作成

```oci os bucket create -c $compartment_id --name "hoge (options)"```
options
- --public-access-type [**NoPublicAccess**|ObjectRead|ObjectReadWithoutList]
- --storage-tier [**Standard**|Archive]

（例）特定のcompartmentにbucketを作成

```
oci os bucket delete -c $compartment_id --name "hoge"
```

### bucket：bucketの削除

```oci os bucket delete --name "hoge"```

```
$ oci os bucket delete --name "hoge"
Are you sure you want to delete this resource? [y/N]: y
```

### Object: オブジェクトの一覧取得

``` oci os object list -bn $bucket_name```

```
oci os object list -bn $bucket_name
{
  "data": [
    {
      "md5": "Av+EbU/sV6TkwDYjN7qVWA==",
      "name": "start_stopped_compute.sh",
      "size": 1297,
      "time-created": "2019-01-30T04:30:27.915000+00:00"
    },
    {
      "md5": "9t0j9SODIFVAIEi09jAUTw==",
      "name": "stop_running_compute.sh",
      "size": 1298,
      "time-created": "2019-01-30T04:30:28.330000+00:00"
    },
  ],
  "prefixes": []
}

```

### Object: ファイルアップロード （from a file)

``` oci os object put -bn $bucket_name --file[text] [options]```
options
- --overwite/**--no-overwrite** : 
- --parallel-upload-count[int] : the number of parallel operations to perform

```
oci os object put -bn $bucket_name --file date.txt
```

### Object: ファイルアップロード （from stdin)

``` oci os object put -bn $bucket_name --file '-' --name[text] [options]```
options
- --overwite/**--no-overwrite** : 
- --parallel-upload-count[int] : the number of parallel operations to perform

```
$ cat date.txt | oci os object put -bn $bucket_name --file '-' --name "date_new.txt"
Uploading object part  [####################################]  100%
{
  "etag": "fd6d011e-f9c0-4b4a-be15-2cc3fc0ad906",
  "last-modified": "Wed, 30 Jan 2019 04:43:01 GMT",
  "opc-multipart-md5": "PlO67BWCIqXExgbxEsjcpQ==-1"
}
```

### Object: ファイルアップロード （bulk)

``` oci os object bulk-upload -bn $bucket_name --src-dir[text] [options]```
options
- --overwite/**--no-overwrite** : 
- --parallel-upload-count[int] : the number of parallel operations to perform
- --include[text] : only upload files which match the provided pattern. *:match everything , ?: match any single character
- --exclude[text] : only upload files which don't match the provided pattern

（例）カレンドディレクトリのファイルをアップロード、ただしファイル名にstartが含まれるものは除外

```
$ ls
list_compute_id_and_displayname.sh start_stopped_compute.sh
list_running_compute_id.sh         stop_running_compute.sh

$ oci os object bulk-upload -bn $bucket_name --src-dir . --exclude "start*" --parallel-upload-count 2 --overwrite
Uploaded stop_running_compute.sh  [####################################]  100%           
{
  "skipped-objects": [],
  "upload-failures": {},
  "uploaded-objects": {
    "list_compute_id_and_displayname.sh": {
      "etag": "cb6531e5-fa8b-4b1b-8656-1abecc682bc3",
      "last-modified": "Wed, 30 Jan 2019 04:31:18 GMT",
      "opc-content-md5": "obuhDwFW8Q24neVWCMd4Xg=="
    },
    "list_running_compute_id.sh": {
      "etag": "8e9c946e-2f8e-4c89-9998-b46abaf081dc",
      "last-modified": "Wed, 30 Jan 2019 04:31:18 GMT",
      "opc-content-md5": "8daBM37b5l+IV2kHMGlQeQ=="
    },
    "stop_running_compute.sh": {
      "etag": "a271fe13-69e7-4a54-8c5a-2c86c04c3aea",
      "last-modified": "Wed, 30 Jan 2019 04:31:18 GMT",
      "opc-content-md5": "9t0j9SODIFVAIEi09jAUTw=="
    }
  }
}
```

### Object: ダウンロード （to file)

``` oci os object get -bn $bucket_name --name "foo.txt" --file "baa.txt" [text] [options]```
options
- --multipart-download-threshold[int] : Objects larget than the size(in MiB) will be downloaded in multiple parts / the minimum allowable threshold is 128MiB
- --parallel-download-count[int] : the number of parallel operations to perform

```
oci os object get -bn $bucket_name --name date.txt --file date2.txt
Downloading object  [####################################]  100%
``` 
### Object: ダウンロード （to stdout)

``` oci os object get -bn $bucket_name --name "foo.txt" --file '-' [text] [options]```
options
- --multipart-download-threshold[int] : Objects larget than the size(in MiB) will be downloaded in multiple parts / the minimum allowable threshold is 128MiB
- --parallel-download-count[int] : the number of parallel operations to perform

```
$ oci os object get -bn $bucket_name --name date.txt --file '-'
Wed Jan 30 13:39:36 JST 2019
```  

### Object: ダウンロード （bulk-download)

``` oci os object bulk-download -bn $bucket_name --download-dir "download_dir" [options]```
[options]
- --overwite/**--no-overwrite** : 
- --parallel-download-count[int] : the number of parallel operations to perform
- --include[text] : only upload files which match the provided pattern. *:match everything , ?: match any single character
- --exclude[text] : only upload files which don't match the provided pattern

（例）拡張子「.dat」をもったファイルをカレンドディレクトリにダウンロードする

```
$ oci os object bulk-download -bn $bucket_name --include "*.dat" --download-dir "."
Downloaded sale1v3.dat  [####################################]  100% 
{
  "download-failures": {},
  "skipped-objects": []
}
```

### Object: 削除

``` oci os object delete -bn $bucket_name --name "foo"  [options]```

``` oci os object bulk-delete -bn $bucket_name  [options]```
[options]

- --include[text] : only delete objects which match the provided patter.
- --dry-run
- --force

（例）拡張子".dat"を持ったオブジェクトを一括削除

```
$ oci os object bulk-delete -bn $bucket_name --include "*.dat" --dry-run
{
  "delete-failures": {},
  "deleted-objects": [
    "chan_v3.dat",
    "costs.dat",
    "coun_v3.dat",
    "cust1v3.dat",
    "dem1v3.dat",
    "dmsal_v3.dat",
    "prod1v3.dat",
    "prom1v3.dat",
    "sale1v3.dat",
    "time_v3.dat"
  ]
}
$ oci os object bulk-delete -bn $bucket_name --include "*.dat"
WARNING: This command will delete all matching objects in the bucket. Please use --dry-run to list the objects which would be deleted. Are you sure you wish to continue? [y/N]: y
Deleted cust1v3.dat  [####################################]  100% 
{
  "delete-failures": {},
  "deleted-objects": [
    "costs.dat",
    "prod1v3.dat",
    "chan_v3.dat",
    "time_v3.dat",
    "dmsal_v3.dat",
    "dem1v3.dat",
    "prom1v3.dat",
    "sale1v3.dat",
    "coun_v3.dat",
    "cust1v3.dat"
  ]
}
```

## File Storage

(近いうちに記載予定：たぶん)

## Container Engine for Kubernates

(近いうちに記載予定：もしかしたら先になるかも)

# Tips
## OCI tips

### SHELL変数取得script

前提 ： 以下の変数がセット済み

- $compartment_id
- $nw_comparment_id ※ 上記と同じ場合あり

```
# iam
set -x ad_names (oci iam availability-domain list -c $compartment_id| jq -r '.data[]|."name"')
set -x ad_ids (oci iam availability-domain list -c $compartment_id| jq -r '.data[]|."id"')

# compute 

set -x compute_id \
      (oci compute instance list -c $compartment_id \
             | jq -r '.data[]' \
             | jq -r 'select(."display-name" == "instance-20190127-1249")' \
             | jq -r '."id"')

# network ( might need to change $compartment_id for network one )
set -x vcn_id \
         (oci network vcn list -c $nw_compartment_id  \
            | jq -r '.data[]' \
            | jq -r 'select(."display-name" == "test01VCN")' \
            | jq -r '."id"' ) 

set -x subnet_id \
         (oci network subnet list -c $nw_compartment_id --vcn-id $vcn_id \
            | jq -r '.data[]' \
            | jq -r 'select(."display-name" == "BastionSubnetAD3")' \
            | jq -r '."id"' )

# DBaaS
set -x db_system_id \
           (oci db system list -c $compartment_id \
                   | jq -r '.data[]' \
                   | jq -r 'select(."display-name" == "DBforODI")' \
                   | jq -r '."id"')

set -x db_id \
           (oci db database list -c $compartment_id --db-system-id $db_system_id \
                   | jq -r '.data[]' \
                   | jq -r 'select(."db-name" == "myODIDB")' \
                   | jq -r '."id"')

set -x db_node_id \
         (oci db node list -c $compartment_id --db-system-id $db_system_id \
                   | jq -r '.data[]' \
                   | jq -r 'select(."hostname" == "db01forodi01")' \
                   | jq -r '."id"')

# ADWC
set -x adwc_id \
           (oci db autonomous-data-warehouse list -c $compartment_id \
                     |jq -r '.data[]' \
                     |jq -r 'select(."db-name" == "adwdb1")' \
                     |jq -r '."id"' )

```



## JQ Tips

### Fileter処理

#### like検索
"select ... "を以下のようにlike検索（あいまい検索）に変更することも可能
select( .foo == "baa" ) => select ( .foo | contains("ba"))
↓ （例）

```
oci network vcn list -c $comaprment_id  | \
jq '.data[] | {"vcnid" : ."id","display-name" : ."display-name", "domain_name" : ."vcn-domain-name", "cidr-block" : ."cidr-block" , "lifecycle-state" : ."lifecycle-state"} | select(."display-name" | contains("test01"))'
{
  "vcnid": "($vcn-id)",
  "display-name": "test01VCN",
  "domain_name": "xxxxx.oraclevcn.com",
  "cidr-block": "10.0.0.0/16",
  "lifecycle-state": "AVAILABLE"
}
```

#### 特定のValueをもったKeyを削除 ```map_values```

（例）Valueが（null or false）のkeyを削除
```jq '.|map_values(select(. != false)) | map_values(select(. != null))'```

#### key/value追加  : ```| . + {"key": "value"}```
 
（例）コメントエントリーを追加

```
$ oci os bucket list -c $compartment_id | jq -r '.data[]| . + {"COMMENT": ."This is just for sample"}'
{
(略)
  "time-created": "2019-01-23T12:23:14.180000+00:00",
  "COMMENT": "This is just for sample"
}
```



#### ネストの展開（CSV準備等？）: ```| . + ."key" | del(."key") ```

before
```
jq '. + ."tcp-options" | . + ."destination-port-range" + ."source-port-range" | del(."tcp-options") | del(."destination-port-range") 
```

after

```
| jq '. + ."tcp-options" | . + . "destination-port-range" |del(."tcp-options") | del(."destination-port-range")'
{
  "icmp-options": null,
  "is-stateless": false,
  "protocol": "6",
  "source": "0.0.0.0/0",
  "source-type": "CIDR_BLOCK",
  "udp-options": null,
  "source-port-range": null,
  "max": 22,
  "min": 22
}
```


### 出力形式

以下のような出力形式が可能

#### コンパクト表示 : ``` jq -c '.' ```

```
oci network vcn list -c ocid1.compartment.oc1..xxxxxxxxxx  | jq -c '.data[] | {"vcnid" : ."id","display-name" : ."display-name", "domain_name" : ."vcn-domain-name", "cidr-block" : ."cidr-block" , "lifecycle-state" : ."lifecycle-state"} | select(."display-name" == "test01VCN")'
{"vcnid":"ocid1.vcn.oc1.iad.xxxxxxxxxx","display-name":"test01VCN","domain_name":"xxxxxx.oraclevcn.com","cidr-block":"10.0.0.0/16","lifecycle-state":"AVAILABLE"}
```

#### CSV表示 : ``` jq -c '.[] | ([."foo", ."baa"]) | @csv ' ```

```
oci network vcn list -c ocid1.compartment.oc1..xxxxxxxxxx | jq -r '["display-name","lifecycle-state","cidr-block" ],(.data[] | [."display-name", ."lifecycle-state", ."cidr-block"]) | @csv'
"display-name","lifecycle-state","cidr-block"
"test_vcn2","AVAILABLE","10.0.0.0/16"
"test01VCN","AVAILABLE","10.0.0.0/16"
```


#### Valueの連結（？）
    

``` jq -c '.data[] | ."displayname" | ."cidr-block" | ."lifecycle-state"' ```

```
$ oci network vcn list '.data[] | ."displayname" | ."cidr-block" | ."lifecycle-state"'
```

#### 特定のValueを置換 ：```| (."foo") |= "baa"```

（例）特定の日付より前に作られたエントリーの"name"をキーとしたvalueを置換

```
$ oci os bucket list -c $compartment_id | \ 
jq -r '.data[]|. + {"date-created": ."time-created" | split("T")[0]}|{"name": ."name", "time-creatd": ."time-created", "date-created": ."date-created"} | (select(."date-created" < "2019-01-22") | ."name") |= "plan to delete soon" '
{
  "name": "plan to delete soon",
  "time-creatd": "2019-01-21T02:35:11.208000+00:00",
  "date-created": "2019-01-21"
}
{
  "name": "test01oac01",
  "time-creatd": "2019-01-23T12:23:14.180000+00:00",
  "date-created": "2019-01-23"
}
```

#### 文字列の分割（Split）: ``` jq -c '.data[] | ."abc-def" | split ("-")'```

```
$ oci os bucket list -c $compartment_id | \ 
jq -r '.data[]|. + {"date-created": ."time-created" | split("T")[0]}|{"name": ."name", "time-creatd": ."time-created", "date-created": ."date-created"} '
{
  "name": "test01-odics01",
  "time-creatd": "2019-01-21T02:35:11.208000+00:00",
  "date-created": "2019-01-21"
}
```

#### ダブルクォーテーション外し : ```jq -r```

```
$ oci network vcn list | jq '.data[]."vcn-domain-name" | split(".") | .[0]'
test01vcn
```


### その他の関数
#### ソート : ```jq -s```
※配列に対するソートが前提
```
oci network vcn list  | jq '."data" | sort_by(."display-name")'
```
↑ 配列で無いデータを扱うと以下のエラーがでてしまう。
jq: error (at <stdin>:34): Cannot index string with string "display-name"

*こんなときは、-s/--slurpで配列化してからSortすればOK*

#参考情報
### OCI
[Oracle Cloud Infrastructure Documentation(Official):CLI](https://docs.cloud.oracle.com/iaas/Content/API/Concepts/cliconcepts.htm)
[Oracle Cloud Infrastructure CLI Command Reference(Official)](https://docs.cloud.oracle.com/iaas/tools/oci-cli/latest/oci_cli_docs/)
[Oracle Cloud OCI IaaS関連情報まとめ](https://qiita.com/feifo/items/551a8a4250f83e4c33c3)
[コマンドライン(CLI)でOCIを操作する - Oracle Cloud Infrastructureアドバンスド
](https://community.oracle.com/docs/DOC-1019624)
[Oracle Cloud にあるデータベースをコマンドライン（CLI）から起動・停止してみた](https://qiita.com/mikika/items/5a93c669d725a9dfc846)

### JQ
[jq manual(Official)](https://stedolan.github.io/jq/manual/)
[jq sandbox](https://jqplay.org/)
[jqコマンドで覚えておきたい使い方17個](https://orebibou.com/2016/05/jq%E3%82%B3%E3%83%9E%E3%83%B3%E3%83%89%E3%81%A7%E8%A6%9A%E3%81%88%E3%81%A6%E3%81%8A%E3%81%8D%E3%81%9F%E3%81%84%E4%BD%BF%E3%81%84%E6%96%B917%E5%80%8B/)
[jq コマンドで JSON を CSV に変換する](https://medium.com/veltra-engineering/jq-supports-json-to-csv-fb5c951a9575)

### fish shell
[fish document(Official)](https://fishshell.com/docs/current/index.html)
[Oracle Cloud Infrastructure CLI用のfish補完を作ってみた](https://qiita.com/sugimount/items/fb71920f846175633d68)