Exemple #1
0
        def cbCreateTargets(fbmirrors, mirrors, schedulers, push_pull = True):
            for i in range(len(targets)):
                if push_pull:
                    # FIXME: we only take the the first mirrors
                    mirror = mirrors[i]
                    fallback = fbmirrors[i]
                    uri = '%s://%s:%s%s' % (mirror['protocol'], 
                                            mirror['server'], 
                                            str(mirror['port']), 
                                            mirror['mountpoint']) \
                        + '||' + '%s://%s:%s%s' % (fallback['protocol'], 
                                                   fallback['server'],
                                                   str(fallback['port']), 
                                                   fallback['mountpoint'])
                else:
                    uri = '%s://%s' % ('file', root)

                targetsdata[i]['mirrors'] = uri
                # Keep not blacklisted target name for commands_on_host
                # creation.
                targets_name.append(targets[i][1])
                # Maybe could be done in prepareTarget
                targetsdata[i] = self.blacklistTargetHostname(targetsdata[i])

                targets_to_insert.append((targetsdata[i], 
                                          targets[i][1],    
                                          schedulers[i]))
            session = create_session()
            session.begin()
            for atarget, target_name, ascheduler in targets_to_insert :
                target = Target()
                target.target_macaddr = atarget["target_macaddr"]
                target.id_group = atarget["id_group"]
                target.target_uuid = atarget["target_uuid"]
                target.target_bcast = atarget["target_bcast"]
                target.target_name = atarget["target_name"]
                target.target_ipaddr = atarget["target_ipaddr"]
                target.mirrors = atarget["mirrors"]
                target.target_network = atarget["target_network"]

                session.add(target)
                session.flush()
                if hasattr(session, "refresh") :
                    session.refresh(target)

                order_in_proxy = None
                max_clients_per_proxy = 0
                try:
                    candidates = filter(lambda(x): x['uuid'] == atarget["target_uuid"], proxies)
                    if len(candidates) == 1:
                        max_clients_per_proxy = candidates[0]['max_clients']
                        order_in_proxy = candidates[0]['priority']
                except ValueError:
                    self.logger.warn("Failed to get values 'order_in_proxy' or 'max_clients'")
                coh_to_insert.append(self.createCommandsOnHost(cmd_id,
                                                               atarget, 
                                                               target.id, 
                                                               target_name, 
                                                               cmd.max_connection_attempt,
                                                               cmd.start_date,
                                                               cmd.end_date,
                                                               ascheduler, 
                                                               order_in_proxy, 
                                                               max_clients_per_proxy))
            session.execute(self.commands_on_host.insert(), coh_to_insert)
            session.commit()

            cohs = cmd.getCohIds(target_uuids=target_uuids)
            session = create_session()
            self._createPhases(session,
                               cohs,
                               cmd.do_imaging_menu,
                               cmd.do_wol,
                               cmd.files,
                               cmd.start_script,
                               cmd.clean_on_success,
                               cmd.do_inventory,
                               cmd.do_halt,
                               cmd.do_reboot,
                               is_quick_action = False)

            return cmd_id
Exemple #2
0
        def cbCreateTargets(fbmirrors, mirrors, schedulers, push_pull=True):

            for i in range(len(targets)):
                if push_pull:
                    # FIXME: we only take the the first mirrors
                    mirror = mirrors[i]
                    fallback = fbmirrors[i]
                    uri = '%s://%s:%s%s' % (mirror['protocol'],
                                            mirror['server'],
                                            str(mirror['port']),
                                            mirror['mountpoint']) \
                        + '||' + '%s://%s:%s%s' % (fallback['protocol'],
                                                   fallback['server'],
                                                   str(fallback['port']),
                                                   fallback['mountpoint'])
                else:
                    uri = '%s://%s' % ('file', root)

                targetsdata[i]['mirrors'] = uri
                # Keep not blacklisted target name for commands_on_host
                # creation.
                targets_name.append(targets[i][1])
                # Maybe could be done in prepareTarget
                targetsdata[i] = self.blacklistTargetHostname(targetsdata[i])

                targets_to_insert.append(
                    (targetsdata[i], targets[i][1], schedulers[i]))
            session = create_session()
            session.begin()
            cmd = self.createCommand(
                session, package_id, start_file, parameters, files,
                start_script, clean_on_success, start_date, end_date,
                connect_as, ctx.userid, title, do_halt, do_reboot, do_wol,
                next_connection_delay, max_connection_attempt, do_inventory,
                maxbw, deployment_intervals, fk_bundle, order_in_bundle,
                proxies, proxy_mode, state)
            session.flush()

            for atarget, target_name, ascheduler in targets_to_insert:
                target = Target()
                target.target_macaddr = atarget["target_macaddr"]
                target.id_group = atarget["id_group"]
                target.target_uuid = atarget["target_uuid"]
                target.target_bcast = atarget["target_bcast"]
                target.target_name = atarget["target_name"]
                target.target_ipaddr = atarget["target_ipaddr"]
                target.mirrors = atarget["mirrors"]
                target.target_network = atarget["target_network"]

                session.add(target)
                session.flush()
                if hasattr(session, "refresh"):
                    session.refresh(target)

                order_in_proxy = None
                max_clients_per_proxy = 0
                try:
                    candidates = filter(
                        lambda (x): x['uuid'] == atarget["target_uuid"],
                        proxies)
                    if len(candidates) == 1:
                        max_clients_per_proxy = candidates[0]['max_clients']
                        order_in_proxy = candidates[0]['priority']
                except ValueError:
                    self.logger.warn(
                        "Failed to get values 'order_in_proxy' or 'max_clients'"
                    )
                coh_to_insert.append(
                    self.createCommandsOnHost(cmd.getId(), atarget, target.id,
                                              target_name,
                                              max_connection_attempt,
                                              start_date, end_date, ascheduler,
                                              order_in_proxy,
                                              max_clients_per_proxy))
            session.execute(self.commands_on_host.insert(), coh_to_insert)
            session.commit()

            return cmd.getId()
