Shell Can someone test my attempt to get org babel working?

Here is my attempt to create an org babel script which in turn generates a shell script which creates a jail with apache running in it:-
Code:
#+begin_src shell :var JAIL="apache" :var pkgs='("apache24" "mod_php84") :tangle create-apache-jail.sh :shebang "#!/usr/bin/env sh" :results none
JAIL="$JAIL"
BASE="/zroot/iocage/jails/${JAIL}/root/usr/local"

# Create package list
cat <<EOF > ${JAIL}.json
{
    "pkgs": [
$(for pkg in $pkgs; do echo "        \"$pkg\","; done | sed '$s/,$//')
    ]
}
EOF

# Create and start the jail
iocage create -r latest -p ./${JAIL}.json -n ${JAIL} vnet=on dhcp=on
iocage start ${JAIL}

# Configure PHP for Apache
cat <<EOF > ${BASE}/etc/apache24/modules.d/001_php.conf
<IfModule dir_module>
    DirectoryIndex index.php index.html
    <FilesMatch "\.php$">
        SetHandler application/x-httpd-php
    </FilesMatch>
    <FilesMatch "\.phps$">
        SetHandler application/x-httpd-php-source
    </FilesMatch>
</IfModule>
EOF

# Create test PHP file
echo '<?php phpinfo(); ?>' > ${BASE}/www/apache24/data/index.php

# Enable and start Apache
iocage exec ${JAIL} sysrc apache24_enable=YES
iocage exec ${JAIL} service apache24 start
#+end_src

All comments, especially anything which shows how to improve this, are welcomed.

I should mention that this is for creating IOCAGE jails and assumes you have IOCAGE installed.
 
shell should be sh
dont use cat

you need a separate code blocks for json, conf and php

var are used when you when you call a noweb block from another block

you are misunderstanding what tangling does
it only writes out the code to a text file

tangling doesnt run code
 
you dont use cat at all
thats what the code blocks are for

with separate code blocks for each language
and each tangled ( written out ) to a text file at a specific location

specified by :tangle

eg

Code:
#+begin_src json

#+end_src

Code:
#+begin_src conf :tangle "/zroot/iocage/jails/apache/root/usr/local/etc/apache24/modules.d/001_php.conf"

#+end_src

Code:
#+begin_src php tangle: "/zroot/iocage/jails/apache/root/usr/local/www/apache24/data/index.php"

#+end_src

tangling is only for write the code blocks to a text file
thats it, it doesnt run code its not a shell script

you need to read the manual so you understand org babel code blocks
and what tangling then does
 
You mean something like this?


Code:
#+begin_src conf :tangle "/zroot/iocage/jails/apache/root/usr/local/etc/apache24/modules.d/001_php.conf"
<IfModule dir_module>
    DirectoryIndex index.php index.html
    <FilesMatch "\.php$">
        SetHandler application/x-httpd-php
    </FilesMatch>
    <FilesMatch "\.phps$">
        SetHandler application/x-httpd-php-source
    </FilesMatch>
</IfModule>
#+end_src
 
Yes thats exactly what you need to do

That outputs this code

Code:
<IfModule dir_module>
    DirectoryIndex index.php index.html
    <FilesMatch "\.php$">
        SetHandler application/x-httpd-php
    </FilesMatch>
    <FilesMatch "\.phps$">
        SetHandler application/x-httpd-php-source
    </FilesMatch>
</IfModule>

you should also name blocks like this

Code:
#+NAME: apache-php
#+begin_src conf :tangle "/zroot/iocage/jails/apache/root/usr/local/etc/apache24/modules.d/001_php.conf"
<IfModule dir_module>
    DirectoryIndex index.php index.html
    <FilesMatch "\.php$">
        SetHandler application/x-httpd-php
    </FilesMatch>
    <FilesMatch "\.phps$">
        SetHandler application/x-httpd-php-source
    </FilesMatch>
</IfModule>
#+end_src
 
Here's an org file that creates all the config file

notice the #+PROPERTY section

Code:
#+PROPERTY: header-args :mkdirp yes :noweb yes

the mkdir yes create directories if they dont exist

Its best to run the iocage code in the terminal

#+STARTUP: content
#+PROPERTY: header-args :mkdirp yes :noweb yes
* apache set up
** tangle
*** tangle document

C-c C-v t

*** tangle only one code block

Put your cursor on a code block and run

C-u C-c C-v t

** doas set up

doas can be set so you arent prompted for you password
when you tangle files which emacs to directories that require root access

Install doas

#+begin_src sh
sudo pkg install doas
#+end_src

Edit the doas.conf using your text editor

#+begin_src sh
sudo vim /usr/local/etc/doas.conf
#+end_src

#+begin_src conf
#=========================================================================#
# doas.conf
#=========================================================================#

#=========================================================================#
# permit user
#=========================================================================#

permit keepenv setenv { PATH } balanga


#=========================================================================#
# allow root to switch to our user
#=========================================================================#

permit nopass keepenv setenv { PATH } root as balanga


#=========================================================================#
# permit user - no pass
#=========================================================================#

permit nopass keepenv setenv { PATH } balanga


#=========================================================================#
# root as root
#=========================================================================#

permit nopass keepenv root as root
#+end_src

** apache-php

You will need root privileges to tangle files to some directories

Notice the /doas:: in the tangle code

Also notice the tangle mode which sets the permisions on the file

For example set the permissions to 755

#+begin_example
:tangle-mode (identity #o755)
#+end_example

#+NAME: apache-php
#+begin_src conf :tangle "/doas::/zroot/iocage/jails/apache/root/usr/local/etc/apache24/modules.d/001_php.conf" :tangle-mode (identity #o755)
<IfModule dir_module>
DirectoryIndex index.php index.html
<FilesMatch "\.php$">
SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch "\.phps$">
SetHandler application/x-httpd-php-source
</FilesMatch>
</IfModule>
#+end_src

** apache-json

#+NAME: apache-json
#+begin_src json tangle: "apache.json"
{
"pkgs": [
$(for pkg in $pkgs; do echo " \"$pkg\","; done | sed '$s/,$//')
]
}
#+end_src

** index-php

You will need root privileges to tangle files to some directories

Notice the /doas:: in the tangle code

Also notice the tangle mode which sets the permisions on the file

For example set the permissions to 755

#+begin_example
:tangle-mode (identity #o755)
#+end_example

#+NAME: php-info
#+begin_src php :tangle "/doas::/zroot/iocage/jails/apache/root/usr/local/www/apache24/data/index.php" :tangle-mode (identity #o755)
<?php phpinfo(); ?>
#+end_src
 
No problem

Takes a while for all the neurons to fire
and the light bulb to come on

The advantage is you can have org files you tangle
that create all the files for a project in one go

It is possible to run shell commands in org file ( like iocage )
But its better to just run them in the terminal to be honest
 
#+NAME: php-info
#+begin_src php :tangle "/doas::/zroot/iocage/jails/apache/root/usr/local/www/apache24/data/index.php" :tangle-mode (identity #o755)
<?php phpinfo(); ?>
#+end_src

This location cannot be specified until the jail, $JAIL, is created.

The actual address would be

/zroot/iocage/jails/$JAIL/root/usr/local/www/apache24/data/index.php
 
OK here is a new version

With the iocage code in the org file that you can run

Code:
#+STARTUP: content
#+PROPERTY: header-args :mkdirp yes :noweb yes :results silent
* apache set up
** set up
*** doas set up

doas can be set so you arent prompted for you password
when you tangle files which emacs to directories that require root access

Install doas

#+begin_src sh
sudo pkg install doas
#+end_src

Edit the doas.conf using your text editor

#+begin_src sh
sudo vim /usr/local/etc/doas.conf
#+end_src

#+begin_src conf
#=========================================================================#
# doas.conf
#=========================================================================#

#=========================================================================#
# permit user
#=========================================================================#

permit keepenv setenv { PATH } balanga


#=========================================================================#
# allow root to switch to our user
#=========================================================================#

permit nopass keepenv setenv { PATH } root as balanga


#=========================================================================#
# permit user - no pass
#=========================================================================#

permit nopass keepenv setenv { PATH } balanga


#=========================================================================#
# root as root
#=========================================================================#

permit nopass keepenv root as root
#+end_src

*** Emacs babel

Add the following code to your emacs init.el

#+begin_example
~/.config/emacs/init.el
#+end_example

This will stop emacs asking you if you want to evaluate a code block every time

#+begin_src emacs-lisp
(setq org-confirm-babel-evaluate nil)
#+end_src

You can execute a code block to run the code by putting your cursor on the code block and pressing

#+begin_example
C-c C-c
#+end_example

** apache-json

Put your cursor on a code block and run

C-u C-c C-v t

To tangle the file

#+NAME: apache-json
#+begin_src json :tangle apache.json
{
  "pkgs": [
    "apache24",
    "mod_php84"
  ]
}
#+end_src

** iocage create and start

Put your cursor on the code blocks and run the following short cut to execute the code

#+begin_example
C-c C-c
#+end_example

Iocage create

#+NAME: iocage-create
#+begin_src sh
doas iocage create -r latest -p ./apache.json -n apache vnet=on dhcp=on
#+end_src

Iocage start

#+NAME: iocage-start
#+begin_src sh
doas iocage start apache
#+end_src

** apache-php

You will need root privileges to tangle files to some directories

Notice the /doas:: in the tangle code

Also notice the tangle mode which sets the permisions on the file

For example set the permissions to 755

#+begin_example
:tangle-mode (identity #o755)
#+end_example

Put your cursor on a code block and run

C-u C-c C-v t

To tangle the file

#+NAME: apache-php
#+begin_src conf :tangle "/doas::/zroot/iocage/jails/apache/root/usr/local/etc/apache24/modules.d/001_php.conf" :tangle-mode (identity #o755)
<IfModule dir_module>
DirectoryIndex index.php index.html
<FilesMatch "\.php$">
SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch "\.phps$">
SetHandler application/x-httpd-php-source
</FilesMatch>
</IfModule>
#+end_src

** index-php

You will need root privileges to tangle files to some directories

Notice the /doas:: in the tangle code

Also notice the tangle mode which sets the permisions on the file

For example set the permissions to 755

#+begin_example
:tangle-mode (identity #o755)
#+end_example

Put your cursor on a code block and run

C-u C-c C-v t

To tangle the file

#+NAME: php-info
#+begin_src php :tangle "/doas::/zroot/iocage/jails/apache/root/usr/local/www/apache24/data/index.php" :tangle-mode (identity #o755)
<?php phpinfo(); ?>
#+end_src

** Iocage sysrc and service

Put your cursor on the code blocks and run the following short cut to execute the code

#+begin_example
C-c C-c
#+end_example

Iocage sysrc

#+NAME: iocage-sysrc
#+begin_src sh
doas iocage exec apache sysrc apache24_enable=YES
#+end_src

Iocage service

#+NAME: iocage-service
#+begin_src sh
doas iocage exec apache service apache24 start
#+end_src
 
Back
Top