@@ -19,7 +19,9 @@ public function installAction()
19
19
$ userParams = $ this ->params ('userParams ' , array ());
20
20
$ appName = $ this ->params ('userAppName ' );
21
21
$ appId = 0 ;
22
+ $ appStatus = null ;
22
23
$ wait = $ this ->params ('wait ' );
24
+ $ safe = $ this ->params ('safe ' );
23
25
24
26
$ apiManager = $ this ->serviceLocator ->get ('zend_server_api ' );
25
27
$ zpkService = $ this ->serviceLocator ->get ('zpk ' );
@@ -50,6 +52,7 @@ public function installAction()
50
52
foreach ($ response ->responseData ->applicationsList ->applicationInfo as $ appElement ) {
51
53
if ($ appElement ->userAppName == $ appName ) {
52
54
$ appId = $ appElement ->id ;
55
+ $ appStatus = sprintf ("%s " , $ appElement ->status );
53
56
break ;
54
57
}
55
58
}
@@ -74,24 +77,68 @@ public function installAction()
74
77
if ($ wait ) {
75
78
$ xml = new \SimpleXMLElement ($ response ->getBody ());
76
79
$ appId = $ xml ->responseData ->applicationInfo ->id ;
80
+ $ response = $ this ->repeater ()->doUntil (array ($ this , 'onWaitInstall ' ),
81
+ array (
82
+ 'appId ' =>sprintf ("%s " , $ appId )
83
+ ));
77
84
}
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
80
97
$ response = $ this ->forward ()->dispatch ('webapi-api-controller ' , array (
81
98
'action ' => 'applicationUpdate ' ,
82
99
'appId ' => $ appId ,
83
100
'appPackage ' => $ zpk ,
84
101
'userParams ' => $ userParams ,
85
102
));
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 );
86
116
}
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
+ });
93
132
}
94
133
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
+
95
142
/**
96
143
* Returns response if the action finished as expected
97
144
* @param AbstractActionController $controller
0 commit comments