Tuesday, July 14, 2015

Setting up Node with NVM

I always keep facing problems with Node. It just keeps pushing me around with it's numerous errors. So, lately I found out about NVM and tried it. Still had an issue with it. But finally got it up and running. Here are some important things to keep note of when attempting to install node using NVM.
1. The default installation directory of the NVM installer (provided in the repo readme) is ~/.nvm. I find this a bit problematic so I actually just clone the repo into /opt/nvm since I kind of stick to sudoing things when they need to be global for node and ~/.nvm isn't owned by root, I kind of like to keep it that way.
2. Now setting up the envvars is done using profile.d/nvm.sh. This could also be done using ~/.zshrc or ~/.zprofile but I like it this way. In whatever case the envvar setup script is like this
export NVM_DIR=/usr/local/nvm
source /opt/nvm/nvm.sh

export NPM_CONFIG_PREFIX=/usr/local/node
export PATH="/usr/local/node/bin:$PATH"
3. So it's basically done (setting up NVM, not node) but since to install anything via nvm I'll need sudo so I must (at least) source the /etc/profile.d/nvm.sh into root. So I just do
$ sudo su
# source /etc/profile.d/nvm.sh
4. Now for the installation of a node version
# nvm install 0.12
# nvm ls
The last one should be done from root because it creates a particular directory that $ can't create.
5. Now we can simply execute
$ nvm use 0.12

Credits:
http://stackoverflow.com/questions/11542846/nvm-node-js-recommended-install-for-all-users
https://www.digitalocean.com/community/tutorials/how-to-install-node-js-with-nvm-node-version-manager-on-a-vps
Found a nice article here: http://www.liquidweb.com/kb/how-to-install-node-js-via-nvm-node-version-manager-on-ubuntu-14-04-lts/

Adding profile.d envvars to zsh

This is an extremely simple task to accomplish. Since, bash uses the following code segment to source all the files under /etc/profile.d you just have to add this into your ~/.zprofile or /etc/zprofile (the former sounds like a much more flexible choice).
# The default umask is now handled by pam_umask.
# See pam_umask(8) and /etc/login.defs.

if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
    fi
  done
  unset i
fi


Monday, July 13, 2015

When you can't log into Postgresql from Web Interface (Adminer etc.)

I ran into this problem earlier this week and left it until today, when I needed it work at all cost. Turns out it was a simple issue with the configuration file. Here's the process.

First of all, open the file /etc/postgresql/9.1/main/pg_hba.conf
Find the line : "local" is for Unix domain socket connections only
Below that line there is a line like this: local all all md5
Just change the last value to: peer
So the line now looks like: local all all peer
Now save the file (you need to be root)
Then restart the Postgresql process using the command: sudo service postgresql restart
Done.

Friday, May 15, 2015

Show all combinations of numbers from 1 to 9 that add up to 100 by adding, subtracting and concatenating

So, this week I saw a blog post somewhere where the poster claimed that if you can't solve the mentioned 5 problems each in under and hour, you ain't no programmer, dev or software engineer. So, I went in and read the problems. 1-4 were easy but number 5 got me a worrying a little. And expected, I failed to solve that in under 1 hour yesterday. But today morning I solved that in less than 30 minutes. But whatever, I can't call myself a programmer anymore :(
Here's my approach though, using recursion:
Each i (from 1 through 9) has 2 different ways of connecting to the sequence.
1. Add itself to the closest sum (1 + 23 + 4 + .......)
2. Or concatenate itself (1 + 234 + ......)
So if we either add an i to the sequence that has reached it or we concatenate. Examples: .......8 + 9
.......89
Now the part before 8 has the same behavior with 8
.......7 + 8
.......78
This goes all the way back to 1 and 2
1 + 2
12
So, the program works something like this
Each branch ultimately ends on the leftmost call.
Code:
#include <cstdio>
#include <iostream>
#include <sstream>

using namespace std;

string i2s(int i) {
    stringstream op;
    op << i;
    return op.str();
}

int solveS(int i, int sum, string p) {

    if (i >= 10) {
        if (sum == 100) {
            cout << p + " = " << sum << endl;
        }
        return 0;
    }

    int iSum = 0;
    for (int j = i ; j<=9 ; j++) {
        iSum = iSum * 10 + j;
        if (i == 1) {
            solveS(j+1, iSum, i2s(iSum));
        } else {
            solveS(j+1, sum+iSum, p + " + " + i2s(iSum));
            solveS(j+1, sum-iSum, p + " - " + i2s(iSum));
        }
    }

    return 0;
}

int main() {

    //freopen("output.txt", "w+", stdout);

    solveS(1, 0, "");

    return 0;
}


Sunday, May 03, 2015

Vagrant কি আর কিভাবে এটা ব্যবহার করব?

Vagrant আসলে একটা virtualization tool (wrapper আসলে)। এটা যা করবে, তোমার PC-র RAM খরচ করে একটা virtual computer বানাবে। সেটার সাথে তোমার real PC-র একটা LAN (NAT আসলে) বানাবে। আর সেই PC-র Port 80 (HTTP Listener) টা কে তোমার real PC-র কোন একটা port এ forward করে দেবে। যেখানে তুমি তোমার app টা কে access করতে পারবা। OK, that part aside, তুমি এখন এই same port কে কোন public IP তেও দিয়ে দিতে পারো।

কথা হচ্ছে, কিভাবে এত সব হবে। প্রথম কথা, Vagrant তোমার জন্য, সবই নিজে করবে, তুমি শুধু কিছু script লিখে বলে দিবা কি কি software তোমার server এ রাখবা। যেমন সাধারণত আমি LAMP stack টা ছাড়া কিছু রাখিনা, আর Django রাখি (Python)। তো আমি একটা provisioning script লিখে রেখে দেই। এটা একটা directory তে রেখে "vagrant up" command লিখলেই কিছুক্ষণের মধ্যে আমার server ready। তো এতসব যে হচ্ছে, বাতাস থেকে হচ্ছে নাকি? না। আসলে তুমি তোমার core OS টার একটা image (ubuntu 14.04, 12.04, etc) প্রথমবার vagrant কে দিয়ে download করাবা। সে এটা একবারই নামায় রেখে দিবে আর পরে আরও server বানালে সে এটা দিয়ে OS install করে তোমার provision script থেকে software গুলি install করবে।

তো এখন কি তুমি provisioning script এর language শিখতে বসবা? না। ভাল একটা vagrant file নামায় একটা directory তে রেখে "vagrant up" করে দিবা। প্রথমবার সে image নামাবে। তো এসব vagrant file পাবা কোথায়? Google দিয়ে পাবা। আমি কয়েকটা use করি। মোটামুটি ২/৩ টার মধ্যেই হয়ে যায় সব। Github এ অনেকে তাদের configure করা VM এর vagrantfile share করে দেয়। ভাল করে লিখেও দেয় এটাতে PHP কোনটা, phpMyAdmin আছে কিনা। কোন port এ কি service চলে। পরে শুনে দেখে একটা তে settle হবা এই আর কি।

Happy exploring.

Saturday, March 28, 2015

Stop the laptop from going "stand by" when lid is closed in Xubuntu

I hate the fact that I have to figure this out for every new distro I use. But here it goes for Xubuntu. Tested and working in 14.04 LTS.
1. Open the file /etc/systemd/logind.conf
2. Find the line "HandleLidSwitch="
3. Set it like "HandleLidSwitch=ignore" and if it has a "#" (hash" in front of it, remove it.
Save and reboot and you are good. :)
Credits: http://askubuntu.com/a/362692/145915

Monday, March 23, 2015

Laravel validation trouble shootings.

While working on a project I stumbled upon this huge pile of problem of validating a form. Even though the fancy coding styles did make some things easy, it made some other things hard, especially when not having a complete documentation. So, here's a note for future.
\ 1. Custom validation messages: Custom messages, if not in a language file must be written for each field and each of the rules of that field. So you cannot escape with a single message written for email field if you want it to be shown on every rule. You have to write that same message for each rule, individually
2. Field name renaming: When printing validation messages Laravel, if not instructed otherwise, will use a slightly polished version of the 'name' attributes of you fields. So, for example name='user_name' will be made into user name. So if you instead want it to look like "Username", you have to use the method called
setAttributeNames
Like this:
$attributeNames = array(
   'name' => 'Name',
   'cat' => 'Category',     
);
$validator = Validator::make ( Input::all (), $rules );
$validator->setAttributeNames($attributeNames);
// Credits for: http://stackoverflow.com/a/25189223/1928610
3. Check uniqueness in a table having a different name than field name: Now what if you want to ensure that the user always enters an email that is not yet present in the 'users' table when signing up. Laravel has a great validation rule for it called 'unique:table_name'. So you use it like
'email' => 'unique:users'
But if you are like me, you may at first run into the problem where your field 'name' attribute does not match with the uniqueness field in your database. Like in my case, email field's name attribute contained 'email_address' but in my 'users' table, the column name was 'email'. So to fix this, Laravel validaton just takes another attribute with the validator.
'email_address' => 'unique:users,email'
Neat right?
Scouring through the documentation will help you more as it is doing for me. I'll keep posting further troubles I shoot in Laravel. InshaAllah.

Connect Rapoo MT750S with Linux (Tested on Manjaro)

 I bought this obvious copy of MX Master 2S in hopes of having the device switching functionality along with a lightweight body because I ha...