def _write_buildproc_yaml(build_data, env, user, cmd, volumes, app_folder): """ Write a proc.yaml for the container and return the container path """ buildproc = ProcData({ 'app_folder': str(app_folder), 'app_name': build_data.app_name, 'app_repo_url': '', 'app_repo_type': '', 'buildpack_url': '', 'buildpack_version': '', 'config_name': 'build', 'env': env, 'host': '', 'port': 0, 'version': build_data.version, 'release_hash': '', 'settings': {}, 'user': user, 'cmd': cmd, 'volumes': volumes, 'proc_name': 'build', 'image_name': build_data.image_name, 'image_url': build_data.image_url, 'image_md5': build_data.image_md5, }) # write a proc.yaml for the container. with open('buildproc.yaml', 'w') as f: f.write(buildproc.as_yaml()) return get_container_path(buildproc)
def get_proc_lxc_tmpl_ctx(self): return { 'proc_path': get_container_path(self.config), 'network_config': get_lxc_network_config(get_lxc_version()), 'memory_limits': self.get_lxc_memory_limits(), 'volumes': self.get_lxc_volume_str(), }
def test_proc_lxc(self, get_lxc_version_, runner): get_lxc_version_.return_value = parse_version('1.0.8') runner.setup() p = get_container_path(runner.config) proc_lxc = open(os.path.join(p, '../proc.lxc'), 'r').read() assert 'lxc.network.type = none' in proc_lxc assert 'lxc.mount.entry = overlayfs ' in proc_lxc
def get_lxc_volume_str(self): content = '' # append lines to bind-mount volumes. volumes = getattr(self.config, 'volumes', []) or [] volume_tmpl = "\nlxc.mount.entry = %s %s%s none bind 0 0" for outside, inside in volumes: content += volume_tmpl % (outside, get_container_path( self.config), inside) return content
def test_setup(self, get_lxc_version_, runner): get_lxc_version_.return_value = parse_version('1.0.8') runner.setup() p = get_container_path(runner.config) assert os.path.exists(os.path.join(p, 'env.sh')), 'env.sh missing' assert os.path.exists(os.path.join(p, 'proc.sh')), 'proc.sh missing' assert os.path.exists(os.path.join(p, 'settings.yaml')), \ 'settings.yaml missing' assert os.path.exists(os.path.join(p, '../proc.lxc')), \ 'proc.lxc missing' assert os.path.isdir(os.path.join(p, '../work')), \ 'work_dir does not exists'
def get_proc_lxc_tmpl_ctx(self): proc_path = get_container_path(self.config) work_path = get_lxc_work_path(self.config) ctx = { 'proc_path': proc_path, 'image_path': self.get_image_folder(), 'work_path': work_path, 'network_config': get_lxc_network_config(get_lxc_version()), 'memory_limits': self.get_lxc_memory_limits(), 'volumes': self.get_lxc_volume_str(), } overlay_config_fmt = get_lxc_overlayfs_config_fmt(get_lxc_version()) ctx['overlay_config'] = overlay_config_fmt % ctx return ctx
def write_proc_conf(settings): proc_path = get_proc_path(settings) proc_conf_vars = { 'proc_yaml_path': posixpath.join(proc_path, 'proc.yaml'), 'container_name': get_container_name(settings), 'container_path': get_container_path(settings), 'log': posixpath.join(proc_path, 'log'), 'runner': get_runner(settings), 'user': '******', } proc_conf_tmpl = get_template('proc.conf') files.upload_template(proc_conf_tmpl, posixpath.join(proc_path, 'proc.conf'), proc_conf_vars, use_sudo=True)
def make_proc_dirs(self): print("Making directories") proc_path = get_proc_path(self.config) mkdir(proc_path) container_path = get_container_path(self.config) mkdir(container_path) work_path = get_lxc_work_path(self.config) mkdir(work_path) volumes = getattr(self.config, 'volumes', None) or [] for _, inside in volumes: mkdir(os.path.join(container_path, inside.lstrip('/')))
def write_env_sh(self): print("Writing env.sh") envsh_path = os.path.join(get_container_path(self.config), 'env.sh') with open(envsh_path, 'w') as f: def _interpolate(val): if isinstance(val, six.string_types) and val.startswith('$'): return os.environ.get(val[1:], '') return val def format_var(key, val): return 'export %s="%s"' % (key, _interpolate(val)) e = self.config.env or {} env_str = '\n'.join(format_var(k, e[k]) for k in e) + '\n' f.write(env_str)
def write_proc_sh(self): """ Write the script that is the first thing called inside the container. It sets env vars and then calls the real program. """ print("Writing proc.sh") context = { 'tmp': '/tmp', 'home': '/app', 'settings': '/settings.yaml', 'envsh': '/env.sh', 'port': self.config.port, 'cmd': self.get_cmd(), } sh_path = os.path.join(get_container_path(self.config), 'proc.sh') rendered = get_template('proc.sh') % context with open(sh_path, 'w') as f: f.write(rendered) st = os.stat(sh_path) os.chmod(sh_path, st.st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
def uptest(self): # copy the uptester into the container. ensure it's executable. src = pkg_resources.resource_filename('vr.runners', 'uptester/uptester') container_path = get_container_path(self.config) dest = os.path.join(container_path, 'uptester') shutil.copy(src, dest) st = os.stat(dest) os.chmod(dest, st.st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) proc_name = getattr(self.config, 'proc_name', None) if proc_name: app_path = get_app_path(self.config) uptests_path = os.path.join(app_path, 'uptests', proc_name) if os.path.isdir(uptests_path): # run an LXC container for the uptests. inside_path = os.path.join('/app/uptests', proc_name) cmd = '/uptester %s %s %s ' % (inside_path, self.config.host, self.config.port) self._lxc_start(special_cmd=cmd) else: # There are no uptests for this proc. Output an empty # JSON list. print("[]")
def ensure_char_devices(self): for path_, devnums, perms in self.char_devices: fullpath = get_container_path(self.config) + path_ ensure_char_device(fullpath, devnums, perms)
def write_settings_yaml(self): print("Writing settings.yaml") path = os.path.join(get_container_path(self.config), 'settings.yaml') with open(path, 'w') as f: f.write( yaml.safe_dump(self.config.settings, default_flow_style=False))