Exemple #1
0
def update_auth_details_from_rbac_registration(auth_details, service_name):
    print("Please authenticate with the RBAC service to register this MAAS")
    client = RBACUserClient(auth_details.rbac_url)
    services = {
        service["name"]: service
        for service in client.get_registerable_services()
    }
    if service_name is None:
        if not services:
            raise CommandError(
                "No registerable MAAS service on the specified RBAC server")
        service = _pick_service(services)
    else:
        service = services.get(service_name)
        if service is None:
            create_service = prompt_for_choices(
                "A service with the specified name was not found, "
                "do you want to create one? (yes/no) [default=no]? ",
                ["yes", "no"],
                default="no",
            )
            if create_service == "no":
                raise CommandError("Registration with RBAC service canceled")
            try:
                service = client.create_service(service_name)
            except APIError as error:
                if error.status_code == 409:
                    raise CommandError(
                        "User not allowed to register this service")
                raise CommandError(str(error))
    _register_service(client, service, auth_details)
    print('Service "{}" registered'.format(service["name"]))
Exemple #2
0
def update_auth_details_from_rbac_registration(auth_details, service_name):
    print('Please authenticate with the RBAC service to register this MAAS')
    client = RBACUserClient(auth_details.rbac_url)
    services = {
        service['name']: service
        for service in client.get_registerable_services()
    }
    if service_name is None:
        if not services:
            raise CommandError(
                'No registerable MAAS service on the specified RBAC server')
        service = _pick_service(services)
    else:
        service = services.get(service_name)
        if service is None:
            create_service = prompt_for_choices(
                'A service with the specified name was not found, '
                'do you want to create one? (yes/no) [default=no]? ',
                ['yes', 'no'],
                default='no')
            if create_service == 'no':
                raise CommandError('Registration with RBAC service canceled')
            try:
                service = client.create_service(service_name)
            except APIError as error:
                if error.status_code == 409:
                    raise CommandError(
                        'User not allowed to register this service')
                raise CommandError(str(error))
    _register_service(client, service, auth_details)
    print('Service "{}" registered'.format(service['name']))
Exemple #3
0
    def handle(self, options):
        if os.getuid() != 0:
            raise SystemExit("The 'init' command must be run by root.")

        mode = options.run_mode
        current_mode = get_current_mode()
        if current_mode != "none":
            if not options.force:
                init_text = "initialize again"
                if mode == "none":
                    init_text = "de-initialize"
                else:
                    print_msg("Controller has already been initialized.")
                initialize = prompt_for_choices(
                    "Are you sure you want to %s "
                    "(yes/no) [default=no]? " % init_text,
                    ["yes", "no"],
                    default="no",
                )
                if initialize == "no":
                    sys.exit(0)

        rpc_secret = None
        if mode in ("region", "region+rack"):
            try:
                database_settings = get_database_settings(options)
            except DatabaseSettingsError as error:
                raise CommandError(str(error))
        else:
            database_settings = {}
        maas_url = options.maas_url
        if mode != "none" and not maas_url:
            maas_url = required_prompt(
                "MAAS URL",
                default=get_default_url(),
                help_text=ARGUMENTS["maas-url"]["help"],
            )
        if mode == "rack":
            rpc_secret = options.secret
            if not rpc_secret:
                rpc_secret = required_prompt(
                    "Secret", help_text=ARGUMENTS["secret"]["help"])

        if current_mode != "none":

            def stop_services():
                render_supervisord("none")
                sighup_supervisord()

            perform_work("Stopping services", stop_services)

        # Configure the settings.
        settings = {"maas_url": maas_url}
        settings.update(database_settings)

        MAASConfiguration().update(settings)
        set_rpc_secret(rpc_secret)

        # Finalize the Initialization.
        self._finalize_init(mode, options)
Exemple #4
0
    def handle(self, options):
        if os.getuid() != 0:
            raise SystemExit("The 'init' command must be run by root.")

        mode = options.mode
        current_mode = get_current_mode()
        if current_mode != 'none':
            if not options.force:
                init_text = 'initialize again'
                if mode == 'none':
                    init_text = 'de-initialize'
                else:
                    print_msg('Controller has already been initialized.')
                initialize = prompt_for_choices('Are you sure you want to %s '
                                                '(yes/no) [default=no]? ' %
                                                init_text, ['yes', 'no'],
                                                default='no')
                if initialize == 'no':
                    sys.exit(0)

        if not mode:
            mode = prompt_for_choices(
                "Mode ({choices}) [default={default}]? ".format(
                    choices='/'.join(ARGUMENTS['mode']['choices']),
                    default=DEFAULT_OPERATION_MODE),
                ARGUMENTS['mode']['choices'],
                default=DEFAULT_OPERATION_MODE,
                help_text=OPERATION_MODES)
        if current_mode == 'all' and mode != 'all' and not options.force:
            print_msg(
                'This will disconnect your MAAS from the running database.')
            disconnect = prompt_for_choices(
                'Are you sure you want to disconnect the database '
                '(yes/no) [default=no]? ', ['yes', 'no'],
                default='no')
            if disconnect == 'no':
                return 0
        elif current_mode == 'all' and mode == 'all' and not options.force:
            print_msg('This will re-initialize your entire database and all '
                      'current data will be lost.')
            reinit_db = prompt_for_choices(
                'Are you sure you want to re-initialize the database '
                '(yes/no) [default=no]? ', ['yes', 'no'],
                default='no')
            if reinit_db == 'no':
                return 0

        maas_url = options.maas_url
        if mode != 'none' and not maas_url:
            maas_url = prompt_for_maas_url()
        database_host = database_name = None
        database_user = database_pass = None
        rpc_secret = None
        if mode == 'all':
            database_host = os.path.join(get_base_db_dir(), 'sockets')
            database_name = 'maasdb'
            database_user = '******'
            database_pass = ''.join(
                random.choice(string.ascii_uppercase + string.digits)
                for _ in range(10))
        if mode in ['region', 'region+rack']:
            database_host = options.database_host
            if not database_host:
                database_host = required_prompt(
                    "Database host: ",
                    help_text=ARGUMENTS['database-host']['help'])
            database_name = options.database_name
            if not database_name:
                database_name = required_prompt(
                    "Database name: ",
                    help_text=ARGUMENTS['database-name']['help'])
            database_user = options.database_user
            if not database_user:
                database_user = required_prompt(
                    "Database user: "******"Database password: "******"Secret: ", help_text=ARGUMENTS['secret']['help'])

        # Stop all services if in another mode.
        if current_mode != 'none':

            def stop_services():
                render_supervisord('none')
                sighup_supervisord()

            perform_work('Stopping services', stop_services)

        # Configure the settings.
        settings = {
            'maas_url': maas_url,
            'database_host': database_host,
            'database_name': database_name,
            'database_user': database_user,
            'database_pass': database_pass
        }

        # Add the port to the configuration if exists. By default
        # MAAS handles picking the port automatically in the backend
        # if none provided.
        if options.database_port:
            settings['database_port'] = options.database_port

        MAASConfiguration().update(settings)
        set_rpc_secret(rpc_secret)

        # Finalize the Initialization.
        self._finalize_init(mode, options)
Exemple #5
0
    def handle(self, options):
        if os.getuid() != 0:
            raise SystemExit("The 'init' command must be run by root.")

        mode = options.mode
        current_mode = get_current_mode()
        if current_mode != "none":
            if not options.force:
                init_text = "initialize again"
                if mode == "none":
                    init_text = "de-initialize"
                else:
                    print_msg("Controller has already been initialized.")
                initialize = prompt_for_choices(
                    "Are you sure you want to %s "
                    "(yes/no) [default=no]? " % init_text,
                    ["yes", "no"],
                    default="no",
                )
                if initialize == "no":
                    sys.exit(0)

        if not mode:
            mode = prompt_for_choices(
                "Mode ({choices}) [default={default}]? ".format(
                    choices="/".join(ARGUMENTS["mode"]["choices"]),
                    default=DEFAULT_OPERATION_MODE,
                ),
                ARGUMENTS["mode"]["choices"],
                default=DEFAULT_OPERATION_MODE,
                help_text=OPERATION_MODES,
            )
        if current_mode == "all" and mode != "all" and not options.force:
            print_msg(
                "This will disconnect your MAAS from the running database.")
            disconnect = prompt_for_choices(
                "Are you sure you want to disconnect the database "
                "(yes/no) [default=no]? ",
                ["yes", "no"],
                default="no",
            )
            if disconnect == "no":
                return 0
        elif current_mode == "all" and mode == "all" and not options.force:
            print_msg("This will re-initialize your entire database and all "
                      "current data will be lost.")
            reinit_db = prompt_for_choices(
                "Are you sure you want to re-initialize the database "
                "(yes/no) [default=no]? ",
                ["yes", "no"],
                default="no",
            )
            if reinit_db == "no":
                return 0

        maas_url = options.maas_url
        if mode != "none" and not maas_url:
            maas_url = prompt_for_maas_url()
        database_host = database_name = None
        database_user = database_pass = None
        rpc_secret = None
        if mode == "all":
            database_host = os.path.join(get_base_db_dir(), "sockets")
            database_name = "maasdb"
            database_user = "******"
            database_pass = "".join(
                random.choice(string.ascii_uppercase + string.digits)
                for _ in range(10))
        if mode in ["region", "region+rack"]:
            database_host = options.database_host
            if not database_host:
                database_host = required_prompt(
                    "Database host: ",
                    help_text=ARGUMENTS["database-host"]["help"],
                )
            database_name = options.database_name
            if not database_name:
                database_name = required_prompt(
                    "Database name: ",
                    help_text=ARGUMENTS["database-name"]["help"],
                )
            database_user = options.database_user
            if not database_user:
                database_user = required_prompt(
                    "Database user: "******"database-user"]["help"],
                )
            database_pass = options.database_pass
            if not database_pass:
                database_pass = required_prompt(
                    "Database password: "******"database-pass"]["help"],
                )
        if mode == "rack":
            rpc_secret = options.secret
            if not rpc_secret:
                rpc_secret = required_prompt(
                    "Secret: ", help_text=ARGUMENTS["secret"]["help"])

        # Stop all services if in another mode.
        if current_mode != "none":

            def stop_services():
                render_supervisord("none")
                sighup_supervisord()

            perform_work("Stopping services", stop_services)

        # Configure the settings.
        settings = {
            "maas_url": maas_url,
            "database_host": database_host,
            "database_name": database_name,
            "database_user": database_user,
            "database_pass": database_pass,
        }

        # Add the port to the configuration if exists. By default
        # MAAS handles picking the port automatically in the backend
        # if none provided.
        if options.database_port:
            settings["database_port"] = options.database_port

        MAASConfiguration().update(settings)
        set_rpc_secret(rpc_secret)

        # Finalize the Initialization.
        self._finalize_init(mode, options)
Exemple #6
0
    def handle(self, options):
        if os.getuid() != 0:
            raise SystemExit("The 'init' command must be run by root.")

        mode = options.mode
        current_mode = get_current_mode()
        if current_mode != "none":
            if not options.force:
                init_text = "initialize again"
                if mode == "none":
                    init_text = "de-initialize"
                else:
                    print_msg("Controller has already been initialized.")
                initialize = prompt_for_choices(
                    "Are you sure you want to %s "
                    "(yes/no) [default=no]? " % init_text,
                    ["yes", "no"],
                    default="no",
                )
                if initialize == "no":
                    sys.exit(0)

        if not mode:
            print_msg(
                dedent("""
            Select the mode the snap should operate in.

            When installing region or rack+region modes, MAAS needs a
            PostgreSQL database to connect to.

            If you want to set up PostgreSQL on this machine, and configure it
            for use with MAAS, you can run the following command:

            /snap/maas/current/helpers/maas-database-setup

            """))
            mode = prompt_for_choices(
                "Mode ({choices}) [default={default}]? ".format(
                    choices="/".join(ARGUMENTS["mode"]["choices"]),
                    default=DEFAULT_OPERATION_MODE,
                ),
                ARGUMENTS["mode"]["choices"],
                default=DEFAULT_OPERATION_MODE,
                help_text=OPERATION_MODES,
            )
        if current_mode == "all" and not options.force:
            print_msg(
                "This will disconnect your MAAS from the running database.")
            disconnect = prompt_for_choices(
                "Are you sure you want to disconnect the database "
                "(yes/no) [default=no]? ",
                ["yes", "no"],
                default="no",
            )
            if disconnect == "no":
                return 0

        maas_url = options.maas_url
        if mode != "none" and not maas_url:
            maas_url = prompt_for_maas_url()
        database_host = database_name = None
        database_user = database_pass = None
        rpc_secret = None
        if mode in ["region", "region+rack"]:
            database_host = options.database_host
            if not database_host:
                database_host = required_prompt(
                    "Database host: ",
                    help_text=ARGUMENTS["database-host"]["help"],
                )
            database_name = options.database_name
            if not database_name:
                database_name = required_prompt(
                    "Database name: ",
                    help_text=ARGUMENTS["database-name"]["help"],
                )
            database_user = options.database_user
            if not database_user:
                database_user = required_prompt(
                    "Database user: "******"database-user"]["help"],
                )
            database_pass = options.database_pass
            if not database_pass:
                database_pass = required_prompt(
                    "Database password: "******"database-pass"]["help"],
                )
        if mode == "rack":
            rpc_secret = options.secret
            if not rpc_secret:
                rpc_secret = required_prompt(
                    "Secret: ", help_text=ARGUMENTS["secret"]["help"])

        # Stop all services if in another mode.
        if current_mode != "none":

            def stop_services():
                render_supervisord("none")
                sighup_supervisord()

            perform_work("Stopping services", stop_services)

        # Configure the settings.
        settings = {
            "maas_url": maas_url,
            "database_host": database_host,
            "database_name": database_name,
            "database_user": database_user,
            "database_pass": database_pass,
        }

        # Add the port to the configuration if exists. By default
        # MAAS handles picking the port automatically in the backend
        # if none provided.
        if options.database_port:
            settings["database_port"] = options.database_port

        MAASConfiguration().update(settings)
        set_rpc_secret(rpc_secret)

        # Finalize the Initialization.
        self._finalize_init(mode, options)
