ruby - Is there a better way of Chef recipe coding on the below code? -


i trying base backup of postgresql. code simple bash run in ruby. basebackup , tar /tmp/(dir)

 file "/tmp/basebackup.sh"  user "postgres"  mode 0755  content <<-eoh      su - postgres      export path=$path:/usr/lib/postgresql/9.2/bin      su -c "/usr/lib/postgresql/9.2/bin/initdb -d /tmp/pg_data/ -s --no-locale --encoding=utf8"  - postgres      su -c "/usr/lib/postgresql/9.2/bin/pg_ctl -d /tmp/pg_data -l logfile start" - postgres      su -c "/usr/lib/postgresql/9.2/bin/psql -c "select pg_start_backup('initial backup sr')" template1" - postgres      tar -cvf pg_base_backup.tar /tmp/pg_data      su -c "/usr/lib/postgresql/9.2/bin/psql -c "select pg_stop_backup()" template1" - postgres      exit 0  eoh  end   execute "/tmp/basebackup.sh"        ignore_failure true       action :run  end 

is there better way of doing this?

ruby supports __end__ used mark end of runnable script. after isn't seen running program, can accessed using data file handle, can read, normal file. try bit of code see work:

file.open('trashme.txt', 'w') |content|   content << data.read end  __end__ su - postgres export path=$path:/usr/lib/postgresql/9.2/bin su -c "/usr/lib/postgresql/9.2/bin/initdb -d /tmp/pg_data/ -s --no-locale --encoding=utf8"  - postgres su -c "/usr/lib/postgresql/9.2/bin/pg_ctl -d /tmp/pg_data -l logfile start" - postgres su -c "/usr/lib/postgresql/9.2/bin/psql -c "select pg_start_backup('initial backup sr')" template1" - postgres tar -cvf pg_base_backup.tar /tmp/pg_data su -c "/usr/lib/postgresql/9.2/bin/psql -c "select pg_stop_backup()" template1" - postgres exit 0 

i'd use feature move shell script outside normal ruby/chef script maintenance , clarity.


Comments