Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
wims-info
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Nicolas Thiery
wims-info
Commits
0699a7c9
Commit
0699a7c9
authored
9 years ago
by
Nicolas M. Thiéry
Browse files
Options
Downloads
Patches
Plain Diff
Nettoyage, noms plus coherents, TODO, ...
parent
1a9fcf92
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
test~coding~readingCppPrograms.fr/docker-compile-and-run.cpp
+26
-36
26 additions, 36 deletions
test~coding~readingCppPrograms.fr/docker-compile-and-run.cpp
with
26 additions
and
36 deletions
test~coding~readingCppPrograms.fr/docker-compile-and-run.cpp
+
26
−
36
View file @
0699a7c9
...
...
@@ -11,14 +11,17 @@ using namespace std;
/**
* TODO:
* - [X] make this into a program rather than a shell script for a minimum of safety
* - [ ] set tight resources limits
* - [ ] make this into a program rather than a shell script for a minimum of safety
* - [ ] check proper handling of all potential errors
* - [ ] make this into a plain C program to not add a dependency on C++
**/
string
compile_and_run
=
"compile-and-run.sh"
;
//string bin_dir = "/home/wims/public_html/bin/";
string
bin_dir
=
""
;
string
docker
=
"/usr/bin/docker"
;
bool
verbose
=
false
;
void
usage
()
{
cerr
<<
"docker-compile-and-run [program.cpp]"
<<
endl
;
...
...
@@ -28,20 +31,13 @@ void usage () {
cerr
<<
"The exit status is that of the compiler."
<<
endl
;
}
int
system
(
string
command
)
{
// cerr << "Running " << command << endl;
std
::
system
(
command
.
c_str
());
}
void
report
(
string
comment
,
string
cmd
,
vector
<
string
>
args
)
{
cerr
<<
comment
;
cerr
<<
cmd
;
for
(
auto
arg
:
args
)
cerr
<<
" "
<<
arg
;
cerr
<<
endl
;
}
int
cpp_exec
(
string
cmd
,
const
vector
<
string
>
args
)
{
int
my_exec
(
string
cmd
,
const
vector
<
string
>
args
)
{
if
(
verbose
)
{
cerr
<<
"exec: "
<<
cmd
;
for
(
auto
arg
:
args
)
cerr
<<
" "
<<
arg
;
cerr
<<
endl
;
}
vector
<
const
char
*>
argv
;
argv
.
push_back
(
cmd
.
c_str
());
for
(
auto
&
s
:
args
)
...
...
@@ -51,9 +47,7 @@ int cpp_exec(string cmd, const vector<string> args) {
return
execv
(
cmd
.
c_str
(),
(
char
*
const
*
)
argv
.
data
());
}
int
exec
(
string
cmd
,
const
vector
<
string
>
args
,
const
string
in
,
const
string
out
,
const
string
err
)
{
report
(
"exec: "
,
cmd
,
args
);
int
my_system
(
string
cmd
,
const
vector
<
string
>
args
,
const
string
in
,
const
string
out
,
const
string
err
)
{
// Taken from man waitpid
int
status
;
pid_t
w
;
...
...
@@ -73,7 +67,7 @@ int exec(string cmd, const vector<string> args, const string in, const string ou
dup2
(
fileno
(
f
),
STDOUT_FILENO
);
fclose
(
f
);
}
cpp
_exec
(
cmd
,
args
);
my
_exec
(
cmd
,
args
);
perror
(
"exec"
);
exit
(
EXIT_FAILURE
);
}
...
...
@@ -85,13 +79,11 @@ int exec(string cmd, const vector<string> args, const string in, const string ou
return
EXIT_SUCCESS
;
}
int
exec
(
string
cmd
,
const
vector
<
string
>
args
)
{
return
exec
(
cmd
,
args
,
""
,
""
,
""
);
int
my_system
(
string
cmd
,
const
vector
<
string
>
args
)
{
return
my_system
(
cmd
,
args
,
""
,
""
,
""
);
}
std
::
string
pexec
(
const
string
cmd
,
vector
<
string
>
args
)
{
report
(
"pexec: "
,
cmd
,
args
);
std
::
string
my_popen
(
const
string
cmd
,
vector
<
string
>
args
)
{
int
pipefd
[
2
];
pid_t
cpid
;
...
...
@@ -109,7 +101,7 @@ std::string pexec(const string cmd, vector<string> args) {
if
(
cpid
==
0
)
{
/* Child writes to pipe */
close
(
pipefd
[
0
]);
/* Close unused read end */
dup2
(
pipefd
[
1
],
STDOUT_FILENO
);
cpp
_exec
(
cmd
,
args
);
my
_exec
(
cmd
,
args
);
perror
(
"exec"
);
exit
(
EXIT_FAILURE
);
}
...
...
@@ -131,7 +123,7 @@ string docker_run(string container, string cmd, vector<string> args) {
vector
<
string
>
docker_args
=
{
"run"
,
"-d"
,
container
,
cmd
};
for
(
auto
arg
:
args
)
docker_args
.
push_back
(
arg
);
string
ID
=
pexec
(
docker
,
docker_args
);
string
ID
=
my_popen
(
docker
,
docker_args
);
// see http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
ID
.
erase
(
ID
.
find_last_not_of
(
"
\n\r\t
"
)
+
1
);
return
ID
;
...
...
@@ -141,26 +133,24 @@ void docker_exec(string docker_id, vector<string> args) {
vector
<
string
>
docker_args
=
{
"exec"
,
"-i"
,
docker_id
};
for
(
auto
arg
:
args
)
docker_args
.
push_back
(
arg
);
exec
(
docker
,
docker_args
);
my_system
(
docker
,
docker_args
);
}
void
docker_cp
(
string
docker_id
,
string
source
,
string
target
)
{
// See http://stackoverflow.com/questions/22907231/copying-files-from-host-to-docker-container
// TODO: replace with 'docker cp' of docker 1.8 when possible
exec
(
docker
,
{
"exec"
,
"-i"
,
docker_id
,
"/bin/bash"
,
"-c"
,
"cat > "
+
target
,},
source
,
""
,
""
);
my_system
(
docker
,
{
"exec"
,
"-i"
,
docker_id
,
"/bin/bash"
,
"-c"
,
"cat > "
+
target
,},
source
,
""
,
""
);
}
void
docker_rm
(
string
docker_id
)
{
exec
(
docker
,
{
"rm"
,
"-f"
,
docker_id
},
""
,
"/dev/null"
,
""
);
my_system
(
docker
,
{
"rm"
,
"-f"
,
docker_id
},
""
,
"/dev/null"
,
""
);
}
int
main
(
int
argc
,
char
**
argv
)
{
//cout << pexec("/usr/bin/id", {}) << "|" << endl;
//cout << my_popen("/usr/bin/id", {}) << "|" << endl;
//printf("egid: %d\n", getegid());
//exec("/usr/bin/id", {});
//system("/usr/bin/id");
...
...
@@ -172,12 +162,12 @@ int main(int argc, char **argv) {
string
program
=
argv
[
1
];
string
docker_id
=
docker_run
(
"crosbymichael/build-essential"
,
"sleep"
,
{
"1000"
});
cout
<<
"docker_id: "
<<
docker_id
<<
endl
;
if
(
verbose
)
cerr
<<
"docker_id: "
<<
docker_id
<<
endl
;
docker_cp
(
docker_id
,
bin_dir
+
compile_and_run
,
compile_and_run
);
docker_cp
(
docker_id
,
program
,
program
);
docker_exec
(
docker_id
,
{
"chmod"
,
"700"
,
compile_and_run
});
docker_exec
(
docker_id
,
{
"./"
+
compile_and_run
,
program
});
docker_rm
(
docker_id
);
cout
.
flush
();
return
0
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment