Thursday, 27 November 2014

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.


Thursday, 30 October 2014

Docker design doc1


A sample docker design doc, how we can implement docker in the production environment. Will update other docker command later in the blog.

The ports are just for example. Note: we can run the keepalived and haproxy on the same server where docker is running, just for simplicity I draw at different server.

Following image's original pdf link:
https://drive.google.com/file/d/0B02LZ59YLdSdaTBlVGdmX0RlWU0/view?usp=sharing



Thursday, 23 October 2014

vimrc

my quick ~/.vimrc

set hlsearch
syntax enable
set tabstop=4
set softtabstop=4
set expandtab
set showcmd
set cursorline
filetype indent on
set showmatch
set incsearch

Wednesday, 8 October 2014

nc - [ netcat ] - unix command

nc

0)

Copy a complete directory [ lets say a mysql data " but you should not take a running mysql data " ]

Go to destination computer's destination EMPTY directory where you want to dump the data and run:[[[  nc -l 3333 | tar -zxvf -   ]]] - Note you can use any free port at the place of 3333

now go to the source computer and type: [[[ tar cvz <directory> | nc destination_hostname 3333 ]]]

1)

Destination host:

nc -lp [3333] > [file]     [ listen to port 3333 and what ever comes from that port put that to <<file>>


Sources host:
cat <<file>> | nc -w 1 IP <<3333>>   [ to transfer a <<file>> of course host with netcat with having timeout of 1 sec " -w 1 " and on port of 3333


2)
man nc:

-l      Used to specify that nc should listen for an incoming connection rather than initiate a connection to a remote host.  It is an error to use this option in conjunction
             with the -p, -s, or -z options.  Additionally, any timeouts specified with the -w option are ignored.

-p source_port
             Specifies the source port nc should use, subject to privilege restrictions and availability.  It is an error to use this option in conjunction with the -l option.


-w timeout
             If a connection and stdin are idle for more than timeout seconds, then the connection is silently closed.  The -w flag has no effect on the -l option, i.e. nc will
             listen forever for a connection, with or without the -w flag.  The default is no timeout.


3)
CLIENT/SERVER MODEL
     It is quite simple to build a very basic client/server model using nc.  On one console, start nc listening on a specific port for a connection.  For example:

           $ nc -l 1234

     nc is now listening on port 1234 for a connection.  On a second console (or a second machine), connect to the machine and port being listened on:

           $ nc 127.0.0.1 1234

     There should now be a connection between the ports.  Anything typed at the second console will be concatenated to the first, and vice-versa.  After the connection has been set
     up, nc does not really care which side is being used as a ‘server’ and which side is being used as a ‘client’.  The connection may be terminated using an EOF (‘^D’).


4)
DATA TRANSFER
     The example in the previous section can be expanded to build a basic data transfer model.  Any information input into one end of the connection will be output to the other
     end, and input and output can be easily captured in order to emulate file transfer.

     Start by using nc to listen on a specific port, with output captured into a file:

           $ nc -l 1234 > filename.out

     Using a second machine, connect to the listening nc process, feeding it the file which is to be transferred:

           $ nc host.example.com 1234 < filename.in

     After the file has been transferred, the connection will close automatically.


5)
TALKING TO SERVERS
     It is sometimes useful to talk to servers “by hand” rather than through a user interface.  It can aid in troubleshooting, when it might be necessary to verify what data a
     server is sending in response to commands issued by the client.  For example, to retrieve the home page of a web site:

           $ echo -n "GET / HTTP/1.0\r\n\r\n" | nc host.example.com 80

     Note that this also displays the headers sent by the web server.  They can be filtered, using a tool such as sed(1), if necessary.

     More complicated examples can be built up when the user knows the format of requests required by the server.  As another example, an email may be submitted to an SMTP server
     using:

           $ nc localhost 25 << EOF
           HELO host.example.com
           MAIL FROM: <user@host.example.com>
           RCPT TO: <user2@host.example.com>
           DATA
           Body of email.
           .
           QUIT
           EOF



6)
PORT SCANNING
     It may be useful to know which ports are open and running services on a target machine.  The -z flag can be used to tell nc to report open ports, rather than initiate a con-
     nection.  For example:

           $ nc -z host.example.com 20-30
           Connection to host.example.com 22 port [tcp/ssh] succeeded!
           Connection to host.example.com 25 port [tcp/smtp] succeeded!

     The port range was specified to limit the search to ports 20 - 30.

     Alternatively, it might be useful to know which server software is running, and which versions.  This information is often contained within the greeting banners.  In order to
     retrieve these, it is necessary to first make a connection, and then break the connection when the banner has been retrieved.  This can be accomplished by specifying a small
     timeout with the -w flag, or perhaps by issuing a "QUIT" command to the server:

           $ echo "QUIT" | nc host.example.com 20-30
           SSH-1.99-OpenSSH_3.6.1p2
           Protocol mismatch.
           220 host.example.com IMS SMTP Receiver Version 0.84 Ready

7)
EXAMPLES
     Open a TCP connection to port 42 of host.example.com, using port 31337 as the source port, with a timeout of 5 seconds:

           $ nc -p 31337 -w 5 host.example.com 42

     Open a UDP connection to port 53 of host.example.com:

           $ nc -u host.example.com 53

     Open a TCP connection to port 42 of host.example.com using 10.1.2.3 as the IP for the local end of the connection:

           $ nc -s 10.1.2.3 host.example.com 42

     Create and listen on a Unix Domain Socket:

           $ nc -lU /var/tmp/dsocket

     Connect to port 42 of host.example.com via an HTTP proxy at 10.2.3.4, port 8080.  
This example could also be used by ssh(1); see the ProxyCommand directive in ssh_config(5)
     for more information.

           $ nc -x10.2.3.4:8080 -Xconnect host.example.com 42


8) Minimal web-server example:
You can set up netcat to act as a very basic webserver which can just serve one file:
while `netcat -lp 8080 -c 'echo HTTP/1.0 200 OK';echo;cat file`;do;done


9) local port forwarding:
This command would forward every request on port 8080 to port 80:
while `netcat -lp 8080 -c 'netcat localhost 80'`;do;done


10) getting telnet command output using nc: [ example memcache ]

printf 'stats\n' | nc 127.0.0.1 11211 | grep limit_maxbytes