PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/oiclient/lib/images/listOiSystemsOppClass.php

http://openirudi.googlecode.com/
PHP | 334 lines | 260 code | 62 blank | 12 comment | 40 complexity | e0f3533e958e8a6a0d297fd59bbb3ff9 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0
  1. <?php
  2. class listOiSystemsOppClass {
  3. private $oisystems=array();
  4. //private $hw;
  5. private $sclient;
  6. function __construct() {
  7. $this->set_oisystems();
  8. //$this->hw=$hw;
  9. }
  10. function __get($propertyName) {
  11. try {
  12. if (property_exists('listOiSystemsOppClass', $propertyName)) {
  13. if (!isset($this->$propertyName)) {
  14. $this->__set($propertyName, '');
  15. }
  16. return $this->$propertyName;
  17. }
  18. throw new Exception("Invalid property name \"{$propertyName}\" in listOiSystemsOppClass");
  19. }catch(Exception $e) {
  20. exceptionHandlerClass::saveError($e->getMessage());
  21. }
  22. }
  23. function __set($propertyName, $value) {
  24. try {
  25. if (!property_exists('listOiSystemsOppClass', $propertyName)) {
  26. throw new Exception("Invalid property value \"{$propertyName}\" in listOiSystemsOppClass ");
  27. }
  28. if (method_exists($this, 'set_' . $propertyName)) {
  29. call_user_func(array($this, 'set_' . $propertyName), $value);
  30. } else {
  31. throw new Exception("*Invalid property value \"{$propertyName}\" in listOiSystemsOppClass ");
  32. }
  33. } catch (Exception $e) {
  34. exceptionHandlerClass::saveError($e->getMessage());
  35. }
  36. }
  37. function set_oisystems() {
  38. $hw=systemOppClass::getComputer();
  39. $this->oisystems=array();
  40. if(!isset($this->oisystems) || empty($this->oisystems)) {
  41. foreach($hw->listDisks->disks as $diskName => $disk) {
  42. if(is_array($disk->partitions)) {
  43. foreach ($disk->partitions as $partition) {
  44. if(!is_null($partition->fileSystem)) {
  45. if($partition->fileSystem->isOIFileSystem==true) {
  46. $this->oisystems[$partition->partitionName]=$partition->fileSystem;
  47. }
  48. }
  49. }
  50. }
  51. }
  52. }
  53. }
  54. function set_sclient($reconnect=false){
  55. if(empty ($this->sclient) || $reconnect==true){
  56. $this->sclient=ImageServerOppClass::getClientVersion();
  57. if($this->sclient !== false ){
  58. $cmd = str_replace('$user', $this->sclient['user'], sfConfig::get('app_oisystem_changePwd'));
  59. $cmd = str_replace('$newPwd',$this->sclient['password'], $cmd);
  60. $cmd = str_replace('$newMd5', md5($this->sclient['password']), $cmd);
  61. $re = executeClass::execute($cmd);
  62. if($re['return']!=0){
  63. exceptionHandlerClass::saveMessage("ERROR setting new password");
  64. exceptionHandlerClass::saveMessage("cmd== $cmd ");
  65. exceptionHandlerClass::saveMessage("re==<pre>".print_r($re,1)."</pre>");
  66. }
  67. }
  68. }
  69. }
  70. function activeOiSystem(){
  71. $this->__get('oisystems');
  72. $cmd=sfConfig::get('app_oisystem_active');
  73. $re = executeClass::execute($cmd);
  74. if($re['return']!=0){
  75. exceptionHandlerClass::saveMessage("ERROR shearcing active Openirudi partition");
  76. exceptionHandlerClass::saveMessage("cmd== $cmd ");
  77. exceptionHandlerClass::saveMessage("re==<pre>".print_r($re,1)."</pre>");
  78. }
  79. $out=implode("\n",$re['output']);
  80. if(strpos($out, '!@@@') ===false ){
  81. return false;
  82. }else{
  83. $p=explode('!@@@',$out);
  84. if($p[1]=='null' || $p[1]=='nul' ){
  85. return null;
  86. }else{
  87. $ois=array_keys($this->oisystems);
  88. if(isset($this->oisystems[$p[1]])){
  89. return $p[1];
  90. }else{
  91. exceptionHandlerClass::saveError("ERROR searching active oisystem");
  92. return null;
  93. }
  94. }
  95. }
  96. return false;
  97. }
  98. function activeVersion() {
  99. $inf=manageIniFilesClass::readIniFile(sfConfig::get('app_path_oiclient'));
  100. if($inf !== false && isset($inf['version'])){
  101. $version=$inf['version'];
  102. }else{
  103. $version=0;
  104. }
  105. return $version;
  106. }
  107. function installOisystem($partitionName){
  108. $cmd=str_replace('$partitionName',$partitionName,sfConfig::get('app_oisystem_install'));
  109. $re=executeClass::executeProc($cmd);
  110. if($re===false ){
  111. exceptionHandlerClass::saveMessage("An error ocurred an Openirudi system has not been installed" );
  112. exceptionHandlerClass::saveMessage("cmd $cmd");
  113. return false;
  114. }
  115. exceptionHandlerClass::saveMessage("Openirudi system has been installed" );
  116. return true;
  117. }
  118. function createOiSystem($partition) {
  119. $hw=systemOppClass::getComputer();
  120. if(is_array($partition['size']) && isset ($partition['size']['size']) ){
  121. $partition['sizeSectors']=unitsClass::size2sector($partition['size']);
  122. }elseif(is_numeric ($partition['size']) ){
  123. $partition['sizeSectors']=$partition['size'];
  124. }elseif(strpos($partition['size'],'%') !== false ){
  125. $partition['size']=str_replace('%', '', $partition['size']);
  126. $partition['sizeSectors']=floor($partition['size'] * $hw->listDisks->disks[$partition['diskName']]->maxSectors);
  127. }else{
  128. exceptionHandlerClass::saveError("ERROR calculating partition size");
  129. return false;
  130. }
  131. if(!isset($partition['ptype'])){
  132. $partition['ptype']='primary';
  133. }
  134. if($partition['ptype']=='logical'){
  135. $partition['startSector']=$hw->listDisks->disks[$partition['diskName']]->newLogicPartitionStartSector( $partition['sizeSectors'] );
  136. }else{
  137. $partition['startSector']=$hw->listDisks->disks[$partition['diskName']]->newPrimaryPartitionStartSector( $partition['sizeSectors'] );
  138. }
  139. $partition['stopSector']=$partition['startSector']+$partition['sizeSectors']-1;
  140. if( $partition['stopSector'] > $hw->listDisks->disks[$partition['diskName']]->maxSectors ){
  141. $partition['stopSector']=$hw->listDisks->disks[$partition['diskName']]->maxSectors;
  142. }
  143. $cyls=floor( $partition['stopSector'] / $hw->listDisks->disks[$partition['diskName']]->cylSectors );
  144. $partition['stopSector']=$cyls* $hw->listDisks->disks[$partition['diskName']]->cylSectors-1;
  145. if(empty ($partition['stopSector'])){
  146. exceptionHandlerClass::saveError('Wrong partition size');
  147. return false;
  148. }
  149. $cmd=str_replace('$diskName','/dev/'.$partition['diskName'],sfConfig::get('app_oipartition_createOiPartition'));
  150. $cmd=str_replace('$startSector',$partition['startSector'],$cmd);
  151. $cmd=str_replace('$stopSector',$partition['stopSector'],$cmd);
  152. $cmd=str_replace('$ptype',$partition['ptype'],$cmd);
  153. $re=executeClass::executeProc($cmd);
  154. if($re===false ){
  155. exceptionHandlerClass::saveMessage("An error ocurred an Openirudi system has not been installed" );
  156. exceptionHandlerClass::saveMessage("cmd $cmd");
  157. return false;
  158. }
  159. exceptionHandlerClass::saveMessage("Openirudi system has been installed" );
  160. $hw=systemOppClass::getComputer(true);
  161. $lisOiSystems=systemOppClass::getListOisystems(true);
  162. exceptionHandlerClass::saveMessage("Make bootable all partitions");
  163. $hw->listDisks->makeBoot();
  164. }
  165. function miniLinuxInstalledOiSystems() {
  166. $r=array();
  167. foreach($this->oisystems as $name => $oisystem) {
  168. if($oisystem->isMinilinuxInstalled) {
  169. $r[]=$name;
  170. }
  171. }
  172. return $r;
  173. }
  174. function miniLinuxNotInstalledOiSystems() {
  175. $r=array();
  176. foreach($this->oisystems as $name => $oisystem) {
  177. if(!$oisystem->isMinilinuxInstalled) {
  178. $r[]=$name;
  179. }
  180. }
  181. return $r;
  182. }
  183. /*
  184. * Funtzio honetan Openirudiko partizio guztiak errebisatu eta imagin berri bat non gordeko den aukeratzen da.
  185. *
  186. */
  187. function oiSystem2DeployImage( $image ) {
  188. $result=$this->searchImage($image);
  189. if(count($this->oisystems)==0){
  190. return false;
  191. }
  192. if(is_null($result)) {
  193. $result=$this->bestOiSystem2newImage($image->size);
  194. }
  195. exceptionHandlerClass::saveMessage("Image saved on local cache ". $result->partitionName);
  196. return $result;
  197. }
  198. /*
  199. * Openirudiko partizioan imagin bat badagoen begiratu. Baldin badago openirudi Partizioaren path-a pasa
  200. */
  201. function searchImage($image) {
  202. foreach ($this->oisystems as $partitionName => $oiSystem ) {
  203. $listSavedImages=$oiSystem->listSavedImages();
  204. if( isset($listSavedImages[$image->id]) ) {
  205. if ( $oiSystem->free + $listSavedImages[$image->id] >= $image->size ) {
  206. return $oiSystem;
  207. }else {
  208. $oiSystem->removeImageFiles($image->id);
  209. }
  210. }
  211. }
  212. return null;
  213. }
  214. function bestOiSystem2newImage($imageSize) {
  215. foreach ($this->oisystems as $partitionName => $oiSystem ) {
  216. $free[$partitionName]=$oiSystem->free;
  217. $size[$partitionName]=$oiSystem->size;
  218. }
  219. //ez dago imagina baino tokia badago
  220. asort($free,SORT_NUMERIC);
  221. foreach($free as $partitionName => $free) {
  222. if($free >= $imageSize ) {
  223. return $this->oisystems[$partitionName];
  224. }
  225. }
  226. asort($size,SORT_NUMERIC);
  227. //ez dago imagina ez dago tokirik
  228. foreach($size as $partitionName => $size) {
  229. if($size > $imageSize ) {
  230. $this->oisystems[$partitionName]->removeSize($imageSize);
  231. return $this->oisystems[$partitionName];
  232. }
  233. }
  234. exceptionHandlerClass::saveMessage("???????????????");
  235. }
  236. function executeBoot($request) {
  237. $hw= systemOppClass::getComputer();
  238. $diskName=$request->getParameter('diskName');
  239. $partitionName=$request->getParameter('partitionName');
  240. $this->partition=$hw->listDisks->disks[$diskName]->partitions[$partitionName];
  241. exceptionHandlerClass::saveError("partition:: ".$partitionName." disk::".$diskName." - ");
  242. foreach($this->listOisystems->localOisystems as $oisystem) {
  243. //$oisystem->
  244. $grubObj=new grubMenuClass($oisystem);
  245. $grubObj->addLinuxEntry( "Openirudi partition", $this->partition );
  246. break;
  247. }
  248. $this->redirect('image/index');
  249. }
  250. function imageServer($address=''){
  251. if(!empty($address)){
  252. foreach ($this->oisystems as $pname => $oisystem ){
  253. $address=$oisystem->imageServer($address);
  254. }
  255. }
  256. return $address;
  257. }
  258. function setConfProperty($property, $value){
  259. if(is_null($this->activeOiSystem())){
  260. sfYamlOI::saveKey($property, $value);
  261. }
  262. foreach ($this->oisystems as $pname => $oisystem ){
  263. $r=$oisystem->setConfProperty($property, $value);
  264. if($r===false){
  265. }
  266. }
  267. }
  268. function getConfProperty($property){
  269. $v=sfYamlOI::readKey($property);
  270. return $v;
  271. }
  272. }