Esempio n. 1
0
 def get_comps_file(self):
     """ Return a file-like object of the comps.xml for the channel. """
     if self.channel.comps:
         comps_view = view.CompsView(self.channel.comps)
         return comps_view.get_file()
     elif self.channel.label in comps_mapping:
         comps_view = view.CompsView(
             Comps(
                 None,
                 os.path.join(CFG.mount_point,
                              comps_mapping[self.channel.label])))
         return comps_view.get_file()
     else:
         if self.channel.cloned_from_id is not None:
             log_debug(
                 1,
                 "No comps and no comps_mapping for [%s] cloned from [%s] trying to get comps from the original one."
                 % (self.channel.id, self.channel.cloned_from_id))
             cloned_from_channel = rhnChannel.Channel().load_by_id(
                 self.channel.cloned_from_id)
             cloned_from_channel_label = cloned_from_channel._row['label']
             return Repository(
                 rhnChannel.channel_info(
                     cloned_from_channel_label)).get_comps_file()
     return None
Esempio n. 2
0
 def get_repomd_file(self, repomd_obj, func_name):
     """ Return a file-like object of the comps.xml/modules.yaml for the channel. """
     if repomd_obj:
         repomd_view = view.RepoMDView(repomd_obj)
         return repomd_view.get_file()
     elif func_name == 'get_comps_file' and self.channel.label in comps_mapping:
         comps_view = view.RepoMDView(
             RepoMD(
                 None,
                 os.path.join(CFG.mount_point,
                              comps_mapping[self.channel.label])))
         return comps_view.get_file()
     else:
         if self.channel.cloned_from_id is not None:
             log_debug(
                 1,
                 "No comps/modules and no comps_mapping for [%s] cloned from [%s] trying to get comps from the original one."
                 % (self.channel.id, self.channel.cloned_from_id))
             cloned_from_channel = rhnChannel.Channel().load_by_id(
                 self.channel.cloned_from_id)
             cloned_from_channel_label = cloned_from_channel._row['label']
             func = getattr(
                 Repository(
                     rhnChannel.channel_info(cloned_from_channel_label)),
                 func_name)
             return func()
     return None
Esempio n. 3
0
def create_channel(label, channel_family, org_id=None, channel_arch=None):
    vdict = new_channel_dict(label=label,
                             channel_family=channel_family,
                             org_id=org_id,
                             channel_arch=channel_arch)
    c = rhnChannel.Channel()
    c.load_from_dict(vdict)
    c.save()
    return c
Esempio n. 4
0
    def test_new_channel_2(self):
        """Tests new channel creation from dictionary"""
        cf = rhnChannel.ChannelFamily()
        cf.load_from_dict(self._new_channel_family_dict())
        cf.save()

        label = cf.get_label()
        vdict = self._new_channel_dict(label=label, channel_family=label)

        c = rhnChannel.Channel()
        c.load_from_dict(vdict)
        c.save()
        channel_id = c.get_id()

        c = rhnChannel.Channel()
        c.load_by_label(label)
        for k, v in list(vdict.items()):
            method = getattr(c, "get_" + k)
            dbv = method()
            self.assertEqual(v, dbv)
        rhnSQL.commit()
        return c
Esempio n. 5
0
    def test_list_channels_1(self):
        """Tests rhnChannel.list_channels"""

        # create some channel
        cf = rhnChannel.ChannelFamily()
        cf.load_from_dict(self._new_channel_family_dict())
        cf.save()

        label = cf.get_label()
        vdict = self._new_channel_dict(label=label, channel_family=label)

        c = rhnChannel.Channel()
        for k, v in list(vdict.items()):
            method = getattr(c, "set_" + k)
            method(v)
        c.save()

        channels = rhnChannel.list_channels(pattern="rhn-unittest-%")
        self.assertTrue(len(channels) > 0)
Esempio n. 6
0
def token_channels(server, server_arch, tokens_obj):
    """ Handle channel subscriptions for the registration token """
    assert(isinstance(tokens_obj, ActivationTokens))

    server_id, server_arch_id = server['id'], server['server_arch_id']

    # what channels are associated with this token (filter only those
    # compatible with this server)
    h = rhnSQL.prepare("""
    select
        rtc.channel_id id, c.name, c.label, c.parent_channel
    from
        rhnRegTokenChannels rtc,
        rhnChannel c,
        rhnServerChannelArchCompat scac
    where rtc.token_id = :token_id
        and rtc.channel_id = c.id
        and c.channel_arch_id = scac.channel_arch_id
        and scac.server_arch_id = :server_arch_id
    """)

    chash = {}
    base_channel_token = None
    base_channel_id = None

    for token in tokens_obj.tokens:
        token_id = token['token_id']
        h.execute(token_id=token_id, server_arch_id=server_arch_id)
        while 1:
            row = h.fetchone_dict()
            if not row:
                break
            channel_id = row['id']
            chash[channel_id] = row
            if row['parent_channel'] is not None:
                # Not a base channel
                continue

            # We only allow for one base channel
            if base_channel_id is not None and channel_id != base_channel_id:
                # Base channels conflict - are they coming from the same
                # token?
                if base_channel_token == token:
                    log_error("Token has multiple base channels", token_id,
                              base_channel_id)
                    raise rhnFault(62,
                                   _("Token `%s' has more than one base channel assigned")
                                   % token['note'])
                raise rhnFault(63, _("Conflicting base channels"))
            base_channel_id = channel_id
            base_channel_token = token

    bc = chash.get(base_channel_id)
    log_debug(4, "base channel", bc)

    # get the base channel for this server
    # Note that we are hitting this codepath after newserver.__save() has been
    # run, which means we've already chosen a base channel
    # from rhnDistChannelMap
    sbc = rhnChannel.get_base_channel(server_id, none_ok=1)

    # prepare the return value
    ret = []

    # now try to figure out which base channel we prefer
    if bc is None:
        if sbc is None:
            # we need at least one base channel definition
            log_error("Server has invalid release and "
                      "token contains no base channels", server_id,
                      tokens_obj.tokens)
            ret.append("System registered without a base channel")
            ret.append("Unsupported release-architecture combination "
                       "(%s, %s)" % (server["release"], server_arch))
            return ret
    else:  # do we need to drop the one from sbc?
        if sbc and sbc["id"] != bc["id"]:  # we need to prefer the token one
            # unsubscribe from old channel(s)
            rhnChannel.unsubscribe_all_channels(server_id)
            sbc = None  # force true on the next test
        if sbc is None:
            # no base channel subscription at this point
            try:
                rhnChannel._subscribe_sql(server_id, bc["id"], commit=0)
            except rhnChannel.SubscriptionCountExceeded:
                ret.append("System registered without a base channel: "
                           "subscription count exceeded for channel %s (%s)" %
                           (bc["name"], bc["label"]))
                return ret

            ret.append("Subscribed to base channel '%s' (%s)" % (
                bc["name"], bc["label"]))
            sbc = bc

    # attempt to subscribe all non-base channels associated with this
    # token
    subscribe_channel = rhnSQL.Procedure("rhn_channel.subscribe_server")
    # Use a set here to ensure uniqueness of the
    # channel family ids used in the loop below.
    channel_family_ids = set()

    for c in filter(lambda a: a["parent_channel"], chash.values()):
        # make sure this channel has the right parent
        if str(c["parent_channel"]) != str(sbc["id"]):
            ret.append("NOT subscribed to channel '%s' "
                       "(not a child of '%s')" % (
                           c["name"], sbc["name"]))
            continue
        try:
            # don't run the EC yet
            # XXX: test return code when this one will start returning
            # a status
            subscribe_channel(server_id, c["id"], 0, None, 0)
            child = rhnChannel.Channel()
            child.load_by_id(c["id"])
            child._load_channel_families()
            cfamid = child._channel_families[0]
            channel_family_ids.add(cfamid)
        except rhnSQL.SQLError, e:
            log_error("Failed channel subscription", server_id,
                      c["id"], c["label"], c["name"])
            ret.append("FAILED to subscribe to channel '%s'" % c["name"])
        else:
            ret.append("Subscribed to channel '%s'" % c["name"])