IBM Cloud CLI と Cloud Functions Plugin のインストール (Ubuntu 18.04)

# IBM Cloud CLI

## ドキュメント
以下に手順がありますが Linux 環境における手順を補足します。

- [Installing the stand-alone IBM Cloud CLI](https://console.bluemix.net/docs/cli/reference/ibmcloud/download_cli.html
)
    - [Install from shell](https://console.bluemix.net/docs/cli/reference/ibmcloud/download_cli.html#shell_install)

## Install from shell

```sh
$ curl -fsSL https://clis.ng.bluemix.net/install/linux | sh
Current platform is linux64. Downloading corresponding Bluemix CLI...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   118    0   118    0     0    134      0 --:--:-- --:--:-- --:--:--   134
100 14.2M  100 14.2M    0     0   244k      0  0:00:59  0:00:59 --:--:--  176k
Download complete. Executing installer...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    40    0    40    0     0     47      0 --:--:-- --:--:-- --:--:--    47
Bluemix_CLI/
Bluemix_CLI/autocomplete/
Bluemix_CLI/autocomplete/bash_autocomplete
Bluemix_CLI/autocomplete/zsh_autocomplete
Bluemix_CLI/install
Bluemix_CLI/install_bluemix_cli
Bluemix_CLI/bin/
Bluemix_CLI/bin/ibmcloud-analytics
Bluemix_CLI/bin/ibmcloud
Bluemix_CLI/bin/cfcli/
Bluemix_CLI/bin/cfcli/cf
Bluemix_CLI/bin/LICENSE
Bluemix_CLI/bin/NOTICE
Bluemix_CLI/uninstall
Copying files ...
The installation completed successfully.
To get started, open a terminal window and enter "ibmcloud help".

IBM Cloud CLI has a plug-in framework to extend its capability. To install the recommended plug-ins and dependencies, run the install script from http://ibm.biz/install-idt. For additional plug-in details, see https://console.bluemix.net/docs/cli/reference/bluemix_cli/extend_cli.html.

IBM Cloud CLI automatically collects data for usage analysis and user experience improvement. To disable the collecting, run "ibmcloud config --usage-stats-collect false"

Bash and zsh completion scripts have been installed in "/usr/local/ibmcloud/autocomplete/". To enable bash completion, add the following line into "~/.bash_profile":
. /usr/local/ibmcloud/autocomplete/bash_autocomplete

To enable zsh completion, add the following line in "~/.zshrc":
. /usr/local/ibmcloud/autocomplete/zsh_autocomplete

To uninstall, run "/usr/local/ibmcloud/uninstall"
Install complete.
```

## Enable bash completion

install スクリプト上では `.bash_profile` に追記と書いてありますが、 [`.bashrc` に追記します](https://qiita.com/dark-space/items/cf25001f89c41341a9fd)

```sh
$ echo -e "\n# IBM Cloud CLI\n. /usr/local/ibmcloud/autocomplete/bash_autocomplete" >> .bashrc

$ tail -n3 .bashrc

# IBM Cloud CLI
. /usr/local/ibmcloud/autocomplete/bash_autocomplete
```

`.bashrc` を読み直して動作確認

```sh
$ exec $SHELL -l
$ ibmcloud
account   app       catalog   cfee      h         iam       login     plugin    resource  sl        update
api       billing   cf        config    help      info      logout    regions   service   target
```

## Uninstall
インストーラーは `To uninstall, run "/usr/local/ibmcloud/uninstall"` なんて言ってますがこんなファイルは存在しないので、インストーラーを展開した中の `Bluemix_CLI/uninstall` を実行しましょう

## 参考
- Windows CLI のインストール
    - [IBM Cloud 開発環境を整えるその１](https://qiita.com/pooshikin/items/2aabb19c2a32aea213f2)
        - 私も Windows マシンを使っていますが WSL 使いなのです…

# Cloud Functions Plugin

## ドキュメント

https://console.bluemix.net/openwhisk/learn/cli

## Install
`bx` コマンドも `ibmcloud` コマンドもバイナリは同じですので、 `ibmcloud` コマンドを実行します。

```sh
$ ibmcloud plugin install cloud-functions -r Bluemix
Looking up 'cloud-functions' from repository 'Bluemix'...
Plug-in 'cloud-functions 1.0.22' found in repository 'Bluemix'
Attempting to download the binary file...
 11.26 MiB / 11.26 MiB [============================================================================================================================================================] 100.00% 51s
11804649 bytes downloaded
Installing binary...
OK
Plug-in 'cloud-functions 1.0.22' was successfully installed into ~/.bluemix/plugins/cloud-functions. Use 'ibmcloud plugin show cloud-functions' to show its details.
```

プラグインはユーザーのホームにインストールされます。（ `sudo` で実行しても同じなので `sudo` なしで実行しましょう）

## Log in
`au-syd` リージョンはまだ `cloud-functions plug-in` でサポートされていないため、他を選びましょう。
`-o` で組織を、 `-s` でスペースも指定します。

```sh
$ ibmcloud login -o "yukisakai@example.jp" -s "dev"

Select an API endpoint:
1. eu-de - https://api.eu-de.bluemix.net
2. au-syd - https://api.au-syd.bluemix.net
3. us-east - https://api.us-east.bluemix.net
4. us-south - https://api.ng.bluemix.net
5. eu-gb - https://api.eu-gb.bluemix.net
6. Enter a different API endpoint
Enter a number> 4

Email> yukisakai@example.jp

Password>
Authenticating...
OK

Targeted account Yuki Sakai's Account (????????) <-> ???????

Targeted resource group Default

Targeted Cloud Foundry (https://api.ng.bluemix.net)

Targeted org yukisakai@example.jp

Targeted space dev


API endpoint:      https://api.ng.bluemix.net
Region:            us-south
User:              yukisakai@example.jp
Account:           Yuki Sakai's Account (????????)
Resource group:    Default
CF API endpoint:   https://api.ng.bluemix.net (API version: 2.92.0)
Org:               yukisakai@example.jp
Space:             dev
```

## Test
[`wsk` コマンドは今後 `ibmcloud fn` に置き換えられます。](https://console.bluemix.net/docs/openwhisk/bluemix_cli.html#cli_migration)

```sh
$ ibmcloud fn action invoke /whisk.system/utils/echo -p message hello --result
{
    "message": "hello"
}
```

### Packages について
Test では `/whisk.systems` ネームスペース下の `utils` パッケージ の `echo` エンティティを利用しています。
詳細は [Using and creating OpenWhisk packages](https://github.com/apache/incubator-openwhisk/blob/master/docs/packages.md)

```sh
$ ibmcloud fn package list /whisk.system
packages
/whisk.system/pushnotifications                                        shared
/whisk.system/websocket                                                shared
/whisk.system/watson-textToSpeech                                      shared
/whisk.system/watson-translator                                        shared
/whisk.system/utils                                                    shared
/whisk.system/weather                                                  shared
/whisk.system/combinators                                              shared
/whisk.system/samples                                                  shared
/whisk.system/github                                                   shared
/whisk.system/watson-speechToText                                      shared
/whisk.system/slack                                                    shared
/whisk.system/alarms                                                   shared
/whisk.system/messaging                                                shared
/whisk.system/cloudant                                                 shared

$ ibmcloud fn action get --summary /whisk.system/utils/echo
action /whisk.system/utils/echo: Returns the input
   (parameters: payload)
```

# 関連

- [IBM Cloud Functions - Your first Action, Trigger, and Rule](https://github.com/SakaiYuki/ibm-cloud-functions-action-trigger-rule)