summaryrefslogtreecommitdiff
path: root/components/services/install/command-line/eazel-install.c
blob: eafc7242f35a6fb4a4edf5a63e27a89941270489 (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* 
 * Copyright (C) 2000 Eazel, Inc
 * Copyright (C) 2000 Helix Code, Inc
 *
 * This program 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 2 of the
 * License, or (at your option) any later version.
 *
 * This program 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 this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Authors: J Shane Culpepper <pepper@eazel.com>
 *          Joe Shaw <joe@helixcode.com>
 *
 */

/* eazel-install - services command line install/update/uninstall
 * component.  This program will parse the eazel-services-config.xml
 * file and install a services generated package-list.xml.
 */

#include "eazel-install-types.h"
#include "eazel-install-protocols.h"
#include "eazel-install-rpm-glue.h"
#include "eazel-install-xml-package-list.h"
#include "eazel-install-metadata.h"
#include <libtrilobite/helixcode-utils.h>
#include <config.h>
#include <popt-gnome.h>

#define PACKAGE_FILE_NAME "package-list.xml"
#define DEFAULT_CONFIG_FILE "/var/eazel/services/eazel-services-config.xml"
	

static void show_usage (int exitcode, char* error);
static void show_license (int exitcode, char* error);
static void generate_new_package_list (const char* popt_genpkg_file,
                                       const char* config_file);
static void create_temporary_directory (const char* tmpdir);
static void fetch_remote_package_list (const char* pkg_list,
                                       TransferOptions* topts);

static void
show_usage (int exitcode, char* error) {
	fprintf (stderr, "Usage: eazel-install [options]\n"
			"Valid options are:\n"
			"	--help                   : show help\n"
			"	--License                : show license\n"
			"	--local                  : use local files\n"
			"	--http                   : use http\n"
			"	--ftp                    : use ftp\n"
			"	--verbose                : give more information during processing\n"
			"	--log <file>             : redirect all information to log file.\n"
			"	--test                   : dry run - don't actually install\n"
			"	--force                  : force package to install/uninstall\n"
			"	--uninstall              : uninstall the package list\n"
			"	--tmpdir <dir>           : temporary directory to store rpms\n"
			"	--server <url>)          : url of remote server\n"
			"        --new-pkg-list <file>    : generate xml package list from template\n"
			"\nExample: eazel-install --http --server www.eazel.com --tmpdir /tmp\n\n");
			
	if (error) {
		fprintf (stderr, "%s\n", error);
	}

	exit (exitcode);

} /* end usage */

static void
show_license (int exitcode, char* error) {
	fprintf (stderr, "eazel-install: a quick and powerful remote installation utility.\n\n"
			"Copyright (C) 2000 Eazel, Inc.\n\n"
			"This program is free software; you can redistribute it and/or\n"
			"modify it under the terms of the GNU General Public License as\n"
			"published by the Free Software Foundation; either version 2 of the\n"
			"License, or (at your option) any later version.\n\n"
			"This program is distributed in the hope that it will be useful,\n"
			"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
			"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n"
			"General Public License for more details.\n\n"
			"You should have received a copy of the GNU General Public License\n"
			"along with this program; if not, write to the Free Software\n"
			"Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n\n");

	if (error) {
		fprintf(stderr, "%s\n", error);
	}

	exit(exitcode);

} /* end show_license */

static void
generate_new_package_list (const char* popt_genpkg_file,
                           const char* config_file) {

	gboolean retval;

	retval = generate_xml_package_list (popt_genpkg_file, config_file);

	if (!retval) {
		g_error (_("*** Could not generate xml package list! ***\n"));
	}

	g_print (_("XML package list successfully generated ...\n"));
	exit (1);
} /* end generate_new_package_list */

static void
create_temporary_directory (const char* tmpdir) {

	int retval;

	g_print (_("Creating temporary download directory ...\n"));

	retval = mkdir (tmpdir, 0755);
	if (retval < 0) {
		if (errno != EEXIST) {
			g_error (_("*** Could not create temporary directory! ***\n"));
		}
	}
} /* end create_temporary_directory */

static void
fetch_remote_package_list (const char* pkg_list, TransferOptions* topts) {

	gboolean retval;
	char* url;

	g_print (_("Getting package-list.xml from remote server ...\n"));

	url = g_strdup_printf ("http://%s%s", topts->hostname,
                                topts->pkg_list_storage_path);

	retval = http_fetch_remote_file (url, pkg_list);

	if (!retval) {
		g_free (url);
		g_error ("*** Unable to retrieve package-list.xml! ***\n");
	}
	g_free (url);
} /* end fetch_remote_package_list */

int
main (int argc, char* argv[]) {
	char opt;
	gboolean retval;
	gboolean USE_LOCAL, USE_HTTP, USE_FTP, UNINSTALL_MODE, DOWNGRADE_MODE, TEST_MODE, FORCE_MODE, VERBOSE_MODE, LOGGING_MODE;
	InstallOptions* iopts;
	TransferOptions* topts;
	poptContext pctx;
	char* config_file;
	char* target_file;
	char* popt_log_file;
	char* popt_tmpdir;
	char* popt_server;
	char* popt_genpkg_file;
		
	struct poptOption optionsTable[] = {
		{ "help", 'h', 0, NULL, 'h' }, 
		{ "License", 'L', 0, NULL, 'L' },
		{ "local", 'l', 0, NULL, 'l'},
		{ "http", 'w', 0, NULL, 'w' },
		{ "ftp", 'f', 0, NULL, 'f' },
		{ "verbose", 'v', 0, NULL, 'v' },
		{ "log", 'S', POPT_ARG_STRING, &popt_log_file, 'S' },
		{ "force", 'F', 0, NULL, 'F' },
		{ "test", 't', 0, NULL, 't' },
		{ "uninstall", 'u', 0, NULL, 'u' },
		{ "downgrade", 'D', 0, NULL, 'D' },
		{ "tmpdir", 'T', POPT_ARG_STRING, &popt_tmpdir, 'T' },
		{ "server", 's', POPT_ARG_STRING, &popt_server, 's' },
		{ "genpkg_list", 'g', POPT_ARG_STRING, &popt_genpkg_file, 'g' },
		{ NULL, '\0', 0, NULL, 0 }
	};

	retval = FALSE;
	USE_LOCAL = FALSE;
	USE_HTTP = FALSE;
	USE_FTP = FALSE;
	VERBOSE_MODE = FALSE;
	LOGGING_MODE = FALSE;
	FORCE_MODE = FALSE;
	TEST_MODE = FALSE;
	UNINSTALL_MODE = FALSE;
	DOWNGRADE_MODE = FALSE;
	popt_log_file = NULL;
	popt_server = NULL;
	popt_tmpdir = NULL;
	popt_genpkg_file = NULL;

	config_file = g_strdup (DEFAULT_CONFIG_FILE);
	target_file = g_strdup (PACKAGE_FILE_NAME);
	
	pctx = poptGetContext ("eazel-install", argc, argv, optionsTable, 0);

	while ( (opt = poptGetNextOpt (pctx)) >= 0) {
		switch (opt) {
			case 'h':
				show_usage (0, NULL);
				break;
			case 'L':
				show_license (0, NULL);
				break;
			case 'l':
				USE_LOCAL = TRUE;
				break;
			case 'w':
				USE_HTTP = TRUE;
				break;
			case 'f':
				USE_FTP = TRUE;
				break;
			case 'v':
				VERBOSE_MODE = TRUE;
				break;
			case 'S':
				LOGGING_MODE = TRUE;
				break;
			case 'F':
				FORCE_MODE = TRUE;
				break;
			case 't':
				TEST_MODE = TRUE;
				break;
			case 'u':
				UNINSTALL_MODE = TRUE;
				break;
			case 'D':
				DOWNGRADE_MODE = TRUE;
				break;
			case 'T':
				break;
			case 's':
				break;
			case 'g':
				break;
		}
	}

	if (opt < -1) {

		/* Error generated during option processing */

		fprintf (stderr, "***Option = %s: %s***\n",
				poptBadOption (pctx, POPT_BADOPTION_NOALIAS),
				poptStrerror (opt));
		exit (1);
	}

	poptFreeContext (pctx);

	retval = check_for_root_user ();
	if (!retval) {
		g_error (_("*** You must run eazel-install as root! ***\n"));
	}

	retval = check_for_redhat ();
	if (!retval) {
		g_error (_("*** eazel-install can only be used on RedHat! ***\n"));
	}

	if ( DOWNGRADE_MODE ) {
		g_error (_("*** Downgrade Mode not supported yet! ***\n"));
	}

	/* Initialize iopts and topts with defaults from the configuration file */
	g_print (_("Reading the eazel services configuration ...\n"));
	iopts = init_default_install_configuration (config_file);
	topts = init_default_transfer_configuration (config_file);

	if (popt_genpkg_file) {
		generate_new_package_list (popt_genpkg_file, target_file);
	}

	if ( LOGGING_MODE ) {
		g_error (_("*** Logging not currently supported! ***\n"));
		exit (1);
	}

	if ( USE_FTP ) {
		g_error (_("*** FTP installs are not currently supported! ***\n"));
		exit (1);
	}
	else if (USE_HTTP) {
		iopts->protocol = PROTOCOL_HTTP;
	}
	else {
		iopts->protocol = PROTOCOL_LOCAL;
	}

	if (VERBOSE_MODE) {
		iopts->mode_verbose = TRUE;
	}

	if (TEST_MODE) {
		iopts->mode_test = TRUE;
	}
	
	if (FORCE_MODE) {
		iopts->mode_force = TRUE;
	}

	if (popt_server) {
		topts->hostname = g_strdup_printf ("%s", popt_server);
	}

	if (popt_tmpdir) {
		topts->tmp_dir = g_strdup_printf ("%s", popt_tmpdir);
	}

	if (!g_file_exists (topts->tmp_dir)) {
		create_temporary_directory (topts->tmp_dir);
	}
	
	if (iopts->protocol == PROTOCOL_HTTP) {
		fetch_remote_package_list (iopts->pkg_list, topts);
	}

	if (UNINSTALL_MODE) {
		iopts->mode_uninstall = TRUE;

		retval = uninstall_packages (iopts, topts);
		if (!retval) {
			g_error (_("*** The uninstall failed! ***\n"));
		}
	}
	else {
		retval = install_new_packages (iopts, topts);
		if (!retval) {
			g_error (_("*** The install failed! ***\n"));
		}
	}

	g_print (_("Transaction completed normally...\n"));
	g_free (config_file);
	g_free (iopts);
	g_free (topts);
	exit (0);
}