Friday, October 23, 2015

Github without password

You don't want to type your github password every time you push on your repo. What you need to do is simply to put your public key in github.

1. In the client where you want to clone your git repository just create a private / public key pair:

ssh-keygen -t rsa -b 4096

PS: Leave the keyring password blank
2. Copy your public key (your private will stay in your client) 
cat ~/.ssh/id_rsa.pub

3. Go to the github  SSH  keys settings page  and copy the public key above

4. You should be done! 

Wednesday, July 22, 2015

Postgresql for dummies

Just a set of basic commands to get up to speed with Postgres
select distinct  field1 from my-beautiful-schema.column-within-schema;

Monday, January 5, 2015

how to print percentages in python

Very simple pattern to show your percentages into a printed string
>>> print "%.0f%%" % (100.0 * 1/3)

Tuesday, December 2, 2014

how to query mongo and match subdocuments

Untitled Document.md

Imagine that a collection called recipes in your mongodb has entries with the following structure:

    "ingredients" : {
        "dairy" : 0,
        "vegetables" : [ ]

If you want to match subdocuments into this collection it is actually easier that it seems:

 db.recipes.find({ "ingredients.dairy" : "gwiyomi_sherlocked"}).count()

Friday, October 3, 2014

Queries to remember

In mongo, you can indeed use regular expressions! Look at the example below: we find and count all the matches containing mytag:
db.collection.find({"tags":{ $regex: '.*mytag.*', $options: 'i' }}).count()

Wednesday, October 1, 2014

logging your screen output in linux bash

Redirecting the bash output does not work with the classic redirection operator > ? No worries, screen comes with a built in option to dump everything when screen is terminating. Just add -L to your screen command as in the example below:
screen -L -S myGreatScreen my_bash_command

Tuesday, August 19, 2014

Switch your EC2 instance from command line with python and BOTO

First thing first: security. Even before starting messing around with your AWS credentials make yourself sure to get the required precautions.

You dont want to wake up and find that someone has launched 60 c3.x8 servers across 5 regions using the credentials that you left in a backed up directory...

First thing create a user if you dont have yet:  go in the IAM console, create new user and that's it ..

Then the more interesting part: associate a user policy to that user. This will limit the amount of messing that the user can do into your AWS account.

  1. click on the user -> in users policies -> click on "Attach User Policy"
  2. Click on custom policy generator
  3. give your policy a name (e.g. 'my-restricted-policy')
  4. copy and paste the following policy 
   "Version": "2012-10-17",
   "Statement": [{
      "Effect": "Allow",
      "Action": [
        "ec2:DescribeInstances", "ec2:DescribeImages",
        "ec2:StopInstances", "ec2:StartInstances"
      "Resource": "*"

You are almost done: you just need to generate your security credentials for the user with the limited policy generator. 

So click on the user -> select Security Credentials, click on Manage Access Keys and then Create access key. 

At this point you should have something that looks like the following

aws_access_key_id        = 'DSFSDFSDFWEFEWF'
aws_secret_access_key  = 'ldfjjs8wnoliencdnscdmsfkmsdkfml32'

First part is done, now a few more lines of python and that 's it : 
import boto.ec2

name_of_the_instance = 'put-here-the-name-of-your-instance'
name_of_the_region ='put-here-the-name-of-the-region-where-the-machine-is-located-eg-eu-west-1' 

# access key only for on / off
aws_access_key_id_     = 'DSFSDFSDFWEFEWF'
aws_secret_access_key_ = 'ldfjjs8wnoliencdnscdmsfkmsdkfml32'

conn = boto.ec2.connect_to_region(region_name, 
                                  aws_access_key_id     = aws_access_key_id_,
                                  aws_secret_access_key = aws_secret_access_key_ )

inst = conn.get_all_instances(filters={'tag:Name': name_of_the_instance})[0].instances[0]
print inst.stop()

Friday, August 15, 2014

how to migrate your mongodb into a bigger volume

First stop your mongodb
sudo service mongod stop
Assuming that your existing mongo installation is on /var/lib/mongodb/
sudo mv /var/lib/mongodb/ /your/preferred/destination/for/mongo/
set the rights correctly
sudo chmod -R mongodb.mongodb /your/preferred/destination/for/mongo/
and update the mongodb configuration file

# dbpath=/var/lib/mongodb 
dbpath= /your/preferred/destination/for/mongo/