Example #1
0
def do_alias_service(options):
    ''' Create an alias of a service

    '''
    # Ensure that the base service is a service
    if not config.is_service(options.aliasof):
        raise SystemExit(_("\nService does not exist: %s\n") % options.aliasof)

    basesvc = AIService(options.aliasof)

    # Ensure that the base service is not an alias
    if basesvc.is_alias():
        raise SystemExit(_("\nError: Cannot create alias of another alias.\n"))

    image = basesvc.image
    if options.svcname in DEFAULT_ARCH:
        if ((image.arch == 'sparc' and 'sparc' not in options.svcname)
                or (image.arch == 'i386' and 'i386' not in options.svcname)):
            raise SystemExit(
                cw(
                    _("\nError: %s can not be an alias of a "
                      "service with a different architecture.\n") %
                    options.svcname))

    logging.debug("Creating alias of service %s", options.aliasof)

    print _("\nCreating %(arch)s alias: %(name)s\n") % \
            {'arch': image.arch,
             'name': options.svcname}

    logging.debug("Creating AIService aliasname %s base svc=%s, bootargs=%s",
                  options.svcname, options.aliasof, options.bootargs)
    try:
        service = AIService.create(options.svcname,
                                   image,
                                   alias=options.aliasof,
                                   bootargs=options.bootargs)
    except AIServiceError as err:
        raise SystemExit(err)

    # if recreating default-sparc alias, recreate symlinks
    if service.is_default_arch_service() and image.arch == 'sparc':
        logging.debug("Recreating default-sparc symlinks")
        service.do_default_sparc_symlinks(options.svcname)

    # Register & enable service
    # (Also enables system/install/server, as needed)
    try:
        service.enable()
    except (config.ServiceCfgError, MountError) as err:
        raise SystemExit(err)
    except aismf.ServicesError as err:
        # don't error out if the service is successfully created
        # but the services fail to start - just print out the error
        # and proceed
        print err
def do_alias_service(options):
    ''' Create an alias of a service

    '''
    # Ensure that the base service is a service
    if not config.is_service(options.aliasof):
        raise SystemExit(_("\nService does not exist: %s\n") % options.aliasof)

    basesvc = AIService(options.aliasof)

    # Ensure that the base service is not an alias
    if basesvc.is_alias():
        raise SystemExit(_("\nError: Cannot create alias of another alias.\n"))

    image = basesvc.image
    if options.svcname in DEFAULT_ARCH:
        if ((image.arch == 'sparc' and 'sparc' not in options.svcname) or
            (image.arch == 'i386' and 'i386' not in options.svcname)):
            raise SystemExit(cw(_("\nError: %s can not be an alias of a "
                                  "service with a different architecture.\n") %
                                  options.svcname))
 
    logging.debug("Creating alias of service %s", options.aliasof)

    print _("\nCreating %(arch)s alias: %(name)s\n") % \
            {'arch': image.arch,
             'name': options.svcname}

    logging.debug("Creating AIService aliasname %s base svc=%s, bootargs=%s",
                  options.svcname, options.aliasof, options.bootargs)
    try:
        service = AIService.create(options.svcname, image,
                                   alias=options.aliasof,
                                   bootargs=options.bootargs)
    except AIServiceError as err:
        raise SystemExit(err)

    # if recreating default-sparc alias, recreate symlinks
    if service.is_default_arch_service() and image.arch == 'sparc':
        logging.debug("Recreating default-sparc symlinks")
        service.do_default_sparc_symlinks(options.svcname)

    # Register & enable service
    # (Also enables system/install/server, as needed)
    try:
        service.enable()
    except (config.ServiceCfgError, MountError) as err:
        raise SystemExit(err)
    except aismf.ServicesError as err:
        # don't error out if the service is successfully created
        # but the services fail to start - just print out the error
        # and proceed
        print err
Example #3
0
def do_create_baseservice(options):
    '''
    This method sets up the install service by:
        - creating the target image directory from an iso or pkg
        - creating the /var/ai service structure
        - enabling tftp service or configuring wanboot
        - configuring dhcp if desired
    
    '''
    tempdir = None
    print _("\nCreating service from: %s") % options.srcimage
    if is_iso(options.srcimage):
        have_iso = True
        # get default service name, if needed
        logging.debug("Creating ISO based service")
    else:
        have_iso = False
        logging.debug("Creating pkg(5) based service")

    # If imagepath specified by user, use that.
    # If imagepath not specified  by user:
    #    a) if svcname specified by user, set up image in
    #       <default image path>/<svcname>
    #    b) if svcname not specified by user, set up image in
    #       <tmp location> and move to <default image path>/<svcname>
    #       once svcname is determined.

    # If imagepath not specified, verify that default image path is
    # ok with user
    if not options.imagepath:
        if options.svcname:
            imagepath = os.path.join(BASE_IMAGE_DIR, options.svcname)
            prompt = (_("OK to use default image path: %s? [y/N]: " %
                        imagepath))
        else:
            prompt = (_("OK to use subdir of %s to store image? [y/N]: " %
                        BASE_IMAGE_DIR))
        try:
            if not options.noprompt:
                if not com.ask_yes_or_no(prompt):
                    raise SystemExit(
                        _('\nPlease re-enter command with '
                          'desired --imagepath\n'))
        except KeyboardInterrupt:
            raise SystemExit(1)

        # If we know the svcname, we know where to put the image.
        # Otherwise, put the image into a temp directory and move
        # it to correct location when we know it later
        if options.svcname:
            options.imagepath = os.path.join(BASE_IMAGE_DIR, options.svcname)
            try:
                check_imagepath(options.imagepath)
            except ValueError as error:
                raise SystemExit(error)
        else:
            try:
                os.makedirs(BASE_IMAGE_DIR)
            except OSError as err:
                if err.errno != errno.EEXIST:
                    raise
                if not os.path.isdir(BASE_IMAGE_DIR):
                    raise SystemExit(
                        cw(
                            _('\nThe default image base '
                              'directory, %(dir)s, is not a directory. Check the '
                              'SMF setting for property %(prop)s in servce '
                              '%(svc)s.') % {
                                  'dir': BASE_IMAGE_DIR,
                                  'prop': com.BASEDIR_PROP,
                                  'svc': com.SRVINST
                              }))
            tempdir = tempfile.mkdtemp(dir=BASE_IMAGE_DIR)
            options.imagepath = tempdir
        logging.debug('Using default image path: %s', options.imagepath)

    # create the image area
    if have_iso:
        try:
            image = InstalladmIsoImage.unpack(options.srcimage,
                                              options.imagepath)
        except CalledProcessError as err:
            raise SystemExit(err.popen.stderr)
        except ImageError as err:
            print >> sys.stderr, str(err)
            shutil.rmtree(options.imagepath, ignore_errors=True)
            raise SystemExit(
                cw(
                    _('Please re-enter command and specify '
                      'a valid Automated Installer ISO file')))
    else:
        try:
            image = InstalladmPkgImage.image_create(
                options.srcimage,
                options.imagepath,
                arch=options.arch,
                publisher=options.publisher)
        except (ImageError, pkg.client.api_errors.ApiException) as err:
            print >> sys.stderr, cw(_("The specified data source, %s, "
                "for the service is not a path to an existing ISO image.") % \
                options.srcimage)
            print >> sys.stderr, cw(_("Attempting to create the service from"
                " pkg(5) package, %s, failed for the following reasons:") % \
                options.srcimage)
            if isinstance(err, pkg.client.api_errors.VersionException):
                print >> sys.stderr, cw(
                    _("The IPS API version specified, " +
                      str(err.received_version) +
                      ", is incompatible with the expected version, " +
                      str(err.expected_version) + "."))
            elif isinstance(err,
                            pkg.client.api_errors.CatalogRefreshException):
                for pub, error in err.failed:
                    print >> sys.stderr, "   "
                    print >> sys.stderr, str(error)
                if err.errmessage:
                    print >> sys.stderr, err.errmessage
            shutil.rmtree(options.imagepath, ignore_errors=True)
            raise SystemExit(err)

    # get default service name, if needed
    if not options.svcname:
        if tempdir and options.imagepath == tempdir:
            specified_path = None
        else:
            specified_path = options.imagepath
        options.svcname = get_default_service_name(specified_path,
                                                   image=image,
                                                   iso=have_iso)

    print _("\nCreating %(arch)s service: %(name)s\n") % \
            {'arch': image.arch,
             'name': options.svcname}

    # If image was created in temporary location, move to correct
    # location now that we know the svcname.
    if tempdir is not None:
        new_imagepath = os.path.join(BASE_IMAGE_DIR, options.svcname)
        try:
            check_imagepath(new_imagepath)
        except ValueError as error:
            # leave image in temp location so that service can be created
            logging.debug('unable to move image to %s: %s', new_imagepath,
                          error)
        else:
            options.imagepath = image.move(new_imagepath)
            logging.debug('image moved to %s', options.imagepath)

    set_permissions(options.imagepath)
    print _("Image path: %s\n") % options.imagepath
    try:
        if options.dhcp_ip_start:
            service = AIService.create(options.svcname,
                                       image,
                                       options.dhcp_ip_start,
                                       options.dhcp_ip_count,
                                       options.dhcp_bootserver,
                                       bootargs=options.bootargs)
        else:
            service = AIService.create(options.svcname,
                                       image,
                                       bootargs=options.bootargs)
    except AIServiceError as err:
        raise SystemExit(err)

    # Register & enable service
    # (Also enables system/install/server, as needed)
    got_services_error = False
    try:
        service.enable()
    except (config.ServiceCfgError, MountError) as err:
        raise SystemExit(err)
    except aismf.ServicesError as svc_err:
        # Don't print the error now.  It will either get printed out
        # upon exit or when services are enabled after creating the
        # alias
        got_services_error = True

    # create default-<arch> alias if this is the first aliasable
    # service of this architecture
    if should_be_default_for_arch(service):
        defaultarch = 'default-' + image.arch
        print(_("\nCreating %s alias.\n") % defaultarch)
        try:
            defaultarchsvc = AIService.create(defaultarch,
                                              image,
                                              bootargs=options.bootargs,
                                              alias=options.svcname)
        except AIServiceError as err:
            raise SystemExit(err)
        except UnsupportedAliasError as err:
            if got_services_error:
                # Print the services error string before printing the
                # unsupported alias error
                print svc_err, '\n'

            # Print the error, but have installadm exit successfully.
            # Since the user did not explicitly request this alias,
            # it's not a problem if an alias can't be made for this service
            print err
            return 0

        # For sparc, create symlinks for default sparc service
        if image.arch == 'sparc':
            logging.debug("Creating default-sparc symlinks")
            defaultarchsvc.do_default_sparc_symlinks(defaultarch)

        # Register & enable default-<arch> service
        try:
            defaultarchsvc.enable()
        except (config.ServiceCfgError, MountError) as err:
            raise SystemExit(err)
        except aismf.ServicesError as err:
            print err
    elif got_services_error:
        # Print the services start error generated when creating the service
        print svc_err
def do_create_baseservice(options):
    '''
    This method sets up the install service by:
        - creating the target image directory from an iso or pkg
        - creating the /var/ai service structure
        - enabling tftp service or configuring wanboot
        - configuring dhcp if desired
    
    '''
    tempdir = None
    print _("\nCreating service from: %s") % options.srcimage
    if is_iso(options.srcimage):
        have_iso = True
        # get default service name, if needed
        logging.debug("Creating ISO based service")
    else:
        have_iso = False
        logging.debug("Creating pkg(5) based service")

    # If imagepath specified by user, use that.
    # If imagepath not specified  by user:
    #    a) if svcname specified by user, set up image in
    #       <default image path>/<svcname>
    #    b) if svcname not specified by user, set up image in
    #       <tmp location> and move to <default image path>/<svcname>
    #       once svcname is determined.

    # If imagepath not specified, verify that default image path is
    # ok with user
    if not options.imagepath:
        if options.svcname:
            imagepath = os.path.join(BASE_IMAGE_DIR, options.svcname)
            prompt = (_("OK to use default image path: %s? [y/N]: " %
                      imagepath))
        else:
            prompt = (_("OK to use subdir of %s to store image? [y/N]: " %
                      BASE_IMAGE_DIR))
        try:
            if not options.noprompt:
                if not com.ask_yes_or_no(prompt):
                    raise SystemExit(_('\nPlease re-enter command with '
                                       'desired --imagepath\n'))
        except KeyboardInterrupt:
            raise SystemExit(1)

        # If we know the svcname, we know where to put the image.
        # Otherwise, put the image into a temp directory and move
        # it to correct location when we know it later
        if options.svcname:
            options.imagepath = os.path.join(BASE_IMAGE_DIR, options.svcname)
            try:
                check_imagepath(options.imagepath)
            except ValueError as error:
                raise SystemExit(error)
        else:
            try:
                os.makedirs(BASE_IMAGE_DIR)
            except OSError as err:
                if err.errno != errno.EEXIST:
                    raise
                if not os.path.isdir(BASE_IMAGE_DIR):
                    raise SystemExit(cw(_('\nThe default image base '
                        'directory, %(dir)s, is not a directory. Check the '
                        'SMF setting for property %(prop)s in servce '
                        '%(svc)s.') %
                        {'dir': BASE_IMAGE_DIR, 'prop': com.BASEDIR_PROP,
                         'svc': com.SRVINST}))
            tempdir = tempfile.mkdtemp(dir=BASE_IMAGE_DIR)
            options.imagepath = tempdir
        logging.debug('Using default image path: %s', options.imagepath)

    # create the image area
    if have_iso:
        try:
            image = InstalladmIsoImage.unpack(options.srcimage,
                                              options.imagepath)
        except CalledProcessError as err:
            raise SystemExit(err.popen.stderr)
        except ImageError as err:
            print >> sys.stderr, str(err)
            shutil.rmtree(options.imagepath, ignore_errors=True)
            raise SystemExit(cw(_('Please re-enter command and specify '
                             'a valid Automated Installer ISO file')))
    else:
        try:
            image = InstalladmPkgImage.image_create(options.srcimage,
                options.imagepath,
                arch=options.arch,
                publisher=options.publisher)
        except (ImageError,
                pkg.client.api_errors.ApiException) as err:
            print >> sys.stderr, cw(_("The specified data source, %s, "
                "for the service is not a path to an existing ISO image.") % \
                options.srcimage)
            print >> sys.stderr, cw(_("Attempting to create the service from"
                " pkg(5) package, %s, failed for the following reasons:") % \
                options.srcimage)
            if isinstance(err, pkg.client.api_errors.VersionException):
                print >> sys.stderr, cw(_("The IPS API version specified, "
                    + str(err.received_version) +
                    ", is incompatible with the expected version, "
                    + str(err.expected_version) + "."))
            elif isinstance(err,
                            pkg.client.api_errors.CatalogRefreshException):
                for pub, error in err.failed:
                    print >> sys.stderr, "   "
                    print >> sys.stderr, str(error)
                if err.errmessage:
                    print >> sys.stderr, err.errmessage
            shutil.rmtree(options.imagepath, ignore_errors=True)
            raise SystemExit(err)

    # get default service name, if needed
    if not options.svcname:
        if tempdir and options.imagepath == tempdir:
            specified_path = None
        else:
            specified_path = options.imagepath
        options.svcname = get_default_service_name(specified_path,
                                                   image=image, iso=have_iso)

    print _("\nCreating %(arch)s service: %(name)s\n") % \
            {'arch': image.arch,
             'name': options.svcname}

    # If image was created in temporary location, move to correct
    # location now that we know the svcname.
    if tempdir is not None:
        new_imagepath = os.path.join(BASE_IMAGE_DIR, options.svcname)
        try:
            check_imagepath(new_imagepath)
        except ValueError as error:
            # leave image in temp location so that service can be created
            logging.debug('unable to move image to %s: %s',
                          new_imagepath, error)
        else:
            options.imagepath = image.move(new_imagepath)
            logging.debug('image moved to %s', options.imagepath)

    set_permissions(options.imagepath)
    print _("Image path: %s\n") % options.imagepath
    try:
        if options.dhcp_ip_start:
            service = AIService.create(options.svcname, image,
                                       options.dhcp_ip_start,
                                       options.dhcp_ip_count,
                                       options.dhcp_bootserver,
                                       bootargs=options.bootargs)
        else:
            service = AIService.create(options.svcname, image,
                                       bootargs=options.bootargs)
    except AIServiceError as err:
        raise SystemExit(err)

    # Register & enable service
    # (Also enables system/install/server, as needed)
    got_services_error = False
    try:
        service.enable()
    except (config.ServiceCfgError, MountError) as err:
        raise SystemExit(err)
    except aismf.ServicesError as svc_err:
        # Don't print the error now.  It will either get printed out
        # upon exit or when services are enabled after creating the
        # alias
        got_services_error = True

    # create default-<arch> alias if this is the first aliasable
    # service of this architecture
    if should_be_default_for_arch(service):
        defaultarch = 'default-' + image.arch
        print (_("\nCreating %s alias.\n") % defaultarch)
        try:
            defaultarchsvc = AIService.create(defaultarch, image,
                                              bootargs=options.bootargs,
                                              alias=options.svcname)
        except AIServiceError as err:
            raise SystemExit(err)
        except UnsupportedAliasError as err:
            if got_services_error:
                # Print the services error string before printing the
                # unsupported alias error
                print svc_err, '\n'

            # Print the error, but have installadm exit successfully.
            # Since the user did not explicitly request this alias,
            # it's not a problem if an alias can't be made for this service
            print err
            return 0

        # For sparc, create symlinks for default sparc service
        if image.arch == 'sparc':
            logging.debug("Creating default-sparc symlinks")
            defaultarchsvc.do_default_sparc_symlinks(defaultarch)

        # Register & enable default-<arch> service
        try:
            defaultarchsvc.enable()
        except (config.ServiceCfgError, MountError) as err:
            raise SystemExit(err)
        except aismf.ServicesError as err:
            print err
    elif got_services_error:
        # Print the services start error generated when creating the service
        print svc_err