100 Days of Code – second edition

In 2016/04/10 I start 100 days of code challenge. It was not easy and I almost have done it. Almost it means that one day stays without commit. But happy information is that 99 days was with at least one commit. I was a very busy time for me. I learn a lot, especially how to code in Python. But this one-day caused that this whole challenge it does not count. So from today I start again nex, the second edition 100 days of code challenge. Rules are the same.

zrzut-ekranu-2016-11-23-o-01-26-32

As we can see in above picture there is the screenshot from my GitHub account from today.

Simple scaffold application in Rails

In last few hours, I was creating some application that could work in near future as a blog. But before that good to make some nice structure to have a base of application. So this post is about: how to create a blog using scaffolding. Scaffolding is a quick way to generate some of the most important pieces of an application. If I want to create models, view and also controllers for a new resource in a single operation scaffolding is a great way to make the job quick and easier.

Creating a new rails app is very easy. To do that, I can type only:

rails new scaffold_app

The whole structure of app is as:

To create an MVC components needed to post comments in the scaffold app directory we need to use scaffold generator:

rails generate scaffold post title:string body:text

And this generator creates post:

rails generate scaffold comment post_id:integer body:text

Creates post and comments tables in database:

rake db:migrate

Rails uses rake commands to run migrations:

Rake routes are the lists of all the URLs currently uses in the application.

zrzut-ekranu-2016-11-21-o-01-09-00 zrzut-ekranu-2016-11-21-o-01-11-43zrzut-ekranu-2016-11-21-o-01-09-05

 

After running rails server we can see that some very simple post and comments formulas were created. Good idea is to add some user with some unique id – integer type, public name – string type, email address – string type that will be used as a username. id, name, email attributes are columns in users table in a database.

To user should be associated each post that user will create:

rails generate scaffold Micropost content:string user_id:integer

So, for now, we should have scaffold application with:

  • comments
  • posts
  • users
  • and adding each of them to database.

Reference:

FATAL: database files are incompatible with server

A few day ago I was struggling with some strange (for me as a beginner) problem with PostgreSQL database during generate some application skeleton. Running some: rails new app —database=postgresql and after that: rake db:migrate or rake db:create cause the fallowing problem:

I restart also database but the problem still appears. So I start to looking for some answer. First I check if my database it’s running, to perform some simple check:

ps auxww | grep postrges

This simple command should display all processes that are running and also connected with postres, but of corse, in my case, output was totally different. PostgreSQL was not running at all:

andrzejdubaj     19418   0,0  0,0  2442020    896 s003  S+   12:43     0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn postgres

I have PostgreSQL data files from a previous version of PostgreSQL and they are not compatible with current one. One solution was to delete all data and get a fresh database, basically to wipe data from PostrgreSQL completely including the user and data files.

rm -rf /usr/local/var/postgres && initdb /usr/local/var/postgres -E utf8

After that:

rake db:setup

and

rake db:migrate

from my rails application to get setup again. Next step was to check if my changes work as intended:

First process that is displayed gere is a masterserver process. The command shown for it are the same ones given when it was launched. Next two ones are the bacground workers processes launched automatically by the master process. Each of the remaining processes is a server process handling one client connection.

So I we can see this simple solution resolve problem with database on version 9.5.


Reference:

String interpolation in Ruby

Today I have struggled with one exercise from exercism.io. In the first look exercise was very simple: ‘Write a function that greets the user by name, or by saying “Hello, World!” if no name is given. For an input of “Alice”, the response should be “Hello, Alice!”. If a name is not given, the response should be “Hello, World!”‘.
On the first look, this task was very simple. To create some function that takes some argument instance variable. Below I passed the first resolution:

class HelloWorld
  def self.hello(name = "World")
    @name = name
    "Hello, #{@name}!"
  end
end

This solution is ok, but this is one little thing that can be changed to a proper more elegant solution. But first let thing what for is this line:

@name = name

This line is instance variable and becomes class variable. Is this particular line is needed? I was thinking that yes, this line is needed, for me, this notation was more readable. But I was in mistake. This piece of code is useless. I can remove this line and execution this simple program will not change. Why?
Variable:

name

that is in:

""Hello, #{name}!""

is interpolated inside the string, to get their value and not just referenced by their name to print on the screen. To wrap variable we can use:

#{name}

So there is no need to create additional value and the final program code looks as follows:

class HelloWorld
  def self.hello(name = "World")
    "Hello, #{name}!"
  end
end

Above code looks nicer that before. But why as a developer I should use string interpolation and what for? This code is very simple and short and probably someone can say why to use this type of construction? Let’s imagine some longer string that contains more than 5 or 6, maybe 122, or more variables. What then? Probably is more convenient to use this kind of string structure to make life easier. And the second thing: for me, the syntax is more readable and of a first look, it’s easier to figure out what’s going on in the code.


Reference:

Raspberry pi 3 and LCD TFT 3,2” 320x240px GPIO

A few days ago I bought a LCD TFT 3.2 ” 320x240px GPIO my Raspbbery pi 3. After unpacking and application LCD TFT 3.2 ” 320x240px for GPIO GPIO pins raspberry in the screen light up a bright light but not seen at all boot the system, or even Raspbian desktop. After formatting the SD card and re-uploading Raspbian still I had the same problem. I started to look for solutions and it turned out that the manufacturer of TFT LCD 3.2 ” 320x240px GPIO controller provides the Raspbian – some older version. It even has the iso image with a preinstalled driver.  Despite the changes in the configuration: raspi-config and rebooting system problem with the display of anything still remained unresolved.

Below aI put some steps that help to run and use LCD TFT 3,2” 320x240px GPIO.

  • Expand Filesystem
  • Select from Boot Options: B4 Desktop Autologin Desktop GUI, automatically logged in as ‘pi’ user
  • From Advanced Options choose:
    • AA GPIO Server
  • Download driver from manufacturer web page
    • Driver
    • Unzip zip file:
      tar xvf <file>
    • Go to: LCD-show catalog
    • Run:
      ./LCD32-show
  • Reboot system

After this steps, we should see desktop displayed on the screen. It takes me some time to figure out that documentation doesn’t say about one step, run GPIO Server. It should be obvious but for a person, that first time tries to use LCD it not very clear. And one more thing is better to just install new Raspbian on SD card and download and install driver, then download iso image with preinstalled driver. From my experience, only one iso image that I download was booting.


References:

Continuous integration services – review

For few day I was looking for some continuous integration services that will allow me to build and deploy my open source projects now and near feature. I spend several hours searching, reading, trying to use and build some code. Some CI services are for free and works very good with for example GitHub. It’s a big plus, especially when you trying to run open source.

Below I add a small table that contains some essential information for devs that are interested in that kind of services.

