Exemple #1
0
def schedule_action(action_type, action_name=None, delta_time=0,
                    scheduler=None, org_id=None, prerequisite=None):
    action_id = next(rhnSQL.Sequence('rhn_event_id_seq'))

    at = rhnSQL.Table('rhnActionType', 'label')
    if not at.has_key(action_type):
        raise ValueError("Unknown action type %s" % action_type)

    params = {
        'action_id': action_id,
        'org_id': org_id,
        'action_type_id': at[action_type]['id'],
        'action_name': action_name,
        'delta': delta_time,
        'scheduler': scheduler,
        'prerequisite': prerequisite,
    }

    h = rhnSQL.prepare("""
        insert into rhnAction
               (id, org_id, action_type, name, scheduler, earliest_action, prerequisite)
        values (:action_id, :org_id, :action_type_id, :action_name, :scheduler,
                current_timestamp + numtodsinterval(:delta, 'second'), :prerequisite)
    """)
    h.execute(**params)

    return action_id
Exemple #2
0
def _create_rresult(testresult_id, result_label):
    rr_id = rhnSQL.Sequence("rhn_xccdf_rresult_id_seq")()
    h = rhnSQL.prepare(_query_insert_rresult)
    h.execute(rr_id=rr_id,
              testresult_id=testresult_id,
              result_label=result_label)
    return rr_id
Exemple #3
0
    def _set(self, name, val):
        if self._row_server_group is None:
            self._row_server_group = rhnSQL.Row('rhnServerGroup', 'id')
            server_group_id = rhnSQL.Sequence('rhn_server_group_id_seq').next()
            self._row_server_group.create(server_group_id)

        self._row_server_group[name] = val
Exemple #4
0
    def _insert_virtual_instance(self, hostid, guestid, fakeuuid=True):
        log_debug(4, hostid, guestid, fakeuuid)
        viid = rhnSQL.Sequence('rhn_vi_id_seq')()
        q_insert = rhnSQL.prepare("""
            INSERT INTO rhnVirtualInstance
                (id, host_system_id, virtual_system_id, uuid, confirmed)
            VALUES
                (:viid, :host_id, :guest_id, :uuid, 1)
        """)
        fake_uuid = None
        if fakeuuid:
            fake_uuid = str(uuid.uuid4())
            fake_uuid = fake_uuid.replace('-', '').strip()
        q_insert.execute(viid=viid,
                         host_id=hostid,
                         guest_id=guestid,
                         uuid=fake_uuid)

        q_insert = rhnSQL.prepare("""
            INSERT INTO rhnVirtualInstanceInfo
                (instance_id, state, instance_type)
            VALUES
                (:viid,
                 (
                     SELECT rvis.id
                     FROM rhnVirtualInstanceState rvis
                     WHERE rvis.label = 'unknown'
                 ),
                 (
                     SELECT rvit.id
                     FROM rhnVirtualInstanceType rvit
                     WHERE rvit.label = :virt_type
                 ))
        """)
        q_insert.execute(viid=viid, virt_type='fully_virtualized')
    def _push_contents(self, file):

        checksum_type = 'sha256'  # FIXME: this should be configuration option

        file['file_size'] = 0
        file['is_binary'] = 'N'

        file_path = file.get('path')
        file_contents = file.get('file_contents') or ''

        if 'enc64' in file and file_contents:
            file_contents = base64.decodestring(file_contents)

        if 'config_file_type_id' not in file:
            log_debug(
                4,
                "Client does not support config directories, so set file_type_id to 1"
            )
            file['config_file_type_id'] = '1'

        file['checksum_type'] = checksum_type
        file['checksum'] = getStringChecksum(checksum_type, file_contents
                                             or '')

        if file_contents:
            file['file_size'] = len(file_contents)

            if file['file_size'] > self._get_maximum_file_size():
                raise ConfigFileTooLargeError(file_path, file['file_size'])

            # Is the content binary data?
            # XXX We may need a heuristic; this is what the web site does, and we
            # have to be consistent
            # XXX Yes this is iterating over a string
            try:
                file_contents.decode('UTF-8')
            except UnicodeDecodeError:
                file['is_binary'] = 'Y'

        h = rhnSQL.prepare(self._query_content_lookup)
        h.execute(**file)
        row = h.fetchone_dict()

        if row:
            db_contents = rhnSQL.read_lob(row['contents']) or ''
            if file_contents == db_contents:
                # Same content
                file['config_content_id'] = row['id']
                log_debug(5, "same content")
                return

        # We have to insert a new file now
        content_seq = rhnSQL.Sequence('rhn_confcontent_id_seq')
        config_content_id = content_seq.next()
        file['config_content_id'] = config_content_id
        file['contents'] = file_contents

        h = rhnSQL.prepare(self._query_insert_content,
                           blob_map={'contents': 'contents'})
        h.execute(**file)
Exemple #6
0
    def test_lobs(self):
        new_id = rhnSQL.Sequence('misatestlob_id_seq').next()
        h = rhnSQL.prepare("""
            insert into misatestlob (id, val) values (:id, empty_blob())
        """)
        h.execute(id=new_id)

        h = rhnSQL.prepare("""
            select val from misatestlob where id = :id for update of val
        """)
        h.execute(id=new_id)
        row = h.fetchone_dict()
        self.assertNotEqual(row, None)
        lob = row['val']
        s = ""
        for i in range(256):
            s = s + chr(i)
        lob.write(s)
        rhnSQL.commit()

        h = rhnSQL.prepare("""
            select val from misatestlob where id = :id
        """)
        h.execute(id=new_id)
        row = h.fetchone_dict()
        self.assertNotEqual(row, None)
        lob = row['val']
        data = rhnSQL.read_lob(lob)
        self.assertEqual(data, s)
def schedule_kickstart_delta(server_id, kickstart_session_id,
                             installs, removes):
    log_debug(3, server_id, kickstart_session_id)
    row = get_kickstart_session_info(kickstart_session_id, server_id)
    org_id = row['org_id']
    scheduler = row['scheduler']

    action_id = rhnAction.schedule_server_action(
        server_id,
        action_type='packages.runTransaction', action_name="Package delta",
        delta_time=0, scheduler=scheduler, org_id=org_id,
    )

    package_delta_id = rhnSQL.Sequence('rhn_packagedelta_id_seq').next()

    h = rhnSQL.prepare(_query_insert_package_delta)
    h.execute(package_delta_id=package_delta_id)

    h = rhnSQL.prepare(_query_insert_action_package_delta)
    h.execute(action_id=action_id, package_delta_id=package_delta_id)

    h = rhnSQL.prepare(_query_insert_package_delta_element)
    col_names = ['n', 'v', 'r', 'e']
    __execute_many(h, installs, col_names, operation='insert', a=None,
                   package_delta_id=package_delta_id)
    __execute_many(h, removes, col_names, operation='delete', a=None,
                   package_delta_id=package_delta_id)

    update_ks_session_table(kickstart_session_id, 'package_synch_scheduled',
                            action_id, server_id)

    return action_id
Exemple #8
0
def __create_server_group(group_label, org_id):
    """ create the initial server groups for a new server """
    # Add this new server to the pending group
    h = rhnSQL.prepare("""
    select sg.id, sg.current_members
    from rhnServerGroup sg
    where sg.group_type = ( select id from rhnServerGroupType
                            where label = :group_label )
    and sg.org_id = :org_id
    """)
    h.execute(org_id=org_id, group_label=group_label)
    data = h.fetchone_dict()
    if not data:
        # create the requested group
        ret_id = rhnSQL.Sequence("rhn_server_group_id_seq")()
        h = rhnSQL.prepare("""
        insert into rhnServerGroup
        ( id, name, description,
          group_type, org_id)
        select
            :new_id, sgt.name, sgt.name,
            sgt.id, :org_id
        from rhnServerGroupType sgt
        where sgt.label = :group_label
        """)
        rownum = h.execute(new_id=ret_id, org_id=org_id,
                           group_label=group_label)
        if rownum == 0:
            # No rows were created, probably invalid label
            raise rhnException("Could not create new group for org=`%s'"
                               % org_id, group_label)
    else:
        ret_id = data["id"]
    return ret_id
