0

There's a command from Amazon Web Services called eb init that allows you to initiate the current working directory as a elasticbeanstalk app.

This command is interactive, meaning eb init will respond with a list of options.

$ eb init

Select a default region
1) us-east-1 : US East (N. Virginia)
2) us-west-1 : US West (N. California)
3) us-west-2 : US West (Oregon)
4) eu-west-1 : EU (Ireland)
5) eu-central-1 : EU (Frankfurt)
6) ap-south-1 : Asia Pacific (Mumbai)
7) ap-southeast-1 : Asia Pacific (Singapore)
8) ap-southeast-2 : Asia Pacific (Sydney)
9) ap-northeast-1 : Asia Pacific (Tokyo)
10) ap-northeast-2 : Asia Pacific (Seoul)
11) sa-east-1 : South America (Sao Paulo)
12) cn-north-1 : China (Beijing)
(default is 3):

This above is the region option, the second is the app option, and the third is a list of possible environment (or env) options.

What I'm looking for is to wrap the eb init command and be able to pass in strings that can bypass the interactivity of the script.

ebInit --region=eu-central-1 --app=my-app --env=my-app-live

I'd need to parse the stdout here and split the lines and get the number corresponding to the passed in argument option, which is pretty easy, because the options can shift numbers.

As a proof-of-concept I'd even be fine with something like this.

ebInit --region=5 --app=1 --env=1

I find it hard to believe that these kind of interactive commands are black boxes and cannot be programmatically interacted with. I've tried to do this with node.js and could not get it to respond, here's my old stackoverflow post "Responding to a interactive command via child process", which has received no attention.

I'm posting this here in unix / linux to ask if this is even possible, and if so how and in what language?

  • 3
    Wrapping interactive console programs is exactly what expect was made for. I imagine this would be a straightforward task in expect – Eric Renouf Nov 22 '16 at 21:16
0

With expect a solution might run along the lines of

#!/usr/bin/env expect

# or instead figure out a TCL getopt library
if {[llength $argv] != 3} {
    puts stderr "Usage: $argv0 region app env"
    exit 64
}
set aws_region [lindex $argv 0]
set aws_app    [lindex $argv 1]
set aws_env    [lindex $argv 2]

spawn -noecho eb init

set get_regions 1
while {$get_regions} {
    expect {
        # look for the "1) us-east-1 : US East (N. Virginia)"
        # assign to what TCL calls an array
        -re {([0-9]+). ([a-z0-9-]+) :} {
            set region_to_num($expect_out(2,string)) $expect_out(1,string)
        }
        # how we break out of the loop, also array entry
        # for "default" region if need be
        -re {\(default is ([0-9]+)\)} {
            set region_to_num(default) $expect_out(1,string)
            set get_regions 0
        }
    }
}

# show what we got based on arguments and parse of eb output
puts "region=$aws_region region_num=$region_to_num($aws_region) env=$aws_env"

or I guess you could find or write an expect-like library in JavaScript, but that might be more work. Whatever tickles your fancy...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.