initial commits
[random_node_code.git] / lxc / lxc-nodejs
1 #!/bin/bash
2
3 #
4 # lxc: linux Container library
5
6 # Authors:
7 # Paul Robinson <takigama@gmail.com>
8
9 # This library is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public
11 # License as published by the Free Software Foundation; either
12 # version 2.1 of the License, or (at your option) any later version.
13
14 # This library is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # Lesser General Public License for more details.
18
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with this library; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23 setup_node()
24 {
25         user=$3
26     rootfs=$1
27     name=$2
28     
29     echo "Creating default configuration, $rootfs/config - EDIT THIS CONFIG"
30     
31     pushd $rootfs
32     cat > config <<EOF
33 # there arent many config variables here
34 # just ip address, hostname and gateway
35 # debug=true
36 ip4address=eth0:10.0.3.100/24
37 ip4address=lo:127.0.0.1/8
38 ip6address=eth0:auto
39 hostname=nd
40 ip4gateway=10.0.3.1
41 ip6gateway=auto
42 dnsserver=10.0.3.1
43 checktimer=1000
44 EOF
45         popd
46
47     pushd $rootfs
48         cat > node_base/config <<EOF
49 # this is where a list of apps to startup are stored
50 # format is:
51 # appname:directory:commandline[:opts]
52 myapp:/node_code/somethingorother:dontrestart
53 EOF
54         popd
55 }
56
57
58 install_node()
59 {
60         user=$3
61     rootfs=$1
62     name=$2
63     res=0
64     tree="\
65 $rootfs/dev \
66 $rootfs/sbin \
67 $rootfs/proc \
68 $rootfs/etc \
69 $rootfs/tmp \
70 $rootfs/dev/pts \
71 $rootfs/dev/shm \
72 $rootfs/lib \
73 $rootfs/usr/lib \
74 $rootfs/node_modules \
75 $rootfs/node_base \
76 $rootfs/node_base/control \
77 $rootfs/node_base/logs \
78 $rootfs/lib64 \
79 $rootfs/usr/lib64"
80
81         mkdir -p $tree || return 1
82     chmod 755 $tree || return 1
83         
84     pushd $rootfs/dev > /dev/null || return 1
85
86     # minimal devices needed for busybox
87     mknod tty c 5 0       || res=1
88     mknod console c 5 1   || res=1
89     chmod 666 tty console || res=1
90     mknod tty0 c 4 0      || res=1
91     mknod tty1 c 4 0      || res=1
92     mknod tty5 c 4 0      || res=1
93     chmod 666 tty0        || res=1
94     mknod ram0 b 1 0      || res=1
95     chmod 600 ram0        || res=1
96     mknod null c 1 3      || res=1
97     chmod 666 null        || res=1
98
99
100         echo "Downloading latest nodejs binaries for linux x64"
101         #fname=/tmp/nodejs.$RANDOM.`date +%s`tar.gz
102         # TODO: DONT FORGET TO REMOVE THIS
103         fname=/tmp/nodejs.11298.tar.gz
104         #wget -O $fname http://nodejs.org/dist/v0.8.17/node-v0.8.17-linux-x64.tar.gz
105         
106         pushd $rootfs
107         tar xfz $fname
108         ln -s node*linux*64* node
109         popd
110         
111         pushd $rootfs
112         echo "Installing ip cmd from upper OS"
113         cp /sbin/ip ./sbin/
114         popd
115         
116         pushd $rootfs
117         echo "PWD is $PWD"
118         cat >> sbin/init <<EOF
119 #!/node/bin/node
120
121 var uid=$user
122 var fs = require("fs");
123 var mnt = require("/node_modules/mount");
124
125 mnt.mount("proc", "/proc", "proc");
126
127 console.log("starting scheduler");
128 setTimeout(scheduler, 2000);
129 setTimeout(startnode, 4000);
130
131
132 function scheduler()
133 {
134         console.log("scheduler called: ", process.getuid());
135
136         //fs.readdir("/proc", function(err, files) {
137                 //console.log("files: ", files);
138         //});
139
140         setTimeout(scheduler, 2000);
141 }
142
143 function startnode() {
144         var opts = { uid: uid, gid: uid };
145         var nodespawn = require("child_process").spawn,
146             nodeproc = nodespawn("/node/bin/node", ["/node_code/nc.js"], opts);
147         nodeproc.stdout.on("data", function(data) {
148                 console.log("data from nodespawn: " + data);
149         });
150         nodeproc.stderr.on("data", function(data) {
151                 console.log("stderrdata from nodespawn: " + data);
152         });
153         nodeproc.on("exit", function(data) {
154                 console.log("nodespawn died");
155         });
156 }
157 EOF
158
159         chmod a+x sbin/init
160         popd
161         
162         pushd $rootfs
163         echo "Installing mount via npm"
164         ./node/bin/npm install mount
165         popd
166         
167         
168     return $res
169 }
170
171
172 copy_configuration()
173 {
174     path=$1
175     rootfs=$2
176     name=$3
177
178 cat <<EOF >> $path/config
179 lxc.utsname = $name
180 lxc.tty = 1
181 lxc.pts = 1
182 lxc.rootfs = $rootfs
183 # uncomment the next line to run the container unconfined:
184 #lxc.aa_profile = unconfined
185 EOF
186
187 if [ -d "$rootfs/lib" ]; then
188 cat <<EOF >> $path/config
189 lxc.mount.entry=/lib $rootfs/lib none ro,bind 0 0
190 lxc.mount.entry=/usr/lib $rootfs/usr/lib none ro,bind 0 0
191 EOF
192 fi
193
194 if [ -d "/lib64" ] && [ -d "$rootfs/lib64" ]; then
195 cat <<EOF >> $path/config
196 lxc.mount.entry=/lib64 $rootfs/lib64 none ro,bind 0 0
197 EOF
198 fi
199
200 if [ -d "/usr/lib64" ] && [ -d "$rootfs/usr/lib64" ]; then
201 cat <<EOF >> $path/config
202 lxc.mount.entry=/usr/lib64 $rootfs/usr/lib64 none ro,bind 0 0
203 EOF
204 fi
205 }
206
207 usage()
208 {
209     cat <<EOF
210 $1 -h|--help -p|--path=<path> -u|--user=<uid> -i|--ip=<ip>
211 EOF
212     return 0
213 }
214
215 user=0
216 ip_last=$(($RANDOM%100+100))
217 ip=10.0.3.$ip_last/24
218
219 options=$(getopt -o hp:u:n:i:g: -l help,path:,user:,name:,ip:,gateway: -- "$@")
220 if [ $? -ne 0 ]; then
221         usage $(basename $0)
222         exit 1
223 fi
224 eval set -- "$options"
225
226 while true
227 do
228     case "$1" in
229         -h|--help)      usage $0 && exit 0;;
230         -u|--user)              user=$2; shift 2;;
231         -i|--ip)                ip=$2; shift 2;;
232         -g|--gateway)           gw=$2; shift 2;;
233         -p|--path)      path=$2; shift 2;;
234         -n|--name)      name=$2; shift 2;;
235         --)             shift 1; break ;;
236         *)              break ;;
237     esac
238 done
239
240 if [ "$(id -u)" != "0" ]; then
241     echo "This script should be run as 'root'"
242     exit 1
243 fi
244
245 if [ -z "$path" ]; then
246     echo "'path' parameter is required"
247     exit 1
248 fi
249
250 rootfs=$path/rootfs
251
252 install_node $rootfs $name $user
253 if [ $? -ne 0 ]; then
254     echo "failed to install the nodejs rootfs"
255     exit 1
256 fi
257
258 setup_node $rootfs $name $user
259 if [ $? -ne 0 ]; then
260         echo "Failed to setup node"
261         exit 1
262 fi
263
264 echo "sleeping for check - 20s"
265 #sleep 20
266
267 echo Params are, $user, $path, $name
268 #exit 1
269
270 copy_configuration $path $rootfs $name
271 if [ $? -ne 0 ]; then
272     echo "failed to write configuration file"
273     exit 1
274 fi