Exemple #3
0
        def cbCreateTargets(fbmirrors, mirrors, schedulers, session, push_pull = True):
            # For each command, we prepare the targets list
            # There is one targets list per command because the root package
            # path may change according to the package
            for command in commands:
                targets_to_insert = []
                root = command['root']
                if root == None:
                    root = self.config.repopath
                for i in range(len(targets)):
                    if push_pull:
                        # FIXME: we only take the the first mirrors
                        mirror = mirrors[i]
                        fallback = fbmirrors[i]
                        uri = '%s://%s:%s%s' % (mirror['protocol'], 
                                                mirror['server'], 
                                                str(mirror['port']), 
                                                mirror['mountpoint']) + \
                              '||' + '%s://%s:%s%s' % (fallback['protocol'], 
                                                       fallback['server'], 
                                                       str(fallback['port']), 
                                                       fallback['mountpoint'])
                    else:
                        uri = '%s://%s' % ('file', root)

                    targetsdata[i]['mirrors'] = uri
                    # Keep not blacklisted target name for commands_on_host
                    # creation.
                    targets_name.append(targets[i][1])
                    # Maybe could be done in prepareTarget
                    targetsdata[i] = self.blacklistTargetHostname(targetsdata[i])
                    targets_to_insert.append((dict(targetsdata[i]),
                                              targets[i][1],
                                              schedulers[i]
                                             ))
                targets_to_insert_list.append(targets_to_insert)

            if session == None:
                session = create_session(transactional = True)
            session.begin()
            ret = []
            cohs = []
            for cmd, targets_to_insert in zip(commands, targets_to_insert_list):

                time_defaults = self.__getTimeDefaults(cmd["start_date"], cmd["end_date"])
                cmd["start_date"], cmd["end_date"], cmd["max_connection_attempt"] = time_defaults
                cmd["do_wol_with_imaging"] = "disable"

                cobj = self.createCommand(session, 
                                          cmd['package_id'], 
                                          cmd['start_file'], cmd['parameters'], 
                                          cmd['files'], cmd['start_script'], 
                                          cmd['clean_on_success'], 
                                          cmd['start_date'], cmd['end_date'], 
                                          cmd['connect_as'], ctx.userid, 
                                          cmd['title'],  
                                          cmd['next_connection_delay'], 
                                          cmd['max_connection_attempt'], 
                                          cmd['maxbw'], 
                                          cmd['deployment_intervals'], 
                                          cmd['fk_bundle'], cmd['order_in_bundle'], 
                                          cmd['proxies'], cmd['proxy_mode'],
                                          cmd['state'],
                                          len(targets_to_insert))
                session.flush()
                ret.append(cobj.getId())

                for atarget, target_name, ascheduler in targets_to_insert:
                    order_in_proxy = None
                    max_clients_per_proxy = 0

                    target = Target()
                    target.target_macaddr = atarget["target_macaddr"]
                    target.id_group = atarget["id_group"]
                    target.target_uuid = atarget["target_uuid"]
                    target.target_bcast = atarget["target_bcast"]
                    target.target_name = atarget["target_name"]
                    target.target_ipaddr = atarget["target_ipaddr"]
                    target.mirrors = atarget["mirrors"]
                    target.target_network = atarget["target_network"]

                    session.add(target)
                    session.flush()
                    if hasattr(session, "refresh"):
                        session.refresh(target)
                   
                    try:
                        candidates = filter(lambda(x): x['uuid'] == atarget["target_uuid"], cmd['proxies'])
                        if len(candidates) == 1:
                            max_clients_per_proxy = candidates[0]['max_clients']
                            order_in_proxy = candidates[0]['priority']
                    except ValueError:
                        self.logger.warn("Failed to get values 'order_in_proxy' or 'max_clients'")
                    coh_to_insert.append(self.createCommandsOnHost(cobj.getId(),
                                                                   atarget,
                                                                   target.id, 
                                                                   target_name,
                                                                   cmd['max_connection_attempt'],
                                                                   cmd['start_date'], 
                                                                   cmd['end_date'], 
                                                                   ascheduler, 
                                                                   order_in_proxy, 
                                                                   max_clients_per_proxy))
                    
            session.execute(self.commands_on_host.insert(), coh_to_insert)
            session.commit()

            __cmd_ids = [[v for (k, v) in coh.items() if k=="fk_commands"] for coh in [c for c in coh_to_insert]]
            cmd_ids = []
            [cmd_ids.append(i) for i in __cmd_ids if not cmd_ids.count(i)]


            session = create_session()
            cohs = []
            for id in cmd_ids:
                
                _cohs = session.query(Commands).get(id).getCohIds()
                cohs.extend(_cohs)
            session.close()

                #for coh_id in command_obj.getCohIds():
                #    cohs.append(coh_id)
            session = create_session()
            self._createPhases(session,
                               cohs, 
                               cmd['do_wol_with_imaging'], 
                               cmd['do_wol'],
                               cmd['files'],
                               cmd['start_script'],
                               cmd['clean_on_success'],
                               cmd['do_inventory'],
                               cmd['issue_halt_to'], 
                               cmd['do_reboot'])


            return ret
Exemple #4
0
        def cbCreateTargets(fbmirrors,
                            mirrors,
                            schedulers,
                            session,
                            push_pull=True):
            # For each command, we prepare the targets list
            # There is one targets list per command because the root package
            # path may change according to the package
            for command in commands:
                targets_to_insert = []
                root = command['root']
                if root == None:
                    root = self.config.repopath
                for i in range(len(targets)):
                    if push_pull:
                        # FIXME: we only take the the first mirrors
                        mirror = mirrors[i]
                        fallback = fbmirrors[i]
                        uri = '%s://%s:%s%s' % (mirror['protocol'],
                                                mirror['server'],
                                                str(mirror['port']),
                                                mirror['mountpoint']) + \
                              '||' + '%s://%s:%s%s' % (fallback['protocol'],
                                                       fallback['server'],
                                                       str(fallback['port']),
                                                       fallback['mountpoint'])
                    else:
                        uri = '%s://%s' % ('file', root)

                    targetsdata[i]['mirrors'] = uri
                    # Keep not blacklisted target name for commands_on_host
                    # creation.
                    targets_name.append(targets[i][1])
                    # Maybe could be done in prepareTarget
                    targetsdata[i] = self.blacklistTargetHostname(
                        targetsdata[i])
                    targets_to_insert.append(
                        (dict(targetsdata[i]), targets[i][1], schedulers[i]))
                targets_to_insert_list.append(targets_to_insert)

            if session == None:
                session = create_session(transactional=True)
            session.begin()
            ret = []
            for cmd, targets_to_insert in zip(commands,
                                              targets_to_insert_list):

                time_defaults = self.__getTimeDefaults(cmd["start_date"],
                                                       cmd["end_date"])
                cmd["start_date"], cmd["end_date"], cmd[
                    "max_connection_attempt"] = time_defaults

                cobj = self.createCommand(
                    session, cmd['package_id'], cmd['start_file'],
                    cmd['parameters'], cmd['files'], cmd['start_script'],
                    cmd['clean_on_success'], cmd['start_date'],
                    cmd['end_date'], cmd['connect_as'], ctx.userid,
                    cmd['title'], cmd['issue_halt_to'], cmd['do_reboot'],
                    cmd['do_wol'], cmd['next_connection_delay'],
                    cmd['max_connection_attempt'], cmd['do_inventory'],
                    cmd['maxbw'], cmd['deployment_intervals'],
                    cmd['fk_bundle'], cmd['order_in_bundle'], cmd['proxies'],
                    cmd['proxy_mode'], cmd['state'])
                session.flush()
                ret.append(cobj.getId())

                for atarget, target_name, ascheduler in targets_to_insert:
                    order_in_proxy = None
                    max_clients_per_proxy = 0

                    target = Target()
                    target.target_macaddr = atarget["target_macaddr"]
                    target.id_group = atarget["id_group"]
                    target.target_uuid = atarget["target_uuid"]
                    target.target_bcast = atarget["target_bcast"]
                    target.target_name = atarget["target_name"]
                    target.target_ipaddr = atarget["target_ipaddr"]
                    target.mirrors = atarget["mirrors"]
                    target.target_network = atarget["target_network"]

                    session.add(target)
                    session.flush()
                    if hasattr(session, "refresh"):
                        session.refresh(target)

                    try:
                        candidates = filter(
                            lambda (x): x['uuid'] == atarget["target_uuid"],
                            cmd['proxies'])
                        if len(candidates) == 1:
                            max_clients_per_proxy = candidates[0][
                                'max_clients']
                            order_in_proxy = candidates[0]['priority']
                    except ValueError:
                        self.logger.warn(
                            "Failed to get values 'order_in_proxy' or 'max_clients'"
                        )
                    coh_to_insert.append(
                        self.createCommandsOnHost(
                            cobj.getId(), atarget, target.id, target_name,
                            cmd['max_connection_attempt'], cmd['start_date'],
                            cmd['end_date'], ascheduler, order_in_proxy,
                            max_clients_per_proxy))

            session.execute(self.commands_on_host.insert(), coh_to_insert)
            session.commit()

            return ret
Exemple #5
0
        def cbCreateTargets(fbmirrors, mirrors, schedulers, push_pull = True):

            for i in range(len(targets)):
                if push_pull:
                    # FIXME: we only take the the first mirrors
                    mirror = mirrors[i]
                    fallback = fbmirrors[i]
                    uri = '%s://%s:%s%s' % (mirror['protocol'],
                                            mirror['server'],
                                            str(mirror['port']),
                                            mirror['mountpoint']) \
                        + '||' + '%s://%s:%s%s' % (fallback['protocol'],
                                                   fallback['server'],
                                                   str(fallback['port']),
                                                   fallback['mountpoint'])
                else:
                    uri = '%s://%s' % ('file', root)

                targetsdata[i]['mirrors'] = uri
                # Keep not blacklisted target name for commands_on_host
                # creation.
                targets_name.append(targets[i][1])
                # Maybe could be done in prepareTarget
                targetsdata[i] = self.blacklistTargetHostname(targetsdata[i])

                targets_to_insert.append((targetsdata[i],
                                          targets[i][1],
                                          schedulers[i]))
            session = create_session()
            session.begin()

            cmd = self.createCommand(session, package_id, start_file, parameters,
                                     files, start_script, clean_on_success,
                                     start_date, end_date, connect_as, ctx.userid,
                                     title,
                                     next_connection_delay,
                                     max_connection_attempt,
                                     maxbw,
                                     deployment_intervals, fk_bundle,
                                     order_in_bundle, proxies, proxy_mode,
                                     state, len(targets), cmd_type=cmd_type)
            session.flush()
            # Convergence command (type 2) can have no targets
            # so return command_id if no targets
            if not targets and cmd_type == 2:
                return cmd.getId()

            for atarget, target_name, ascheduler in targets_to_insert :
                target = Target()
                target.target_macaddr = atarget["target_macaddr"]
                target.id_group = atarget["id_group"]
                target.target_uuid = atarget["target_uuid"]
                target.target_bcast = atarget["target_bcast"]
                target.target_name = atarget["target_name"]
                target.target_ipaddr = atarget["target_ipaddr"]
                target.mirrors = atarget["mirrors"]
                target.target_network = atarget["target_network"]

                session.add(target)
                session.flush()
                if hasattr(session, "refresh") :
                    session.refresh(target)

                order_in_proxy = None
                max_clients_per_proxy = 0
                try:
                    candidates = filter(lambda(x): x['uuid'] == atarget["target_uuid"], proxies)
                    if len(candidates) == 1:
                        max_clients_per_proxy = candidates[0]['max_clients']
                        order_in_proxy = candidates[0]['priority']
                except ValueError:
                    self.logger.warn("Failed to get values 'order_in_proxy' or 'max_clients'")
                coh_to_insert.append(self.createCommandsOnHost(cmd.getId(),
                                                               atarget,
                                                               target.id,
                                                               target_name,
                                                               max_connection_attempt,
                                                               start_date,
                                                               end_date,
                                                               ascheduler,
                                                               order_in_proxy,
                                                               max_clients_per_proxy))
            session.execute(self.commands_on_host.insert(), coh_to_insert)

            cohs = cmd.getCohIds()
            self._createPhases(session,
                               cohs,
                               do_wol_with_imaging,
                               do_wol,
                               files,
                               start_script,
                               clean_on_success,
                               do_inventory,
                               do_halt,
                               do_reboot,
                               do_windows_update,
                               is_quick_action)
            cmd.type = cmd_type
            cmd.ready = True
            session.commit()
            return cmd.getId()
Exemple #6
0
        def cbCreateTargets(fbmirrors, mirrors, schedulers, push_pull=True):
            for i in range(len(targets)):
                if push_pull:
                    # FIXME: we only take the the first mirrors
                    mirror = mirrors[i]
                    fallback = fbmirrors[i]
                    uri = '%s://%s:%s%s' % (mirror['protocol'],
                                            mirror['server'],
                                            str(mirror['port']),
                                            mirror['mountpoint']) \
                        + '||' + '%s://%s:%s%s' % (fallback['protocol'],
                                                   fallback['server'],
                                                   str(fallback['port']),
                                                   fallback['mountpoint'])
                else:
                    uri = '%s://%s' % ('file', root)

                targetsdata[i]['mirrors'] = uri
                # Keep not blacklisted target name for commands_on_host
                # creation.
                targets_name.append(targets[i][1])
                # Maybe could be done in prepareTarget
                targetsdata[i] = self.blacklistTargetHostname(targetsdata[i])

                targets_to_insert.append(
                    (targetsdata[i], targets[i][1], schedulers[i]))
            session = create_session()
            session.begin()

            for atarget, target_name, ascheduler in targets_to_insert:
                target = Target()
                target.target_macaddr = atarget["target_macaddr"]
                target.id_group = atarget["id_group"]
                target.target_uuid = atarget["target_uuid"]
                target.target_bcast = atarget["target_bcast"]
                target.target_name = atarget["target_name"]
                target.target_ipaddr = atarget["target_ipaddr"]
                target.mirrors = atarget["mirrors"]
                target.target_network = atarget["target_network"]

                session.add(target)
                session.flush()
                if hasattr(session, "refresh"):
                    session.refresh(target)

                order_in_proxy = None
                max_clients_per_proxy = 0
                try:
                    candidates = filter(
                        lambda (x): x['uuid'] == atarget["target_uuid"],
                        proxies)
                    if len(candidates) == 1:
                        max_clients_per_proxy = candidates[0]['max_clients']
                        order_in_proxy = candidates[0]['priority']
                except ValueError:
                    self.logger.warn(
                        "Failed to get values 'order_in_proxy' or 'max_clients'"
                    )
                coh_to_insert.append(
                    self.createCommandsOnHost(cmd_id, atarget, target.id,
                                              target_name,
                                              cmd.max_connection_attempt,
                                              cmd.start_date, cmd.end_date,
                                              ascheduler, order_in_proxy,
                                              max_clients_per_proxy))
            session.execute(self.commands_on_host.insert(), coh_to_insert)

            cohs = [
                coh for coh in cmd.getCohIds(target_uuids=target_uuids)
                if coh.id not in existing_coh_ids
            ]

            def _get_phase(name):
                return phases.get(name,
                                  False) == 'on' and 'enable' or 'disable'

            self._createPhases(session,
                               cohs,
                               cmd.do_imaging_menu,
                               _get_phase('do_wol'),
                               cmd.files,
                               _get_phase('start_script'),
                               _get_phase('clean_on_success'),
                               _get_phase('do_inventory'),
                               _get_phase('do_halt'),
                               _get_phase('do_reboot'),
                               _get_phase('do_windows_update'),
                               is_quick_action=False)
            session.commit()
            self._force_command_type(cmd_id, 2)
            self._set_command_ready(cmd_id)
            return cmd_id
Exemple #7
0
        def cbCreateTargets(fbmirrors, mirrors, schedulers, push_pull=True):

            for i in range(len(targets)):
                if push_pull:
                    # FIXME: we only take the the first mirrors
                    mirror = mirrors[i]
                    fallback = fbmirrors[i]
                    uri = (
                        "%s://%s:%s%s"
                        % (mirror["protocol"], mirror["server"], str(mirror["port"]), mirror["mountpoint"])
                        + "||"
                        + "%s://%s:%s%s"
                        % (fallback["protocol"], fallback["server"], str(fallback["port"]), fallback["mountpoint"])
                    )
                else:
                    uri = "%s://%s" % ("file", root)

                targetsdata[i]["mirrors"] = uri
                # Keep not blacklisted target name for commands_on_host
                # creation.
                targets_name.append(targets[i][1])
                # Maybe could be done in prepareTarget
                targetsdata[i] = self.blacklistTargetHostname(targetsdata[i])

                targets_to_insert.append((targetsdata[i], targets[i][1], schedulers[i]))
            session = create_session()
            session.begin()
            cmd = self.createCommand(
                session,
                package_id,
                start_file,
                parameters,
                files,
                start_script,
                clean_on_success,
                start_date,
                end_date,
                connect_as,
                ctx.userid,
                title,
                do_halt,
                do_reboot,
                do_wol,
                next_connection_delay,
                max_connection_attempt,
                do_inventory,
                maxbw,
                deployment_intervals,
                fk_bundle,
                order_in_bundle,
                proxies,
                proxy_mode,
            )
            session.flush()

            for atarget, target_name, ascheduler in targets_to_insert:
                target = Target()
                target.target_macaddr = atarget["target_macaddr"]
                target.id_group = atarget["id_group"]
                target.target_uuid = atarget["target_uuid"]
                target.target_bcast = atarget["target_bcast"]
                target.target_name = atarget["target_name"]
                target.target_ipaddr = atarget["target_ipaddr"]
                target.mirrors = atarget["mirrors"]
                target.target_network = atarget["target_network"]

                session.add(target)
                session.flush()
                if hasattr(session, "refresh"):
                    session.refresh(target)

                order_in_proxy = None
                max_clients_per_proxy = 0
                try:
                    candidates = filter(lambda (x): x["uuid"] == atarget["target_uuid"], proxies)
                    if len(candidates) == 1:
                        max_clients_per_proxy = candidates[0]["max_clients"]
                        order_in_proxy = candidates[0]["priority"]
                except ValueError:
                    self.logger.warn("Failed to get values 'order_in_proxy' or 'max_clients'")
                coh_to_insert.append(
                    self.createCommandsOnHost(
                        cmd.getId(),
                        atarget,
                        target.id,
                        target_name,
                        max_connection_attempt,
                        start_date,
                        end_date,
                        ascheduler,
                        order_in_proxy,
                        max_clients_per_proxy,
                    )
                )
            session.execute(self.commands_on_host.insert(), coh_to_insert)
            session.commit()

            return cmd.getId()
Exemple #8
0
        def cbCreateTargets(fbmirrors, mirrors, schedulers, session, push_pull=True):
            # For each command, we prepare the targets list
            # There is one targets list per command because the root package
            # path may change according to the package
            for command in commands:
                targets_to_insert = []
                root = command["root"]
                if root == None:
                    root = self.config.repopath
                for i in range(len(targets)):
                    if push_pull:
                        # FIXME: we only take the the first mirrors
                        mirror = mirrors[i]
                        fallback = fbmirrors[i]
                        uri = (
                            "%s://%s:%s%s"
                            % (mirror["protocol"], mirror["server"], str(mirror["port"]), mirror["mountpoint"])
                            + "||"
                            + "%s://%s:%s%s"
                            % (fallback["protocol"], fallback["server"], str(fallback["port"]), fallback["mountpoint"])
                        )
                    else:
                        uri = "%s://%s" % ("file", root)

                    targetsdata[i]["mirrors"] = uri
                    # Keep not blacklisted target name for commands_on_host
                    # creation.
                    targets_name.append(targets[i][1])
                    # Maybe could be done in prepareTarget
                    targetsdata[i] = self.blacklistTargetHostname(targetsdata[i])
                    targets_to_insert.append((dict(targetsdata[i]), targets[i][1], schedulers[i]))
                targets_to_insert_list.append(targets_to_insert)

            if session == None:
                session = create_session(transactional=True)
            session.begin()
            ret = []
            for cmd, targets_to_insert in zip(commands, targets_to_insert_list):

                time_defaults = self.__getTimeDefaults(cmd["start_date"], cmd["end_date"])
                cmd["start_date"], cmd["end_date"], cmd["max_connection_attempt"] = time_defaults

                cobj = self.createCommand(
                    session,
                    cmd["package_id"],
                    cmd["start_file"],
                    cmd["parameters"],
                    cmd["files"],
                    cmd["start_script"],
                    cmd["clean_on_success"],
                    cmd["start_date"],
                    cmd["end_date"],
                    cmd["connect_as"],
                    ctx.userid,
                    cmd["title"],
                    cmd["issue_halt_to"],
                    cmd["do_reboot"],
                    cmd["do_wol"],
                    cmd["next_connection_delay"],
                    cmd["max_connection_attempt"],
                    cmd["do_inventory"],
                    cmd["maxbw"],
                    cmd["deployment_intervals"],
                    cmd["fk_bundle"],
                    cmd["order_in_bundle"],
                    cmd["proxies"],
                    cmd["proxy_mode"],
                )
                session.flush()
                ret.append(cobj.getId())

                for atarget, target_name, ascheduler in targets_to_insert:
                    order_in_proxy = None
                    max_clients_per_proxy = 0

                    target = Target()
                    target.target_macaddr = atarget["target_macaddr"]
                    target.id_group = atarget["id_group"]
                    target.target_uuid = atarget["target_uuid"]
                    target.target_bcast = atarget["target_bcast"]
                    target.target_name = atarget["target_name"]
                    target.target_ipaddr = atarget["target_ipaddr"]
                    target.mirrors = atarget["mirrors"]
                    target.target_network = atarget["target_network"]

                    session.add(target)
                    session.flush()
                    if hasattr(session, "refresh"):
                        session.refresh(target)

                    try:
                        candidates = filter(lambda (x): x["uuid"] == atarget["target_uuid"], cmd["proxies"])
                        if len(candidates) == 1:
                            max_clients_per_proxy = candidates[0]["max_clients"]
                            order_in_proxy = candidates[0]["priority"]
                    except ValueError:
                        self.logger.warn("Failed to get values 'order_in_proxy' or 'max_clients'")
                    coh_to_insert.append(
                        self.createCommandsOnHost(
                            cobj.getId(),
                            atarget,
                            target.id,
                            target_name,
                            cmd["max_connection_attempt"],
                            cmd["start_date"],
                            cmd["end_date"],
                            ascheduler,
                            order_in_proxy,
                            max_clients_per_proxy,
                        )
                    )

            session.execute(self.commands_on_host.insert(), coh_to_insert)
            session.commit()

            return ret