When using default configuration on Ubuntu, you might experience SSH Timeouts (i.e. the console you are using to connect to a remote server will seem to freeze) after a few minutes of lack of activity.

This is annoying because you need to open a new console instance and reconnect and perform any other steps you need before connecting.

Since using SSH to connect to remote servers is very common and linux being as old as it is, you would think that someone somewhere would have provided a solution for this problem by now; you'd be right.

SSH Client Configuration

Open file /etc/ssh/ssh_config and set directive ServerAliveInterval to a value like 100:

# other configs
ServerAliveInterval 100

This causes your SSH client to send keep-alive messages every 100 seconds so that the server doesn't drop your connection.

Server Configuration

You must also configure the SSH remote Server you are connecting to (or ask whoever is responsible for doing it). Open file /etc/ssh/sshd_config and add these configurations at the end of the file to ensure they're not overriden by later lines:

# other configs
ClientAliveInterval 600
TCPKeepAlive yes
ClientAliveCountMax 10

These are conservative settings that will make your SSH Server only disconnect after (600 * 10 = 6000) seconds of user inactivity. Customize these if you need more.

References

Read more




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 handle 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)

# add another one
my_df[nrow(my_df)+1,] <- c(id=15,age=35)

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)
}

Printing to the console (command-line or RStudio)

Use cat():

cat("foo, bar baz")

Sorting a data frame by a column's value

If you have a data frame(in variable data) that has two columns: "name" and "age", you can sort data using the age attribute like this:

sorted_data <- data[with(data,order(age)),]

If you want to sort your data by age from highest to lowest, just give the column name a minus sign:

sorted_data <- data[with(data,order(-age)),]

more info: Dirk Eddelbuettel on sorting in R

Change column names in a data frame

Use function colnames() to take data frame with unwieldy column names and give it nice and easy names to work with:

> names(my_data_frame)
[1] "Provider.Number" "Hospital.Name"   "Address.1"
colnames(my_data_frame) <- c("number","name","address")
> names(my_data_frame)
[1] "number","name","address"

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 = {
       // scala uses the java driver which listens on port 9300
       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

In 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 activate a 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

  • Install a package for the currently activated virtualenv

    $ pip install <package-name>
    
  • 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