Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Cloud Foundry Cli Plugin入門

Cloud Foundry Cli Plugin入門

Cloud Foundry / Lattice Deep Talks
PaaS勉強会番外編: Cloud FoundryやLatticeを深く知ろう!資料

morika-t

May 09, 2015
Tweet

Other Decks in Technology

Transcript

  1. Cloud Foundry
    Cli Plugin
    Introduction to Cli Plugin

    View Slide

  2. About me
    Takeshi Morikawa
    業務: Cloud Foundry関連
    CF関連チェック範囲(業務外も含む)
    ◎ 少し前
    ○ ccngとdeaまわり
    ◎ 最近
    ○ rootfs(lucid64やcflinuxfs2)
    ○ cli plugin(本日の内容)

    View Slide

  3. Movie & Music & Game
    About me
    Takeshi Morikawa

    View Slide

  4. ◎ Cli Pluginとは
    ◎ pluginの入手と削除
    ◎ pluginの入手その他の方法
    ◎ pluginを作るための知識
    本日の内容

    View Slide

  5. cli pluginとは

    View Slide

  6. cli plugin???
    ◎ 基本的な特長
    ○ cfコマンド(cli)のサブコマンドのように扱える
    ◉ 例:cf hoge APP_NAME
    ○ go版のcfコマンド v6.7以降?から使用可能
    http://docs.cloudfoundry.org/devguide/installcf/use-cli-plugins.html

    View Slide

  7. cli plugin???
    ◎ 基本的な特長
    ○ cli自体と同じようにGolangで実装
    ○ RPCでやりとり
    https://github.com/cloudfoundry/cli/tree/master/plugin_examples#architecture-overview

    View Slide

  8. Install plugin
    『cf install-plugin』からgoのクロスコンパイルで
    生成されたbinファイルを指定
    $ cf install-plugin hoge
    githubのリリース等からバイナリを持ってきた場合
    go get等でバイナリを持ってきた場合
    $ export GOPATH=$HOME/tmp_gopath
    $ go get github.com/hoge/cf-plugin-hoge
    $ cf install-plugin $GOPATH/bin/hoge

    View Slide

  9. Uninstall plugin
    『cf plugins』でPlugin nameを確認し 『cf uninstall-plugin』する
    $ cf plugins
    Listing Installed Plugins...
    OK
    Plugin name Version Command name Command Help
    hoge 1.0.0 hoge cf hoge plugin
    $ cf uninstall-plugin hoge
    Uninstalling plugin hoge...
    OK
    Plugin name tree successfully uninstalled
    plugin nameがhogeの場合

    View Slide

  10. plugin入手
    その他の方法

    View Slide

  11. community pluginを使う

    View Slide

  12. community pluginを使う その1
    communityのplugin用urlをcfコマンドに登録する
    https://blog.starkandwayne.com/2015/03/04/installing-cloud-foundry-cli-plugins/
    $ cf add-plugin-repo community http://plugins.cfapps.io/
    OK
    http://plugins.cfapps.io/list added as 'community'
    $ cf list-plugin-repos
    OK
    Repo Name Url
    community http://plugins.cfapps.io/

    View Slide

  13. community pluginを使う その2
    communityのplugin一覧から入れたいpluginを探す
    https://blog.starkandwayne.com/2015/03/04/installing-cloud-foundry-cli-plugins/
    $ cf repo-plugins community
    Getting plugins from all repositories ...
    Repository: community
    name version description
    CLI-Recorder 1.0.1 Records and playbacks CLI commands.
    Live Stats 1.0.0 Monitor CPU and Memory usage on an app via the
    browser.
    Console 1.0.0 Start a tmate session on an application container
    Diego-Beta 1.3.0 Enables Diego-specific commands and functionality
    etc…

    View Slide

  14. community pluginを使う その3
    communityの一覧経由でpluginを入れる
    https://blog.starkandwayne.com/2015/03/04/installing-cloud-foundry-cli-plugins/
    $ cf install-plugin Console -r community
    Looking up 'Console' from repository 'community'
    7974312 bytes downloaded...
    Installing plugin /tmp/cf-plugin-console...
    OK
    Plugin Console successfully installed.
    $ cf plugins
    Listing Installed Plugins...
    OK
    Plugin name Version Command name Command Help
    hoge 1.0.0 hoge cf hoge plugin
    Console N/A console Start a live console
    入れたいplugin名がConsoleの場合
    ※ -rで取得元のnameを入れる

    View Slide

  15. community plugin
    ざっくり紹介

    View Slide

  16. Community plugin一覧 その1 (2015/5/6調べ)
    Version Description
    CLI-Recorder v1.0.1 Records and playbacks CLI commands.
    Live Stats v1.0.0
    Monitor CPU and Memory usage on an app
    via the browser.
    Console v1.0.0
    Start a tmate session on an application
    container

    View Slide

  17. Live Stats
    localhost:8080でHTTPserverを立ち上げ
    内部的にはstats相当を叩く(cf app APP_NAME)
    https://github.com/danhigham/cf-live-stats

    View Slide

  18. Console
    cloudfoundry-tmate-buildpackやcf-sshと同じように
    tmate経由でコンテナにアクセスするplugin
    https://github.com/danhigham/cf-console
    $ cf console cf-api-checker
    > Finding app guid for cf-api-checker ... 62de88da-8947-46fc-9877-
    52319c0a0bfb
    > Updating app to connect to tmate.
    > Updating app start command to 'curl -s https://raw.githubusercontent.
    com/danhigham/cf-console/master/install.sh > /tmp/install.sh && bash
    /tmp/install.sh && sleep 3600'.
    > Changing instance count to 2.
    > Checking app log datestamps.

    View Slide

  19. Community plugin一覧 その2 (2015/5/6調べ)
    Version Description
    Diego-Beta v1.3.0
    Enables Diego-specific commands and
    functionality
    Open v1.10 Open app url in browser
    autopilot v0.0.1
    zero downtime deploy plugin for cf
    applications

    View Slide

  20. autopilot
    appの旧バージョンmanifestと新バージョンmanifestを指定し
    ダウンタイムゼロでアプリを新バージョンに移行するplugin
    https://github.com/concourse/autopilot
    $ cf zero-downtime-push application-to-replace \
    -f path/to/new_manifest.yml \
    -p path/to/new/path
    pluginの配布元READMEの説明による動作原理
    1. 旧バージョンのアプリ名を -venerableにリネーム
    この時アプリのroutesは変更しない
    2. 新バージョンのアプリを でpush
    この時のroutesは旧バージョンと一緒になるのでロードバランシングされる
    3. 旧バージョン側のroutesとアプリそのものを削除し移行完了

    View Slide

  21. Community plugin一覧 その3 (2015/5/6調べ)
    Version Description
    Brooklyn v0.1.1
    Interact with Service Broker for Apache
    Brooklyn
    kinaba-me-logs v0.3.0
    Launches the Kibana UI (from kibana-me-
    logs) for an application.
    Buildpack
    Usage
    v1.0.0 Buildpack Usage
    CF App Stack
    Changer
    v1.0.0
    Allows admins to list and update
    applications with outdated lucid64 stacks.

    View Slide

  22. cf-plugin-kinaba-me-logs
    (kibana-me-logs)
    Docker/Logstash Service Brokerを使って
    applicationのlogをkibana経由で確認する用のplugin
    https://github.com/cloudfoundry-community/cf-plugin-kibana-me-logs
    pluginの配布元READMEの説明による動作原理
    1. https://github.com/cloudfoundry-community/kibana-me-logs
    を内部的には利用
    2. cf-kinaba-me-logs
    3. 本来のkibana-me-logs単体の使い方
    a. logをkibana UIで確認したいアプリに対して cf bind-service APP_NAM
    b. kibana-me-logs自体をpush&bind-serviceするとbindしたその他のアプリが見える

    View Slide

  23. Buildpack Usage
    特定のspaceやorgが利用しているbuildpackを確認するplugin
    https://github.com/csterwa/cf_buildpacks_usage_cmd
    $ cf buildpack-usage
    13 buildpacks found across 110 app deployments
    Buildpacks Used
    ----------------
    Node.js
    PHP
    Ruby
    https://github.com/cloudfoundry/java-buildpack.git
    https://github.com/cloudfoundry/php-buildpack.git

    View Slide

  24. CF App Stack Changer
    lucid64のstacksをcflinuxfs2に移行する為のplugin
    ◎ lucid64のアプリの確認
    ◎ stacks変更+restartが行える
    https://github.com/simonleung8/cli-stack-changer
    $ cf stack-list
    Getting all apps with lucid64 stack...
    OK
    Total 14 found ...
    name guid state
    hogehoge 09d03ad3-24ee-44a1-8969-4abb3268aab6 STARTED
    $ cf stack-change APP_NAME

    View Slide

  25. Community plugin一覧 その4 (2015/5/6調べ)
    Version Description
    Targets v1.0.0 Easily manage multiple CF targets
    Test User v0.0.1
    Create a user and assign all possible
    permissions, organisation and space are
    created if they do not already exist as well.
    If no organisation or space name are
    specified then the default value of
    'development' is used
    cf-download v1.0.0
    Plugin for downloading your application
    contents after staging.

    View Slide

  26. Targets
    CF_HOMEを利用して複数環境のtarget情報を管理するplugin
    https://github.com/csterwa/cf_buildpacks_usage_cmd
    $ cf api https://api.ng.bluemix.net
    $ cf login
    $ cf save-target bluemix
    $ cf api https://api.10.244.0.34.xip.io --skip-ssl-validation
    $ cf login
    $ cf save-target bosh-lite
    $ cf set-target dev
    $ cf targets
    bluemix
    bosh-lite (current)

    View Slide

  27. cf-download
    cf files APP_NAME app/hogehoge.logのように内部的にfiles相当のAPI経由で
    staging後のアプリ上にあるファイルを取得するplugin
    https://github.com/csterwa/cf_buildpacks_usage_cmd
    $ cf download dora
    Files completed: 1755 /
    $ ls ~/dora-download/
    app logs run.pid staging_info.yml tmp

    View Slide

  28. cli pluginを作るための知識

    View Slide

  29. 実装する上で参考になるコード解説 1
    最初pluginを作る上で参考になるのは
    github/cloudfoundry/cliの中にあるplugin_examples/call_cli_cmdがオススメ
    https://github.com/cloudfoundry/cli/tree/master/plugin_examples/call_cli_cmd/main
    import (
    "fmt"
    "github.com/cloudfoundry/cli/plugin"
    )
    import部分でgithub.com/cloudfoundry/cli/pluginをimportする
    main部分でplugin.Startでstructをnewさせる
    func main() {
    plugin.Start(new(CliCmd))
    }

    View Slide

  30. 実装する上で参考になるコード解説 2
    https://github.com/cloudfoundry/cli/tree/master/plugin_examples/call_cli_cmd/main
    func (c *CliCmd) GetMetadata() plugin.PluginMetadata {
    return plugin.PluginMetadata{
    Name: "CliCmd",
    Version: plugin.VersionType{
    Major: 1,
    Minor: 1,
    Build: 0,
    },
    Commands: []plugin.Command{
    {
    Name: "cli-command",
    HelpText: "Command to call cli command. It passes all arguments
    through to the command",
    UsageDetails: plugin.Usage{
    Usage: "cli-command\n cf cli-command CORE-
    COMMAND",
    },
    },
    },
    }
    }
    GetMetadata()を定義しpluginの名前(uninstall時に利用するもの)や
    コマンド名、およびバージョン定義を行う

    View Slide

  31. 実装する上で参考になるコード解説 3
    https://github.com/cloudfoundry/cli/tree/master/plugin_examples/call_cli_cmd/main
    func (c *CliCmd) Run(cliConnection plugin.CliConnection, args []string) {
    output, err := cliConnection.CliCommand(args[1:]...)
    if err != nil {
    fmt.Println("PLUGIN ERROR: Error from CliCommand: ", err)
    }
    fmt.Println("")
    fmt.Println("---------- Command output from the plugin ----------")
    for index, val := range output {
    fmt.Println("#", index, " value: ", val)
    }
    fmt.Println("---------- FIN -----------")
    }
    Run()部分で実際に処理を行う ※以下は実際のソースのコメントアウト部分を除いたもの
    cliConnection.CliCommand部分でpluginに渡ってきた引数を cliにそのまま渡す

    View Slide

  32. 実装する上で参考になるコード解説 4
    https://github.com/cloudfoundry/cli/tree/master/plugin_examples/call_cli_cmd/main
    $ cf cli-command apps
    Getting apps in org testOrg / space testSpace as admin...
    OK
    name requested state instances memory disk urls
    aaa started ?/1 256M 1G aaa.10.244.0.34.xip.io
    go started 1/1 256M 1G go.10.244.0.34.xip.io
    dora started 1/1 256M 1G dora.10.244.0.34.xip.io
    ---------- Command output from the plugin ----------
    # 0 value: Getting apps in org testOrg / space testSpace as admin...
    # 1 value: OK
    # 2 value:
    # 3 value: name requested state instances memory disk urls
    # 4 value: aaa started ?/1 256M 1G aaa.10.244.0.34.xip.io
    # 5 value: go started 1/1 256M 1G go.10.244.0.34.xip.io
    # 6 value: dora started 1/1 256M 1G dora.10.244.0.34.xip.io
    ---------- FIN -----------
    cli-command pluginに値を渡した時の挙動

    View Slide

  33. CliConnectionのinterface(旧)
    https://github.com/cloudfoundry/cli/tree/master/plugin_examples/call_cli_cmd/main
    v6.10.0の場合は基本的な以下の2つが用意されていた(最新はv6.11.2)
    ● CliCommand
    ○ plugin_examplesで使われている
    ○ plugin内部でコマンド発行した結果も標準出力に出す
    ● CliCommanWithoutTerminalOutput
    ○ plugin内部でコマンドを発行し、発行コマンド自体の標準出力
    は表示はしない
     ⇒ 通常はこちらを使う

    View Slide

  34. CliConnectionのinterface(新)
    https://github.com/cloudfoundry/cli/tree/master/plugin_examples/call_cli_cmd/main
    v6.11.2の場合はpluginを作る際によく使われる値を取得するための
    interfaceが追加された
    type CliConnection interface {
    CliCommandWithoutTerminalOutput(args ...string) ([]string, error)
    CliCommand(args ...string) ([]string, error)
    GetCurrentOrg() (plugin_models.Organization, error)
    GetCurrentSpace() (plugin_models.Space, error)
    Username() (string, error)
    UserGuid() (string, error)
    UserEmail() (string, error)
    IsLoggedIn() (bool, error)
    IsSSLDisabled() (bool, error)
    HasOrganization() (bool, error)
    HasSpace() (bool, error)
    ApiEndpoint() (string, error)
    ApiVersion() (string, error)
    HasAPIEndpoint() (bool, error)
    LoggregatorEndpoint() (string, error)
    DopplerEndpoint() (string, error)
    AccessToken() (string, error)
    }

    View Slide

  35. CliConnectionのinterface(次期)
    https://groups.google.com/a/cloudfoundry.org/d/msg/vcap-dev/jF1h16IFEh4/I2s19nlemgAJ
    更に良く使われる以下のような値が追加される?
    Example:
    apps = cliConnection.GetApps()
    println(apps[0].Name)
    println(apps[0].Guid)
    - GetApps()
    - GetApp()
    - GetOrgs()
    - GetOrg()
    - GetSpaces()
    - GetSpace()
    - GetUsers()
    - GetUser()
    - GetServices()
    - GetService()

    View Slide

  36. まとめ

    View Slide

  37. plugin化の
    メリット
    cliに独自拡張していた場合に
    Upstream追従が大変だが
    独立管理が可能になる
    ニッチな用途のコマンドが
    作りやすい

    View Slide

  38. plugin化の
    デメリット
    cliのバージョンと独立するので
    CCNGのAPI versionとの
    紐付けが重要

    View Slide

  39. その他参考情報
    pluginリリースの参考になる情報
    https://groups.google.com/a/cloudfoundry.org/forum/#!starred/vcap-
    dev/RDk9xbmp3gc

    View Slide

  40. Credits
    Special thanks to all the people who made and released
    these awesome resources for free:
    ◎ Presentation template by SlidesCarnival
    ◎ Photographs by Unsplash & Death to the Stock Photo
    (license)

    View Slide