DevOps

Replace text in a file using Batch script

Yesterday I was working at client site. It is a Windows server isolated from external internet access and prohibited to install any new software. I got a task in hand to remove all yaml files id to null. For example, the input file temp.yaml looks like this:

something
id: 4
something else
id: 64
next one
id: 231
another one
id: 34

and the target file(result.yaml) I want would be like this:

something
id:
somthing else
id:
next on
id:
another one
id:

It is a huge file and it would takes a long time to remove each id number one by one manually. The only tool accessible on that windows server machine is a CMD command prompt, therefore we could write a simple batch script to get the job done. Create a file called convert.bat with text editor like this:

@echo off
(for /f "tokens=1* delims=:" %%a in (temp.yaml) do (
if "%%b"=="" (echo %%a) else (
echo %%a|find " id" >null&& echo %%a: ||echo %%a: %%b
)
))>result.yaml

You can replace the text temp.yaml and result.yaml in the script to your target input and output file respectively. Double click to execute the script and you are done!

For those who are not familiar with Batch script, here are some basic explanation:

@echo off means to get rid of that C:Download prompt

The for loop has some option, where tokens= specify which numbered items to read from each line (default =1) and delims= specify the delimiter character (default = a space). The %%paramater are variables similar to arguments to batch files. The last line export the result to the file I want. It saves a lot of time with this simple script instead of doing manual work 🙂

Deploy Java Spring server with Docker container

I am deploying a Java Spring server using Docker. Here are the steps:

1. Launch a ubuntu server

In this demo, assume you have a server launched with ubuntu 14.04. Install docker via APT repository:

$ sudo apt-get update
$ sudo apt-get install apt-transport-https ca-certificates
$ sudo apt-key adv — keyserver hkp://p80.pool.sks-keyservers.net:80 — recv-keys 58118E89F3A912897C070ADBF76221572C52609D

Open /etc/apt/sources.list.d/docker.list with your favorite editor and add this line:

deb https://apt.dockerproject.org/repo ubuntu-trusty main

Install docker in the server as follow:

$ sudo apt-get update
$ sudo apt-get install docker-engine
$ sudo service docker start

2. Build Docker image

Login to Docker hub (https://hub.docker.com/) and create a new repository. Then in your terminal,

$ docker login

with your username and password.

In your local development Java spring folder, create a Dockerfile like this:

FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD target/fleet-beacon*.jar app.jar
EXPOSE 8080
RUN sh -c ‘touch /app.jar’
ENTRYPOINT [“java”, “-jar”,”/app.jar”]

And build the image:

$ docker build -t username/repo-name .

*where -t stands for tag, 
*replace username and repo-name with your namespace. 
*also don’t forget the dot .

Push the image to your remote repository:

$ docker push username/repo-name

3. Pull the docker image

In your remote ubuntu server, docker login and pull the image:

$ docker pull username/repo-name

Run it in the background

$ docker run -d -p 8080:8080 username/repo-name

*where -d means detached, 
*-p means publish all exposed ports to the host interfaces, for example 8080 to 8080.

4. Nginx setup

With the vim editor, open the file in /etc/nginx/sites-available/default, and edit as follow

server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
 root /usr/share/nginx/html;
index index.html index.htm;
server_name localhost;
location / {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:8080/;
}

Exit and save with :wq! that’s it. Open a browser and point to your remote server IP, you should be able to see the Java Spring page running.

5. Troubleshooting

Sometimes if you encounter a problem with the daemon connection:

Cannot connect to the Docker daemon. Is the docker daemon running on this host?

Run the command:

$ eval $(docker-machine env default)

If your are testing locally and couldn’t find your IP address, try this command to find the IP:

$ docker-machine ls

Leave a comment below if you encounter other issues as well.