Skip to content

Commit 0d2493d

Browse files
committed
load_environment_file support
1 parent 30403a1 commit 0d2493d

6 files changed

Lines changed: 146 additions & 3 deletions

File tree

.github/workflows/cmake.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ env:
66
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
77
BUILD_TYPE: Release
88
PACKAGE_NAME: docker-runner-php
9-
PACKAGE_VERSION: 1.0.1-1
9+
PACKAGE_VERSION: 1.0.2-1
1010
ARCH: amd64
1111

1212
jobs:
@@ -31,7 +31,7 @@ jobs:
3131
runs-on: ${{ matrix.os }}
3232
strategy:
3333
matrix:
34-
os: [ubuntu-latest, ubuntu-20.04]
34+
os: [ubuntu-22.04, ubuntu-20.04, ubuntu-18.04]
3535

3636
steps:
3737
- uses: actions/checkout@v2

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ project(docker-runner)
44
set(CMAKE_CXX_STANDARD 14)
55
set(CMAKE_CXX_FLAGS "-mtune=generic -march=x86-64 -std=c++0x")
66
LINK_LIBRARIES(rt dl)
7-
add_executable(docker-runner main.cpp globals.h cdocker.cpp cdocker.h cdockercontainer.cpp cdockercontainer.h cphprunner.cpp cphprunner.h)
7+
add_executable(docker-runner main.cpp globals.h cdocker.cpp cdocker.h cdockercontainer.cpp cdockercontainer.h cphprunner.cpp cphprunner.h utils.cpp utils.h)

globals.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@
77
#define DBUF_SIZE_DEFAULT 4096
88
#define DBUF_SIZE_SMALL 1024
99

10+
#define ENV_FILE_PRELOAD_DEFAULT "/etc/docker-images/.env"
11+
#define ENV_FILE_PRELOAD_ENV_KEY "ENV_FILE_PRELOAD"
12+
1013
#endif // GLOBALS
1114

main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "cdocker.h"
88
#include "cphprunner.h"
99
#include "globals.h"
10+
#include "utils.h"
1011

1112
extern "C" {
1213
#include <libgen.h>
@@ -82,6 +83,7 @@ int main(int argc, char **argv)
8283
PHPRunner.getSelectedVersion(&dockerHostname);
8384
selected_version_int = PHPRunner.getSelectedVersion();
8485

86+
load_environment_file();
8587
return Docker.run(dockerHostname, selected_version_int, newargs);
8688
}
8789

utils.cpp

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#include "globals.h"
2+
#include <string>
3+
#include <unistd.h>
4+
#include <cstring>
5+
#include <cstdio>
6+
7+
static
8+
void rtrim(char *line) {
9+
if (line == nullptr)
10+
return;
11+
size_t line_length = strlen(line);
12+
char *lhelper = line+line_length-1;
13+
if (line_length == 0)
14+
return;
15+
16+
while (1) {
17+
if (lhelper < line)
18+
break;
19+
20+
// space allowed
21+
if (isspace(*lhelper)) {
22+
*lhelper = 0;
23+
lhelper--;
24+
continue;
25+
} else {
26+
break;
27+
}
28+
}
29+
}
30+
31+
static
32+
char* ltrim(char *line) {
33+
if (line == nullptr)
34+
return line;
35+
size_t line_length = strlen(line);
36+
char *line_new = line;
37+
for (size_t i=0, e=line_length; i<e; i++) {
38+
if (!isspace(line[i])) {
39+
line_new = line+i;
40+
break;
41+
}
42+
}
43+
return line_new;
44+
}
45+
46+
static
47+
bool is_valid_key(char *key) {
48+
if (!key)
49+
return false;
50+
51+
size_t key_len = strlen(key);
52+
if (key_len == 0)
53+
return false;
54+
55+
56+
for (size_t i=0; i<key_len; i++) {
57+
if (isalnum(key[i]) || key[i] == '_') {
58+
continue;
59+
} else {
60+
return false;
61+
}
62+
}
63+
return true;
64+
}
65+
66+
static
67+
void load_environment_line(char *line, size_t length) {
68+
rtrim(line);
69+
char *line_start = ltrim(line);
70+
size_t line_length = strlen(line_start);
71+
if (line_length < 3)
72+
return;
73+
74+
if (line_start[0] == '#') {
75+
return;
76+
}
77+
char *eq_pos = strchr(line_start, '=');
78+
if (eq_pos == nullptr)
79+
return;
80+
if (line_length-(eq_pos-line_start) <= 1)
81+
return;
82+
83+
*eq_pos = 0;
84+
char *key = line_start;
85+
if (!is_valid_key(key)) {
86+
return;
87+
}
88+
89+
char *val = eq_pos+1;
90+
rtrim(key);
91+
size_t val_len = strlen(val);
92+
if (val_len >= 2) {
93+
if ((val[0] == '"' || val[0] == '\'') && val[0] == val[val_len-1]) {
94+
val[val_len-1] = 0;
95+
val++;
96+
val_len -= 2;
97+
}
98+
}
99+
if (val_len == 0) {
100+
unsetenv(key);
101+
return;
102+
}
103+
104+
setenv(key, val, 1);
105+
}
106+
107+
void load_environment_file()
108+
{
109+
std::string env_file = ENV_FILE_PRELOAD_DEFAULT;
110+
char *env_file_from_env = secure_getenv(ENV_FILE_PRELOAD_ENV_KEY);
111+
FILE *f = nullptr;
112+
char linebuf[4096]{};
113+
114+
if (env_file_from_env != nullptr) {
115+
env_file.assign(env_file_from_env);
116+
}
117+
if (access(env_file.c_str(), R_OK) != 0)
118+
return;
119+
f = fopen(env_file.c_str(), "r");
120+
if (f == nullptr) {
121+
return;
122+
}
123+
124+
while (fgets(linebuf, sizeof(linebuf), f)) {
125+
load_environment_line(linebuf, sizeof(linebuf));
126+
}
127+
fclose(f);
128+
}

utils.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//
2+
// Created by macskas on 10/13/23.
3+
//
4+
5+
#ifndef DOCKER_RUNNER_UTILS_H
6+
#define DOCKER_RUNNER_UTILS_H
7+
8+
void load_environment_file();
9+
10+
#endif //DOCKER_RUNNER_UTILS_H

0 commit comments

Comments
 (0)