Skip to content

Commit e57eac2

Browse files
author
Slavey Karadzhov
committedNov 13, 2015
Added --safe flag to the installApp high-level command.
This way on the client side we can check if it is safe to deploy a new version.
1 parent f5ee3d7 commit e57eac2

File tree

3 files changed

+80
-10
lines changed

3 files changed

+80
-10
lines changed
 

‎README.md

+21
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,27 @@ php bin/zs-client.phar installApp --zpk="<location-of-the-zpk-file>" \
154154
--baseUri="<baseUri>" \
155155
--userParams="APPLICATION_ENV=staging&DB_TYPE=mysql"
156156
```
157+
158+
#### Safe Package Deployment ####
159+
If you deploy a new version of your zpk if the old version is still being deployed then
160+
this can lead to unpredictable results. In order to prevent this you can use the `--safe` flag.
161+
If it is present zs-client will check if there is a current deployment going on for this app
162+
and will exit if that is the case.
163+
164+
Example:
165+
```
166+
php bin/zs-client.phar installApp --zpk="<location-of-the-zpk-file>" --safe ...
167+
```
168+
169+
If you want to be safe AND want to wait for the previous deployment to finish then
170+
you can use the --safe and --wait flags together.
171+
172+
Example:
173+
```
174+
php bin/zs-client.phar installApp --zpk="<location-of-the-zpk-file>" --safe --wait ...
175+
```
176+
177+
157178
#### Deploy Multiple Packages
158179
If you use the composer integration then packZpk can create multiple packages, instead of one. Below is a suggestion how you can
159180
deploy these newly created packages in the correct order.

‎module/Client/config/module.config.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,11 @@
119119
),
120120
'installApp' => array(
121121
'options' => array(
122-
'route' => 'installApp --zpk= --baseUri= [--userParams=] [--userAppName=] [--createVhost=] [--defaultServer=] [--ignoreFailures=] [--target=] [--zsurl=] [--zskey=] [--zssecret=] [--zsversion=] [--http=] [--wait]',
122+
'route' => 'installApp --zpk= --baseUri= [--userParams=] [--userAppName=] [--createVhost=] [--defaultServer=] [--ignoreFailures=] [--target=] [--zsurl=] [--zskey=] [--zssecret=] [--zsversion=] [--http=] [--wait] [--safe]',
123123
'defaults' => array(
124124
'controller' => 'webapi-app-controller',
125-
'action' => 'install'
125+
'action' => 'install',
126+
'safe' => false,
126127
),
127128
'group'=>'high-level',
128129
'async' => true,
@@ -146,6 +147,7 @@
146147
array('--http', 'Allows you to set http connection parameters'),
147148
array('--wait', 'If this option is present then the client will wait until the operation finishes successfully on all servers.'.
148149
'By default this option is not present which means that the client will return results and exit as soon as the server has reported that it started to handle the task.'),
150+
array('--safe', 'Deploys the application only when there is not current deployment running for this application'),
149151
),
150152
'arrays' => array(
151153
'userParams',

‎module/Client/src/Client/Controller/AppController.php

+55-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ public function installAction()
1919
$userParams = $this->params('userParams', array());
2020
$appName = $this->params('userAppName');
2121
$appId = 0;
22+
$appStatus = null;
2223
$wait = $this->params('wait');
24+
$safe = $this->params('safe');
2325

2426
$apiManager = $this->serviceLocator->get('zend_server_api');
2527
$zpkService = $this->serviceLocator->get('zpk');
@@ -50,6 +52,7 @@ public function installAction()
5052
foreach ($response->responseData->applicationsList->applicationInfo as $appElement) {
5153
if ($appElement->userAppName == $appName) {
5254
$appId = $appElement->id;
55+
$appStatus = sprintf("%s", $appElement->status);
5356
break;
5457
}
5558
}
@@ -74,24 +77,68 @@ public function installAction()
7477
if ($wait) {
7578
$xml = new \SimpleXMLElement($response->getBody());
7679
$appId = $xml->responseData->applicationInfo->id;
80+
$response = $this->repeater()->doUntil(array($this, 'onWaitInstall'),
81+
array(
82+
'appId'=>sprintf("%s", $appId)
83+
));
7784
}
78-
} else {
79-
// otherwise update the application
85+
86+
return $response;
87+
}
88+
89+
if ($safe && !$wait && $this->isAppDeploymentRunning($appStatus)) {
90+
throw new \Zend\Mvc\Exception\RuntimeException(
91+
"Previous version is still being deployed. Use the --wait flag if you want to wait"
92+
);
93+
}
94+
95+
$updateFunc = function ($wait) use ($appId, $zpk, $userParams) {
96+
// update the application
8097
$response = $this->forward()->dispatch('webapi-api-controller', array(
8198
'action' => 'applicationUpdate',
8299
'appId' => $appId,
83100
'appPackage' => $zpk,
84101
'userParams' => $userParams,
85102
));
103+
104+
if ($wait) {
105+
$response = $this->repeater()->doUntil(array($this, 'onWaitInstall'),
106+
array(
107+
'appId'=>sprintf("%s", $appId),
108+
));
109+
}
110+
111+
return $response;
112+
};
113+
114+
if (!$safe || ($safe && !$this->isAppDeploymentRunning($appStatus))) {
115+
return $updateFunc($wait);
86116
}
87-
88-
if ($wait) {
89-
$response = $this->repeater()->doUntil(array($this, 'onWaitInstall'), array('appId'=>sprintf("%s", $appId)));
90-
}
91-
92-
return $response;
117+
118+
// if safe and wait
119+
return $this->repeater()->doUntil(function () use ($apiManager, $updateFunc, $appId, $wait) {
120+
$response = $apiManager->applicationGetStatus(array('applications'=> $appId));
121+
foreach ($response->responseData->applicationsList->applicationInfo as $appElement) {
122+
if (sprintf("%s", $appElement->id) == $appId) {
123+
$appStatus = sprintf("%s", $appElement->status);
124+
break;
125+
}
126+
}
127+
128+
if (!$this->isAppDeploymentRunning($appStatus)) {
129+
return $updateFunc($wait);
130+
}
131+
});
93132
}
94133

134+
public function isAppDeploymentRunning($appStatus)
135+
{
136+
return !(
137+
in_array($appStatus, array("error", "deployed", "notExists")) ||
138+
preg_match('/(\w+)Error$/', $appStatus)
139+
);
140+
}
141+
95142
/**
96143
* Returns response if the action finished as expected
97144
* @param AbstractActionController $controller

0 commit comments

Comments
 (0)
Please sign in to comment.