使用厨师食谱识别PostgreSQL版本



是否可以在 chef 的食谱中识别数据库服务器上运行的 PostgreSQL 版本。

现在我能够使用awk命令识别它

例:

[postgres@hostname]$ ps -aux  | grep postgres
psharm89 39532  0.0  0.0 112644   960 pts/2    S+   12:31   0:00 grep -- color=auto postgres
postgres 56958  0.0  0.9 2345396 77444 ?       S    May21   1:10 /opt/postgres/9.5/bin/postgres -D /opt/pgdata/9.5/data
postgres 56959  0.0  0.0 166416  1836 ?        Ss   May21   0:57 postgres: logger process
postgres 56961  0.0  2.1 2347908 173768 ?      Ss   May21   0:45 postgres: checkpointer process
postgres 56962  0.0  0.2 2345412 21096 ?       Ss   May21   0:14 postgres: writer process
postgres 56963  0.0  0.2 2345352 18284 ?       Ss   May21   0:25 postgres: wal writer process
postgres 56964  0.0  0.0 2345788 2820 ?        Ss   May21   1:14 postgres: autovacuum launcher process
postgres 56965  0.0  0.0 168668  2156 ?        Ss   May21   2:22 postgres: stats collector process
[postgres@hostname]$ PGHOME=$(ps -aux  | grep postgres | grep -- -D |grep -v grep |awk '{NF=NF-2;print $NF}'|awk -F"/bin" '{print $1}')
[postgres@hostname]$ PGVER=$(/bin/basename $PGHOME)
[postgres@hostname]$ echo $PGVER
9.5

但是,同样,我需要在 Chef 中的食谱中查找,以便我可以创建变量PGVER=9.5并在我的 PostgreSQL 数据库配置配方中使用它。

这通常不是您使用 Chef 的方式。Chef将是强制执行正在使用哪个版本的Postgres的东西。

但如果必须,你可以使用这样的东西:

pgver = shell_out!('ps -aux').stdout[%r{postgres/(9.d+)/bin}, 1]

这主要只是将字符串解析移动到 Ruby 而不是 Bash 中,但基本思想相同。

最新更新