Skip to content

Instantly share code, notes, and snippets.

@ghulevishal
Forked from surdy/CLI-Notes.md
Created March 23, 2017 10:15
Show Gist options
  • Save ghulevishal/e842e63558ea362cf204950318902a67 to your computer and use it in GitHub Desktop.
Save ghulevishal/e842e63558ea362cf204950318902a67 to your computer and use it in GitHub Desktop.

Why I prefer CLI over UI ?

  • CLI tools are Composable using pipes (great old school example http://www.youtube.com/watch?v=tc4ROCJYbm0&t=5m32s)
  • Great for automation and batch processing
  • Fewer mouse clicks. Can go futher using only keyboard
  • Avoid cluky UIs especially over poor connections
  • I'm insulated from UI changes
  • Easier to debug as they are often more verbose than the UI

Intro

Basics

Hidden gems

  • Default config file lives in ~/.dcos/dcos.toml. %USERPROFILE%\.dcos\dcos.toml on Windows
  • Code for subcommands lives in ~/.dcos/subcommands
  • DCOS_CONFIG environment variable can be used to override the default config file.
  • If you want to debug a CLI error use dcos --debug --log-level=debug to get very verbose output

Tips & Tricks

Useful tools

Hitting API endpints in an authenticated cluster

curl --header "Authorization:curl --header token=$(dcos config show core.dcos_acs_token)" http://52.21.51.123/mesos/master/state-summary

curl --header "Authorization:curl --header token=$(dcos config show core.dcos_acs_token)" http://52.21.51.123/marathon/v2/leader

curl -skSL -H "Authorization: token=$(dcos config show core.dcos_acs_token)" $(dcos config show core.dcos_url)/mesos/master/slaves | grep slave_public | wc

Using CLI for Marathon

  • Deploy and update Marathon apps

    dcos marathon app add tomcat.json

    dcos marathon app update /tomcat < tomcat.json

  • Deploy app on MoM instance instead of root marathon

    dcos config set marathon.url $(dcos config show core.dcos_url)/service/marathon-user

    dcos marathon app add nginx.json

Debugging using the CLI

dcos package install cassandra
dcos task
dcos task ls cassandra
dcos task log cassandra stderr

Doing fancy stuff using dcos node ssh (https://dcos.io/docs/1.7/administration/sshcluster/)

  • SSH into nodes for debugging
dcos node ssh --leader --master-proxy
  • Find out which node a service is runnig on
dcos task --json | jq --raw-output '.[] | select(.name == "tomcat") | .slave_id'
dcos node ssh --option StrictHostKeyChecking=no --option LogLevel=quiet --master-proxy --mesos-id=$(dcos task --json | jq --raw-output '.[] | select(.name == "tomcat") | .slave_id') "curl -s ifconfig.co" 2>/dev/null
  • Find out public IPs of all Public agents
dcos node --json | jq --raw-output '.[] | select(.reserved_resources.slave_public != null) | .id'
for id in $(dcos node --json | jq --raw-output '.[] | select(.reserved_resources.slave_public != null) | .id'); do dcos node ssh --option StrictHostKeyChecking=no --option LogLevel=quiet --master-proxy --mesos-id=$id "curl -s ifconfig.co" ; done

Multi-Cluster CLI

  • First approach
cp ~/.dcos/dcos.toml ~/.dcos/alpha.toml
DCOS_CONFIG=~/.dcos/alpha.toml dcos config set core.dcos_url http://52.38.178.200
alias alpha='DCOS_CONFIG=~/.dcos/alpha.toml dcos'
alpha auth login
alpha marathon app list 
  • Better approach In .bashrc put the following snippet :
dcos ()
{
    # find current working directory
    local cwd=$(pwd)
    # path for config file in current directory 
    local config_file="${cwd}/dcos.toml"
    # find the location for dcos CLI binary
    # \which makes sure we use unaliased which
    local dcos_binary="$(\which dcos)"
    #check if config file exists in current directory
    if [ -f "$config_file" ]
    then
        # use config file in current directory
        DCOS_CONFIG="${config_file}" "$dcos_binary" "$@"
    else
        # use default config file 
        "$dcos_binary" "$@"
    fi
}

alias dcos='dcos'
mkdir -p clusters/{alpha,beta,gamma}
for dir in clusters/{alpha,beta,gamma}; do cp ~/.dcos/dcos.toml $dir; done
cd clusters/alpha
dcos config set core.dcos_url http://52.38.178.200
dcos auth login
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment