aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2012-09-20 00:49:00 +0300
committerSergey Poznyakoff <gray@gnu.org.ua>2012-09-20 00:49:00 +0300
commitdf0d5f4df874bd47e5e47d08c67fea7ac17eec1e (patch)
tree2097591c3fd1a8e82bf6101c6d311fc9480917f1 /src
parent96871660d41c0130460f52057c0334cec46f7dd3 (diff)
downloadeclat-df0d5f4df874bd47e5e47d08c67fea7ac17eec1e.tar.gz
eclat-df0d5f4df874bd47e5e47d08c67fea7ac17eec1e.tar.bz2
Implement start-instance.
* lib/qaddparm.c: New file. * lib/qcreat.c: New file. * lib/qencode.c: New file. * lib/qfree.c: new file. * lib/Makefile.am: Add new files. * lib/libeclat.h (ec2_query) <https, verb>: Remove. <flags>: New member. (eclat_query_create, eclat_query_free) (eclat_query_encode): New protos. * lib/q2url.c (eclat_query_to_url): Fix. * lib/reqsign.c (eclat_query_signature): Bugfixes. * src/config.c (config_finish): Call grecs_tree_process. * src/eclat.c (url_base): Remove. (main): Install curl debugging function if required. * src/eclat.h: Update. * src/startinst.c (eclat_start_instance): Implement.
Diffstat (limited to 'src')
-rw-r--r--src/config.c2
-rw-r--r--src/eclat.c110
-rw-r--r--src/eclat.h4
-rw-r--r--src/startinst.c42
4 files changed, 144 insertions, 14 deletions
diff --git a/src/config.c b/src/config.c
index 184b42a..5727167 100644
--- a/src/config.c
+++ b/src/config.c
@@ -102,6 +102,8 @@ config_finish(struct grecs_node *tree)
grecs_print_node(tree, GRECS_NODE_FLAG_DEFAULT, stderr);
fputc('\n', stdout);
}
+ if (grecs_tree_process(tree, eclat_kw))
+ exit(EX_CONFIG);
if (grecs_error_count || run_config_finish_hooks())
exit(EX_CONFIG);
}
diff --git a/src/eclat.c b/src/eclat.c
index 3d2572d..5625076 100644
--- a/src/eclat.c
+++ b/src/eclat.c
@@ -29,8 +29,6 @@ char *secret_key;
char *region_name;
enum eclat_command eclat_command;
-char *url_base;
-
struct debug_trans {
const char *name;
@@ -44,6 +42,7 @@ static struct debug_trans debug_trans[] = {
{ S(cfgram), ECLAT_DEBCAT_CFGRAM },
{ S(cflex), ECLAT_DEBCAT_CFLEX },
{ S(conf), ECLAT_DEBCAT_CONF },
+ { S(curl), ECLAT_DEBCAT_CURL },
{ NULL }
};
@@ -85,6 +84,94 @@ parse_debug_level(const char *arg)
return 0;
}
+static void
+dump(const char *text, FILE *stream, unsigned char *ptr, size_t size)
+{
+ size_t i;
+ size_t c;
+ unsigned int width = 0x10;
+ int hex = debug_level[ECLAT_DEBCAT_CURL] > 2;
+
+ if (!hex)
+ /* without the hex output, we can fit more on screen */
+ width = 0x40;
+
+ fprintf(stream, "%s, %zd bytes (0x%zx)\n", text, size, size);
+
+ for (i = 0; i < size; i += width) {
+ fprintf(stream, "%04zx: ", i);
+
+ if (hex) {
+ for (c = 0; c < width; c++)
+ if (i+c < size)
+ fprintf(stream, "%02x ", ptr[i+c]);
+ else
+ fputs(" ", stream);
+ }
+
+ for(c = 0; (c < width) && (i+c < size); c++) {
+ /* check for CRLf; if found, skip past and start a
+ new line of output */
+ if (!hex && (i + c + 1 < size) &&
+ ptr[i+c] == '\r' && ptr[i+c+1] == '\n') {
+ i += (c + 2 -width);
+ break;
+ }
+ fprintf(stream, "%c",
+ isprint(ptr[i+c]) ? ptr[i+c] : '.');
+ /* check again for CRLF, to avoid an extra \n if
+ it's at width */
+ if (!hex && (i + c + 2 < size) &&
+ ptr[i+c+1] == '\r' && ptr[i+c+2] == '\n') {
+ i += (c + 3 - width);
+ break;
+ }
+ }
+ fputc('\n', stream); /* newline */
+ }
+ fflush(stream);
+}
+
+static int
+eclat_trace_fun(CURL *handle, curl_infotype type,
+ char *data, size_t size,
+ void *userp)
+{
+ struct data *config = (struct data *)userp;
+ const char *text;
+
+ switch (type) {
+ case CURLINFO_TEXT:
+ fprintf(stderr, "== Info: %s", data);
+ default: /* in case a new one is introduced to shock us */
+ return 0;
+
+ case CURLINFO_HEADER_OUT:
+ text = "=> Send header";
+ break;
+ case CURLINFO_DATA_OUT:
+ text = "=> Send data";
+ break;
+ case CURLINFO_SSL_DATA_OUT:
+ text = "=> Send SSL data";
+ break;
+ case CURLINFO_HEADER_IN:
+ text = "<= Recv header";
+ break;
+ case CURLINFO_DATA_IN:
+ text = "<= Recv data";
+ break;
+ case CURLINFO_SSL_DATA_IN:
+ text = "<= Recv SSL data";
+ break;
+ }
+
+ dump(text, stderr, (unsigned char *)data, size);
+ return 0;
+}
+
+
+
#include "cmdline.h"
eclat_command_handler_t handler_tab[] = {
@@ -99,7 +186,6 @@ main(int argc, char **argv)
int index, rc;
struct grecs_node *tree;
CURL *curl;
- size_t size;
set_program_name(argv[0]);
config_init();
@@ -143,19 +229,19 @@ main(int argc, char **argv)
}
if (eclat_command == eclat_command_unspecified)
- die(EX_USAGE, "no command given");
-
- url_base = NULL;
- size = 0;
- if (use_ssl)
- grecs_asprintf(&url_base, &size, "https://%s", default_host);
- else
- grecs_asprintf(&url_base, &size, "http://%s", default_host);
-
+ die(EX_USAGE, "no command given");
curl = curl_easy_init();
if (!curl)
die(EX_UNAVAILABLE, "curl_easy_init failed");
+
+ if (debug_level[ECLAT_DEBCAT_CURL]) {
+ curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
+ if (debug_level[ECLAT_DEBCAT_CURL] > 1)
+ curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION,
+ eclat_trace_fun);
+ }
+
rc = handler_tab[eclat_command](curl, argc, argv);
curl_easy_cleanup(curl);
exit(rc);
diff --git a/src/eclat.h b/src/eclat.h
index 0fb4fd7..953c34c 100644
--- a/src/eclat.h
+++ b/src/eclat.h
@@ -31,7 +31,8 @@
#define ECLAT_DEBCAT_CFGRAM 1
#define ECLAT_DEBCAT_CFLEX 2
#define ECLAT_DEBCAT_CONF 3
-#define ECLAT_DEBCAT_MAX 4
+#define ECLAT_DEBCAT_CURL 4
+#define ECLAT_DEBCAT_MAX 5
extern const char *program_name;
extern int debug_level[];
@@ -42,7 +43,6 @@ extern char *region_name;
extern char *access_file_name;
extern char *access_key;
extern char *secret_key;
-extern char *url_base;
#define debug(cat, lev, s) \
do { \
diff --git a/src/startinst.c b/src/startinst.c
index f07e50a..87887e9 100644
--- a/src/startinst.c
+++ b/src/startinst.c
@@ -16,9 +16,51 @@
#include "eclat.h"
+#define EC2_API_VERSION "2012-08-15"
+
int
eclat_start_instance(CURL *curl, int argc, char **argv)
{
+ int i;
+ struct ec2_query *q;
+ char buf[128], *bend, *url;
+ size_t bs;
+ CURLcode res;
+
+ q = eclat_query_create(use_ssl ? EC2_QF_HTTPS : 0,
+ default_host, "/");
+ if (region_name)
+ eclat_query_add_param(q, "Placement.AvailabilityZone",
+ region_name);
+ eclat_query_add_param(q, "Action", "StartInstances");
+
+ strcpy(buf, "InstanceId.");
+ bend = buf + strlen(buf);
+ bs = sizeof(buf) - strlen(buf);
+ for (i = 0; i < argc; i++) {
+ snprintf(bend, bs, "%lu", (unsigned long)(i + 1));
+ eclat_query_add_param(q, buf, argv[i]);
+ }
+
+ eclat_query_add_param(q, "AWSAccessKeyId", access_key);
+ eclat_query_add_param(q, "Version", EC2_API_VERSION);
+
+ eclat_query_signature(q, secret_key);
+ url = eclat_query_to_url(q, NULL);
+
+ debug(ECLAT_DEBCAT_MAIN, 2, ("using URL: %s", url));
+ curl_easy_setopt(curl, CURLOPT_URL, url);
+
+ free(url);
+ eclat_query_free(q);
+
+ res = curl_easy_perform(curl);
+
+ if (res != CURLE_OK)
+ err("CURL: %s", curl_easy_strerror(res));
+
+ return 0;
+
}
int

Return to:

Send suggestions and report system problems to the System administrator.