Sha256: 5a477450f23cde0d9003e74d150b160bc74fb7ac9b578762cba805fa92b363db

Contents?: true

Size: 1.97 KB

Versions: 12

Compression:

Stored size: 1.97 KB

Contents

# Functions dealing with hook scripts

# True if script starts with #!/bin/bash
# --------------------------------------
def shebang?(bash_hook)
  text = File.open(bash_hook).read
  text.gsub!(/\r\n?/, "\n")
  line = text.next
  return true if line.start_with? '#!/bin/bash'
  true if line.start_with? '#!/usr/bin/env bash'
end

# Handle hook scripts or normal files: returns path to file to provision
# ----------------------------------------------------------------------
def hook_handler(hook)
  # hook might be the upload file or an executable to give the path to the
  # file to be provisioned. Builders can use this to hook into the
  # provisioning process for new snaps and management templates.
  unless File.executable?(hook)
    puts '[WARNING] hook script ' + hook + ' not executable. Abandoning launch.'
    return nil
  end

  fext = File.extname(hook)
  if fext == '.sh' && shebang?(hook)
    `#{hook}`.strip # executes and returns output (should be file)
  # elsif fext == '.bat'
  #   return `#{hook}`.strip # executes and returns output (should be file)
  else
    # raise '[WARNING] hook script not valid bash .sh or .bat. Abandoning launch.'
    raise '[WARNING] hook script not valid bash file with .sh extension. Abandoning launch.'
  end
end

# TODO: Match extension to OS and pick appropriately if
# both a .bat and .sh are present.

# If arg is nil check for presence of snap_hook.${ext}
# ----------------------------------------------------
def snap_handler(hook)
  if hook.nil?
    %w[.sh .bat].each do |e|
      return hook_handler('./snap_hook' + e) if File.exist?('./snap_hook' + e)
    end
    return nil
  end
  hook_handler(hook)
end

# if arg is nil check for presence of management_hook.${ext}
# ----------------------------------------------------
def management_handler(hook)
  if hook.nil?
    %w[.sh .bat].each do |e|
      if File.exist?('./management_hook' + e)
        return hook_handler('management_hook' + e)
      end
    end
    return nil
  end
  hook_handler(hook)
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
vagrant-subutai-1.1.7 lib/vagrant-subutai/packer/subutai_hooks.rb
vagrant-subutai-1.1.6 lib/vagrant-subutai/packer/subutai_hooks.rb
vagrant-subutai-1.1.5 lib/vagrant-subutai/packer/subutai_hooks.rb
vagrant-subutai-1.1.4 lib/vagrant-subutai/packer/subutai_hooks.rb
vagrant-subutai-1.1.3 lib/vagrant-subutai/packer/subutai_hooks.rb
vagrant-subutai-1.1.2 lib/vagrant-subutai/packer/subutai_hooks.rb
vagrant-subutai-1.1.1 lib/vagrant-subutai/packer/subutai_hooks.rb
vagrant-subutai-1.1.0 lib/vagrant-subutai/packer/subutai_hooks.rb
vagrant-subutai-1.0.3 lib/vagrant-subutai/packer/subutai_hooks.rb
vagrant-subutai-1.0.2 lib/vagrant-subutai/packer/subutai_hooks.rb
vagrant-subutai-1.0.1 lib/vagrant-subutai/packer/subutai_hooks.rb
vagrant-subutai-1.0.0 lib/vagrant-subutai/packer/subutai_hooks.rb