Exemple #7
0
    def handle(self, options):
        if os.getuid() != 0:
            raise SystemExit("The 'init' command must be run by root.")

        mode = options.run_mode
        if options.deprecated_mode:
            mode = options.deprecated_mode
        current_mode = get_current_mode()
        if current_mode != "none":
            if not options.force:
                init_text = "initialize again"
                if mode == "none":
                    init_text = "de-initialize"
                else:
                    print_msg("Controller has already been initialized.")
                initialize = prompt_for_choices(
                    "Are you sure you want to %s "
                    "(yes/no) [default=no]? " % init_text,
                    ["yes", "no"],
                    default="no",
                )
                if initialize == "no":
                    sys.exit(0)

        if current_mode == "all" and mode != "all" and not options.force:
            print_msg(
                "This will disconnect your MAAS from the running database.")
            disconnect = prompt_for_choices(
                "Are you sure you want to disconnect the database "
                "(yes/no) [default=no]? ",
                ["yes", "no"],
                default="no",
            )
            if disconnect == "no":
                return 0
        elif current_mode == "all" and mode == "all" and not options.force:
            print_msg("This will re-initialize your entire database and all "
                      "current data will be lost.")
            reinit_db = prompt_for_choices(
                "Are you sure you want to re-initialize the database "
                "(yes/no) [default=no]? ",
                ["yes", "no"],
                default="no",
            )
            if reinit_db == "no":
                return 0

        rpc_secret = None
        if mode == "all":
            database_settings = {
                "database_host":
                os.path.join(get_base_db_dir(), "sockets"),
                "database_name":
                "maasdb",
                "database_user":
                "******",
                "database_pass":
                "".join(
                    random.choice(string.ascii_uppercase + string.digits)
                    for _ in range(10)),
            }
        elif mode in ["region", "region+rack"]:
            try:
                database_settings = get_database_settings(options)
            except DatabaseSettingsError as error:
                raise CommandError(str(error))
        else:
            database_settings = {}
        maas_url = options.maas_url
        if mode != "none" and not maas_url:
            maas_url = required_prompt(
                "MAAS URL",
                default=get_default_url(),
                help_text=ARGUMENTS["maas-url"]["help"],
            )
        if mode == "rack":
            rpc_secret = options.secret
            if not rpc_secret:
                rpc_secret = required_prompt(
                    "Secret", help_text=ARGUMENTS["secret"]["help"])

        # Stop all services if in another mode.
        if current_mode != "none":

            def stop_services():
                render_supervisord("none")
                sighup_supervisord()

            perform_work("Stopping services", stop_services)

        # Configure the settings.
        settings = {"maas_url": maas_url}
        settings.update(database_settings)

        MAASConfiguration().update(settings)
        set_rpc_secret(rpc_secret)

        # Finalize the Initialization.
        self._finalize_init(mode, options)