Saturday, December 28, 2019

Cloud Native 12 factor Application


  1. Codebase/Revision Control
  2. Dependencies
  3. Config
  4. Backing Services
  5. CI/CD
  6. Processes
  7. Port Binding
  8. Concurrency
  9. Disposability
  10. Development/Production Parity
  11. Logs
  12. Admin Processes

Sunday, May 21, 2017

Docker Commands

Docker commands

  • Version, Info …

>docker version

>docker info

  • Search

Searches images in docker registry.

docker search <Image Name>

>docker search ubuntu

  • Images

List all the downloaded images

>docker images

>docker ps –a

    -a – show all

    -l – show last

  • Pull

Pulls the image if it is not available locally. It runs automatically by docker run.

>docker pull <Image name>

  • Run

Docker run starts the container by giving the image name a process to run in that container. The container stops when the main process stops. It automatically pulls the image.

>docker run –ti <image>:[<version/latest>] <program to run>

    -ti – terminal interactive

    -d – detach – will run in the background

    -c – command

    -p – port. eg. –p 1234:3456 Expose port 1234 on the inside of the container to the outside of container as port 3456.

          docker run –p <outside-port>:<inside-port>/protocol(tcp/udp)

    -v – volume

         docker run –v <path in host>:<inside the container path>

        >docker run –ti –v /home/docker/example:/shared-folder ubuntu bash

    --name – name of the container

    --memory – maximum allowed memory

    --cpu-shares – relative to other containers

    --cpu-quota – limit it

    --link <Name of container> – for communication between two containers. It automatically assigns a hostname. Link can break when containers restart.

    --net==<Network name>

    --volumes-from <Name of the container that we want to use the volume> – To share data/file between containers.

  • Commit and Tag

>docker commit <id of the container>

Tagging gives images names

>docker tag <id received from commit> <Image/Tag name>

     or

>docker commit <Name of the container> <Image/Tag name>

  • Build

Creates an image for the docker file

    >docker build –t <Name of tag> .

The (.) shows the docker file in here. Otherwise need to pass the path of docker file.

  • Push

Upload a tagged image to docker hub

    >docker push <Tag name>

  • Attach

>docker attach <Container name>

  • Exec

>docker exec

Starts another process in an existing container.

  • Logs

Docker logs keep the output of the container.

>docker logs <Container name or ID>

  • Stopping and Removing

>docker kill <Container name/ID>

>docker rm <Container name/ID>

  • Network

>docker network create <Network name>

  • Clean Up

>docker rmi <Image name/Image ID>:<Tag>

  • Login

Login in to docker hub

Monday, February 27, 2017

Modules & Classes in ES6

Modules:
Module help us with encapsulation by exposing from a given file the pieces we want to be access publically. An export statement will be used for that.

export {
     show: someFunc()
}


Then an import statement is used to import that piece:

import { show } from './my-file.js'

Sunday, February 19, 2017

ES6 Features

New features in ES6 are:

  • let, const and Block Scoping,
  • Arrow Functions =>,
  • Default Function Parameters,
  • Rest and Spread,
  • Object Literal Extensions,
  • for…of Loops,
  • Octal and Binary Literals,
  • Template Literals
  • Destructuring

let, const and Block Scoping:

let

let statement allows to declare variables that are limited in scope to the block, statement or expression on which it is used.

Arrow Functions =>

Arrow functions are anonymous functions with shorter syntax than a function.

Arrow function without any arguments.

var geEmployee = () => console.log(‘Employee Name’);

Default Function Parameters:

var getEmployee = function(id = 101){
     console.log(id)
};
getEmployee();

Thanks,
Bimal

Saturday, October 29, 2016

Dockerfile Commands

A list of commands are defined to build Docker image.

  • FROM

The first statement/command in the docker file which tells which image to download and start from.

  • MAINTAINER

Describes the author of the docker file.

MAINTAINER <First name> <Last name> <email address>

  • RUN

Run a command, wait for the result and then saves the result.

RUN echo hello docker

  • ADD

Adds local files, contents of a tar archives and works with urls.

  • ENV

Helps to set environment variables, both during the build and when running the result.

ENV artifactId=com.org.artifactId

  • ENTRYPOINT

Specifies the start of the command to run

  • CMD

Specifies the whole command to run

  • EXPOSE

Maps a port into the container

EXPOSE 8080

  • VOLUME

Defines shared or ephemeral volumes.

VOLUME [“/host/path/” “/container/path/”]

VOLUME [“/shared-data”]

  • WORKDIR

Sets the directory the container starts in. It is similar to use ‘cd’ after start.

WORKDIR /install/

  • USER

Sets which user the container will run as.

USER bimal

USER 1000

Thursday, September 22, 2016

TypeScript

TypeScript is a superset of JavaScript. When TypeScript is compiled, it transpiled to JavaScript.

Features:

  • Static Typing
  • Interfaces
  • Class Properties
  • Access modifiers (Public, Private)

Static Typing:

let name: string
let age: number
let dob: data

Interfaces:

interface IEmp {
     name: string
     age?: number //Optional property
     dob: date
}

let myEmp: IEmp

Class Properties:

class Employee {

name: string
dob: date
constructor (name){
       this.name = name
}
}

Access Modifier:

Class members are public by default in both ES6 and TypeScript.

class Employee {

private name: string
private getSalary() {
     console.log(‘Salary of ’ + this.name + ‘ is $5000.00‘)
}
}

let myEmp = new Employee ()
console.log(myEmp.name) //Compile time error

Thanks,
Bimal

Saturday, November 07, 2015

Custom Matchers in Jasmine 2.3

Custom matchers, provided by the behavior-driven development framework Jasmine, is a great feature for testing JavaScript code. Custom Matchers helps to group multiple catch checking which makes the test cases clean and self explanatory.

Let’s look at an example that I tried recently

Here is a Calculator.js file:

   1: var Calculator = function () { };
   2:  
   3: Calculator.prototype.add = function (a, b) {
   4:     return a + b;
   5: };
   6:  
   7: Calculator.prototype.divide = function (a, b) {
   8:     return a / b;
   9: };

One of the test scenarios that I would like to try is to check whether the result of the method is in a particular range. The test method will look like this with existing matchers.



   1: describe('Calculator', function () {
   2:     var calc;
   3:  
   4:     beforeEach(function () {
   5:         calc = new Calculator();       
   6:     });
   7:  
   8:     it('should be able to add 1 and 1', function () {
   9:         expect(calc.add(1, 1)).toBe(2);
  10:     });
  11:  
  12:     it('should be able to divide 6 by 2', function () {
  13:         expect(calc.divide(6, 2)).toBe(3);
  14:     })
  15:  
  16:     it('should be able to divide a rational number', function () {
  17:         expect(calc.divide(1, 3)).toBeLessThan(0.34);
  18:         expect(calc.divide(1, 3)).toBeGreaterThan(0.3);        
  19:     })
  20: });

But you can replace the existing matchers with a custom matcher like this.



   1: describe('Calculator', function () {
   2:     var calc;
   3:  
   4:     beforeEach(function () {
   5:         calc = new Calculator();
   6:  
   7:         jasmine.addMatchers({
   8:             toBeBetween: function (util, customEqualityTesters) {
   9:                 return {
  10:                     compare: function (actual, a, b) {
  11:                         var result = {};
  12:                         result.pass = actual >= a && actual <= b;
  13:                         return result;
  14:                     }
  15:                 }
  16:             }
  17:         });
  18:     });
  19:  
  20:     it('should be able to add 1 and 1', function () {
  21:         expect(calc.add(1, 1)).toBe(2);
  22:     });
  23:  
  24:     it('should be able to divide 6 by 2', function () {
  25:         expect(calc.divide(6, 2)).toBe(3);
  26:     })
  27:  
  28:     it('should be able to divide a rational number', function () {
  29:         expect(calc.divide(1, 3)).toBeBetween(0.3, 0.34);
  30:     })
  31: });

You can reuse custom matchers to make the test cases clear and more simple.


What are some scenarios that you use custom matchers?