1 /* This file is part of Eclat.
2    Copyright (C) 2012-2018 Sergey Poznyakoff.
3 
4    Eclat is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3, or (at your option)
7    any later version.
8 
9    Eclat is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with Eclat.  If not, see <http://www.gnu.org/licenses/>. */
16 
17 #include "eclat.h"
18 
19 static void
parse_options(struct eclat_command const * env,const char * docstring,int argc,char * argv[],int * index)20 parse_options(struct eclat_command const *env,
21 	      const char *docstring,
22               int argc, char *argv[], int *index)
23 {
24 	generic_proginfo->args_doc = "ID [ID...]";
25 	return generic_parse_options(env, docstring, argc, argv, index);
26 }
27 
28 static int
start_stop_instance(eclat_command_env_t * env,int argc,char ** argv)29 start_stop_instance(eclat_command_env_t *env, int argc, char **argv)
30 {
31 	int i;
32 	struct ec2_request *q = env->request;
33 	char buf[128], *bend;
34 	size_t bs;
35 
36 	if (argc == 0)
37 		die(EX_USAGE, "no instance ids");
38 	translate_ids(argc, argv, MAP_INSTANCE);
39 
40 	strcpy(buf, "InstanceId.");
41 	bend = buf + strlen(buf);
42 	bs = sizeof(buf) - strlen(buf);
43 	for (i = 0; i < argc; i++) {
44 		snprintf(bend, bs, "%lu", (unsigned long)(i + 1));
45 		eclat_request_add_param(q, buf, argv[i]);
46 	}
47 
48 	return 0;
49 }
50 
51 int
eclat_start_instance(eclat_command_env_t * env,int argc,char ** argv)52 eclat_start_instance(eclat_command_env_t *env, int argc, char **argv)
53 {
54 	int i;
55 
56 	parse_options(env->cmd,
57 		      "Start named instances",
58 		      argc, argv, &i);
59 
60 	debug(ECLAT_DEBCAT_MAIN, 1, ("starting instances"));
61 	return start_stop_instance(env, argc - i, argv + i);
62 }
63 
64 
65 static int force;
66 
67 #include "stop-cl.h"
68 
69 int
eclat_stop_instance(eclat_command_env_t * env,int argc,char ** argv)70 eclat_stop_instance(eclat_command_env_t *env, int argc, char **argv)
71 {
72 	int i;
73 
74 	parse_stop_options(env, argc, argv, &i);
75 	debug(ECLAT_DEBCAT_MAIN, 1, ("stopping instances"));
76 	if (force)
77 		eclat_request_add_param(env->request, "Force", "1");
78 	return start_stop_instance(env, argc - i, argv + i);
79 }
80 
81 int
eclat_reboot_instance(eclat_command_env_t * env,int argc,char ** argv)82 eclat_reboot_instance(eclat_command_env_t *env, int argc, char **argv)
83 {
84 	int i;
85 
86 	parse_options(env->cmd,
87 		      "Reboot named instances",
88 		      argc, argv, &i);
89 
90 	debug(ECLAT_DEBCAT_MAIN, 1, ("rebooting instances"));
91 	return start_stop_instance(env, argc - i, argv + i);
92 }
93