aboutsummaryrefslogtreecommitdiff
path: root/src/runinsts.c
blob: 826377d29f4defdf58b04a4e1f2017d347f9e46c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/* This file is part of Eclat.
   Copyright (C) 2013 Sergey Poznyakoff.
 
   Eclat is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.
 
   Eclat is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with Eclat.  If not, see <http://www.gnu.org/licenses/>. */

#include "eclat.h"
#include "runinsts-cl.h"

static int
isnumstr(const char *p)
{
	for (; *p; p++)
		if (!isdigit(*p))
			return 0;
	return 1;
}

static void
encode_devmap(struct ec2_query *q)
{
	int i;
	char *bufptr = NULL;
	size_t bufsize = 0;
	struct grecs_list_entry *ep;

	for (i = 1, ep = devmap->head; ep; ep = ep->next, i++) {
		char *dev = ep->data;
		char *p = strchr(dev, '=');

		if (!p)
			die(EX_USAGE, "malformed device mapping: %s", dev);
		*p++ = 0;

		grecs_asprintf(&bufptr, &bufsize,
			       "BlockDeviceMapping.%d.DeviceName",
			       i);
		eclat_query_add_param(q, bufptr, dev);
		
		if (strcmp(p, "none") == 0) {
			grecs_asprintf(&bufptr, &bufsize,
				       "BlockDeviceMapping.%d.NoDevice",
				       i);
			eclat_query_add_param(q, bufptr, NULL);
		} else if (strncmp(p, "ephemeral", 9) == 0) {
			grecs_asprintf(&bufptr, &bufsize,
				       "BlockDeviceMapping.%d.VirtualName",
				       i);
			eclat_query_add_param(q, bufptr, p);
		} else {
			struct wordsplit ws;

	/* [snapshot-id]:[volume-size]:[true|false]:[standard|io1[:iops]] */
			ws.ws_delim = ":";
			if (wordsplit(p, &ws,
				      WRDSF_NOVAR | WRDSF_NOCMD | WRDSF_DELIM))
				die(EX_SOFTWARE,
				    "failed to split string %s: %s",
				    p,
				    wordsplit_strerror(&ws));
			if (ws.ws_wordc == 1) {
				if (strncmp(ws.ws_wordv[0], "snap-", 5) == 0) {
					grecs_asprintf(&bufptr, &bufsize,
						       "BlockDeviceMapping.%d."
						       "Ebs.SnapshotId",
						       i);
				} else if (isnumstr(ws.ws_wordv[0])) {
					grecs_asprintf(&bufptr, &bufsize,
						       "BlockDeviceMapping.%d."
						       "Ebs.VolumeSize",
						       i);
				} else
					die(EX_USAGE,
					    "unrecognized word \"%s\", "
					    "expected either a snapshot ID, "
					    "or disk size",
					    ws.ws_wordv[0]);
				eclat_query_add_param(q, bufptr,
						      ws.ws_wordv[0]);
			} else {
				if (ws.ws_wordv[0]) {
					grecs_asprintf(&bufptr, &bufsize,
						       "BlockDeviceMapping.%d."
						       "Ebs.SnapshotId",
						       i);
					eclat_query_add_param(q, bufptr,
							      ws.ws_wordv[0]);
				}
				
				if (ws.ws_wordv[1]) {
					grecs_asprintf(&bufptr, &bufsize,
						       "BlockDeviceMapping.%d."
						       "Ebs.VolumeSize",
						       i);
					eclat_query_add_param(q, bufptr,
							      ws.ws_wordv[1]);
				}
			}

			switch (ws.ws_wordc) {
			default:
				die(EX_USAGE,
				    "too many parts in device mapping \"%s\"",
				    p);		
			case 5:
				grecs_asprintf(&bufptr, &bufsize,
					       "BlockDeviceMapping.%d.Ebs.Iops",
					       i);
				eclat_query_add_param(q, bufptr, 
						      ws.ws_wordv[4]);
				/* fall through */
			case 4:
				grecs_asprintf(&bufptr, &bufsize,
					 "BlockDeviceMapping.%d.Ebs.VolumeType",
					       i);
				eclat_query_add_param(q, bufptr,
						      ws.ws_wordv[3]);
				/* fall through */
			case 3:
				if (strcmp(ws.ws_wordv[2], "false") &&
				    strcmp(ws.ws_wordv[2], "true"))
					die(EX_USAGE,
					    "expected \"true\" or \"false\", "
					    "but found \"%s\"",
					    ws.ws_wordv[2]);
				grecs_asprintf(&bufptr, &bufsize,
					       "BlockDeviceMapping.%d."
					       "Ebs.DeleteOnTermination",
					       i);
				eclat_query_add_param(q, bufptr,
						      ws.ws_wordv[2]);
			}
			wordsplit_free(&ws);
		}		
	}
	free(bufptr);
}				       

