2017年9月9日土曜日

Oracle Linux 7 で JSON をあつかう。

Oracle Linux 7 の Public Yum リポジトリにも jq があります。
OL6 と同様に、addons リポジトリに配置されているのでインストールしてみます。

Using jq to process JSON on the Oracle Linux Command Line
https://community.oracle.com/community/server_&_storage_systems/linux/oracle_linux/blog/2017/04/14/using-jq-to-process-json-on-the-oracle-linux-command-line

Oracle Linux 7 (x86_64) Addons
http://yum.oracle.com/repo/OracleLinux/OL7/addons/x86_64/

Public Yum の ol7_addons リポジトリを有効にします。
[root@vm01 ~]# yum install -y yum-utils
[root@vm01 ~]# yum-config-manager --enable ol7_addons
[root@vm01 ~]# yum info jq
読み込んだプラグイン:ulninfo
利用可能なパッケージ
名前                : jq
アーキテクチャー    : x86_64
バージョン          : 1.5
リリース            : 1.0.1.el7
容量                : 44 k
リポジトリー        : ol7_addons/x86_64
要約                : Command-line JSON processor
URL                 : http://stedolan.github.io/jq/
ライセンス          : MIT and ASL 2.0 and CC-BY and GPLv3
説明                : lightweight and flexible command-line JSON processor
                    :
                    :  jq is like sed for JSON data ? you can use it to slice
                    :  and filter and map and transform structured data with
                    :  the same ease that sed, awk, grep and friends let you
                    :  play with text.
                    :
                    :  It is written in portable C, and it has zero runtime
                    :  dependencies.
                    :
                    :  jq can mangle the data format that you have into the
                    :  one that you want with very little effort, and the
                    :  program to do so is often shorter and simpler than
                    :  you'd expect.

[root@vm01 ~]#

インストールします。
[root@vm01 ~]# yum install -y jq
[root@vm01 ~]# which jq
/usr/bin/jq

たとえば JSON 情報を整形したり・・・
※ちなみに例は Ravello の ESXi インストール用 VM イメージの情報です。

整形前の JSON 情報。
[root@vm01 ~]# curl -ks -u "$USERID:$PASS" -H "Accept: application/json" -X GET https://cloud.ravellosystems.com/api/v1/images/57049098
{"id":57049098,"name":"Empty ESX","description":"Empty VM enabled for ESXi support. \nThe hard disk of this VM is empty. Use it in order to install ESXi via ISO.","creationTime":1429004455722,"memorySize":{"value":8,"unit":"GB"},"numCpus":4,"platform":"v2-esx","os":"default","rcu":4.0,"supportsCloudInit":false,"requiresKeypair":false,"useCdn":false,"privateCloudImage":false,"hostnames":["esx"],"legacyMode":false,"loadingStatus":"DONE","loadingPercentage":100,"baseVmId":57049098,"biosUuid":"564dadcc-a795-99fc-f2ea-24afead600c3","allowNested":true,"suppliedServices":[{"id":6181349190795264,"name":"ssh","portRange":"22","protocol":"TCP","external":true,"ipConfigLuid":2017823326,"useLuidForIpConfig":true}],"networkConnections":[{"id":803761889,"device":{"mac":"2c:c2:60:7e:18:24","useAutomaticMac":true,"index":0,"deviceType":"e1000","generatedMac":"2c:c2:60:7e:18:24"},"ipConfig":{"id":2017823326,"autoIpConfig":{},"hasPublicIp":true,"externalAccessState":"CONDITIONAL_PUBLIC_IP","needElasticIp":false},"name":"NIC1","accessPort":true,"vlanTag":"1","mirror":false},{"id":1407917856130338,"device":{"mac":"2c:c2:60:77:31:24","useAutomaticMac":true,"index":1,"deviceType":"e1000","generatedMac":"2c:c2:60:77:31:24"},"ipConfig":{"id":8445662235462959,"autoIpConfig":{},"hasPublicIp":false,"externalAccessState":"CONDITIONAL_PUBLIC_IP","needElasticIp":false},"name":"NIC2","accessPort":true,"vlanTag":"1","mirror":false}],"hardDrives":[{"id":931953806,"name":"cdrom","boot":false,"size":{"value":0,"unit":"BYTE"},"index":1,"type":"CDROM","imageFetchMode":"LAZY","controller":"ide","controllerIndex":0,"loadingStatus":"DONE","loadingPercentage":100},{"id":6573608017068032,"name":"vol1","boot":true,"size":{"value":100,"unit":"GB"},"index":2,"type":"DISK","imageFetchMode":"LAZY","controller":"lsi53c1030","controllerIndex":1,"loadingStatus":"DONE","loadingPercentage":100}],"bootOrder":["CDROM","DISK"],"powerOffOnStopTimeOut":false,"importedByUser":false,"isPublic":true,"owner":"Ravello Systems","ownerDetails":{"userId":262148,"name":"Ravello Systems","deleted":false},"peerToPeerShares":0,"communityShares":0,"copies":0,"hasDocumentation":false,"requiresHvm":false}[root@vm01 ~]#

ただ整形してみます。
[root@vm01 ~]# curl -ks -u "$USERID:$PASS" -H "Accept: application/json" -X GET https://cloud.ravellosystems.com/api/v1/images/57049098 | jq
{
  "id": 57049098,
  "name": "Empty ESX",
  "description": "Empty VM enabled for ESXi support. \nThe hard disk of this VM is empty. Use it in order to install ESXi via ISO.",
  "creationTime": 1429004455722,
  "memorySize": {
    "value": 8,
    "unit": "GB"
  },
  "numCpus": 4,
  "platform": "v2-esx",
  "os": "default",
  "rcu": 4,
  "supportsCloudInit": false,
  "requiresKeypair": false,
  "useCdn": false,
  "privateCloudImage": false,
  "hostnames": [
    "esx"
  ],
  "legacyMode": false,
  "loadingStatus": "DONE",
  "loadingPercentage": 100,
  "baseVmId": 57049098,
  "biosUuid": "564dadcc-a795-99fc-f2ea-24afead600c3",
  "allowNested": true,
  "suppliedServices": [
    {
      "id": 6181349190795264,
      "name": "ssh",
      "portRange": "22",
      "protocol": "TCP",
      "external": true,
      "ipConfigLuid": 2017823326,
      "useLuidForIpConfig": true
    }
  ],
  "networkConnections": [
    {
      "id": 803761889,
      "device": {
        "mac": "2c:c2:60:7e:18:24",
        "useAutomaticMac": true,
        "index": 0,
        "deviceType": "e1000",
        "generatedMac": "2c:c2:60:7e:18:24"
      },
      "ipConfig": {
        "id": 2017823326,
        "autoIpConfig": {},
        "hasPublicIp": true,
        "externalAccessState": "CONDITIONAL_PUBLIC_IP",
        "needElasticIp": false
      },
      "name": "NIC1",
      "accessPort": true,
      "vlanTag": "1",
      "mirror": false
    },
    {
      "id": 1407917856130338,
      "device": {
        "mac": "2c:c2:60:77:31:24",
        "useAutomaticMac": true,
        "index": 1,
        "deviceType": "e1000",
        "generatedMac": "2c:c2:60:77:31:24"
      },
      "ipConfig": {
        "id": 8445662235462959,
        "autoIpConfig": {},
        "hasPublicIp": false,
        "externalAccessState": "CONDITIONAL_PUBLIC_IP",
        "needElasticIp": false
      },
      "name": "NIC2",
      "accessPort": true,
      "vlanTag": "1",
      "mirror": false
    }
  ],
  "hardDrives": [
    {
      "id": 931953806,
      "name": "cdrom",
      "boot": false,
      "size": {
        "value": 0,
        "unit": "BYTE"
      },
      "index": 1,
      "type": "CDROM",
      "imageFetchMode": "LAZY",
      "controller": "ide",
      "controllerIndex": 0,
      "loadingStatus": "DONE",
      "loadingPercentage": 100
    },
    {
      "id": 6573608017068032,
      "name": "vol1",
      "boot": true,
      "size": {
        "value": 100,
        "unit": "GB"
      },
      "index": 2,
      "type": "DISK",
      "imageFetchMode": "LAZY",
      "controller": "lsi53c1030",
      "controllerIndex": 1,
      "loadingStatus": "DONE",
      "loadingPercentage": 100
    }
  ],
  "bootOrder": [
    "CDROM",
    "DISK"
  ],
  "powerOffOnStopTimeOut": false,
  "importedByUser": false,
  "isPublic": true,
  "owner": "Ravello Systems",
  "ownerDetails": {
    "userId": 262148,
    "name": "Ravello Systems",
    "deleted": false
  },
  "peerToPeerShares": 0,
  "communityShares": 0,
  "copies": 0,
  "hasDocumentation": false,
  "requiresHvm": false
}
[root@vm01 ~]#

JSON から必要な情報だけ抽出したりできます。

例は Ravello から取得したイメージ全体の情報から、名前だけ抽出したり・・・
[root@vm01 ~]# curl -ks -u "$USERID:$PASS" -H "Accept: application/json" -X GET https://cloud.ravellosystems.com/api/v1/images | jq ".[].name"
"Empty ESX"
"Xubuntu Desktop 14.04.1 with qemu-kvm 20150302"
"Empty Cloud Image"
"Empty"
"nutanix_ovm-2015.1.0-1"
"Ubuntu Server 14.04.1 20150301"
"Windows 2012 R2"
"OracleLinux6.6"
"OracleLinux7.0"
"Ruby"
"CentOS 6.8 Minimal"
"Nutanix CE 01 (2017.01.10)"
"UI-Grid-HUB"
"Tomcat6"
"Python"
"Postgresql"
"Mysql"
"UI-Grid-NODE-Linux-1"
"UI-Grid-NODE-Linux-2"
"Webserver - Sample Application"
"Database - Sample Application"
"MongoDB"
"nginx"
"Tomcat7"
"Ubuntu 12.04.1 vanilla"
"Ubuntu Server 14.04.1 with qemu-kvm 20150301"
"CentOS 6.3 vanilla"
[root@vm01 ~]#

特定のイメージの ID だけ取得できたりします。
[root@vm01 ~]# curl -ks -u "$USERID:$PASS" -H "Accept: application/json" -X GET https://cloud.ravellosystems.com/api/v1/images | jq ".[0].name"
"Empty ESX"
[root@vm01 ~]# curl -ks -u "$USERID:$PASS" -H "Accept: application/json" -X GET https://cloud.ravellosystems.com/api/v1/images | jq ".[0] | .name, .id"
"Empty ESX"
57049098

最近ソフトウェアの設定ファイルやらクラウド サービスの API リクエスト / レスポンスやらで
JSON 形式の情報を扱うことが多いので、公式リポジトリにあるとありがたい気がします。

以上、OL7 で jq を使ってみる話でした。

0 件のコメント:

コメントを投稿