# Run as root or as vagrant user

**URL:** <https://discourse.chef.io/t/run-as-root-or-as-vagrant-user/3934>\
**Category:** Chef Infra (archive)\
**Created:** [May 10, 2013, 10:38pm UTC](https://discourse.chef.io/t/run-as-root-or-as-vagrant-user/3934 "2013-05-10T22:38:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Nichols](https://avatars.discourse-cdn.com/v4/letter/m/eb8c5e/32.png) [@Mark\_Nichols](https://discourse.chef.io/u/Mark_Nichols)\
**Post date:** [May 10, 2013, 10:38pm UTC](https://discourse.chef.io/t/run-as-root-or-as-vagrant-user/3934/1 "2013-05-10T22:38:20Z")

</div>

I am creating a recipe to build JBoss AS 7.1.3 from source. The recipe works until the Maven build runs out of Java heap space. On the virtual machine I’ve set MAVEN\_OPTS and JAVA\_OPTS via a .bashrc file:

MAVEN\_OPTS=’-Xms1024m -Xmx2048m -XX:MaxPermSize=500m’  
JAVA\_OPTS=’-Xms1024m -Xmx2048m -XX:MaxPermSize=512m -noverify’

…

export MAVEN\_OPTS JAVA\_OPTS …

However these environment settings are for the ‘vagrant’ user on the VM and I suspect that the chef solo provisioner runs things as ‘root’.

I tried adding an export of JAVA\_OPTS and MAVEN\_OPTS to the recipe to force the memory setting regardless of the active user:

…  
bash “mvn install -DskipTests” do  
code \<\<-EOH  
cd /home/vagrant/src/jbossas  
export MAVEN\_OPTS=’-Xms1024m -Xmx2048m -XX:MaxPermSize=500m’  
export JAVA\_OPTS=’-Xms1024m -Xmx2048m -XX:MaxPermSize=512m -noverify’  
/usr/local/maven3/bin/mvn install -DskipTests -Dcheckstyle.skip=true  
EOH  
end  
…

But that too resulted in an “out of Java heap space” error.

Obviously I am missing something. How do I ensure that my recipe has the heap space it requires?

Thanks,  
Mark Nichols

---

<div class="post-metadata">

**Author:** ![AJ\_Christensen](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/aj_christensen/32/525_2.png) [@AJ\_Christensen](https://discourse.chef.io/u/AJ_Christensen)\
**Post date:** [May 10, 2013, 10:46pm UTC](https://discourse.chef.io/t/run-as-root-or-as-vagrant-user/3934/2 "2013-05-10T22:46:17Z")

</div>

Try the 'environment' parameter to the bash script resource (it's  
actually from the Execute resource, but this inherits that)

bash 'foo' do  
environment 'MAVEN\_OPTS' =\> '-Xms1024m -Xmx2048m -XX:MaxPermSize=500m'  
end

etc.

Maybe consider structuring it a little different as well? Write a  
shell script or similar, included in your code-base which wraps your  
'mvn install', then just run that from Chef. That way you can re-run  
or developers could run it if you're hooning around by hand anyway.  
Just a thought 🙂

--AJ

On 11 May 2013 10:38, Mark H. Nichols [chef@zanshin.net](mailto:chef@zanshin.net) wrote:

> I am creating a recipe to build JBoss AS 7.1.3 from source. The recipe works  
> until the Maven build runs out of Java heap space. On the virtual machine  
> I've set MAVEN\_OPTS and JAVA\_OPTS via a .bashrc file:
> 
> MAVEN\_OPTS='-Xms1024m -Xmx2048m -XX:MaxPermSize=500m'
> 
> JAVA\_OPTS='-Xms1024m -Xmx2048m -XX:MaxPermSize=512m -noverify'
> 
> …
> 
> export MAVEN\_OPTS JAVA\_OPTS …
> 
> However these environment settings are for the 'vagrant' user on the VM and  
> I suspect that the chef solo provisioner runs things as 'root'.
> 
> I tried adding an export of JAVA\_OPTS and MAVEN\_OPTS to the recipe to force  
> the memory setting regardless of the active user:
> 
> …
> 
> bash "mvn install -DskipTests" do
> 
> code \<\<-EOH
> 
> cd /home/vagrant/src/jbossas
> 
> export MAVEN\_OPTS='-Xms1024m -Xmx2048m -XX:MaxPermSize=500m'
> 
> export JAVA\_OPTS='-Xms1024m -Xmx2048m -XX:MaxPermSize=512m -noverify'
> 
> /usr/local/maven3/bin/mvn install -DskipTests -Dcheckstyle.skip=true
> 
> EOH
> 
> end  
> …
> 
> But that too resulted in an "out of Java heap space" error.
> 
> Obviously I am missing something. How do I ensure that my recipe has the  
> heap space it requires?
> 
> Thanks,  
> Mark Nichols

---

<div class="post-metadata">

**Author:** ![Chandan\_Maheshwari](https://avatars.discourse-cdn.com/v4/letter/c/d6d6ee/32.png) [@Chandan\_Maheshwari](https://discourse.chef.io/u/Chandan_Maheshwari)\
**Post date:** [May 12, 2013, 9:41am UTC](https://discourse.chef.io/t/run-as-root-or-as-vagrant-user/3934/3 "2013-05-12T09:41:56Z")

</div>

I am also having a similar kind of problem, I have my cookbook: where in my  
recipe, I am downloading tomcat.tar (this I have placed somewhere in my  
other machine). After downloading I extract the same tomcat.tar (suppose in  
location /home/ubuntu/tomcat). Now I am simply trying to start tomcat. I  
tried the below mentioned ways, but is not able to start/stop.

1. Modified the ~/.bashrc file from my recipe to export JAVA\_HOME,  
CATALINA\_HOME,  
What I did is:

ran a bash script inside recipe to simply add the environment variables at  
the end of ~/.bashrc and then ran source ~/.bashrc

Now using "execute" resource, ran "/home/ubuntu/tomcat/bin/startup.sh" _BUT  
IT DO NOT START._

1. Modified the ~/.bashrc file from my recipe to export JAVA\_HOME,  
CATALINA\_HOME,  
What I did is:

ran a bash script inside recipe to simply add the environment variables at  
the end of ~/.bashrc and then ran source ~/.bashrc

Inside bash script resource, ran "/home/ubuntu/tomcat/bin/startup.sh" _BUT  
IT DO NOT START._

1. Wrote a shell script and kept at the same location from where I am  
downloading tomcat.tar, downloaded the run\_tomcat.sh on to the chef-client  
(using resource remote\_file) and then using the "execute" resource running  
the run\_tomcat.sh.  
Inside this run\_tomcat.sh, I am exporting the environment variables and  
then running the "/home/ubuntu/tomcat/bin/startup.sh"  
_BUT IT DO NOT START._

2. Wrote a shell script and kept at the same location from where I am  
downloading tomcat.tar, downloaded the run\_tomcat.sh on to the chef-client  
(using resource remote\_file). Set environment variables using "environment  
resource" and run script using "execute" resource.  
Inside this run\_tomcat.sh, running the "/home/ubuntu/tomcat/bin/startup.sh"  
\*BUT IT DO NOT START.

- 

For point 3 and 4 above the strange thing is that other commands inside  
run\_tomcat.sh, like extracting war, deleting some directory from the  
extracted war executes. _BUT TOMCAT DOESN'T START UP._

\*\*

On Sat, May 11, 2013 at 4:16 AM, AJ Christensen [aj@junglist.gen.nz](mailto:aj@junglist.gen.nz) wrote:

> Try the 'environment' parameter to the bash script resource (it's  
> actually from the Execute resource, but this inherits that)
> 
> bash 'foo' do  
> environment 'MAVEN\_OPTS' =\> '-Xms1024m -Xmx2048m -XX:MaxPermSize=500m'  
> end
> 
> etc.
> 
> Maybe consider structuring it a little different as well? Write a  
> shell script or similar, included in your code-base which wraps your  
> 'mvn install', then just run that from Chef. That way you can re-run  
> or developers could run it if you're hooning around by hand anyway.  
> Just a thought 🙂
> 
> --AJ
> 
> On 11 May 2013 10:38, Mark H. Nichols [chef@zanshin.net](mailto:chef@zanshin.net) wrote:
> 
> > I am creating a recipe to build JBoss AS 7.1.3 from source. The recipe  
> > works  
> > until the Maven build runs out of Java heap space. On the virtual machine  
> > I've set MAVEN\_OPTS and JAVA\_OPTS via a .bashrc file:
> > 
> > MAVEN\_OPTS='-Xms1024m -Xmx2048m -XX:MaxPermSize=500m'
> > 
> > JAVA\_OPTS='-Xms1024m -Xmx2048m -XX:MaxPermSize=512m -noverify'
> > 
> > …
> > 
> > export MAVEN\_OPTS JAVA\_OPTS …
> > 
> > However these environment settings are for the 'vagrant' user on the VM  
> > and  
> > I suspect that the chef solo provisioner runs things as 'root'.
> > 
> > I tried adding an export of JAVA\_OPTS and MAVEN\_OPTS to the recipe to  
> > force  
> > the memory setting regardless of the active user:
> > 
> > …
> > 
> > bash "mvn install -DskipTests" do
> > 
> > code \<\<-EOH
> > 
> > cd /home/vagrant/src/jbossas
> > 
> > export MAVEN\_OPTS='-Xms1024m -Xmx2048m -XX:MaxPermSize=500m'
> > 
> > export JAVA\_OPTS='-Xms1024m -Xmx2048m -XX:MaxPermSize=512m -noverify'
> > 
> > /usr/local/maven3/bin/mvn install -DskipTests -Dcheckstyle.skip=true
> > 
> > EOH
> > 
> > end  
> > …
> > 
> > But that too resulted in an "out of Java heap space" error.
> > 
> > Obviously I am missing something. How do I ensure that my recipe has the  
> > heap space it requires?
> > 
> > Thanks,  
> > Mark Nichols

--  
Regards,  
Chandan

---

<div class="post-metadata">

**Author:** ![Mark\_Nichols](https://avatars.discourse-cdn.com/v4/letter/m/eb8c5e/32.png) [@Mark\_Nichols](https://discourse.chef.io/u/Mark_Nichols)\
**Post date:** [May 14, 2013, 8:18pm UTC](https://discourse.chef.io/t/run-as-root-or-as-vagrant-user/3934/4 "2013-05-14T20:18:35Z")

</div>

On May 10, 2013, at 5:46 PM, AJ Christensen [aj@junglist.gen.nz](mailto:aj@junglist.gen.nz) wrote:

> Try the 'environment' parameter to the bash script resource (it's  
> actually from the Execute resource, but this inherits that)
> 
> bash 'foo' do  
> environment 'MAVEN\_OPTS' =\> '-Xms1024m -Xmx2048m -XX:MaxPermSize=500m'  
> end

I added the "environment" setting to my recipe, resulting in this code:

bash "mvn install -DskipTests" do  
code \<\<-EOH  
environment 'MAVEN\_OPTS' =\> '-Xms1024m -Xmx2048m -XX:MaxPermSize=500m'  
environment 'JAVA\_OPTS' =\> '-Xms1024m -Xmx2048m -XX:MaxPermSize=512m -noverify'  
cd /home/vagrant/src/jbossas  
/usr/local/maven3/bin/mvn install -DskipTests -Dcheckstyle.skip=true  
EOH  
end

However, I'm still getting a Java Heap space error.

[INFO] BUILD FAILURE  
[INFO] ------------------------------------------------------------------------  
[INFO] Total time: 2:01.567s  
[INFO] Finished at: Tue May 14 20:13:36 UTC 2013  
[INFO] Final Memory: 105M/121M  
[INFO] ------------------------------------------------------------------------  
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project jboss-as-clustering-infinispan: Fatal error compiling: Error while executing the compiler. InvocationTargetException: Java heap space -\> [Help 1]

From the output it appears that the "environment" setting is not working, as the memory is shown as 121M and not the 1024M minimum asked for.

Is it not possible to use Maven to build something during a Chef run?

> etc.
> 
> Maybe consider structuring it a little different as well? Write a  
> shell script or similar, included in your code-base which wraps your  
> 'mvn install', then just run that from Chef. That way you can re-run  
> or developers could run it if you're hooning around by hand anyway.  
> Just a thought 🙂

Ultimately I'd like to have two recipes in the cookbook. One to install a pre-built JBoss AS 7.1.3 binary, and a second one to build it on the fly. The second one would all developers to tweak the build to add or remove debugging options as needed. I am using :shell provisioning elsewhere in my setup. I'm not convinced that using that method would get around this problem however.

-- Mark

---

<div class="post-metadata">

**Author:** ![AJ\_Christensen](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/aj_christensen/32/525_2.png) [@AJ\_Christensen](https://discourse.chef.io/u/AJ_Christensen)\
**Post date:** [May 14, 2013, 8:26pm UTC](https://discourse.chef.io/t/run-as-root-or-as-vagrant-user/3934/5 "2013-05-14T20:26:45Z")

</div>

You've got the environment parameters in the bash script itself.

The environment parameter is a parameter to the EXECUTE and SCRIPT  
resources e.g.

bash "script with env vars" do  
environment( "SOME\_KEY" =\> "SOME\_VALUE", "ANOTHER\_KEY" =\> "ANOTHER\_VALUE" )  
code \<\<-EOH  
echo 'Env dump'  
env  
echo 'Env dumped'  
env SOME\_KEY  
env ANOTHER\_KEY  
echo 'specific keys dumped'  
EOH  
end

As for whether Maven will honor this, I don't know, but it's trivial  
to test that Chef can pass environment variables into a new process --  
and this definitely does work.

Cheers,

AJ

On 15 May 2013 08:18, Mark H. Nichols [chef@zanshin.net](mailto:chef@zanshin.net) wrote:

> On May 10, 2013, at 5:46 PM, AJ Christensen [aj@junglist.gen.nz](mailto:aj@junglist.gen.nz) wrote:
> 
> Try the 'environment' parameter to the bash script resource (it's  
> actually from the Execute resource, but this inherits that)
> 
> bash 'foo' do  
> environment 'MAVEN\_OPTS' =\> '-Xms1024m -Xmx2048m -XX:MaxPermSize=500m'  
> end
> 
> I added the "environment" setting to my recipe, resulting in this code:
> 
> bash "mvn install -DskipTests" do  
> code \<\<-EOH  
> environment 'MAVEN\_OPTS' =\> '-Xms1024m -Xmx2048m -XX:MaxPermSize=500m'  
> environment 'JAVA\_OPTS' =\> '-Xms1024m -Xmx2048m -XX:MaxPermSize=512m  
> -noverify'  
> cd /home/vagrant/src/jbossas  
> /usr/local/maven3/bin/mvn install -DskipTests -Dcheckstyle.skip=true  
> EOH  
> end
> 
> However, I'm still getting a Java Heap space error.
> 
> ## [INFO] BUILD FAILURE [INFO]
> 
> ## [INFO] Total time: 2:01.567s [INFO] Finished at: Tue May 14 20:13:36 UTC 2013 [INFO] Final Memory: 105M/121M [INFO]
> 
> [ERROR] Failed to execute goal  
> org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile  
> (default-compile) on project jboss-as-clustering-infinispan: Fatal error  
> compiling: Error while executing the compiler. InvocationTargetException:  
> Java heap space -\> [Help 1]
> 
> From the output it appears that the "environment" setting is not working, as  
> the memory is shown as 121M and not the 1024M minimum asked for.
> 
> Is it not possible to use Maven to build something during a Chef run?
> 
> etc.
> 
> Maybe consider structuring it a little different as well? Write a  
> shell script or similar, included in your code-base which wraps your  
> 'mvn install', then just run that from Chef. That way you can re-run  
> or developers could run it if you're hooning around by hand anyway.  
> Just a thought 🙂
> 
> Ultimately I'd like to have two recipes in the cookbook. One to install a  
> pre-built JBoss AS 7.1.3 binary, and a second one to build it on the fly.  
> The second one would all developers to tweak the build to add or remove  
> debugging options as needed. I am using :shell provisioning elsewhere in my  
> setup. I'm not convinced that using that method would get around this  
> problem however.
> 
> -- Mark

---

<div class="post-metadata">

**Author:** ![Mark\_Nichols](https://avatars.discourse-cdn.com/v4/letter/m/eb8c5e/32.png) [@Mark\_Nichols](https://discourse.chef.io/u/Mark_Nichols)\
**Post date:** [May 15, 2013, 12:54pm UTC](https://discourse.chef.io/t/run-as-root-or-as-vagrant-user/3934/6 "2013-05-15T12:54:33Z")

</div>

On May 14, 2013, at 3:26 PM, AJ Christensen [aj@junglist.gen.nz](mailto:aj@junglist.gen.nz) wrote:

> You've got the environment parameters in the bash script itself.
> 
> The environment parameter is a parameter to the EXECUTE and SCRIPT  
> resources e.g.
> 
> bash "script with env vars" do  
> environment( "SOME\_KEY" =\> "SOME\_VALUE", "ANOTHER\_KEY" =\> "ANOTHER\_VALUE" )  
> code \<\<-EOH  
> echo 'Env dump'  
> env  
> echo 'Env dumped'  
> env SOME\_KEY  
> env ANOTHER\_KEY  
> echo 'specific keys dumped'  
> EOH  
> end

That did the trick. Thank you.

> As for whether Maven will honor this, I don't know, but it's trivial  
> to test that Chef can pass environment variables into a new process --  
> and this definitely does work.

It appears Maven did honor the environment settings.

Thanks again,  
Mark
