def init(self, environment): """ Writes an AppScalefile in the local directory, that contains common configuration parameters. Args: environment: A str that indicates whether the AppScalefile to write should be tailed to a 'cloud' environment or a 'cluster' environment. Raises: AppScalefileException: If there already is an AppScalefile in the local directory. """ # first, make sure there isn't already an AppScalefile in this # directory appscalefile_location = self.get_appscalefile_location() if os.path.exists(appscalefile_location): raise AppScalefileException( "There is already an AppScalefile" + " in this directory. Please remove it and run 'appscale init'" + " again to generate a new AppScalefile.") # next, see if we're making a cloud template file or a cluster # template file if environment == 'cloud': template_file = self.TEMPLATE_CLOUD_APPSCALEFILE elif environment == 'cluster': template_file = self.TEMPLATE_CLUSTER_APPSCALEFILE else: raise BadConfigurationException( "The environment you specified " + "was invalid. Valid environments are 'cloud' and " + "'cluster'.") # finally, copy the template AppScalefile there shutil.copy(template_file, appscalefile_location)
def read_appscalefile(self): """ Checks the local directory for an AppScalefile and reads its contents. Raises: AppScalefileException: If there is no AppScalefile in the local directory. Returns: The contents of the AppScalefile in the current working directory. """ try: with open(self.get_appscalefile_location()) as file_handle: return file_handle.read() except IOError: raise AppScalefileException("No AppScalefile found in this " + "directory. Please run 'appscale init' to generate one and try " + "again.")
def ensure_appscalefile_is_up_to_date(cls): """ Examines the AppScalefile in the current working directory to make sure it specifies a keyname and group, updating it if it does not. This scenario can occur if the user wants us to automatically generate a keyname and group for them (in which case they don't specify either). Returns: True if the AppScalefile was up to date, or False if there were changes made to make it up to date. Raises: AppScalefileException: If there is no AppScalefile in the current working directory. """ appscalefile_path = os.getcwd() + os.sep + "AppScalefile" if not os.path.exists(appscalefile_path): raise AppScalefileException("Couldn't find an AppScale file at {0}" \ .format(appscalefile_path)) file_contents = '' with open(appscalefile_path) as file_handle: file_contents = file_handle.read() yaml_contents = yaml.safe_load(file_contents) # Don't write to the AppScalefile if there are no changes to make to it. if 'keyname' in yaml_contents and 'group' in yaml_contents: return True file_contents += "\n# Automatically added by the AppScale Tools: " random_suffix = str(uuid.uuid4()).replace('-', '') cloud_name = "appscale{0}".format(random_suffix) if 'keyname' not in yaml_contents: file_contents += "\nkeyname : {0}".format(cloud_name) if 'group' not in yaml_contents: file_contents += "\ngroup : {0}".format(cloud_name) with open(appscalefile_path, 'w') as file_handle: file_handle.write(file_contents) return False
def read_appscalefile(self): """Checks the local directory for an AppScalefile and reads its contents. Raises: AppScalefileException: If there is no AppScalefile in the local directory. Returns: The contents of the AppScalefile in the current working directory. """ # Don't check for existence and then open it later - this lack of # atomicity is potentially a TOCTOU vulnerability. try: with open(self.get_appscalefile_location()) as file_handle: return file_handle.read() except IOError: raise AppScalefileException( "No AppScalefile found in this " + "directory. Please run 'appscale init' to generate one and try " + "again.")