Wednesday, 16 December 2020

MAC

Installation

  • Homebrew
    • brew install putty
  • MacPorts
    • port install putty
  •  

Putty

  • puttygen privatekey.ppk -O private-openssh -o privatekey.pem

Sunday, 13 December 2020

Linux

Unix vs Linux
  • Unix developed in mid-1970 for multi-user, multi-tasking
    • Mostly used by SUN and their operating system is Solaris, HP-UX, AIX 
    • Supports fewer file systems
  • Linux in 1991 by Linus Torvalds
    • Linux is mostly free
    • Open source
    • Used mostly by Developer communities, or companies(REDhad, Centos, Debian and many)
    • Can be installed on variety of computer hardware, mobiles, tablets, video games, super computers, main frames
    • Can be installed on any hardware that is running by Intel & AMD processor
Oracle Virtual Box
  1. Free and Open source Hypervisor for x86 computers developed by Oracle
Different Linux distributions
  • 200+ OS derived from linux like.. Redhat, Ubuntu, Fedora, suse, Debian, Cent os, 
  • Client
    • Windows
      • Cygwin 
    • mac/Linux client
      • ssh -V
  • Client Server architechture
    • ssh client on client system & ssh daemon on Server
  • commands
    • (sudo) yum -y install nano
    • nano /etc/ssh/sshd_config
    • edit line #PermitRootLogin yes to no
    • systemctl restart sshd
  • Navigation commands
    • ~   short cut for home directory
  • write file
    • echo > a.txt
    • touch b.txt   to create empty file
  • running as admin
    • sudo <command>   run single command as administrator
    • sudo su    to become administrator
  • Install new package
    • yum install java        yum is package manager for Centos systems
    • java -version
  • Bash & Shell
    • Shell is a processor to process what ever we type in terminal(bash, ksh, fish..)
    • Bash is a default processor
      • Bash script
        • #!/bin/bash                -> so say use bash shell
          for i in 1 2 3 4 5; do
          echo "Hello $i";
          sleep 2;
          done;
        • bash <script name>
  • Background process
    • & at the end of command
  • stopping background process
    • ps aux
    • kill -9 <pid>
  • start HTTP server
    • sudo python -m SimpleHTTPServer 80
  • Environment Variables
    • env    to list all env variables
    • echo $PATH
    • export  - adds new environment variable
      •  export abc="asdfasdf"
  • Strings
    • "   -> interpolating variables
    • '   -> preserving strings exactly
    • `  -> interpolating command output
      • echo "today is `date`"
  • Linux commands demistified
    • linux commands are executables
    • which <command> gives the location

SSH Fundamentals

  • Interactive ssh
    • ssh user@[hostname/ipaddress]
  • long lived processes
    • start process and keep it running after terminating ssh session
    • nohup <command>
      • It separates process from terminatl
  • non-interactive ssh
    • run multiple commands at a time
    • ssh user@[hostname/ipaddress]  <command>

Creating Server Alias

  • ~/.ssh/config add following
    Host [server_nickname]
    hostname [Server IP address]
    user [username]

How SSH works

  • SSH daemon listens at port 22
  • ECDSA is unique signature of server which can't be tampered. ESDSA is sent to client
  • Encrypted communication channel gets created between both parties
  • Client sends authentication details on encrypted channel
  • Server verified 
  • communication channel gets established. 

Uploading files(SCP)- uses ssh internally

  • upload
    • scp [local source path] user@[hostname/ipaddress]:[server_target_path]
  •  download
    •  
    • scp user@[hostname/ipaddress]:[server_source_path]  [local target path] 

SFTP - ssh file transfer protocol

  •  download cyberduck

SSH Key Authentication

  • Avoid typing password
  • More secure
  • Problem with passwd authentication
    • Secret information is sent over communication channel which can compromise security
  • Process
    • Generate public and private key (ssh key pair)
    • Upload public key on server
    • Tell client to use SSH authentication
  • Private & Public key
    • AssyPublic key can be distributed
    • Private key to be kept confidential
    • Data encrypted with public key can only be decrypted with private key
    • Private key cannot be figured out from public key
    • Public key can be figured/derived out from Private key
  •  Generate ssh keys
    • ssh-keygen -t rsa -b 4086 -C "just comments"
    • Connect to server ~/.ssh/authorized_keys file
    • copy content of public key to authorized_keys on a new line
  • Test ssh keys
    • ssh -i [privatekey] [user]@[server]

Amazon EC2

  • sudo su -    to become a root user
  • Dir structure
    • /   ->ROOT directory similar to  C:\ in windows
    • /root -> home directory for root user- similar to C:\Document and Setting\Administrator
    • /home  -> home directory for other users- similar to C:\Document and Setting\username
    • /usr -> softwares are installed- similar to c:\program files
    • /bin -> commands used by all users
    • /sbin -> commands used by super user(root)
    •  /var -> container variable data like logs, mails..
  • Basic commands
    • User Management
      • Super or root user -> most powerful & admin user
        • /root   is home directory with shell /bin/bash
      • Sytem user ->Created by applications or softwares
        • /var/ftp  is home directory with shell /sbin/no login
      • Normal user ->
        • /home/<user> is home directory with shell /bin/bash
    • user creation
      • useradd <option> <name>
        • UID & GID will be assigned to user
  • Software Management
    • yum for REDHAT
      • yum install <package>
      • yum remove 
      • yum update
      • yum info
      • yum list
    • yum install wget
  • Networking
    • hostname    ->servername
    • init 6   or reboot -> restart system
    • ping  -> to check if target is available or not
    • wget -> download files/packages
    • ifconfig -> to get ipaddress of your system
    • telnet ->to check if a particular port is used or not
    • curl ->access appln as from browser
  • port numbers
    • ftp - 21
    • ssh -22
    • telnet - 23
    • smtp - 25
    • dns - 53
    • http - 80
    • https - 443
  • ports usage
    • netstat -tulpn
  • Services(systemctl)
    • Service
      • Controls starting and stopping of services
      • service <name> <status | start | stop | reload | restart>
      • yum install httpd   -> command to install http
        • service httpd status
        • service httpd start
        • telnet localhost 80
        • curl localhost
    • chkconfig
      • Check availability of service
      • chkconfig --list
      • chkconfig <service> <on | off> -> to make service available/unavailable after restart(boot time)

SSH(Secure shell)

  • Method for secure remote login from one computer to another. 
  • Provides alternative options for authentication
  • Protects communications security and integrity with strong encryption
    • port - 22
    • Daemon - sshd
    • conf file - /etc/ssh/sshd_config
  •  HTTP
    • port - 80
    • Daemon - httpd
    • conf file - /etc/httpd/conf/httpd_config
    • default file - /var/www/html 
     
Process Management
  • ps -ef  -> list of processes which are running in system
  • kill -9 -> kill a process or service
  • jobs -> gives list of jobs getting executed
  • fg & %<job id> -> run process in foreground
  • bg -> run in background
  • top -> list top 20 process which are consuming more cpu
Archiving
  • gzip
    • Create compressed file 
    • gzip <file>  -> file.gz gets created
  • gunzip
    •  gunzip <file.gz>
  • tar
    • extract tar file
    • works on directories
    • tar -cvf <directory> dir1.tar directory1
    • tar -xvf  dir1.tar   ->  tar is a file
    • gzip dir1.tar ->dir1.gz
    • tar -xvzf dir1.gz  to extrat tar & directories -> does 2 steps
CronTab
  • daemon called crond
  • job scheduling
  • crontab -l
    • List crons
  • crontab -e
    • Min  Hr Day_Of_Month Month  Day_of_week  <command>
  • Execute job at 8.30 in mornring
    • 30 8 * * * 
  • Execute job at 2 pm every saturday
    • 0 14 * * 6
  • Execute job at 12am on Jul1st
    • 00 00 1 6 *
  • Execute job at 3.30pm every month 25th
    • 30 15 25 * *
Link files
  • Softlink
    • shortcut file
    • ln -s <source file> <dest file>
    • used to create short cut of shell script at /usr/local/bin/hello
  • Harklink
    • Backup file
    • ln <source file> <dest file>
Windows to Linux
  • Mobaxterm
  • winscp
Copy from LINUX to LINUX server
  • scp

Tuesday, 27 October 2020

Spring

  • To build enterprise java applications
  • simpler & light weight
  • Spring 4 Key Features
    • Inversion of Control
    • Dependency injection
    • Spring AOP
    • Spring MVC
  • Spring 5 new features
    • Spring WebFlux

Spring Framework

  • Goals
    • Light weight java development with pojos
    • Dependency injection for loose coupling
    • Declarative programming with AOP(Aspect Oriented Programming)
    • Minimize boiler plate java code
  • Core Container
    • Beans
    • Core
    • SpEL
    • Context
  • Infrastructure
    • AOP
    • Aspects
    • Instrumentation
    • Messaging
  • Data access Layer
    • JDBC
    • ORM -> Object relation mapping
    • Transactions
      • OXM
      • JMS
  • Web Layer
    • Servlet
    • Websocket
    • web
    • portlet
  • Test layer
    • Unit
    • integration
    • Mock
Spring Projects
  •     Only use what you need
    • Spring Cloud, Spring Data
    • Spring Batch, Spring Security
    • Spring for Android, Spring for web flow
    • Spring for Webservices, Spring for LDAP
    • Spring Boot, Spring Social

Dev Env setup

  • Tomcat8.5 + Eclipse 
  • Create Project
  • Create a new folder lib in the project
  • Download spring-framework-5.0.2-Release-dist.zip
  • Add jar to the lib folder in the project
  • Project properties -> java build path -> libraries -> classpath -> add jars -> select jars from lib folder
  • Now referenced libraries should show the jars added
Spring Inversion of Control
  • Design process of externalizing of construction and management of Objects
Spring Container
  • Primary functions 
    • Create and manage Objects(Inversion of control) - Object Factory
    • Inject Objects dependencies (Dependency management) -
  • Configuration
    • XML configuration (legacy)
    • Java annotations(modern)
    • Java source code(modern)
Spring development process
  • Configure spring beans in config file
    • applicationContext.xml -> define beans with bean ID & fully qualified class name
  • Create Spring container
    • This is generally known as ApplicationContext
    • ClassPathXmlApplicationContext context = new  ClassPathXmlApplicationContext("applicationContext.xml");
  • Retrieve beans from container
    • Coach coach = context.getBean("myCoach",MyCoach.class);
Spring Dependency Injection
  • Dependency is same as helper objects
  • Injection Types
    • Constructor Injection
    • Setter Injection
  • Development process - constructor injection
    • Create Dependency Interface & class that implements it
    • Create Constructor in your class with parameter as Dependency Interface type
    • Spring bean container/context
      • Define Dendency bean
      • Define regular bean and inject Dependency-bean as Constructor-arg
  • Development process - setter injection
Bean Scopes
  • Singleton
    • for stateless beans
  • prototype
    • new object for each request
    • for statefull bean
  • request
    •  
  • session
  • global-session

Configuration with Annotations(XML Component scan)

  • XML configuration can be too verbose for big projects
  • Scanning for component classes
    • Spring scans classes for special annotations
    • They automatically register them in container
  • Development process
    • Enable component scanning in spring config file
      • provide the base package for  component scan in config file
    • add @Component annotation to java class
    • retrieve bean  from spring container using context.getBean(<beanID>,Bean.class)
  • autoWiring
    • Spring looks for class that matches the property(class/interface) and injects it automatically.
    • Injection types
      • Construction
        • @Autowired on top of constructor
      • Setter
      • Field

Java Configuration

  • Development process
    • Annotate java class with @ComponentScan & @Configuration

Spring MVC

  • webpages to layout UI components
  • Spring beans(Controller, services)
  • Spring configuration(xm, annotations or java)
  • Controller
    • @Controller which inherits from Component
      • @RequestMapping -> controller method
  • Model
    • It is container of application data
  • Form Tags

AOP(Aspect Oriented Programming).

  • It is module/code for cross cutting concenrs
  • Technique based on concept of Aspect
  • Aspect encapsulates cross-cutting logic
  • It can be reused at multiple locations
  • same aspect can be applied based on configuration
  • Benefits
    • Code lies in one class and not scattered
    • code reuse and easy to change
    • Business code is clean
    • Configurable
  • Use cases
    • Logging, security, transactions
    • audit logging
    • exception handling
    • API Management
      • analytics/metrics
  • Disadvantages
    • Too many aspects - hard to follow app flow
    • minor perf cost for aspect execution
  • Terminology
    • Aspect - It is module/code for cross cutting concerns
    • Advice -What action is taken and when it should be applied
      • BeforeAdvice..........
    • Joint Point - When to apply code during program execution
    • Pointcut-
    • Weaving 
      • Connecting aspects to target objects to create an advised object
      • types of weaving
        • Compile time, load time or run time
  • Frameworks
    • SpringAOP, AspectJ
    • SpringAOP advantages
      • Simpler than SpectJ
      • users Proxy pattern
      • can migrate to AspectJ when using @Aspect annotation
    • Spring AOP disadvantages
      • supports only method-level joint points
      • can apply aspects to beans created by Spring app context
      • minor perf impact due to runtime aspect

Spring Security

  • HTTP Basic Authentication
    • builtin dialog from browser
  • Default login form
    • Spring provides default form
  • Custom login form

Spring REST

  • JSON
    • Databinding 
      • process of converting json data into java pojo and vice versa
      • spring uses jackson project behind the scene
      • Json data sent to spring controller will be automatically converted to pojo
      • java object returned from rest  controller will be converted to JSON
    • HTTP Request Message
      • Request line
      • Header variables
      • Message Body
    • REST Controller
    • Exception Handler
      • ResponseEntity is a wrapper on HTTP response object

Spring JDBC

  • Spring simplifies the way we use java technologies
  • JDBC 
    • Developers write lot of code which is repeated across classes in application
      Connection, Statement, setParameters,
    • handle checked exceptions
  • SpringJDBC
    • Simplifies jdbc by creating JDBCTemplate
    • it is combination of JDBC + Template design pattern
    • Template carries common or boiler plate code and reduces amount of developers work
    • JDBCTemplate depends on javax.sql.DataSource
  • Steps to use JDBCTemplate
    • config.xml
      • create/configure bean dataSource for class DriverManagerDataSource providing driverClassName, url, user, pwd
      • create/configure bean jdbcTemplate for class JDBCTemplate passing dataSource from above step
      • jdbcTemplat.update(sql, <params to sql>);
    • Query
      • RowMapper
        • Interace in Spring framework which needs to be implemented
        • It maps resultset into Object of class that we create
        • mapRow() to be implemented
        •  
    • config.xml
Spring Boot
  • Challenges addressed
    • what maven arche type to be used
    • what maven dependencies to be used
    • configuration file
    • what server to be used
  • Spring boot 
    • easier to get started with Spring development
    • minimize amount of manual configuration
    • auto config based on props file and jar class path
    • embedded server
    • Overall- It is self-contained unit
  • Spring initializer
    • create starter project, select dependencies,
      • https://start.spring.io/
    •  Creates Maven/gradle project
    • Import project into IDE
  • One can run the app from command line and IDE
    • java -jar <jar name>
  • Source code generated by spring boot initializer
    • import org.springframework.boot.autoconfigure.SpringBootApplication
      @SpringBootApplication  -> This is composed of other annotations
      • The annotation composed of @EnableAutoConfiguration
        @ComponentScan
        @Configuration
    • SpringApplication.run
      • Creates application context, register all beans
      • starts embedded tomcat server
    • application.properties for Custom properties
      • Change default port
        server.port=8181
      • Properties defined here can be referenced in using
        @Value("${<property name>}")
        private String var1
  • Spring boot dev tools
    • Automatically restart application when code is changed
    • simply add dependency spring-boot-devtools
  • Spring boot Actuator
    • Exposes end points to monitor application
    • Easily get dev ops functionality out of box
    • simply add dependency spring-boot-starter-actuator
      • ../health
        ../info
  • Run from Command line
    • java -jar <application.jar>
    • mvnw:spring-boot:run

SpringBoot - Datasource configuration for Hibernate 

  • application.properties
    • spring.datasource.url
    • spring.datasource.username
    • spring.datasource.password
  • Spring boot will automatically create beans for DataSource and EntityManager
  •  

Monday, 26 October 2020

Prometheus

  • The Prometheus monitoring system and time series database
  • To monitor workloads
  • open source monitoring solution
  • Scape data/metrics using http end points
  • All data is stored as TimeSeries
    • Every time series is identified by metric name and a set of key-value pairs, called labels

  • Types of metrics
    • Counter
      • Value that only goes up
    • Gauge
      • Numeric value that can go up and down(like cpu load)
    • Histogram
      • Sample observations(request duration, response sizes)
    • Summary
  • PromQL
    • Provides builtin operators and functions
    • expressions over time-series vectors
  • Querying metrics
    • Instant Vector
        • single sample for each time series - all sharing the same time stamp
        • eg: node_cpu_seconds_total
    • Range Vector
      • range of data points over time for each time series
      • eg: node_cpu_seconds_total[5min]
    • Scalar
      • Simple numeric floating point value
  • Service discovery
    • Automatic detection of devices and services offered by these devices on a computer network
  • Exporters
    • Export third party metrics  to Prometheus metrics

Alerting

  • Alerting rules
    • Rules live in Prometheus server config
    •  
  • Alert manager