/* format:
   
   dev-index:subnet:description:priv-ip:sgs:DOT:sip-count:sips
      0        1         2         3     4   5      6      7
      
   where:

   dev-index   device index
   subnet      subnet ID
   sgs         a comma-separated list of security group IDs
   DOT         delete the interface on termination: true or false
   sip-count   number of secondary IP addresses to assign
   sips        a comma-separated list of secondary IP addresses
   
   only dev-index and subnet are mandatory;
   either sip-count or sips can be specified, but not both.
*/
void
add_iface(struct ec2_query *q, int ifno, char *ifspec)
{
	struct wordsplit ws;
	char *bufptr = NULL;
	size_t bufsize = 0;
	char *p;
	int i;
	
	ws.ws_delim = ":";
	if (wordsplit(ifspec, &ws, WRDSF_NOVAR | WRDSF_NOCMD | WRDSF_DELIM))
		die(EX_SOFTWARE,
		    "failed to split string %s: %s",
		    p,
		    wordsplit_strerror(&ws));
	
	switch (ws.ws_wordc) {
	default:
		die(EX_USAGE,
		    "bad number of parts in interface specification \"%s\": %d",
		    ifspec, ws.ws_wordc);
	case 8:
		for (i = 1, p = strtok(ws.ws_wordv[7], ","); p;
		     i++, p = strtok(NULL, ",")) {
			grecs_asprintf(&bufptr, &bufsize,
				       "NetworkInterface.%d."
				       "PrivateIpAddresses.%d.PrivateIpAddress",
				       ifno, i);
			eclat_query_add_param(q, bufptr, p);
		}
		/* fall through */
	case 7:
		if (ws.ws_wordv[6][0]) {
			grecs_asprintf(&bufptr, &bufsize,
				       "NetworkInterface.%d."
				       "SecondaryPrivateIpAddressCount",
				       ifno);
			eclat_query_add_param(q, bufptr, ws.ws_wordv[6]);
		}
	case 6:
		if (ws.ws_wordv[5][0]) {
			grecs_asprintf(&bufptr, &bufsize,
				       "NetworkInterface.%d."
				       "DeleteOnTermination",
				       ifno);
			eclat_query_add_param(q, bufptr, ws.ws_wordv[5]);
		}
	case 5:
		if (ws.ws_wordv[4])
			for (i = 1, p = strtok(ws.ws_wordv[4], ","); p;
			     i++, p = strtok(NULL, ",")) {
				grecs_asprintf(&bufptr, &bufsize,
					       "NetworkInterface.%d."
					       "SecurityGroupId.%d",
					       ifno, i);
				eclat_query_add_param(q, bufptr, p);
			}
	case 4:
		if (ws.ws_wordv[3]) {
			grecs_asprintf(&bufptr, &bufsize,
				       "NetworkInterface.%d.PrivateIpAddress",
				       ifno);
			eclat_query_add_param(q, bufptr, ws.ws_wordv[3]);
		}
	case 3:
		if (ws.ws_wordv[2]) {
			grecs_asprintf(&bufptr, &bufsize,
				       "NetworkInterface.%d.Description",
				       ifno);
			eclat_query_add_param(q, bufptr, ws.ws_wordv[2]);
		}
	case 2:
		if (!ws.ws_wordv[1][0])
			die(EX_USAGE,
			    "no subnet ID in interface specification \"%s\"",
			    ifspec);
	}
	grecs_asprintf(&bufptr, &bufsize, "NetworkInterface.%d.SubnetId",
		       ifno);
	eclat_query_add_param(q, bufptr,
			      ws.ws_wordv[1][0] ? ws.ws_wordv[1] : "0");

	if (ws.ws_wordv[0][0]) {
		grecs_asprintf(&bufptr, &bufsize,
			       "NetworkInterface.%d.DeviceIndex",
			       ifno);
		eclat_query_add_param(q, bufptr, ws.ws_wordv[0]);
	}
	
	wordsplit_free(&ws);
	free(bufptr);
}

