def stop(self, only_shutdown: bool, output: AbstractOutputWriter): # stop container stop_container_script = StopContainerScript(self.container_commands).render() stop_container_command = get_script_command('stop-container', stop_container_script) exit_code = self.exec(stop_container_command) if exit_code != 0: raise ValueError('Failed to stop the container')
def test_script_arguments(self): script_name = 'echo' script_content = 'echo test $1 $2' # no arguments script_command = get_script_command(script_name, script_content, script_args=None, logging=False) output = self.spotty.exec(script_command) self.assertEqual(output.strip(), 'test') # custom arguments script_command = get_script_command(script_name, script_content, script_args=['arg 1', 'arg 2'], logging=False) output = self.spotty.exec(script_command) self.assertEqual(output.strip(), 'test arg 1 arg 2')
def _run(self, instance_manager: AbstractInstanceManager, args: Namespace, output: AbstractOutputWriter): # check that the script exists script_name = args.script_name scripts = instance_manager.project_config.scripts if script_name not in scripts: raise ValueError( 'Script "%s" is not defined in the configuration file.' % script_name) # replace script parameters params = parse_script_parameters(args.parameter) script_content = render_script(scripts[script_name], params) # check that the instance is started if not instance_manager.is_running(): raise InstanceNotRunningError( instance_manager.instance_config.name) # sync the project with the instance if not args.no_sync: try: instance_manager.sync(output) except NothingToDoError: pass # get a command to run the script with "docker exec" script_command = get_script_command(script_name, script_content, script_args=args.custom_args, logging=args.logging) command = instance_manager.container_commands.exec(script_command, interactive=True, tty=True, user=args.user) # wrap the command with the tmux session if instance_manager.use_tmux: session_name = args.session_name if args.session_name else 'spotty-script-%s' % script_name default_command = instance_manager.container_commands.exec( get_bash_command(), interactive=True, tty=True, user=args.user) command = get_tmux_session_command(command, session_name, script_name, default_command=default_command, keep_pane=True) # execute command on the host OS instance_manager.exec(command)
def test_script_logging(self): script_name = 'echo' script_content = 'echo test' # run the script with logging script_command = get_script_command(script_name, script_content, script_args=None, logging=True) output = self.spotty.exec(script_command) self.assertEqual(output.strip(), 'test') # read the latest log file output = self.spotty.exec( 'bash -c \'cat /var/log/spotty/run/$(ls -rt /var/log/spotty/run | tail -n1)\'' ) self.assertEqual(output.splitlines()[0], 'test')
def render(self, print_trace: bool = False) -> str: # read template file template_path = os.path.join(os.path.dirname(__file__), 'data', 'start_container.sh.tpl') with open(template_path) as f: template = f.read() # generate "docker build" command if necessary if self.commands.instance_config.dockerfile_path: image_name = '%s:%d' % ( self.commands.instance_config.full_container_name, time.time()) build_image_cmd = self.commands.build(image_name) pull_image_cmd = '' else: image_name = self.commands.instance_config.container_config.image build_image_cmd = '' pull_image_cmd = self.commands.pull() # generate a command to run the startup script exec_script_cmd = '' if self.commands.instance_config.container_config.commands: startup_script_cmd = get_script_command( 'container-startup-commands', self.commands.instance_config.container_config.commands) exec_script_cmd = self.commands.exec(startup_script_cmd, user='******') # render the script content = chevron.render( template, data={ 'bash_flags': 'set -xe' if print_trace else 'set -e', 'is_created_cmd': self.commands.is_created(), 'remove_cmd': self.commands.remove(), 'build_image_cmd': build_image_cmd, 'pull_image_cmd': pull_image_cmd, 'tmp_container_dir': self.commands.instance_config.host_container_dir, 'start_container_cmd': self.commands.run(image_name), 'docker_exec_startup_script_cmd': exec_script_cmd, }, partials_dict=self._partials()) return content
def start_container(self, output: AbstractOutputWriter, dry_run=False): """Starts or restarts container on the host OS.""" # make sure the Dockerfile exists self._check_dockerfile_exists() # sync the project with the instance try: self.sync(output, dry_run=dry_run) except NothingToDoError: pass # generate a script that starts container start_container_script = StartContainerScript(self.container_commands).render() start_container_command = get_script_command('start-container', start_container_script) # start the container exit_code = self.exec(start_container_command) if exit_code != 0: raise ValueError('Failed to start the container')