def start(self, pull: bool, recreate: bool): """If not started, start the containers defined in config""" verb = self.context['VERBOSE'] debug = self.context['DEBUG'] try: docker_actions.check_cts_are_running(self.project_name) puts(colored.yellow('[INFO]') + ' stakkr is already started ...') sys.exit(0) except SystemError: pass if pull is True: command.launch_cmd_displays_output( self.compose_base_cmd + ['pull'], verb, debug, True) recreate_param = '--force-recreate' if recreate is True else '--no-recreate' cmd = self.compose_base_cmd + [ 'up', '-d', recreate_param, '--remove-orphans' ] command.verbose(self.context['VERBOSE'], 'Command: ' + ' '.join(cmd)) command.launch_cmd_displays_output(cmd, verb, debug, True) self.running_cts, self.cts = docker_actions.get_running_containers( self.project_name) if self.running_cts is 0: raise SystemError( "Couldn't start the containers, run the start with '-v' and '-d'" ) os_patch.start(self.project_name) self._run_iptables_rules() self._run_services_post_scripts()
def start(self, pull: bool, recreate: bool): """If not started, start the containers defined in config""" verb = self.context['VERBOSE'] debug = self.context['DEBUG'] try: docker_actions.check_cts_are_running(self.project_name) puts(colored.yellow('[INFO]') + ' stakkr is already started ...') sys.exit(0) except SystemError: pass if pull is True: command.launch_cmd_displays_output(self.compose_base_cmd + ['pull'], verb, debug, True) recreate_param = '--force-recreate' if recreate is True else '--no-recreate' cmd = self.compose_base_cmd + ['up', '-d', recreate_param, '--remove-orphans'] command.verbose(self.context['VERBOSE'], 'Command: ' + ' '.join(cmd)) command.launch_cmd_displays_output(cmd, verb, debug, True) self.running_cts, self.cts = docker_actions.get_running_containers(self.project_name) if self.running_cts is 0: raise SystemError("Couldn't start the containers, run the start with '-v' and '-d'") os_patch.start(self.project_name) self._run_iptables_rules() self._run_services_post_scripts()
def start(self, container: str, pull: bool, recreate: bool, proxy: bool): """If not started, start the containers defined in config.""" self.init_project() verb = self.context['VERBOSE'] debug = self.context['DEBUG'] self._is_up(container) if pull is True: command.launch_cmd_displays_output(self._get_compose_base_cmd() + ['pull'], verb, debug, True) recreate_param = '--force-recreate' if recreate is True else '--no-recreate' cmd = self._get_compose_base_cmd() + ['up', '-d', recreate_param, '--remove-orphans'] cmd += _get_single_container_option(container) command.verbose(self.context['VERBOSE'], 'Command: ' + ' '.join(cmd)) command.launch_cmd_displays_output(cmd, verb, debug, True) running_cts, cts = docker.get_running_containers(self.project_name) if not running_cts: raise SystemError("Couldn't start the containers, run the start with '-v' and '-d'") self._run_iptables_rules(cts) if proxy is True: conf = self.config['proxy'] Proxy(conf.get('http_port'), conf.get('https_port')).start( docker.get_network_name(self.project_name))
def start(self, container: str, pull: bool, recreate: bool, proxy: bool): """If not started, start the containers defined in config.""" self.init_project() verb = self.context['VERBOSE'] debug = self.context['DEBUG'] self._is_up(container) if pull is True: command.launch_cmd_displays_output( self._get_compose_base_cmd() + ['pull'], verb, debug, True) recreate_param = '--force-recreate' if recreate is True else '--no-recreate' cmd = self._get_compose_base_cmd() + [ 'up', '-d', recreate_param, '--remove-orphans' ] cmd += _get_single_container_option(container) command.verbose(self.context['VERBOSE'], 'Command: ' + ' '.join(cmd)) command.launch_cmd_displays_output(cmd, verb, debug, True) running_cts, cts = docker.get_running_containers(self.project_name) if not running_cts: raise SystemError( "Couldn't start the containers, run the start with '-v' and '-d'" ) self._run_iptables_rules(cts) if proxy is True: conf = self.config['proxy'] Proxy(conf.get('http_port'), conf.get('https_port')).start( docker.get_network_name(self.project_name))
def _call_service_post_script(self, service: str): service_script = package_utils.get_file( 'static', 'services/{}.sh'.format(service)) if os.path.isfile(service_script) is True: cmd = ['bash', service_script, docker.get_ct_item(service, 'name')] subprocess.call(cmd) command.verbose(self.context['VERBOSE'], 'Service Script : ' + ' '.join(cmd))
def _run_iptables_rules(self): """For some containers we need to add iptables rules added from the config""" block_config = self.config['network-block'] for service, ports in block_config.items(): error, msg = docker_actions.block_ct_ports(service, ports, self.project_name) if error is True: click.secho(msg, fg='red') continue command.verbose(self.context['VERBOSE'], msg)
def _run_iptables_rules(self): """For some containers we need to add iptables rules added from the config.""" block_config = self.config['network-block'] for service, ports in block_config.items(): error, msg = docker.block_ct_ports(service, ports, self.project_name) if error is True: click.secho(msg, fg='red') continue command.verbose(self.context['VERBOSE'], msg)
def console(self, container: str, user: str, tty: bool): """Enter a container. Stakkr will try to guess the right shell.""" docker.check_cts_are_running(self.project_name) tty = 't' if tty is True else '' ct_name = docker.get_ct_name(container) cmd = ['docker', 'exec', '-u', user, '-i' + tty] cmd += [docker.get_ct_name(container), docker.guess_shell(ct_name)] subprocess.call(cmd) command.verbose(self.context['VERBOSE'], 'Command : "' + ' '.join(cmd) + '"')
def console(self, container: str, user: str, tty: bool): """Enter a container (stakkr allows only apache / php and mysql)""" docker_actions.check_cts_are_running(self.project_name) tty = 't' if tty is True else '' ct_name = docker_actions.get_ct_name(container) cmd = ['docker', 'exec', '-u', user, '-i' + tty] cmd += [docker_actions.get_ct_name(container), docker_actions.guess_shell(ct_name)] subprocess.call(cmd) command.verbose(self.context['VERBOSE'], 'Command : "' + ' '.join(cmd) + '"')
def console(self, container: str, user: str, tty: bool): """Enter a container. Stakkr will try to guess the right shell.""" self.init_project() docker.check_cts_are_running(self.project_name) tty = 't' if tty is True else '' ct_name = docker.get_ct_name(container) cmd = ['docker', 'exec', '-u', user, '-i' + tty] cmd += [docker.get_ct_name(container), docker.guess_shell(ct_name)] subprocess.call(cmd) command.verbose(self.context['VERBOSE'], 'Command : "' + ' '.join(cmd) + '"')
def exec_cmd(self, container: str, user: str, args: tuple, tty: bool): """Run a command from outside to any container. Wrapped into /bin/sh""" docker_actions.check_cts_are_running(self.project_name) # Protect args to avoid strange behavior in exec args = ['"{}"'.format(arg) for arg in args] tty = 't' if tty is True else '' ct_name = docker_actions.get_ct_name(container) cmd = ['docker', 'exec', '-u', user, '-i' + tty, ct_name, 'sh', '-c'] cmd += ["""test -d "/var/{0}" && cd "/var/{0}" ; exec {1}""".format(self.cwd_relative, ' '.join(args))] command.verbose(self.context['VERBOSE'], 'Command : "' + ' '.join(cmd) + '"') subprocess.call(cmd, stdin=sys.stdin)
def _run_iptables_rules(self, cts: dict): """For some containers we need to add iptables rules added from the config.""" for _, ct_info in cts.items(): container = ct_info['compose_name'] ct_config = self.config['services'][container] if 'blocked_ports' not in ct_config: continue blocked_ports = ct_config['blocked_ports'] error, msg = docker.block_ct_ports(container, blocked_ports, self.project_name) if error is True: click.secho(msg, fg='red') continue command.verbose(self.context['VERBOSE'], msg)
def console(self, container: str, user: str, tty: bool): """Enter a container (stakkr allows only apache / php and mysql)""" docker_actions.check_cts_are_running(self.project_name) tty = 't' if tty is True else '' ct_name = docker_actions.get_ct_name(container) cmd = ['docker', 'exec', '-u', user, '-i' + tty] cmd += [ docker_actions.get_ct_name(container), docker_actions.guess_shell(ct_name) ] subprocess.call(cmd) command.verbose(self.context['VERBOSE'], 'Command : "' + ' '.join(cmd) + '"')
def exec_cmd(self, container: str, user: str, args: tuple, tty: bool, workdir: str): """Run a command from outside to any container. Wrapped into /bin/sh.""" self.init_project() docker.check_cts_are_running(self.project_name) # Protect args to avoid strange behavior in exec args = ['"{}"'.format(arg) for arg in args] workdir = "/var/{}".format( self.cwd_relative) if workdir is None else workdir tty = 't' if tty is True else '' ct_name = docker.get_ct_name(container) cmd = [ 'docker', 'exec', '-u', user, '-i' + tty, '-w', workdir, ct_name, 'sh', '-c' ] cmd += ["""exec {}""".format(' '.join(args))] command.verbose(self.context['VERBOSE'], 'Command : "' + ' '.join(cmd) + '"') subprocess.call(cmd, stdin=sys.stdin)
def start(self, container: str, pull: bool, recreate: bool): """If not started, start the containers defined in config""" verb = self.context['VERBOSE'] debug = self.context['DEBUG'] self._is_containers_running(container) if pull is True: command.launch_cmd_displays_output(self.compose_base_cmd + ['pull'], verb, debug, True) recreate_param = '--force-recreate' if recreate is True else '--no-recreate' cmd = self.compose_base_cmd + ['up', '-d', recreate_param, '--remove-orphans'] cmd += self._get_single_container_option(container) command.verbose(self.context['VERBOSE'], 'Command: ' + ' '.join(cmd)) command.launch_cmd_displays_output(cmd, verb, debug, True) self.running_cts, self.cts = docker_actions.get_running_containers(self.project_name) if self.running_cts is 0: raise SystemError("Couldn't start the containers, run the start with '-v' and '-d'") self._run_iptables_rules() self._run_services_post_scripts()
def _call_service_post_script(self, service: str): service_script = package_utils.get_file('static', 'services/{}.sh'.format(service)) if os.path.isfile(service_script) is True: cmd = ['bash', service_script, docker_actions.get_ct_item(service, 'name')] subprocess.call(cmd) command.verbose(self.context['VERBOSE'], 'Service Script : ' + ' '.join(cmd))