def pull(self, options={}): ref = options.get('ref') path = os.path.join(settings.STRETCH_CACHE_DIR, hashlib.sha1(self.url).hexdigest()) log.debug('Using repo directory: %s' % path) log.debug('Checking if repo is already cached...') if os.path.exists(path): log.debug('Cached repo exists') repo = git.Repo(path) # Pull repository changes repo.remotes.origin.pull() else: log.debug("Cached repo doesn't exist") log.info('Cloning repo: %s' % self.url) # Create directory utils.makedirs(path) # Clone the repository into cache repo = git.Repo.clone_from(self.url, path) if ref: log.info('Using commit: %s' % ref) repo.head.reset(ref, working_tree=True) else: log.info('No commit specified.') log.info('Using commit: %s' % repo.head.commit.hexsha) return path
def compile_template(self, rel_path, src, dest, node): src_path = os.path.join(src, rel_path) dest_path, ext = os.path.splitext(os.path.join(dest, rel_path)) # Remove .jinja extension ext = ext.lower() if ext != '.jinja': dest_path += ext # Ensure container folder exists utils.makedirs(os.path.split(dest_path)[0]) context = { 'env_name': self.node.data['env_name'], 'host_name': self.data['host_name'], 'instance_id': self.data['_id'], 'release': self.node.data['sha'] } utils.render_template_to_file(src_path, dest_path, [context])
def __init__(self, path, containers, parent, ancestor_paths): self.base_container = None self.path = path self.parent = parent self.built = False # Check for reference loop if self.path in ancestor_paths: raise Exception('Encountered container reference loop.') ancestor_paths.append(self.path) def expect(path): if not os.path.exists(path): raise exceptions.MissingFile(path) # Find Dockerfile self.dockerfile_path = os.path.join(self.path, 'Dockerfile') if not os.path.exists(self.dockerfile_path): raise exceptions.MissingFile(self.dockerfile_path) # Make required directories and files utils.makedirs(os.path.join(self.path, 'files')) utils.makedirs(os.path.join(self.path, 'app')) if not os.path.exists(os.path.join(self.path, 'files/autoload.sh')): with open(os.path.join(self.path, 'files/autoload.sh'), 'w') as f: f.write('exit 3') # Parse and find base containers container_path = os.path.join(self.path, 'container.yml') if os.path.exists(container_path): container_data = get_data(container_path) base_path = container_data.get('from') if base_path: base_container_path = os.path.join(self.path, base_path) self.base_container = Container.create( base_container_path, containers, self, ancestor_paths)
def test_makedirs(mock_os): def mock_makedirs(path): err = OSError() err.errno = errno.EEXIST raise err mock_os.path.isdir.return_value = True mock_os.makedirs = mock_makedirs utils.makedirs('/foo') mock_os.path.isdir.return_value = False with assert_raises(OSError): utils.makedirs('/foo') def mock_makedirs(path): err = OSError() err.errno = 'other' raise err mock_os.makedirs = mock_makedirs mock_os.path.isdir.return_value = True with assert_raises(OSError): utils.makedirs('/foo')