You are not logged in.

#1 2007-10-29 03:12:25

dev
Member
Registered: 2007-10-28
Posts: 8

Updating SVN via PHP script and C Program

What I really need is for my users to be able to update a working copy via a web form rather than the update happen following each commit.. I want to execute a c program (since the PHP script runs as Apache user) from a PHP script instead of from the svn post-commit hook.

My C program works from the command line but does not when executed by the Apache user via a php script. I have set the s bit set via "chmod +s programName"

Below is my php script and my c source
Thanks,
Dev


<< PHP Script >>
<?php

// Execute the c program svnUpdate (see below for source)
$command = "/home/myUserName/webapps/myApp/workingCopy/svnUpdate";

echo "Date:" . date('l dS \of F Y h:i:s A');
echo "<pre>Executing command as user" . exec('whoami') . "</pre>\n";
echo "<pre>Executing $command" . system($command) . "</pre>\n";
echo "<pre>... command executed.</pre>\n";
?>
<< end php script >>

<< svnUpdate.c:  C Source which is compiled and s bit set via chmod +s>>

int main( int argc, char *argv[] )

        // printf("Calling execl:\n");

        execl("/usr/local/bin/svn", "svn", "update",
             "/home/myUserName/webapps/myApp/workingcopy",
                 (const char *) NULL);     

        return(EXIT_FAILURE);
}

<< end C Source >>

Last edited by dev (2007-10-29 03:13:19)

Offline

 

#2 2007-10-29 10:48:07

IAIHMB
Member
From: Hudson, Florida.
Registered: 2006-09-19
Posts: 1362

Re: Updating SVN via PHP script and C Program

Hello,

This isn't going to work unless the apache user has RWX access to ~/.subversion/ and ~/webapps/(Application Name)/. If you were to give the apache user RWX access to ~/.subversion/ and ~/webapps/(Application Name)/ any files created by the apache user would be owned by the apache user, which won't help one's sanity. smile

You've two options:

1) Execute your C script from a CGI script, for example:

Code:

#!/usr/local/bin/python2.5

import subprocess

proc = subprocess.Popen('/usr/local/bin/svn update ...', shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
proc.wait()

print 'Content-Type: text/plain'
print
print 'stderr: %s' % proc.stderr.read()
print 'stdout: %s' % proc.stdout.read()

If you must use PHP you could even make this CGI script web accessible and wget it from PHP.

2) Install PHP and nginx/LightTPD/whatever so that PHP is executed as your user.

Hope it helps. smile


-David Sissitka

Offline

 

#3 2007-10-30 04:29:10

dev
Member
Registered: 2007-10-28
Posts: 8

Re: Updating SVN via PHP script and C Program

Have you seen: http://forum.webfaction.com/viewtopic.php?pid=216#p216

I want to do almost the same thing except that my c binary will run from a Web form. Since both the svn post-commit hook and the script behind the Web form execute as the Apache user, shouldn't this work?


In option 1. It looks as if you are executing everything via the Python script. I don't see any call to a C binary there. I'm not familiar with python CGI but I'll look into it. I don't care about PHP, I need to allow some users of mine update the live working copy so I can remove myself from their development loop without granting SSH access.

Thanks!
Dev

Offline

 

#4 2007-10-30 09:19:50

IAIHMB
Member
From: Hudson, Florida.
Registered: 2006-09-19
Posts: 1362

Re: Updating SVN via PHP script and C Program

dev wrote:

Have you seen: http://forum.webfaction.com/viewtopic.php?pid=216#p216

I want to do almost the same thing except that my c binary will run from a Web form. Since both the svn post-commit hook and the script behind the Web form execute as the Apache user, shouldn't this work?

It should, what's written to stderr and stdout when your program is executed by the apache user? See:

http://www.php.net/manual/en/function.system.php#52770

dev wrote:

In option 1. It looks as if you are executing everything via the Python script. I don't see any call to a C binary there.

You could use Python to execute your C program if you'd like, simply replace:

proc = subprocess.Popen('/usr/local/bin/svn update ...', shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)

With:

proc = subprocess.Popen('XXX', shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)

Where XXX is the path to C program.

In my original example I just eliminated the need for the C program by doing the "svn update ..." in Python. smile


-David Sissitka

Offline

 

#5 2007-10-31 01:27:29

dev
Member
Registered: 2007-10-28
Posts: 8

Re: Updating SVN via PHP script and C Program

Here's what I get on stderr with the following php code

PHP Script:
   $command = "/home/myUserName/webapps/myApp/workingCopy/svnUpdate";
   $command += $command .= " 2>&1";
   echo "<pre>Executing $command\n" . exec($command) . "</pre>\n";

Output:
    Executing /home/myUserName/webapps/myApp/workingCopy/svnUpdate 2>&1
    Can't check path '/root/.subversion': Permission denied

Permissions for my c binary:
-rwsrwsr-x  1 myUserName myUserName    5126 Oct 29 02:56 svnUpdate

Seems the c binary is also executing as ApacheUser?

Dev

Offline

 

#6 2007-11-08 06:20:27

dev
Member
Registered: 2007-10-28
Posts: 8

Re: Updating SVN via PHP script and C Program

I've abandoned the update via a web form.

I just want to update my entire working copy stored on the server. My C program is executing, however the 'svn update' itself is failing. I know the C program is executing because I have system('touch someFile') at the start and end of the program.

Is there something wrong server side? I've been trying to get this working for a few weeks now off and on and I'm at the end of my rope!

Here is my C Program which is called by SVN post-commit hook.

#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
        system("touch /home/myUserName/temp/ExecutedSvnCPostCommit-Start");    /// file is sucessfully created
        system("/usr/local/bin/svn update /home/myUserName/webapps/myApp/");     /// this line seems to do nothing!!!
        system("touch /home/myUserName/temp/ExecutedSvnCPostCommit-End");     /// file is successfully created
  return(EXIT_SUCCESS);

}

Offline

 

#7 2007-11-08 08:42:24

IAIHMB
Member
From: Hudson, Florida.
Registered: 2006-09-19
Posts: 1362

Re: Updating SVN via PHP script and C Program

Have you tried redirecting stderr and stdout to a file? That is:

/usr/local/bin/svn update /home/myUserName/webapps/myApp/ 2>&1 >  /home/myUserName/post-commit.log

Hope it helps. smile

Last edited by IAIHMB (2007-11-08 12:08:23)


-David Sissitka

Offline

 

#8 2007-11-08 11:59:00

dev
Member
Registered: 2007-10-28
Posts: 8

Re: Updating SVN via PHP script and C Program

You made a mistake. "2>&1" must come at the end of the command. I changed the C program so that it now looks like the following:

#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
system("touch /home/myUserName/temp/ExecutedSvnCPostCommit-Start");    /// file is sucessfully created
system("/usr/local/bin/svn update /home/myUserName/webapps/myApp/ > /home/myUserName/temp/post-commit.log 2>&1" );
system("touch /home/myUserName/temp/ExecutedSvnCPostCommit-End");     /// file is successfully created
  return(EXIT_SUCCESS);

}

The file post-commit.log is created and says:
     svn: Can't open file '/home/myUserName/webapps/myApp/.svn/lock': Permission denied


Dev

Last edited by dev (2007-11-08 12:11:38)

Offline

 

#9 2008-01-26 14:32:44

Techie-Micheal
Member
Registered: 2008-01-26
Posts: 57

Re: Updating SVN via PHP script and C Program

Something like this should work, though I haven't tried it:

Code:

<?php
putenv("USERNAME=[insert your username]");
$sys = system("svn update [path/to/checkout]");
echo $sys;
?>

However, I do know that this works (at least, on my server):

Code:

#!/usr/bin/python2.4
import os
import commands

print "Content-type: text/html\n\n";
status, output = commands.getstatusoutput('svn update [path/to/checkout]')
paras = output.split('\n')
paras = ['<p>%s</p>' % para.strip() for para in paras]
print '\n'.join(paras)

Offline

 

#10 2008-10-29 18:29:12

Simon
Member
Registered: 2008-10-29
Posts: 1

Re: Updating SVN via PHP script and C Program

This works for me:

Code:

<?php
        putenv("HOME=/home/username"); //USERNAME=username does not help, it is HOME that matters.
        passthru("svn up", $ret);
        echo "[$ret]";
?>

Last edited by Simon (2008-10-29 18:30:01)

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson