sakana

very short memo

insert characters in multiple lines

Sometime you would like to insert same characters into multiple lines. I have to confess that I’ve been using sed command to achieve this, say,

$ sed -e "s/^/> /g" in.txt > out.txt

You can do the same within vim.

  • press ctrl + v
  • press shift + i (I)
  • input character(s) of your choice
  • press esc

So easy...

start mongo

It occurred to me that I want some place to store data like CSV file. Ok, let us use NoSQL like mongodb.

Like other tools, set up process is quite easy for anyone.

Installation requires only one line of command execution. It depends upon existing packages though, it may take time to download all the necessary package.

$ sudo apt-get install mongodb

Installation will automatically starts up daemon, mongod.

$ ps -ef|grep mongod
mongodb    759     1  0 03:47 ?        00:00:27 /usr/bin/mongod --config /etc/mongodb.conf

Configuration file shall be /etc/mongodb.conf. According to configuration file, database repository seems to reside under /var/lib/mongodb directory.

# Where to store the data.
dbpath=/var/lib/mongodb

mongod listesn on default port, TCP 27107.

$ sudo lsof -nPi:27017
COMMAND PID    USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
mongod  759 mongodb    6u  IPv4 113195      0t0  TCP 127.0.0.1:27017 (LISTEN)

OK, let us import CSV file into mongodb. You can import CSV file into collection, an quivalent of page in spreadsheet(?), under database.

$ mongoimport -d <DB_NAME> -c <COLLECTION_NAME> --type csv --file <CSV_FILE> --headerline

After installation, you will check its content from interactive shell.

$ mongo
MongoDB shell version: 2.2.4
connecting to: test
> use <DB_NAME>
switched to db salary
> db.<COLLECTION_NAME>.find().pretty()

port forwarding

Now you have working wiki in lxc network. Let’s configure instances to launch automatically upon system boot and can be accessed from outside network.

lxc Instance be started by creating symbolic link under /etc/lxc/auto directory, which points config file for each instance.

$ sudo ln -s /var/lib/lxc/wiki/config /etc/lxc/auto/wiki.conf
$ sudo ln -s /var/lib/lxc/proxy/config /etc/lxc/auto/proxy.conf

You will see AUTOSTART flag as set to YES.

$ sudo lxc-ls --fancy
NAME   STATE    IPV4       IPV6  AUTOSTART
------------------------------------------
proxy  RUNNING  10.0.3.20  -     YES
wiki   RUNNING  10.0.3.21  -     YES

By default, no one can access from outside.

$ sudo iptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination

Configure routing tables so that access to TCP port 443 shall be forwarded to reverse proxy instance.

$ sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 443 -j DNAT --to 10.0.3.20:443
$ sudo iptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination
DNAT       tcp  --  anywhere             anywhere             tcp dpt:https to:10.0.3.20:443

Now you can access wiki by IP address of host.

This change of routing table is not persistent and will be lost upon reboot. So, for example, add such lines as follows in /etc/rc.local file.

NETWORK_IF=eth0
WIKI_IP=10.0.3.20
WIKI_PORT=443

iptables -t nat -A PREROUTING -i $NETWORK_IF -p tcp --dport $WIKI_PORT -j DNAT --to $WIKI_IP:$WIKI_PORT