1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* DNCreateEnvironment |
5
|
|
|
* |
6
|
|
|
* @property string Data |
7
|
|
|
* @property string ResqueToken |
8
|
|
|
* @property string Status |
9
|
|
|
* @property bool IsInitialEnvironment |
10
|
|
|
* |
11
|
|
|
* @method DNProject Project() |
12
|
|
|
* @property int $ProjectID |
13
|
|
|
* @method Member Creator() |
14
|
|
|
* @property int $CreatorID |
15
|
|
|
*/ |
16
|
|
|
class DNCreateEnvironment extends DataObject { |
|
|
|
|
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private static $db = array( |
22
|
|
|
'Data' => 'Text', |
23
|
|
|
'ResqueToken' => 'Varchar(255)', |
24
|
|
|
"Status" => "Enum('Queued, Started, Finished, Failed, n/a', 'n/a')", |
25
|
|
|
'IsInitialEnvironment' => 'Boolean', |
26
|
|
|
); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private static $has_one = array( |
32
|
|
|
'Project' => 'DNProject', |
33
|
|
|
'Creator' => 'Member' |
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* |
38
|
|
|
* @param int $int |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
|
View Code Duplication |
public static function map_resque_status($int) { |
|
|
|
|
42
|
|
|
$remap = array( |
43
|
|
|
Resque_Job_Status::STATUS_WAITING => "Queued", |
44
|
|
|
Resque_Job_Status::STATUS_RUNNING => "Running", |
45
|
|
|
Resque_Job_Status::STATUS_FAILED => "Failed", |
46
|
|
|
Resque_Job_Status::STATUS_COMPLETE => "Complete", |
47
|
|
|
false => "Invalid", |
48
|
|
|
); |
49
|
|
|
return $remap[$int]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public function Name() { |
56
|
|
|
$data = unserialize($this->Data); |
57
|
|
|
return !empty($data['Name']) ? Convert::raw2xml($data['Name']) : ''; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public function Link() { |
64
|
|
|
return Controller::join_links($this->Project()->Link(), 'createenv', $this->ID); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public function LogLink() { |
71
|
|
|
return $this->Link() . '/log'; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return boolean |
76
|
|
|
*/ |
77
|
|
|
public function canView($member = null) { |
78
|
|
|
return $this->Project()->canView($member); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Return a path to the log file. |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
protected function logfile() { |
86
|
|
|
return sprintf( |
87
|
|
|
'%s.createenv.%s.log', |
88
|
|
|
$this->Project()->Name, |
89
|
|
|
$this->ID |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return \DeploynautLogFile |
95
|
|
|
*/ |
96
|
|
|
public function log() { |
97
|
|
|
return Injector::inst()->createWithArgs('DeploynautLogFile', array($this->logfile())); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function LogContent() { |
101
|
|
|
return $this->log()->content(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Returns the status of the resque job |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
View Code Duplication |
public function ResqueStatus() { |
|
|
|
|
110
|
|
|
$status = new Resque_Job_Status($this->ResqueToken); |
|
|
|
|
111
|
|
|
$statusCode = $status->get(); |
112
|
|
|
// The Resque job can no longer be found, fallback to the DNDeployment.Status |
113
|
|
|
if($statusCode === false) { |
114
|
|
|
// Translate from the DNDeployment.Status to the Resque job status for UI purposes |
115
|
|
|
switch($this->Status) { |
116
|
|
|
case 'Finished': |
117
|
|
|
return 'Complete'; |
118
|
|
|
case 'Started': |
119
|
|
|
return 'Running'; |
120
|
|
|
default: |
121
|
|
|
return $this->Status; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
return self::map_resque_status($statusCode); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Start a resque job for this creation. |
129
|
|
|
* |
130
|
|
|
* @return string Resque token |
131
|
|
|
*/ |
132
|
|
|
protected function enqueueCreation() { |
133
|
|
|
$project = $this->Project(); |
134
|
|
|
$log = $this->log(); |
135
|
|
|
|
136
|
|
|
$args = array( |
137
|
|
|
'createID' => $this->ID, |
138
|
|
|
'logfile' => $this->logfile(), |
139
|
|
|
'projectName' => $project->Name |
140
|
|
|
); |
141
|
|
|
|
142
|
|
|
if(!$this->CreatorID) { |
143
|
|
|
$this->CreatorID = Member::currentUserID(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if($this->CreatorID) { |
147
|
|
|
$creator = $this->Creator(); |
148
|
|
|
$message = sprintf( |
149
|
|
|
'Environment creation for project %s initiated by %s (%s), with IP address %s', |
150
|
|
|
$project->Name, |
151
|
|
|
$creator->getName(), |
152
|
|
|
$creator->Email, |
153
|
|
|
Controller::curr()->getRequest()->getIP() |
154
|
|
|
); |
155
|
|
|
$log->write($message); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return Resque::enqueue('create', 'CreateEnvJob', $args, true); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function start() { |
162
|
|
|
$log = $this->log(); |
163
|
|
|
$token = $this->enqueueCreation(); |
164
|
|
|
$this->ResqueToken = $token; |
165
|
|
|
$this->Status = 'Queued'; |
166
|
|
|
$this->write(); |
167
|
|
|
|
168
|
|
|
$message = sprintf('Environment creation queued as job %s', $token); |
169
|
|
|
$log->write($message); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function createEnvironment() { |
173
|
|
|
$backend = $this->getBackend(); |
174
|
|
|
if($backend) { |
175
|
|
|
return $backend->createEnvironment($this); |
176
|
|
|
} |
177
|
|
|
throw new Exception("Unable to find backend."); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Fetches the EnvironmentCreateBackend based on the EnvironmentType saved to this job. |
182
|
|
|
* |
183
|
|
|
* @return EnvironmentCreateBackend|null |
184
|
|
|
* @throws Exception |
185
|
|
|
*/ |
186
|
|
|
public function getBackend() { |
187
|
|
|
$data = unserialize($this->Data); |
188
|
|
|
if(isset($data['EnvironmentType']) && class_exists($data['EnvironmentType'])) { |
189
|
|
|
$env = Injector::inst()->get($data['EnvironmentType']); |
190
|
|
|
if($env instanceof EnvironmentCreateBackend) { |
191
|
|
|
return $env; |
192
|
|
|
} else { |
193
|
|
|
throw new Exception("Invalid backend: " . $data['EnvironmentType']); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
return null; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
|
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
.