Skip to content
37 changes: 31 additions & 6 deletions devops/01 - git/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,39 @@

1. What command can I use to view the commit history?

1. What command can I use to undo the last commit?
Answer 1: **git log**

1. What command can I use to create a new branch and a new tag?
2. What command can I use to undo the last commit?

1. How do I exclude a file / folder from a commit?
Answer 2: **the “git reset” command with the “–soft” option that will preserve changes done to your files. and "--hard incase of removing changes"**

1. In case of a merge conflict, what commands can be used to resolve it?
3. What command can I use to create a new branch and a new tag?

1. `*` What are pre-commit hooks and post-commit hooks, and what are they for?
Answer 3:
***A. git branch: us used to create, list,rename and delete branches. Moreover, we can use (git checkout -b) to creare***

1. `*` How do I change the last commit without adding a new commit?
***B. tagging are use to make a point in git history (e.g. v1.2). git tag <tag name> is used to create tag.***

4. How do I exclude a file / folder from a commit?

Answer 4: ***A. you can also use (git update-index --assume-unchanged filepath) this command modifies the index or directory cache.***

***B. To exclude a file from being published, we need to creat a file named ".gitignore". in this we can list the names of the files that we don't want to expose it either to local or remote repo. Files could be some (key, passpord) such that we don't want to expose it***

5. In case of a merge conflict, what commands can be used to resolve it?


Answer 5:

***(git log --merge):Passing the --merge argument to the git log command will produce a log with a list of commits that conflict between the merging branches.***
***(git merge --abort) exit the merge process.***
***(git reset): Git reset can be used during a merge conflict to reset conflicted files to a know good state***

6. `*` What are pre-commit hooks and post-commit hooks, and what are they for?

Answer 6: ***hooks are the secriptes that run after or before a git action we do. suppose we want to write a script to send email everytime we commit.therefore we need to hook a script to the commit action.***
***Pre-commit hook: this hook or script runs before the action commit is being done, we use it in case we want to run the hook script before commit (f.e. pervent commit action, or give a warning.) in the other hand we have post-commit hooks with run after a commit is done, we use it for some notification purpose. (f.e. email me each time commit is done.)***

7. `*` How do I change the last commit without adding a new commit?

Answer 7: ***we can use (git commit --amend --no-edit)***
24 changes: 22 additions & 2 deletions devops/02 - dockerfile/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
FROM golang:1.15
# tagged as builder

FROM golang:1.15 AS builder
WORKDIR /usr/src/app
COPY main.go .

# copy and downloading go dependecies fromm go.mod file.
COPY go.mod .
RUN go mod download

#
COPY .env .
RUN go get -d github.com/joho/godotenv
# below address of godotenv is added as binary. old address was as library which we don't need.
RUN go get -d -v github.com/joho/godotenv/cmd/godotenv
# RUN go get -d github.com/joho/godotenv
RUN CGO_ENABLED=0 GOOS=linux go build -o simple-webpage .


# another stage added and copies the binary file from prebiew stage
FROM alpine
WORKDIR /app
COPY --from=builder /usr/src/app /app/
CMD ["./simple-webpage"]
# we can expose incase of inter-container connection, also this is a hint to show on which port our app is running.
EXPOSE 8888



43 changes: 43 additions & 0 deletions devops/02 - dockerfile/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,61 @@ You need to optimize the Dockerfile by correcting or adding steps.

1. What is Docker? Which technology is it based on?

Answer 1: ***docker is an OS lever virtualization which uses the container based approach to isolade an application. We pack all the libraries and neccesarry dependencies of an application in so called docker image. And a container is an image in running mode.***

2. Look at the Docker file – what would you change in it?

Answer 2: *** A docker file is simply a text based configuration file to build an image. basically we mention all the steps need to be done to build the image (f.g. docker build) command. I changed updated the docker file and new we can run our go app with out any problem. all the changes have been commented, you can refer to the docker file.***


3. How do I pass variables to the Docker file when building and running the container?

Answer3: ***we can pass variable using ENV command in the docker file***

4. Why do we need multistage build ?

Answer 3:
***this approach is used to reduce the final size of the final build image and fastern the process. We can use mutli FROM command in our dockerfile to make it multi-stage***

## Tasks

* Dockerfile - generate .env file inside Dockerfile, provide value of port at build step.

* Multi-stage build – change the Dockerfile to make it multi-stage. Try to get the lowest container size.

Answer: ***below shows when we build the image in with regular practice and the size of the image is 858MB***
```
hamid@ubuntu:~/Desktop/internship/devops/02 - dockerfile$ sudo docker images | grep testgo
testgo latest 3f91a210f0a6 30 seconds ago 858MB
```
***we can also use alpine version of the golang image which also reduce the size of the docker image to 337 see below***
```
hamid@ubuntu:~/Desktop/internship/devops/02 - dockerfile$ sudo docker images | grep testgo-alpine
testgo-alpine latest 37d7953f8f3f 11 seconds ago 337MB
```

