LCOV - code coverage report
Current view: top level - src - externals.c (source / functions) Hit Total Coverage
Test: deployctl-0.3.15.2.96a2d Code Coverage Lines: 101 103 98.1 %
Date: 2018-06-22 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   externals.c
       3             :   Created by Danny Goossen, Gioxa Ltd on 22/3/17.
       4             : 
       5             : MIT License
       6             : 
       7             : Copyright (c) 2017 deployctl, Gioxa Ltd.
       8             : 
       9             : Permission is hereby granted, free of charge, to any person obtaining a copy
      10             : of this software and associated documentation files (the "Software"), to deal
      11             : in the Software without restriction, including without limitation the rights
      12             : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      13             : copies of the Software, and to permit persons to whom the Software is
      14             : furnished to do so, subject to the following conditions:
      15             : 
      16             : The above copyright notice and this permission notice shall be included in all
      17             : copies or substantial portions of the Software.
      18             : 
      19             : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      20             : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      21             : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      22             : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      23             : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      24             : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
      25             : SOFTWARE.
      26             : 
      27             : */
      28             : 
      29             : \
      30             : #include "deployd.h"
      31             : 
      32             : 
      33             : 
      34             : 
      35             : /*----------------------------------------------------------------------------------
      36             :  * url_download curl to baseHREF/.commitsh and compaire content to commit_sha
      37             :  * returns 0 on success
      38             :  *-----------------------------------------------------------------------------------*/
      39           3 : int url_repo_base_path  (char ** basepath,char * baseHREF,struct trace_Struct * trace)
      40             : {
      41           3 :    int exitcode=0;
      42             :    char request[1024];
      43           3 :    sprintf(request, "%s/repo.json",baseHREF);
      44           3 :    debug("preprepare curl for url %s\n",request);
      45           3 :    CURL *curl = curl_easy_init();
      46             :    CURLcode res;
      47           3 :    if(curl)
      48             :    {
      49             :       // check url
      50             :       struct MemoryStruct chunk;
      51             : 
      52           3 :       chunk.memory = malloc(1);  // will be grown as needed by the realloc above
      53           3 :       chunk.size = 0;    // no data at this point
      54             : 
      55           3 :       debug("curl easy setup for url >%s<\n",(char*)request);
      56           3 :       curl_easy_setopt(curl, CURLOPT_URL, (char*)request);
      57           3 :       curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
      58           3 :       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
      59           3 :       curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
      60           3 :       curl_easy_setopt(curl, CURLOPT_FRESH_CONNECT, 1L);
      61           3 :       curl_easy_setopt(curl, CURLOPT_FORBID_REUSE,1L);
      62             : 
      63           3 :       res = curl_easy_perform(curl);
      64           3 :       curl_easy_cleanup(curl);
      65           3 :       debug("curl result: %d\n",res);
      66           3 :       if (res!=CURLE_OK )
      67             :       {
      68           1 :          Write_dyn_trace(trace, red,"\nERROR: Failed to get %s\n",request);
      69           1 :          debug("ERROR: Failed to get %s\n",request);
      70           1 :          exitcode=1;
      71             :       }
      72             :       else
      73             :       {
      74           2 :          cJSON * repo=NULL;
      75           2 :          char * base=NULL;
      76           2 :          if (chunk.memory){
      77           2 :                 repo=cJSON_Parse(chunk.memory);
      78           2 :             if (repo) base=cJSON_get_key(repo, "base_path");
      79             :          }
      80           2 :          if (!(base && strlen(base)>0))
      81             :          {
      82           1 :             Write_dyn_trace(trace, red,"[FAILED]\n  ERROR: check FAILED for %s\n",request);
      83             : 
      84           1 :             exitcode=1;
      85             :          }
      86             :          else
      87             :          {
      88           1 :             *basepath=strdup(base);
      89           1 :             Write_dyn_trace(trace, green,"[OK]\n");
      90             :          }
      91           2 :          if (repo) cJSON_Delete(repo);
      92             :       }
      93           3 :       free(chunk.memory);
      94             :       return exitcode;
      95             :    }
      96             :    return exitcode=1;
      97             : }
      98             : 
      99             : 
     100             : /*----------------------------------------------------------------------------------
     101             :  * url_verify curl to baseHREF/.commitsh and compaire content to commit_sha
     102             :  * returns 0 on success
     103             :  *-----------------------------------------------------------------------------------*/
     104           3 : int url_verify(char * commit_sha,char * baseHREF,struct trace_Struct * trace)
     105             : { // write http config for production environment
     106           3 :    int  exitcode=0;
     107             :    char request[1024];
     108           3 :    sprintf(request, "%s/.commit_sha.txt",baseHREF);
     109           3 :    debug("preprepare curl for url %s\n",request);
     110           3 :    CURL *curl = curl_easy_init();
     111             :    CURLcode res;
     112           3 :    if(curl)
     113             :    {
     114             :       // check url
     115             :       struct MemoryStruct chunk;
     116             : 
     117           3 :       chunk.memory = malloc(1);  // will be grown as needed by the realloc above
     118           3 :       chunk.size = 0;    // no data at this point
     119             : 
     120           3 :       debug("curl easy setup for url >%s<\n",(char*)request);
     121           3 :       curl_easy_setopt(curl, CURLOPT_URL, (char*)request);
     122           3 :       curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
     123           3 :       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
     124           3 :       curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
     125           3 :       curl_easy_setopt(curl, CURLOPT_FRESH_CONNECT, 1L);
     126           3 :       curl_easy_setopt(curl, CURLOPT_FORBID_REUSE,1L);
     127             : 
     128           3 :       res = curl_easy_perform(curl);
     129           3 :       curl_easy_cleanup(curl);
     130           3 :       debug("curl result: %d\n",res);
     131           3 :       if (res!=CURLE_OK )
     132             :       {
     133           1 :          Write_dyn_trace(trace, red,"\nERROR: Failed to get %s\n",request);
     134           1 :          debug("ERROR: Failed to get %s\n",request);
     135           1 :          free(chunk.memory);
     136           1 :          exitcode=1;
     137             :       }
     138             :       else
     139             :       {
     140           2 :          size_t len=strlen(commit_sha);
     141           2 :          if (len > chunk.size ||strncmp(commit_sha,chunk.memory,len)!=0)
     142             :          {
     143           1 :             chunk.memory[len]=0;
     144           1 :             debug("fail url check >%s< != >%s<",commit_sha,chunk.memory);
     145           1 :             Write_dyn_trace(trace, red,"[FAILED]\n  ERROR: check FAILED for %s\n",request);
     146             : 
     147           1 :             free(chunk.memory);
     148           1 :             exitcode=1;
     149             :          }
     150             :          else
     151             :          {
     152           1 :             Write_dyn_trace(trace, green,"[OK]\n");
     153           1 :             free(chunk.memory);
     154             :          }
     155             :       }
     156             :       return exitcode;
     157             :    }
     158           0 :    else Write_dyn_trace(trace, red,"[FAILED]\n  ERROR: could not initialise curl\n");
     159             : 
     160           0 :    return exitcode=1;
     161             : }
     162             : 
     163             : /*----------------------------------------------------------------------------------
     164             :  * verify_http_config:
     165             :  * exec /bin/sh nginx -t
     166             :  * returns 0 on success
     167             :  *-----------------------------------------------------------------------------------*/
     168           1 : int verify_http_config(void * opaque)
     169             : {
     170             :         char * newarg[4];
     171             : 
     172           1 :    int exitcode=0;
     173           1 :    struct trace_Struct *trace=((data_exchange_t *)opaque)->trace;
     174             : 
     175             :         // no need to set environment for individual commands
     176           1 :         ((data_exchange_t *)opaque)->needenvp=0;
     177             : 
     178           1 :         void * saved_args=((data_exchange_t *)opaque)->paramlist;
     179           1 :         ((data_exchange_t *)opaque)->paramlist=(char **)newarg;
     180             : 
     181           1 :         newarg[0]="/bin/sh";
     182           1 :         newarg[1]="-c";
     183           1 :         newarg[2]="sudo /usr/sbin/nginx -t -q";
     184           1 :         newarg[3]=NULL;
     185             : 
     186           1 :         Write_dyn_trace_pad(trace, none,75,"+ Test nginx config ...");
     187           1 :         debug("cmd: %s %s %s \n",newarg[0],newarg[1],newarg[2]);
     188             : 
     189           1 :         exitcode=cmd_exec(opaque);
     190           1 :         if (exitcode) {debug("nginx config failure\n");Write_dyn_trace(trace, red,"[FAILED]\n");}
     191           1 :         if (!exitcode) Write_dyn_trace(trace, green,"[OK]\n");
     192           1 :         ((data_exchange_t *)opaque)->paramlist=saved_args;
     193           1 :         return exitcode;
     194             : 
     195             : }
     196             : 
     197             : /*----------------------------------------------------------------------------------
     198             :  * reload_http_config:
     199             :  * exec systemctl reload nginx
     200             :  * returns 0 on success
     201             :  *-----------------------------------------------------------------------------------*/
     202           1 : int reload_http_config(void * opaque)
     203             : {
     204             :         char * newarg[4];
     205             : 
     206           1 :    int exitcode=0;
     207           1 :    struct trace_Struct *trace=((data_exchange_t *)opaque)->trace;
     208             : 
     209             :         // no need to set environment for individual commands
     210           1 :         ((data_exchange_t *)opaque)->needenvp=0;
     211             : 
     212           1 :         void * saved_args=((data_exchange_t *)opaque)->paramlist;
     213           1 :         ((data_exchange_t *)opaque)->paramlist=(char **)newarg;
     214             : 
     215           1 :         newarg[0]="/bin/sh";
     216           1 :         newarg[1]="-c";
     217           1 :         newarg[2]="sudo /usr/sbin/nginx -s reload";
     218           1 :         newarg[3]=NULL;
     219             : 
     220           1 :         Write_dyn_trace_pad(trace, none,75,"+ Reload nginx ...");
     221           1 :         debug("cmd: %s %s %s \n",newarg[0],newarg[1],newarg[2]);
     222             : 
     223           1 :         exitcode=cmd_exec(opaque);
     224           1 :         if (exitcode) {debug("nginx reload failure\n");Write_dyn_trace(trace, red,"[FAILED]\n");}
     225           1 :         if (!exitcode) Write_dyn_trace(trace, green,"[OK]\n");
     226           1 :         ((data_exchange_t *)opaque)->paramlist=saved_args;
     227           1 :    usleep(500000); // allow reload before checking with url_verify!!!
     228           1 :         return exitcode;
     229             : }

Generated by: LCOV version 1.10