|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class APIEnvironment extends APINoun { |
|
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @var array |
|
7
|
|
|
*/ |
|
8
|
|
|
private static $allowed_actions = array( |
|
9
|
|
|
'ping', |
|
10
|
|
|
'deploy' |
|
11
|
|
|
); |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @param \SS_HTTPRequest $request |
|
15
|
|
|
* @return \SS_HTTPResponse |
|
16
|
|
|
*/ |
|
17
|
|
|
public function index(\SS_HTTPRequest $request) { |
|
18
|
|
|
if(!$this->record->canView($this->getMember())) { |
|
19
|
|
|
return $this->message('You are not authorized to view this environment', 403); |
|
20
|
|
|
} |
|
21
|
|
|
switch($request->httpMethod()) { |
|
22
|
|
|
case 'GET': |
|
23
|
|
|
$href = Director::absoluteURL($this->record->Project()->APILink($this->record->Name)); |
|
24
|
|
|
return $this->getAPIResponse(array( |
|
25
|
|
|
"name" => $this->record->Name, |
|
26
|
|
|
"project" => $this->record->Project()->Name, |
|
27
|
|
|
"href" => $href, |
|
28
|
|
|
"created" => $this->record->Created, |
|
29
|
|
|
"last-edited" => $this->record->LastEdited, |
|
30
|
|
|
|
|
31
|
|
|
// Stolen from https://github.com/kevinswiber/siren spec |
|
32
|
|
|
"actions" => array( |
|
33
|
|
|
array( |
|
34
|
|
|
"name" => "deploy", |
|
35
|
|
|
"method" => "POST", |
|
36
|
|
|
"href" => "$href/deploy", |
|
37
|
|
|
"type" => "application/json", |
|
38
|
|
|
"fields" => array( |
|
39
|
|
|
array("name" => "release", "type" => "text"), |
|
40
|
|
|
), |
|
41
|
|
|
) |
|
42
|
|
|
) |
|
43
|
|
|
)); |
|
44
|
|
|
default: |
|
45
|
|
|
return $this->message('API not found', 404); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param \SS_HTTPRequest $request |
|
51
|
|
|
* @return \SS_HTTPResponse |
|
52
|
|
|
*/ |
|
53
|
|
View Code Duplication |
public function ping(\SS_HTTPRequest $request) { |
|
|
|
|
|
|
54
|
|
|
if(!$this->record->canView($this->getMember())) { |
|
55
|
|
|
return $this->message('You are not authorized to do that on this environment', 403); |
|
56
|
|
|
} |
|
57
|
|
|
switch($request->httpMethod()) { |
|
58
|
|
|
case 'GET': |
|
59
|
|
|
return $this->getPing($this->getRequest()->param('ID')); |
|
60
|
|
|
case 'POST': |
|
61
|
|
|
return $this->createPing(); |
|
62
|
|
|
default: |
|
63
|
|
|
return $this->message('API not found', 404); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @deprecated 2.0.0 - moved to DeployDispatcher |
|
69
|
|
|
* |
|
70
|
|
|
* @param \SS_HTTPRequest $request |
|
71
|
|
|
* @return \SS_HTTPResponse |
|
72
|
|
|
*/ |
|
73
|
|
View Code Duplication |
public function deploy(\SS_HTTPRequest $request) { |
|
|
|
|
|
|
74
|
|
|
if(!$this->record->canView($this->getMember())) { |
|
75
|
|
|
return $this->message('You are not authorized to do that on this environment', 403); |
|
76
|
|
|
} |
|
77
|
|
|
switch($request->httpMethod()) { |
|
78
|
|
|
case 'GET': |
|
79
|
|
|
return $this->getDeploy($this->getRequest()->param('ID')); |
|
|
|
|
|
|
80
|
|
|
case 'POST': |
|
81
|
|
|
return $this->createDeploy(); |
|
|
|
|
|
|
82
|
|
|
default: |
|
83
|
|
|
return $this->message('API not found', 404); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
|
|
public function Link() { |
|
91
|
|
|
return Controller::join_links( |
|
92
|
|
|
$this->parent->Link(), |
|
93
|
|
|
$this->record->Project()->Name, |
|
94
|
|
|
$this->record->Name |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return SS_HTTPResponse |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function showRecord() { |
|
102
|
|
|
return $this->getAPIResponse($this->record->toMap()); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @return SS_HTTPResponse |
|
107
|
|
|
*/ |
|
108
|
|
|
protected function createPing() { |
|
109
|
|
|
if(!$this->record->canDeploy($this->getMember())) { |
|
110
|
|
|
return $this->message('You are not authorized to do that on this environment', 403); |
|
111
|
|
|
} |
|
112
|
|
|
$ping = DNPing::create(); |
|
113
|
|
|
$ping->EnvironmentID = $this->record->ID; |
|
114
|
|
|
$ping->write(); |
|
115
|
|
|
$ping->start(); |
|
116
|
|
|
|
|
117
|
|
|
$location = Director::absoluteBaseURL() . $this->Link() . '/ping/' . $ping->ID; |
|
118
|
|
|
$output = array( |
|
119
|
|
|
'message' => 'Ping queued as job ' . $ping->ResqueToken, |
|
120
|
|
|
'href' => $location, |
|
121
|
|
|
); |
|
122
|
|
|
|
|
123
|
|
|
$response = $this->getAPIResponse($output); |
|
124
|
|
|
$response->setStatusCode(201); |
|
125
|
|
|
$response->addHeader('Location', $location); |
|
126
|
|
|
return $response; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param int $ID |
|
131
|
|
|
* @return SS_HTTPResponse |
|
132
|
|
|
*/ |
|
133
|
|
View Code Duplication |
protected function getPing($ID) { |
|
|
|
|
|
|
134
|
|
|
$ping = DNPing::get()->byID($ID); |
|
135
|
|
|
if(!$ping) { |
|
136
|
|
|
return $this->message('Ping not found', 404); |
|
137
|
|
|
} |
|
138
|
|
|
$output = array( |
|
139
|
|
|
'status' => $ping->ResqueStatus(), |
|
140
|
|
|
'message' => $ping->LogContent() |
|
141
|
|
|
); |
|
142
|
|
|
|
|
143
|
|
|
return $this->getAPIResponse($output); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @deprecated 2.0.0 - moved to DeployDispatcher |
|
148
|
|
|
* |
|
149
|
|
|
* @return SS_HTTPResponse |
|
150
|
|
|
*/ |
|
151
|
|
|
protected function createDeploy() { |
|
152
|
|
|
if(!$this->record->canDeploy($this->getMember())) { |
|
153
|
|
|
return $this->message('You are not authorized to do that on this environment', 403); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
$reqBody = $this->getRequestBody(); |
|
157
|
|
|
|
|
158
|
|
|
if($reqBody === null) { |
|
159
|
|
|
return $this->message('the request body did not contain a valid JSON object.', 400); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
if(empty($reqBody['release'])) { |
|
163
|
|
|
return $this->message('deploy requires a {"release": "sha1"} in the body of the request.', 400); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
$strategy = new DeploymentStrategy($this->record, array( |
|
|
|
|
|
|
167
|
|
|
'sha' => $reqBody['release'] |
|
168
|
|
|
)); |
|
169
|
|
|
$deploy = $strategy->createDeployment(); |
|
170
|
|
|
$deploy->start(); |
|
|
|
|
|
|
171
|
|
|
$location = Director::absoluteBaseURL() . $this->Link() . '/deploy/' . $deploy->ID; |
|
172
|
|
|
$output = array( |
|
173
|
|
|
'message' => 'Deploy queued as job ' . $deploy->ResqueToken, |
|
174
|
|
|
'href' => $location, |
|
175
|
|
|
); |
|
176
|
|
|
$response = $this->getAPIResponse($output); |
|
177
|
|
|
$response->setStatusCode(201); |
|
178
|
|
|
$response->addHeader('Location', $location); |
|
179
|
|
|
return $response; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @deprecated 2.0.0 - moved to DeployDispatcher |
|
184
|
|
|
* |
|
185
|
|
|
* @param int $id |
|
186
|
|
|
* @return SS_HTTPResponse |
|
187
|
|
|
*/ |
|
188
|
|
View Code Duplication |
protected function getDeploy($id) { |
|
|
|
|
|
|
189
|
|
|
$deploy = DNDeployment::get()->byID($id); |
|
190
|
|
|
if(!$deploy) { |
|
191
|
|
|
return $this->message('Deploy not found', 404); |
|
192
|
|
|
} |
|
193
|
|
|
$output = array( |
|
194
|
|
|
'status' => $deploy->ResqueStatus(), |
|
195
|
|
|
'message' => $deploy->LogContent() |
|
196
|
|
|
); |
|
197
|
|
|
|
|
198
|
|
|
return $this->getAPIResponse($output); |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString.