Using Array.wrap in Rails when the results could either be a string or an array

Problem

You want to use some array method in a value returned, which can either be a single value or an array of values.

Solution

Rails ActiveSupport Array, provides a method called wrap, that can be used to:

Wraps its argument in an array unless it is already an array (or array-like)

Full explanation of the method here.

Thanks Miles

Converting number from any base system to decimal in linux shell

Problem

You would like to quickly convert a number from a different base system (i.e. binary, octal, hexadecimal), to decimal.

Solution

Use the echo $(()) shell command to convert it passing it the current base system and the number, as in $((2#101010)) or using the O for octal or 0x for hexadecimal notations.
Examples:

echo $(( 16#FF ))
255
echo $(( 0xFF ))
255
echo $(( 8#21 ))
17
echo $(( 021 ))
17

Taken from Linux Journal’s Work the Shell column (March 2016)

Combine multiple zip files (from windows) and unzip them in linux

Problem

You have received multiple zip files that were originally one big zip file, possibly from windows, and you want to put them back together and unzip them in linux.

Solution

So if your file names are for example called: file1.zip file2.zip etc, you can do the following:

cat file* > one_file.zip
unzip one_file.zip

Book Review: Elasticsearch in Action by Radu Gheorghe, Matthew Lee Hinman, Roy Russo (Manning)

Elasticsearch has been around for a while now, and it can be found in quite a lot of different websites nowadays. This book reviewed here aims to cover both the fundamentals of using elasticsearch, as well as the advanced usage, scaling and performance optimisation of elasticsearch in production.

In order to do this, the book is mainly divided in two distinct parts named “Core functionality” and “Advanced Functionality”, but there are also another six chapters at the end as appendices.

So let’s take things in order, as is also the recommended way of getting through the book, and start with the first part of the book. There are eight different chapters, covering all of the core functionality of elasticsearch. In these chapters the reader can find out about what kind of search problems can be solved, some typical use cases, understanding the meaning of documents, types, indices, shards and clusters. Following this, there is a thorough explanation about indexing, updating and deleting data. Another chapter is dedicated to searching the data with match and filter queries as well as using the best query for the specific job required. Analysing the data using analyzers, tokenizers and token filters with special sections about ngrams, edge ngrams, shingles and stemming, follows next. How scoring works and a description of the different scoring methods as well as boosting is described in the next chapter. The first part finishes off with two chapters about aggregations (metrics, multi-bucket and nesting aggregations) and the different ways to describe relationships (nested, parent-child etc) between documents.

The second part of the book, as mentioned previously, is dedicated to the advanced usage, scaling and performance in production.
There are three main chapters in this part.
The first one is about scaling out, and specifically about adding nodes, discovering other nodes, removing nodes and upgrading them in your Elastic cluster. There also sections about using the _cat API that provides helpful diagnostic and debugging tools in a more human readable way, different scaling strategies namely over-sharding, splitting data between indices and shards, and maximising throughput, using aliases, and finally routing.
The second chapter is mainly about improving performance, through the use of request grouping (bulk indexing, updating and deleting), multisearch and multiget APIs, Lucene segment optimisation, and the best use of caches, including a section about performance trade offs in different use cases.
The last chapter of this part concerns the administration of the elasticsearch cluster. This is done by means of improving the defaults that come ‘out of the box’, using ‘allocation awareneness’ that reduces central points of failure, and the subject of monitoring that includes checking the cluster health, CPU and memory usage, OS caches and store throttling. Finally there is also a section about backing up of the data and restoring it.

The third part with the appendices, is also a very thorough addition for many different subjects, and it should not be thought of as a very artificial mention of them as there is enough information provided. This includes a section about working with geospatial data, another one about a few of the most common plugins that can be used (both open source and commercial), the functionality of results highlighting, some monitoring plugins, the explanation and use case of the percolator for doing ‘upside down’ searches, and finally using suggesters for auto-completion and suggestions (did-you-mean) functionality.

It has to be noted, that throughout the book there are plenty of examples that you can follow and experiment for the simple ‘events’ example application that is provided. Everything that is been described has its own example code that can be used.
There are also numerous graphs and diagrams in all of the chapters, that make some concepts much easier to grasp and understand.
Many different use cases are also described as well as suggestions where each solution is best suited.

As a conclusion, this is a very useful book for both an experienced elasticsearch user/administrator as well as somebody that starts now. There is a lot of detail and examples that cover all the necessary functionality that someone needs in order to use and administer elasticsearch successfully. Many common use cases are provided and suggestions for each one are analysed in detail. Scaling and performance optimisation are also very well covered.
This is another indispensable book in ‘.. in Action’ Manning series, that has a very good balance between the theory and practice of its subject. Highly recommended.

Disclosure of Material Connection: I received this book free from the publisher. I was not required to write a positive review. The opinions I have expressed are my own. Regardless, I only recommend products or services I use personally and believe will add value to readers.

Remove terminated/unused docker containers from your system

Problem

After playing around with docker containers for some time the terminated/unused ones are still hanging around your system, and prevent you from using a new container with the same name as any of the previous ones with the error:

Error response from daemon: Conflict. The name "container_name" is already in use by container 61f023f06a98. You have to remove (or rename) that container to be able to reuse that name.

Solution

You can remove all the containers by using the following:

docker rm $(docker ps -aq)

git apply name_of_file.patch does not work

Problem

You are given a patch file to apply to your git repository but using git apply does not work. No errors reported but no changes applied neither.

Solution

Try to use the patch command command possibly with the -p1 parameter like:

patch -p1 < path_to_the_patch_file.patch

NOTE: Make sure that you are in the right folder relative to the file path in the patch file

SSL received a record that exceeded the maximum permissible length.

Problem

You want to install rbenv, but when you are trying to clone the code with:

git clone https://github.com/rbenv/rbenv.git ~/.rbenv

you get the following error:

fatal: unable to access 'https://github.com/rbenv/rbenv.git/': SSL received a record that exceeded the maximum permissible length.

Solution

If you are behind a proxy server make sure that you add your proxy server details in your ~/.bash_profile and source the file afterwards.

So first add the details of your proxy like:

export http_proxy=http://xxx.xxx.xxx.xxx:8080
export https_proxy=https://xxx.xxx.xxx.xxx:8080

And finally source the file before running the installation again:

source ~/.bash_profile

Creating a simple rails docker image for testing in cloud deployment

Trying to deploy a rails application in a cloud provider ie dreamhost, that uses OpenStack these are the steps needed:

  1. Install docker in your development machine and your cloud provider by following the installation instructions from here
  2. Create an account in Docker Hub, that will be needed later on to push your docker image with the application
  3. Pull the official rails docker image to your development environment:
    sudo docker pull rails:latest
    
  4. Create a new simple rails application:
    rails new docker_test
  5. Change to the application directory and add a Dockerfile in the root directory containing the following:
    FROM rails:onbuild
  6. Build your new image by using:
    sudo docker build -t rails_docker_test .
  7. Check that your image was build by using:
    sudo docker images
  8. Start the container with:
    sudo docker run --name rails_test -p 0.0.0.0:3000:3000 -d rails_docker_test
  9. Make sure that you can see the initial rails page by using your browser to go to http://127.0.0.1:3000
  10. Push your image to your Docker Hub account by first logging in to it from the command line:
    sudo docker login --username=yourhubusername --email=youremail@company.com

    , and then when you get ‘Login Succeeded’, push your image to your account:

    sudo docker push yourhubusername/rails_docker_test
  11. TBC