Skip to content

Instantly share code, notes, and snippets.

View hemalvarambhia's full-sized avatar
👨‍🔬

Hemal Varambhia hemalvarambhia

👨‍🔬
View GitHub Profile
@hemalvarambhia
hemalvarambhia / default.rb
Created November 5, 2014 10:29
The refactored cookbook shown at the November London Chef Users Meetup. The declarations here are Chef definitions that grew out of the refactoring process.
#
# Cookbook Name:: rails_app
# Recipe:: default
#
# Copyright 2014, AbelsonInfo
#
# All rights reserved - Do Not Redistribute
#
include_recipe 'rails_app_server::default'
@hemalvarambhia
hemalvarambhia / app.rb
Created November 6, 2014 07:46
One of the recipes in the legacy chef repo shown at the November London Chef Users Meetup.
# Determine which apps require the 'app' tier to be set up on this node:
tier_apps = ai_stack.apps_for_tier(node, 'app')
# Initialize an array to store user names that will later be added to the
# "sshlogin" group:
sshlogin_users = []
# Perform configuration required by all apps:
unless tier_apps.empty?
# Ensure all dependencies necessary to build Ruby are present:
@hemalvarambhia
hemalvarambhia / nginx_matcher.rb
Last active August 29, 2015 14:08
The nginx Rspec matcher for serverspec
module NginxMatchers
RSpec::Matchers.define :permit do |ip_addresses|
match do |content|
escaped_url = Regexp.escape @url
ip_addresses.all?{ |ip_address|
content=~/location #{escaped_url} \{[^}]+allow #{ip_address};/m
}
end
chain :access_to do |url|
@hemalvarambhia
hemalvarambhia / nginx_matcher_usage_example.rb
Last active August 29, 2015 14:08
An example of how the nginx rpsec matcher is used in serverspec. This was also shown to the London Chef Users Meetup on Tuesday.
describe "AI office" do
describe "/feed URL" do
its(:content) {
should permit(client_machines).access_to("/feed")
}
end
end
@hemalvarambhia
hemalvarambhia / postgresql_matchers.rb
Created November 7, 2014 16:45
An RSpec matcher to check a user is allowed to connect to a database using TCP/IP connections unconditionally.
module PostgresqlMatchers
RSpec::Matchers.define :permit_user do |user|
match do |content|
!(content=~/host #{@database} #{user} 127\.0\.0\.1\/32 trust/).nil?
end
chain :to_connect_to do |database|
@database = database
end
@hemalvarambhia
hemalvarambhia / nginx_privilege_matcher.rb
Last active August 29, 2015 14:09
A ChefSpec nginx matcher that checks requests from a list of IP addresses have been whitelisted
if defined?(ChefSpec)
module ChefSpec::Matchers
class NginxPrivilegeMatcher
def initialize(runner, ip_addresses, permission="allow")
@runner = runner
@ip_addresses = ip_addresses
@permission = permission
end
@hemalvarambhia
hemalvarambhia / matchers.rb
Created November 24, 2014 14:20
Factory methods that provide a fluent interface to the NginxPrivilegeMatcher
if defined?(ChefSpec)
def whitelist_requests_from(ip_addresses)
ChefSpec::Matchers::NginxPrivilegeMatcher.new(chef_run, ip_addresses, "allow")
end
def blacklist_requests_from(ip_addresses)
ChefSpec::Matchers::NginxPrivilegeMatcher.new(chef_run, ip_addresses, "deny")
end
end
@hemalvarambhia
hemalvarambhia / example_usage.rb
Created November 24, 2014 14:24
Example usage of the Nginx chefspec matchers
describe "configuration" do
before :each do
@nginx_configuration = chef_run.template(@server_nginx_config_file)
end
describe "server" do
describe "location" do
describe "/" do
it "whitelists requests from Abelson Info servers" do
expect(@nginx_configuration).to whitelist_requests_from(["92.165.78.7", "12.34.56", "24.68.12"]).to_url "/"
import numpy as np
from numpy.linalg import inv
from scipy import linalg
from sklearn.decomposition import PCA
from sklearn import utils
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
mean_ = np.mean(X, axis=0)
X = X - mean_
@hemalvarambhia
hemalvarambhia / pca_tutorial_lindsay_smith.py
Created April 20, 2019 16:25
Using Scikit-Learn's PCA to reproduce the results of the Lindsay Smith PCA Tutorial Paper
import numpy as np
from numpy.linalg import inv
from scipy import linalg
from sklearn.decomposition import PCA
from sklearn import utils
X = np.array([[2.5, 2.4], [0.5, 0.7], [2.2, 2.9], [1.9, 2.2], [3.1, 3.0], [2.3, 2.7], [2, 1.6], [1, 1.1], [1.5, 1.6], [1.1, 0.9]])
mean_ = np.mean(X, axis=0)
X = X - mean_