1

While runnig this chef block:-

bash 'Do some sed' do
    user node['aem_dispatcher_cookbook']['owner']['user']
    group node['aem_dispatcher_cookbook']['owner']['group']
    cwd '/ebiz'
    code <<-EOH
    source /etc/profile.d/disp_profile.sh
      sed  -i -e 's/umask 007/umask 022/g' #{node['aem_dispatcher_cookbook']['user']['home']}/.profile
      sed  -i -e 's/umask 007/umask 022/g' #{node['aem_dispatcher_cookbook']['user']['home']}/.wsenv
      sed  -i -e 's/AUTHOR_HOST/'$AUTHOR_HOST'/g' #{node['aem_dispatcher_cookbook']['owner']['home']}/conf.d/author_farm.any
      sed  -i -e 's/APORT/'$APORT'/g' #{node['aem_dispatcher_cookbook']['owner']['home']}/conf.d/author_farm.any
      sed  -i -e 's/URL1/'$URL'/g' #{node['aem_dispatcher_cookbook']['owner']['home']}/conf.d/author_farm.any
      sed  -i -e 's/PUBLISH_HOST/'$PUBLISH_HOST'/g' #{node['aem_dispatcher_cookbook']['owner']['home']}/conf.d/publish_farm.any
      sed  -i -e 's/PPORT/'$PPORT'/g' #{node['aem_dispatcher_cookbook']['owner']['home']}/conf.d/publish_farm.any  
      sed  -i -e 's/P_HOST_ADDY/'$P_HOST_ADDY'/g' #{node['aem_dispatcher_cookbook']['owner']['home']}/conf.d/publish_farm.any
    EOH
  end

Getting error:-

STDERR: /tmp/chef-script20200518-26241-1f7ynpy: line 2: sed  -i -e s/umask 007/umask 022/g /home/aemadmin/.profile: No such file or directory
/tmp/chef-script20200518-26241-1f7ynpy: line 3: sed  -i -e s/umask 007/umask 022/g /home/aemadmin/.wsenv: No such file or directory
/tmp/chef-script20200518-26241-1f7ynpy: line 4: sed  -i -e s/AUTHOR_HOST//g /ebiz/apache/conf.d/author_farm.any: No such file or directory
/tmp/chef-script20200518-26241-1f7ynpy: line 5: sed  -i -e s/APORT/8080/g /ebiz/apache/conf.d/author_farm.any: No such file or directory
/tmp/chef-script20200518-26241-1f7ynpy: line 6: sed  -i -e s/URL1/dev.uhg/g /ebiz/apache/conf.d/author_farm.any: No such file or directory
/tmp/chef-script20200518-26241-1f7ynpy: line 7: sed  -i -e s/PUBLISH_HOST//g /ebiz/apache/conf.d/publish_farm.any: No such file or directory
/tmp/chef-script20200518-26241-1f7ynpy: line 8: sed  -i -e s/PPORT/8080/g /ebiz/apache/conf.d/publish_farm.any  : No such file or directory
/tmp/chef-script20200518-26241-1f7ynpy: line 9: sed  -i -e s/P_HOST_ADDY//g /ebiz/apache/conf.d/publish_farm.any: No such file or directory
---- End output of "bash"  "/tmp/chef-script20200518-26241-1f7ynpy" ----
Ran "bash"  "/tmp/chef-script20200518-26241-1f7ynpy" returned 127

But these files are available in same location. Please help solve this issue

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 1
    The error messages indicate that *each entire line* is being treated as a single word in shell (not a string to parse and separate into words). It's been too many years since I worked with Chef to have a lot of specific insights -- but any chance you might have non-breaking spaces instead of regular ASCII ones in your code? That's certainly one way to get this bug. – Charles Duffy May 18 '20 at 15:20
  • That is to say, instead of trying to run `sed` as a command and pass `-i` as an argument to it, the error message says it's trying to find a command with the filename `sed -i -e s/umask 007/umask 022/g /home/aemadmin/.profile`, **with the spaces and slashes as part of the command's name**. – Charles Duffy May 18 '20 at 15:21
  • ...it's not a problem with `sed` being unable to find the files, which is why whether the files exist is moot. `sed` isn't even running, because the shell doesn't see `sed` as the name of a command to run, but the first three characters of a much longer command name... which is why I suspect you have non-breaking spaces in your file instead of regular ones. – Charles Duffy May 18 '20 at 15:21

1 Answers1

1

I don't know exactly, why your sed command fails, but Charles Duffy has a point.

I would like to introduce a more Chef way of managing configuration files.

There is a template resource, that creates a file from an .erb template adding some variables:

# template (templates/default/file.conf.erb)
key1 = value1
key2 = <%= @value2 %>

# recipe
template '/etc/conf/file.conf' do
  source 'file.conf.erb'
  variables(value2: node['value2'])  # this will set @value2 in template to node['value2']
end

If you can use template, then you should. But if you can't, for example this file can be also changed by other software, then you can use Chef::Util::FileEdit library:

How can I change a file with Chef?

In your case you will probably need:

ruby_block "Do some regex replacement" do
  block do
    fe = Chef::Util::FileEdit.new("#{node['aem_dispatcher_cookbook']['user']['home']}/.profile")
    fe.search_file_replace('umask 007', 'umask 022')
    fe.write_file

    fe = Chef::Util::FileEdit.new("#{node['aem_dispatcher_cookbook']['owner']['home']}/conf.d/author_farm.any")
    fe.search_file_replace('AUTHOR_HOST', ENV['AUTHOR_HOST'])
    fe.write_file
    [.. and similarly with other files ..]
  end
end
Draco Ater
  • 20,820
  • 8
  • 62
  • 86