Print this page
6774 - create-client should take imagepath as optional argument
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/cmd/installadm/installadm.c
+++ new/usr/src/cmd/installadm/installadm.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 */
26 26
27 27 #include <stdio.h>
28 28 #include <stdlib.h>
29 29 #include <locale.h>
30 30 #include <sys/param.h>
31 31 #include <fcntl.h>
32 32 #include <sys/types.h>
33 33 #include <sys/stat.h>
34 34 #include <unistd.h>
35 35 #include <string.h>
36 36 #include <sys/socket.h>
37 37 #include <netinet/in.h>
38 38 #include <arpa/inet.h>
39 39 #include <netdb.h>
40 40 #include <errno.h>
41 41
42 42 #include "installadm.h"
43 43
44 44 typedef int cmdfunc_t(int, char **, scfutilhandle_t *, const char *);
45 45
46 46 static cmdfunc_t do_create_service, do_delete_service;
47 47 static cmdfunc_t do_list, do_enable, do_disable;
48 48 static cmdfunc_t do_create_client, do_delete_client;
49 49 static cmdfunc_t do_add, do_remove, do_help;
50 50 static void do_opterr(int, int, const char *);
51 51 static char *progname;
52 52 static void smf_service_enable_attempt(char *);
53 53 static boolean_t check_for_enabled_install_services(scfutilhandle_t *);
54 54 static boolean_t enable_install_service(scfutilhandle_t *, char *);
55 55
56 56 char instance[sizeof (INSTALL_SERVER_FMRI_BASE) +
57 57 sizeof (INSTALL_SERVER_DEF_INST) + 1];
58 58
59 59 typedef struct cmd {
60 60 char *c_name;
61 61 cmdfunc_t *c_fn;
62 62 const char *c_usage;
63 63 boolean_t c_priv_reqd;
64 64 } cmd_t;
65 65
66 66 static cmd_t cmds[] = {
67 67 { "create-service", do_create_service,
68 68 "\tcreate-service\t[-f <bootfile>] [-n <svcname>]\n"
69 69 "\t\t\t[-i <dhcp_ip_start>] [-c <count_of_ipaddr>]\n"
70 70 "\t\t\t[-s <srcimage>] <targetdir>",
71 71 PRIV_REQD },
72 72
73 73 { "delete-service", do_delete_service,
74 74 "\tdelete-service\t[-x] <svcname>",
75 75 PRIV_REQD },
76 76
77 77 { "list", do_list,
78 78 "\tlist\t[-n <svcname>]",
79 79 PRIV_NOT_REQD },
80 80
|
↓ open down ↓ |
80 lines elided |
↑ open up ↑ |
81 81 { "enable", do_enable,
82 82 "\tenable\t<svcname>",
83 83 PRIV_REQD },
84 84
85 85 { "disable", do_disable,
86 86 "\tdisable\t[-t] <svcname>",
87 87 PRIV_REQD },
88 88
89 89 { "create-client", do_create_client,
90 90 "\tcreate-client\t[-b <property>=<value>,...] \n"
91 - "\t\t\t-e <macaddr> -t <imagepath> -n <svcname>",
91 + "\t\t\t-e <macaddr> -n <svcname> [-t <imagepath>]",
92 92 PRIV_REQD },
93 93
94 94 { "delete-client", do_delete_client,
95 95 "\tdelete-client\t<macaddr>",
96 96 PRIV_REQD },
97 97
98 98 { "add", do_add,
99 99 "\tadd\t-m <manifest> -n <svcname>",
100 100 PRIV_REQD },
101 101
102 102 { "remove", do_remove,
103 103 "\tremove\t-m <manifest> -n <svcname>",
104 104 PRIV_REQD },
105 105
106 106 { "help", do_help,
107 107 "\thelp\t[<subcommand>]",
108 108 PRIV_NOT_REQD }
109 109 };
110 110
111 111 static void
112 112 usage(void)
113 113 {
114 114 int i;
115 115 cmd_t *cmdp;
116 116
117 117 (void) fprintf(stderr, MSG_INSTALLADM_USAGE);
118 118 for (i = 0; i < sizeof (cmds) / sizeof (cmds[0]); i++) {
119 119 cmdp = &cmds[i];
120 120 if (cmdp->c_usage != NULL)
121 121 (void) fprintf(stderr, "%s\n", gettext(cmdp->c_usage));
122 122 }
123 123 exit(INSTALLADM_FAILURE);
124 124 }
125 125
126 126
127 127 int
128 128 main(int argc, char *argv[])
129 129 {
130 130 int i;
131 131 cmd_t *cmdp;
132 132 scfutilhandle_t *handle;
133 133
134 134 (void) setlocale(LC_ALL, "");
135 135
136 136 /*
137 137 * Must have at least one additional argument to installadm
138 138 */
139 139 if (argc < 2) {
140 140 usage();
141 141 }
142 142
143 143 progname = argv[0];
144 144 (void) snprintf(instance, sizeof (instance), "%s:%s",
145 145 INSTALL_SERVER_FMRI_BASE, INSTALL_SERVER_DEF_INST);
146 146 /*
147 147 * If it is valid subcommand, call the do_subcommand function
148 148 * found in cmds. Pass it the subcommand's argc and argv, as
149 149 * well as the smf handle and the subcommand specific usage.
150 150 */
151 151 for (i = 0; i < sizeof (cmds) / sizeof (cmds[0]); i++) {
152 152 int ret = 0;
153 153 cmdp = &cmds[i];
154 154 if (strcmp(argv[1], cmdp->c_name) == 0) {
155 155 if ((cmdp->c_priv_reqd) && (geteuid() > 0)) {
156 156 (void) fprintf(stderr, MSG_ROOT_PRIVS_REQD,
157 157 argv[0], cmdp->c_name);
158 158 exit(INSTALLADM_FAILURE);
159 159 }
160 160
161 161 handle = ai_scf_init();
162 162 if (handle == NULL) {
163 163 (void) fprintf(stderr, MSG_AI_SMF_INIT_FAIL);
164 164 exit(INSTALLADM_FAILURE);
165 165 }
166 166
167 167 /*
168 168 * set the umask, for all subcommands to inherit
169 169 */
170 170 (void) umask(022);
171 171
172 172 if (cmdp->c_fn(argc - 1, &argv[1], handle,
173 173 cmdp->c_usage)) {
174 174 ret = INSTALLADM_FAILURE;
175 175 } else {
176 176 ret = INSTALLADM_SUCCESS;
177 177 }
178 178
179 179 /* clean-up SMF handle */
180 180 ai_scf_fini(handle);
181 181 exit(ret);
182 182 }
183 183 }
184 184
185 185 /*
186 186 * Otherwise, give error and print usage
187 187 */
188 188 (void) fprintf(stderr, MSG_UNKNOWN_SUBCOMMAND,
189 189 progname, argv[1]);
190 190 usage();
191 191
192 192 exit(INSTALLADM_FAILURE);
193 193 }
194 194
195 195 /*
196 196 * get_ip_from_hostname:
197 197 *
198 198 * Description:
199 199 * Resolves given hostname to IPv4 address. Result is stored as string
200 200 * into given buffer. If more than one IP address is returned, the first
201 201 * one is picked.
202 202 *
203 203 * parameters:
204 204 * name - simple or fully qualified hostname to be resolved
205 205 * ip_string - pointer to string buffer where IP address will
206 206 * be stored
207 207 * buffer_size - size of ip_string
208 208 *
209 209 * return:
210 210 * 0 - success
211 211 * -1 - resolve process failed - string buffer is left untouched
212 212 */
213 213 static int
214 214 get_ip_from_hostname(char *name, char *ip_string, int buffer_size)
215 215 {
216 216 struct hostent *hp;
217 217 struct in_addr in;
218 218
219 219 hp = gethostbyname(name);
220 220 if (hp == NULL) {
221 221 return (-1);
222 222 } else {
223 223 (void) memcpy(&in.s_addr, hp->h_addr_list[0],
224 224 sizeof (in.s_addr));
225 225
226 226 (void) snprintf(ip_string, buffer_size, "%s", inet_ntoa(in));
227 227 }
228 228
229 229 return (0);
230 230 }
231 231
232 232 static int
233 233 call_script(char *scriptname, int argc, char *argv[])
234 234 {
235 235 int i;
236 236 char cmd[BUFSIZ];
237 237 char cmdargs[BUFSIZ];
238 238
239 239
240 240 cmdargs[0] = '\0';
241 241 for (i = 0; i < argc; i++) {
242 242 (void) strcat(cmdargs, argv[i]);
243 243 (void) strcat(cmdargs, " ");
244 244 }
245 245
246 246 (void) snprintf(cmd, sizeof (cmd), "%s %s",
247 247 scriptname, cmdargs);
248 248
249 249 return (installadm_system(cmd));
250 250
251 251 }
252 252
253 253 /*
254 254 * Function: check_for_enabled_install_services
255 255 * Description:
256 256 * Check to see if any of the install services are
257 257 * enabled. If not, the smf install/server service
258 258 * should be placed in maintenance.
259 259 * Parameters:
260 260 * handle - scfutilhandle_t * for use with scf calls
261 261 * Return:
262 262 * B_FALSE - Service not placed in maintenance.
263 263 * B_TRUE - No enabled services found. SMF service in maint.
264 264 * Scope:
265 265 * Private
266 266 */
267 267 static boolean_t
268 268 check_for_enabled_install_services(scfutilhandle_t *handle)
269 269 {
270 270 char *value;
271 271 ai_pg_list_t *pg_list = NULL;
272 272 ai_pg_list_t *pg = NULL;
273 273 char *state;
274 274
275 275 /*
276 276 * Are there any install services still with status "on"?
277 277 */
278 278
279 279 /* get the list of property groups */
280 280 if (ai_get_pgs(handle, &pg_list) != AI_SUCCESS) {
281 281 return (B_FALSE);
282 282 }
283 283 if ((pg = pg_list) == NULL) {
284 284 /*
285 285 * No property groups for install services. Put smf
286 286 * instance into maintenance.
287 287 */
288 288 goto out;
289 289 }
290 290 while (pg != NULL && pg->pg_name != NULL) {
291 291 /*
292 292 * for each property group read the status
293 293 */
294 294 if (ai_read_property(handle, pg->pg_name, SERVICE_STATUS,
295 295 &value) == AI_SUCCESS) {
296 296 if (strcmp(value, STATUS_ON) == 0) {
297 297 /*
298 298 * At least one is enabled. Return
299 299 * without putting in maint.
300 300 */
301 301 free(value);
302 302 ai_free_pg_list(pg_list);
303 303 return (B_FALSE);
304 304 }
305 305 }
306 306 free(value);
307 307 pg = pg->next;
308 308 }
309 309 /*
310 310 * No enabled install services. Put smf
311 311 * instance into maintenance.
312 312 */
313 313 ai_free_pg_list(pg_list);
314 314 out:
315 315 state = smf_get_state(instance);
316 316 if (strcmp(state, SCF_STATE_STRING_MAINT) == 0) {
317 317 /*
318 318 * If the service is already in maintenance don't try
319 319 * to put it there and don't send the message saying
320 320 * you're doing so.
321 321 */
322 322 (void) fprintf(stderr, MSG_SERVER_SMF_DISABLED, instance);
323 323 return (B_FALSE);
324 324 }
325 325 (void) fprintf(stderr, MSG_SERVER_SMF_OFFLINE, instance);
326 326
327 327 smf_maintain_instance(instance, SMF_IMMEDIATE);
328 328 /*
329 329 * Wait for it to really go into maintenance state.
330 330 */
331 331 do {
332 332 state = smf_get_state(instance);
333 333 } while (strcmp(state, SCF_STATE_STRING_MAINT) != 0);
334 334
335 335 (void) fprintf(stderr, MSG_SERVER_SMF_DISABLED, instance);
336 336
337 337 return (B_TRUE);
338 338 }
339 339
340 340 /*
341 341 * smf_service_enable_attempt
342 342 * Description:
343 343 * Attempt to enable the designated smf service.
344 344 * If the service goes into maintenance mode,
345 345 * return an error to the caller.
346 346 * Parameters:
347 347 * instance - The instance to attempt to enable
348 348 * Return:
349 349 * None
350 350 * Scope:
351 351 * Private
352 352 */
353 353 static void
354 354 smf_service_enable_attempt(char *instance)
355 355 {
356 356 char *orig_state = NULL;
357 357 int enable_tried = 0;
358 358
359 359 /*
360 360 * Check the service status here.
361 361 * Algorithm:
362 362 * If the service is online, everything is OK. return.
363 363 * If the service is offline, SMF is settling. Return
364 364 * or we get caught in recursion.
365 365 * If the service is disabled, try to enable it.
366 366 * If the service is in maintenance, try to clear it.
367 367 */
368 368 orig_state = smf_get_state(instance);
369 369 if (orig_state == NULL) {
370 370 (void) smf_enable_instance(instance, 0);
371 371 } else if (strcmp(orig_state, SCF_STATE_STRING_ONLINE) == 0) {
372 372 /*
373 373 * Instance is online and running.
374 374 */
375 375 free(orig_state);
376 376 return;
377 377 } else if (strcmp(orig_state, SCF_STATE_STRING_OFFLINE) == 0) {
378 378 free(orig_state);
379 379 return;
380 380 } else if (strcmp(orig_state, SCF_STATE_STRING_DISABLED) == 0) {
381 381 /*
382 382 * Instance is disabled try to enable it.
383 383 */
384 384 (void) smf_enable_instance(instance, 0);
385 385 } else if (strcmp(orig_state, SCF_STATE_STRING_MAINT) == 0) {
386 386 (void) smf_restore_instance(instance);
387 387 }
388 388 free(orig_state);
389 389
390 390 }
391 391
392 392
393 393 /*
394 394 * Function: enable_install_service
395 395 * Description:
396 396 * Enable the specified install service and update the
397 397 * service's property group.
398 398 * Parameters:
399 399 * handle - scfutilhandle_t * for use with scf calls
400 400 * service_name - service to enable
401 401 * Return:
402 402 * B_TRUE - Service enabled
403 403 * B_FALSE - If there is a failure
404 404 * Scope:
405 405 * Private
406 406 */
407 407 static boolean_t
408 408 enable_install_service(scfutilhandle_t *handle, char *service_name)
409 409 {
410 410 char *port;
411 411 service_data_t data;
412 412 char cmd[MAXPATHLEN];
413 413
414 414 if (service_name == NULL || handle == NULL) {
415 415 return (B_FALSE);
416 416 }
417 417
418 418 /*
419 419 * get the data for the service
420 420 */
421 421 if (get_service_data(handle, service_name, &data) != B_TRUE) {
422 422 (void) fprintf(stderr, MSG_SERVICE_DOESNT_EXIST,
423 423 service_name);
424 424 return (B_FALSE);
425 425 }
426 426
427 427 /*
428 428 * txt_record should be of the form
429 429 * "aiwebserver=<host_ip>:<port>" and the directory location
430 430 * will be AI_SERVICE_DIR_PATH/<port>
431 431 */
432 432 port = strrchr(data.txt_record, ':');
433 433
434 434 if (port == NULL) {
435 435 (void) fprintf(stderr, MSG_SERVICE_PORT_MISSING,
436 436 service_name, data.txt_record);
437 437 return (B_FALSE);
438 438 }
439 439
440 440 /*
441 441 * Exclude colon from string (so advance one character)
442 442 */
443 443
444 444 port++;
445 445 snprintf(cmd, sizeof (cmd), "%s %s %s %s %s %s %s %s",
446 446 SETUP_SERVICE_SCRIPT, SERVICE_REGISTER,
447 447 service_name, INSTALL_TYPE,
448 448 LOCAL_DOMAIN, port, data.txt_record, data.image_path);
449 449 if (installadm_system(cmd) != 0) {
450 450 (void) fprintf(stderr, MSG_REGISTER_SERVICE_FAIL,
451 451 service_name);
452 452 return (B_FALSE);
453 453 }
454 454
455 455 /*
456 456 * Update status in service's property group
457 457 */
458 458 strlcpy(data.status, STATUS_ON, STATUSLEN);
459 459 if (save_service_data(handle, data) != B_TRUE) {
460 460 (void) fprintf(stderr, MSG_SAVE_SERVICE_PROPS_FAIL,
461 461 service_name);
462 462 return (B_FALSE);
463 463 }
464 464
465 465 /* ensure install service is online */
466 466 smf_service_enable_attempt(instance);
467 467
468 468 return (B_TRUE);
469 469 }
470 470
471 471 /*
472 472 * do_create_service:
473 473 * This function parses the command line arguments and sets up
474 474 * the image, the DNS service, the network configuration for the
475 475 * the clients to boot from this image (/tftpboot) and dhcp if desired.
476 476 * This function calls shell scripts to handle each of the tasks
477 477 */
478 478 static int
479 479 do_create_service(
480 480 int argc,
481 481 char *argv[],
482 482 scfutilhandle_t *handle,
483 483 const char *use)
484 484 {
485 485 int opt;
486 486 boolean_t named_service = B_FALSE;
487 487 boolean_t named_boot_file = B_FALSE;
488 488 boolean_t dhcp_setup_needed = B_FALSE;
489 489 boolean_t create_netimage = B_FALSE;
490 490 boolean_t create_service = B_FALSE;
491 491 boolean_t have_sparc = B_FALSE;
492 492
493 493 char *boot_file = NULL;
494 494 char *ip_start = NULL;
495 495 short ip_count;
496 496 char *service_name = NULL;
497 497 char *source_path = NULL;
498 498 char *target_directory = NULL;
499 499
500 500 struct stat stat_buf;
501 501 struct stat sb;
502 502 char cmd[MAXPATHLEN];
503 503 char mpath[MAXPATHLEN];
504 504 char bfile[MAXPATHLEN];
505 505 char server_hostname[DATALEN];
506 506 char server_ip[DATALEN];
507 507 char srv_name[MAXPATHLEN];
508 508 char srv_address[DATALEN] = "unknown";
509 509 char txt_record[DATALEN];
510 510 char dhcp_macro[MAXNAMELEN+12]; /* dhcp_macro_<filename> */
511 511 int size;
512 512 service_data_t data;
513 513 char *pg_name;
514 514
515 515 while ((opt = getopt(argc, argv, ":f:n:i:c:s:")) != -1) {
516 516 switch (opt) {
517 517 /*
518 518 * Create a boot file for this service with the supplied name
519 519 */
520 520 case 'f':
521 521 named_boot_file = B_TRUE;
522 522 boot_file = optarg;
523 523 break;
524 524 /*
525 525 * The name of the service is supplied.
526 526 */
527 527 case 'n':
528 528 if (!validate_service_name(optarg)) {
529 529 (void) fprintf(stderr, MSG_BAD_SERVICE_NAME);
530 530 return (INSTALLADM_FAILURE);
531 531 }
532 532 named_service = B_TRUE;
533 533 service_name = optarg;
534 534 break;
535 535 /*
536 536 * The starting IP address is supplied.
537 537 */
538 538 case 'i':
539 539 dhcp_setup_needed = B_TRUE;
540 540 ip_start = optarg;
541 541 break;
542 542 /*
543 543 * Number of IP addresses to be setup
544 544 */
545 545 case 'c':
546 546 ip_count = atoi(optarg);
547 547 break;
548 548 /*
549 549 * Source image is supplied.
550 550 */
551 551 case 's':
552 552 create_netimage = B_TRUE;
553 553 source_path = optarg;
554 554 break;
555 555 default:
556 556 (void) fprintf(stderr, "%s\n", gettext(use));
557 557 return (INSTALLADM_FAILURE);
558 558 }
559 559 }
560 560
561 561 /*
562 562 * The last argument is the target directory.
563 563 */
564 564 target_directory = argv[optind++];
565 565
566 566 if (target_directory == NULL) {
567 567 (void) fprintf(stderr, "%s\n", gettext(use));
568 568 return (INSTALLADM_FAILURE);
569 569 }
570 570
571 571 /*
572 572 * Verify that the server settings are not obviously broken.
573 573 * These checks cannot be complete, but check for things which will
574 574 * definitely cause failure.
575 575 */
576 576 (void) snprintf(cmd, sizeof (cmd), "%s %s",
577 577 CHECK_SETUP_SCRIPT, ((ip_start != NULL) ? ip_start : ""));
578 578 if (installadm_system(cmd) != 0) {
579 579 (void) fprintf(stderr, MSG_BAD_SERVER_SETUP);
580 580 return (INSTALLADM_FAILURE);
581 581 }
582 582
583 583 /*
584 584 * obtain server hostname and resolve it to IP address
585 585 * If this operation fails, something is wrong with network
586 586 * configuration - exit
587 587 */
588 588 if (gethostname(server_hostname, sizeof (server_hostname)) != 0) {
589 589 (void) fprintf(stderr, MSG_GET_HOSTNAME_FAIL);
590 590 return (INSTALLADM_FAILURE);
591 591 }
592 592
593 593 /* resolve host name to IP address */
594 594 if (get_ip_from_hostname(server_hostname, server_ip,
595 595 sizeof (server_ip)) != 0) {
596 596 (void) fprintf(stderr, MSG_GET_HOSTNAME_FAIL);
597 597 return (INSTALLADM_FAILURE);
598 598 }
599 599
600 600 /*
601 601 * Check whether target exists
602 602 * If it doesn't exist, the setup-image script will
603 603 * create the directory.
604 604 * If it exists, check whether it has a valid net image
605 605 */
606 606 if (access(target_directory, F_OK) == 0) {
607 607 if (stat(target_directory, &stat_buf) == 0) {
608 608 char path[MAXPATHLEN];
609 609 /*
610 610 * If the directory is empty, then it is okay
611 611 */
612 612 if (stat_buf.st_nlink > 2) {
613 613 /*
614 614 * Check whether it has valid file solaris.zlib
615 615 */
616 616 (void) snprintf(path, sizeof (path), "%s/%s",
617 617 target_directory,
618 618 AI_NETIMAGE_REQUIRED_FILE);
619 619 if (access(path, R_OK) != 0) {
620 620 (void) fprintf(stderr,
621 621 MSG_TARGET_NOT_EMPTY);
622 622 return (INSTALLADM_FAILURE);
623 623 }
624 624 /*
625 625 * Already have an image. We can't create a
626 626 * new one w/o removing the old one.
627 627 * Display error
628 628 */
629 629 if (create_netimage) {
630 630 (void) fprintf(stderr,
631 631 MSG_VALID_IMAGE_ERR,
632 632 target_directory);
633 633 return (INSTALLADM_FAILURE);
634 634 }
635 635 }
636 636 } else {
637 637 (void) fprintf(stderr,
638 638 MSG_DIRECTORY_ACCESS_ERR,
639 639 target_directory, errno);
640 640 return (INSTALLADM_FAILURE);
641 641 }
642 642 }
643 643
644 644 /*
645 645 * call the script to create the netimage
646 646 */
647 647 if (create_netimage) {
648 648 (void) snprintf(cmd, sizeof (cmd), "%s %s %s %s",
649 649 SETUP_IMAGE_SCRIPT, IMAGE_CREATE,
650 650 source_path, target_directory);
651 651 if (installadm_system(cmd) != 0) {
652 652 (void) fprintf(stderr, MSG_CREATE_IMAGE_ERR);
653 653 return (INSTALLADM_FAILURE);
654 654 }
655 655 }
656 656
657 657 /*
658 658 * Check whether image is sparc or x86 by checking existence
659 659 * of key directories
660 660 */
661 661 (void) snprintf(mpath, sizeof (mpath), "%s/%s", target_directory,
662 662 "platform/sun4v");
663 663 if ((stat(mpath, &sb) == 0) && S_ISDIR(sb.st_mode)) {
664 664 have_sparc = B_TRUE;
665 665 } else {
666 666 (void) snprintf(mpath, sizeof (mpath), "%s/%s",
667 667 target_directory, "platform/i86pc");
668 668 if (stat(mpath, &sb) || !S_ISDIR(sb.st_mode)) {
669 669 (void) fprintf(stderr, MSG_UNABLE_TO_DETERMINE_ARCH);
670 670 return (INSTALLADM_FAILURE);
671 671 }
672 672 }
673 673
674 674 /*
675 675 * The net-image is created, now start the service
676 676 * If the user provided the name of the service, use it
677 677 */
678 678 txt_record[0] = '\0';
679 679 srv_name[0] = '\0';
680 680 if (named_service) {
681 681 /*
682 682 * Check to see if service exists
683 683 */
684 684 if (service_exists(handle, service_name)) {
685 685 if (!have_sparc) {
686 686 /*
687 687 * We need to remove the existing entry in
688 688 * /etc/vfstab before adding the new entry
689 689 * and updating the smf information.
690 690 * X86 only
691 691 */
692 692 snprintf(cmd, sizeof (cmd), "%s %s %s",
693 693 SETUP_TFTP_LINKS_SCRIPT, TFTP_REMOVE_VFSTAB,
694 694 service_name);
695 695
696 696 if (installadm_system(cmd) != 0) {
697 697 (void) fprintf(stderr,
698 698 MSG_SERVICE_REMOVE_VFSTAB_FAILED,
699 699 service_name);
700 700 return (INSTALLADM_FAILURE);
701 701 }
702 702 }
703 703 /*
704 704 * Service exists. Make sure it is enabled.
705 705 */
706 706 if (!enable_install_service(handle, service_name)) {
707 707 return (INSTALLADM_FAILURE);
708 708 }
709 709
710 710 /*
711 711 * Service now running, save txt record info
712 712 */
713 713 if (get_service_data(handle, service_name, &data) !=
714 714 B_TRUE) {
715 715 (void) fprintf(stderr, MSG_SERVICE_DOESNT_EXIST,
716 716 service_name);
717 717 return (INSTALLADM_FAILURE);
718 718 }
719 719 strlcpy(txt_record, data.txt_record,
720 720 sizeof (txt_record));
721 721 } else {
722 722 /*
723 723 * Named service does not exist, create it below
724 724 */
725 725 create_service = B_TRUE;
726 726 }
727 727 strlcpy(srv_name, service_name, sizeof (srv_name));
728 728 } else {
729 729 /*
730 730 * The service is not given as input. We will generate
731 731 * a service name and start the service.
732 732 */
733 733 create_service = B_TRUE;
734 734 }
735 735
736 736 if (create_service) {
737 737 uint16_t wsport;
738 738
739 739 wsport = get_a_free_tcp_port(handle, START_WEB_SERVER_PORT);
740 740 if (wsport == 0) {
741 741 (void) fprintf(stderr, MSG_CANNOT_FIND_PORT);
742 742 return (INSTALLADM_FAILURE);
743 743 }
744 744 snprintf(txt_record, sizeof (txt_record), "%s=%s:%u",
745 745 AIWEBSERVER, server_hostname, wsport);
746 746 if (!named_service) {
747 747 snprintf(srv_name, sizeof (srv_name),
748 748 "_install_service_%u", wsport);
749 749 }
750 750
751 751 snprintf(cmd, sizeof (cmd), "%s %s %s %s %s %u %s %s",
752 752 SETUP_SERVICE_SCRIPT, SERVICE_REGISTER,
753 753 srv_name, INSTALL_TYPE,
754 754 LOCAL_DOMAIN, wsport, txt_record, target_directory);
755 755 if (installadm_system(cmd) != 0) {
756 756 (void) fprintf(stderr,
757 757 MSG_REGISTER_SERVICE_FAIL, srv_name);
758 758 return (INSTALLADM_FAILURE);
759 759 }
760 760
761 761 /*
762 762 * save location of service in format <server_ip_address>:<port>
763 763 * It will be used later for setting service discovery fallback
764 764 * mechanism
765 765 */
766 766
767 767 snprintf(srv_address, sizeof (srv_address), "%s:%u",
768 768 server_ip, wsport);
769 769 }
770 770
771 771 bfile[0] = '\0';
772 772 if (named_boot_file) {
773 773 strlcpy(bfile, boot_file, sizeof (bfile));
774 774 } else {
775 775 strlcpy(bfile, srv_name, sizeof (bfile));
776 776 }
777 777
778 778 /*
779 779 * Register the information about the service, image and boot file
780 780 * so that it can be used later
781 781 */
782 782 pg_name = ai_make_pg_name(srv_name);
783 783 if (pg_name == NULL) {
784 784 (void) fprintf(stderr, MSG_GET_PG_NAME_FAILED, srv_name);
785 785 return (INSTALLADM_FAILURE);
786 786 }
787 787 if (ai_create_pg(handle, pg_name) != AI_SUCCESS) {
788 788 free(pg_name);
789 789 (void) fprintf(stderr, MSG_CREATE_INSTALL_SERVICE_FAILED,
790 790 srv_name);
791 791 return (INSTALLADM_FAILURE);
792 792 }
793 793 free(pg_name);
794 794
795 795 strlcpy(data.svc_name, srv_name, DATALEN);
796 796 strlcpy(data.image_path, target_directory, MAXPATHLEN);
797 797 strlcpy(data.boot_file, bfile, MAXNAMELEN);
798 798 strlcpy(data.txt_record, txt_record, MAX_TXT_RECORD_LEN);
799 799 strlcpy(data.status, STATUS_ON, STATUSLEN);
800 800
801 801 if (save_service_data(handle, data) != B_TRUE) {
802 802 (void) fprintf(stderr, MSG_SAVE_SERVICE_PROPS_FAIL,
803 803 data.svc_name);
804 804 return (INSTALLADM_FAILURE);
805 805 }
806 806
807 807 /*
808 808 * Setup dhcp
809 809 */
810 810 if (dhcp_setup_needed && create_netimage) {
811 811 snprintf(cmd, sizeof (cmd), "%s %s %s %d",
812 812 SETUP_DHCP_SCRIPT, DHCP_SERVER, ip_start, ip_count);
813 813 if (installadm_system(cmd) != 0) {
814 814 (void) fprintf(stderr,
815 815 MSG_CREATE_DHCP_SERVER_ERR);
816 816 return (INSTALLADM_FAILURE);
817 817 }
818 818 }
819 819
820 820 if (create_netimage) {
821 821 char dhcpbfile[MAXPATHLEN];
822 822 char dhcprpath[MAXPATHLEN];
823 823
824 824 snprintf(dhcp_macro, sizeof (dhcp_macro),
825 825 "dhcp_macro_%s", bfile);
826 826
827 827 /*
828 828 * determine contents of bootfile info passed to dhcp script
829 829 * as well as rootpath for sparc
830 830 */
831 831 if (have_sparc) {
832 832 snprintf(dhcpbfile, sizeof (dhcpbfile),
833 833 "http://%s:%s/%s", server_ip, HTTP_PORT,
834 834 WANBOOTCGI);
835 835 snprintf(dhcprpath, sizeof (dhcprpath),
836 836 "http://%s:%s%s", server_ip, HTTP_PORT,
837 837 target_directory);
838 838 } else {
839 839 strlcpy(dhcpbfile, bfile, sizeof (dhcpbfile));
840 840 }
841 841
842 842 snprintf(cmd, sizeof (cmd), "%s %s %s %s %s %s",
843 843 SETUP_DHCP_SCRIPT, DHCP_MACRO, have_sparc?"sparc":"x86",
844 844 server_ip, dhcp_macro, dhcpbfile);
845 845 /*
846 846 * The setup-dhcp script takes care of printing output for the
847 847 * user so there is no need to print anything for non-zero
848 848 * return value.
849 849 */
850 850 installadm_system(cmd);
851 851 }
852 852
853 853 if (dhcp_setup_needed && create_netimage) {
854 854 snprintf(cmd, sizeof (cmd), "%s %s %s %d %s",
855 855 SETUP_DHCP_SCRIPT, DHCP_ASSIGN,
856 856 ip_start, ip_count, dhcp_macro);
857 857 if (installadm_system(cmd) != 0) {
858 858 (void) fprintf(stderr,
859 859 MSG_ASSIGN_DHCP_MACRO_ERR);
860 860 }
861 861 }
862 862
863 863 /*
864 864 * Perform sparc/x86 specific actions.
865 865 */
866 866 if (have_sparc) {
867 867 /* sparc only */
868 868 snprintf(cmd, sizeof (cmd), "%s %s %s %s %s",
869 869 SETUP_SPARC_SCRIPT, SPARC_SERVER, target_directory,
870 870 srv_name, srv_address);
871 871
872 872 if (installadm_system(cmd) != 0) {
873 873 (void) fprintf(stderr, MSG_SETUP_SPARC_FAIL);
874 874 return (INSTALLADM_FAILURE);
875 875 }
876 876 } else {
877 877 /* x86 only */
878 878 snprintf(cmd, sizeof (cmd), "%s %s %s %s %s %s",
879 879 SETUP_TFTP_LINKS_SCRIPT, TFTP_SERVER, srv_name,
880 880 srv_address, target_directory, bfile);
881 881
882 882 if (installadm_system(cmd) != 0) {
883 883 (void) fprintf(stderr, MSG_CREATE_TFTPBOOT_FAIL);
884 884 return (INSTALLADM_FAILURE);
885 885 }
886 886 }
887 887
888 888 /* if needed, enable install service */
889 889 smf_service_enable_attempt(instance);
890 890
891 891 return (INSTALLADM_SUCCESS);
892 892 }
893 893
894 894 /*
895 895 * do_delete_service:
896 896 * Simply call SERVICE_DELETE_SCRIPT
897 897 * All service deletion is handled and done in the SERVICE_DELETE_SCRIPT
898 898 */
899 899 static int
900 900 do_delete_service(
901 901 int argc,
902 902 char *argv[],
903 903 scfutilhandle_t *handle,
904 904 const char *use)
905 905 {
906 906 char cmd[MAXPATHLEN];
907 907 char *service;
908 908 boolean_t delete_image = B_FALSE;
909 909
910 910 if (argc != 2 && argc != 3) {
911 911 (void) fprintf(stderr, "%s\n", gettext(use));
912 912 return (INSTALLADM_FAILURE);
913 913 }
914 914
915 915 if (argc == 3) {
916 916 if (strcmp(argv[1], "-x") != 0) {
917 917 (void) fprintf(stderr, "%s\n", gettext(use));
918 918 return (INSTALLADM_FAILURE);
919 919 }
920 920 delete_image = B_TRUE;
921 921 service = argv[2];
922 922 } else {
923 923 service = argv[1];
924 924 }
925 925
926 926 if (!validate_service_name(service)) {
927 927 (void) fprintf(stderr, MSG_BAD_SERVICE_NAME);
928 928 return (INSTALLADM_FAILURE);
929 929 }
930 930
931 931 /* if delete_image is true we are removing the image, pass -x flag */
932 932 if (delete_image == B_TRUE) {
933 933 snprintf(cmd, sizeof (cmd), "%s -x %s",
934 934 SERVICE_DELETE_SCRIPT, service);
935 935 }
936 936 /* if delete_image is false we are not removing the image */
937 937 else {
938 938 snprintf(cmd, sizeof (cmd), "%s %s",
939 939 SERVICE_DELETE_SCRIPT, service);
940 940 }
941 941 if (installadm_system(cmd) == 0) {
942 942 return (INSTALLADM_SUCCESS);
943 943 }
944 944 return (INSTALLADM_FAILURE);
945 945 }
946 946
947 947 /*
948 948 * do_list:
949 949 * List A/I services or print service manifests and criteria
950 950 * Parse the command line for service name; if we do not have one, then
951 951 * print a list of installed services; if we have a service name, get the
952 952 * service directory path from that service name; then pass service directory
953 953 * path to list-manifests(1) (if the internal -c option is provided pass it
954 954 * to list-manifests(1) as well).
955 955 */
956 956 static int
957 957 do_list(int argc, char *argv[], scfutilhandle_t *handle, const char *use)
958 958 {
959 959 int opt;
960 960 char *port = NULL;
961 961 char *service_name = NULL;
962 962 boolean_t print_criteria = B_FALSE;
963 963 char cmd[MAXPATHLEN];
964 964 int ret;
965 965 service_data_t data;
966 966
967 967 /*
968 968 * The -c option is an internal option
969 969 */
970 970 while ((opt = getopt(argc, argv, "n:c")) != -1) {
971 971 switch (opt) {
972 972 /*
973 973 * The name of the service is supplied.
974 974 */
975 975 case 'n':
976 976 if (!validate_service_name(optarg)) {
977 977 (void) fprintf(stderr, MSG_BAD_SERVICE_NAME);
978 978 return (INSTALLADM_FAILURE);
979 979 }
980 980 service_name = optarg;
981 981 break;
982 982 case 'c':
983 983 print_criteria = B_TRUE;
984 984 break;
985 985 default:
986 986 (void) fprintf(stderr, "%s\n", gettext(use));
987 987 return (INSTALLADM_FAILURE);
988 988 }
989 989 }
990 990
991 991 /*
992 992 * Make sure correct option combinations
993 993 */
994 994 if ((print_criteria == B_TRUE) && (service_name == NULL)) {
995 995 (void) fprintf(stderr, MSG_MISSING_OPTIONS, argv[0]);
996 996 (void) fprintf(stderr, "%s\n", gettext(use));
997 997 return (INSTALLADM_FAILURE);
998 998 }
999 999
1000 1000 if (service_name != NULL) {
1001 1001 /*
1002 1002 * Get the list of published manifests from the service
1003 1003 */
1004 1004 /*
1005 1005 * Gather the directory location of the service
1006 1006 */
1007 1007 if (get_service_data(handle, service_name, &data) != B_TRUE) {
1008 1008 (void) fprintf(stderr, MSG_SERVICE_PROP_FAIL);
1009 1009 return (INSTALLADM_FAILURE);
1010 1010 }
1011 1011 /*
1012 1012 * txt_record should be of the form
1013 1013 * "aiwebserver=<host_ip>:<port>" and the directory location
1014 1014 * will be AI_SERVICE_DIR_PATH/<port>
1015 1015 */
1016 1016 port = strrchr(data.txt_record, ':');
1017 1017
1018 1018 if (port == NULL) {
1019 1019 (void) fprintf(stderr, MSG_SERVICE_PORT_MISSING,
1020 1020 service_name, data.txt_record);
1021 1021 return (INSTALLADM_FAILURE);
1022 1022 }
1023 1023
1024 1024 /*
1025 1025 * Exclude colon from string (so advance one character)
1026 1026 */
1027 1027 port++;
1028 1028
1029 1029 /*
1030 1030 * Print criteria if requested
1031 1031 */
1032 1032 if (print_criteria == B_TRUE) {
1033 1033 (void) snprintf(cmd, sizeof (cmd), "%s %s %s%s",
1034 1034 MANIFEST_LIST_SCRIPT, "-c", AI_SERVICE_DIR_PATH,
1035 1035 port);
1036 1036 } else {
1037 1037 (void) snprintf(cmd, sizeof (cmd), "%s %s%s",
1038 1038 MANIFEST_LIST_SCRIPT, AI_SERVICE_DIR_PATH,
1039 1039 port);
1040 1040 }
1041 1041
1042 1042 ret = installadm_system(cmd);
1043 1043
1044 1044 /*
1045 1045 * Ensure we return an error if ret != 0.
1046 1046 * If ret == 1 then the Python handled the error, do not print a
1047 1047 * new error.
1048 1048 */
1049 1049 if (ret != 0) {
1050 1050 if (ret == 256) {
1051 1051 return (INSTALLADM_FAILURE);
1052 1052 }
1053 1053 (void) fprintf(stderr, MSG_SUBCOMMAND_FAILED, argv[0]);
1054 1054 return (INSTALLADM_FAILURE);
1055 1055 }
1056 1056
1057 1057 } else {
1058 1058 /*
1059 1059 * Get the list of services running on this system
1060 1060 */
1061 1061
1062 1062 snprintf(cmd, sizeof (cmd), "%s %s %s %s",
1063 1063 SETUP_SERVICE_SCRIPT, SERVICE_LIST,
1064 1064 INSTALL_TYPE, LOCAL_DOMAIN);
1065 1065 ret = installadm_system(cmd);
1066 1066 if (ret != 0) {
1067 1067 (void) fprintf(stderr, MSG_LIST_SERVICE_FAIL);
1068 1068 return (INSTALLADM_FAILURE);
1069 1069 }
1070 1070 }
1071 1071
1072 1072 return (INSTALLADM_SUCCESS);
1073 1073 }
1074 1074
1075 1075 /*
1076 1076 * do_enable:
1077 1077 * do_enable verifies syntax and then calls enable_install_service to
1078 1078 * enable the service.
1079 1079 */
1080 1080 static int
1081 1081 do_enable(int argc, char *argv[], scfutilhandle_t *handle, const char *use)
1082 1082 {
1083 1083 char *service_name;
1084 1084
1085 1085 if (argc != 2) {
1086 1086 (void) fprintf(stderr, "%s\n", gettext(use));
1087 1087 return (INSTALLADM_FAILURE);
1088 1088 }
1089 1089
1090 1090 if (!validate_service_name(argv[1])) {
1091 1091 (void) fprintf(stderr, MSG_BAD_SERVICE_NAME);
1092 1092 return (INSTALLADM_FAILURE);
1093 1093 }
1094 1094 service_name = argv[1];
1095 1095
1096 1096 if (! enable_install_service(handle, service_name)) {
1097 1097 return (INSTALLADM_FAILURE);
1098 1098 }
1099 1099
1100 1100 return (INSTALLADM_SUCCESS);
1101 1101 }
1102 1102
1103 1103 /*
1104 1104 * do_disable:
1105 1105 * Disable the specified service and optionally update the service's
1106 1106 * properties to reflect the new status.
1107 1107 * If the -t flag is specified, the service property group should not
1108 1108 * be updated to status=off. If -t is not specified it should be.
1109 1109 */
1110 1110 static int
1111 1111 do_disable(int argc, char *argv[], scfutilhandle_t *handle, const char *use)
1112 1112 {
1113 1113 char cmd[MAXPATHLEN];
1114 1114 char *service_name;
1115 1115 service_data_t data;
1116 1116 boolean_t transient = B_FALSE;
1117 1117 int option;
1118 1118
1119 1119 while ((option = getopt(argc, argv, "t")) != -1) {
1120 1120 switch (option) {
1121 1121 case 't':
1122 1122 transient = B_TRUE;
1123 1123 break;
1124 1124 default:
1125 1125 do_opterr(optopt, option, use);
1126 1126 return (INSTALLADM_FAILURE);
1127 1127 }
1128 1128 }
1129 1129
1130 1130 service_name = argv[optind++];
1131 1131 if (service_name == NULL) {
1132 1132 (void) fprintf(stderr, "%s\n", gettext(use));
1133 1133 return (INSTALLADM_FAILURE);
1134 1134 }
1135 1135 if (!validate_service_name(service_name)) {
1136 1136 (void) fprintf(stderr, MSG_BAD_SERVICE_NAME);
1137 1137 return (INSTALLADM_FAILURE);
1138 1138 }
1139 1139
1140 1140 /*
1141 1141 * make sure the service exists
1142 1142 */
1143 1143 if (get_service_data(handle, service_name, &data) != B_TRUE) {
1144 1144 (void) fprintf(stderr, MSG_SERVICE_DOESNT_EXIST,
1145 1145 service_name);
1146 1146 return (INSTALLADM_FAILURE);
1147 1147 }
1148 1148
1149 1149 if (strcasecmp(data.status, STATUS_OFF) == 0) {
1150 1150 (void) fprintf(stderr, MSG_SERVICE_NOT_RUNNING,
1151 1151 service_name);
1152 1152 return (INSTALLADM_FAILURE);
1153 1153 }
1154 1154
1155 1155 if (transient == B_FALSE) {
1156 1156 /*
1157 1157 * Update status in service's property group
1158 1158 */
1159 1159 strlcpy(data.status, STATUS_OFF, STATUSLEN);
1160 1160 if (save_service_data(handle, data) != B_TRUE) {
1161 1161 (void) fprintf(stderr, MSG_SAVE_SERVICE_PROPS_FAIL,
1162 1162 service_name);
1163 1163 return (INSTALLADM_FAILURE);
1164 1164 }
1165 1165
1166 1166 /*
1167 1167 * if no longer needed, puts install instance into
1168 1168 * maintenance
1169 1169 */
1170 1170 (void) check_for_enabled_install_services(handle);
1171 1171 }
1172 1172
1173 1173 /*
1174 1174 * Stop the service
1175 1175 */
1176 1176 snprintf(cmd, sizeof (cmd), "%s %s %s %s %s",
1177 1177 SETUP_SERVICE_SCRIPT, SERVICE_DISABLE,
1178 1178 service_name, INSTALL_TYPE, LOCAL_DOMAIN);
1179 1179 if (installadm_system(cmd) != 0) {
1180 1180 /*
1181 1181 * Print informational message. This
1182 1182 * will happen if service was already stopped.
1183 1183 */
1184 1184 (void) fprintf(stderr,
1185 1185 MSG_SERVICE_WASNOT_RUNNING, service_name);
1186 1186 return (INSTALLADM_FAILURE);
1187 1187 }
1188 1188
1189 1189 return (INSTALLADM_SUCCESS);
1190 1190
1191 1191 }
1192 1192
1193 1193 static int
1194 1194 do_create_client(
1195 1195 int argc,
1196 1196 char *argv[],
1197 1197 scfutilhandle_t *handle,
1198 1198 const char *use)
1199 1199 {
1200 1200
1201 1201 int option;
1202 1202 int ret;
1203 1203 char *mac_addr = NULL;
1204 1204 char *bootargs = NULL;
1205 1205 char *imagepath = NULL;
1206 1206 char *svcname = NULL;
1207 1207
1208 1208 while ((option = getopt(argc, argv, ":b:e:n:t:")) != -1) {
1209 1209 switch (option) {
1210 1210 case 'b':
1211 1211 bootargs = optarg;
1212 1212 break;
1213 1213 case 'e':
1214 1214 mac_addr = optarg;
1215 1215 break;
1216 1216 case 'n':
1217 1217 svcname = optarg;
1218 1218 break;
1219 1219 case 't':
1220 1220 imagepath = optarg;
|
↓ open down ↓ |
1119 lines elided |
↑ open up ↑ |
1221 1221 break;
1222 1222 default:
1223 1223 do_opterr(optopt, option, use);
1224 1224 return (INSTALLADM_FAILURE);
1225 1225 }
1226 1226 }
1227 1227
1228 1228 /*
1229 1229 * Make sure required options are there
1230 1230 */
1231 - if ((mac_addr == NULL) || (svcname == NULL) || (imagepath == NULL)) {
1231 + if ((mac_addr == NULL) || (svcname == NULL)) {
1232 1232 (void) fprintf(stderr, MSG_MISSING_OPTIONS, argv[0]);
1233 1233 (void) fprintf(stderr, "%s\n", gettext(use));
1234 1234 return (INSTALLADM_FAILURE);
1235 1235 }
1236 1236
1237 1237 if (!validate_service_name(svcname)) {
1238 1238 (void) fprintf(stderr, MSG_BAD_SERVICE_NAME);
1239 1239 return (INSTALLADM_FAILURE);
1240 1240 }
1241 1241
1242 1242 ret = call_script(CREATE_CLIENT_SCRIPT, argc-1, &argv[1]);
1243 1243 if (ret != 0) {
1244 1244 return (INSTALLADM_FAILURE);
1245 1245 }
1246 1246
1247 1247 /* if not enabled, enable install service */
1248 1248 if (!check_for_enabled_install_services(handle)) {
1249 1249 smf_service_enable_attempt(instance);
1250 1250 }
1251 1251
1252 1252 return (INSTALLADM_SUCCESS);
1253 1253 }
1254 1254
1255 1255
1256 1256 static int
1257 1257 do_delete_client(
1258 1258 int argc,
1259 1259 char *argv[],
1260 1260 scfutilhandle_t *handle,
1261 1261 const char *use)
1262 1262 {
1263 1263 int ret;
1264 1264 char cmd[MAXPATHLEN];
1265 1265
1266 1266 /*
1267 1267 * There is one required argument, mac_addr of client
1268 1268 */
1269 1269 if (argc != 2) {
1270 1270 (void) fprintf(stderr, "%s\n", gettext(use));
1271 1271 return (INSTALLADM_FAILURE);
1272 1272 }
1273 1273
1274 1274 snprintf(cmd, sizeof (cmd), "%s %s",
1275 1275 DELETE_CLIENT_SCRIPT, argv[1]);
1276 1276 if (installadm_system(cmd) == 0) {
1277 1277 return (INSTALLADM_SUCCESS);
1278 1278 }
1279 1279 return (INSTALLADM_FAILURE);
1280 1280 }
1281 1281
1282 1282 /*
1283 1283 * do_add:
1284 1284 * Add manifests to an A/I service
1285 1285 * Parse command line for criteria manifest and service name; get service
1286 1286 * directory path from service name; then pass manifest and service directory
1287 1287 * path to publish-manifest(1)
1288 1288 */
1289 1289 static int
1290 1290 do_add(int argc, char *argv[], scfutilhandle_t *handle, const char *use)
1291 1291 {
1292 1292 int option = NULL;
1293 1293 char *port = NULL;
1294 1294 char *manifest = NULL;
1295 1295 char *svcname = NULL;
1296 1296 char cmd[MAXPATHLEN];
1297 1297 int ret;
1298 1298 service_data_t data;
1299 1299
1300 1300 /*
1301 1301 * Check for valid number of arguments
1302 1302 */
1303 1303 if (argc != 5) {
1304 1304 (void) fprintf(stderr, "%s\n", gettext(use));
1305 1305 return (INSTALLADM_FAILURE);
1306 1306 }
1307 1307
1308 1308 while ((option = getopt(argc, argv, ":n:m:")) != -1) {
1309 1309 switch (option) {
1310 1310 case 'n':
1311 1311 svcname = optarg;
1312 1312 break;
1313 1313 case 'm':
1314 1314 manifest = optarg;
1315 1315 break;
1316 1316 default:
1317 1317 do_opterr(optopt, option, use);
1318 1318 return (INSTALLADM_FAILURE);
1319 1319 }
1320 1320 }
1321 1321
1322 1322 /*
1323 1323 * Make sure required options are there
1324 1324 */
1325 1325 if ((svcname == NULL) || (manifest == NULL)) {
1326 1326 (void) fprintf(stderr, MSG_MISSING_OPTIONS, argv[0]);
1327 1327 (void) fprintf(stderr, "%s\n", gettext(use));
1328 1328 return (INSTALLADM_FAILURE);
1329 1329 }
1330 1330
1331 1331 if (!validate_service_name(svcname)) {
1332 1332 (void) fprintf(stderr, MSG_BAD_SERVICE_NAME);
1333 1333 return (INSTALLADM_FAILURE);
1334 1334 }
1335 1335
1336 1336 /*
1337 1337 * Gather the directory location of the service
1338 1338 */
1339 1339 if (get_service_data(handle, svcname, &data) != B_TRUE) {
1340 1340 (void) fprintf(stderr, MSG_SERVICE_PROP_FAIL);
1341 1341 return (INSTALLADM_FAILURE);
1342 1342 }
1343 1343 /*
1344 1344 * txt_record should be of the form
1345 1345 * "aiwebserver=<host_ip>:<port>"
1346 1346 * and the directory location will be AI_SERVICE_DIR_PATH/<port>
1347 1347 */
1348 1348 port = strrchr(data.txt_record, ':');
1349 1349
1350 1350 if (port == NULL) {
1351 1351 (void) fprintf(stderr, MSG_SERVICE_PORT_MISSING,
1352 1352 svcname, data.txt_record);
1353 1353 return (INSTALLADM_FAILURE);
1354 1354 }
1355 1355 /*
1356 1356 * Exclude colon from string (so advance one character)
1357 1357 */
1358 1358 port++;
1359 1359 (void) snprintf(cmd, sizeof (cmd), "%s %s %s %s%s %s",
1360 1360 MANIFEST_MODIFY_SCRIPT, "-c",
1361 1361 manifest, AI_SERVICE_DIR_PATH, port, data.image_path);
1362 1362
1363 1363 ret = installadm_system(cmd);
1364 1364
1365 1365 /*
1366 1366 * Ensure we return an error if ret != 0.
1367 1367 * If ret == 1 then the Python handled the error, do not print a
1368 1368 * new error.
1369 1369 */
1370 1370 if (ret != 0) {
1371 1371 if (ret == 256) {
1372 1372 return (INSTALLADM_FAILURE);
1373 1373 }
1374 1374 (void) fprintf(stderr, MSG_SUBCOMMAND_FAILED, argv[0]);
1375 1375 return (INSTALLADM_FAILURE);
1376 1376 }
1377 1377 return (INSTALLADM_SUCCESS);
1378 1378 }
1379 1379
1380 1380 /*
1381 1381 * do_remove:
1382 1382 * Remove manifests from an A/I service
1383 1383 * Parse the command line for service name and manifest name (and if
1384 1384 * provided, internal instance name); then, get the service directory
1385 1385 * path from the provided service name; then pass the manifest name
1386 1386 * (instance name if provided) and service directory path to
1387 1387 * delete-manifest(1)
1388 1388 */
1389 1389 static int
1390 1390 do_remove(int argc, char *argv[], scfutilhandle_t *handle, const char *use)
1391 1391 {
1392 1392 int option;
1393 1393 char *port = NULL;
1394 1394 char *manifest = NULL;
1395 1395 char *serv_instance = NULL;
1396 1396 char *svcname = NULL;
1397 1397 char cmd[MAXPATHLEN];
1398 1398 int ret;
1399 1399 service_data_t data;
1400 1400
1401 1401 /*
1402 1402 * Check for valid number of arguments
1403 1403 */
1404 1404 if (argc != 5 && argc != 7) {
1405 1405 (void) fprintf(stderr, "%s\n", gettext(use));
1406 1406 return (INSTALLADM_FAILURE);
1407 1407 }
1408 1408
1409 1409 /*
1410 1410 * The -i option is an internal option
1411 1411 */
1412 1412 while ((option = getopt(argc, argv, ":n:m:i")) != -1) {
1413 1413 switch (option) {
1414 1414 case 'n':
1415 1415 svcname = optarg;
1416 1416 break;
1417 1417 case 'm':
1418 1418 manifest = optarg;
1419 1419 break;
1420 1420 case 'i':
1421 1421 serv_instance = optarg;
1422 1422 break;
1423 1423 default:
1424 1424 do_opterr(optopt, option, use);
1425 1425 return (INSTALLADM_FAILURE);
1426 1426 }
1427 1427 }
1428 1428
1429 1429 /*
1430 1430 * Make sure required options are there
1431 1431 */
1432 1432 if ((svcname == NULL) || (manifest == NULL)) {
1433 1433 (void) fprintf(stderr, MSG_MISSING_OPTIONS, argv[0]);
1434 1434 (void) fprintf(stderr, "%s\n", gettext(use));
1435 1435 return (INSTALLADM_FAILURE);
1436 1436 }
1437 1437
1438 1438 if (!validate_service_name(svcname)) {
1439 1439 (void) fprintf(stderr, MSG_BAD_SERVICE_NAME);
1440 1440 return (INSTALLADM_FAILURE);
1441 1441 }
1442 1442
1443 1443 /*
1444 1444 * Gather the directory location of the service
1445 1445 */
1446 1446 if (get_service_data(handle, svcname, &data) != B_TRUE) {
1447 1447 (void) fprintf(stderr, MSG_SERVICE_PROP_FAIL);
1448 1448 return (INSTALLADM_FAILURE);
1449 1449 }
1450 1450
1451 1451 /*
1452 1452 * txt_record should be of the form "aiwebserver=<host_ip>:<port>"
1453 1453 * and the directory location will be AI_SERVICE_DIR_PATH/<port>
1454 1454 */
1455 1455 port = strrchr(data.txt_record, ':');
1456 1456
1457 1457 if (port == NULL) {
1458 1458 (void) fprintf(stderr, MSG_SERVICE_PORT_MISSING,
1459 1459 svcname, data.txt_record);
1460 1460 return (INSTALLADM_FAILURE);
1461 1461 }
1462 1462 /*
1463 1463 * Exclude colon from string (so advance one character)
1464 1464 */
1465 1465 port++;
1466 1466
1467 1467 /*
1468 1468 * See if we're removing a single instance or a whole manifest
1469 1469 */
1470 1470 if (serv_instance == NULL) {
1471 1471 (void) snprintf(cmd, sizeof (cmd), "%s %s %s%s",
1472 1472 MANIFEST_REMOVE_SCRIPT,
1473 1473 manifest, AI_SERVICE_DIR_PATH, port);
1474 1474 } else {
1475 1475 (void) snprintf(cmd, sizeof (cmd), "%s %s %s %s %s%s",
1476 1476 MANIFEST_REMOVE_SCRIPT,
1477 1477 manifest, "-i", serv_instance,
1478 1478 AI_SERVICE_DIR_PATH, port);
1479 1479 }
1480 1480 ret = installadm_system(cmd);
1481 1481
1482 1482 /*
1483 1483 * Ensure we return an error if ret != 0.
1484 1484 * If ret == 1 then the Python handled the error, do not print a
1485 1485 * new error.
1486 1486 */
1487 1487 if (ret != 0) {
1488 1488 if (ret == 256) {
1489 1489 return (INSTALLADM_FAILURE);
1490 1490 }
1491 1491 (void) fprintf(stderr, MSG_SUBCOMMAND_FAILED, argv[0]);
1492 1492 return (INSTALLADM_FAILURE);
1493 1493 }
1494 1494
1495 1495 return (INSTALLADM_SUCCESS);
1496 1496 }
1497 1497
1498 1498 static int
1499 1499 do_help(int argc, char *argv[], scfutilhandle_t *handle, const char *use)
1500 1500 {
1501 1501 int i;
1502 1502 int numcmds;
1503 1503 cmd_t *cmdp;
1504 1504
1505 1505 if (argc == 1) {
1506 1506 usage();
1507 1507 return (INSTALLADM_FAILURE);
1508 1508 }
1509 1509
1510 1510 numcmds = sizeof (cmds) / sizeof (cmds[0]);
1511 1511 for (i = 0; i < numcmds; i++) {
1512 1512 cmdp = &cmds[i];
1513 1513 if (strcmp(argv[1], cmdp->c_name) == 0) {
1514 1514 if (cmdp->c_usage != NULL) {
1515 1515 (void) fprintf(stdout, "%s\n",
1516 1516 gettext(cmdp->c_usage));
1517 1517 } else {
1518 1518 (void) fprintf(stdout,
1519 1519 MSG_OPTION_NOHELP, progname,
1520 1520 argv[0], cmdp->c_name);
1521 1521 }
1522 1522 return (INSTALLADM_SUCCESS);
1523 1523 }
1524 1524 }
1525 1525
1526 1526 (void) fprintf(stderr, MSG_UNKNOWN_HELPSUBCOMMAND,
1527 1527 progname, argv[0], argv[1]);
1528 1528 usage();
1529 1529 return (INSTALLADM_FAILURE);
1530 1530 }
1531 1531
1532 1532
1533 1533 static void
1534 1534 do_opterr(int opt, int opterr, const char *usage)
1535 1535 {
1536 1536 switch (opterr) {
1537 1537 case ':':
1538 1538 (void) fprintf(stderr,
1539 1539 MSG_OPTION_VALUE_MISSING, opt, gettext(usage));
1540 1540 break;
1541 1541 case '?':
1542 1542 default:
1543 1543 (void) fprintf(stderr,
1544 1544 MSG_OPTION_UNRECOGNIZED, opt, gettext(usage));
1545 1545 break;
1546 1546 }
1547 1547 }
|
↓ open down ↓ |
306 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX