コード例 #1
0
ファイル: cache.py プロジェクト: waterbear-cloud/paco
def load_cached_project(project_path):
    """
    Creates or updates the paco model cache if any files have been modified since
    the last cache time
    """
    last_mtime = get_max_mtime(project_path, exclude=('build'))
    cache_dir = os.path.join(project_path, "build")
    mtime_cache_file = os.path.join(cache_dir, "cache_last_mtime.txt")
    model_cache_file = os.path.join(cache_dir, "cache_model.pickle")
    pathlib.Path(cache_dir).mkdir(parents=True, exist_ok=True)

    # no last modified time cache file, so load project and cache it
    if not os.path.isfile(mtime_cache_file):
        with open(mtime_cache_file, 'w') as cache_file:
            cache_file.write(str(last_mtime))
        project = load_project_from_yaml(project_path)
        with open(model_cache_file, 'wb') as model_cache_file:
            pickle.dump(project, model_cache_file)

    # already cached, either update the cache or use it
    else:
        with open(mtime_cache_file) as cache_file:
            cache_mtime = cache_file.readlines()
        cache_mtime = float(cache_mtime[0])
        # if you CTRL-C right after running paco, you can create the mtime file but not the pickle cache
        if cache_mtime < last_mtime or not os.path.isfile(model_cache_file):
            # cache is stale
            project = load_project_from_yaml(project_path)
            with open(model_cache_file, 'wb') as model_cache_file:
                pickle.dump(project, model_cache_file)
            with open(mtime_cache_file, 'w') as cache_file:
                cache_file.write(str(last_mtime))
        else:
            # good to go - return the cache
            with open(model_cache_file, 'rb') as model_cache_file:
                project = pickle.load(model_cache_file)

    return project
コード例 #2
0
    def load_project(self, project_init=False, project_only=False, master_only=False):
        "Load a Paco Project from YAML, initialize settings and controllers, and load Service plug-ins."
        self.project_folder = self.home
        if project_init == True:
            return

        # Load the model from YAML
        print("Loading Paco project: %s" % (self.home))
        self.project = load_project_from_yaml(self.project_folder, None)
        if project_only == True:
            return

        # Settings
        self.build_folder = os.path.join(self.home, "build", self.project.name)
        self.master_account = AccountContext(
            paco_ctx=self,
            name='master',
            mfa_account=None
        )
        os.environ['AWS_DEFAULT_REGION'] = self.project['credentials'].aws_default_region
        if master_only:
            return

        # Initialize Controllers so they can initialize their
        # resolve_ref_obj's to allow reference lookups
        self.get_controller('Route53')
        self.get_controller('CodeCommit')
        self.get_controller('S3').init({'name': 'buckets'})
        self.get_controller('NotificationGroups')

        # Load the Service plug-ins
        service_plugins = paco.models.services.list_service_plugins()
        for plugin_name, plugin_module in service_plugins.items():
            try:
                self.project['service'][plugin_name.lower()]
            except KeyError:
                # ignore if no config files for a registered service
                self.log_action_col("Skipping", 'Service', plugin_name)
                continue

            self.log_action_col('Init', 'Service', plugin_name)
            service = plugin_module.instantiate_class(self, self.project['service'][plugin_name.lower()])
            service.init(None)
            self.services[plugin_name.lower()] = service
            self.log_action_col('Init', 'Service', plugin_name, 'Completed')
コード例 #3
0
ファイル: paco_context.py プロジェクト: dwtcourses/paco
    def load_project(self, project_init=False, project_only=False, master_only=False, config_scope=None, command_name=None):
        "Load a Paco Project from YAML, initialize settings and controllers, and load Service plug-ins."
        self.project_folder = self.home
        if project_init == True:
            return

        # Load the model from YAML
        print("Loading Paco project: %s" % (self.home))
        self.project = load_project_from_yaml(self.project_folder, None, warn=self.warn)
        self.paco_buckets = PacoBuckets(self.project)
        if self.verbose:
            print("Finished loading.")
        if project_only == True:
            return

        # Locate a model object and summarize it
        # init commands do not have a config_scope
        # ToDo: the 'accounts' scope does resolve properly
        model_obj = None
        if self.config_scope not in [None, 'accounts']:
            paco_ref = 'paco.ref {}'.format(self.config_scope)
            model_obj = get_model_obj_from_ref(paco_ref, self.project)
            if self.verbose:
                print('Object selected to {}:'.format(self.command))
                print('  Name: {}'.format(
                    getattr(model_obj, 'name', 'unnamed')
                ))
                print('  Type: {}'.format(model_obj.__class__.__name__))
                if getattr(model_obj, 'title', None):
                    print('  Title: {}'.format(model_obj.title))
                if hasattr(model_obj, 'paco_ref_parts'):
                    print('  Reference: {}'.format(model_obj.paco_ref_parts))
                print()

            # Check Notifications and warn about Alarms without any notifications
            if self.warn:
                self.check_notification_config()

        # Settings
        self.master_account = AccountContext(
            paco_ctx=self,
            name='master',
            mfa_account=None
        )
        os.environ['AWS_DEFAULT_REGION'] = self.project['credentials'].aws_default_region
        if master_only or self.config_scope == 'accounts':
            return

        # Initialize Controllers so they can set their resolve_ref_obj's for reference lookups
        self.get_controller('Route53')
        self.get_controller('CodeCommit')
        self.get_controller('S3')
        self.get_controller('SNSTopics')
        self.get_controller('SNS')

        # Load the Service plug-ins
        service_plugins = paco.models.services.list_service_plugins()
        for plugin_name, plugin_module in service_plugins.items():
            try:
                self.project['service'][plugin_name.lower()]
            except KeyError:
                continue

            service_config = self.project['service'][plugin_name.lower()]
            self.log_section_start("Init", service_config)
            service = plugin_module.instantiate_class(self, service_config)
            service.init(None, model_obj)
            self.services[plugin_name.lower()] = service
コード例 #4
0
 def setUpClass(cls):
     # set the fixtures dir
     cls.path = fixtures_path()
     cls.project = load_project_from_yaml(cls.path + os.sep +
                                          cls.project_name)
コード例 #5
0
ファイル: test_loader.py プロジェクト: Mahaswami/paco.models
 def setUp(self):
     # set the fixtures dir
     self.path = fixtures_path()
     self.project = load_project_from_yaml(self.path + os.sep +
                                           self.project_name)