19

I wish I could use conditional for .ebextensions configuration, but I don't know how to use it, my current case are :

One of .ebextensions configuration content are create a folder, actually the folder that must be created it's only once, because if I'm deploying app for second times or more I've got error, and the error said "the folder already exist".

So I need to give conditional, if the folder already exist it's not necessary to run again the command for create a folder.

If anyone has any insight or direction on how this can be achieved, I would greatly appreciate it. Thank you!

Khalid
  • 887
  • 2
  • 13
  • 26

2 Answers2

35

The .ebextensions config files allow conditional command execution by using the test: directive on a command. Then the command only runs if the test is true (returns 0).

Example .ebextensions/create_dir.config file:

commands:
  01_create_dir:
    test: test ! -d "${DIR}"
    command: mkdir "${DIR}"

Another example (actually tested on EB) to conditionally run a script if a directory is not there:

commands:
  01_intall_foo:
    test: test ! -d /home/ec2-user/foo
    command: "/home/ec2-user/install-foo.sh"
    cwd: "/home/ec2-user/"

The sparse documentation from AWS is here:

http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html#customize-containers-format-commands

PS. If you just need to conditionally create the directory, you can do it without conditionals, using the -p option for mkdir to conditionally create the directory like so:

commands:
  01_create_dir:
    command: mkdir -p "${DIR}"
mjul
  • 531
  • 1
  • 5
  • 6
  • In their example on http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html they confuse me by using `test: '[ ! /usr/bin/python ] && echo "python not installed"'` to conditionally run a python script. Doesn't that run the command if python is _missing_? – oskarth Aug 04 '15 at 15:02
  • mkdir -p was a great idea. Thanks – Yaron Idan May 26 '16 at 08:46
  • @oskarth The `!` in the test makes the if statement test for the existence of that path, it reads "if not `/usr/bin/python` return false". – Ryan Fisher Jun 03 '16 at 18:18
  • @RyanFisher Thx, I'm confused by my own comment (the echo) as well, maybe the docs changed. – oskarth Jun 05 '16 at 09:46
  • 1
    @oskarth well I did a good job of confusing you more, my explanation is wrong, the test returns true not false. The brackets are shorthand for an if statement, so if `/usr/bin/python` is missing, the left of the `&&` is true so the right is executed (echoing the message). – Ryan Fisher Jun 05 '16 at 17:00
  • @RyanFisher Yeah, that's what I thought initially, `foo && bar()` is a pretty common short-circuiting pattern, I think the check in the docs was backwards or something, idunno. It's been a while now. – oskarth Jun 07 '16 at 12:49
11

I think that the only way to do it is with shell conditions:

commands:
  make-directory:
    command: |
      if [ ! -f "${DIR}" ]; then
        mkdir "${DIR}"
      fi

See bigger example in jcabi-beanstalk-maven-plugin.

yegor256
  • 102,010
  • 123
  • 446
  • 597
  • yeah you right, I already ask to aws conditional is not supported in .ebextensions. ah ok, thanks for your suggestion, I'll try first. – Khalid Jun 19 '13 at 02:38
  • btw, how do I to get understanding about if [ ! -f "${DIR}" ]; then mkdir "${DIR}" fi – Khalid Jun 19 '13 at 02:40
  • I need 3 conditions to setup for 3 commands, 1) the command need to check is there any folder already created or not, 2) I put one line script in /etc/fstab, so I need to check is script already created or not, 3) I need to check is a folder alredy mounted (mount) or not. it will great if I could understand the conditional script command. thanks – Khalid Jun 19 '13 at 02:45