1

I'm trying to write a bash script which will leverage Augeas to create a conf file in /etc/httpd/conf.d/ and also set up the necessary nodes for the vhost. For some reason I am unable to set the DocumentRoot node. When I try via the interactive shell, I can create it just fine:

[root@panel conf.d]# augtool 
augtool> print /files/etc/httpd/conf.d/hey.com.conf/
/files/etc/httpd/conf.d/hey.com.conf
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost/arg = "*:80"
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost/directive = "DocumentRoot"
augtool> set /files/etc/httpd/conf.d/hey.com.conf/VirtualHost/*[self::directive='DocumentRoot']/arg "/var/www/vhosts/hey.com"
augtool> print /files/etc/httpd/conf.d/hey.com.conf/
/files/etc/httpd/conf.d/hey.com.conf
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost/arg = "*:80"
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost/directive = "DocumentRoot"
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost/directive/arg = "/var/www/vhosts/hey.com"
augtool> save
Saved 1 file(s)
augtool> quit
[root@panel conf.d]# cat hey.com.conf 
<VirtualHost *:80>
DocumentRoot /var/www/vhosts/hey.com
</VirtualHost>

However, my script is unable to create it:

(I put the script in my path for easy execution) Usage example: # aug hey.com


#!/bin/bash

# Set up conf file
name=$1
path="/files/etc/httpd/conf.d/"
conf="$name.conf"
echo ${path}${conf}
docroot="/var/www/vhosts/$name"

# Create Vhost
echo "DocRoot = $docroot"
mkdir $docroot
echo $path$conf
augtool -s set ${path}${conf}/VirtualHost/arg "*:80"
augtool -s set ${path}${conf}/VirtualHost/directive "DocumentRoot"

echo $docroot
echo "Nodes Present"

augtool print ${path}${conf}
#augtool -s set ${path}${conf}/VirtualHost/*[self::directive='DocumentRoot']/arg "$docroot"
augtool -s set /files/etc/httpd/conf.d/hey.com.conf/VirtualHost/*[self::directive=DocumentRoot]/arg /var/www/vhosts/hey.com
augtool print ${path}${conf}
augtool match /augeas//error

As far as I can tell it is trying to do the same command but it fails to write the argument to DocumentRoot. Any help to point me in the right direction or rework my thinking in general would be greatly appreciated.

This is on a CentOS 6.6 box.

Update:

The script creates the file on the fly, so it doesn't exist beforehand. I delete it in between runs of the script. Sample output is as follows:

[root@panel ~]# aug hey.com
/files/etc/httpd/conf.d/hey.com.conf
DocRoot = /var/www/vhosts/hey.com
/files/etc/httpd/conf.d/hey.com.conf
Saved 1 file(s)
Saved 1 file(s)
/var/www/vhosts/hey.com
Nodes Present
/files/etc/httpd/conf.d/hey.com.conf
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost/arg = "*:80"
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost/directive = "DocumentRoot"
/files/etc/httpd/conf.d/hey.com.conf
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost/arg = "*:80"
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost/directive = "DocumentRoot"
/augeas/files/etc/sysconfig/iptables.save/error = parse_failed
4

1 回答 1

1

明白了,我没有正确引用。在 augtool 提示符下工作的相同命令在我的 bash 脚本中不起作用。在脚本中,我能够设置指令本身,唯一不同的是参数的使用 * 和 [] 事实证明 bash 正在处理它们而不是 augtool。我引用了它:

augtool -s set '/files/etc/httpd/conf.d/hey.com.conf/VirtualHost/*[self::directive="DocumentRoot"]/arg "/var/www/vhosts/hey.com"'

现在我可以很好地构建节点了:

[root@panel ~]# cat /etc/httpd/conf.d/hey.com.conf 
<VirtualHost *:80>
DocumentRoot /var/www/vhosts/hey.com
</VirtualHost>
于 2015-02-02T19:10:22.600 回答