Welcome to mirror list, hosted at ThFree Co, Russian Federation.

transifexSync.php « translations - github.com/nextcloud/docker-ci.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7f0e263251f98b4abaecb0fdf28aa5b47cd4fb67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php

declare(strict_types=1);

$logPath = '/var/log/cronie/';
$dataPath = '/srv/cronie-data/';
$triggerPath = '/var/log/cronie/trigger';

if (gethostname() !== 'transifex-sync') {
	$logPath = __DIR__ . '/status/log/';
	$dataPath = __DIR__ . '/data/';
	$triggerPath = __DIR__ . '/status/trigger';
}

$triggerContent = '';
if ($argc === 2 && $argv[1] === 'trigger') {
	if (file_exists($triggerPath)){
		$triggerContent = trim(file_get_contents($triggerPath));
		if($triggerContent === '') {
			die();
		}
		file_put_contents($triggerPath, '');
	} else {
		die();
	}
}

class ResultInfo {
	/** @var string */
	public $name;
	/** @var string */
	public $arguments;
	/** @var DateTime */
	public $startDate;
	/** @var DateTime */
	public $endDate;
	/** @var string */
	public $errorMessage = '';

	public function getJson(): string {
		$data = [
			'name' => $this->name,
			'arguments' => $this->arguments,
			'start' => $this->startDate->format(\DateTime::ISO8601),
			'end' => $this->endDate->format(\DateTime::ISO8601),
			'error' => $this->errorMessage
		];
		return json_encode($data);
	}
}

function runJob(string $name, string $arguments, string $dataPath, string $logPath): ResultInfo {

	if ($name === 'server') {
		$imageName = '';
	} else {
		$imageName = '-' . $name;
	}

	$result = new ResultInfo();

	$result->name = $name;
	$result->arguments = $arguments;
	$result->startDate = new DateTime();

	print('  Pulling container' . PHP_EOL);
	shell_exec('docker pull ghcr.io/nextcloud/continuous-integration-translations' . $imageName);
	print('  Running container' . PHP_EOL);
	exec('docker run -v ' . $dataPath . 'transifexrc:/root/.transifexrc -v ' . $dataPath . 'gpg:/gpg -v ' . $dataPath . 'ssh/id_rsa:/root/.ssh/id_rsa --rm -i ghcr.io/nextcloud/continuous-integration-translations' . $imageName . ' ' . $arguments . ' 2>&1', $output, $returnValue);

	$result->endDate = new DateTime();

	$output = implode(PHP_EOL, $output);

	if ($returnValue !== 0) {
		$result->errorMessage = $output;
	}

	$fileName = $name . '-' . str_replace(' ', '-', $arguments) . '-' .$result->startDate->format('Y-m-d.H-i-s');
	file_put_contents($logPath . $fileName . '.log', $output);
	file_put_contents($logPath . $fileName . '.json', $result->getJson());

	return $result;
}

$data = json_decode(file_get_contents(__DIR__ . '/config.json'), true);

if(is_null($data)) {
	print('Cannot decode JSON from config.json.'. PHP_EOL);
	die(1);
}

if(!file_exists($dataPath . '/transifexrc')) {
	print('"transifexrc" does not exist in "' . $dataPath . '".' . PHP_EOL);
	die(1);
}
if(!file_exists($dataPath . '/gpg')) {
	print('"gpg" does not exist in "' . $dataPath . '".' . PHP_EOL);
	die(1);
}
if(!file_exists($dataPath . '/ssh/id_rsa')) {
	print('"ssh/id_rsa" does not exist in "' . $dataPath . '".' . PHP_EOL);
	die(1);
}
if(substr($logPath, 0, 1) !== '/' || substr($logPath, strlen($logPath) - 1, 1) !== '/') {
	print('$logPath (' . $logPath . ') needs to start and end with a "/"' . PHP_EOL);
	die(1);
}
if(substr($dataPath, 0, 1) !== '/' || substr($dataPath, strlen($dataPath) - 1, 1) !== '/') {
	print('$dataPath (' . $dataPath . ') needs to start and end with a "/"' . PHP_EOL);
	die(1);
}
if(!file_exists($dataPath) || !file_exists($dataPath)) {
	print('$dataPath (' . $dataPath . ') and $logPath (' . $logPath . ') need to exist' . PHP_EOL);
	die(1);
}
if(!is_writable($logPath)) {
	print('$logPath (' . $logPath . ') need to be writable' . PHP_EOL);
	die(1);
}

$jobs = $data['jobs'];

foreach ($jobs as $job) {
	if(!isset($job['name']) || !isset($job['arguments'])) {
		print('Job does not have a name or arguments list.');
		print_r($job);
	}
	$name = $job['name'];
	$argumentsList = $job['arguments'];

	print('Job: ' . $name . PHP_EOL);

	foreach ($argumentsList as $arguments) {
		print('  Arguments: ' . $arguments . PHP_EOL);

		// if trigger call then skip all except the one to be triggered
		if ($triggerContent !== '' && $triggerContent !== trim($name . ' ' . $arguments)) {
			continue;
		}
		$result = runJob($name, $arguments, $dataPath, $logPath);

		// try to run it a second time (maybe a github pull issue)
		if ($result->errorMessage !== '') {
			print('  Second run' . PHP_EOL);
			$result = runJob($name, $arguments, $dataPath, $logPath);

			// send email in case of trouble
			if ($result->errorMessage !== '') {
				@mail('joas@nextcloud.com,tobias@nextcloud.com', 'Transifex sync job failed for ' . ($arguments ?: $name), 'This is the log:' . PHP_EOL . $result->errorMessage, ['From' => 'no-reply@nextcloud.com']);
			}
		}
	}
}