Skip to content

Commit 5d7a45e

Browse files
committed
Added initial state of scripts for fetching agenda files from mounted folder.
1 parent 15202d8 commit 5d7a45e

File tree

6 files changed

+387
-27
lines changed

6 files changed

+387
-27
lines changed

.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,6 @@
3333

3434
## Variable used only in docker-compose.yaml
3535
# OS2WEB_TAG=
36+
# Meeting folder mounting credentials
37+
# OS2WEB_MEETINGS_MNT_HOST=
38+
# OS2WEB_MEETINGS_MNT_SHARES=

docker-compose.yaml

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ volumes:
1111
driver: local
1212
os2web:
1313
driver: local
14-
1514
services:
1615

1716
# General application container.
@@ -31,28 +30,16 @@ services:
3130
networks:
3231
- backend
3332
- frontend
34-
environment:
35-
## Environment sensitive settings. See .env file.
36-
- MYSQL_HOSTNAME=${MYSQL_HOSTNAME}
37-
- MYSQL_DATABASE=${MYSQL_DATABASE}
38-
- MYSQL_USER=${MYSQL_USER}
39-
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
40-
- MYSQL_PORT=${MYSQL_PORT}
41-
- DRUPAL_HASH_SALT=${DRUPAL_HASH_SALT}
42-
- OS2WEB_THEME=${OS2WEB_THEME}
43-
33+
env_file:
34+
- .env
4435
mariadb:
4536
image: mariadb:latest
4637
container_name: mariadb
4738
volumes:
4839
- ./.docker/mariadb/data:/var/lib/mysql:delegated
4940
- ./.docker/mariadb/my.cnf:/etc/mysql/conf.d/my.cnf:ro,delegated
50-
environment:
51-
## Environment sensitive settings. See .env file.
52-
- MYSQL_ROOT_PASSWORD=root
53-
- MYSQL_DATABASE=${MYSQL_DATABASE}
54-
- MYSQL_USER=${MYSQL_USER}
55-
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
41+
env_file:
42+
- .env
5643
networks:
5744
- backend
5845

@@ -104,13 +91,25 @@ services:
10491
- /bin/bash
10592
- -c
10693
- "drush status --root=/opt/drupal"
107-
environment:
108-
## Environment sensitive settings. See .env file.
109-
- MYSQL_HOSTNAME=${MYSQL_HOSTNAME}
110-
- MYSQL_ROOT_PASSWORD=root
111-
- MYSQL_DATABASE=${MYSQL_DATABASE}
112-
- MYSQL_USER=${MYSQL_USER}
113-
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
114-
- MYSQL_PORT=${MYSQL_PORT}
115-
- DRUPAL_HASH_SALT=${DRUPAL_HASH_SALT}
116-
- OS2WEB_THEME=${OS2WEB_THEME}
94+
env_file:
95+
- .env
96+
97+
agenda_import:
98+
# Check agenda file container.
99+
image: dkbellcom/os2web8:${OS2WEB_TAG}
100+
container_name: agenda_import
101+
volumes:
102+
- ./files:/opt/drupal/files
103+
- ./:/opt/drupal
104+
- ./private:/opt/drupal/private
105+
env_file:
106+
- .env
107+
networks:
108+
- backend
109+
entrypoint:
110+
- /bin/bash
111+
- -c
112+
command:
113+
- "mkdir -p mnt/Testshares; \
114+
cat /opt/drupal/private/mntCred; \
115+
php /opt/drupal/scripts/dagsorden/check_shares.php -h=${OS2WEB_MEETINGS_MNT_HOST} -s=${OS2WEB_MEETINGS_MNT_SHARES} -m=/opt/drupal/mnt -c=/opt/drupal/private/mntCred -d"

scripts/dagsorden/check_shares.php

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
#!/usr/bin/php
2+
<?php
3+
/**
4+
* This script checks if the file shares at Ringsted are available, and makes
5+
* sure they are mounted. On top of this, the script also checks what the last
6+
* file is. Upon errors, it will send them by mail, and later by POST when our
7+
* event console is up and running.
8+
*
9+
* Maintained and written by Bellcom's Operations
10+
*
11+
* Last revision:
12+
* Jan 26 2021
13+
*
14+
*/
15+
16+
if (PHP_SAPI !== 'cli') {
17+
return;
18+
}
19+
20+
/**
21+
* Configuration
22+
*/
23+
24+
/** Configuration */
25+
$shortopts = "h::s::p::c::m::l::d::";
26+
$longopts = ["host", "sharedArray", "port", "cred", "mountPath", "log", "debug"];
27+
$opts = getopt($shortopts, $longopts);
28+
$port = 445;
29+
$debug = FALSE;
30+
foreach ($opts as $key => $value) {
31+
switch ($key) {
32+
case 'h':
33+
case 'host':
34+
$host = $value;
35+
break;
36+
37+
case 's':
38+
case 'sharedArray':
39+
$sharesArray = explode(',', $value);
40+
break;
41+
42+
case 'p':
43+
case 'port':
44+
$port = $value;
45+
break;
46+
47+
case 'c':
48+
case 'cred':
49+
$credentials = $value;
50+
break;
51+
52+
case 'm':
53+
case 'mountPath':
54+
$mountPath = $value;
55+
break;
56+
57+
case 'l':
58+
case 'log':
59+
$logFileArg = $value;
60+
break;
61+
62+
case 'd':
63+
case 'debug':
64+
$debug = TRUE;
65+
break;
66+
67+
}
68+
}
69+
print_r($opts);
70+
71+
72+
if (!$host || !$sharesArray || !$credentials || !$mountPath) {
73+
die("The script is used like so: -h=host -s=directory1,directory2 -m=/path/mount/to -c=/path/to/cred [-p=port] [-l=/path/to/logfile]" . PHP_EOL);
74+
}
75+
76+
77+
/**
78+
* Code
79+
*/
80+
// By default script will log info into /dev/stdout.
81+
// Unless log file could be specified as second argument
82+
// or environment variable.
83+
$log = empty($logFile) ? NULL : fopen($logFile, "a+");
84+
85+
writeToLog("----"); // Just to more easily read logs
86+
echo writeToLog("Initiated check.");
87+
88+
// Check if connection exists
89+
if (checkIfConnectionExists($host, $port)) {
90+
// If connection exists, check if shares are mounted, and if not, mount them.
91+
mountshares($host, $port, $sharesArray, $mountPath, $credentials);
92+
checkLastFile($mountPath, $sharesArray);
93+
}
94+
else {
95+
// in exit status - Also making sure the log file is closed properly.
96+
if ($log) {
97+
fclose($log);
98+
}
99+
exit("ERROR: Connection to $host through port $port is closed or not otherwise not available. Stopping script");
100+
}
101+
102+
echo writeToLog("Script has now finished the run");
103+
104+
// Close opened file.
105+
if ($log) {
106+
fclose($log);
107+
}
108+
109+
/**
110+
* Functions
111+
*/
112+
113+
/**
114+
* Simply checks if the connection to the host to the specified port is
115+
* accessible Currently does not send mails nor POST.
116+
*
117+
* @param $host
118+
* @param $port
119+
* @param $openLogfile
120+
*
121+
* @return bool
122+
*/
123+
function checkIfConnectionExists($host, $port = 445) {
124+
// Check if connection to host through port 445 is open
125+
$connectionOpen = @fsockopen($host, $port, $errNo, $errStr, 5);
126+
// If resource is available
127+
if (is_resource($connectionOpen)) {
128+
// Write to log
129+
echo writeToLog("Connection to $host through port $port is open. Continuing.");
130+
}
131+
else {
132+
// else, report error
133+
// in log
134+
echo writeToLog("ERROR: Connection to $host through port $port is closed or not otherwise not available. Stopping script");
135+
136+
}
137+
return TRUE; // If everything works, it'll return true. If nothing works, it'll exit anyway.
138+
}
139+
140+
/**
141+
*
142+
* Checks if shares are mounted, and if not mounts them.
143+
* Currently doesn't report any errors, because I don't know what an erroneous
144+
* output looks like at the moment.
145+
*
146+
* @param $host
147+
* @param $port
148+
* @param $sharesArray
149+
* @param $mountPath
150+
* @param $credentialFile
151+
*
152+
* @return bool
153+
*/
154+
function mountShares($host, $port, $sharesArray, $mountPath, $credentialFile) {
155+
global $debug;
156+
157+
// Loop through shares
158+
foreach ($sharesArray as $share) {
159+
160+
// Check if mount points are of type nfs or cifs
161+
$command = "df -P -T $mountPath/$share | tail -n +2 | awk '{print $2}'";
162+
if ($debug) {
163+
echo writeToLog("DEBUG: Executing command: " . $command);
164+
}
165+
$checkMountPoint = exec($command);
166+
// If the mount point is of type nfs or cifs, don't remount it, and simply write to log that it's already mounted
167+
if ($checkMountPoint == "cifs") {
168+
echo writeToLog("The mount point $mountPath/$share is of type NFS or CIFS, so it's most likely mounted already. Not remounting.");
169+
}
170+
else {
171+
// If the mount point is of something else, mount the remote share to the mount points
172+
echo writeToLog("The mount point $mountPath/$share is not of type NFS or CIFS, so it's most likely not mounted. Mounting.");
173+
$command = "mount -t cifs //$host/$share $mountPath/$share -o cred=$credentialFile";
174+
if ($debug) {
175+
echo writeToLog("DEBUG: Executing command: " . $command);
176+
}
177+
if ($port) {
178+
$command .= ',port=' . $port;
179+
}
180+
exec($command, $mountOutput);
181+
echo writeToLog(print_r($mountOutput, 1));
182+
}
183+
}
184+
185+
return TRUE;
186+
}
187+
188+
/**
189+
* This simply checks the last modified file for each share
190+
*
191+
* @param $mountPath
192+
* @param $sharesArray
193+
*
194+
* @return bool
195+
*/
196+
function checkLastFile($mountPath, $sharesArray) {
197+
198+
$findOutput = FALSE; // Just so we don't have an undefined variable
199+
200+
foreach ($sharesArray as $share) {
201+
$findOutput = exec("/usr/bin/find $mountPath/$share -type f -printf '%T+\t%p\n' | /usr/bin/sort | /usr/bin/tail -1");
202+
echo writeToLog("The last modified file in $mountPath/$share is: $findOutput");
203+
}
204+
205+
return $findOutput;
206+
}
207+
208+
/**
209+
* This function helps put stuff in the configured log file
210+
*
211+
* @param $logString
212+
*
213+
* @return string
214+
*/
215+
function writeToLog($logString) {
216+
// Get current date and time.
217+
$dt = new DateTime();
218+
$now = $dt->format('Y-m-d H:i:s');
219+
220+
// Attach date to logString.
221+
$dateString = "[" . $now . "] - ";
222+
$logString = $dateString . $logString . "\n";
223+
224+
global $log;
225+
// Do some logging.
226+
if (isset($log)) {
227+
fwrite($log, $logString);
228+
}
229+
return $logString;
230+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/php
2+
<?php
3+
/**
4+
* This script checks the folders last updated file and logs it.
5+
*/
6+
7+
if (PHP_SAPI !== 'cli') {
8+
return;
9+
}
10+
11+
/** Configuration */
12+
$args = $_SERVER['argv'];
13+
if (empty($args) || count($args) < 2) {
14+
exit("The script is used like so: last_modified_file.php /path/to/directory [/path/to/logfile]" . PHP_EOL);
15+
}
16+
17+
// By default script will log info into /dev/stdout.
18+
// Unless log file could be specified as second argument
19+
// or environment variable.
20+
$logFile = empty($args[2]) ? (getenv('LOG_FILE') ?: NULL) : $args[2];
21+
22+
// Path to subject directory.
23+
$directoryPath = $args[1];
24+
if (!file_exists($directoryPath)) {
25+
exit("WARNING: Directory $directoryPath does not exist." . PHP_EOL);
26+
}
27+
28+
/** Code */
29+
// Open log file and put resource in variable $log
30+
// Used later for writing and closing; +a for creating file if it doesn't exist
31+
// and setting cursor to end of file.
32+
$log = empty($logFile) ? NULL : fopen($logFile, "a+");
33+
34+
echo writeToLog("Initiated check.");
35+
$findOutput = exec("/usr/bin/find $directoryPath -type f -printf '%TY-%Tm-%Td %TH:%TM:%.2TS\t%p\n' | /usr/bin/sort | /usr/bin/tail -1");
36+
echo writeToLog("The last modified file in $directoryPath is: $findOutput");
37+
echo writeToLog("Script has now finished the run");
38+
39+
// Close opened file.
40+
if ($log) {
41+
fclose($log);
42+
}
43+
44+
/**
45+
* This function helps put stuff in the configured log file
46+
*
47+
* @param $logString
48+
* @return string
49+
*/
50+
function writeToLog($logString) {
51+
// Get current date and time.
52+
$dt = new DateTime();
53+
$now = $dt->format('Y-m-d H:i:s');
54+
55+
// Attach date to logString.
56+
$dateString = "[" . $now . "] - ";
57+
$logString = $dateString . $logString . "\n";
58+
59+
global $log;
60+
// Do some logging.
61+
if (isset($log)) {
62+
fwrite($log, $logString);
63+
}
64+
return $logString;
65+
}

0 commit comments

Comments
 (0)