***But after editing the docker file into multi-stage mode we see a significant amount of change in size to 12.3MB as shown below***

```
hamid@ubuntu:~/Desktop/internship/devops/02 - dockerfile$ sudo docker images | grep stage
testgo-multi-stage latest 756259ca568b 56 seconds ago 12.3MB
```



* Compare size of docker images with and without multistage build.

* Write down all commands which you have used.

Generating alpine version of the image.

```
sudo docker build -t testgo-alpine .
sudo docker images | grep testgo-alpine
```
Generating multi-stage version of the image.

```
sudo docker build -t testgo-multiStage .
sudo docker build -t testgo-multi-stage .
```
52 changes: 50 additions & 2 deletions devops/03 - docker-compose/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,71 @@ Docker compose with 3 applications (frontend + backend + DB).

3. Browse to localhost:8080 to see the app in action.

Answer: I performed the above actions and I accessed web server as below shows.

![](https://i.imgur.com/ZIgtQaR.png)


## Questions

1. What is the difference between Docker Compose and dockerfile? Why do I need Docker Compose?

Answer 1:
***Docker file is used to create an image and it contains text based configurations and commands to assemble and the neccesarry steps to containerize an application, wherease the docker compose is used to run multi-container docker application.***

***Suppose we have a web app that uses the web server (i.e. nginx) and database (i.e mangodb). What we can do is to create a docker compose file which runs many services or images at once so we don't need to have one docker file for each service. docker compose is a powerful tool which makes things very easy***


2. How do I parameterize compose for different environments?

Answer 2: ***To parameterize a docker compose file, the easy way is to use .env file. we can put all the variables in this file and call them in docker compose.yaml file. this is a best practice of better control of the flow and to have less error fault***


3. What types of entities are used in docker-compose and for what purpose?

Answer 3: ***This question is a bit unclear for me. if I understood correctly this means all the configuration blocks such as building block, network block, port block, enviromental variables block, dependencies block and command blocks. from thier names they are self explainatory.***
4. `*` How to output application logs?
Answer 4: ***We can use below config in our docker file to output the log of the nginx application***
```

4. `*` How to copy\upload a file from host machine to the container?
# forward request and error logs to docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log \
&& ln -sf /dev/stderr /var/log/nginx/error.log
```
***As it is shown below i can access the log of nginx***

5. `*` How to save file changes made inside the container?
![](https://i.imgur.com/gP485XN.png)


5. `*` How to copy\upload a file from host machine to the container?

Answer 5: ***Below is the command***

```
hamid@ubuntu:$ sudo docker ps | grep nginx
b7c9cbc77177 nginx:1.13.5 "nginx -g 'daemon of…" About an hour ago Up About an hour 0.0.0.0:8080->80/tcp, :::8080->80/tcp example_nginx_1
hamid@ubuntu:$ sudo docker cp ~/Desktop/testfile b7c9cbc77177:/
hamid@ubuntu:$ sudo docker exec -it b7c9cbc77177 bash
root@b7c9cbc77177:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys testfile tmp usr var
root@b7c9cbc77177:/#
```
***Note: we can also coply the file while building image using docker file or docker compose***

6. `*` How to save file changes made inside the container?

Answer 6: ***Whatever changes that we make can be persist by commiting it. Below is the command***

```
sudo docker commit [CONTAINER_ID] [new_image_name]
```
***Note: we can also assign a folder in our host machine to make the container persistent in some cases we need***


## Tasks

Answer : *** I have less time for this task otherwise i would do that***

* Docker-compose has a bug - investigate it! What would you improve?

* Docker-compose with an environment file. Create 2 different environment files for docker-compose
Expand Down
21 changes: 20 additions & 1 deletion devops/04 - bash/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,30 @@
* Docker on your local machine to launch testing environment

## Questions
* Mention the advantages and disadvantages of bash scripts
* Mention the advantages and disadvantages of bash scripts.

Answer: ***Bash shell scripting is used for automation, best to save time and if we run command as bulk. It can be run in any unix-like OS***

***The problem with the bash scripting is that is slow and a seperate process runs at each command that runs in the scrips.***
* What types of variables are used in bash?
Answer: ***there are mainly two types of the bash variables, a: system-defined and b: user-defined variables.***

***System variables are those which are pre-defiend by the OS and is used for OS and the standard convention is UPERCASE letter we can see the list with (printevn, env, set commands ). And in the other hand user variables are those where we create it during the sripting.***

* What is pipes in shell script and how to use it?

Answer: ***Pipes are very useful tools when we want to redirect the output of the a command as input of another command the symbole for pipe is (|)***

* How to show unique values of the second column from the CSV comma-separated file and sort results with the alphabet?

Answer: ***we can show it using different command below is one of the examples***

```
cat path/to/the/file.csv | cut -f2 -d , | sort | uniq
```



## Task
Create script which provide aggregated information about analysises and they datasets. Information about each analysis/deployment could be retrieved from `awscli.sh`:
``` bash
Expand Down
116 changes: 116 additions & 0 deletions devops/05 - cloud-ops/Cloudformation-apache-ec2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "AWS CloudFormation Sample Template VPC_with_PublicIPs_And_DNS: Sample template that creates a VPC with DNS and public IPs enabled. Note that you are billed for the AWS resources that you use when you create a stack from this template.",
"Parameters": {
"KeyPair": {
"Description": "Name of the keypair to use for SSH access",
"Type": "String"
}
},

"Resources" : {
"VPC" : {
"Type" : "AWS::EC2::VPC",
"Properties" : {
"EnableDnsSupport" : "true",
"EnableDnsHostnames" : "true",
"CidrBlock" : "10.0.0.0/16"
}
},
"PublicSubnet" : {
"Type" : "AWS::EC2::Subnet",
"Properties" : {
"VpcId" : { "Ref" : "VPC" },
"CidrBlock" : "10.0.0.0/24"
}
},
"InternetGateway" : {
"Type" : "AWS::EC2::InternetGateway"
},
"VPCGatewayAttachment" : {
"Type" : "AWS::EC2::VPCGatewayAttachment",
"Properties" : {
"VpcId" : { "Ref" : "VPC" },
"InternetGatewayId" : { "Ref" : "InternetGateway" }
}
},
"PublicRouteTable" : {
"Type" : "AWS::EC2::RouteTable",
"Properties" : {
"VpcId" : { "Ref" : "VPC" }
}
},
"PublicRoute" : {
"Type" : "AWS::EC2::Route",
"DependsOn" : "VPCGatewayAttachment",
"Properties" : {
"RouteTableId" : { "Ref" : "PublicRouteTable" },
"DestinationCidrBlock" : "0.0.0.0/0",
"GatewayId" : { "Ref" : "InternetGateway" }
}
},
"PublicSubnetRouteTableAssociation" : {
"Type" : "AWS::EC2::SubnetRouteTableAssociation",
"Properties" : {
"SubnetId" : { "Ref" : "PublicSubnet" },
"RouteTableId" : { "Ref" : "PublicRouteTable" }
}
},
"PublicSubnetNetworkAclAssociation" : {
"Type" : "AWS::EC2::SubnetNetworkAclAssociation",
"Properties" : {
"SubnetId" : { "Ref" : "PublicSubnet" },
"NetworkAclId" : { "Fn::GetAtt" : ["VPC", "DefaultNetworkAcl"] }
}
},
"WebServerSecurityGroup" : {
"Type" : "AWS::EC2::SecurityGroup",
"Properties" : {
"GroupDescription" : "Enable HTTP ingress",
"VpcId" : { "Ref" : "VPC" },
"SecurityGroupIngress" : [
{"IpProtocol" : "tcp","FromPort" : "80","ToPort" : "80","CidrIp" : "0.0.0.0/0"},
{"IpProtocol" : "tcp", "FromPort" : "22", "ToPort" : "22", "CidrIp" : "0.0.0.0/0"}]
}
},
"WebServerInstance": {
"Type": "AWS::EC2::Instance",
"Properties": {
"InstanceType": "t2.micro",
"ImageId": "ami-8c1be5f6",
"NetworkInterfaces" : [{
"GroupSet" : [{"Ref": "WebServerSecurityGroup"}],
"AssociatePublicIpAddress" : "true",
"DeviceIndex" : "0",
"DeleteOnTermination" : "true",
"SubnetId" : {"Ref": "PublicSubnet"}
}],
"KeyName": {
"Ref": "KeyPair"
},
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"\n",
[
"#!/bin/bash -xe",
"sudo yum update -y",
"sudo yum install httpd -y",
"sudo /etc/init.d/httpd start",
"echo \"<html><body><h1>Awesome !!!</h1>\" > /var/www/html/index.html",
"echo \"</body></html>\" >> /var/www/html/index.html"
]
]
}
}
}
}

},
"Outputs" : {
"URL" : {
"Description" : "URL of the sample website",
"Value" : { "Fn::Join" : [ "", [ "http://", { "Fn::GetAtt" : [ "WebServerInstance", "PublicDnsName" ]}]]}
}
}
}
Loading