Monday, September 2, 2019

How to use git to connect to Github and Gitlab on the same machine

Well, easy problem to solve.

Just edit/create the SSH config file ~/.ssh/config and add the two providers

Host github
HostName github.com 
IdentityFile ~/.ssh/github

Host gitlab
HostName gilab.com 
IdentityFile ~/.ssh/gitlab
changing/defining your SSH keys for each provider.

Go to Github config area and Gitlab config area and add your PUBLIC key (normally something like id_rsa.pub) to the list of keys.

That's all, now you can work with both platforms.


Saturday, August 31, 2019

Setting up PostgreSQL DB with Java Spring Boot and Gradle

Let's start checking the dependencies, considering that I am using for this example spring boot 2.1.7.

Go to your build.gradle file and in the section dependencies be sure that you have the following:

compile 'org.springframework.boot:spring-boot-starter-jdbc'
compile 'org.postgresql:postgresql:42.2.2'

If you still don't have the resources.config.application.yml file, create it or edit it adding the following lines:

spring:
  application:
    name: appname
  profiles:
    active: dev

spring.datasource:
  driver-class-name: org.postgresql.Driver
  url: ${ENV_DATASOURCE_URL}
  username: ${ENV_DB_APP_USER}
  password: ${ENV_DB_APP_PASSWORD}

server:
  port: 8080
  servlet.context-path: /appname

logging.file: logs/${spring.application.name}.log

and create an environment variables file outside the project, for example, .env file in your project root folder. Don't forget to add the file to .gitignore. For example:

ENV_DB_HOST=development
ENV_DB_PORT=5432
ENV_DB_SCHEMA=schema
ENV_DB_APP_USER=username
ENV_DB_APP_PASSWORD=password

and run

$ export $(cat .env | xargs)

Now create a resolurces.schema.sql file with the schema that you want to create. For example:

CREATE TABLE clients
(
  id                    bigserial primary key,
  uuid                  varchar(255)             not null unique,
  name                  text                     not null,
  version               varchar(64)              null,
  template              json                     null,
  create_time           timestamp with time zone not null,
  update_time           timestamp with time zone not null
);

And... that's all! You are ready to start working with your PostgreSQL database.

You can include in your resources.application.properties file the following lines

spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
#spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.ddl-auto=none

The first remove a possible error launching your project. The second drop and creates the database using the schema.sql file that you defined previously. You should use this line only once and never in production. The third line deactivates this functionality. I recommend you to use update if you want to start using migrations or jump directly into Flyway to versioning your migrations.






Wednesday, March 8, 2017

Setting up Strict Transport Security (HSTS) in NGINX under a Vagrant Box

Today we I going to show how to set up HTTP Strict Transport Security (HSTS) in NGINX to improve slightly the security of your webapp.

HTTPS (HTTP encrypted with SSL or TLS) makes very difficult for an attacker to intercept, modify, or fake traffic between a user and the website. HSTS seeks to deal with potential vulnerabilities by instructing the browser that a domain can only be accessed using HTTPS. Even if the user enters a plain HTTP link, the browser will strictly upgrade the connection to HTTPS.

Let's learn by doing setting up HSTS in an standard Vagrant box running NGINX.

First lets create a self-signed SSL certificate for our HTTPS connection to the vagrant box (by default there is only HTTP connection available).

1) Create the certificates directory

$ sudo mkdir YOUR_CONFIG_FOLDER/certs

2) Create the key, csr and certificate:

$ cd YOUR_CONFIG_FOLDER/certs/

# Generate a new private key
$ sudo openssl genrsa -out "devcert.key" 2048

# Generate a CSR using the private key for encryption. It is interesting to enter the server name in this step.
$ sudo openssl req -new -key "devcert.key" -out "devcert.csr"

3) Sign and generate the certificate devcert.crt

$ sudo openssl x509 -req -days 365 -in "devcert.csr" -signkey "devcert.key" -out "devcert.crt"


Now set up the HSTS:

1) Edit the available site you want to setup in the sites-available folder

$ sudo vi /etc/nginx/sites-available/YOUR_SITE

2) Add the lines

add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";

ssl_certificate     YOUR_CONFIG_FOLDER/certs/devcert.crt;
ssl_certificate_key YOUR_CONFIG_FOLDER/certs/devcert.key;

after the listen or server_name variables. It will look like that:

server {
    listen 443 ssl;

    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";

ssl_certificate     YOUR_CONFIG_FOLDER/certs/devcert.crt;
ssl_certificate_key YOUR_CONFIG_FOLDER/certs/devcert.key;

    [...]
}

3) Save the changes and restart the nginx service

$ sudo service nginx restart

That's all. Now you should be able to see also the HSTS header in the server response:

Strict-Transport-Security:max-age=31536000; includeSubDomains




Thursday, September 29, 2016

Setting up PHP-Kafka using librdkafka wrapper phpkafka from EVODelavega under Ubuntu.


We will install kafka for PHP based on the libraries:

https://github.com/EVODelavega/phpkafka
https://github.com/edenhill/librdkafka

Setting up
==========
Fast way (Only Ubuntu)
--------

$ sudo apt-get install librdkafka1

Dependencies
------------
First let install some dependencies (installing manually)

$ sudo apt-get install libsasl2-dev liblz4-dev

Installing php-rdkafka (Other OS)
----------------------

$ git clone https://github.com/edenhill/librdkafka/
$ cd librdkafka
$ ./configure
$ make
$ make test
$ sudo make install

Installing phpkafka Extension
-----------------------------

$ git clone https://github.com/EVODelavega/phpkafka.git
$ cd phpkafka
$ phpize
$ ./configure --enable-kafka
$ sudo make install

If you want to check that kafka is in place:

$ php -m | grep kafka
kafka


Final PHP configuration
-----------------------
1) Create a PHP file containing only the call to phpinfo()

2) Run the script from your browser and check the path of the config ini files that your system is using. For example, my additional INI files are in /etc/php5/fpm/conf.d.

3) Creates a new file librd.con in that folder. Example:

$ sudo touch /etc/php5/fpm/conf.d/librd.conf

4) Add to the file the path of your libraries: /usr/local/lib. You can also do (as superuser):

$ echo "/usr/local/lib" >> /etc/ld.so.conf.d/librd.conf

6) Create the file 20-kafka.ini in your additional INI directory:

$ sudo vi /etc/php5/fpm/conf.d/20-kafka.ini

and insert the line:

extension=kafka.so

7) Run the command

$ sudo ldconfig

8) Check that everthing is file and the extension was loaded

$ ldconfig -p | grep kafka

9) Reset services

sudo /etc/init.d/php5-fpm restart


Setting the kafka with Docker for testing
=========================================

1) Download and install the containers (Check https://hub.docker.com/r/confluent/platform/)

$ docker-machine start
$ eval $(docker-machine env)
$ docker pull confluent/platform

2) Run the hub

# Start Zookeeper and expose port 2181 for use by the host machine
docker run -d --name zookeeper -p 2181:2181 confluent/zookeeper

# Start Kafka and expose port 9092 for use by the host machine
docker run -d --name kafka -p 9092:9092 --link zookeeper:zookeeper confluent/kafka

# Start Schema Registry and expose port 8081 for use by the host machine
docker run -d --name schema-registry -p 8081:8081 --link zookeeper:zookeeper \
--link kafka:kafka confluent/schema-registry

# Start REST Proxy and expose port 8082 for use by the host machine
docker run -d --name rest-proxy -p 8082:8082 --link zookeeper:zookeeper \
--link kafka:kafka --link schema-registry:schema-registry confluent/rest-proxy

3) Everything now is ready to continue


Working with kafka
==================

Create the files producer.php and consumer.php as follows in this section. For testing, open 2 terminals and run first the consumer.php script. On the other terminal run the producer.php script many times as you want. You should see how the message from the producer is picked by the consumer.

PHP Producer (producer.php)
------------
<?php

$avro_schema = [
"namespace" => "yournamespace",
"type" => "record",
"name" => "machineLog",
"doc" => "That is the documentation",
"fields" => [
["name" => "host", "type" => "string"],
["name" => "log", "type" => "string"],
]
];

$records = [
[ "host" => "hostname1", "host" => "cpu 67590 0 28263 13941723 602 7 1161 0 0 0" ],
[ "host" => "hostname2", "host" => "cpu0 67591 0 28266 13944700 602 7 1161 0 0 0" ]
];

$msg = json_encode([
"value_schema" => $avro_schema,
"records" => $records
]);

//var_dump(json_encode($msg));
$kafka = new Kafka("192.168.99.100:9092");
try {
$kafka->produce("jsontest", $msg);
} catch (Exception $e) {
echo $e->getMessage() . PHP_EOL;
}

$kafka->disconnect(Kafka::MODE_PRODUCER);



PHP Consumer (consumer.php)
------------
<?php

$kafka = new Kafka("192.168.99.100:9092");
$partitions = $kafka->getPartitionsForTopic('jsontest');
$kafka->setPartition($partitions[0]);
$offset = 1;
$size = 1;

while (1) {
try {
$messages = $kafka->consume("jsontest", $offset, $size);
if (count($messages) > 0) {
foreach ($messages as $message) {
echo $message . PHP_EOL;
$offset += 1;
}
}
} catch (Exception $e) {
echo $e->getMessage() . PHP_EOL;
break;
}
}

$kafka->disconnect();


That's all! Enjoy Kafka!

Sunday, September 4, 2016

How to run Laravel 5.3 in a 1and1 shared hosting


How to run Laravel 5.3 in a 1and1 shared hosting

1.- Go to your Control Panel and set the default PHP version to 5.6 for your current domain.

2.- Artisan script

Open artisan file and replace the first line to:

#!/usr/local/bin/php5.5

which corresponds to the 1and1 php5.5 path. Now you can run artisan using ./artisan.

3.- composer.json

Open composer.json file and replace all the php references to php5.5. For example:

[...]

"scripts": {
        "post-root-package-install": [
            "/usr/local/bin/php5.5 -r \"copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "/usr/local/bin/php5.5 artisan key:generate"
        ],
        "post-install-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postInstall",
            "/usr/local/bin/php5.5 artisan optimize"
        ],
        "post-update-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postUpdate",
            "/usr/local/bin/php5.5 artisan optimize"
        ]
    },

[...]


Now you can update your dependencies:

Run the command:

curl -sS https://getcomposer.org/installer | php5.5 

to get your composer.phar script. Now you can update as follow:

php5.5 composer.phar update


4) .htaccess

Go to your public folder and replace the default setup with the following:




5) .env

Of course you have to configure your database credentials an the rest of setups editing the file .env.

Happy Laravel

Wednesday, August 3, 2016

How to download a file grom S3 with Laravel

Today let's explain how to download a file from S3 using laravel. Next time I will explain how to upload the file.

1) Set up the bucket name. Ex. YOUR_BUCKET_NAME

2) Set up the bucket policy

{
  "Id": "PolicyXXXXXX",
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "XXXXXX",
      "Action": [
        "s3:GetObject"
      ],
      "Effect": "Allow",
      "Resource": "arn:aws:s3:::testfcce/*",
      "Principal": "*"
    }
  ]
}
You can use the AWS policy generator for that:
http://awspolicygen.s3.amazonaws.com/policygen.html

3) Generate a credentials to have access to the bucket (if you don't have)

4) Change the config/filesystems.php to

        's3' => [
            'driver' => 's3',
            'key'    => env('S3_KEY'),
            'secret' => env('S3_SECRET'),
            'region' => env('S3_REGION'),
            'bucket' => env('S3_BUCKET'),
        ],

5) Add the keys to the .env file

S3_USER=YOUR_USER_NAME
S3_KEY=YOUR_KEY
S3_SECRET=YOUR_SECRET
S3_REGION=eu-central-1
S3_BUCKET=YOUR_BUCKET_NAME

6) Example how to check if a file called image001.jpg is in the bucket and in positive case get the URL

$s3 = Storage::disk('s3');

if ($s3->exists('image001.jpg'))
{
    $bucket = 'YOUR_BUCKET_NAME';

    return $s3->getDriver()->getAdapter()->getClient()->getObjectUrl($bucket, 'image001.jpg');
}


References URLs:

https://return-true.com/uploading-directly-to-amazon-s3-from-your-laravel-5-application/
http://www.server180.com/2016/01/upload-files-to-s3-using-laravel-52.html
https://chrisblackwell.me/upload-files-to-aws-s3-using-laravel/

Best

Tuesday, March 1, 2016

Setting up Capistrano for Laravel 5 in Ubuntu

Capistrano for Laravel 5 in Ubuntu

Install Capistrano in Ubuntu


gem install capistrano

SSH access

Capistrano deploys using SSH. Thus, you must be able to SSH from the deployment system to the destination system for Capistrano to work. You can test the SSH connection using a ssh client, e.g.

ssh your_username@destinationserver

If you cannot connect, in order to be able to do this you need two things:
1) An account on the target machine
2) Your public key added to ~/.ssh/authorized_keys file.

So, SSH into the staging machine and add your key to username’s ~/.ssh/authorized_keys. You should now be able to pull from git on the server without it asking you for identification.

Server privileges

In order to deploy, the user needs some special privileges.

First add a group named deploy to the server:

sudo groupadd deploy

We now need to add ourselves and any other users that need access (other developers) to the deploy group, and the user that executes our web server. Opening the /etc/group file, and look for the line that beings with deploy. We append users, comma delimited. In this example I am using a nginx server.

deploy:x:1003:your_username,nginx

Now we have a deploy group, whose members include us, any other developers who will be deploying and the web server user.

Add the deploy group to the /etc/sudoers. For that create file /etc/sudoers.d/01deply to enable members of deploy group to access root via sudo without passwords

%deploy ALL = (ALL) NOPASSWD: ALL

and set up 440 permissions to the file

chmod 440 /etc/sudoers.d/01wheel

Project structure

Now define the new structure for your project

projectname
 |-- components
 |-- deploy

all of then will have 755 permissions and g+i flag that indicates that old the subdirectories will have the same privileges as the project directory.

sudo mkdir -p new-project/components 
sudo mkdir -p new-project/deploy 
sudo chmod -R 775 new-project 
sudo chmod -R g+i new-project 
sudo chown -R your_username new-project 
sudo chgrp -R deploy new-project

On the local machine

Go to the Laravel root project's folder and run

cap install

The command creates the following structure:

mkdir -p config/deploy 
create config/deploy.rb 
create config/deploy/staging.rb 
create config/deploy/production.rb 
mkdir -p lib/capistrano/tasks 
create Capfile Capified

where staging.rb and deploy.rb are the files that we are going to work.
We have to create an additional file to store our credentials:

vi config/myconfig.rb

set :ssh_options, { user: 'your_username' }
set :tmp_dir, '/home/your_username/tmp'



Adding Task to Capistrano

Vendor files and config files

Edit the config file deploy.rb

vi config/deploy.rb

and add the next lines after the lock line.

set :application, 'new-project'
set :repo_url, 'git@github.com:githubusername/project.git'

set :deploy_to, '/var/www/new-project/deploy'

components_dir = '/var/www/new-project/components'
set :components_dir, components_dir

# Devops commands
namespace :ops do

  desc 'Copy non-git files to servers.'
  task :put_components do
    on roles(:app), in: :sequence, wait: 1 do
      system("tar czf .build/vendor.tar.gz ./vendor")
      upload! '.build/vendor.tar.gz', "#{components_dir}", :recursive => true
      execute "cd #{components_dir}
      tar -zxf /var/www/new-project/components/vendor.tar.gz"
    end
  end

end


Now configure the staging.rb condig file

vi config/deploy/staging.rb

adding the next lines:

role :app, %w{your_username@destinationserver}

require './config/myconfig.rb'

namespace :deploy do

  desc 'Get stuff ready prior to symlinking'
  task :compile_assets do
    on roles(:app), in: :sequence, wait: 1 do
      execute "cp #{deploy_to}/../components/.env.staging.php #{release_path}"
      execute "cp -r #{deploy_to}/../components/vendor #{release_path}"
    end
  end

  after :updated, :compile_assets

end


# Devops commands
namespace :ops do

  desc 'Copy non-git ENV specific files to servers.'
  task :put_env_components do
    on roles(:app), in: :sequence, wait: 1 do
      upload! './.env.staging.php', "#{deploy_to}/../components/.env.staging.php"
    end
  end

end


Deploy

Let's try if everything works. Check if the tasks are available for cap writing cat -T.

Now let's transfer the config and vendor files to the server:

cap staging ops:put_components 
cap staging ops:put_env_components

Once the files are in the server we can deploy

cap staging deploy


Possible errors

Is you receive an error like this:

the deploy has failed with an error: Exception while executing as your_username@destinationserver: git exit status: 128 git 
stdout: Nothing written git stderr: Error reading response length from authentication socket. 
Permission denied (publickey). 
fatal: Could not read from remote repository.

that means you have to follow an additional step in your local terminal. Run

eval $(ssh-agent) 
ssh-add ~/.ssh/id_rsa

And try again, e.g.

cap staging deploy

That's all

Hope helps

Monday, February 29, 2016

Rustic way to import your AngularJS configuration from an external JS file

Many times we want to store our constants/config details into a different file to simplify the deployment process or simply to organise the code.

Here you can see a very basic way to save your app's config file into a different location.

https://jsfiddle.net/sergioloppe/bvajr7bw/

The code is self-explanatory.

Hope helps





Friday, February 12, 2016

How to avoid errors using the function eval in php (under any framework)

Sometimes when you have to evaluate some mathematical expressions with eval it is difficult to control the errors even using try-catch. Here you have a trick to solve that problem:


$calculation = '1/0';

$result = @eval($calculation . "; return true;");
if($result) {
$rule2_val = doubleval(@eval($calculation));
}
else {
return 'Impossible to perform the calculation';
}

Hope helps!


Sunday, January 24, 2016

Using Illuminate/Html in Laravel 5.2


As you know, long time ago, Illuminate/Html was remove from laravel because it is not a core feature. Today I need to use Form for one of my projects and I am going to show how to include in Laravel 5.2. (I prefer not to use Form but you never know).

1) Add to composer.json

"illuminate/html": "5.*"

2) Update your project

composer update

3) Edit the file config/app.php and add to providers

Illuminate\Html\HtmlServiceProvider::class,

4) Edit config/app.php and add to aliases

        'Html'      => Illuminate\Html\HtmlFacade::class,
        'Form'      => Illuminate\Html\FormFacade::class,

5) Check it for example using the code:

{!! Form::open() !!}

{!! Form::text('name', @$name) !!}

{!! Form::password('password') !!}

{!! Form::submit('Send') !!}

{!! Form::close() !!}


If you receive the error:

[Symfony\Component\Debug\Exception\FatalErrorException]                   

  Call to undefined method Illuminate\Foundation\Application::bindShared() 

that means you need an additional step. Laravel 5.2 use singleton instead bindShared which is deprecated. You have to change that in your vendor HtmlServiceProvider as:

/**
* Register the HTML builder instance.
*
* @return void
*/
protected function registerHtmlBuilder()
{
$this->app->singleton('html', function($app)
{
return new HtmlBuilder($app['url']);
});
}

/**
* Register the form builder instance.
*
* @return void
*/
protected function registerFormBuilder()
{
$this->app->singleton('form', function($app)
{
$form = new FormBuilder($app['html'], $app['url'], $app['session.store']->getToken());

return $form->setSessionStore($app['session.store']);
});
}


Hope it helps





Wednesday, January 13, 2016

Github Two-Factor Authentication on the command line


Today is the day to deploy things and... also the day to set up the Github's two-factor authentication on the command line. In order to be able to work with your repositories once you set up your w-factor authentication follow the next steps:
  1. Log into your Github account and go to Settings
  2. Select the "Personal Access Tokens" tab on the left-hand navigation
  3. Click on "Generate new" personal access token and enter a proper description
  4. Copy the 40 characters long token and use it as your password on the command line
That's all. 

Hope help!

Sunday, January 3, 2016

Installing Ruby on Rails in a Ubuntu/trusty64 Vagrant Box under OS El Capitan

To install Ruby on Rails in a Ubuntu Vagrant Box in a Mac follow the steps below:

1) Download and install vagrant and virtualbox
  - http://www.vagrantup.com/downloads
  - https://www.virtualbox.org/wiki/Downloads

2) Install the Ubuntu box
# vagrant init ubuntu/trusty64

4) Set up the forwarding

# vi Vagrantfile

Add the line in the forwarded port mapping

config.vm.network "forwarded_port", guest: 3000, host: 3030, protocol: 'tcp', auto_correct: true

5) Runing Ubuntu

# vagrant up --provider virtualbox
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'ubuntu/trusty64' is up to date...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 3000 (guest) => 3030 (host) (adapter 1)
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default: 
    default: Guest Additions Version: 4.3.34
    default: VirtualBox Version: 5.0
==> default: Mounting shared folders...
    default: /vagrant => /Users/sergio/vagrant_boxes
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.


6) Access the virtual machine
# vagrant ssh

Now it's time to install Ruby on Rails. Follow the next steps_
1) Installing dependencies

# cd
# sudo apt-get update
# sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev

2) Installing Ruby dev packages

# sudo apt-get install ruby-all-dev
# sudu updatedb

3) Installing rbenv

# cd
# git clone git://github.com/sstephenson/rbenv.git .rbenv
# echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
# echo 'eval "$(rbenv init -)"' >> ~/.bashrc
# exec $SHELL

# git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
# echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
# exec $SHELL

# git clone https://github.com/sstephenson/rbenv-gem-rehash.git ~/.rbenv/plugins/rbenv-gem-rehash

# rbenv install 2.2.3
# rbenv global 2.2.3
# ruby -v

4) If you want to avoid installing the packages documentation execute the following commands

echo "gem: --no-ri --no-rdoc" > ~/.gemrc
gem install bundler

4) Installing NodeJS

# curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
# sudo apt-get install -y nodejs

5) Installing Rails

# gem install rails -v 4.2.4
# rails -v

6) Set up PostgreSQL

sudo sh -c "echo 'deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main' > /etc/apt/sources.list.d/pgdg.list"

wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add -

sudo apt-get update
sudo apt-get install postgresql-common
sudo apt-get install postgresql-9.3 libpq-dev

7) Create a PostgrSQL user

# sudo -u postgres createuser INSERT_THE_USERNAME_HERE -s
# sudo -u postgres psql
postgres=# \password INSERT_THE_USERNAME_HERE

Now it is time to check the installation creating a rails application.

1) We will use sqlite in this case

# rails new myapp

If you want to use your PostgreSQL server use instead

# rails new myapp -d postgresql 

2) Go to the new app and create the database

# cd myapp
# rake db:create

3) Run the rails server

# rails s -b 0.0.0.0
=> Booting WEBrick
=> Rails 4.2.4 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2016-01-03 16:53:10] INFO  WEBrick 1.3.1
[2016-01-03 16:53:10] INFO  ruby 1.9.3 (2013-11-22) [x86_64-linux]
[2016-01-03 16:53:10] INFO  WEBrick::HTTPServer#start: pid=1956 port=3000


4) Check the server from your local machine at port 3030: http://127.0.0.1:3030

Hope help!



Friday, December 11, 2015

How to add a timestamp to your log files in your bash scripts in Linux


Today I show how to add a timestamp to your log files in your bash scripts. That is easy. In this solution we have to include the function

adddate() {
    while IFS= read -r line; do
        echo "$(date) $line"
    done
}

in your script file and add pipe the command to the function using the systax:

command | adddate

For example:

#!/bin/bash
adddate() {
    while IFS= read -r line; do
        echo "$(date) $line"
    done
}
echo -e "Doing something"
ls -la | adddate

Friday, August 21, 2015

Multiple DB Connections in Laravel 5. How to connect to a different Schema in Postgresql


What happens if you are using Laravel and PostgrSQL and you have to connect to multiple schemas. This post shows how to solve this problem easily in a few steps:



1) Create a new connection in the file app/config/database.php. For example the "test" connection linked to the "your_schema" schema.

...

'connections' => [

...

'test' => [
'driver'   => 'pgsql',
'host'     => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'homestead'),
'username' => env('DB_USERNAME', 'homestead'),
'password' => env('DB_PASSWORD', 'secret'),
'charset'  => 'utf8',
'prefix'   => '',
'schema'   => 'your_schema',
],

...
],

...

2) Create a new Model using artisan. For example: OtherDB model.


use Illuminate\Database\Eloquent\Model;

use Illuminate\Support\Facades\DB;

class OtherDB extends Model {

protected $connection = 'test'; // Connection name
protected $table = 'masterscale'; // table name

public $timestamps = false;

public static function loanSearch(){

$query = new OtherDB;

// Example 1: Using Eloquent
//dd($query::all());

// Example 2: Using a raw query
$masterscale = $query::where('risk_class','B1')->get();
foreach ($masterscale as $value) {
echo "
Risk class ". $value->risk_class . " has score " . $value->score;
}

//dd($masterscale);

return;
}

}


3) Create a controller with artisan or use an existing one. For example: otherDBController


use App\OtherDB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class otherDBController extends Controller {

/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$loans = OtherDB::loanSearch();
dd($loans);
}

}

4) Create the route to the controller is it does not exist

Route::resource('test', 'otherDBController');

5) Test it :)


Monday, June 15, 2015

Searching for packages in R. What is the best package to analyse my data?


Many times we want to apply some method or we need an algorithm but we don't have the time to implemented or simply we don't know how to do it. For this situations is good to know how to look for the best or more standardized packages that R offered.

Here is my trick.


  1. Install the package "sos"
  2. Use the function findFn to find the related packages to a given topic.
Let's first install the package sos using the code:


install.packages("sos")
library("sos")

Now we want to know which packages are available for example for "Particle Swarm Optimisation". Let's use the function findFn as follow:

findFn("Particle Swarm Optimisation")
found 18 matches;  retrieving 1 page

Downloaded 10 links in 4 packages.

And automatically we will be redirected to a website with the list of packages founded:



I don't know which is the best package for PSO but at least I know the best scored.




Two-Vector Dictionary in R using "match"


Something common in R is to have two vectors representing a key-value dictionary, for example:


  • Car name
  • Car price

Let's see an example:

We created two vectors using the dataset "car.test.frame" included in the package rpart. The first vector contains the name of the cars (key) and the second vector the list of prices (value). The function "match" is used to look for a couple of cars in the vector of names. If there is a match we get the index in the car_name vector (in our case, 37 and 43). Now we can use the indexes to work with the vector car_price or with the original data frame as a key-value dictionary.

Here is the code:

library(rpart)
str(car.test.frame)

# Get the name of the cars
car_name = row.names(car.test.frame)
car_price = car.test.frame$Price

# List of car's names
cars = c("Volvo 240 4", "Ford Taurus V6")
match(cars, car_name)

# Get the price of the car
car.test.frame[match(cars, car_name),]$Price

# or using the value-vector
car_price[match(cars, car_name)]

Tuesday, June 9, 2015

Problems printing graphical contents / canvas in lastest version of Chrome.


In the last release of Chrome something was wrong printing canvas contents. To see the problem visit the website:

https://developers.google.com/maps/documentation/javascript/examples/circle-simple

  1. Do you see the circles on the map?
  2. Print the page
  3. Observe the differents with the printing version. Do you see the circles? 
If the answer is not, this post solves your problem.


We can solve temporarily the problem until Google launch a new version of Chrome or an update, following the next steps:

  1. Go to: chrome://flags/   (writing "chome://flags/" in your navigation bar)
  2. Deactivate display list 2D canvas.
  3. Restart Chrome (clicking on "Restart" at the bottom of the flags page).
Sorry my Chrome is in German. chrome://flags


Try again printing the previous page and observe the map. Now you should see the circles on the map.






Wednesday, February 11, 2015

How to speed up your site with htaccess


Loading speed is a common problem considering that your website is downloading again and  again the same files (css, js, png,...).

Let's load a couple of modules:

  • mod_expires
  • mod_header

The idea is to use the max-age header parameter which lets us says "this file expires 1 week from today", which is simpler than setting an explicit date. The max-age is measured in seconds, ie. 1 day = 86400, 1 week=604800, and so on.

In uuntu we can write:
sudo a2enmod headers
sudo a2enmod expires
sudo service apache2 restart
Then edit your .htaccess and insert the folowing lines:
#
# Configure mod_expires
#
# URL: http://httpd.apache.org/docs/2.2/mod/mod_expires.html
#

    ExpiresActive On
    ExpiresDefault A3600
    ExpiresByType image/x-icon A2592000
    ExpiresByType application/x-javascript A86400
    ExpiresByType application/javascript A86400
    ExpiresByType text/javascript A86400
    ExpiresByType text/css A86400
    ExpiresByType image/gif A604800
    ExpiresByType image/png A604800
    ExpiresByType image/jpeg A604800
    ExpiresByType application/font-woff A604800
    ExpiresByType application/octet-stream A604800

#
# Configure mod_headers
#
# URL: http://httpd.apache.org/docs/2.2/mod/mod_headers.html
#

   
        Header set Cache-Control "max-age=86400, public"
   

   
        Header set Cache-Control "max-age=600, private, must-revalidate"
   

    Header unset ETag
    Header unset Last-Modified

Done!. You can try now and check if your page is using the cache.

Hope help



Monday, October 27, 2014

Port tunneling on Mac with SSH


Imagine you need to connect from your browser to another computer which has access to an specific service, por example a private control panel. How to do it using SSH on a mac?

Well, lets describe the problem:


I want to access to the Service 192.168.300.12:11801 which can be accesed only from the Server SSH 192.168.200.1. To increase the complexity of the problem, we should use HTTPS to access the Service, ie, the direct URL from Server SSH looks like: https://192.168.300.12:11801/index.html

Well, first we open a terminal and execute the command:

$ sudo ssh -L 443:192.168.300.12:11801 -p 22 -l username -N 192.168.200.1

The command opens a tunnel between our machine on port 443 to machine 192.168.300.12 using the port 11801 through 192.168.200.1. Just what we want. "-p  20" is only the SSH port, "-l username" is clear and "-N" indicates that we will not execute remove commands.

Now, we can simply set up a SOCKS-Proxy going to Mac > Installation > Network > More Options. Select the tab "Proxies" and enable the protocol SOCKS-Proxy. You need only to insert:
  • SOCKS-Proxy-Server: 127.0.0.1
  • Port: 443
Now, open your favorite browser and write the URL:
https://localhost/index.html
to access to the Service.

Hope help



How normalization between two numbers works


A friend asked me today about how to normalize a range into another one. Well, given a range from A to B we want to convert it to a scale of C to D, where A maps to C and b maps to D, and we want to do it with a linear function. For example, if C=1 and D=5, ie, interval [1,5], that means that A maps 1 and B maps 5.

The following linear equation does exactly what we need

f(x) = 1 + (x-A)*(5-1)/(B-A) 

because
f(A)=1 + (A-A)*(5-1)/(B-A) = 1+0 = 1 
and
f(B)=1 + (B-A)/(B-A)*(5-1) = 1 + 1*(5-1) = 5.

In general, the linear ecuation looks like:

f(x) = C + (x-A)*(D-C)/(B-A)
Easy peasy, isn't it?

Hope help!