--- /dev/null
+<?php
+
+abstract class Command {
+
+ const UUID_FILE = "/opt/allo/appliance.uuid";
+
+ /** @var AlloServeur */
+ private $alloServeur;
+
+ protected $allo_serveur_url;
+
+ protected $login;
+ protected $password;
+
+ public function __construct($allo_serveur_url,$login = '',$password = ''){
+ $this->allo_serveur_url = $allo_serveur_url;
+ $this->login = $login;
+ $this->password = $password;
+ }
+
+ abstract public function go();
+ abstract public function getDescription();
+
+ public function needLogin(){
+ return false;
+ }
+
+ public function login($login){
+ if ($login == ""){
+ throw new Exception("Cette fonction nécessite une connexion");
+ }
+ $password = $this->readlineWithoutEcho("Mot de passe: ");
+ $this->getAlloServeur()->login($login,$password);
+ }
+
+
+ protected function readlineWithoutEcho($prompt){
+ echo $prompt;
+ system('stty -echo');
+ $fp = fopen("php://stdin","r");
+ $line = rtrim(fgets($fp, 1024));
+ system('stty echo');
+ echo "\n";
+ return $line;
+ }
+
+ protected function readline($prompt){
+ echo $prompt;
+ $fp = fopen("php://stdin","r");
+ $line = rtrim(fgets($fp, 1024));
+ return $line;
+ }
+
+ protected function getAlloServeur(){
+ if (! $this->alloServeur){
+ $this->alloServeur = new AlloServeur($this->allo_serveur_url);
+ }
+ return $this->alloServeur;
+ }
+
+ protected function getAlloUUID(){
+ if (file_exists(self::UUID_FILE)){
+ return file_get_contents(self::UUID_FILE);
+ } else {
+ return false;
+ }
+ }
+ protected function setAlloUUID($appliance_uuid){
+ file_put_contents(self::UUID_FILE,$appliance_uuid);
+ }
+
+ protected function verifInit(){
+ if (! $this->getAlloUUID()){
+ throw new Exception("Allo n'est pas initialisé, cette opération n'est pas possible");
+ }
+ }
+
+ protected function getInfoFromInstance($fqdn){
+ $content = file_get_contents($fqdn . "/api/rest/allo/version");
+
+ if (!$content) {
+ throw new Exception("L'instance ($fqdn) ne répond pas)");
+ }
+ $content = json_decode($content, true);
+ if (!$content) {
+ throw new Exception("La réponse de $fqdn n'est pas compréhensible");
+ }
+ if (! $content['produit']) {
+ throw new Exception("[ERREUR] produit non-trouvée sur le FQDN");
+ }
+ if (! $content['version']) {
+ throw new Exception("[ERREUR] version non trouvée sur le FQDN");
+ }
+
+ if (! $this->getAlloServeur()->productExitsByName($content['produit'])){
+ throw new Exception("Le produit {$content['produit']} n'existe pas.");
+ }
+
+ if (! $this->getAlloServeur()->versionExistsByValue($content['produit'],$content['version'])){
+ throw new Exception("La version {$content['version']} n'existe pas pour le produit {$content['produit']}.");
+ }
+ return $content;
+ }
+
+
+}
\ No newline at end of file