Skip to content

AdvancedConfiguration

Yosuke Mizutani edited this page Nov 23, 2015 · 13 revisions

Advanced Configuration

Meta Variables

You can specify options by writing meta variables in the menu level, or command level.

Option Type Description
work_dir String Path to the working directory
env dict Environment variables
lock boolean Enables duplicate check

Note: If lock option is enabled, process information is stored under the directory ~/.easy-menu/pid.

example:

Main Menu:
  - menu 1:
    - echo $ENV1 $ENV2 $ENV3:            # command level setting
                                         # (Don't miss the trailing colon!)
        work_dir: /path/to/work2         # work_dir will be overwritten
        env:                             # env will be merged with parent's
          ENV1: VAL9
          ENV3: VAL3
        lock: yes                        # if already running, warn it
    - echo $ENV1 $ENV2
  - Sub Menu:
    - menu 2: echo $ENV1 $ENV2 $ENV4
    - menu 3:
      - echo $ENV1 $ENV2 $ENV3 $ENV4:    # command level setting
          env:
            ENV3: VAL7
      - echo $ENV1 $ENV2 $ENV3 $ENV4     # settings are individual to each command
    meta:                                # sub menu level setting
      env:
        ENV2: VAL8
        ENV4: VAL4
      lock: yes
meta:                                    # root menu level (global) setting
  work_dir: /path/to/work
  env:
    ENV1: VAL1
    ENV2: VAL2

Template Menu

You can write jinja2 template notation by default.

example:

{% set cmd = 'echo' %}
---
Main Menu:
{% for i in [1, 2, 3] %}
  - Menu {{ i }}: {{ cmd }} {{ i }}
{% endfor %}

Eval Caching

You can specify cache option to eval section. Set time to preserve command result in seconds. If you want to clear cache explicitly, use --clear-cache option for the command line option.

Note: Cache files are stored under the directory ~/.easy-menu/eval.

example:

---
Main Menu:
  - Menu 1: echo 1
  - eval: "echo '{\"Sub Menu\": [{\"Menu 2\": \"echo 2\"}, {\"Menu 3\": \"echo 3\"}]}'"
    cache: 3600    # time to preserve cache in SECONDS

Dynamic EC2 Login Menu

Do you use Easy Menu in the AWS EC2 instances? If you have set up AWS Command Line Interface (>=1.3), the following script will help you create the dynamic login menu.

  • Store a bash script like this in your server.

scripts/create_ec2_menu.sh

#!/bin/bash
# Create EC2 login menu for Easy Menu

set -e

SSH_USER='ec2-user'              # Rewrite to your actual user.
SSH_KEY='path/to/key.pem'        # Rewrite to your actual path to the SSH key file.
SSH_OPTS='-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -t'
AWS_EC2_REGION='ap-northeast-1'  # Rewrite to your actual region.

AWS="aws --region ${AWS_EC2_REGION}"
SSH="ssh ${SSH_OPTS} -i ${SSH_KEY} -l ${SSH_USER}"

filters='Name=instance-state-name,Values=running'

query='Reservations[].Instances[?not_null(Tags[?Key==`Name`].Value)][]'
query="${query}"'.[PrivateIpAddress,Tags[?Key==`Name`].Value|[0]]'
query='sort_by('"${query}"', &[1])'

echo '{"Server Login":['
comma=
$AWS ec2 describe-instances --filters "${filters}" --output text --query "${query}" | while IFS= read -r line; do
    arr=(${line})
    name="${arr[1]}"
    local_ip="${arr[0]}"
    echo "${comma}"'{"'"${name}"'":"'"${SSH} ${local_ip}"'"}'
    comma=','
done
echo ']}'

The output will be like this.

{"Server Login":[
{"aaa-01":"ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -t -i path/to/key.pem -l ec2-user 172.31.xxx.xxx"}
,{"aaa-02":"ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -t -i path/to/key.pem -l ec2-user 172.31.xxx.xxx"}
,{"bbb-01":"ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -t -i path/to/key.pem -l ec2-user 172.31.xxx.xxx"}
,{"bbb-02":"ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -t -i path/to/key.pem -l ec2-user 172.31.xxx.xxx"}
]}

Then, add eval field to your configuration file.

  - eval: "scripts/create_ec2_menu.sh"
    cache: 3600

In this case, the result of the command will be cached for one hour (=3,600sec).

Clone this wiki locally