-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.kubernetes.php
105 lines (97 loc) · 3.66 KB
/
main.kubernetes.php
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
<?php
/**
* Usage:
* File Name: main.kubernetes.php
* Author: annhe
* Mail: [email protected]
* Created Time: 2018-04-18 13:44:54
**/
/** To be Update
* 目前Secret类暂时不用,后续如果改成Secret链接多个APP的形式,
* 需要在这里添加上Secret联系人自动更新的逻辑
*/
class UpdateKubernetesContacts implements iBackgroundProcess
{
public function GetPeriodicity()
{
$interval = MetaModel::GetModuleSetting('kubernetes', 'cron_updatecontacts_interval', 180);
return $interval;
}
private function Add($list, $kubernetes_id) {
// ormLinkSet需要设置oOriginalSet
$oOriginalSet = DBObjectSet::FromScratch('lnkPersonToKubernetes');
$c = new ormLinkSet("Kubernetes", 'person_list', $oOriginalSet);
foreach($list as $v)
{
$p = new lnkPersonToKubernetes();
$p->Set('person_id', $v->Get('contact_id'));
$p->Set('kubernetes_id', $kubernetes_id);
$c->AddItem($p);
}
return $c;
}
private function Update($appid, $aReport) {
$deployment = new DBObjectSet(DBObjectSearch::FromOQL("SELECT Deployment WHERE applicationsolution_id = $appid"));
$ingress = new DBObjectSet(DBObjectSearch::FromOQL("SELECT Ingress WHERE applicationsolution_id = $appid"));
// 根据关联的APP自动填充联系人字段
$app = MetaModel::GetObject('ApplicationSolution', $appid);
$list = $app->Get('contact_list_custom');
while($item = $deployment->Fetch()) {
$c = $this->Add($list, $item->GetKey());
$item->Set('person_list', $c);
$item->DBUpdate();
$aReport['Auto update lnkPersonToKubernetes'][] = $item->Get('friendlyname');
}
while($item = $ingress->Fetch()) {
$c = $this->Add($list, $item->GetKey());
$item->Set('person_list', $c);
$item->DBUpdate();
$aReport['Auto update lnkPersonToKubernetes'][] = $item->Get('friendlyname');
}
return $aReport;
}
public function Process($iTimeLimit)
{
$aReport = array();
if (MetaModel::IsValidClass('Deployment') || MetaModel::IsValidClass('Ingress'))
{
$iAutoCloseDelay = 3 * $this->GetPeriodicity();
$oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT CMDBChangeOpCreate WHERE objclass='lnkContactToApplicationSolution' AND date >= DATE_SUB(NOW(), INTERVAL $iAutoCloseDelay SECOND)"));
$oSet->OptimizeColumnLoad(array()); // Optimize the query, we'll do a reload for each ticket to actually process but it's peanuts
while ((time() < $iTimeLimit) && $oToUpdate = $oSet->Fetch())
{
$key = $oToUpdate->Get('objkey');
$lnk = MetaModel::GetObject('lnkContactToApplicationSolution', $key);
$appid = $lnk->Get('applicationsolution_id');
$aReport = $this->Update($appid, $aReport);
}
$oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT CMDBChangeOpDelete WHERE objclass='lnkContactToApplicationSolution' AND date >= DATE_SUB(NOW(), INTERVAL $iAutoCloseDelay SECOND)"));
$oSet->OptimizeColumnLoad(array()); // Optimize the query, we'll do a reload for each ticket to actually process but it's peanuts
while ((time() < $iTimeLimit) && $oToUpdate = $oSet->Fetch())
{
$key = $oToUpdate->Get('fname');
$appid = explode(" ", $key)[1];
$aReport = $this->Update($appid, $aReport);
}
}
$aStringReport = array();
foreach ($aReport as $sOperation => $aTicketRefs)
{
if (count($aTicketRefs) > 0)
{
$aStringReport[] = $sOperation.': '.count($aTicketRefs).' {'.implode(', ', $aTicketRefs).'}';
}
}
if (count($aStringReport) == 0)
{
return "No lnkPersonToKubernetes to process";
echo "No lnkPersonToKubernetes to process";
}
else
{
return "Some Kubernetes Obj were updated - ".implode('; ', $aStringReport);
echo "Some Kubernetes Obj were updated - ".implode('; ', $aStringReport);
}
}
}
?>