~ubuntu-branches/ubuntu/raring/eucalyptus/raring

« back to all changes in this revision

Viewing changes to node/test_nc.c

  • Committer: Package Import Robot
  • Author(s): Brian Thomason
  • Date: 2011-11-29 13:17:52 UTC
  • mfrom: (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 185.
  • Revision ID: package-import@ubuntu.com-20111129131752-rq31al3ntutv2vvl
Tags: upstream-3.0.999beta1
ImportĀ upstreamĀ versionĀ 3.0.999beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- mode: C; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil -*-
 
2
// vim: set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
 
3
 
1
4
/*
2
5
Copyright (c) 2009  Eucalyptus Systems, Inc.    
3
6
 
52
55
  SOFTWARE, AND IF ANY SUCH MATERIAL IS DISCOVERED THE PARTY DISCOVERING
53
56
  IT MAY INFORM DR. RICH WOLSKI AT THE UNIVERSITY OF CALIFORNIA, SANTA
54
57
  BARBARA WHO WILL THEN ASCERTAIN THE MOST APPROPRIATE REMEDY, WHICH IN
55
 
  THE REGENTSā€™ DISCRETION MAY INCLUDE, WITHOUT LIMITATION, REPLACEMENT
 
58
  THE REGENTS' DISCRETION MAY INCLUDE, WITHOUT LIMITATION, REPLACEMENT
56
59
  OF THE CODE SO IDENTIFIED, LICENSING OF THE CODE SO IDENTIFIED, OR
57
60
  WITHDRAWAL OF THE CODE CAPABILITY TO THE EXTENT NEEDED TO COMPLY WITH
58
61
  ANY SUCH LICENSES OR RIGHTS.
64
67
#include <libvirt/virterror.h>
65
68
#include "misc.h"
66
69
#include "eucalyptus.h"
 
70
#include "diskutil.h"
67
71
 
68
72
#define MAXDOMS 1024
69
73
 
76
80
    }
77
81
}
78
82
 
79
 
int main (int argc, char * argv[] )
 
83
// find value of the given param in the eucalyptus.conf (e.g., /usr/bin/euca-bundle-upload for NC_BUNDLE_UPLOAD_PATH)
 
84
// return NULL if the param is commented out
 
85
static char* find_conf_value(const char* eucahome, const char* param)
80
86
{
81
 
  virConnectPtr conn = NULL;
82
 
  int dom_ids [MAXDOMS];
83
 
  int num_doms = 0;
84
 
  char *hypervisor, hypervisorURL[32], cmd[1024];
85
 
  char *eucahome=NULL;
86
 
  
87
 
  logfile (NULL, EUCAFATAL); /* suppress all messages */
88
 
 
89
 
  if (argc != 2) {
90
 
      fprintf (stderr, "error: test_nc expects one parameter (name of hypervisor)\n");
91
 
      exit (1);
92
 
  }
93
 
 
94
 
  hypervisor = argv[1];
95
 
  if (!strcmp(hypervisor, "kvm")) {
96
 
      snprintf(hypervisorURL, 32, "qemu:///system");
97
 
  } else if (!strcmp(hypervisor, "xen")) {
98
 
      snprintf(hypervisorURL, 32, "xen:///");      
99
 
  } else if (!strcmp(hypervisor, "not_configured")) {
100
 
      fprintf (stderr, "error: HYPERVISOR variable is not set in eucalyptus.conf\n");
101
 
      exit (1);
102
 
  } else {
103
 
      fprintf (stderr, "error: hypervisor type (%s) is not recognized\n", hypervisor);
104
 
      exit (1);
105
 
  }
106
 
  
107
 
  /* check that commands that NC needs are there */
108
 
  
109
 
  if ( system("perl --version") ) {
110
 
    fprintf (stderr, "error: could not run perl\n");
111
 
    exit (1);
112
 
  }
113
 
  
114
 
  eucahome = getenv (EUCALYPTUS_ENV_VAR_NAME);
115
 
  if (!eucahome) {
116
 
    eucahome = strdup (""); /* root by default */
117
 
  } else {
118
 
    eucahome = strdup(eucahome);
119
 
  }
120
 
  
121
 
  if (!strcmp(hypervisor, "kvm")) {
122
 
    snprintf(cmd, 1024, "%s/usr/lib/eucalyptus/euca_rootwrap %s/usr/share/eucalyptus/get_sys_info", eucahome, eucahome);
123
 
  } else {
124
 
    snprintf(cmd, 1024, "%s/usr/lib/eucalyptus/euca_rootwrap %s/usr/share/eucalyptus/get_xen_info", eucahome, eucahome);
125
 
  }
126
 
  
127
 
  if ( system(cmd) ) {
128
 
    fprintf (stderr, "error: could not run '%s'\n", cmd);
129
 
    exit (1);
130
 
  }
131
 
  
132
 
  /* check that libvirt can query the hypervisor */
133
 
  conn = virConnectOpen (hypervisorURL); /* NULL means local hypervisor */
134
 
  if (conn == NULL) {
135
 
    print_libvirt_error ();
136
 
    fprintf (stderr, "error: failed to connect to hypervisor\n");
137
 
    exit (1);
138
 
  }
139
 
  
140
 
  num_doms = virConnectListDomains (conn, dom_ids, MAXDOMS);
141
 
  if (num_doms < 0) {
142
 
    print_libvirt_error ();
143
 
    fprintf (stderr, "error: failed to query running domains\n");
144
 
    exit (1);
145
 
  }
146
 
  
147
 
  return 0;
 
87
    char conf_path[1024];
 
88
    char line[1024];
 
89
    char *value = NULL;
 
90
    FILE *f_conf = NULL;
 
91
    int i =0; 
 
92
 
 
93
    if (!eucahome || !param)
 
94
        return NULL;
 
95
    
 
96
    snprintf (conf_path, 1024, "%s/etc/eucalyptus/eucalyptus.conf", eucahome); 
 
97
    f_conf = fopen (conf_path, "r");
 
98
    if (!f_conf){ 
 
99
        return NULL;
 
100
    }
 
101
    
 
102
    while (fgets (line, 1024, f_conf)!=NULL) {
 
103
        if (strstr(line, param)!=NULL) { // found the param in the line
 
104
            if (strchr(line, '#')!= NULL) { // the line is commented out (assume # can't appear in the middle)
 
105
                break;   
 
106
            } else {
 
107
                char* pch = strtok(line, "="); // again assume '=' can't appear in the middle of value
 
108
                pch = strtok(NULL, "=");
 
109
                if (pch && strlen(pch)>0) {
 
110
                    value = calloc(strlen(pch)+1, 1);
 
111
                    if (!value) {
 
112
                        fclose(f_conf); 
 
113
                        return NULL;
 
114
                    }
 
115
                    snprintf(value, strlen(pch)+1, "%s", pch);
 
116
                }             
 
117
                break;
 
118
            }
 
119
        }
 
120
        bzero(line, 1024);
 
121
    } 
 
122
    
 
123
    // remove "" from the value         
 
124
    if (value){
 
125
        int quote=0;
 
126
        for (int i=0; i<strlen(value); i++)     {       
 
127
            if(value[i]=='\"')  
 
128
                quote++;
 
129
            else
 
130
                value[i-quote] = value[i];
 
131
        }
 
132
        value[strlen(value)-quote] = 0x00;
 
133
        
 
134
        // remove spaces
 
135
        i=0;
 
136
        while (value[i]==' ' || value[i]=='\t')
 
137
            i++;
 
138
        for(int j=i; j<strlen(value); j++)
 
139
            value[j-i] = value[j];
 
140
        value[strlen(value)-i] = 0x00;
 
141
        
 
142
        if(value[strlen(value)-1] == '\n')
 
143
            value[strlen(value)-1] = 0x00;
 
144
    }
 
145
    
 
146
    fclose(f_conf); 
 
147
    return value;
 
148
}
 
149
 
 
150
int main (int argc, char * argv[])
 
151
 
152
    virConnectPtr conn = NULL;
 
153
    int dom_ids [MAXDOMS];
 
154
    int num_doms = 0;
 
155
    char *hypervisor, hypervisorURL[32], cmd[1024];
 
156
    char *eucahome=NULL;
 
157
    
 
158
    //  logfile (NULL, EUCAFATAL); // suppress all messages
 
159
    
 
160
    if (argc != 2) {
 
161
        fprintf (stderr, "error: test_nc expects one parameter (name of hypervisor)\n");
 
162
        exit (1);
 
163
    }
 
164
    
 
165
    hypervisor = argv[1];
 
166
    if (!strcmp(hypervisor, "kvm")) {
 
167
        snprintf(hypervisorURL, 32, "qemu:///system");
 
168
    } else if (!strcmp(hypervisor, "xen")) {
 
169
        snprintf(hypervisorURL, 32, "xen:///");      
 
170
    } else if (!strcmp(hypervisor, "not_configured")) {
 
171
        fprintf (stderr, "error: HYPERVISOR variable is not set in eucalyptus.conf\n");
 
172
        exit (1);
 
173
    } else {
 
174
        fprintf (stderr, "error: hypervisor type (%s) is not recognized\n", hypervisor);
 
175
        exit (1);
 
176
    }
 
