Template value from script output

If I have a template file, that I need to get a value only when it’s changed, how do I set that up in the recipe?

For example:

script "get_appserverval"
interpreter "bash"
cwd "/tmp"
code <<-EOH
SERVERURL=$1
SERVERPORT=$2
USERNAME=$3
PASSWORD=$4

URL=wget --user-agent="Mozilla/5.0 (Windows NT 6.1; U; en; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6" --keep-session-cookies --save-cookies=mycookie ${SERVERURL}:${SERVERPORT} -O - |grep "action/Login" | awk -Faction= '{print $2}' |awk '{print $1}' |sed "s/:80/:${SERVERPORT}/g"

BASEURL=wget --user-agent="Mozilla/5.0 (Windows NT 6.1; U; en; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6" --keep-session-cookies --load-cookies=mycookie --save-cookies=mycookie2 ${URL} --post-data=UserName=${USERNAME}\&Password=${PASSWORD}\&F_LOCALE=en_us\&submitButton=\&page=submit -O - | grep "var gBaseActionURL" | sed 's/\r//g' | sed "s/:80/:${SERVERPORT}/g" | awk -F= '{print $2}' |sed 's/ //g' | sed 's/\"//g' | sed 's/;//g'

APPSERVERVAL=wget --user-agent="Mozilla/5.0 (Windows NT 6.1; U; en; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6" --keep-session-cookies --load-cookies=mycookie2 --save-cookies=mycookie3 ${BASEURL}Report.Open?F_REPORTID=11 -O - | grep APPSERVERVALUE |awk -FVALUE= '{print $2}' |sed 's/>//g' | sed 's/\"//g'

echo $APPSERVERVAL
EOH
end

So I have a few problems:

Need to pass some values to the script.
Need to get a value from the script, to pass to a template resource.
How do I run this script only if the affected template changes, or it’s a new deploy?

This is a Java WebApp. I’m using application to deploy the war file. I modified the tomcat cookbook to take a parameter for Path to tell the deployment path, and the app[id] is set as the Context name.
I also modified the application cookbook to allow me to override the db setup parameters, but I would like to know if there’s a better way to do it… I’ll post a diff shortly.

So right now I have a role, which has a run list of application, and appname. This code would be called in the appname recipe, which copies over the supporting template files and restarts tomcat when done (each file has a restart tomcat notification, which works fine). Should I use the apps data bag, or use a different one?

Thanks.

-Matt

On Friday, June 24, 2011 at 10:50 AM, Matthew Drobnak wrote:

If I have a template file, that I need to get a value only when it’s changed, how do I set that up in the recipe?

For example:

script “get_appserverval”
interpreter “bash”
cwd “/tmp”
code <<-EOH
SERVERURL=$1
SERVERPORT=$2
USERNAME=$3
PASSWORD=$4

URL=wget --user-agent="Mozilla/5.0 (Windows NT 6.1; U; en; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6" --keep-session-cookies --save-cookies=mycookie ${SERVERURL}:${SERVERPORT} -O - |grep "action/Login" | awk -Faction= '{print $2}' |awk '{print $1}' |sed "s/:80/:${SERVERPORT}/g"

BASEURL=wget --user-agent="Mozilla/5.0 (Windows NT 6.1; U; en; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6" --keep-session-cookies --load-cookies=mycookie --save-cookies=mycookie2 ${URL} --post-data=UserName=${USERNAME}\&Password=${PASSWORD}\&F_LOCALE=en_us\&submitButton=\&page=submit -O - | grep "var gBaseActionURL" | sed 's/\r//g' | sed "s/:80/:${SERVERPORT}/g" | awk -F= '{print $2}' |sed 's/ //g' | sed 's/\"//g' | sed 's/;//g'

APPSERVERVAL=wget --user-agent="Mozilla/5.0 (Windows NT 6.1; U; en; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6" --keep-session-cookies --load-cookies=mycookie2 --save-cookies=mycookie3 ${BASEURL}Report.Open?F_REPORTID=11 -O - | grep APPSERVERVALUE |awk -FVALUE= '{print $2}' |sed 's/>//g' | sed 's/\"//g'

echo $APPSERVERVAL
EOH
end

So I have a few problems:

Need to pass some values to the script.
You can "interpolate" values into a heredoc in ruby via the normal syntax:

mystring=<<-EOH
this is a variable: #{my_variable}
EOH

Need to get a value from the script, to pass to a template resource.
This is contrary to Chef's design and philosophy. You should be declaring what the state of the system should be from the top down (like a boss, if you will ;). You should see if you can design your system not to require this. Could you summarize why you need this?

How do I run this script only if the affected template changes, or it’s a new deploy?

template "path/to/file" do

other stuff

notifies(:run, "script[get_appserverval")
end

This is a Java WebApp. I’m using application to deploy the war file. I modified the tomcat cookbook to take a parameter for Path to tell the deployment path, and the app[id] is set as the Context name.
I also modified the application cookbook to allow me to override the db setup parameters, but I would like to know if there’s a better way to do it… I’ll post a diff shortly.

So right now I have a role, which has a run list of application, and appname. This code would be called in the appname recipe, which copies over the supporting template files and restarts tomcat when done (each file has a restart tomcat notification, which works fine). Should I use the apps data bag, or use a different one?

Thanks.

-Matt

--
Dan DeLeo

We're dealing with a closed-source app here, trying to get it to produce a report. When you do, you need to pass a "security" parameter which is this APPSERVERVALUE.
Each value is different depending on the IP of the server making the request.
This script "Logs in", goes to the right page to run a report, and scrapes the value out.

This can then be put in the appname.properties file which is on the node.

-Matt

-----Original Message-----
From: Daniel DeLeo [mailto:ddeleo@kallistec.com] On Behalf Of Daniel DeLeo
Sent: Friday, June 24, 2011 2:13 PM
To: chef@lists.opscode.com
Subject: [chef] Re: Template value from script output

On Friday, June 24, 2011 at 10:50 AM, Matthew Drobnak wrote:

If I have a template file, that I need to get a value only when it’s changed, how do I set that up in the recipe?

For example:

script “get_appserverval”
interpreter “bash”
cwd “/tmp”
code <<-EOH
SERVERURL=$1
SERVERPORT=$2
USERNAME=$3
PASSWORD=$4

URL=wget --user-agent="Mozilla/5.0 (Windows NT 6.1; U; en; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6" --keep-session-cookies --save-cookies=mycookie ${SERVERURL}:${SERVERPORT} -O - |grep "action/Login" | awk -Faction= '{print $2}' |awk '{print $1}' |sed "s/:80/:${SERVERPORT}/g"

BASEURL=wget --user-agent="Mozilla/5.0 (Windows NT 6.1; U; en; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6" --keep-session-cookies --load-cookies=mycookie --save-cookies=mycookie2 ${URL} --post-data=UserName=${USERNAME}\&Password=${PASSWORD}\&F_LOCALE=en_us\&submitButton=\&page=submit -O - | grep "var gBaseActionURL" | sed 's/\r//g' | sed "s/:80/:${SERVERPORT}/g" | awk -F= '{print $2}' |sed 's/ //g' | sed 's/\"//g' | sed 's/;//g'

APPSERVERVAL=wget --user-agent="Mozilla/5.0 (Windows NT 6.1; U; en; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6" --keep-session-cookies --load-cookies=mycookie2 --save-cookies=mycookie3 ${BASEURL}Report.Open?F_REPORTID=11 -O - | grep APPSERVERVALUE |awk -FVALUE= '{print $2}' |sed 's/>//g' | sed 's/\"//g'

echo $APPSERVERVAL
EOH
end

So I have a few problems:

Need to pass some values to the script.
You can "interpolate" values into a heredoc in ruby via the normal syntax:

mystring=<<-EOH
this is a variable: #{my_variable}
EOH

Need to get a value from the script, to pass to a template resource.
This is contrary to Chef's design and philosophy. You should be declaring what the state of the system should be from the top down (like a boss, if you will ;). You should see if you can design your system not to require this. Could you summarize why you need this?

How do I run this script only if the affected template changes, or it’s a new deploy?

template "path/to/file" do

other stuff

notifies(:run, "script[get_appserverval")
end

This is a Java WebApp. I’m using application to deploy the war file. I modified the tomcat cookbook to take a parameter for Path to tell the deployment path, and the app[id] is set as the Context name.
I also modified the application cookbook to allow me to override the db setup parameters, but I would like to know if there’s a better way to do it… I’ll post a diff shortly.

So right now I have a role, which has a run list of application, and appname. This code would be called in the appname recipe, which copies over the supporting template files and restarts tomcat when done (each file has a restart tomcat notification, which works fine). Should I use the apps data bag, or use a different one?

Thanks.

-Matt

--
Dan DeLeo