NameDescriptionFeaturesSupported repositoriesDocumentationPrice
AppveyorAppVeyor automates building, testing and deployment of .NET applications.• GitHub
• BitBucket
documentationFree with limitations
AssertibleAutomated post-deployment testing and web service monitoring• GitHubdocumentationFree with limitations
BitriseiOS Continuous Integration and Delivery.• GitHub
• BitBucket
• Custom
documentationFree
BuildkiteA build automation platform which gives you complete control, without the pain of running your own CI system.Languages: Ruby, Python, Node.js, JavaScript, PHP, Go, Rust, Erlang, Elixir, Java, Clojure, Scala, C/C++, Objective-C, Swift, .NET/C#
Source code is available: Buildkite Agent
• GitHub
• BitBucket
• Custom
documentation• 14-day Trial
• Free for Education
• Open Source
Circle CIContinuous Integration and Deployment• GitHubdocumentationPricing
CodeshipOne more cloud based CI service: running tests and deployment.• GitHub
• BitBucket
documentationFree for opensource projects or 100 builds per month
Code ClimateHosted platform to continuously measure and monitor code quality Languages: Ruby, Javascript, PHP• GitHub
• Custom
documentation14-day Trial
Coveralls Coveralls works with your continuous integration server to give you test coverage history and statistics.Languages: Ruby, Javascript, Python, PHP, C, Objective-C, Scala, GO• GitHub
• BitBucket
documentationFree for opensource projects
CoverityCode analysis, test analysisLanguages: C/C++, Java, C#noneFree for opensource projects
CoviolationsTool for collecting and visualisation code violations works with Travis-CI, drone.io and JenkinsSource code is available: Web application, Applicationdocumentation
DroneContinuous Integration serviceSource code is available.• GitHub
• BitBucket
• Google Code
• Custom
documentationPublic projects for free
FactorService to automate developer workflows by defining them programmatically using the Factor.io syntax and running them using the Factor.io Server.documentation
GitLab CI• GitLabdocumentationPricing
GitHost• GitLabdocumentationPricing
Hound CIReview your JavaScript, CoffeeScript, and Ruby code for style guide violations with a trusty hound.• GitLabnonePublic repositories for free
Magnum CIHosted Continuous Integration and Delivery Platform• GitHub
• BitBucket
• GitLab
• Custom
documentationFree beta
PHP CIFree and open source continuous integration tool specifically designed for PHPLanguage: PHP, Source code is available• BitBucket
• GitLab
• Custom
nonePricing
SaucelabsAutomated testing in the cloud for CIdocumentation14-day Trial
ScrutinizerBuild quality software, betterSources are available• GitHub
• BitBucket
documentation14-day Trial
SemaphoreHosted continuous integration and delivery solution for open source and private projects.Ruby, Node.js, JavaScript, Go, Clojure, Elixir, Erlang, Java, PHP, Scala, C/C++• GitHub
• BitBucket
documentationFree for Open Source, 100 free builds for private projects
ShippableContinuous DeliveryLanguages: Ruby, Python, Java, Node.js, Scala, PHP, Go; Databases: MongoDB, MySQL, Redis, Postgres, CouchDB, RethinkDB, Neo4j, and SQLite• GitHub
• BitBucket
• GitLab
documentationFree with limitations
Snap• GitHubdocumentationFree
Solano CIHosted service that runs your test suite on a distributed infrastructure - fast• GitHub
• BitBucket
• GitLab
• Custom
documentation14-day Trial
StyleCIThe PHP Coding Style Service, used by Cachet.• GitHubdocumentationFree for opensource projects
TestlingHosted service that runs your test suite in many different browsers every time you push to GitHub. It can run any test suite so long as it writes TAP to the browser console.documentationFree for opensource projects and 3 min sessions
Travis CIHosted continuous integration service for open source and private projects.Languages: C, C++, Clojure, Erlang, Go, Groovy, Haskell, Java, Javascript (with Node.js), Objective-C, Perl, PHP, Python, Ruby, Rust, Scala. Source code is available.• GitHubdocumentationFree for opensource projects and 3 min sessions
Visual Studio Online Cloud-based collaboration services for version control, agile planning, continuous delivery, and analytics application for Visual Studio, Eclipse, Xcode.• Visual Studio
• Team Services
• GitHub
• Custom
documentationFree
WerkerContinuous delivery platform• Docker Hubdocumentation

Changes changes changes

Currently on my blog I carry out changes which allow me to more frequent blogging, sharing greater quantities of content which I want to convey. There will be no changes in blog template, more in functionality specially for me. Those changes will not affect any user which ant to visit my site, read post or leave comment.

Be patient. New post coming soon 😉

Apktool

Apktool is a tool 😉 for reverse engendering 3rd party, closed, binary Android *.apk files. It can decode resources to nearly original form and then also rebuild them. Rebuild them also when you provide some modifications.

I create two simple apps to try apktool and basically check how it’s work. In directory we have two apps. First: app-debug.apk and app-debug-unaligned.apk. In this case, not matter really what’s are these apps for.

andrzejdubaj  staff  4530973 22 maj 23:15 app-debug-unaligned.apk
andrzejdubaj  staff  4531823 22 maj 23:15 app-debug.apk

To use apktool simply open terminal and go to this directory where are yours app’s(app). Type:

apktool d <name_of_your_app.apk>

and press ENTER. If everything it’s ok output should be similar to this one below:

