Thursday 27 November 2014

reading a file using golang

Example 1:

$cat run readFile.go 

package main

import(
"io/ioutil"
)

func main() {
contents,_ := ioutil.ReadFile("readFile.go")
println(string(contents))
}




Example 2:

$ cat fileRead.go 

package main

import(
"io/ioutil"
)

func main() {
contents,_ := ioutil.ReadFile("/etc/passwd")
println(string(contents))
}

go installation and build with cross platform compilation

On this note:

1. We do the installation of go on the mac
2. Then create a simple hello world program
3. Execute and test
4. Build the code [ for this example its for mac]
5. Setup cross platform build



Installation of go [ for this example on mac ]

  • For detail information: link: http://golang.org/doc/install
  • installed from the tar.gz, but you can try from mac pkg file too.
  • created a directory "go" under "/usr/local"
  • downloaded go1.3.3.darwin-amd64-osx10.8.tar.gz from https://golang.org/dl/
  • copied the file to /usr/local folder.
  • change the ownership or give the full access to your current user for /usr/local/go folder
  • extract the your download file go1.3.3.darwin-amd64-osx10.8.tar.gz it will extract everything to your /usr/local/go folder
  • update your PATH with adding /usr/local/go/bin
  • type go and you should see the go usage output. [ it mean you have successfully install go :) ]

Create a simple hello world program


package main
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}

 Execute and test

$ go run hello.go

hello, world


Build the code [ for this example its for mac]

$go build -o hello-mac hello.go

The above command will create an mac executable file.

$file hello-mac
hello-mac: Mach-O 64-bit executable x86_64

now you can run the code directly to the mac os:
$ ./hello-mac
hello, world


Setting up cross build

Further detail information: Link:
http://dave.cheney.net/2013/07/09/an-introduction-to-cross-compilation-with-go-1-1

$ cd /usr/local/go/scr
./all.bash [ if you get some error, try with sudo , just incase if you don't have write access. ]


https://github.com/davecheney/golang-crosscompile

$ git clone git://github.com/davecheney/golang-crosscompile.git
$ source golang-crosscompile/crosscompile.bash
[ if you get some error, try with sudo , just incase if you don't have write access. ]

go-crosscompile-build-all
[ if you get some error, try with sudo , just incase if you don't have write access. ]



This will compile the Go runtime and standard library for each platform. You can see these packages if you look in go/pkg.

% ls -1 go/pkg
darwin_386
darwin_amd64
freebsd_386
freebsd_amd64
linux_386
linux_amd64
linux_arm
obj
tool
windows_386
windows_amd64

And in your shell you can find all the following commands:

go                         go-darwin-386              go-linux-386               go-windows-386
go-all                     go-darwin-amd64            go-linux-amd64             go-windows-amd64
go-build-all               go-freebsd-386             go-linux-arm               godoc
go-crosscompile-build      go-freebsd-amd64           go-openbsd-386             gofmt
go-crosscompile-build-all  go-freebsd-arm             go-openbsd-amd64



go-build-all hello.go [ This will build your code for all the platform :) ]

and you can see all your build file:
hello-darwin-386 hello-freebsd-arm hello-mac hello-windows-amd64
hello-darwin-amd64 hello-linux-386 hello-openbsd-386 hello.go
hello-freebsd-386 hello-linux-amd64 hello-openbsd-amd64
hello-freebsd-amd64 hello-linux-arm hello-windows-386

I have tested and found, I can able to run the code in different arch.
NOTE: for windows build file, rename them to .exe before running them.


hello-darwin-386:    Mach-O executable i386
hello-darwin-amd64:  Mach-O 64-bit executable x86_64
hello-freebsd-386:   ELF 32-bit LSB executable, Intel 80386, version 1 (FreeBSD), statically linked, not stripped
hello-freebsd-amd64: ELF 64-bit LSB executable, x86-64, version 1 (FreeBSD), statically linked, not stripped
hello-freebsd-arm:   ELF 32-bit LSB executable, ARM, version 1 (FreeBSD), statically linked, not stripped
hello-linux-386:     ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, not stripped
hello-linux-amd64:   ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped
hello-linux-arm:     ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, not stripped
hello-openbsd-386:   ELF 32-bit LSB executable, Intel 80386, version 1 (OpenBSD), statically linked, for OpenBSD, not stripped
hello-openbsd-amd64: ELF 64-bit LSB executable, x86-64, version 1 (OpenBSD), statically linked, for OpenBSD, not stripped
hello-windows-386:   PE32 executable for MS Windows (console) Intel 80386 32-bit
hello-windows-amd64: PE32+ executable for MS Windows (console) Mono/.Net assembly
hello.go:            ASCII Java program text  <---- For me at mac it shows like this :)


Friday 21 November 2014

simple Dockerfile example1

1. cat Dockerfile

FROM cassandra
ADD start.sh /start.sh # to copy the file into
CMD ["bash", "./start.sh"]
EXPOSE 9160
EXPOSE 9042
EXPOSE 7000
EXPOSE 44478
EXPOSE 7199


2. cat start.sh
#!/bin/bash
service cassandra start
while true; do sleep 60; done  # Not sure any other best way for now

3. 
docker build -t="runcassandra" .     # this will create an image named runcassandra

4.
docker run -d -P --name cass1 runcassandra

NOTE: The cassandra will be up and will listen to rest of the port. But key thing is when you use EXPOSE, these ports get use for container to container communication ***ONLY***,  as per my current understanding, and this is what I found from the documentation too.


Wednesday 19 November 2014

copy file from host to the docker container

copy file from host to the docker container:

Steps:

1: Get container name or short container id :
  1a. docker ps
  1b. Get full container id
docker inspect -f '{{.Id}}' SHORT_CONTAINER_ID-or-CONTAINER_NAME

  1c. copy file :
  sudo cp path-file-host /var/lib/docker/aufs/mnt/FULL_CONTAINER_ID/PATH-NEW-FILE


EXAMPLE :

$docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
450b4b6a87e7 amit/ssh:latest /usr/sbin/sshd -D cranky_pare

$docker inspect -f '{{.Id}}' cranky_pare

or

$docker inspect -f '{{.Id}}' 450b4b6a87e7
450b4b6a87e734ba68d1c077de5d3d9ac617b1b9b6e829fe28c917abf1dce44f

From your docker server:
$ sudo cp file.txt /var/lib/docker/aufs/mnt/450b4b6a87e734ba68d1c077de5d3d9ac617b1b9b6e829fe28c917abf1dce44f/root/file.txt


docker networking with hack part1

I found the easy way of having multiple container running on the same host, bind with the host IP, then assign multiple IP the same interface of the host.

1. How do I bind multiple IP to the same interface:

amund@prod-docker-app01:~$ cat /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
address 172.22.19.75
netmask 255.255.255.0
network 172.22.19.0
broadcast 172.22.19.255
gateway 172.22.19.1
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 172.17.0.10 172.17.0.14 172.27.2.20
dns-search example.com example.net example.in

# bind-ed one assigned IP
auto eth0:0
iface eth0:0 inet static
        address 172.22.19.76
        netmask 255.255.255.0
        network 172.22.19.0
        broadcast 172.22.19.255
        gateway 172.22.19.1
        # dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 172.17.0.10 172.17.0.14 172.27.2.20
dns-search example.com example.net example.in

# bind-ed second assigned IP
auto eth0:1
iface eth0:1 inet static
        address 172.22.19.77
        netmask 255.255.255.0
        network 172.22.19.0
        broadcast 172.22.19.255
        gateway 172.22.19.1
        # dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 172.17.0.10 172.17.0.14 172.27.2.20
dns-search example.com example.net example.in

# bind-ed thired assigned IP
auto eth0:2
iface eth0:2 inet static
        address 172.22.19.78
        netmask 255.255.255.0
        network 172.22.19.0
        broadcast 172.22.19.255
        gateway 172.22.19.1
        # dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 172.17.0.10 172.17.0.14 172.27.2.20
dns-search example.com example.net example.in

2. If you are using remote host, good to reboot once and check your network update.