177
    
 
178
    // check that commands that NC needs are there
 
179
    
 
180
    if (system("perl --version")) {
 
181
        fprintf (stderr, "error: could not run perl\n");
 
182
        exit (1);
 
183
    }
 
184
    
 
185
    eucahome = getenv (EUCALYPTUS_ENV_VAR_NAME);
 
186
    if (!eucahome) {
 
187
        eucahome = strdup (""); // root by default
 
188
    } else {
 
189
        eucahome = strdup(eucahome);
 
190
    }
 
191
    
 
192
    add_euca_to_path (eucahome);
 
193
 
 
194
    fprintf (stderr, "looking for system utilities...\n");
 
195
    if (diskutil_init())
 
196
        exit (1);
 
197
    
 
198
    // check if euca2ools commands for bundle-instance are available
 
199
    fprintf (stderr, "ok\n\nlooking for euca2ools...\n");
 
200
    static char * helpers_name [3] = {
 
201
        "euca-bundle-upload",
 
202
        "euca-check-bucket",
 
203
        "euca-delete-bundle"
 
204
    };
 
205
 
 
206
    char * helpers_path [3]; // load paths from eucalyptus.conf or set to NULL
 
207
    helpers_path [0] = find_conf_value (eucahome, "NC_BUNDLE_UPLOAD_PATH");
 
208
    helpers_path [1] = find_conf_value (eucahome, "NC_CHECK_BUCKET_PATH");
 
209
    helpers_path [2] = find_conf_value (eucahome, "NC_DELETE_BUNDLE_PATH");
 
210
 
 
211
    if (verify_helpers (helpers_name, helpers_path, 3) > 0) {
 
212
       if (verify_helpers (helpers_name, NULL, 3) > 0) {
 
213
             fprintf (stderr, "error: failed to find required euca2ools\n");
 
214
             exit (1);
 
215
       }
 
216
    }
 
217
 
 
218
    // ensure hypervisor information is available
 
219
    fprintf (stderr, "ok\n\nchecking the hypervisor...\n");
 
220
    if (!strcmp(hypervisor, "kvm")) {
 
221
        snprintf(cmd, 1024, "%s/usr/lib/eucalyptus/euca_rootwrap %s/usr/share/eucalyptus/get_sys_info", eucahome, eucahome);
 
222
    } else {
 
223
        snprintf(cmd, 1024, "%s/usr/lib/eucalyptus/euca_rootwrap %s/usr/share/eucalyptus/get_xen_info", eucahome, eucahome);
 
224
    }
 
225
    if (system(cmd)) {
 
226
        fprintf (stderr, "error: could not run '%s'\n", cmd);
 
227
        exit (1);
 
228
    }
 
229
    
 
230
    // check that libvirt can query the hypervisor
 
231
    conn = virConnectOpen (hypervisorURL); // NULL means local hypervisor
 
232
    if (conn == NULL) {
 
233
        print_libvirt_error ();
 
234
        fprintf (stderr, "error: failed to connect to hypervisor\n");
 
235
        exit (1);
 
236
    }
 
237
    
 
238
    num_doms = virConnectListDomains (conn, dom_ids, MAXDOMS);
 
239
    if (num_doms < 0) {
 
240
        print_libvirt_error ();
 
241
        fprintf (stderr, "error: failed to query running domains\n");
 
242
        exit (1);
 
243
    }
 
244
    
 
245
    return 0;
148
246
}
149
247
 
150
248