All articles

Sending files by SCP (secure copy)

Category:

Send the whole current directory to a remote server

scp -r * username@hostname_or_ip:/home/username/directory/

Download a directory to the current directory

scp -r username@hostname_or_ip:/home/username/directory/ .

Installing Docker

Category:

Installing Docker on Ubuntu 16.4

source: https://docs.docker.com/install/linux/docker-ce/ubuntu/
sudo apt update
sudo apt install apt-transport-https
sudo apt install ca-certificates
sudo apt install curl
sudo apt install software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt update
sudo apt install docker-ce

Configure Docker to run without sudo.

source: https://docs.docker.com/install/linux/linux-postinstall/
sudo groupadd docker
sudo usermod -aG docker $USER

Log out and log in.

Test

docker run hello-world

Install Docker Compose

source: https://docs.docker.com/compose/install/

Check the current version on this site https://github.com/docker/compose/releases

Install it using curl

sudo curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Decorator Pattern

Category:

Adding functionality to objects by creating new small classes instead of adding and changing methods in old classes.

class Robot
  def initialize(name)
    @name = name 
  end
  
  def name
    puts @name
  end
end

class Aged < SimpleDelegator
  def initialize(source, age=0)
    super(source)
    @age = age
  end

  def age
    puts @age
  end
end

class Ranked < SimpleDelegator
  def initialize(source, rank=10)
    super(source)
    @rank = rank
  end

  def rank
    puts @rank
  end
end

class HumanFriendly < SimpleDelegator
  def age
    puts "My age is:"
    super
  end
  
  def rank
    puts "My rank is:"
    super
  end
  
  def name
    puts "My name is:"
    puts "Friend of Humanity"
  end
end

robot = HumanFriendly.new(Ranked.new(Aged.new(Robot.new("Destroyer of Humanity"))))
robot.name
robot.age
robot.rank

# output
# My name is:
# Friend of Humanity
# My age is:
# 0
# My rank is:
# 10

Docker commannds

Category:

docker stop [OPTIONS] CONTAINER [CONTAINER…]

The main process inside the container will receive SIGTERM, and after a grace period, SIGKILL.

docker kill [OPTIONS] CONTAINER [CONTAINER…]

The main process inside the container will be sent SIGKILL, or any signal specified with option --signal.

docker pause CONTAINER [CONTAINER…]

Suspends all processes in the specified containers. On Linux, this uses the cgroups freezer. Traditionally, when suspending a process the SIGSTOP signal is used, which is observable by the process being suspended.

docker start [OPTIONS] CONTAINER [CONTAINER…]

Start one or more stopped containers.

Installation and configuration

Category:

This installation description is for Ubuntu.

It is installed by default.


User name and email configuration (for all projects of that user).

git config --global user.name "Krzysztof Kowalski"
git config --global user.email k.kowalski@example.com

User name and email configuration just for that project.

git config --local user.name "Krzysztof Kowalski"
git config --local user.email k.kowalski@example.com

Undo changes

Category:

Back to the last commit, when unwanted changes are not commited and not added.

git checkout -- .

and then

git clean -df

Or

git reset --hard

and then

git clean -df

Or probably the best way:

git add .

and then

git reset --hard

Back to the last commit, when unwanted changes are not commited but added.

git reset --hard

Back to the any commit with working tree added to the index or not.

To be sure that all changes are added.

git add .

And then to go back to the pre-pre-last (second to last) commit.

git reset --hard HEAD~2

Setup a new pure Ruby project

Category:

I assume that you use rbenv.


Update your rbenv and ruby-build

cd ~/.rbenv && git pull

cd "$(rbenv root)"/plugins/ruby-build && git pull


Check what versions are installed

rbenv versions


Check what versions of Ruby are available for installing

rbenv install -l


Install Ruby in some version

rbenv install 2.4.2


Create the project directory and go there

mkdir ~/projects/hosting_manager

cd ~/projects/hosting_manager/


Switch the directory to your Ruby version

rbenv local 2.4.2


Install Bundler and init it

gem install bundler

bundle init

bundle install --path vendor/bundle

Grep

Category:
grep -rw “lol” *.rb

Find “lol” in ruby files in the current directory.

  • -r - recursive
  • -w - whole words

Skipping "return" keyword in annonymous functions

Category:
   [1,2,3,4].map((n) => {return n*2});
=> [ 2, 4, 6, 8 ]
   [1,2,3,4].map((n) => {n*2});
=> [ undefined, undefined, undefined, undefined ]
   [1,2,3,4].map((n) => n*2);
=> [ 2, 4, 6, 8 ]
   [1,2,3,4].map(n => n*2);
=> [ 2, 4, 6, 8 ]

Percent String Literals

Category:

The most common delimeters are { and }, but it is possible to use any kind of brackets or pair of @, #, %, ^, &, *, -, =, +, /, \, ;, |, ?, ., !, $

type definition interpolation
%W Array of double-quoted Strings yes
%w Array of single-quoted Strings  
%Q Double-quoted String yes
%q Single-quoted String  
%i Array of Symbols  
%s Symbol  
% Same as %Q yes
%r Regular Expression yes
%x Capture output of running the command in a subshell (backtick ). yes