$ apktool d app-debug.apk 
I: Using Apktool 2.1.1 on app-debug.apk
I: Loading resource table...
I: Decoding AndroidManifest.xml with resources...
I: Loading resource table from file: /Users/andrzejdubaj/Library/apktool/framework/1.apk
I: Regular manifest package...
I: Decoding file-resources...
I: Decoding values */* XMLs...
I: Baksmaling classes.dex...
I: Copying assets and libs...
I: Copying unknown files...
I: Copying original files...

In our directory, a new folder was created with name of the app. It contains all folders and files that are contained in the *.apk file, for example:

  • res folder
  • smali folder
  • original
  • AndroidManifest.xml
  • apktool.yml
drwxr-xr-x    7 andrzejdubaj  staff   238 22 maj 23:18 .
drwxr-xr-x    5 andrzejdubaj  staff   170 22 maj 23:18 ..
-rw-r--r--    1 andrzejdubaj  staff  3588 22 maj 23:18 AndroidManifest.xml
-rw-r--r--    1 andrzejdubaj  staff   394 22 maj 23:18 apktool.yml
drwxr-xr-x    4 andrzejdubaj  staff   136 22 maj 23:18 original
drwxr-xr-x  137 andrzejdubaj  staff  4658 22 maj 23:18 res
drwxr-xr-x    4 andrzejdubaj  staff   136 22 maj 23:18 smali

From now ok we can:

  • analyze code
  • create modes

After we perform some edits, we can build our app. For this purpose I create separate folder and copy there decompile folder with all my changes:

total 0
drwxr-xr-x  3 andrzejdubaj  staff  102 23 maj 21:55 .
drwxr-xr-x  6 andrzejdubaj  staff  204 23 maj 21:54 ..
drwxr-xr-x  7 andrzejdubaj  staff  238 22 maj 23:18 app-debug

To build application, we can use:

apktool b <name_of_your_app.apk>

We can get something like that:

$ apktool b app-debug/
I: Using Apktool 2.1.1
I: Checking whether sources has changed...
I: Smaling smali folder into classes.dex...
I: Checking whether resources has changed...
I: Building resources...
I: Building apk file...
I: Copying unknown files/dir...

But I can’t get any *.apk file. But also at that point I was wrong. Why ? Because default directory for output file is:

... /app/app-01/app-debug/dist

Top build and receive *.apk file in the same catalog use:

apktool b <folder_name> -o <file_name.apk>

If everything it’s ok output should be similar to this one below:

$ apktool b app-debug -o apk-debug.apk
I: Using Apktool 2.1.1
I: Checking whether sources has changed...
I: Checking whether resources has changed...
I: Building apk file...
I: Copying unknown files/dir...

Now we can so that our new *.apk file is in the same directory like rest of files:

-rw-r--r--  1 andrzejdubaj  staff  4512866 23 maj 22:13 apk-debug.apk
drwxr-xr-x  9 andrzejdubaj  staff      306 23 maj 21:57 app-debug
drwxr-xr-x  9 andrzejdubaj  staff      306 23 maj 22:07 app-debug-unaligned

But sometimes command described before really don’t work, and you can receive similar android Exception:

I: Checking whether sources has changed...
I: Checking whether resources has changed...
I: Building resources...
Exception in thread "main" brut.androlib.AndrolibException: brut.common.BrutException: could not exec command: [aapt, p, -F, /tmp/APKTOOL3630495287059303807.tmp, -I, /home/awesomename/apktool/framework/1.apk, -S, /home/awesomename/out/./res, -M, /home/awesomename/out/./AndroidManifest.xml]
    at brut.androlib.res.AndrolibResources.aaptPackage(Unknown Source)
    at brut.androlib.Androlib.buildResourcesFull(Unknown Source)
    at brut.androlib.Androlib.buildResources(Unknown Source)
    at brut.androlib.Androlib.build(Unknown Source)
    at brut.androlib.Androlib.build(Unknown Source)
    at brut.apktool.Main.cmdBuild(Unknown Source)
    at brut.apktool.Main.main(Unknown Source)
Caused by: brut.common.BrutException: could not exec command: [aapt, p, -F, /tmp/APKTOOL3630495287059303807.tmp, -I, /home/windows/apktool/framework/1.apk, -S, /home/windows/out/./res, -M, /home/windows/out/./AndroidManifest.xml]
    at brut.util.OS.exec(Unknown Source)
    ... 7 more
Caused by: java.io.IOException: Cannot run program "aapt": error=2, No such file or directory
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041)
    at java.lang.Runtime.exec(Runtime.java:617)
    at java.lang.Runtime.exec(Runtime.java:485)
    ... 8 more
Caused by: java.io.IOException: error=2, No such file or directory
    at java.lang.UNIXProcess.forkAndExec(Native Method)
    at java.lang.UNIXProcess.(UNIXProcess.java:135)
    at java.lang.ProcessImpl.start(ProcessImpl.java:130)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1022)
    ... 10 more

I found soloution for that:

apktool d -f -r apkfilename.apk

-f replace previous decompiled apk’s code
-r ignore the decompiling of resources
This would prevent the resources from being decompiled, will copy the same resources when you recompile the apk.


Reference:

Kali Linux – sources.list and update

In Unix systems, repositories are called source software, of which the package manager downloads and installs programs. The list of Debian (and its derivatives) is located in: /etc/apt/sources.list and files in the directory /etc/apt/sources.list.d/. The format of the file is:

{type} {adres} {system version} {section1} {section2} {section3}

Description:

  • {type}

    – is kind of repository distinguish repositories of binary packages (deb), ready to use, and repositories of source packages (deb-src)

  • {adres}

    – repository address, it is usually http or ftp server can also be a local repository (file: //) or CD / DVD

  • {system version}

    – version used by our system, for example. Stable, testing, unstable. In most cases (in the case of official repositories always) may also be a code, for example: etch, lenny, sid

  • {section}

    – packages group, for example. main (main section includes packages comply with the Debian Free Software Guidelines), non-free (contains the packages non-free), contrib (includes packages free, in line with the DFSG, but somehow depend on packages in non-free). Sections may be given in any order.

nano /etc/apt/sources.list

In sources.list file we can add one line:

deb http://http.kali.org/kali kali-rolling main contrib non-free

This one allows to update and install new repositories for Kali Rolling users.
Kali_01

Why kali-rolling repository ? Answer from kali.org:

In contrast to kali-dev, kali-rolling is expected to be of better quality because it’s managed by a tool that ensures installability of all the packages it contains. The tool picks updated packages from kali-dev and copies them to kali-rolling only when they have been verified to be installable. The repository is also fed by a stream of tool updates, of which we get notified via our upstream git tagging watch list.

After set up our repository in sources.list file we can easily update system by:

apt-get update && apt-get upgrade

Kali_03

Kali will now check servers for updates. If there any repositories (and it will because we just have a fresh, new system in a virtual environment), it will ask you to install them. Sometimes I can remember the whole command to execute I use permanent aliases. I will create some article: how to create a permanent alias.(status: in progress …)

During installation Kali will ask for restart services during update repositories:
restarted services

When list of all repositories will be updated, we can use:

shutdown -r now

to restart our machine. Now all repositories should be updated. If you will some problems during update, please leave comment below.


Reference:

How to set up Kali Linux on Virtual Box

The purpose of this guide is to show how step by step install Kali Linux on VirtualBox. Kali Linux is very useful tool 😉

1

Open VirtaulBox and create the virtual machine by clicking on: ‘New’ or ‘CTRL + N’. When new window appears choose:

  • name for your virtual machine
  • type and version on an operating system: Linux, Debian(32 bit)/ Debian(64 bit)

2

Set memory at least 512MB. I set more than 700MB but after first run always change to a higher number. It always depends on how memory you have on a host machine.

3

Select option: ‘Create a virtual disk now’ and then click on: ‘Create’.

4

On this window, you can  select a few options, but I recommend to choose VMDK. Other options:

  • VDI  (VirtualBox Disk Image) – virtual disk image file in VirtaulBox systems
  • VHD (Virtual Hard Disk) – virtual hard disk drive, VHDs are implemented as files that reside on the native host file system, used by Microsoft
  • HDD (Parallels Hard Disk) – format used by commercial software “Parallels Workstation” Company “Parallels, Inc.”.
  • QED (QEMU enhanced disk) – format-specific virtual machine (KVM) based on the Linux kernel
  • QCOW (QEMU Copy-On-Write) – format used to obtain virtual storage space. This mechanism allows for a significant reduction in disk space requirements in a situation where the same image of the hard disk is used by many virtual machines and helps to increase productivity, as it reads from the disk is stored in the cache located in the RAM and the memory serves request readings from other virtual machines.

5

Select: ‘Dynamically allocated’. This option allows changing memory according to the requirements.

6

In: ‘Processors’ option select: ‘Enable PAE/NX’. Reserving for 32-bit memory larger than 4 GB require the use of Enable PAE / NX.

8

After starting virtual machine select locations of *.iso file with Kali Linux.

9

Select: ‘Install’ option and press ENTER key.

11

Select location.

12

Choose keyboard.

14

In this step yu can choose: ‘Continue’.

15

This is optional. You can skip this option.

16

Set up password.

17

Re-enter password.

18

Choose: ‘Guided – use entire disk’.

19

Confirm the disk partition.

20

Select partition schema. Choose first option.

21

Select: ‘Yes’ option to write changes to disk.

22

System installation starts. In the middle of the process, two windows will appear.  First with network mirror and second with installing the GRUB boot loader installation on a hard drive.

1

After reboot enter username as: ‘root’ and password configured previously.

2

 If you have some problems with installation, please leave comment below.