ISSUE: Rails Assets Pre-compilation with Yarn

Tags

, , , , ,

Today I was deploying one of my application build with Rails 6 and React. During the deployment, everything went well but when it came to assets pre-compilation the deployment start failing. After checking the logs I found that the YARN package was not installed properly which is needed for React-Rails application.

Failure/Error:

 deploy:assets:precompile
      01 /home/deploy/current exec bundle exec rake assets:precompile
      01 Usage: yarn [options]
      01
      01 yarn: error: no such option: --no-progress

FIX

Configure the repository:

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - && echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

Install Yarn:

sudo apt update && sudo apt install yarn

After doing proper installation of Yarn package, I ran the Capistrano deployment command:

BASTION=true cap production deploy –trace 

Whoo!, the application is running in Production mode.

Happy Learning.

How to “Git Tag”?

Tags

, , , , , , , ,

Tags are ref’s that point to specific points in Git history. Tagging is generally used to capture a point in history that is used for a marked version release (i.e. v1. 0.1). A tag is like a branch that doesn’t change. Unlike branches, tags, after being created, have no further history of commits. Tagging is an additional mechanism used to create a snapshot of a Git repo. Tagging is traditionally used to create semantic version number identifier tags that correspond to software release cycles. The git tag command is the primary driver of tag: creation, modification and deletion. There are two types of tags; annotated and lightweight. Annotated tags are generally the better practices as they store additional valuable metadata about the tag.

Tag Commands

To list stored tags in a repo execute the following

$ git tag

Search tags for a particular pattern:

$ git tag -l <tag-pattern>

Show a tag data:

$ git show <tag-name>

A common pattern is to use version numbers like git tag v1.4. Git supports two different types of tags, annotated and lightweight tags.

Annotated Tag:

Annotated tags are stored as full objects in the Git database. To reiterate, They store extra metadata such as the tagger name, email, and date. Similar to commits and commit messages Annotated tags to have a tagging message. Additionally, for security, annotated tags can be signed and verified with GNU Privacy Guard (GPG). Suggested best practices for git tagging is to prefer annotated tags over lightweight so you can have all the associated meta-data.

Create an Annotated Tag:

$ git tag -a <tag-name> -m <tag-message>

Lightweight Tag

Another way to tag commits is with a lightweight tag. This is basically the commit checksum stored in a file — no other information is kept. To create a lightweight tag, don’t supply any of the -a, -s, or -m options, just provide a tag name. Lightweight tags create a new tag checksum and store it in the .git/ directory of the project’s repo.

Create a Lightweight Tag

$ git tag <tag-name>

Create a tag for a specific commit:

$ git tag -a <tag-name> <commit-checksome>

Push a specific tag to remote:

$ git push origin <tag-name>

Push all the tags to remote:

$ git push origin --tags

Checkout a specific to local:

$ git checkout -b <branch-name> <tag-name>

Deleting tags is a straightforward operation. Passing the -d option and a tag identifier to a git tag will delete the identified tag.

$ git tag -d <tag-name>  [NOTE: Think multiple times before execution]

Happy learning!!

Fix sudo: apt-get: command not found

Tags

, , , , ,

Sometimes when we are playing around or we are more concern with the security about the system and wants to remove/uninstall packages from our server, sometimes some package accidentally gets removed. Among those, some are very critical for the system to manage.

Once I was analysing security on my ubuntu server, I accidentally remove the package manager and rebooted my server. Now when I try to install any package eg  htop, I got issue regarding apt-get: command not found

OMG, what’s happening, now neither I can update/install/remove any of the packages, the options suggested on the internet was to re-install or a very complex way to repair, huuu, I can’t do that. After hard research and try, I came up with a solution.

Here are the steps how I resolved it on Ubuntu Xenial

Search the appropriate version of apt from here apt_1.4_amd64.deb for ubuntu xenial

1. Download the apt.deb

wget http://security.ubuntu.com/ubuntu/pool/main/a/apt/apt_1.4_amd64.deb

2. Install the apt.deb package

sudo dpkg -i apt_1.4_amd64.deb

3. Now we can easily run

sudo apt-get install 

Thank GOD, now I can manage the packages which I want as per need.

I hope this post will help you and save your time, money and infrastructure.

Happy Learning

Setup service supervision with runit

Tags

, , , , , , , , , ,

When an application has live traffic then the application is marked critical. Downtime incident to that service is not tolerable by users. So as an engineer we should design the application infrastructure more reliable and resilient. Among lots, one of the ways is to add service supervision.

Out of many supervisors, we have chosen runit as our best suitable service supervisor. Runit provides us following benefits:
1. Runit guarantees each service a clean process state
2. The runsv program provides a reliable logging facility for the service daemon
3. Fast system boot up and shutdown
4. Small code size

Installation

Install runit

sudo apt-get install runit

Let’s check whether runit is installed properly or not

ps -ef | grep runsvdir

Now, runit has been installed. Since the instance/machine has a different user to perform different activities. So let’s say runit will be supervising each user assigned service/server. For example, assume a deploy user which takes care of background worker named ‘Sidekiq‘.

Note: You don’t need to create nested directories if you want to be simple.

So, as a root user create a required directory based on username:

sudo mkdir -p /etc/sv/user
sudo mkdir -p /etc/sv/user/deploy

Runit supervisor looks for a run file to execute, so let’s create a run file.

sudo vi /etc/sv/user/deploy/run

and add this content:

#!/bin/sh -e
exec 2>&1
exec chpst -udeploy runsvdir /home/deploy/service

Next, change the permission of run script so that its executable.

sudo chmod +x run

Since runit search run file under folder/etc/service. So let’s symlink

sudo ln -sf /etc/sv/user/deploy/ /etc/service/deploy

So, as a deploy user let’s, create required directory as mentioned in /etc/sv/user/deploy/run script

mkdir /home/deploy/service

As deploy user task is to run background worker, let’s create directory for that

mkdir /home/deploy/service/sidekiq

and add this content to run the script inside the /home/deploy/service/sidekiq directory

#!/bin/bash
exec 2>&1 > /tmp/runit_sidekiq.log
NUM=0
APP= path/to/app_folder
source ~/.rvm/scripts/rvm
rvm use 2.3.1

SIDEKIQ_CONFIG='-C config/sidekiq.yml -c 6'
RAILS_ENV=production

cd $APP/current
RAILS_ENV=$RAILS_ENV bundle exec sidekiq \
         $SIDEKIQ_CONFIG \
         --index $NUM \
         --pidfile $APP/shared/tmp/pids/sidekiq-$NUM.pid \
         --environment $RAILS_ENV \
         --logfile $APP/shared/log/sidekiq-$NUM.log

Now, change the permission of run script so that its executable.

sudo chmod +x run

Now when you check process list, you should be able to view sidekiq running under runsv sidekiq.

ps aux | grep sidekiq
deploy    1255  0.0  0.0    168    32 ?        Ss   Jan22   0:00 runsv sidekiq-0
deploy   11188 36.5 13.6 1076644 525272 ?      Sl   05:10   1:15 sidekiq 4.2.10 applicationme [4 of 6 busy]
deploy   11775  0.0  0.0  10460   916 pts/0    S+   05:13   0:00 grep sidekiq

Woo, now sidekiq service is resilient.
Happy learning!!