Friday, 27 July 2018

Example How to work with User and Instance profile in AWS IAM using CLI

How to create an IAM user and attach the user policy and group: 
aws iam --create-user --user-name james 
attach the user policy-AdministratorAccess: 
aws iam attach-user-policy --policy-arn arn:aws:iam::aws:policy/AdministratorAccess --user-name james
Add user to group: aws iam add-user-to-group --group-name admin --user-name james

How to Delete user and its policy :
aws iam detach-user-policy --user-name james --policy-arn arn:aws:iam::aws:policy/AdministratorAccess
Note: Before you delete the user, you should remove the user from the group if the user is added into the group 

  • Remove user from the group: aws iam remove-user-from-group --group-name admin --user-name james
  • Delete the user: aws iam delete-user --user-name james 

Create and Delete an instance profile Example: 
aws iam create-instance-profile --instance-profile-name WebserverProfile
aws iam add-role-to-instance-profile --instance-profile-name WebserverProfile --role-name role_ec2S3Fullaccess
Note: Before you delete the instance profile you need to detach the role from the instance profile 

  • aws iam remove-role-from-instance-profile --instance-profile-name WebserverProfile --role-name role_ec2S3Fullaccess 
  • aws iam delete-instance-profile --instance-profile-name WebserverProfile

Monday, 2 July 2018

How to Validate the AWS CloudFormation Template and execute the Dry-Run

Commands for validate the Cloudformation file and Run the CloudFormation template as a Dry-Run. The file can be in a json or yaml format To Run the below AWS commands on your terminal you should first install awscli and configure the user credentials and also a cloudformation template running in your region to which we are going to change some resources in the template and update the cfn template and run as dry-run and check the changes are valid or not and then execute the changes.

$ aws cloudformation validate-template --template-body file:///home/local/test/example.json

Working with ChangeSet:


create a changeset: aws cloudformation create-change-set --stack-name stackID|stackName --change-set-name changesetname --use-previous-template --parameters ParameterKey="InstanceType",UsePreviousValue=true ParameterKey="KeyPairName",UsePreviousValue=true ParameterKey="Purpose",ParameterValue="production"
View the changeset:
To get the ID of the change set, run the aws cloudformation list-change-sets command: aws cloudformation list-change-sets --stack-name stackID|stackName
Run the aws cloudformation describe-change-set command specifying the ID of the change set that you want to view: aws cloudformation describe-change-set --change-set-name changesetID
Execute the changeset: aws cloudformation execute-change-set --change-set-name changesetID
Delete the changeset: aws cloudformation delete-change-set --change-set-name changesetID

Friday, 7 July 2017

How can I shut down or reboot Ubuntu using terminal commands?

For Shutdown:
$sudo poweroff
(or)
$sudo shutdown -h now
(or)
$sudo halt
(or)
$sudo init 0
For Restart:
$sudo reboot
(or)
$sudo shutdown -r now
(or)
$sudo init 6

For Ubuntu 15.04 and later:
systemctl poweroff
systemctl reboot
systemctl suspend
systemctl hibernate
systemctl hybrid-sleep

Ubuntu 16.04 no need of say sudo also
To shutdown :
poweroff
To Reboot :
reboot

Install docker on Ubuntu 16.04 using command line interface (CLI)

Prerequisites
  • 64-bit Ubuntu 16.04 server
  • Ubuntu 16.04 Operating System
  • Step1 Installing Docker First, add the GPG key for the official Docker repository to the system: $curl -fsSL https://download.docker.com/linux/ubuntu/gpg |sudo apt-key add - Add the Docker repository to APT sources: $sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" update the package database with the Docker packages from the newly added repo: $sudo apt-get update Make sure you are about to install from the Docker repo instead of the default Ubuntu 16.04 repo: $apt-cache policy docker-ce Check that its Running: $sudo systemctl status docker Step2 Executing the Docker Command Without Sudo $sudo usermod -aG docker ${USER} $sudo usermod -aG docker username restart the machine $sudo shutdown -r now verify the docker commands are running without sudo: $docker info step3 Working with Docker Images $docker search ubuntu $docker pull ubuntu $docker run ubuntu $docker images step4 Running a Docker Container $docker run -it ubuntu root@d9b100f2f636:/# step5 Pushing Docker Images to a Docker Repository $docker login -u docker-registry-username $docker push docker-registry-username/docker-image-name

    Wednesday, 14 June 2017

    Write a bash script which remove all the ECS-Task Definition except the latest one

    !/bin/bash rev_arry=$(aws ecs list-task-definitions --family-prefix <taskdef_name> --query taskDefinitionArns[]|cut -f7 -d :| cut -c1-2) maxrev_num=($rev_arry) arrlen=$((${#maxrev_num[@]}-2)) latest_revision=$(aws ecs list-task-definitions --family <taskdef_name> --sort desc --max-items 1 --query taskDefinitionArns|cut -c64-65) n="1" while [ $n -lt $arrlen ] do if [ ${maxrev_num[n]} -eq $latest_revision ]; then n=$[$n+1] fi delrevision=${maxrev_num[n]} aws ecs deregister-task-definition --task-definition <taskdef_name>:$delrevision echo "ecr taskdefiniton:$delrevision is deleted" n=$[$n+1] done

    How do we install the PhpMyAdmin on ec2 instance and dockerize the phpmyadmin

    phpMyAdmin is a free software tool written in PHP, intended to handle the administration of MySQL over the Web Prechecklist: Install php yum -y install php make sure we have all these packages: rpm -qa | grep php php-common-5.4.16-42.el7.x86_64 php-fedora-autoloader-1.0.0-1.el7.noarch php-mysql-5.4.16-42.el7.x86_64 php-5.4.16-42.el7.x86_64 php-mbstring-5.4.16-42.el7.x86_64 php-php-gettext-1.0.12-1.el7.noarch php-bcmath-5.4.16-42.el7.x86_64 php-pdo-5.4.16-42.el7.x86_64 php-cli-5.4.16-42.el7.x86_64 php-tidy-5.4.16-7.el7.x86_64 php-gd-5.4.16-42.el7.x86_64 php-tcpdf-dejavu-sans-fonts-6.2.13-1.el7.noarch phpMyAdmin-4.4.15.10-1.el7.noarch php-xml-5.4.16-42.el7.x86_64 php-process-5.4.16-42.el7.x86_64 php-tcpdf-6.2.13-1.el7.noarch yum install php-mysqlnd . (A module for PHP applications that use MySQL databases) Install apache webserver on centos yum -y install httpd systemctl start httpd systemctl enable httpd systemctl status httpd Installation steps for phpMyAdmin: Add the EPEL Repository: CentOS and Red Hat Enterprise Linux 6.x wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm sudo rpm -Uvh epel-release-6*.rpm CentOS and Red Hat Enterprise Linux 7.x wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm sudo rpm -Uvh epel-release-latest-7*.rpm Install phpMyAdmin yum -y update yum -y install phpmyadmin Basic configuration for phpMyAdmin vi /etc/httpd/conf.d/phpMyAdmin.conf By default, the configuration for phpMyAdmin only allows access from the server on which it is installed. Find the following sections and change each IP address to the one you found in Step 3, or another IP address that will be connecting to phpMyAdmin remotely: <Directory /usr/share/phpMyAdmin/> AddDefaultCharset UTF-8 <IfModule mod_authz_core.c> # Apache 2.4 <RequireAny> Require ip 127.0.0.1 Require ip ::1 Replace the above block ,with the below mentioned code <Directory /usr/share/phpMyAdmin/> AddDefaultCharset UTF-8 <IfModule mod_authz_core.c> # Apache 2.4 <RequireAny> # Require ip 127.0.0.1 # Require ip ::1 Require all granted Database configuration file vi /etc/phpMyadmin/config.inc.php changes: $cfg['Servers'][$i]['host'] : Mention the endpoint of database $cfg['Servers'][$i][&#8216;port&#8217;] : Mention the port number $cfg['Servers'][$i]['extension'] : &#8216;mysqli &#8216;;// The php MYSQL extension to use Restart apache: systemctl restart httpd Dockerizing the phpmyadmin using the Dockerfile: FROM centos #FROM httpd:latest #RUN yum install -y update RUN yum install -y httpd #RUN systemctl start httpd #RUN systemctl enable httpd RUN yum install -y php RUN yum install -y php-mysqlnd php-fpm php-dba RUN yum install -y epel-release Run yum install -y phpmyadmin RUN mkdir -p /etc/phpMyAdmin COPY ./config.inc.php /etc/phpMyAdmin/config.inc.php COPY ./phpMyAdmin.conf /etc/httpd/conf.d/phpMyAdmin.conf EXPOSE 80 ADD run-httpd.sh /run-httpd.sh RUN chmod -v +x /run-httpd.sh CMD ["/run-httpd.sh"] #RUN service httpd start #CMD ["systemctl", "start", "httpd"] #CMD [ "httpd" , "start" ] #CMD [ "httpd" , "enable" ] References: ------------ https://www.liquidweb.com/kb/how-to-install-and-configure-phpmyadmin-on-centos-7/ https://www.digitalocean.com/community/tutorials /how-to-install-and-secure-phpmyadmin-with-apache-on-a-centos-7-server

    write a bash script which remove all the images from the ECR except the latest image

    !/bin/bash $myecs_securitytoken=$(aws ecr get-authorization-token --region <region_name> --output text --query authorizationData[].authorizationToken) $myecs_securitytoken tag=$(aws ecr list-images --repository-name <repository_name> --output text --query imageIds[].imageTag) imgflag=($tag) buildnum=$(curl --user <user_name>:uservalue$ http://<IP_address>: <port_no>/rest/api/latest/result/<BambooPlan_name>.json |sed 's/"buildNumber":/&\n/;s/.\n//;s/}/\n&/;s/\n.//' ) n="0" while [ $n -lt ${#imgflag[@]} ] do if [ ${imgflag[n]} -eq $buildnum ]; then n=$[$n+1] fi delimg=${imgflag[n]} aws ecr batch-delete-image --repository-name <repository_name> --image-ids imageTag=$delimg echo "ecr imagetag:$delimg is deleted" n=$[$n+1] done note: The images are differentiate with the image tags as a Bamboo build number All the images tags are collected in imgflag and iterate one by one and delete the images using the image tag

    Write a bash script which remove the instance from ECS cluster and chef server

    !/bin/bash /usr/bin/knife node delete -y -c /root/.chef/knife.rb <%= node['fqdn']%> /usr/bin/knife client delete -y -c /root/.chef/knife.rb <%= node['fqdn']%> containeruuid=($(aws ecs --region <region_name> list-container-instances --cluster <%= default['recipe_name']['cluster']%> --output text|cut -d '/' -f2)) myinstance=$(wget -q -O- http://169.254.169.254/latest/meta-data/instance-id) for myuuid in "${containeruuid[@]}" do ecsinstance=$(aws ecs describe-container-instances --cluster <%= default['recipe_name']['cluster']%> --container-instances $myuuid --output text --query containerInstances[].ec2InstanceId|cut -d: -f2) if [[ $ecsinstance = $myinstance ]] then aws ecs deregister-container-instance --cluster <%= default['recipe_name']['cluster']%> --container-instance ${containeruuid} --force exit else echo "Instance does not match inside the ecs cluster" fi done

    Starting the new ECS task on the ec2 instances using the latest TaskDefinition of EC2 container using bash script

    $myecs_securitytoken=$(aws ecr get-authorization-token --region <region_name> --output text --query authorizationData[].authorizationToken) $myecs_securitytoken mytaskdef=$(aws ecs list-task-definitions --family <task_familyname> --sort desc --max-items 1 --query taskDefinitionArns|cut -c75-76) echo $mytaskdef > taskd.out $aws ecs run-task --cluster "<cluster_name>" --task-definition "task_definition_name:cat taskd.out" Docker commands ------------------- Remove all the exited containers: docker rm -v $(docker ps -aq -f status=exited) Remove all the docker images: docker rmi -f $(docker images -q)

    Stopping the ECS Task which are running on ec2 instances using bash script

    #!/bin/bash $myecs_securitytoken=$(aws ecr get-authorization-token --region <region_name> --output text --query authorizationData[].authorizationToken) $myecs_securitytoken $aws ecs list-tasks --region <region_name> --cluster <cluster_name> --family <task_familyname> --query taskArns| cut -c46-81 > taskidinfo.out filename="taskidinfo.out" IFS=$'\n' for line in $(cat $filename) do $aws ecs stop-task --region <region_name> --cluster "<cluster_name>" --task $line --reason "stopping the task" done Note: Collect all the IDs of the task in the file taskidinfo.out, read each line from the file and stop the task using the taskID

    Wednesday, 22 February 2017

    Create a php project which retrieve records from AWS RDS Database instances running on AWS EC2 instances

    EC2 Instance RDS Instance ---------------------------------------------------------------- Instance type t2.micro (Free tier) db.t2.micro (Free tier) Availability zone us-east-1 us-east-1 Security group DemoSG DemoSG IAM role RDSfullaccess-role ---------- Root volume 8 GB SSD 8GB SSD Public IP address Yes Yes VPC ID and Subnet Default Default -------------------------------------------------------------- Security Group (DemoSG): -------------------------------------------------------------- inbound : open http (80), https (443), ssh (22) and MySQL/Aurora (3306) Outbound: All traffic IAM role: Attach AmazonRDSReadOnlyAccess policy -------------------------------------------------------------- Test the RDS Database connection from the Linux/Ubuntu Terminal --------------------------------------------------------------- Install the NETCAT software $sudo yum install nc Test the RDS connection $nc -zv <DB-instance-endpoint> <port> (Syntax) --------------------------------------------------------------- Steps to install Apache Webserver on Centos 7: --------------------------------------------------------------- step1: Connect to your EC2 instance and install the Apache web server $ sudo yum -y install httpd step2: About service. $ sudo systemctl start httpd [start the apache server] $ sudo systemctl enable httpd [apche start at boot] $ sudo systemctl status httpd [check the status of httpd] $ sudo systemctl stop httpd [stop the appache] step3: Create a sample html file [index.html] $ vi /var/www/html/index.html <html> <title>Demo Page </title> <body> <h1>Welcome to Apace Webserver! </h1></body> </html> step4: Open a browser window and enter the URL to access the file (It is the public DNS name of the EC2 instance followed by the file name) http://EC2-instance-public-DNS Step5: Add the www group to your instance $sudo group add www step7: Add your user (in our case centos) to the www group sudo usermod -a -G www centos Logout and connect the instance onces again step8:Change the group ownership of /var/www and connects to the www group sudo chown -R root:www /var/www step9: Recursively change the directory permissions of /var/www and its subdirectories to add group Write permissions to set the group ID on future subdirectories sudo chmod 2775 /var/www $find /var/www -type d -exec sudo chmod 2775 {}\; step10: Recursively change the file permissions of /var/www and its subdirectories to add group write Permissions $find /var/www -type f -exec sudo chmod 0664 {}\; ---------------------------------------------------------------------- Install the PHP, extensions and configure the php.ini ----------------------------------------------------------------------- step1: Connect to the EC2 Instance and install the PHP $sudo yum install php Step2: Restart the httpd service $sudo systemctl restart httpd.service Step3: Install the PHP extensions or Modules $sudo yum search php- (display all the php packages) $sudo yum install php-fpm (PHP FastCGI Process Manager) $sudo yum install php-dba (A database abstraction layer module for PHP applications) $sudo yum install php-mysqlnd . (A module for PHP applications that use MySQL databases) $sudo yum install php-pdo (A database access abstraction module for PHP applications) $sudo yum install php-pgsql (A PostgreSQL database module for PHP) Step4: Add the extension in php.ini in same order and restart httpd service (/etc/php.ini) extension pdo.so extension mysqlnd.so extension pdo_sqlite.so extension pdo_mysqlnd.so extension pdo_sqlite.so extension mysqlnd_mysql.so step5: Create files in /var/www/html ------------------------------------------------------------------------- index.html ------------------------------------------------------------------------- <html> <body> <h1>Connect to the DataBase! </h1> <form action="script.php" method="post"> <input type="submit" value=&#8221;connect&#8221;/> </form> </body> </html> ----------------------------------------------------------------- dbinfo.inc ------------------------------------------------------------------ <!--php define('DB_SERVER', 'RDS_EndPoint'); define('DB_USERNAME', 'RDS_masterUsername'); define('DB_PASSWORD', 'RDS_master_password'); define('DB_DATABASE', 'RDS_database_name'); ?--> ----------------------------------------------------------------- script.php ----------------------------------------------------------------- <html> <body> <h1>DATABASE TABLE </h1> <!--php include "dbinfo.inc"; $db=DB_DATABASE; /* Connect to MySQL and select the database. */ $con = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD); if (mysqli_connect_errno()) echo "Failed to connect to MySQL: " . mysqli_connect_error(); $statement="SELECT * FROM $db.Employee"; $result=mysqli_query($con,$statement); if(!$result){ die("SQL Error:".mysqli_error($con)); } // Fetch one and one row while ($row = mysqli_fetch_array($result)) { echo $row['name'],"<br-->"; } // Free result set mysqli_free_result($result); mysqli_close($con); ?> </body> </html> Step6: To bind the address form our httpd web server with RDS instances, we need to run the following Commands: $ausearch -c 'httpd' --raw | audit2allow -M my-httpd $semodule -X 300 -i my-httpd.pp ------------------------------------------------------------------------- Logs and Important files used for debugging ------------------------------------------------------------------------ httpd: /var/log/httpd/access_log /var/log/httpd/error_log /etc/httpd/conf/httpd.conf /etc/httpd/modules PHP modules: /usr/lib64/php/modules /etc/php.ini Datadir /var/lib/mysql Socket /var/lib/mysql/mysql.sock MySQL file /etc/my.cnf ------------------------------------------------------------------------ RDS Database SQL Quieres: ------------------------------------------------------------------------- Step1: MySQL workbench download url: http://dev.mysql.com/downloads/workbench/ Step2: Install the MSI installer [windowns] $sudo apt-get install mysql-server [ubuntu] $sudo yum install mysql-server [linux] Step3: >mysql -h yourDatabaseEndpoint -P 3306 -u USERNAME -p >show databases; [Show the Databases] >use database_name; [use the following database] >Create table Employee( ID integer(3),name varchar(20),desg varchar(20),sal integer(6)); >desc Employee; Step4: >insert into Employee(ID,name,desg,sal)values(101,'fasi','CEO',80000); >insert into Employee(ID,name,desg,sal)values(102,'Raj','SSE',50000); >insert into Employee(ID,name,desg,sal)values(103,'suraj','SE',30650); >insert into Employee(ID,name,desg,sal)values(104,'prem','HW',18800); Step5: > select * from Employee; ---------------------------------------------------------------------------------- IF you want to create Docker container Convert your PHP application into Image by using Docker file below: Note: copy the /etc/php.ini file to your /var/www/html/ where your Dockerfile is there Dockerfile: ------------ FROM centos/httpd RUN yum install -y php RUN yum install -y php-mysqlnd php-fpm php-dba COPY . /var/www/html/ COPY php.ini /etc/php.ini EXPOSE 80 Run these commands: --------------------- docker build -t fasi/httpd . docker run -d -p 80:80 fasi/httpd docker exec -it <contianerID> bash Test in the browser: http://yourEC2_public_IP_addresss

    Tuesday, 11 October 2016

    STEPS TO INSTALL JAVA ON LINUX x64 AND SETTING JAVA PATH

    step1: download the javaSE jdk-8u11-linux-x64.tar.gz step2: open the terminal and unzip the tar file $ tar -zxvf jdk-8u101-linux-x64.tar.gz step3: copy the unzip folder to usr/lib/java, $ cd /usr/lib $ sudo mkdir java $ cd ~/home/user/Downloads $ sudo mv jdk1.8.0_101/ /usr/lib/java $ cd /usr/java/java1.8.0_101/bin step4: Run the below commands sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/java/jdk1.8.0_101/bin/java" 1 sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/java/jdk1.8.0_101/bin/javac" 1 sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/lib/java/jdk1.8.0_101/bin/javaws" 1 step5: update the JAVA_HOME in your ~/.bashrc #JAVA HOME directory setup export JAVA_HOME=/usr/lib/java/jdk1.8.0_101 export PATH="$PATH:$JAVA_HOME/bin" VERIFY: $ java -version jdk1.8.0_101 version $ echo $JAVA_HOME /usr/lib/java/jdk1.8.0_101

    Saturday, 24 September 2016

    Demonstrate Dockerswarm to create, add, deploy and inspect the service

    Create a swarm

    (Make sure the Docker Engine daemon is started on the host machines) Step1:Open a terminal and ssh into the machine where you want to run your manager node. Step2:$docker swarm init --advertise-addr 192.168.99.100 To add a worker to this swarm, run the following command: docker swarm join \ --token SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0xx\ 192.168.99.100:2377 To add a manager to this swarm, run "docker swarm join-token manager" and follow the instructions. Step3:Run docker info to view the current state of the swarm:$ docker info Step4:Run the docker node ls command to view information about nodes: $ docker node ls

    Add nodes to the swarm

    (Once you've created a swarm with a manager node, you're ready to add worker nodes) Step5: Open a terminal and ssh into the machine where you want to run a worker node Step6: create a worker node joined to the existing swarm $ docker swarm join\ --token SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0xx\ 192.168.99.100:2377 If you don't have the command available, you can run the following command on a manager node to retrieve the join command for a worker: $ docker swarm join-token worker Step7: Open a terminal and ssh into the machine where you want to run a second worker node[worker2] Step8: $ docker swarm join\ --token SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0\ 192.168.99.100:2377 Step9:Open a terminal and ssh into the machine where the manager node runs. $ docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER 03g1y59jwfg7 worker2 Ready Active 9j68exjopxe7 worker1 Ready Active dxn1zf6l61qs * manager1 Ready Active Leader

    Deploy a service to the swarm

    (After you create a swarm, you can deploy a service to the swarm. For this tutorial, you also added worker nodes, but that is not a requirement to deploy a service) Step1:Open a terminal and ssh into the machine where you run your manager node Step2:$ docker service create --replicas 1 --name helloworld alpine ping docker.com Step3:$ docker service ls //shows the list of running services

    Inspect a service on the swarm

    (When you have deployed a service to your swarm, you can use the Docker CLI to see details about the service running in the swarm) Step1:If you haven't already, open a terminal and ssh into the machine where you run your manager node Step2:$docker service inspect --pretty helloworld //display the details about a service Step3:Run docker service ps <SERVICE-ID> to see which nodes are running the service: $docker service ps helloworld Step4:$ docker ps

    Scale the service in the swarm

    (Once you have deployed a service to a swarm, you are ready to use the Docker CLI to scale the number of service ps in the swarm) Step1: If you haven't already, open a terminal and ssh into the machine where you run your manager node $docker service scale <SERVICE-ID>=<NUMBER-OF-TASKS> $docker service scale helloworld=5 Step2: Run docker service ps <SERVICE-ID> to see the updated task list: $ docker service ps helloworld Step3: Run docker ps to see the containers running on the node.

    Delete the service running on the swarm

    Step1: If you havent already, open a terminal and ssh into the machine where you run your manager node $docker service rm helloworld Step2: Run docker service inspect <SERVICE-ID> to verify that the swarm manager removed the service: $docker service inspect helloworld

    Installing Docker (OperatingSystem : CentOs 7)

    There are two method to Install Docker in CentOS 7

    [Method1]: Install with yum

    $ cd /etc/yum.repos.d yum.repos.d]$ ls -la CentOs-Base.repo,CentOs-CR.repo,CentOs-Debuginfo.repo, CentOs-fasttrack.repo,CentOs-Media.repo,CentOs-Soureces.repo CentOs-Vault.repo,epel.repo, epel-testing.repo $ sudo vim docker.repo [dockerrepo] name=Docker Repository baseurl=https://yum.dockerproject.org/repo/main/centos/7/ enable=1 gpgcheck=1 gpgkey=https://yum.dockerproject.org/gpg $sudo yum update $sudo yum install docker-engine $sudo systemctl enable docker //To enable our docker service $sudo systemctl start docker //start the docker $sudo systemctl status docker //check the status of the docker By default the docker create a file in "/var/run" directory that own by the docker group called docker.sock, if you are not the member of that group or you are not root by default you dont have access to the docker daemon. $ sudo usermod -aG docker your_username //add your user to docker group $ docker images $ docker run hello-world // pull the image and create a Container

    [Method2]: Install with script

    $ sudo yum update //make sure your existing yum packages are up-to-date $ curl -fsSL https://get.docker.com/| sh //run the docker installation script This Script adds the docker.repo repository and installs Docker. $ sudo service docker start

    Uninstall Docker:

    $ yum list installed |grep docker //list the package you have installed $ sudo yum -y remove docker-engine.x86_64 //remove the package This command does not remove images, containers, volumes or user-created configuration files on host. $ rm -rf /var/lib/docker //delete all your images,containers,volumes

    Friday, 6 February 2015

    Socket Programming with Multiple Thread

    JSERVER.JAVA import java.io.*; import java.net.*; class JServer { public static void main(String[] args) { try{ ServerSocket ss=new ServerSocket(5700); while(true){ Socket cs=ss.accept(); Worker w=new Worker(cs); Thread t1=new Thread(w); t1.start(); } } catch(Exception e) {System.out.println(e.getMessage());} } } WORKER.JAVA import java.io.*; import java.net.*; class Worker implements Runnable { Socket s; Worker(Socket s) { this.s=s; } public void run() { try{ InputStreamReader isr=new InputStreamReader(s.getInputStream()); OutputStreamWriter osw=new OutputStreamWriter(s.getOutputStream()); BufferedReader br=new BufferedReader(isr); BufferedWriter bw=new BufferedWriter(osw); while(true){ String input=br.readLine(); bw.write("KMIT"+input); bw.newLine(); bw.flush(); } }catch(Exception e) {System.out.println(e.getMessage());} } } JCLIENT.JAVA import java.io.*; import java.net.*; class JClient { public static void main(String[] args) { try{ Socket s=new Socket("localhost",5700); InputStreamReader isr=new InputStreamReader(s.getInputStream()); OutputStreamWriter osw=new OutputStreamWriter(s.getOutputStream()); BufferedReader br=new BufferedReader(isr); BufferedWriter bw=new BufferedWriter(osw); while(true) { bw.write("HYDERABAD"); bw.newLine(); bw.flush(); String output=br.readLine(); System.out.println(output); } } catch(Exception e) {System.out.println(e.getMessage());} } }

    Given two strings a and b, find whether any anagram of string a is a sub-string of string b

    if a = xyz and b = afdgzyxksldfm then the program should return true. class StringDemo3 { static boolean checkAnagram(String s1,String s2) { int mycount[]=new int[128]; for (int i=0;i<s1.length();i++) { mycount[s1.charAt(i)]++; mycount[s2.charAt(i)]--; } for (int i=0;i<127;i++) { if(mycount[i]!=0) return false; } return true; } public static void main(String[] args) { String target="xyz"; String source="afdgzyxksldfm"; boolean flag=false; for (int i=0;i<source.length()-target.length()+1;i++) { if(checkAnagram(target,source.substring(i,i+target.length()))) { System.out.println("Anagram Found"); flag=true; break; } } if(!flag) System.out.println("Anagram Not Found"); } }

    Code a function that receives a string composed by words separated by spaces and returns a string where words appear in the same order but than the original string, but every word is inverted

    Example, for this input string "the boy ran" the output would be "eht yob nar" Tell the complexity of the solution class StringDemo2 { static String myRevers(String s) { StringBuilder sb=new StringBuilder(); for (int i=s.length()-1;i>=0;i-- ) { sb.append(s.charAt(i)); } return sb.toString(); } public static void main(String[] args) { String x="the boy ran"; } }

    Write a program to expand a given string x to y?

    input string x = "1..5,8,11..14,18,20,26..29" output string y = "1,2,3,4,5,8,11,12,13,14,18,20,26,27,28,29" import java.util.*; class StringDemo1 { public static void main(String[] args) { String x="1..5,8,11..14,18,20,26..29"; String sarr[]=x.split(","); StringBuilder sb=new StringBuilder(); for (String s:sarr) { if(s.indexOf('.')!=-1) { String limits[]=s.split("\\.\\."); int llimit=Integer.parseInt(limits[0]); int ulimit=Integer.parseInt(limits[1]); for (int i=llimit;i<=ulimit;i++) { sb.append(""+i+","); } } else sb.append(s+","); } System.out.println(sb); } }

    pattern 5

    1 2*3 4*5*6 7*8*9*10 7*8*9*10 4*5*6 2*3 1 #include <stdio.h> void draw_line(int n); void draw_pattern(int n,int m); void draw_rpattern(int n,int m); void main() { } void draw_line(int n) { int i; for (i=0;i<n-1;i++ ) { printf("%d*",n); } printf("%d\n",n); } void draw_pattern(int n,int m) { if(n==1) draw_line(n); else { draw_pattern(n-1,m); draw_line(n); if(n==m) draw_rpattern(n,m); } } void draw_rpattern(int n,int m) { if(n==1) draw_line(n); else { draw_line(n); draw_rpattern(n-1,m); } }

    pattern 4

    1 2*2 3*3*3 4*4*4*4 4*4*4*4 3*3*3 2*2 1 #include <stdio.h> void draw_line(int n); void draw_pattern(int n,int m); void draw_rpattern(int n,int m); void main() { draw_pattern(4,4); } void draw_line(int n) { int i; for (i=0;i<n-1;i++ ) { printf("%d*",n); } printf("%d\n",n); } void draw_pattern(int n,int m) { if(n==1) draw_line(n); else { draw_pattern(n-1,m); draw_line(n); if(n==m) draw_rpattern(n,m); } } void draw_rpattern(int n,int m) { if(n==1) draw_line(n); else { draw_line(n); draw_rpattern(n-1,m); } }