help-guix
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: declarative containers (was Re: [EXT] Re: Enterprise Guix Hosting?)


From: Wojtek Kosior
Subject: Re: declarative containers (was Re: [EXT] Re: Enterprise Guix Hosting?)
Date: Mon, 23 Jan 2023 18:59:11 +0100

Witaj, Przemku!

I don't have anything with Postgres at hand but I do have a container
definition with services that use Flask[1] :) I also didn't see any
convincing examples of WSGI/CGI applications in Guix so I figured out a
working solution by myself. Here[2] is the definition of one of my WSGI
packages (which is later imported by code in [1]).

My Guix code got quite complex by now. I explain the crucial parts
below.

1. I packaged my Flask apps so that each of them has a valid WSGI
   script in the store. That's tricky because such WSGI script is going
   to be executed by some HTTP server (Apache in my case) which by
   default does not know about the extra Guix stuff that needs to be
   put in the GUIX_PYTHONPATH. There might be different approaches to
   this problem but I solved it by embedding GUIX_PYTHONPATH in the
   very WSGI script. Below I'm quoting the relevant part of my package
   definition from [2].

(arguments
     `(#:phases
       (modify-phases %standard-phases
         (add-after 'unpack 'replace-wsgi.py
           (lambda* (#:key inputs outputs #:allow-other-keys)
             ;; In the wsgi.py file, embed the PYTHONPATH containing both the
             ;; dependencies and the python modules of this package. This will
             ;; make them available at runtime.
             (let ((pythonpath
                    (string-append (getenv "GUIX_PYTHONPATH")
                                   ":"
                                   (site-packages inputs outputs))))
               (substitute* "wsgi.py"
                 (("^from .* import .*" import-line)
                  (string-append
                   "# Make Guix-installed dependencies visible to Python.\n"
                   "import sys\n"
                   "sys.path.extend('" pythonpath "'.split(':'))\n"
                   "\n"
                   import-line))))))
         (add-after 'install 'install-wsgi-script
           (lambda* (#:key inputs outputs #:allow-other-keys)
             (let* ((out (assoc-ref outputs "out"))
                    (share-dir (string-append out "/share/koszko-org-website")))
               (mkdir-p share-dir)
               (copy-file "wsgi.py" (string-append share-dir "/wsgi.py"))))))))

2. In the operating system (well, container...) declaration I make use of
   `file-append` from `(guix gexp)` to construct the WSGI script paths
   for use in HTTP server configuration. In a simplified setting it
   could look more of less like

(define %my-store-file-object
  (file-append package "/share/koszko-org-website/wsgi.py"))

3. I ungexp such objects wherever in the configuration I need the
   actual WSGI script path. In [1] I used Apache and wrote my own helper
   functions and structures to complement its minimal configuration
   system that Guix provides. If (like probably most Guix users out
   there) you are instead using the better-supported Nginx, it should
   be easier and there's no need for you to look into my helper
   functions. If for some awkward reason you want to use Apache like I
   do, feel free to adapt my code from [1].

It might all be a bit overwhelming at first but once you get the grasp
of gexps (described in Guix manual) it gets pretty approachable :)

Feel free to ask again in case I missed some important detail.

Pozderki,
Wojtek

[1] https://git.koszko.org/koszko-org-server/plain/container.scm
[2] 
https://git.koszko.org/koszko-org-website/plain/guix-module-dir/koszko-org-website.scm

-- (sig_start)
website: https://koszko.org/koszko.html
PGP: https://koszko.org/key.gpg
fingerprint: E972 7060 E3C5 637C 8A4F  4B42 4BC5 221C 5A79 FD1A

♥ R29kIGlzIHRoZXJlIGFuZCBsb3ZlcyBtZQ== | ÷ c2luIHNlcGFyYXRlZCBtZSBmcm9tIEhpbQ==
✝ YnV0IEplc3VzIGRpZWQgdG8gc2F2ZSBtZQ== | ? U2hhbGwgSSBiZWNvbWUgSGlzIGZyaWVuZD8=
-- (sig_end)


On Mon, 23 Jan 2023 16:48:17 +0000
Przemysław Kamiński <cgenie@pm.me> wrote:

> On 23.01.2023 16:34, Giovanni Biscuolo wrote:
> > Hello everybody,
> > 
> > (this is an old thread started on help-guix [1])
> > 
> > Ludovic Courtès <ludo@gnu.org> writes:
> >   
> >> "Thompson, David" <dthompson2@worcester.edu> skribis:
> >>  
> >>> On Wed, Aug 31, 2022 at 2:40 AM Ricardo Wurmus <rekado@elephly.net> 
> >>> wrote:  
> >>>>
> >>>> Another thing that seems to be missing is a way to supervise and manage
> >>>> running containers.  I use a shepherd instance for this with
> >>>> container-specific actions like this:  
> > 
> > [...]
> >   
> >>> Hey that's a real nice starting point for a container management tool!
> >>>   So maybe there should be a system service to manage containers and
> >>> then a 'docker compose'-like tool for declaratively specifying
> >>> containers and their network bridging configuration that is a client
> >>> of the service?  
> >>
> >> Agreed!  We could turn Ricardo’s code into ‘container-guest-service’ or
> >> something and have ‘containerized-operating-system’ add it
> >> automatically.  
> > 
> > please there was some progress with this service?
> > 
> > once done, could it be possible to declaratively start a whole network
> > of containers using a dedicated home-service, or
> > containerized-operating-systems (also on foreign distros)?
> > 
> > right now with "guix system container" we can imperatively manage
> > (start/stop, connect to the console with nsenter) and connect them
> > to the network [2], Ricardo showed us how he do it programmatically;
> > having a declarative interface (os-records) whould be awesome!
> > 
> > I'm very interested and willing to test it, if needed
> > 
> > thanks! Gio'
> > 
> > 
> > [1] id:878rn4syql.fsf@elephly.net
> > 
> > [2] thank you Ricardo for the cookbook section!
> > https://guix.gnu.org/en/cookbook/en/guix-cookbook.html#Guix-System-Containers
> >   
> 
> 
> Does anyone have a simple example of a container with PostgreSQL and 
> some web service like Flask? I'm new to Guix and I did see the 
> PostgreSQL example that is linked in codebook but I'm missing an example 
> of adding a custom service and was a bit overwhelming when I looked at 
> the source code.
> 
> Best,
> Przemek


Attachment: pgpNtjC0TxjcJ.pgp
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]