int
eclat_run_instances(eclat_command_env_t *env, int argc, char **argv)
{
	int i;
	char *bufptr = NULL;
	size_t bufsize = 0;
	struct grecs_list_entry *ep;
	struct ec2_query *q = env->query;
	char *p;
	int iface_no = 1;
	
	parse_options(argc, argv);

	eclat_query_add_param(q, "ImageId", ami);
	p = strchr(instance_count, '-');
	eclat_query_add_param(q, "MinCount", instance_count);
	if (p) {
		*p++ = 0;
		eclat_query_add_param(q, "MaxCount", p);
	} else
		eclat_query_add_param(q, "MaxCount", instance_count);
	
	if (keypair)
		eclat_query_add_param(q, "KeyName", keypair);

	if (secgrp) {
		for (i = 1, ep = secgrp->head; ep; ep = ep->next, i++) {
			grecs_asprintf(&bufptr, &bufsize,
				       "SecurityGroup.%d", i);
			eclat_query_add_param(q, bufptr, ep->data);
		}
	}

	if (type)
		eclat_query_add_param(q, "InstanceType", type);

	if (zone)
		eclat_query_add_param(q, "Placement.AvailabilityZone", zone);
	
	if (kernel)
		eclat_query_add_param(q, "KernelId", kernel);

	if (ramdisk)
		eclat_query_add_param(q, "RamdiskId", ramdisk);

	if (devmap)
		encode_devmap(q);
		
	if (monitor)
		eclat_query_add_param(q, "Monitoring.Enabled", "true");

	if (disable_term)
		eclat_query_add_param(q, "DisableApiTermination", "true");

	if (shutdown_behavior)
		eclat_query_add_param(q, "InstanceInitiatedShutdownBehavior",
				      "true");
	if (placement_group)
		eclat_query_add_param(q, "Placement.GroupName",
				      placement_group);

	if (tenancy)
		eclat_query_add_param(q, "Placement.Tenancy", tenancy);

	if (subnet)
		eclat_query_add_param(q, "SubnetId", subnet);

	if (private_ip)
		eclat_query_add_param(q, "PrivateIpAddress", private_ip);

	/* FIXME: I'm not at all sure whether this is the right way of
	   doing it. */
	if (privip) {
		for (i = 1, ep = privip->head; ep; i++, ep = ep->next) {
			grecs_asprintf(&bufptr, &bufsize,
				       "NetworkInterface.1."
				       "PrivateIpAddresses.%d.PrivateIpAddress",
				       i);
			eclat_query_add_param(q, bufptr, ep->data);
		}
		iface_no++;
	} else if (secipcount) {
		eclat_query_add_param(q,
				      "NetworkInterface.1."
				      "SecondaryPrivateIpAddressCount",
				      secipcount);
		iface_no++;
	}

	if (iface)
		for (ep = iface->head; ep; ep = ep->next)
			add_iface(q, iface_no++, ep->data);
	
	if (profile_name)
		eclat_query_add_param(q,
				      strncmp(profile_name, "arn:", 4) == 0 ?
				      "IamInstanceProfile.Arn" :
				      "IamInstanceProfile.Name",
				      profile_name);

	if (ebs_opt)
		eclat_query_add_param(q, "EbsOptimized", "true");

	if (user_data) {
		size_t enclen;
		
		eclat_base64_encode((unsigned char *)user_data,
				    strlen(user_data),
				    (unsigned char**) &p, &enclen);
		
		eclat_query_add_param(q, "UserData", p);
		free(p);
	}
	
	return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.