1
votes

I have a script in python which get one path with OptionParser and copy the file in that path to another path with timestamp+name using shutil.copy.

When I launch the script from shell, everything works fine. But when Jenkins launches the script, I get the following exception:

Traceback (most recent call last):
  File "/home/helper/add_file.py", line 101, in <module>
    uploader.run()
  File "/home/helper/add_file.py", line 80, in run
    shutil.copy(self.filename,destination_path+disk_filename)
  File "/usr/lib/python2.7/shutil.py", line 119, in copy
    copyfile(src, dst)
  File "/usr/lib/python2.7/shutil.py", line 83, in copyfile
    with open(dst, 'wb') as fdst:
IOError: [Errno 13] Permission denied: u'/home/blabla/destinationfolder/1399362213.01file.apk'

With whoami and pwd I recreate the execution in shell with the same user (tomcat7) and from the same location (jenkins project workspace) and there is no permission denied problem, user tomcat7 has full writing permission in destination directory and the source directory...

So, why with the same user and using the same destination and source path, from the shell everything works fine and executing from Jenkins I get this "Permission Denied" exception?

UPDATED

I tested the user with getpass.getuser() and the result is the same in jenkins and in the shell (tomcat7).

I changed the permisions from 775 to 777 and then I can write at the directory... But this is not a solution because I dont want give write permissions to all. The user tomcat7 is in the group who has permission to write the folder so... It's like Jenkins is ignoring the groups from the user tomcat7 (I checked the /etc/group file and tomcat7 is in the group with permissions)

1
Have you tried adding a line to execute whoami in your Jenkins configuration and checking the output in the Jenkins logs, to see which user Jenkins is really executing this as? The Jenkins instances I run execute all jobs under the jenkins user by default (though of course yours may be configured differently)robjohncox
Yes, I executed the whoami in my Jenkins configuration. I added too a getpass.getuser() to my python script; with the same result in shell and in jenkins ( tomcat7 user ); but... It's like executing with jenkins the group is ignored.Pedro Náñez
Make Jenkins as owner of the directory, so that nobody else will have write access to it.rajpy

1 Answers

1
votes

Finally, I found this: https://unix.stackexchange.com/questions/45184/why-this-error-cannot-create-directory-foo-permission-denied

My problem was the same... I changed the group from tomcat7 user, but my tomcat was in execution since the last boot, with the old user. After restart the tomcat service, tomcat user got the correct group and the script execution in jenkins was OK.