Difference between pages "Zero Configuration Networking" and "Zope HOWTO"

From Funtoo
(Difference between pages)
Jump to: navigation, search
(add note about mdns4*)
 
m
 
Line 1: Line 1:
Zero Configuration Networking, also called Zeroconf or [[wikipedia:Bonjour_(software)|Bonjour]] ([http://developer.apple.com/softwarelicensing/agreements/bonjour.html Apple's trademark] for their Zero Configuration Networking implementation) is a suite of related technologies that allow networked devices to interoperate on a local network without requiring explicit configuration.
+
This page documents how to use Zope with Funtoo Experimental, which currently has good Zope support thanks to [[Progress Overlay Python]] integration.
  
= Requirements =
+
== About Zope ==
  
Zero Configuration Networking requires the following things to operate:
+
Zope is an Open Source application server framework written in Python. It has an interesting history which you should familiarize yourself with before starting Zope development, as it contains several interesting twists and turns.
  
* A valid IP address, obtained either by:
+
=== Zope History ===
** Static assignment
+
** DHCP
+
** Link-local Addressing (part of Zero Configuration Networking)
+
* A means to address other devices by name, provided either by:
+
** DNS
+
** multicast DNS (mDNS -- part of Zero Configuration Networking)
+
  
= Service Discovery =
+
{{Note}} This HOWTO targets Zope 2.13, which includes Five. It is typically the version you should be using for new Zope projects.
  
On top of this, Zero Configuration Networking also provides a means to discover what services are available on each device. This is something that is provided exclusively by Zero Configuration Networking and is called Zeroconf Service Discovery.
+
* There are two versions of Zope, Zope 2 and Zope 3. One might assume that Zope 3 is the version that people should use for new software development projects by default, but this is not the case. Most Zope-based projects continue to use Zope 2. Zope 3 was an attempt to redesign Zope 2 from scratch, and is completely different from Zope 2, but it was not adopted by the community.
  
= Configuration =
+
* There is also something called [http://codespeak.net/z3/five/ Five] (named because it is "2 + 3") that backports many of the new features of Zope 3 into the Zope 2 framework. Several projects will use Zope 2 plus Five in order to use some of the newer features in Zope. Five was merged into mainline Zope 2 in early 2010, and first appeared in Zope 2.8.
  
In order to get these sort of networking services running, some configuration needs to happen initially.
+
* You can learn more about the history of Zope 2, 3 and Five in the [http://svn.zope.org/Zope/trunk/src/Products/Five/README.txt?view=markup Five README].
  
== Link-local Addresses ==
+
* To make things even more interesting, work on [http://docs.zope.org/zope2/releases/4.0/ Zope 4] is underway, and it will be based on 2.13 rather than 3.x. It includes a number of [http://docs.zope.org/zope2/releases/4.0/CHANGES.html#restructuring incompatible changes] with prior versions.
 +
=== Zope Resources ===
  
It's only necessary to use link-local addressing if you have no other means of obtaining a valid IP address on your LAN. Typically, this is the case if you are setting up a small or ad-hoc network where no DHCP server has been configured.
+
Now that you understand what version of Zope you should be targeting (2.13), we can point you towards the correct documentation :)
  
To use link-local addressing, first set up the proper routes:
+
; [http://docs.zope.org/zope2/zope2book/ The Zope 2 Book]: This book provides a general introduction to Zope concepts and ZMI. It is a good place to start, but doesn't provide a direct introduction to Zope development. It's recommended that you skim through this book to familiarize yourself with Zope. It generally does not assume much prior knowledge about Web development or Python.
 +
; [http://docs.zope.org/zope2/zdgbook/ Zope Developer's Guide]: This guide will give you a better introduction to Zope development. It assumes you already know Python. Skip chapters 1 and 2 and start in [http://docs.zope.org/zope2/zdgbook/ComponentsAndInterfaces.html chapter 3], which covers components and interfaces. [http://docs.zope.org/zope2/zdgbook/Products.html Chapter 5] covers the creation of your first product.
 +
; Five: We're not done yet. There is a bunch of stuff in Zope 2.13 that is not in the official documentation. Namely, the stuff in Five. Check out [http://codespeak.net/z3/five/manual.html The Five Manual].
 +
; ZTK: [http://docs.zope.org/ztkpackages.html ZTK Documentation]
 +
; ZCA: [http://www.muthukadan.net/docs/zca.html A Comprehensive Guide to Zope Component Architecture] offers a good introduction to the programming concepts of ZCA. We also have a new page on [[Zope Component Architecture]] which will help you to understand the big picture of ZCA and why it is useful. ZCML ("Z-camel") is a part of ZCA and  was introduced in Zope 3, so typically you will find ZCML documented within Zope 3 documentation and book.
 +
; Content Components: Views and Viewlets: [http://docs.zope.org/zope.viewlet/index.html This tutorial on viewlets] also contains some viewlet-related ZCML examples near the end. The "Content Component way" of developing in Zope seems to be a Zope 3 thing and tied to ZCML. Chapter 13+ of Stephan Richter's ''Zope 3 Developer's Handbook'' (book) seems to cover this quite well. You will probably also want to check out Philipp Weitershausen's ''Web Component Development with Zope 3'' (book).
 +
; [http://wiki.zope.org/zope2/Zope2Wiki Zope 2 Wiki]: Main wiki page for all things related to Zope 2.
 +
; [http://docs.zope.org docs.zope.org]: This is the main site for Zope documentation.
  
<pre>
+
== First Steps ==
route add default dev eth0 metric 99
+
route add -net 169.254.0.0 netmask 255.255.0.0 dev eth0 metric 99
+
</pre>
+
  
Then, use avahi-autoipd to discover a valid link-local IP address:
+
First, you will need to emerge {{Package|net-zope/zope}}:
  
<pre>
+
<console>
/usr/sbin/avahi-autoipd --daemonize --syslog --wait eth0
+
# ##i## emerge -av zope
</pre>
+
</console>
  
Once a valid link-local IP address is found, eth0 will now have a 169.254.x.x address that can be used to communicate on the local LAN.
+
Zope is now installed.
  
As an alternative, you can have dhcpcd built with <code>USE="zeroconf"</code>, and it will provide a link local address if no DHCP server is found.
+
== Project Skeleton ==
  
== Multicast DNS ==
+
{{Note}} Zope should be run by a regular user account, not as the root user.
  
Multicast DNS, or mDNS, is a means by which individual machines can broadcast their DNS information to machines on the local LAN so that a DNS server is not required to address local devices by name. The ".local" domain is typically used for multicast DNS, so your laptop might be addressable by pinging "mylaptop.local", for example. mDNS is not necessary if you have some other means of addressing machines by name, such as unicast (regular) DNS. But many LANs do not have their own DNS server configured, in which case mDNS can be very handy.
+
The first step in using Zope is to ensure that you are using a regular user account. Create a new directory called ''<tt>zope_test</tt>'':
 +
<console>
 +
$##bl## cd
 +
$##bl## mkdir zope_test
 +
</console>
  
== Receiving mDNS ==
+
Now, enter the directory, and create an "instance", which is a set of files and directories that are used to contain a Zope project:
 +
<console>
 +
$##bl## cd zope_test
 +
$##bl## /usr/lib/zope-2.13/bin/mkzopeinstance
 +
</console>
  
The ebuild "nss-mdns" provides the necessary functionality for your Funtoo/Gentoo Linux machine to receive mDNS broadcasts, so that it can do things like ping mybox.local, or ssh mylaptop.local.
+
You will see the following output, and will be prompted to answer a few questions:
 +
<console>
 +
Please choose a directory in which you'd like to install
 +
Zope "instance home" files such as database files, configuration
 +
files, etc.
  
From the [http://0pointer.de/lennart/projects/nss-mdns/#overview nss-mdns] home page:
+
Directory: instance
 +
Please choose a username and password for the initial user.
 +
These will be the credentials you use to initially manage
 +
your new Zope instance.
  
''nss-mdns is a plugin for the GNU Name Service Switch (NSS) functionality of the GNU C Library (glibc) providing host name resolution via Multicast DNS (aka Zeroconf, aka Apple Rendezvous, aka Apple Bonjour), effectively allowing name resolution by common Unix/Linux programs in the ad-hoc mDNS domain .local.
+
Username: admin
 +
Password: ****
 +
Verify password: ****
 +
</console>
  
''nss-mdns provides client functionality only, which means that you have to run a mDNS responder daemon seperately from nss-mdns if you want to register the local host name via mDNS. I recommend Avahi.
+
Now, we will start our Zope instance:
 +
<console>
 +
$##bl## cd instance
 +
$##bl## bin/runzope
 +
</console>
  
''nss-mdns is very lightweight (9 KByte stripped binary .so compiled with -DNDEBUG=1 -Os on i386, gcc 4.0), has no dependencies besides the glibc and requires only minimal configuration.
+
Now that Zope is running, you can visit ''<tt>localhost:8080</tt>'' in your Web browser. You will see a nice introductory page to Zope.
  
''By default nss-mdns tries to contact a running avahi-daemon for resolving host names and addresses and making use of its superior record cacheing. Optionally nss-mdns can be compiled with a mini mDNS stack that can be used to resolve host names without a local Avahi installation. Both Avahi support and this mini mDNS stack are optional, however at least one of them needs to be enabled. If both are enabled a connection to Avahi is tried first, and if that fails the mini mDNS stack is used.
+
If you now go to the ''<tt>localhost:8080/manage</tt>'' URL, you will be prompted to log in. Enter the username and password you specified. You are now logged in to the ZMI (Zope Management Interface.)
  
<pre>
+
You can stop your application by pressing Control-C. In the future, you can start and stop your Zope instance using the following commands:
emerge nss-mdns
+
</pre>
+
  
Set up multicast route:
+
<console>
 +
$##bl## zopectl start
 +
$##bl## zopectl stop
 +
</console>
  
<pre>
+
{{Note}} ''<tt>zopectl start</tt>'' will cause your instance to run in the background rather than consuming a shell console.
route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
+
</pre>
+
  
Note: Adding <code>multicast="yes"</code> in <code>/etc/conf.d/netif.foo</code> (replace foo with your actual network interface name) will set up the multicast route automatically at startup.
+
== First Project ==
  
Now, it's necessary to modify <tt>/etc/nsswitch.conf</tt> so that your system will use multicast DNS for hostname lookup. In the example, we use the <tt>mdns_minimal</tt> and <tt>mdns</tt> words, which enable multicast DNS for IPv4 and IPv6. If you only want to enable IPv4-based multicast DNS, which is recommended for IPv4-only networks, use <tt>mdns4_minimal</tt> and <tt>mdns4</tt> instead. This will improve hostname lookup performance.
+
We will create a single very primitive Zope package, consisting of an Interface for a TODO class, and a TODO class.
  
/etc/nsswitch.conf:
+
Create the following files and directories relative to your project root:
  
<pre>
+
* Create the directory <tt>lib/python/example</tt>.
hosts:      files mdns_minimal [NOTFOUND=return] dns mdns
+
* Create the file <tt>lib/python/example/__init__.py</tt> by typing <tt>touch lib/python/example/__init__.py</tt>.
</pre>
+
* Create these files:
  
== Sending Multicast DNS ==
+
=== <tt>etc/package-includes/example-configure.zcml</tt> ===
  
Avahi-daemon handles the task of '''sending''' multicast DNS broadcasts, as well as service discovery broadcasts, on your local LAN. If you want other devices to be able to reach your Funtoo/Gentoo Linux machine via multicast DNS, and Zeroconf Service Discovery, you'll want to enable avahi-daemon. This will also improve the efficiency of performing multicast DNS lookups locally.
+
This file registers the ''<tt>example</tt>'' directory you created in ''<tt>lib/python</tt>'' as a ''package'', so that it is seen by Zope:
  
<pre>
+
<console>
rc-update add avahi-daemon default
+
<include package="example" />
rc
+
</console>
</pre>
+
  
Test:
+
=== <tt>lib/python/example/interfaces.py</tt> ===
  
<pre>
+
The following file defines the ''<tt>ITODO</tt>'' interface, and also uses some Zope Schema functions to define what kind of data we expect to store in objects that implement ''<tt>ITODO</tt>'':
ninja1 ~ # ping daniel-pc.local
+
PING daniel-pc.local (10.0.1.11) 56(84) bytes of data.
+
64 bytes from Daniel-PC.local (10.0.1.11): icmp_req=1 ttl=128 time=3.73 ms
+
64 bytes from Daniel-PC.local (10.0.1.11): icmp_req=2 ttl=128 time=0.905 ms
+
64 bytes from Daniel-PC.local (10.0.1.11): icmp_req=3 ttl=128 time=0.922 ms
+
64 bytes from Daniel-PC.local (10.0.1.11): icmp_req=4 ttl=128 time=0.827 ms
+
</pre>
+
  
== Service Discovery ==
+
<console>
 +
from zope.interface import Interface
 +
from zope.schema import List, Text, TextLine, Int
  
Get a list of services on the LAN:
+
class ITODO(Interface):
 +
    name = TextLine(title=u'Name', required=True)
 +
    todo = List(title=u"TODO Items", required=True, value_type=TextLine(title=u'TODO'))
 +
    daysleft = Int(title=u'Days left to complete', required=True)
 +
    description = Text(title=u'Description', required=True)
 +
</console>
  
<pre>
+
=== <tt>lib/python/example/TODO.py</tt> ===
ninja1 ~ # avahi-browse -ac
+
</pre>
+
[[Category:HOWTO]]
+
[[Category:Networking]]
+
[[Category:First Steps]]
+
  
= Resources =
+
Now, we define ''<tt>TODO</tt>'' to be a ''persistent'' object, meaning it can be stored in the ZODB. We specify that it implements our previously-defined ''<tt>ITODO</tt>'' interface, and provide reasonable defaults for all values when we create a new TODO object:
 +
<console>
 +
from persistent import Persistent
 +
from zope.interface import implements
 +
from example.interfaces import ITODO
  
[http://developer.apple.com/library/mac/#qa/qa2004/qa1357.html Apple Technical Q&A QA1357]
+
class TODO(Persistent):
 +
    implements(ITODO)
 +
    name = u''
 +
    todo = []
 +
    daysleft = 0
 +
    description = u''
 +
</console>
  
[http://en.gentoo-wiki.com/wiki/Avahi Gentoo Wiki Avahi]
+
=== <tt>lib/python/example/configure.zcml</tt> ===
  
[http://www.ibiblio.org/pub/linux/docs/HOWTO/other-formats/html_single/Multicast-HOWTO.html#toc2 Multicast over TCP/IP HOWTO]
+
Create an empty ''<tt>configure.zcml</tt>'' configuration file:
 +
<console>
 +
<configure xmlns="http://namespaces.zope.org/zope"
 +
    xmlns:five="http://namespaces.zope.org/five"
 +
    xmlns:browser="http://namespaces.zope.org/browser">
 +
</configure>
 +
</console>
  
[http://sitka.triumf.ca/pub/linux/multicast-FAQ Linux Multicast FAQ]
+
== Debug Mode ==
  
[http://www.multicastdns.org/ multicastdns.org]
+
We can test our first project by entering debug mode:
 +
<console>
 +
$##bl## bin/zopectl debug
 +
Starting debugger (the name "app" is bound to the top-level Zope object)
 +
</console>
 +
 
 +
Now, let's try creating a new TODO object and writing it out to a ZODB database:
 +
<console>
 +
>>> from ZODB import FileStorage, DB
 +
>>> storage = FileStorage.FileStorage('mydatabase.fs')
 +
>>> db = DB(storage)
 +
>>> connection = db.open()
 +
>>> import transaction
 +
>>> root = connection.root()
 +
>>> from example.TODO import TODO
 +
>>> a = TODO
 +
>>> a.name = u'My TODOs'
 +
>>> a.TODOS = [ u'Do Laundry', u'Wash Dishes' ]
 +
>>> a.daysleft = 1
 +
>>> a.description = u'Things I need to do today.'
 +
>>> root[u'today'] = a
 +
>>> transaction.commit()
 +
</console>
 +
 
 +
[[Category:HOWTO]]
 +
[[Category:Featured]]

Revision as of 14:21, 13 January 2014

This page documents how to use Zope with Funtoo Experimental, which currently has good Zope support thanks to Progress Overlay Python integration.

Contents

About Zope

Zope is an Open Source application server framework written in Python. It has an interesting history which you should familiarize yourself with before starting Zope development, as it contains several interesting twists and turns.

Zope History

Note Note: This HOWTO targets Zope 2.13, which includes Five. It is typically the version you should be using for new Zope projects.

  • There are two versions of Zope, Zope 2 and Zope 3. One might assume that Zope 3 is the version that people should use for new software development projects by default, but this is not the case. Most Zope-based projects continue to use Zope 2. Zope 3 was an attempt to redesign Zope 2 from scratch, and is completely different from Zope 2, but it was not adopted by the community.
  • There is also something called Five (named because it is "2 + 3") that backports many of the new features of Zope 3 into the Zope 2 framework. Several projects will use Zope 2 plus Five in order to use some of the newer features in Zope. Five was merged into mainline Zope 2 in early 2010, and first appeared in Zope 2.8.
  • You can learn more about the history of Zope 2, 3 and Five in the Five README.
  • To make things even more interesting, work on Zope 4 is underway, and it will be based on 2.13 rather than 3.x. It includes a number of incompatible changes with prior versions.

Zope Resources

Now that you understand what version of Zope you should be targeting (2.13), we can point you towards the correct documentation :)

The Zope 2 Book
This book provides a general introduction to Zope concepts and ZMI. It is a good place to start, but doesn't provide a direct introduction to Zope development. It's recommended that you skim through this book to familiarize yourself with Zope. It generally does not assume much prior knowledge about Web development or Python.
Zope Developer's Guide
This guide will give you a better introduction to Zope development. It assumes you already know Python. Skip chapters 1 and 2 and start in chapter 3, which covers components and interfaces. Chapter 5 covers the creation of your first product.
Five
We're not done yet. There is a bunch of stuff in Zope 2.13 that is not in the official documentation. Namely, the stuff in Five. Check out The Five Manual.
ZTK
ZTK Documentation
ZCA
A Comprehensive Guide to Zope Component Architecture offers a good introduction to the programming concepts of ZCA. We also have a new page on Zope Component Architecture which will help you to understand the big picture of ZCA and why it is useful. ZCML ("Z-camel") is a part of ZCA and was introduced in Zope 3, so typically you will find ZCML documented within Zope 3 documentation and book.
Content Components
Views and Viewlets: This tutorial on viewlets also contains some viewlet-related ZCML examples near the end. The "Content Component way" of developing in Zope seems to be a Zope 3 thing and tied to ZCML. Chapter 13+ of Stephan Richter's Zope 3 Developer's Handbook (book) seems to cover this quite well. You will probably also want to check out Philipp Weitershausen's Web Component Development with Zope 3 (book).
Zope 2 Wiki
Main wiki page for all things related to Zope 2.
docs.zope.org
This is the main site for Zope documentation.

First Steps

First, you will need to emerge net-zope/zope:

#  emerge -av zope

Zope is now installed.

Project Skeleton

Note Note: Zope should be run by a regular user account, not as the root user.

The first step in using Zope is to ensure that you are using a regular user account. Create a new directory called zope_test:

$ cd
$ mkdir zope_test

Now, enter the directory, and create an "instance", which is a set of files and directories that are used to contain a Zope project:

$ cd zope_test
$ /usr/lib/zope-2.13/bin/mkzopeinstance

You will see the following output, and will be prompted to answer a few questions:

Please choose a directory in which you'd like to install
Zope "instance home" files such as database files, configuration
files, etc.

Directory: instance
Please choose a username and password for the initial user.
These will be the credentials you use to initially manage
your new Zope instance.

Username: admin
Password: ****
Verify password: **** 

Now, we will start our Zope instance:

$ cd instance
$ bin/runzope

Now that Zope is running, you can visit localhost:8080 in your Web browser. You will see a nice introductory page to Zope.

If you now go to the localhost:8080/manage URL, you will be prompted to log in. Enter the username and password you specified. You are now logged in to the ZMI (Zope Management Interface.)

You can stop your application by pressing Control-C. In the future, you can start and stop your Zope instance using the following commands:

$ zopectl start
$ zopectl stop

Note Note: zopectl start will cause your instance to run in the background rather than consuming a shell console.

First Project

We will create a single very primitive Zope package, consisting of an Interface for a TODO class, and a TODO class.

Create the following files and directories relative to your project root:

  • Create the directory lib/python/example.
  • Create the file lib/python/example/__init__.py by typing touch lib/python/example/__init__.py.
  • Create these files:

etc/package-includes/example-configure.zcml

This file registers the example directory you created in lib/python as a package, so that it is seen by Zope:

<include package="example" />

lib/python/example/interfaces.py

The following file defines the ITODO interface, and also uses some Zope Schema functions to define what kind of data we expect to store in objects that implement ITODO:

from zope.interface import Interface
from zope.schema import List, Text, TextLine, Int

class ITODO(Interface):
    name = TextLine(title=u'Name', required=True)
    todo = List(title=u"TODO Items", required=True, value_type=TextLine(title=u'TODO'))
    daysleft = Int(title=u'Days left to complete', required=True)
    description = Text(title=u'Description', required=True)

lib/python/example/TODO.py

Now, we define TODO to be a persistent object, meaning it can be stored in the ZODB. We specify that it implements our previously-defined ITODO interface, and provide reasonable defaults for all values when we create a new TODO object:

from persistent import Persistent
from zope.interface import implements
from example.interfaces import ITODO

class TODO(Persistent):
    implements(ITODO)
    name = u''
    todo = []
    daysleft = 0
    description = u''

lib/python/example/configure.zcml

Create an empty configure.zcml configuration file:

<configure xmlns="http://namespaces.zope.org/zope"
     xmlns:five="http://namespaces.zope.org/five"
     xmlns:browser="http://namespaces.zope.org/browser">
</configure>

Debug Mode

We can test our first project by entering debug mode:

$ bin/zopectl debug
Starting debugger (the name "app" is bound to the top-level Zope object)

Now, let's try creating a new TODO object and writing it out to a ZODB database:

>>> from ZODB import FileStorage, DB
>>> storage = FileStorage.FileStorage('mydatabase.fs')
>>> db = DB(storage)
>>> connection = db.open()
>>> import transaction
>>> root = connection.root()
>>> from example.TODO import TODO
>>> a = TODO
>>> a.name = u'My TODOs'
>>> a.TODOS = [ u'Do Laundry', u'Wash Dishes' ]
>>> a.daysleft = 1
>>> a.description = u'Things I need to do today.'
>>> root[u'today'] = a
>>> transaction.commit()