perling

Friday, September 30, 2011

Your own network space within LXC / EC2

Not really on the subject of Perl but very interesting still.

The below works with Ubuntu Lucid, but I'd expect the networking will working with any Linux box. You can ignore the apt stuff if you don't use Ubuntu.

This LXC configuration may help if you have a server at say EC2 and have multiple containers within that server and you want your own local network address space.

Then you may want to do port forwarding to the container too so I've put some stuff about it.

External address 74.125.237.16
Internal address 192.168.1.1 #-kind of acts like your router

Linux server configuration /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 74.125.237.16
netmask xxx.xxx.xxx.xxx #you'll have your our values
gateway xxx.xxx.xxx.xxx

auto br0
iface br0 inet static
address 192.168.1.1
netmask 255.255.255.0
bridge_stp off
bridge_maxwait 0
pre-up /usr/sbin/brctl addbr br0
post-up /usr/sbin/brctl setfd br0 0
post-up /sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
post-up echo 1 > /proc/sys/net/ipv4/ip_forward


There is some magic here that was adapted from Daniel Lezcano information

Container configuration /etc/network/interfaces
# Used by ifup(8) and ifdown(8). See the interfaces(5) manpage or
# /usr/share/doc/ifupdown/examples for more information.

auto lo
iface lo inet loopback

#auto eth0
#iface eth0 inet dhcp

auto eth0
iface eth0 inet static
address 192.168.1.209
netmask 255.255.255.0
gateway 192.168.1.1
broadcast 102.168.0.255
network 192.168.1.0

Don't forget to setup your containers resolv.conf, it should point to the nameservers your Linux server uses.

Also just a little gripe and some information that may come in handy if your using Lucid. LXC doesn't work by default because the Lucid LTS build doesn't have namespaces and I'm not entirely sure if LXC are going to work around it or if Lucid will have it rebuilt back into the kernel. Anyway to work around it you need to use an older kernel. This is my work around, I'm sure there are others out there, but rebuilding your kernel may not be your idea of fun.

Do this at your own risk too if you don't install your old kernel properly you may need to do some livecd magic to ensure a kernel is on the system.



sudo apt-get install python-software-properties
sudo add-apt-repository ppa:kernel-ppa/ppa
sudo apt-get install linux-image-2.6.32-21-generic
#you may need to remove more kernels unless you can be bothered setting up grub2 to always boot the old kernel
sudo apt-get remove linux-image-2.6.32-33-generic
sudo update-grub



And just for good measure I think Serge has maybe been doing something to fix stuff with Lucid so you could throw in his update if you like



sudo add-apt-repository ppa:serge-hallyn/lxc-lucid-updates
sudo apt-get update
sudo apt-get install lxc


You can then use iptable to port forward stuff from the external address to the internal address. Just like a router port forward and you can do port re-address too.

Example iptables setup on Linux Server to Linux Container (192.168.1.2) - port forward port 4566 to port 4566


sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 5566 -j DNAT --to-destination 192.168.1.2:5566


Many thanks to all the other people on the Internet that have provided information on this subject. Disclaimer I haven't tried this on EC2, but I expect it still works.

Saturday, November 29, 2008

Catalyst Action REST - ExtJS TreeLoader

Catalyst doesn't play with ExtJS TreeLoader by default as the View::JSON returns hashes of JSON data, ExtJS TreeLoader requires an array. However you can setup View::JSON to expose a set hash key only, therefore only the array is returned.

Note that on existing stashes you will need to pre-pend the existing key with an addition key 'json' to ensure existing functionality works.

Catalyst Controller

$c->stash->{json} = \@nodes; #array of treeloader data


Catalyst Application lib/app.pm


__PACKAGE__->config(
'View::JSON' => {
expose_stash => 'json', #extjs TreeLoader requires an array of data
}
);

Friday, November 28, 2008

Catlyst use PDF::Table v0.93 doesn't generate PDF

PDF::Table - doesn't produce PDF files while running inside Catalyst unless you correct a known bug by using the following bug fix, see this link

Sunday, November 23, 2008

ModPerl Apache2 FasMmap no support threads

Cache::FastMmap does not support threads sorry at /usr/lib/perl5/Cache/FastMmap.pm line 1048.

This is a really annoying error and occured for me as I'd installed the wrong module

try running the following

apt-get install apache2-mpm-prefork
apt-get install libapache2-mod-perl2

Apache on a Flash Drive

If you're attempting to run Apache from a Flash drive this is EnableSendfile is well worth disabling, otherwise your web browser will just load zero byte files.....


EnableSendfile Off

http://httpd.apache.org/docs/2.0/mod/core.html#enablesendfile

Apache error message

[info] [client 127.0.0.1] (22)Invalid argument: core_output_filter: writing data to the network

Even by doing this I've found that Apache on a flash drive with bootable Linux doesn't really work......

Tuesday, July 15, 2008

AJAX - Printing Catalyst Error response in extjs

I'm creating an application using Ext JS still and have done some AJAX connections to my Catalyst framework (Perl). Sometimes the scripts fail and the response text includes CSS and other such wonderful HTML goodies which ruins my current browser page. However I'd like to carry on so I used a ExtJS window to display the results and it all works swimmingly

The code looks a little something like this (my page doesn't do code to well)


Ext.Ajax.request({
url: '/Contact/save',
method: 'POST',
waitMsg:'Saving Data...',
success: function (response, options) {
Ext.MessageBox.alert('Message', 'Saved OK' + response.responseText);
},

failure:function(response, options) {
//Ext.MessageBox.alert('Message', 'Save failed ' +response.responseText);

win = new Ext.Window({
title:'Save Failed - Back End Error Message',
plain: true,
height:400,
width:600,
autoScroll:true,
items: [{html:response.responseText}]
});
win.show(this);
},
jsonData: formJsonData
});


This post can also be found in the ExtJS forum

http://extjs.com/forum/showthread.php?p=196929#post196929

Thursday, July 10, 2008

Speed Reading Problems - Catalyst, DBIx

I'm a speed reader and unfortunately miss things, if you find yourself here you just might be a speed reader too ^^ Or perhaps you just intentionally don't read things :) I've spent too much time on figuring out the following when I could have just re-read the documentation. That's what I ended up doing anyhow.

My static files aren't accessible in the root url, where are they?

Catalyst static files can be accessed by specifying static in the URL path, I thought it would default to the root path, silly me.

http://hostname:3000/static/index.html

And I also had to add the following in my Root.pm to get it to serve and index.html file

my $file_path = '/TimeApp/root/static/index.html';
$c->serve_static_file($file_path);

Apache does it when I'm not developing but restarting Apache after every code change is a real drag.....

I can't access my DBIx alias column using the object notation

i.e.

'+select' => [{ extract => 'epoch FROM me.create_date'}],
'+as' => ['timestamp_date'],
.....
$c->log->info("create_date" . $record->timestamp_date;



It specifies in the documentation to use get_column for alias columns

Shameless copy, paste and alter from the DBIx Cookbook

'If on the other hand the alias does not correspond to an existing column, you have to fetch the value using the get_column accessor:

my $name_length = $record->get_column('create_date');'



I'm sure had I asked on IRc mst would have crucified me.
:)