Skip to content

Instantly share code, notes, and snippets.

View juanje's full-sized avatar

Juanje Ojeda juanje

View GitHub Profile
@juanje
juanje / gist:1474442
Created December 13, 2011 23:23
Install required library
diff --git a/providers/bookmarks.rb b/providers/bookmarks.rb
index ac470fa..f75fe11 100644
--- a/providers/bookmarks.rb
+++ b/providers/bookmarks.rb
@@ -1,4 +1,8 @@
-require ('sqlite3')
+begin
+ require 'sqlite3'
+rescue LoadError => e
+ Chef::Log.warn("Dependency 'gem' not loaded: #{e}")
@juanje
juanje / gist:1386361
Created November 22, 2011 17:52
Split a subdirectory into a new Git repo
# You need to install the command git-subtree from here:
# https://github.com/apenwarr/git-subtree
# Let's say you got a subdirectory 'lib-abc' under 'project' git repo that you like to
# split into a new repository
# First you need to create a bare repository, i.e. a Github project called 'lib-abc'
# Your new remote could be somethings like '[email protected]:yourname/lib-abc.git'
cd project/
@juanje
juanje / cookbooks_foo_libraries_file.rb
Created November 15, 2011 22:53
Add include? and replace(str, str2) to Chef::Resource::File
class Chef
class Resource
class File
def include?(str)
return false unless ::File.exists?(@path)
file_content = IO.read @path
if file_content =~ /#{str}/
Chef::Log.info("file[#{@path}] contains the string '#{str}'")
true
@juanje
juanje / autocommit_monitor.sh
Last active September 28, 2015 00:38
Commit any changes on a specific directory
#!/bin/sh
#
# autocommit_monitor is a simple script to monitorize a directory and commit to
# the Git repo inside all the changes on files (files added, removed, changed, etc).
# It uses inotify to be aware of thos changes and you need to have installed
# the tool 'inotifywait'.
#
# Copyright (C) 2011-2014, Juanje Ojeda
# Author: Juanje Ojeda <[email protected]>
#
@juanje
juanje / Vagrantfile.rb
Created November 1, 2011 13:27
Vagrant and Chef stuff
# Vagrantfile for node: vagrant-vm
Vagrant::Config.run do |config|
# Every Vagrant virtual environment requires a box to build off of.
config.vm.box = "ubuntu-11.04-server"
# The Opscode Platform uses HTTPS. Substitute your organization for
# ORGNAME in the URL and validation key.
#
config.vm.provision :chef_client do |chef|
@juanje
juanje / gist:1310403
Created October 24, 2011 21:37
Use Ruby as AWK or Grep
# A few examples about how to use Ruby for parsing files as we could do
# with Awk or Grep. This is based on what I learn fro this post:
# http://code.joejag.com/2009/using-ruby-as-an-awk-replacement/
# Split each line with ':' and print the first $F[0] field
awk -F: '{ print $1 }' /etc/passwd
ruby -F: -nae 'puts $F[0]' /etc/passwd
# Parse the 'ps aux' output
# It'll print the ID process for the 'jojeda' user
@juanje
juanje / home_users.rb
Created October 21, 2011 09:12
Chef ohai plugin for home users
provides 'home_users'
require 'etc'
home_users Mash.new
etcpasswd = []
Etc.passwd do |entry|
etcpasswd << entry
end
@juanje
juanje / Chuletas.markdown
Created September 22, 2011 00:28
Archivos de apoyo a un mini curso de VIM

f -> (find) buscar siguiente caracter. Se posiciona en dicho caracter
t -> (?) buscar el caracter, pero posicionarse justo antes
b -> (begin word) principio de la palabra (o anterior si se está al principio)
w -> (word) ir al principio de la siguiente palabra
e -> (end of word) va al final de la palabra en la que está o de la siguiente
$ -> (end of line) va al final de la línea
0 -> (col 0) va a la columna 0. El principio de la línea
^ -> (begin of line) va al principio del texto de la línea

d[orden] -> (delete) borra lo que se le diga detrás:

@juanje
juanje / gist:1213900
Created September 13, 2011 14:16
Ruby class template based on attribs and a passed hash using mixins
module Base
def initialize args
update args
end
def update args
args.each do |k,v|
instance_variable_set "@#{k}", v if respond_to? k
end
@juanje
juanje / gist:1210545
Created September 12, 2011 03:49
Ruby class template based on attribs and a passed hash
class Base
def initialize args
update args
end
def update args
args.each do |k,v|
send "#{k}=", v if respond_to? k
end