Skip to content
Snippets Groups Projects
Select Git revision
  • 0826815daf43b9663410c0db95534ec5f350c449
  • master default protected
  • Legacy_Php7
3 results

rollout_awx.php

Blame
  • rollout_awx.php 4.90 KiB
    <?php
    
    /**
     * 
     * Rollout plugin - awx
     * 
     * Run an Https POST request to AWX
     *
     * @author axel
     */
    class rollout_awx extends rollout_base {
    
        /**
         * check requirements if the plugin could work
         */
        public function checkRequirements() {
            // no specific checks needed ... always true
            return true;
        }
    
        /**
         * make an http get request and return the response body
         * it is called by _makeRequest
         * $aRequest contains subkeys
         * - url
         * - method; one of GET|POST|PUT|DELETE
         * - postdata; for POST only
         * 
         * @param array   $aRequest   arrayurl for Foreman API
         * @return string
         */
        protected function _httpRequest($aRequest=false, $iTimeout = 5) {
    
            if (!function_exists("curl_init")) {
                die("ERROR: PHP CURL module is not installed.");
            }
            $aConfig=$this->getConfig();
            
            
            $ch = curl_init($aConfig['url'].$aRequest['url']);
    
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $aRequest['method']);
            if ($this->_aRequest['method']==='POST'){
                curl_setopt($ch, CURLOPT_POSTFIELDS, $aRequest['postdata']);
            }
    
            if ($aConfig['user']){
                curl_setopt($ch, CURLOPT_USERPWD, $aConfig['user'].':'.$aConfig['password']);
            }
    
            if (isset($aConfig['ignore-ssl-error']) && $aConfig['ignore-ssl-error']){
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            }
            
            curl_setopt($ch, CURLOPT_TIMEOUT, $iTimeout);
            curl_setopt($ch, CURLOPT_USERAGENT, 'IML Deployment :: rollout plugin awx ' . __CLASS__);
            curl_setopt($ch, CURLOPT_HEADER,1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    
            $res = curl_exec($ch);
            
            $aReturn=array('info'=>curl_getinfo($ch), 'error'=>curl_error($ch));
            curl_close($ch);
            
            $sHeader=substr($res, 0, $aReturn['info']['header_size']);
            $aReturn['header']=explode("\n", $sHeader);
            $aReturn['body']=str_replace($sHeader, "", $res);
    
            // print_r($aReturn);
            return $aReturn;
        }
    
       
      
        /**
         * get AWX Job Templates and return them as array for select box
         * [id] => array('value' => [ID], 'label' => [PLAYBOOK] [ID])
         * @return array
         */
        static public function getAwxInventories(){
            $aResponse=$this->_httpRequest(array(
                    'url'=>'/inventories/?order_by=name',
                    'method'=>'GET',
                )
            );
    
            if(!isset($aResponse['info']['http_code']) || $aResponse['info']['http_code']!==200){
                return false;
            }
    
            $aData=json_decode($aResponse['body'], 1);
            $aReturn=array();
    
            foreach ($aData['results'] as $aItem){
                $aReturn[$aItem['id']]= [
                    'value'=>$aItem['id'], 
                    'label'=>$aItem['name'].' (id: '.$aItem['id'].')'
                ];
            }
            /*
            echo '<pre>RETURN: '; 
            print_r($aReturn);
            die(__METHOD__);
            */
            
            return  $aReturn;
        }
        /**
         * get AWX Job Templates and return them as array for select box
         * [id] => array('value' => [ID], 'label' => [PLAYBOOK] [ID])
         * @return array
         */
        static public function getAwxJobTemplates(){
            $aResponse=$this->_httpRequest(array(
                    'url'=>'/job_templates/?order_by=playbook',
                    'method'=>'GET',
                )
            );
    
            if(!isset($aResponse['info']['http_code']) || $aResponse['info']['http_code']!==200){
                return false;
            }
    
            $aData=json_decode($aResponse['body'], 1);
            $aReturn=array();
    
            foreach ($aData['results'] as $aItem){
                $aReturn[$aItem['id']]= [
                    'value'=>$aItem['id'], 
                    'label'=>$aItem['playbook'].' (id: '.$aItem['id'].')'
                ];
            }
            /*
            echo '<pre>RETURN: '; 
            print_r($aReturn);
            die(__METHOD__);
            */
            
            return  $aReturn;
        }
        
        /**
         * check access to a deploy target
         */
        public function checkConnectionToTarget() {
            // do nothing ... always true
            return true;
        }
    
        public function getDeployCommands($sPhase){
            $aReturn=array();
            $aConfig=$this->getConfig($sPhase);
            if(!(int)$aConfig['inventory']){
                $aReturn[]='echo "ERROR: no inventory was given."; exit 1';
            }
            $sJson='{ 
            "inventory": "'.$aConfig['inventory'].'",
                "limit": "'.$aConfig['limit'].'",
                "job_tags": "'.$aConfig['tags'].'", 
                "extra_vars": '.$aConfig['extravars'].' 
            }';
            $sAuth=($aConfig['user'] ? '--user '.$aConfig['user'].':'.$aConfig['password'] : '');
            $aReturn[]="curl -f -k -H 'Content-Type: application/json' -XPOST -d '".$sJson."' $sAuth ".$aConfig['url']."/job_templates/".$aConfig['jobtemplate']."/launch/";
            return $aReturn;
        }
        
    
    }