I have a script name first.sh and in that I have called few variables like below:
#!/bin/bash
index1_0="a_b_c"
index2_0="b_c_d"
index3_0="c_d_e"
The script path of first.sh is goal/first.sh.I'm trying to use above variables called in first.sh in another second.erb file like below:
% H = Hash["a" => "#index1_0",
% "b" => "#index2_0",
% "d" => "#index3_0",]
I need to use the variables declared in first.sh script file in second.erb file but not getting the exact output. Can someone help here?
You need
<%= ENV['index1_0'] %>
to access the value of an environment variable that you need to export from first.sh like
export index1_0="a_b_c"
and source it
source first.sh
Related
def path = ....
def files = []
sh "for file in $path/*.json; do files.add(file); done"
echo ${files}
Error I get in jenkins: /jenkins/workspace/....."syntax error near unexpected token 'file'
Can someone help me as to how can I add file in files? I tried looking for answers but couldn't find anything useful which solved my scenario.
I want to add the file variable inside Arraylist variable files so that I can fire curl command for each file in my Jenkins pipeline.
Also needed to know is there some way I can test the script before deploying it on any environment?
If you want to get a List JSON files in a directory as a List you can use the following code.
path = "/home/your/path"
dir(path) {
def files = findFiles(glob: '**/*.json')
println files.size()
println files[0].name
}
I have a .properties files as below:
user:abcd
pwd:xyz
system:test
Next, I have a ruby script with Watir for browser automation. In this script, I have statements like
browser.text_field(:id => 'identifierId').set "#{user}:variable to be replaced by its value from .properties file".
Similarly, other values need to be replaced for "pwd" and "system".
I tried the solution per below posts:
Replace properties in one file from those in another in Ruby
However, "set" command is setting whatever has been paased as arguments to it instead of replacing the variable with its value.
Please help.
You have to read the information out of the file.
Most Watir users leverage yaml files for this.
config/properties.yml:
user: abcd
pwd: xyz
system: test
Then read the yaml file & parse your data:
properties = YAML.safe_load(IO.read('config/properties.yml'))
text_field = browser.text_field(id: 'identifierId')
text_field.set properties['user']
Alternately you can take a look at Cheezy's Fig Newton gem, which is designed to work with his Page Object gem
Is there some way to access a variable defined in a prerequisite? e.g.
task :new_file do
filename = 'foobar.txt' # in reality I ask the user for the filename
File.write(filename, 'Some glorious content')
end
task :new_file! => [:new_file] do
exec "vim #{filename.inspect}"
end
I'd like rake new_file! to simply be a shorthand for rake new_file along with launching vim for whatever file I created in the new_file task.
All I can think of is populating a global variable FILENAME in :new_file and using it in new_file! and then clearing it, but if there is a "more Rake" way to do it, I'd like to know.
One way would be to define the variable outside the tasks like this:
filename = 'default.name'
task :new_file do
filename = 'foobar.txt' # in reality I ask the user for the filename
File.write(filename, 'Some glorious content')
end
task :new_file! => [:new_file] do
# filename will be visible here too, and its value was set in new_file
exec "vim #{filename.inspect}"
end
I'm trying to work out the best way to set some environment variables with puppet.
I could use exec and just do export VAR=blah. However, that would only last for the current session. I also thought about just adding it onto the end of a file such as bashrc. However then I don't think there is a reliable method to check if it is all ready there; so it would end up getting added with every run of puppet.
I would take a look at this related question.
*.sh scripts in /etc/profile.d are read at user-login time (as the post says, at the same time /etc/profile is sourced)
Variables export-ed in any script placed in /etc/profile.d will therefore be available to your users.
You can then use a file resource to ensure this action is idempotent. For example:
file { "/etc/profile.d/my_test.sh":
content => 'export MYVAR="123"'
}
Or an alternate means to an indempotent result:
Example
if [[ ! grep PINTO_HOME /root/.bashrc | wc -l > 0 ]] ; then
echo "export PINTO_HOME=/opt/local/pinto" >> /root/.bashrc ;
fi
This option permits this environmental variable to be set when the presence of the
pinto application makes it warrented rather than having to compose a user's
.bash_profile regardless of what applications may wind up on the box.
If you add it to your bashrc you can check that it's in the ENV hash by doing
ENV[VAR]
Which will return => "blah"
If you take a look at Github's Boxen they source a script (/opt/boxen/env.sh) from ~/.profile. This script runs a bunch of stuff including:
for f in $BOXEN_HOME/env.d/*.sh ; do
if [ -f $f ] ; then
source $f
fi
done
These scripts, in turn, set environment variables for their respective modules.
If you want the variables to affect all users /etc/profile.d is the way to go.
However, if you want them for a specific user, something like .bashrc makes more sense.
In response to "I don't think there is a reliable method to check if it is all ready there; so it would end up getting added with every run of puppet," there is now a file_line resource available from the puppetlabs stdlib module:
"Ensures that a given line is contained within a file. The implementation matches the full line, including whitespace at the beginning and end. If the line is not contained in the given file, Puppet appends the line to the end of the file to ensure the desired state. Multiple resources can be declared to manage multiple lines in the same file."
Example:
file_line { 'sudo_rule':
path => '/etc/sudoers',
line => '%sudo ALL=(ALL) ALL',
}
file_line { 'sudo_rule_nopw':
path => '/etc/sudoers',
line => '%sudonopw ALL=(ALL) NOPASSWD: ALL',
}
i am trying to install java using chef-solo. The problem is to set the JAVA_HOME and PATH variables in /etc/profile file. I tried using 'file' resource provided by chef. here is some of my code:
java_home = "export JAVA_HOME=/usr/lib/java/jdk1.7.0_05"
path = "export PATH=$PATH:/usr/lib/java/jdk1.7.0_05/bin"
execute "make_dir" do
cwd "/usr/lib/"
user "root"
command "mkdir java"
end
execute "copy" do
cwd "/usr/lib/java"
user "root"
command "cp -r /home/user/Downloads/jdk1* /usr/lib/java"
end
file "/etc/profile" do
owner "root"
group "root"
action :touch
content JAVA_HOME
content PATH
end
but the problem is content command overrides all the content of file, is there any way to UPDATE the file while using chef-solo resources. Thanks!
UPDATE: i have found some code from chef-recipe, but i am not sure what it does exactly, the code is..
ruby_block "set-env-java-home" do
block do
ENV["JAVA_HOME"] = java_home
end
end
Does it set JAVA_HOME variable for only that instance or permanently? Can anybody help?
Use Chef::Util::FileEdit. Below is an example how I modify .bashrc. The idea here is that I just add:
# Include user specific settings
if [ -f ~/.bashrc_user ]; then . ~/.bashrc_user; fi
to the end of default .bashrcand all other modifications take place in .bashrc_user that is part of my cookbook.
cookbook_file "#{ENV['HOME']}/.bashrc_user" do
user "user"
group "user"
mode 00644
end
ruby_block "include-bashrc-user" do
block do
file = Chef::Util::FileEdit.new("#{ENV['HOME']}/.bashrc")
file.insert_line_if_no_match(
"# Include user specific settings",
"\n# Include user specific settings\nif [ -f ~/.bashrc_user ]; then . ~/.bashrc_user; fi"
)
file.write_file
end
end
As #user272735 's suggestion, a clean way to modify .bashrc is:
write all your modification in a .bashrc_local file,
include your specific settings to .bashrc.
For step 1, we can use template resource.
For step 2, I prefer use line cookbook.
Sample codes as below,
templates/bashrc_local.erb
export JAVA_HOME=/usr/lib/java/jdk1.7.0_05
export PATH=$PATH:/usr/lib/java/jdk1.7.0_05/bin
recipes/default.rb
# add bashrc_local
template "#{ENV['HOME']}/.bashrc_local" do
source 'bashrc_local.erb'
mode 00644
end
# update bashrc
append_if_no_line "add bashrc_local" do
path "#{ENV['HOME']}/.bashrc"
line "if [ -f ~/.bashrc_local ]; then . ~/.bashrc_local; fi"
end
You can fix this by either using a template resource instead of a file resource, or if you are just appending those two variables, try doing this:
content "#{java_home}\n#{path}"
The second content line is overriding the first, as you have already discovered. You also don't need the action :touch.