Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
def sh command
system command
unless $?.success?
log "failed: #{command}"
exit 1
end
end
def write_host_entry ip, host
add_line_to_file "/etc/hosts", "#{ip} #{host}"
end
def add_line_to_file path, line
original_lines = File.read(path).each_line.map{|l| l.chomp}
if original_lines.include?(line)
log "already added line to '#{path}': #{line}"
else
log "adding line to '#{path}': #{line}"
sh "echo '#{line}' >> #{path}"
end
end
def add_line_to_bash_profile line
add_line_to_file("/etc/profile", line)
system "source /etc/profile"
end
def package_installed? package_name
installed = `dpkg-query -W --showformat='${Status}\n' #{package_name}` =~ /install ok installed/
log "#{package_name} already installed..." if installed
installed
end
def add_package_source source
add_line_to_file "/etc/apt/sources.list", "deb http://debian.datastax.com/community stable main"
@updated_apt_cache = false
end
def ensure_package package_name
unless package_installed? package_name
unless @updated_apt_cache
sh "apt-get update -y --fix-missing"
@updated_apt_cache = true
end
sh "apt-get install -y #{package_name}"
end
end
def log line
puts "[vosbox] #{line}"
end
def ensure_file path, content
path = File.expand_path(path)
if File.exist?(path) && File.read(path) == content
log "exists: #{path}"
else
log "create: #{path}"
File.open(path, "w"){|f| f.write(content)}
sh "chown vagrant:vagrant #{path}"
end
end
def ensure_symlink from, to
if File.symlink?(to)
log "symlink already exists: #{from} => #{to}"
else
log "creating symlink: #{from} => #{to}"
sh "ln -s #{from} #{to}"
end
end
def ensure_directory directory
if Dir.exist? directory
log "directory exists: #{directory}"
else
log "creating directory: #{directory}"
sh "mkdir -p #{directory}"
sh "chown -R vagrant:vagrant #{directory}"
end
end
def ensure_file_copy from, to
sh "cp #{from} #{to}"
end
def ensure_gem gem
if Gem.available? gem
log "gem '#{gem}' already installed..."
else
sh "gem install #{gem} --no-rdoc --no-ri"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment