Thanks to visit codestin.com
Credit goes to corecode.wordpress.com

Feeds:
Posts
Comments

Archive for the ‘tips’ Category

I hate docker! Yes, I hate a bit. Why? Just because I tried a lot to do a simple application that uses a mysql everything internal into a docker and I failed, terrible! I did researches a lot about how to set it and also failed terrible. Every piece of tutorial I found was about using a docker image container and linking it to another container running the application or even some bullshit about docker mysql image and web applications. NO! That wasn’t what I looking for at all. After did more research I found docker-compose, but yet nothing about put my application in a single image/docker together with mysql. I don’t care how it’s for some people that like things separated, but I also think in docker to do things that are easy to delivery and that was why I tried that. But let’s forget the rage and focus in the tutorial itself that I hope will help some people that are/were looking for the same as me.

Step 1: Install docker and docker-compose.

I choose for docker-ce and you can follow the steps to download/install here. For docker-compose I just did a sudo apt-get install docker-compose (I’m using ubuntu 16.04 LTS).

Step 2: You probably will see some error while trying to run docker. So, before you see that add you $USER to the docker group. Do this:

  1. sudo usermod -aG docker $USER 
  2. newgrp docker

Now you can use docker without issues.

Step 3: Write your docker-compose.yaml file: In my case I wanted to use mysql and everything into the same docker container image. When I say everything I mean my mysql databases, my mysqlserver with my databases, my application itself running internally to the container accessing the database and so on…

database:
  image: mysql
  container_name: application.v1
  volumes:
    - ./somefolder/some_script_to_install_your_app.sh:/tmp/install_your_app.sh
    - ./data_volume:/opt/data_volume/
  ports:
    - "3306:3306"
  environment:
   MYSQL_DATABASE: "your_data_base_name"
   MYSQL_USER: "some_user"
   MYSQL_PASSWORD: "some_user's_password"
   MYSQL_ROOT_PASSWORD: "root_passwd"

Using the docker-compose.yaml above you’ll be able to create a mysql image/container. Cool! But what about my application? The way I did it can be a little hack or not right but in few words, who cares! In essence a mysql image is a debian imagem with mysql set in it. So you can just install things into it and put your application to into that container/image as you want. In order to do that I just add some scripts in volume to install my application and internally the application access mysql through localhost host, simple! YEAH! The only thing is that you need to run first a docker-compose up (in the same directory were you .yaml file is)   to put your mysql server to run and in separated run your application as: docker exec application.v1 application (supposing your install it in /bin). If you don’t like or don’t want do type docker exec … you can just create a shell/bash wrap around the command that 1) starts the mysql server and 2) runs your application \o/. And finally you have an application running beautifully with a internal docker container mysql.  For the inconvenience of run a docker-compose up and so your application you can just hack this and run a: docker-compose stop && docker-compose up &> /dev && docker exec application.v1. It’s not pretty as you wish probably, but it works as a charm.
I hope that I have help you somehow because the internet is fully of garbage that don’t explain nothing right of with all details. I’m sure I not put all details here, but I’ willing to help in the comments session or even when I edit this post. For now, just try as an exercise put a script into your docker-compose.yaml that e.g:  install git and git clone your app and compile it and install into /bin. See if it runs, if it access mysql internally and that is all!

Read Full Post »

AVISO: Caso você opte por realizar os passos descritos aqui saiba que você está por sua conta. O guardião do banco X ou  keylogger/malware do banco X (como é apelidado carinhosamente) serve para dar segurança nas suas transações via browser. (Ao menos é o que dizem – Talvez se você achar em algum lugar o site ou o canal com a cia de Software, que codou ele você consiga uma melhor informação).

Faz algumas semanas que tenho enfrentado a dor de cabeça que é realizar transações online via o banco X e não consigo porque o guardião simplesmente parou de funcionar. Não adianta tentar reinstalar, nem numa VM eu consegui fazer ele funcionar novamente (Eu uso ubuntu 16.04 LTS). Há erros com o certutil na hora da instalação e nada se consegue tentando instalá-lo. O que parece uma instalação normal termina com uma tela dizendo que o guardião não está instalado e dores de cabeça se você precisa pagar algo ou realizar alguma transação online.

Mas eis que hoje me passaram esta dica. Existem addons que modificam o user-agent do seu browser e ao fazê-lo o guardião é jogado de lado e você consegue realizar as transações tranquilamente (Lembre-se do disclaim acima, você está por sua conta em risco). Mas se o seu browser não possui nenhum addon que faça isso você pode adicionar o novo user-agent na mão.
Aqui eu usei o Firefox 59.01 (Quantun).
Passos:

  1. Na área de por a URL digite: about:config
  2. Agore pesquise por: general.useragent.override
  3. Provavelmente o seu browser não tem essa flag, então basta adicioná-la. Clique com o botão direito no corpo da página: new->string. Adicione o nome da variável e logo em seguida o valor dela: Mozilla/5.0 (X11; FreeBSD amd64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36
  4. Se tudo deu certo agora você pode tentar acessar o site do banco X e verificar que ele não reclama mais sobre a instalação do guardião.

Boa sorte e lembre-se, por sua conta em risco.

 

Referências:

https://www.howtogeek.com/113439/how-to-change-your-browsers-user-agent-without-installing-any-extensions/

https://udger.com/resources/ua-list/browser-detail?browser=Chrome

Read Full Post »

The basic of the magic: Quilt

If you play with debian packages you probably know what is quilt. This nice tool makes the life of any developer easy. But what about if you don’t have a debian package and still want to play using such magic?
It’s quite simple, believe me :).

First thing you need to do is, of course, install quilt, if it wasn’t already there.

  • sudo apt-get install quilt

Once you done now is time to see what it does for you. But before it let’s configure a simple .quiltrc (this one need to be there in your /home/username/.quiltrc,  OK). Into it add those lines:

QUILT_PATCHES=debian/patches # change debian/patches for patches, by example
QUILT_NO_DIFF_INDEX=1
QUILT_NO_DIFF_TIMESTAMPS=1
QUILT_REFRESH_ARGS="-p ab"
QUILT_DIFF_ARGS="--color=auto" # this is for colors

This file says basically that your patches will be in your patches folder or debian/patches if it’s debian package and that’s the basic you need to know, but if you want dig more in those variables just type man quilt on your terminal, the explanation will be there for sure.

In your source directory, if you already has a patch folder and want to apply the patches are there type quilt push -a everything will be applied.

push_quilt

If you want to check just type quilt applied.

applied_quilt

Here are the main commands you’ll need to know:

  • quilt new name_of_your_patch.patch: create a new patch ‘session’ with the passed name
  • quilt add src/somecode.c: after create a patch you need to add what files you’ll change in order to create in fact the patch.
  • quilt refresh: when you finish to edit those files add into your patch ‘quilted’ you do a refresh. If you cat patches/name_of_your_patch.patch you will see your nice patch there :).
  • quilt pop: it’s used if you want to take off a patch of the stack of patches to be applied, as you can see bellow it also restore the changes you did in the file.

pop_quilt

I think quilt is that kind of tool that make things magic to you. Imagine that you have a lot of patches to apply or a lot of backport changes to do and apply. Quilt gives to you a easy way to manager this task in a quite beautiful mode. And if you are thinking to use it only in your .deb, well I pretty sure you have fun using it in any project/source you dev.

References:

[1] https://wiki.debian.org/UsingQuilt

Read Full Post »

Sometimes is quite interesting to measure the run time of a given snippet your code. In C++11 is very simple and in my case I’m using just like that.

In my .h file I define

#ifndef __PROFILER__H
#define __PROFILER__H

#include <chrono>

using namespace std::chrono;

high_resolution_clock::time_point start(void);
high_resolution_clock::time_point end(void);
void finished(high_resolution_clock::time_point, high_resolution_clock::time_point);

#endif

And in my .cpp file

#include <iostream>
#include "profiler.h"


high_resolution_clock::time_point start(void) 
{
    return high_resolution_clock::now();
}

high_resolution_clock::time_point end(void)
{
    return high_resolution_clock::now();
}

void finished(high_resolution_clock::time_point start, high_resolution_clock::time_point end)
{
    duration<double> elapse = end - start;
    std::cout << "Elapsed time for function " << __FUNCTION__ << " :"<< elapse.count() << "s" << std::endl;
}

In order to use the code you just need to add the code ‘surrounded’ by start and finished functions, passing end as parameter. Like that:

#include <iostream>

// It's not often that you'll profiling your code so just add a D flag
// See Makefile
#if PROFILER 
#include "profiler.h"
#endif

// Some function to measure
void initLoad(void) {
    cout << "Initializing data in memory..." << endl;

#if PROFILER
    high_resolution_clock::time_point s = start();
#endif

    for (int i = 0; i < 100000000000; i++)
        cout << i << endl;

#if PROFILER
    finished(s, end());
#endif
}

In your Makefile you can use a D flag to optionally compile your code to use the profiling, such as:

SRC = $(wildcard src/*.cpp)
PROG_NAME = test
INCLUDE = -Iinclude 
LIBS = -Ilibs
FLAGS = -std=c++0x
DFLAGS = -q -nx -tui
PROFILER ?= 0

# To use profiler just add: make PROFILER=1
ifeq ($(PROFILER), 1)
	FLAGS += -DPROFILER
endif

test:
	g++ -o $(PROG_NAME) $(SRC) $(INCLUDE) $(LIBS) $(FLAGS)

Now you can use this ‘lib’ for any source you want to measure the run time typing: make PROFILER=1 🙂

That’s all folks!

Read Full Post »

Screen tip

How about to use bash configuration in your screen session?
Well that is very simple to config. Just add this into your .screenrc

defshell -bash`

Since sometimes your bash in a screen session is not identify in your terminal, you can also add this info into your PS variable in .bashrc. Just adding something like this in the end of your .bashrc:


set_term_name() {
    current="screen"
    if [ $TERM=$current ];
    then
        echo $TERM
    fi
}
export PS1="\u@\h \[\033[32m\]\w[\033[33m\] \$(set_term_name)\[\033[00m\] $"

That’s all folks!

Read Full Post »

Sometimes you need to delete a bunch of files that has a limit number of lines. One option is to see one by one and rm it. But you can make you day more agile using the follow commands together 🙂

find <directory> -type f -exec sh -c '[ $(wc -l < "$1") -lt 20 ] && rm -- "$1"' inline {} \;

Let’s explain what it does. <directory> is the folder where the files you want to delete are. I set 20 lines as minimum, just change it using your criteria.

find -type f, says that is a file we are looking for. -exec sh says we want to execute some command for each entry we find using find <dir> -type f. In our case  we exec a sh -c. The -c options says to sh<ell> that we will run a inline code without open a file with shell commands. The follows commands into ” says we want to count the number of line in entry file $1 and if it is less than 20 we’ll delete it. inline and {} \; are just some find command conventions.

References:

[1] http://stackoverflow.com/questions/935251/linux-delete-files-that-dont-contain-specific-number-of-lines

Read Full Post »

The magic of crontab

Sometimes you need to auto things, task, whatever. As an example I have access to a local network but first to get into it I need to reach the external network. What happens is that the PC I access via ssh sometimes changes its IP, since I don’t have any control to put it fixed I  need some how to know what is the current IP it has.

So, why not to use crontab in a new way and update my current IP :).

That’s all about the follow bash script

#!/bin/bash

# Gets my current ip and send it over ssh to the server that is also the entry point
# for local network
IP=`ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1  -d'/'`
ssh -p 4422 user_name@server_ip "echo '$IP' > current_ip"

One thing to remember is to use ssh-copy-id, this way you don’t need to type passwd ok 😀

Now, the magic with crontab can be resumed by:

47 11 * * * bash /path_to_script_above/update_ip.sh

But what does mean those numbers and *?

* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

Easy right?
In order to add a new crontab just type: crontab -e, choice your favorite editor and add some line :v

Read Full Post »

Tip to gmail

Usually in our email accounts we have many folders, each one to an specify purpose. I dont remember me when, but one day a mate asked me about folders in gmail. Well, I answered that gmail did not has folders, instead this it has labels and dont give to make much with them. So, today seeing a lot email from a django group, seeking from a way to make a folder or else thing I saw that I made a mistake. Because, gmail has folders, but like labels :P.
Now, I will show how this great mistery works.

  1. In your gmail, should have you many labels,right?
  2. Go to settings and choose  filter;
  3. Do a  filter, like show the image bellow [Figure 1];
  4. Click  next and mark, like show the image [Figure 2], option “Skip the Inbox (Archive it)”.
  5. Update your filter and is done!

 

Figure 1. Make your filter , choose you label and next.

Figure 2: Mark "skip the inbox" and update.

Obs.: Remember , create labels to do this.

Read Full Post »

Design a site like this with WordPress.com
Get started