Exemple #9
0
    def _set(self, name, val):
        if self._row_reg_token is None:
            self._row_reg_token = rhnSQL.Row('rhnRegToken', 'id')
            token_id = rhnSQL.Sequence('rhn_reg_token_seq').next()
            self._row_reg_token.create(token_id)
            self._row_reg_token['usage_limit'] = None

        self._row_reg_token[name] = val
Exemple #10
0
 def getid(self):
     if not self.server.has_key("id"):
         sysid = rhnSQL.Sequence("rhn_server_id_seq")()
         self.server["digital_server_id"] = "ID-%09d" % sysid
         # we can't reset the id column, so we need to poke into
         # internals. kind of illegal, but it works...
         self.server.data["id"] = (sysid, 0)
     else:
         sysid = self.server["id"]
     return sysid
    def store(self):
        if self.type in getNotificationsTypeDisabled():
            return

        i = rhnSQL.prepare("""
          SELECT users.id
            FROM (
                  SELECT wc.id,
                         (CASE WHEN (SELECT 1
                                       FROM rhnwebcontactchangelog wccl
                                       JOIN (
                                             SELECT web_contact_id, max(id) current_state
                                               FROM rhnwebcontactchangelog uch
                                           GROUP BY web_contact_id
                                            ) X ON X.current_state = wccl.id
                                       JOIN rhnwebcontactchangestate wcsc ON wccl.change_state_id = wcsc.id
                                      WHERE wcsc.label = 'disabled'
                                        AND X.web_contact_id = wc.id) = 1
                               THEN 1
                               ELSE 0
                           END) disabled
                    FROM rhnusergrouptype ugt
                    JOIN rhnusergroup ug ON ug.group_type = ugt.id
                    JOIN rhnusergroupmembers ugm ON ugm.user_group_id = ug.id
                    JOIN web_contact wc ON wc.id = ugm.user_id
                   WHERE ugt.label = 'satellite_admin'
                 ) users
           WHERE users.disabled = 0;
        """)
        i.execute()
        affected_user = i.fetchall_dict() or None
        if not affected_user:
            return

        mid = rhnSQL.Sequence('suse_notif_message_id_seq')()

        h = rhnSQL.prepare("""
          INSERT INTO suseNotificationMessage (id, type, data)
          VALUES (:mid, :type, :data)
        """)
        h.execute(mid=mid, type=self.type, data=json.dumps(self.__dict__))

        j = rhnSQL.prepare("""
          INSERT INTO suseUserNotification (id, user_id, message_id)
          VALUES (sequence_nextval('suse_user_notif_id_seq'), :uid, :mid)
        """)

        for user in affected_user:
            j.execute(uid=user['id'], mid=mid)
        rhnSQL.commit()
Exemple #12
0
def _insertPrep_rhnCryptoKey(rhn_cryptokey_id, description, org_id):
    """ inserts a row given that a cert is not already in the database
        lob rewrite occurs later during update.
        Used ONLY by: store_rhnCryptoKey(...)
    """

    # NOTE: due to a uniqueness constraint on description
    #       we can't increment and reinsert a row, so we only
    #       do so if the row does not exist.
    #       bugzilla: 120297 - and no I don't like it.
    rhn_cryptokey_id_seq = rhnSQL.Sequence('rhn_cryptokey_id_seq')
    rhn_cryptokey_id = rhn_cryptokey_id_seq.next()
    # print 'no cert found, new one with id:', rhn_cryptokey_id
    h = rhnSQL.prepare(_queryInsertCryptoCertInfo)
    # ...insert
    h.execute(rhn_cryptokey_id=rhn_cryptokey_id,
              description=description, org_id=org_id)
    return rhn_cryptokey_id
Exemple #13
0
 def getid(self):
     if self.id == 0:
         self.id = rhnSQL.Sequence(self.sequence)()
     return self.id
 def new_config_channel_id(self):
     return rhnSQL.Sequence('rhn_confchan_id_seq').next()