-
Notifications
You must be signed in to change notification settings - Fork 1
/
SaltStackService.php
137 lines (105 loc) · 2.77 KB
/
SaltStackService.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
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
<?php
class SaltStackService
{
/**
* Send a post to saltStack api
*
* @return array
*/
public function curlSaltApi($host, $function, $args) {
$ch = curl_init('http://salt:8000/run');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json')
);
$request_body = '{
"client": "local",
"tgt": "'.$host.'",
"fun": "'.$function.'",
"arg": '.$args.',
"username": "saltdev",
"password": "123",
"eauth": "pam"
}';
curl_setopt($ch, CURLOPT_POSTFIELDS, $request_body);
$result = curl_exec($ch);
$result = json_decode($result, true);
return $result['return'][0][$host];
}
/**
* Verify if a file exists in determined server
*
* @return bool
*/
public function fileExists ($host, $file_path)
{
$function = "file.file_exists";
$args = '"'.$file_path.'"';
return $this->curlSaltApi($host, $function, $args);
}
/**
* Copy a file in a remote server
*
* @return bool
*/
public function fileCopy ($host, $file_path, $copy_file_path)
{
$function = "file.copy";
$args = '[
"'.$file_path.'",
"'.$copy_file_path.'"
]';
return $this->curlSaltApi($host, $function, $args);
}
/**
* Move a file in a remote server
*
* @return bool
*/
public function fileMove ($host, $file_path, $copy_file_path)
{
$function = "file.move";
$args = '[
"'.$file_path.'",
"'.$copy_file_path.'"
]';
return $this->curlSaltApi($host, $function, $args);
}
/**
* Write to a file in a remote server, if it does not exist, create it
*
* @return bool
*/
public function fileWrite ($host, $file_path, $content)
{
$function = "file.write";
$args = '[
"'.$file_path.'",
'.json_encode($content).'
]';
return $this->curlSaltApi($host, $function, $args);
}
/**
* Reload nginx Configs
*
* @return bool
*/
public function nginxReload ($host)
{
$function = "nginx.signal";
$args = '"reload"';
return $this->curlSaltApi($host, $function, $args);
}
/**
* Run arbitrary shell command
*
* @return bool
*/
public function runCmd ($host, $args)
{
$function = "cmd.run";
return $this->curlSaltApi($host, $function, $args);
}
}