"Fossies" - the Fresh Open Source Software Archive

Member "freezer-10.0.0/freezer/openstack/admin.py" (14 Apr 2021, 6290 Bytes) of package /linux/misc/openstack/freezer-10.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "admin.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 9.0.0_vs_10.0.0.

    1 # (c) Copyright 2014,2015 Hewlett-Packard Development Company, L.P.
    2 #
    3 # Licensed under the Apache License, Version 2.0 (the "License");
    4 # you may not use this file except in compliance with the License.
    5 # You may obtain a copy of the License at
    6 #
    7 #     http://www.apache.org/licenses/LICENSE-2.0
    8 #
    9 # Unless required by applicable law or agreed to in writing, software
   10 # distributed under the License is distributed on an "AS IS" BASIS,
   11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   12 # See the License for the specific language governing permissions and
   13 # limitations under the License.
   14 
   15 
   16 """
   17 Freezer Admin modes related functions
   18 """
   19 
   20 import time
   21 
   22 from oslo_config import cfg
   23 from oslo_log import log
   24 from oslo_service import loopingcall
   25 
   26 CONF = cfg.CONF
   27 LOG = log.getLogger(__name__)
   28 
   29 
   30 class AdminOs(object):
   31     def __init__(self, client_manager):
   32         """
   33         :param client_manager:
   34         :return:
   35         """
   36         self.client_manager = client_manager
   37         self.cinder_client = self.client_manager.get_cinder()
   38 
   39     def del_cinderbackup_and_dependend_incremental(self, backup_id):
   40         """
   41         :param backup_id: backup_id  of cinder volume
   42         :return:
   43         """
   44         cinder_client = self.cinder_client
   45         search_opts = {
   46             'parent_id': backup_id
   47         }
   48         backups = cinder_client.backups.list(search_opts=search_opts)
   49         if backups:
   50             for backup in backups:
   51                 self.del_cinderbackup_and_dependend_incremental(backup.id)
   52         LOG.info("preparing to delete backup %s", backup_id)
   53         cinder_client.backups.delete(backup_id)
   54 
   55         start_time = int(time.time())
   56 
   57         def wait_del_backup():
   58             timeout = 120
   59             del_backup = cinder_client.backups.list(
   60                 search_opts={'id': backup_id})
   61             if len(del_backup) == 0:
   62                 LOG.info("Delete backup %s complete" % backup_id)
   63                 raise loopingcall.LoopingCallDone()
   64             if del_backup[0].status in ['error', 'error_deleting']:
   65                 raise Exception("Delete backup %s failed, "
   66                                 "the status of backup is %s."
   67                                 % (backup_id, del_backup[0].status))
   68             if (del_backup[0].status == 'deleting') and (int(time.time()) -
   69                                                          start_time > timeout):
   70                 LOG.error("Delete backup %s failed, In a state of "
   71                           "deleting over 120s", backup_id)
   72                 raise Exception(
   73                     "Delete backup %s failed due to timeout over 120s, "
   74                     "the status of backup is %s."
   75                     % (backup_id, del_backup[0].status))
   76         timer = loopingcall.FixedIntervalLoopingCall(wait_del_backup)
   77         timer.start(interval=0.5).wait()
   78 
   79     def del_off_limit_fullbackup(self, volume_id, keep_number):
   80         """
   81         :param volume_id: id of Volume
   82         :param keep_number: int  keep number of fullbackup
   83         :return:
   84         """
   85         cinder_client = self.cinder_client
   86         search_opts = {
   87             'volume_id': volume_id,
   88             'status': 'available'
   89         }
   90         backups = cinder_client.backups.list(search_opts=search_opts,
   91                                              sort='created_at:asc')
   92         # Filter fullbackup
   93         fullbackups = [backup for backup in backups
   94                        if not backup.is_incremental]
   95         if len(fullbackups) <= keep_number:
   96             LOG.info("The numbers of %s fullbackup is %d,"
   97                      "but keep-number-of-fullbackup is %d,"
   98                      "don't need delete old backups."
   99                      % (volume_id, len(fullbackups), keep_number))
  100             return
  101         for fullbackup in fullbackups[:-keep_number]:
  102             self.del_cinderbackup_and_dependend_incremental(fullbackup.id)
  103 
  104     def remove_cinderbackup_older_than(self, volume_id,
  105                                        remove_older_timestamp):
  106         """
  107         :param volume_id: id of Volume
  108         :param remove_older_timestamp: int
  109         :return:
  110         """
  111         gmstr_remove_older_timestamp = \
  112             time.strftime("%Y-%m-%dT%H:%M:%S",
  113                           time.gmtime(remove_older_timestamp))
  114         cinder_client = self.cinder_client
  115         search_opts = {
  116             'volume_id': volume_id,
  117             'status': 'available'
  118         }
  119         # remove cinder backup order by created_at desc, otherwise
  120         # we need find it incremental backup
  121         backups = cinder_client.backups.list(search_opts=search_opts,
  122                                              sort='created_at:desc')
  123         for backup in backups:
  124 
  125             if backup.created_at <= gmstr_remove_older_timestamp:
  126                 LOG.info("preparing to delete backup %s", backup.id)
  127                 cinder_client.backups.delete(backup.id)
  128                 start_time = int(time.time())
  129 
  130                 def wait_del_backup():
  131                     timeout = 120
  132                     del_backup = cinder_client.backups.list(
  133                         search_opts={'id': backup.id})
  134                     if len(del_backup) == 0:
  135                         LOG.info("Delete backup %s complete" % backup.id)
  136                         raise loopingcall.LoopingCallDone()
  137                     if del_backup[0].status in ['error', 'error_deleting']:
  138                         raise Exception("Delete backup %s failed, "
  139                                         "the status of backup is %s."
  140                                         % (backup.id, del_backup[0].status))
  141                     if (del_backup[0].status == 'deleting') \
  142                             and (int(time.time()) - start_time > timeout):
  143                         LOG.error("Delete backup %s failed, In a state of "
  144                                   "deleting over 120s", backup.id)
  145                         raise Exception(
  146                             "Delete backup %s failed due to timeout over 120s,"
  147                             " the status of backup is %s."
  148                             % (backup.id, del_backup[0].status))
  149 
  150                 timer = loopingcall.FixedIntervalLoopingCall(wait_del_backup)
  151                 timer.start(interval=0.5).wait()