Vagrant on init up check if folder exists, else create?

1.3k Views Asked by At

When we want first time run vagrant up, how to check if in project directory exists synced folders, and if not exists, then create before rest of process go on?

i try with:

  config.trigger.before [:up] do |trigger|
    trigger.info = "Create log, tmp, webroot/img folders"
    trigger.run = {path: ".provisioning/before_up.sh"}
  end

and

#!/bin/bash
# before_up.sh

if [ ! -d "logs" ]; then
  sudo mkdir logs
fi

if [ ! -d "tmp" ]; then
  sudo mkdir tmp
fi

if [ ! -d "webroot/img" ]; then
  sudo mkdir webroot/img
fi

but have errors

==> default: Running triggers before up ...
==> default: Running trigger...
==> default: Create logs, tmp, webroot/img folders
    default: Running local script: .provisioning/before_up.sh
==> default: Trigger run failed
==> default: Permission denied - /Users/me/apps/.provisioning/before_up.sh
1

There are 1 best solutions below

0
On

here is solution

at top of Vagrant file add:

FileUtils.mkdir_p './logs'
FileUtils.mkdir_p './tmp'
FileUtils.mkdir_p './webroot/img'

Vagrant.configure("2") do |config|
 # config here
end

from ruby documents: https://ruby-doc.org/stdlib-1.9.3/libdoc/fileutils/rdoc/FileUtils.html#method-c-mkdir_p

mkdir_p(list, options = {}) click to toggle source Options: mode noop verbose

Creates a directory and all its parent directories. For example,

FileUtils.mkdir_p '/usr/local/lib/ruby' causes to make following directories, if it does not exist.

  • /usr
  • /usr/local
  • /usr/local/lib
  • /usr/local/lib/ruby