コード例 #1
0
def app_restore(storage, bucket_name=None):
    """ Restores the app source code from the backups location on the filesystem.

  Args:
    storage: A str, one of the StorageTypes class members.
    bucket_name: A str, the name of the bucket to restore apps from.
  Returns:
    True on success, False otherwise.
  """
    # Create app backups dir if it doesn't exist.
    if not makedirs(APP_BACKUP_DIR_LOCATION):
        logging.warning(
            "Dir '{0}' already exists. Skipping dir creation...".format(
                APP_BACKUP_DIR_LOCATION))

    # Download from GCS to backups location.
    if storage == StorageTypes.GCS:
        objects = gcs_helper.list_bucket(bucket_name)
        for app_path in objects:
            if not app_path.startswith(gcs_helper.APPS_GCS_PREFIX):
                continue

            # Only keep the relative name of the app file.
            # E.g. myapp.tar.gz (app_file) out of apps/myapp.tar.gz (app_path)
            app_file = app_path[len(gcs_helper.APPS_GCS_PREFIX):]
            source = 'gs://{0}/{1}'.format(bucket_name, app_path)
            destination = '{0}/{1}'.format(APP_BACKUP_DIR_LOCATION, app_file)
            if not gcs_helper.download_from_bucket(source, destination):
                logging.error(
                    "Error while downloading '{0}' from GCS.".format(source))
                delete_app_tars(APP_BACKUP_DIR_LOCATION)
                return False

    # Deploy apps.
    apps_to_deploy = [
        os.path.join(APP_BACKUP_DIR_LOCATION, app)
        for app in os.listdir(APP_BACKUP_DIR_LOCATION)
    ]
    if not deploy_apps(apps_to_deploy):
        logging.error("Failed to successfully deploy one or more of the "
                      "following apps: {0}".format(apps_to_deploy))
        return False

    return True
コード例 #2
0
def app_restore(storage, bucket_name=None):
  """ Restores the app source code from the backups location on the filesystem.

  Args:
    storage: A str, one of the StorageTypes class members.
    bucket_name: A str, the name of the bucket to restore apps from.
  Returns:
    True on success, False otherwise.
  """
  # Create app backups dir if it doesn't exist.
  if not makedirs(APP_BACKUP_DIR_LOCATION):
    logging.warning("Dir '{0}' already exists. Skipping dir creation...".
      format(APP_BACKUP_DIR_LOCATION))

  # Download from GCS to backups location.
  if storage == StorageTypes.GCS:
    objects = gcs_helper.list_bucket(bucket_name)
    for app_path in objects:
      if not app_path.startswith(gcs_helper.APPS_GCS_PREFIX):
        continue

      # Only keep the relative name of the app file.
      # E.g. myapp.tar.gz (app_file) out of apps/myapp.tar.gz (app_path)
      app_file = app_path[len(gcs_helper.APPS_GCS_PREFIX):]
      source = 'gs://{0}/{1}'.format(bucket_name, app_path)
      destination = '{0}/{1}'.format(APP_BACKUP_DIR_LOCATION, app_file)
      if not gcs_helper.download_from_bucket(source, destination):
        logging.error("Error while downloading '{0}' from GCS.".format(source))
        delete_app_tars(APP_BACKUP_DIR_LOCATION)
        return False

  # Deploy apps.
  apps_to_deploy = [os.path.join(APP_BACKUP_DIR_LOCATION, app) for app in
    os.listdir(APP_BACKUP_DIR_LOCATION)]
  if not deploy_apps(apps_to_deploy):
    logging.error("Failed to successfully deploy one or more of the "
      "following apps: {0}".format(apps_to_deploy))
    return False

  return True