Upon trying to run npm install to download and install dependencies for the angular-seed app, I've gotten an error saying something about a legacy version of node:

npm WARN This failure might be due to the use of legacy binary "node"
npm WARN For further explanations, please read /usr/share/doc/nodejs/README.Debian
npm ERR! not ok code 0

This error happens when your system (most likely Ubuntu or Debian) call the NodeJS executable nodejs instead of just node. You can solve this by installing nodejs-legacy:

$ sudo apt-get install nodejs-legacy

Keep in mind that you might need to run npm install a couple of times since there are a few errors, too.

References:

Read more




WIP Alert: This is a work in progress

Defining a custom block to define what a double will return

When none of the predefined ways to define what your double will return (once(), twice() and so on) work for you, RSpec lets you specify a block to define exactly what you want a double to return.

For example, I want to make a double that will receive a method called :take! exactly four times. In the first three calls it should return 1 and on the fourth call it should raise an Exception:

it 'contrived example' do
    my_double = double()

    # create a counter outside so you can keep track
    # of how many times the method was called
    count = 0

    expect(my_double).to receive(':take!').exactly(4).times {
        count += 1 

        if count == 4
            raise Exception.new("Ka-boom!")
        else
            1
        end     
    }

    # rest of spec code...
    # ...
end

References:

Read more




These are some points that may help you if you are starting out with R (maybe you are taking the Coursera Data Science course).

R vectors are 1-indexed

That means the first index is 1 and not 0 as you may have expected.

How to make an empty data.frame and add data to it

I'm not sure this is the optimal way to hadnle dataframes but it gets the job done (using rbind changes the names of the labels)

# create a data frame that will hold two integer values
# with labels "id" and "age"
my_df <- data.frame(id=numeric(0),age=numeric(0))

# now add one row to the dataframe
my_df[nrow(my_df)+1,] <- c(id=10,age=25)

Generating sequences

For example, to run a command a given number of times.

# this prints numbers from 1 to 20
number <- 20
for(i in seq(number)){
    print(i)
}

Read more




This is useful if you want to make sure a file contains a given line; it is commonly used to add configuration directives to configuration files.

In order to use this, you must install Puppet module puppetlabs/stdlib

Example usage: add the following to a manifest file if you want to include a directory to a user's $PATH:

file_line{"add a directory to some user's path":
    path => "/home/jdoe/.bashrc",
    line => 'PATH=$PATH:/path/to/some/directory',
}

The default behaviour is to make sure the line you've provided is present in said file. If you want to make sure some line is absent from a file, you can use ensure => "absent";

Example: you want to make sure that your ssh server does not allow password-based user login:

file_line{'disable password login':
    path => '/etc/ssh/sshd_config',
    line => 'PasswordAuthentication yes',
    ensure => 'absent',
}

Read more




Assuming you are using Typesafe Activator to manage your apps, create a simple project based upon the minimal-scala template.

We'll be using elastic4s, which is but one way to connect to elasticsearch using Scala. There are other libraries around and you can also use raw HTTP requests.

Add

libraryDependencies += "com.sksamuel.elastic4s" %% "elastic4s" % "1.2.1.2"

to build.sbt and compile:

~/path/to/your/project$ sbt compile

Change the sample code that comes with the template. Replace it with this instead:

Example 1: using Thread.sleep()

package com.example
// the query dsl
import com.sksamuel.elastic4s.ElasticClient
import com.sksamuel.elastic4s.ElasticDsl._
// elasticsearch stuff
import org.elasticsearch.action.search.SearchResponse
// scala concurrency stuff
import scala.concurrent._
import scala.concurrent.duration._
import ExecutionContext.Implicits.global
import scala.util.{Success, Failure}
object Hello {
    def main(args: Array[String]): Unit = {
       val client = ElasticClient.remote("url.of.your.es.server", 9300)
       val res:Future[SearchResponse] = client execute { search in "index_name/type_name" query "your_query" }
        res onComplete{
            case Success(s) => println(s)
            case Failure(t) => println("An error has occured: " + t)
        }
        println("Request sent")
        //adjust this if needed
        Thread.sleep(1000)
    }
}

Example 2: using Await.result()

package com.example
import com.sksamuel.elastic4s.ElasticClient
import com.sksamuel.elastic4s.ElasticDsl._
import org.elasticsearch.action.search.SearchResponse
import scala.concurrent._
import scala.concurrent.duration._
import ExecutionContext.Implicits.global
import scala.util.{ Success, Failure }
object Hello {
    def main(args: Array[String]) {
        val client = ElasticClient.remote("url.of.your.es.server", 9300)
        val res:Future[SearchResponse] = client execute { search in "index_name/type_name" query "your_query" }
        println("REQUEST SENT")
        val await = Await.result(res, 10.seconds)
        println(await)
    }
}

Read more




There are many useful third-party modules (some by puppetlabs itself) that you can add to your puppet manifests to solve common problems.

However, you need to first install them.

If you're using Vagrant, one option is to use a shell provisioner to install the extra modules on the guest system (example with one of the most useful modules, puppetlabs/stdlib). Something like:

Vagrant.configure("2") do |config|
    # other configs

    config.vm.provision :shell, :inline => "mkdir -p /etc/puppet/modules && ( (puppet module list | grep puppetlabs-stdlib) || puppet module install puppetlabs/stdlib)"

    # other configs
end

The spirit of making sure our provisions are always idempotent (that is, stateless), make sure you verify that the module isn't already installed before you try installing it!

If you're using puppet command line tools, it's even easier:

$ puppet module install puppetlabs-stdlib

- References - Puppet Forge Repository

Read more




If you need to work with python you'll sooner or later bump into one of these terms so they will be defined here for future reference.

  • pip

    Package installer for Python. Kind of like gem for ruby.

  • virtualenv

    A tool to help you keep multiple versions of python (and separate python packages) for each of your projects. Think of rvm and gemsets for ruby.

Examples

  • Create and active virtualenv for a project directory:

    $ virtualenv <your-directory-name>
    $ . <your-directory-name>/bin/activate
    

    (to deactivate it, just run deactivate from anywhere in your system

  • Install a python package

    # this will install a package for the currently activated `virtualenv`
    $ pip install <package-name>
    # this will install a package **globally**
    $ sudo pip install <package-name>
    

References

Read more




Since Puppet resources are run in no particular order, we must explicitly define temporal dependencies between them.

In order words, if we want a given command to be run only after another command has run, we must declare it using keywords such as require, before, notify and subscribe.

For example, making sure a system update is run before we try to install the git package.

# tell puppet what to prepend to commands
Exec { 
    path => [ "/bin/", "/sbin/" , "/usr/bin/", "/usr/sbin/" ]
}  
# a command to run system updates
exec { 'sys_update':
    command => "apt-get update --fix-missing"
}  
class git {
    package{'git-core':
        ensure => 'installed',
        require => Exec['sys_update']
    }
}  
include git

All keywords

  • before => Resource['foo']: current resource will always be applied before Resource 'foo'.
  • notify => Resource['foo']: current resource will always be applied before Resource 'foo', and resource 'foo' will be notified (i.e. re-run) every time the state of the current resource changes.
  • subscribe => Resource['foo']: the current resource will always be applied after Resource 'foo'. The current resource will be re-applied whenever Resource 'foo' changes state.

Using Arrows instead of keywords

TODO: example
TODO: note that requiring a resource with arrows to others **does not** trigger the other resources.

Read more




A smal compilation of some of the most commonly-used Vagrant commands.

Boxes

  • vagrant box add

    Adds a box definition into your local collection. You can then refer to these in Vagrantfiles anywhere in your computer.

    • vagrant box add hashicorp/precise32 (actually existing box from VagrantCloud)
    • vagrant box add <my-new-box> /path/to/box/definition/file (.json or .box file on your local machine)
    • vagrant box add <my-new-box> http://url/to/my/box.box (vagrantbox.es is a good source for standalone boxes)

Read more