signature
stringlengths
8
3.44k
body
stringlengths
0
1.41M
docstring
stringlengths
1
122k
id
stringlengths
5
17
def get_distro_info(self,<EOL>shutit_pexpect_child=None,<EOL>loglevel=logging.DEBUG):
shutit_global.shutit_global_object.yield_to_draw()<EOL>shutit_pexpect_child = shutit_pexpect_child or self.get_current_shutit_pexpect_session().pexpect_child<EOL>shutit_pexpect_session = self.get_shutit_pexpect_session_from_child(shutit_pexpect_child)<EOL>return shutit_pexpect_session.get_distro_info(loglevel=loglevel)<EOL>
Get information about which distro we are using, placing it in the environment object. Fails if distro could not be determined. Should be called with the container is started up, and uses as core info as possible. Note: if the install type is apt, it issues the following: - apt-get update - apt-get install -y -qq lsb-release @param shutit_pexpect_child: See send()
f10347:c2:m58
def lsb_release(self,<EOL>shutit_pexpect_child=None,<EOL>loglevel=logging.DEBUG):
shutit_global.shutit_global_object.yield_to_draw()<EOL>shutit_pexpect_child = shutit_pexpect_child or self.get_current_shutit_pexpect_session().pexpect_child<EOL>shutit_pexpect_session = self.get_shutit_pexpect_session_from_child(shutit_pexpect_child)<EOL>return shutit_pexpect_session.lsb_release(loglevel=loglevel)<EOL>
Get distro information from lsb_release.
f10347:c2:m59
def set_password(self,<EOL>password,<EOL>user='<STR_LIT>',<EOL>shutit_pexpect_child=None,<EOL>note=None):
shutit_global.shutit_global_object.yield_to_draw()<EOL>shutit_pexpect_child = shutit_pexpect_child or self.get_current_shutit_pexpect_session().pexpect_child<EOL>shutit_pexpect_session = self.get_shutit_pexpect_session_from_child(shutit_pexpect_child)<EOL>return shutit_pexpect_session.set_password(password,user=user,note=note)<EOL>
Sets the password for the current user or passed-in user. As a side effect, installs the "password" package. @param user: username to set the password for. Defaults to '' (i.e. current user) @param password: password to set for the user @param shutit_pexpect_child: See send() @param note: See send()
f10347:c2:m60
def whoami(self,<EOL>note=None,<EOL>shutit_pexpect_child=None,<EOL>loglevel=logging.DEBUG):
shutit_global.shutit_global_object.yield_to_draw()<EOL>shutit_pexpect_child = shutit_pexpect_child or self.get_current_shutit_pexpect_session().pexpect_child<EOL>shutit_pexpect_session = self.get_shutit_pexpect_session_from_child(shutit_pexpect_child)<EOL>return shutit_pexpect_session.whoami(note=note,loglevel=loglevel)<EOL>
Returns the current user by executing "whoami". @param note: See send() @return: the output of "whoami" @rtype: string
f10347:c2:m61
def is_user_id_available(self,<EOL>user_id,<EOL>shutit_pexpect_child=None,<EOL>note=None,<EOL>loglevel=logging.DEBUG):
shutit_global.shutit_global_object.yield_to_draw()<EOL>shutit_pexpect_child = shutit_pexpect_child or self.get_current_shutit_pexpect_session().pexpect_child<EOL>shutit_pexpect_session = self.get_shutit_pexpect_session_from_child(shutit_pexpect_child)<EOL>return shutit_pexpect_session.is_user_id_available(user_id,<EOL>note=note,<EOL>loglevel=loglevel)<EOL>
Determine whether the specified user_id available. @param user_id: User id to be checked. @param shutit_pexpect_child: See send() @param note: See send() @type user_id: integer @rtype: boolean @return: True is the specified user id is not used yet, False if it's already been assigned to a user.
f10347:c2:m62
def push_repository(self,<EOL>repository,<EOL>docker_executable='<STR_LIT>',<EOL>shutit_pexpect_child=None,<EOL>expect=None,<EOL>note=None,<EOL>loglevel=logging.INFO):
shutit_global.shutit_global_object.yield_to_draw()<EOL>self.handle_note(note)<EOL>shutit_pexpect_child = shutit_pexpect_child or self.get_shutit_pexpect_session_from_id('<STR_LIT>').pexpect_child<EOL>expect = expect or self.expect_prompts['<STR_LIT>']<EOL>send = docker_executable + '<STR_LIT>' + self.repository['<STR_LIT:user>'] + '<STR_LIT:/>' + repository<EOL>timeout = <NUM_LIT><EOL>self.log('<STR_LIT>' + send,level=logging.INFO)<EOL>self.multisend(docker_executable + '<STR_LIT>',<EOL>{'<STR_LIT>':self.repository['<STR_LIT:user>'], '<STR_LIT>':self.repository['<STR_LIT:password>'], '<STR_LIT>':self.repository['<STR_LIT:email>']},<EOL>shutit_pexpect_child=shutit_pexpect_child,<EOL>expect=expect)<EOL>self.send(send,<EOL>shutit_pexpect_child=shutit_pexpect_child,<EOL>expect=expect,<EOL>timeout=timeout,<EOL>check_exit=False,<EOL>fail_on_empty_before=False,<EOL>loglevel=loglevel)<EOL>self.handle_note_after(note)<EOL>return True<EOL>
Pushes the repository. @param repository: Repository to push. @param docker_executable: Defaults to 'docker' @param expect: See send() @param shutit_pexpect_child: See send() @type repository: string @type docker_executable: string
f10347:c2:m63
def do_repository_work(self,<EOL>repo_name,<EOL>repo_tag=None,<EOL>docker_executable='<STR_LIT>',<EOL>password=None,<EOL>force=None,<EOL>loglevel=logging.DEBUG,<EOL>note=None,<EOL>tag=None,<EOL>push=None,<EOL>export=None,<EOL>save=None):
shutit_global.shutit_global_object.yield_to_draw()<EOL>self.handle_note(note)<EOL>shutit_pexpect_session = self.get_current_shutit_pexpect_session()<EOL>if tag is None:<EOL><INDENT>tag = self.repository['<STR_LIT>']<EOL><DEDENT>if push is None:<EOL><INDENT>push = self.repository['<STR_LIT>']<EOL><DEDENT>if export is None:<EOL><INDENT>export = self.repository['<STR_LIT>']<EOL><DEDENT>if save is None:<EOL><INDENT>save = self.repository['<STR_LIT>']<EOL><DEDENT>if not (push or export or save or tag):<EOL><INDENT>if force:<EOL><INDENT>tag = True<EOL><DEDENT>else:<EOL><INDENT>return True<EOL><DEDENT><DEDENT>shutit_pexpect_child = self.get_shutit_pexpect_session_from_id('<STR_LIT>').pexpect_child<EOL>expect = self.expect_prompts['<STR_LIT>']<EOL>server = self.repository['<STR_LIT>']<EOL>repo_user = self.repository['<STR_LIT:user>']<EOL>if repo_tag is None:<EOL><INDENT>repo_tag = self.repository['<STR_LIT>']<EOL><DEDENT>if repo_user and repo_name:<EOL><INDENT>repository = '<STR_LIT>' % (repo_user, repo_name)<EOL>repository_tar = '<STR_LIT>' % (repo_user, repo_name)<EOL><DEDENT>elif repo_user:<EOL><INDENT>repository = repository_tar = repo_user<EOL><DEDENT>elif repo_name:<EOL><INDENT>repository = repository_tar = repo_name<EOL><DEDENT>else:<EOL><INDENT>repository = repository_tar = '<STR_LIT>'<EOL><DEDENT>if not repository:<EOL><INDENT>self.fail('<STR_LIT>', shutit_pexpect_child=shutit_pexpect_child, throw_exception=False) <EOL><DEDENT>if (export or save) and not repository_tar:<EOL><INDENT>self.fail('<STR_LIT>', shutit_pexpect_child=shutit_pexpect_child, throw_exception=False) <EOL><DEDENT>if server != '<STR_LIT>':<EOL><INDENT>repository = '<STR_LIT>' % (server, repository)<EOL><DEDENT>if self.build['<STR_LIT>']:<EOL><INDENT>repo_tag += '<STR_LIT>'<EOL><DEDENT>if self.repository['<STR_LIT>']:<EOL><INDENT>suffix_date = time.strftime(self.repository['<STR_LIT>'])<EOL>repository = '<STR_LIT>' % (repository, suffix_date)<EOL>repository_tar = '<STR_LIT>' % (repository_tar, suffix_date)<EOL><DEDENT>if repository != '<STR_LIT>' and len(repository.split('<STR_LIT::>')) > <NUM_LIT:1>:<EOL><INDENT>repository_with_tag = repository<EOL>repo_tag = repository.split('<STR_LIT::>')[<NUM_LIT:1>]<EOL><DEDENT>elif repository != '<STR_LIT>':<EOL><INDENT>repository_with_tag = repository + '<STR_LIT::>' + repo_tag<EOL><DEDENT>repository = repository.lower()<EOL>repository_with_tag = repository_with_tag.lower()<EOL>if server == '<STR_LIT>' and len(repository) > <NUM_LIT:30> and push:<EOL><INDENT>self.fail("""<STR_LIT>""" + repository + """<STR_LIT>""", shutit_pexpect_child=shutit_pexpect_child, throw_exception=False) <EOL><DEDENT>if self.send(docker_executable + '<STR_LIT>' + self.target['<STR_LIT>'] + '<STR_LIT:U+0020>' + repository_with_tag,<EOL>expect=[expect,'<STR_LIT>'],<EOL>shutit_pexpect_child=shutit_pexpect_child,<EOL>timeout=<NUM_LIT>,<EOL>check_exit=False,<EOL>loglevel=loglevel) == <NUM_LIT:1>:<EOL><INDENT>self.send(self.host['<STR_LIT:password>'],<EOL>expect=expect,<EOL>check_exit=False,<EOL>record_command=False,<EOL>shutit_pexpect_child=shutit_pexpect_child,<EOL>echo=False,<EOL>loglevel=loglevel)<EOL><DEDENT>self.build['<STR_LIT>'] += '<STR_LIT>' + repository_with_tag<EOL>if export or save:<EOL><INDENT>shutit_pexpect_session.pause_point('<STR_LIT>', print_input=False, level=<NUM_LIT:3>)<EOL>if export:<EOL><INDENT>bzfile = (repository_tar + '<STR_LIT>')<EOL>self.log('<STR_LIT>' + bzfile,level=logging.DEBUG)<EOL>if self.send(docker_executable + '<STR_LIT>' + self.target['<STR_LIT>'] + '<STR_LIT>' + bzfile,<EOL>expect=[expect, '<STR_LIT>'],<EOL>timeout=<NUM_LIT>,<EOL>shutit_pexpect_child=shutit_pexpect_child,<EOL>loglevel=loglevel) == <NUM_LIT:1>:<EOL><INDENT>self.send(password,<EOL>expect=expect,<EOL>shutit_pexpect_child=shutit_pexpect_child,<EOL>loglevel=loglevel)<EOL><DEDENT>self.log('<STR_LIT>' + bzfile, level=loglevel)<EOL>self.log('<STR_LIT>' + bzfile + '<STR_LIT>', level=logging.DEBUG)<EOL>self.build['<STR_LIT>'] += ('<STR_LIT>' + bzfile)<EOL>self.build['<STR_LIT>'] += ('<STR_LIT>' + bzfile + '<STR_LIT>')<EOL><DEDENT>if save:<EOL><INDENT>bzfile = (repository_tar + '<STR_LIT>')<EOL>self.log('<STR_LIT>' + bzfile,level=logging.DEBUG)<EOL>if self.send(docker_executable + '<STR_LIT>' + self.target['<STR_LIT>'] + '<STR_LIT>' + bzfile,<EOL>expect=[expect, '<STR_LIT>'],<EOL>timeout=<NUM_LIT>,<EOL>shutit_pexpect_child=shutit_pexpect_child,<EOL>loglevel=loglevel) == <NUM_LIT:1>:<EOL><INDENT>self.send(password,<EOL>expect=expect,<EOL>shutit_pexpect_child=shutit_pexpect_child,<EOL>loglevel=loglevel)<EOL><DEDENT>self.log('<STR_LIT>' + bzfile, level=logging.DEBUG)<EOL>self.log('<STR_LIT>' + bzfile + '<STR_LIT>', level=logging.DEBUG)<EOL>self.build['<STR_LIT>'] += ('<STR_LIT>' + bzfile)<EOL>self.build['<STR_LIT>'] += ('<STR_LIT>' + bzfile + '<STR_LIT>')<EOL><DEDENT><DEDENT>if self.repository['<STR_LIT>']:<EOL><INDENT>self.push_repository(repository, docker_executable=docker_executable, expect=expect, shutit_pexpect_child=shutit_pexpect_child)<EOL>self.build['<STR_LIT>'] = (self.build['<STR_LIT>'] + '<STR_LIT>' + repository)<EOL><DEDENT>self.handle_note_after(note)<EOL>return True<EOL>
Commit, tag, push, tar a docker container based on the configuration we have. @param repo_name: Name of the repository. @param docker_executable: Defaults to 'docker' @param password: @param force: @type repo_name: string @type docker_executable: string @type password: string @type force: boolean
f10347:c2:m64
def get_config(self,<EOL>module_id,<EOL>option,<EOL>default=None,<EOL>boolean=False,<EOL>secret=False,<EOL>forceask=False,<EOL>forcenone=False,<EOL>hint=None):
shutit_global.shutit_global_object.yield_to_draw()<EOL>cfg = self.cfg<EOL>if module_id not in cfg.keys():<EOL><INDENT>cfg[module_id] = {}<EOL><DEDENT>if not self.config_parser.has_section(module_id):<EOL><INDENT>self.config_parser.add_section(module_id)<EOL><DEDENT>if not forceask and self.config_parser.has_option(module_id, option):<EOL><INDENT>if boolean:<EOL><INDENT>cfg[module_id][option] = self.config_parser.getboolean(module_id, option)<EOL><DEDENT>else:<EOL><INDENT>cfg[module_id][option] = self.config_parser.get(module_id, option)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if not forcenone:<EOL><INDENT>if shutit_global.shutit_global_object.interactive > <NUM_LIT:0>:<EOL><INDENT>if self.build['<STR_LIT>'] is None:<EOL><INDENT>answer = None<EOL>while answer not in ('<STR_LIT:yes>','<STR_LIT>','<STR_LIT>') and shutit_global.shutit_global_object.interactive > <NUM_LIT:1>:<EOL><INDENT>answer = shutit_util.util_raw_input(prompt=shutit_util.colorise('<STR_LIT>', '<STR_LIT>' + '<STR_LIT>'),default='<STR_LIT:yes>',ispass=secret)<EOL><DEDENT>self.build['<STR_LIT>'] = answer in ('<STR_LIT:yes>','<STR_LIT>') or shutit_global.shutit_global_object.interactive < <NUM_LIT:2><EOL><DEDENT>if self.build['<STR_LIT>'] and default != None:<EOL><INDENT>cfg[module_id][option] = default<EOL><DEDENT>else:<EOL><INDENT>prompt = '<STR_LIT>' + module_id + '<STR_LIT:.>' + option<EOL>if default != None:<EOL><INDENT>prompt = prompt + '<STR_LIT>' + str(default) + '<STR_LIT:)>'<EOL><DEDENT>if hint != None:<EOL><INDENT>prompt = prompt + '<STR_LIT>' + hint<EOL><DEDENT>answer = None<EOL>if boolean:<EOL><INDENT>while answer not in ('<STR_LIT:yes>','<STR_LIT>'):<EOL><INDENT>answer = shutit_util.util_raw_input(prompt=shutit_util.colorise('<STR_LIT>',prompt + '<STR_LIT>'),ispass=secret)<EOL><DEDENT>if answer == '<STR_LIT:yes>':<EOL><INDENT>answer = True<EOL><DEDENT>elif answer == '<STR_LIT>':<EOL><INDENT>answer = False<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if re.search('<STR_LIT>',option) is None:<EOL><INDENT>answer = shutit_util.util_raw_input(prompt=shutit_util.colorise('<STR_LIT>',prompt) + '<STR_LIT>',ispass=secret)<EOL><DEDENT>else:<EOL><INDENT>answer = shutit_util.util_raw_input(ispass=True,prompt=shutit_util.colorise('<STR_LIT>',prompt) + '<STR_LIT>')<EOL><DEDENT><DEDENT>if answer == '<STR_LIT>' and default != None:<EOL><INDENT>answer = default<EOL><DEDENT>cfg[module_id][option] = answer<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if default != None:<EOL><INDENT>cfg[module_id][option] = default<EOL><DEDENT>else:<EOL><INDENT>self.fail('<STR_LIT>' + option + '<STR_LIT>' + module_id + '<STR_LIT>' + module_id + '<STR_LIT:U+0020>' + option + '<STR_LIT>', throw_exception=False) <EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>cfg[module_id][option] = default<EOL><DEDENT><DEDENT>return True<EOL>
Gets a specific config from the config files, allowing for a default. Handles booleans vs strings appropriately. @param module_id: module id this relates to, eg com.mycorp.mymodule.mymodule @param option: config item to set @param default: default value if not set in files @param boolean: whether this is a boolean value or not (default False) @param secret: whether the config item is a secret @param forceask: if set to True, allows you to override any value already set (default False) @param forcenone: if set to True, allows you to set the value to None (default False) @param hint: if we are interactive, then show this prompt to help the user input a useful value @type module_id: string @type option: string @type default: string @type boolean: boolean @type secret: boolean @type forceask: boolean @type forcenone: boolean @type hint: string
f10347:c2:m65
def get_emailer(self, cfg_section):
shutit_global.shutit_global_object.yield_to_draw()<EOL>import emailer<EOL>return emailer.Emailer(cfg_section, self)<EOL>
Sends an email using the mailer
f10347:c2:m68
def get_shutit_pexpect_session_from_child(self, shutit_pexpect_child):
shutit_global.shutit_global_object.yield_to_draw()<EOL>if not isinstance(shutit_pexpect_child, pexpect.pty_spawn.spawn):<EOL><INDENT>self.fail('<STR_LIT>' + str(type(shutit_pexpect_child)),throw_exception=True) <EOL><DEDENT>for key in self.shutit_pexpect_sessions:<EOL><INDENT>if self.shutit_pexpect_sessions[key].pexpect_child == shutit_pexpect_child:<EOL><INDENT>return self.shutit_pexpect_sessions[key]<EOL><DEDENT><DEDENT>return self.fail('<STR_LIT>',throw_exception=True)<EOL>
Given a pexpect/child object, return the shutit_pexpect_session object.
f10347:c2:m70
def get_shutit_pexpect_session_id(self, shutit_pexpect_child):
shutit_global.shutit_global_object.yield_to_draw()<EOL>if not isinstance(shutit_pexpect_child, pexpect.pty_spawn.spawn):<EOL><INDENT>self.fail('<STR_LIT>',throw_exception=True) <EOL><DEDENT>for key in self.shutit_pexpect_sessions:<EOL><INDENT>if self.shutit_pexpect_sessions[key].pexpect_child == shutit_pexpect_child:<EOL><INDENT>return key<EOL><DEDENT><DEDENT>return self.fail('<STR_LIT>',throw_exception=True)<EOL>
Given a pexpect child object, return the shutit_pexpect_session_id object.
f10347:c2:m71
def get_shutit_pexpect_session_from_id(self, shutit_pexpect_id):
shutit_global.shutit_global_object.yield_to_draw()<EOL>for key in self.shutit_pexpect_sessions:<EOL><INDENT>if self.shutit_pexpect_sessions[key].pexpect_session_id == shutit_pexpect_id:<EOL><INDENT>return self.shutit_pexpect_sessions[key]<EOL><DEDENT><DEDENT>return self.fail('<STR_LIT>',throw_exception=True)<EOL>
Get the pexpect session from the given identifier.
f10347:c2:m72
def get_commands(self):
shutit_global.shutit_global_object.yield_to_draw()<EOL>s = '<STR_LIT>'<EOL>for c in self.build['<STR_LIT>']:<EOL><INDENT>if isinstance(c, str):<EOL><INDENT>if c and c[<NUM_LIT:0>] != '<STR_LIT:U+0020>':<EOL><INDENT>s += c + '<STR_LIT:\n>'<EOL><DEDENT><DEDENT><DEDENT>return s<EOL>
Gets command that have been run and have not been redacted.
f10347:c2:m77
def build_report(self, msg='<STR_LIT>'):
shutit_global.shutit_global_object.yield_to_draw()<EOL>s = '<STR_LIT:\n>'<EOL>s += '<STR_LIT>'<EOL>s += '<STR_LIT>' + shutit_global.shutit_global_object.build_id + '<STR_LIT:\n>'<EOL>s += self.get_commands()<EOL>s += '<STR_LIT>' + shutit_global.shutit_global_object.build_id + '<STR_LIT:\n>'<EOL>s += '<STR_LIT>'<EOL>s += '<STR_LIT>'<EOL>s += '<STR_LIT>' + shutit_global.shutit_global_object.build_id + '<STR_LIT:\n>'<EOL>s += '<STR_LIT>' + msg + '<STR_LIT:\n>'<EOL>if self.build['<STR_LIT>'] != '<STR_LIT>':<EOL><INDENT>s += self.build['<STR_LIT>'] + '<STR_LIT:\n>'<EOL><DEDENT>else:<EOL><INDENT>s += '<STR_LIT>'<EOL><DEDENT>if '<STR_LIT>' in self.target:<EOL><INDENT>s += '<STR_LIT>' + self.target['<STR_LIT>'] + '<STR_LIT:\n>'<EOL><DEDENT>s += '<STR_LIT>' + shutit_global.shutit_global_object.build_id + '<STR_LIT:\n>'<EOL>s += '<STR_LIT>'<EOL>s += '<STR_LIT>' + sys.executable<EOL>for arg in sys.argv:<EOL><INDENT>s += '<STR_LIT:U+0020>' + arg<EOL><DEDENT>s += '<STR_LIT:\n>'<EOL>s += '<STR_LIT>'<EOL>return s<EOL>
Resposible for constructing a report to be output as part of the build. Returns report as a string.
f10347:c2:m78
def match_string(self, string_to_match, regexp):
shutit_global.shutit_global_object.yield_to_draw()<EOL>if not isinstance(string_to_match, str):<EOL><INDENT>return None<EOL><DEDENT>lines = string_to_match.split('<STR_LIT:\r\n>')<EOL>new_lines = []<EOL>for line in lines:<EOL><INDENT>new_lines = new_lines + line.split('<STR_LIT:\r>')<EOL><DEDENT>for line in lines:<EOL><INDENT>new_lines = new_lines + line.split('<STR_LIT:\n>')<EOL><DEDENT>lines = new_lines<EOL>if not shutit_util.check_regexp(regexp):<EOL><INDENT>self.fail('<STR_LIT>' + regexp) <EOL><DEDENT>for line in lines:<EOL><INDENT>match = re.match(regexp, line)<EOL>if match is not None:<EOL><INDENT>if match.groups():<EOL><INDENT>return match.group(<NUM_LIT:1>)<EOL><DEDENT>return True<EOL><DEDENT><DEDENT>return None<EOL>
Get regular expression from the first of the lines passed in in string that matched. Handles first group of regexp as a return value. @param string_to_match: String to match on @param regexp: Regexp to check (per-line) against string @type string_to_match: string @type regexp: string Returns None if none of the lines matched. Returns True if there are no groups selected in the regexp. else returns matching group (ie non-None)
f10347:c2:m79
def module_ids(self, rev=False):
shutit_global.shutit_global_object.yield_to_draw()<EOL>ids = sorted(list(self.shutit_map.keys()),key=lambda module_id: self.shutit_map[module_id].run_order)<EOL>if rev:<EOL><INDENT>return list(reversed(ids))<EOL><DEDENT>return ids<EOL>
Gets a list of module ids guaranteed to be sorted by run_order, ignoring conn modules (run order < 0).
f10347:c2:m80
def is_to_be_built_or_is_installed(self, shutit_module_obj):
shutit_global.shutit_global_object.yield_to_draw()<EOL>cfg = self.cfg<EOL>if cfg[shutit_module_obj.module_id]['<STR_LIT>']:<EOL><INDENT>return True<EOL><DEDENT>return self.is_installed(shutit_module_obj)<EOL>
Returns true if this module is configured to be built, or if it is already installed.
f10347:c2:m81
def is_installed(self, shutit_module_obj):
shutit_global.shutit_global_object.yield_to_draw()<EOL>if shutit_module_obj.module_id in self.get_current_shutit_pexpect_session_environment().modules_installed:<EOL><INDENT>return True<EOL><DEDENT>if shutit_module_obj.module_id in self.get_current_shutit_pexpect_session_environment().modules_not_installed:<EOL><INDENT>return False<EOL><DEDENT>if shutit_module_obj.is_installed(self):<EOL><INDENT>self.get_current_shutit_pexpect_session_environment().modules_installed.append(shutit_module_obj.module_id)<EOL>return True<EOL><DEDENT>else:<EOL><INDENT>if shutit_module_obj.module_id not in self.get_current_shutit_pexpect_session_environment().modules_not_installed:<EOL><INDENT>self.get_current_shutit_pexpect_session_environment().modules_not_installed.append(shutit_module_obj.module_id)<EOL><DEDENT>return False<EOL><DEDENT>return False<EOL>
Returns true if this module is installed. Uses cache where possible.
f10347:c2:m82
def allowed_image(self, module_id):
shutit_global.shutit_global_object.yield_to_draw()<EOL>self.log("<STR_LIT>" + module_id,level=logging.DEBUG)<EOL>cfg = self.cfg<EOL>if self.build['<STR_LIT>']:<EOL><INDENT>self.log("<STR_LIT>" + module_id,level=logging.DEBUG)<EOL>return True<EOL><DEDENT>self.log(str(cfg[module_id]['<STR_LIT>']),level=logging.DEBUG)<EOL>if cfg[module_id]['<STR_LIT>']:<EOL><INDENT>for regexp in cfg[module_id]['<STR_LIT>']:<EOL><INDENT>if not shutit_util.check_regexp(regexp):<EOL><INDENT>self.fail('<STR_LIT>' + regexp) <EOL><DEDENT>if re.match('<STR_LIT>' + regexp + '<STR_LIT:$>', self.target['<STR_LIT>']):<EOL><INDENT>return True<EOL><DEDENT><DEDENT><DEDENT>return False<EOL>
Given a module id, determine whether the image is allowed to be built.
f10347:c2:m84
def print_modules(self):
shutit_global.shutit_global_object.yield_to_draw()<EOL>cfg = self.cfg<EOL>module_string = '<STR_LIT>'<EOL>module_string += '<STR_LIT>'<EOL>module_string += '<STR_LIT>'<EOL>for module_id in self.module_ids():<EOL><INDENT>module_string += '<STR_LIT:U+0020>' + str(self.shutit_map[module_id].run_order) + '<STR_LIT:U+0020>' + str(<EOL>cfg[module_id]['<STR_LIT>']) + '<STR_LIT:U+0020>' + str(<EOL>cfg[module_id]['<STR_LIT>']) + '<STR_LIT:U+0020>' + module_id + '<STR_LIT:\n>'<EOL><DEDENT>return module_string<EOL>
Returns a string table representing the modules in the ShutIt module map.
f10347:c2:m85
def load_shutit_modules(self):
shutit_global.shutit_global_object.yield_to_draw()<EOL>if self.loglevel <= logging.DEBUG:<EOL><INDENT>self.log('<STR_LIT>',level=logging.DEBUG)<EOL>self.log(self.host['<STR_LIT>'],level=logging.DEBUG)<EOL><DEDENT>for shutit_module_path in self.host['<STR_LIT>']:<EOL><INDENT>self.load_all_from_path(shutit_module_path)<EOL><DEDENT>
Responsible for loading the shutit modules based on the configured module paths.
f10347:c2:m86
def get_command(self, command):
shutit_global.shutit_global_object.yield_to_draw()<EOL>if command in ('<STR_LIT>','<STR_LIT>','<STR_LIT>'):<EOL><INDENT>if self.get_current_shutit_pexpect_session_environment().distro == '<STR_LIT>':<EOL><INDENT>return '<STR_LIT:g>' + command<EOL><DEDENT><DEDENT>return command<EOL>
Helper function for osx - return gnu utils rather than default for eg head and md5sum where possible and needed.
f10347:c2:m87
def get_send_command(self, send):
shutit_global.shutit_global_object.yield_to_draw()<EOL>if send is None:<EOL><INDENT>return send<EOL><DEDENT>cmd_arr = send.split()<EOL>if cmd_arr and cmd_arr[<NUM_LIT:0>] in ('<STR_LIT>','<STR_LIT>','<STR_LIT>'):<EOL><INDENT>newcmd = self.get_command(cmd_arr[<NUM_LIT:0>])<EOL>send = send.replace(cmd_arr[<NUM_LIT:0>],newcmd)<EOL><DEDENT>return send<EOL>
Internal helper function to get command that's really sent
f10347:c2:m88
def load_configs(self):
shutit_global.shutit_global_object.yield_to_draw()<EOL>configs = [('<STR_LIT>', StringIO(default_cnf)), os.path.expanduser('<STR_LIT>'), os.path.join(self.host['<STR_LIT>'], '<STR_LIT>'), '<STR_LIT>']<EOL>for config_file_name in self.build['<STR_LIT>']:<EOL><INDENT>run_config_file = os.path.expanduser(config_file_name)<EOL>if not os.path.isfile(run_config_file):<EOL><INDENT>shutit_global.shutit_global_object.shutit_print('<STR_LIT>' + run_config_file + '<STR_LIT>' + run_config_file + '<STR_LIT:?>')<EOL>shutit_global.shutit_global_object.handle_exit(exit_code=<NUM_LIT:0>)<EOL><DEDENT>configs.append(run_config_file)<EOL><DEDENT>if self.action['<STR_LIT>'] or self.loglevel <= logging.DEBUG:<EOL><INDENT>msg = '<STR_LIT>'<EOL>for c in configs:<EOL><INDENT>if isinstance(c, tuple):<EOL><INDENT>c = c[<NUM_LIT:0>]<EOL><DEDENT>msg = msg + '<STR_LIT>' + c<EOL>self.log('<STR_LIT:U+0020>' + c,level=logging.DEBUG)<EOL><DEDENT><DEDENT>if self.build['<STR_LIT>']:<EOL><INDENT>override_cp = ConfigParser.RawConfigParser()<EOL>for o_sec, o_key, o_val in self.build['<STR_LIT>']:<EOL><INDENT>if not override_cp.has_section(o_sec):<EOL><INDENT>override_cp.add_section(o_sec)<EOL><DEDENT>override_cp.set(o_sec, o_key, o_val)<EOL><DEDENT>override_fd = StringIO()<EOL>override_cp.write(override_fd)<EOL>override_fd.seek(<NUM_LIT:0>)<EOL>configs.append(('<STR_LIT>', override_fd))<EOL><DEDENT>self.config_parser = self.get_configs(configs)<EOL>self.get_base_config()<EOL>
Responsible for loading config files into ShutIt. Recurses down from configured shutit module paths.
f10347:c2:m89
def get_base_config(self):
shutit_global.shutit_global_object.yield_to_draw()<EOL>cp = self.config_parser<EOL>self.build['<STR_LIT>'] = cp.getboolean('<STR_LIT>', '<STR_LIT>')<EOL>self.build['<STR_LIT>'] = cp.get('<STR_LIT>', '<STR_LIT>')<EOL>self.build['<STR_LIT>'] = cp.get('<STR_LIT>', '<STR_LIT>')<EOL>self.build['<STR_LIT>'] = cp.get('<STR_LIT>', '<STR_LIT>')<EOL>if self.build['<STR_LIT>'] is None:<EOL><INDENT>self.build['<STR_LIT>'] = cp.get('<STR_LIT>', '<STR_LIT>')<EOL><DEDENT>self.target['<STR_LIT>'] = cp.get('<STR_LIT:target>', '<STR_LIT>')<EOL>self.target['<STR_LIT>'] = cp.get('<STR_LIT:target>', '<STR_LIT>')<EOL>self.target['<STR_LIT>'] = cp.get('<STR_LIT:target>', '<STR_LIT>')<EOL>self.target['<STR_LIT>'] = cp.get('<STR_LIT:target>', '<STR_LIT>')<EOL>self.target['<STR_LIT:name>'] = cp.get('<STR_LIT:target>', '<STR_LIT:name>')<EOL>self.target['<STR_LIT>'] = cp.getboolean('<STR_LIT:target>', '<STR_LIT>')<EOL>self.host['<STR_LIT>'] = cp.getboolean('<STR_LIT:host>', '<STR_LIT>')<EOL>self.host['<STR_LIT>'] = cp.get('<STR_LIT:host>', '<STR_LIT>')<EOL>self.host['<STR_LIT>'] = cp.get('<STR_LIT:host>', '<STR_LIT>')<EOL>self.host['<STR_LIT:password>'] = cp.get('<STR_LIT:host>', '<STR_LIT:password>')<EOL>if isinstance(self.host['<STR_LIT:password>'],str):<EOL><INDENT>shutit_global.shutit_global_object.secret_words_set.add(self.host['<STR_LIT:password>'])<EOL><DEDENT>self.logfile = cp.get('<STR_LIT:host>', '<STR_LIT>')<EOL>self.host['<STR_LIT>'] = cp.get('<STR_LIT:host>', '<STR_LIT>').split('<STR_LIT::>')<EOL>self.repository['<STR_LIT:name>'] = cp.get('<STR_LIT>', '<STR_LIT:name>')<EOL>self.repository['<STR_LIT>'] = cp.get('<STR_LIT>', '<STR_LIT>')<EOL>self.repository['<STR_LIT>'] = cp.getboolean('<STR_LIT>', '<STR_LIT>')<EOL>self.repository['<STR_LIT>'] = cp.getboolean('<STR_LIT>', '<STR_LIT>')<EOL>self.repository['<STR_LIT>'] = cp.getboolean('<STR_LIT>', '<STR_LIT>')<EOL>self.repository['<STR_LIT>'] = cp.getboolean('<STR_LIT>', '<STR_LIT>')<EOL>self.repository['<STR_LIT>'] = cp.getboolean('<STR_LIT>', '<STR_LIT>')<EOL>self.repository['<STR_LIT>'] = cp.get('<STR_LIT>', '<STR_LIT>')<EOL>self.repository['<STR_LIT:user>'] = cp.get('<STR_LIT>', '<STR_LIT:user>')<EOL>self.repository['<STR_LIT:password>'] = cp.get('<STR_LIT>', '<STR_LIT:password>')<EOL>if isinstance(self.repository['<STR_LIT:password>'],str):<EOL><INDENT>shutit_global.shutit_global_object.secret_words_set.add(self.repository['<STR_LIT:password>'])<EOL><DEDENT>self.repository['<STR_LIT:email>'] = cp.get('<STR_LIT>', '<STR_LIT:email>')<EOL>self.repository['<STR_LIT>'] = cp.get('<STR_LIT>', '<STR_LIT>')<EOL>if self.target['<STR_LIT>'] == '<STR_LIT>':<EOL><INDENT>self.target['<STR_LIT>'] = self.build['<STR_LIT>']<EOL><DEDENT>if self.target['<STR_LIT>'] and (self.repository['<STR_LIT>'] or self.repository['<STR_LIT>'] or self.repository['<STR_LIT>'] or self.repository['<STR_LIT>']): <EOL><INDENT>shutit_global.shutit_global_object.shutit_print("<STR_LIT>")<EOL>shutit_global.shutit_global_object.handle_exit(exit_code=<NUM_LIT:1>)<EOL><DEDENT>if self.target['<STR_LIT>'] != '<STR_LIT>' and self.build['<STR_LIT>'] != '<STR_LIT>' and self.build['<STR_LIT>'] != '<STR_LIT>': <EOL><INDENT>shutit_global.shutit_global_object.shutit_print('<STR_LIT>')<EOL>shutit_global.shutit_global_object.handle_exit(exit_code=<NUM_LIT:1>)<EOL><DEDENT>
Responsible for getting core configuration from config files.
f10347:c2:m90
def load_all_from_path(self, path):
shutit_global.shutit_global_object.yield_to_draw()<EOL>path = os.path.abspath(path)<EOL>if os.path.abspath(path) == self.shutit_main_dir:<EOL><INDENT>return<EOL><DEDENT>if not os.path.exists(path):<EOL><INDENT>return<EOL><DEDENT>if os.path.exists(path + '<STR_LIT>') and not self.build['<STR_LIT>']:<EOL><INDENT>self.log('<STR_LIT>' + path + '<STR_LIT>',level=logging.DEBUG)<EOL>return<EOL><DEDENT>for sub in glob.glob(os.path.join(path, '<STR_LIT:*>')):<EOL><INDENT>subpath = os.path.join(path, sub)<EOL>if os.path.isfile(subpath):<EOL><INDENT>self.load_mod_from_file(subpath)<EOL><DEDENT>elif os.path.isdir(subpath):<EOL><INDENT>self.load_all_from_path(subpath)<EOL><DEDENT><DEDENT>
Dynamically imports files within the same directory (in the end, the path).
f10347:c2:m91
def load_mod_from_file(self, fpath):
shutit_global.shutit_global_object.yield_to_draw()<EOL>fpath = os.path.abspath(fpath)<EOL>file_ext = os.path.splitext(os.path.split(fpath)[-<NUM_LIT:1>])[-<NUM_LIT:1>]<EOL>if file_ext.lower() != '<STR_LIT>':<EOL><INDENT>return<EOL><DEDENT>with open(fpath) as f:<EOL><INDENT>content = f.read().splitlines()<EOL><DEDENT>ok = False<EOL>for line in content:<EOL><INDENT>if line.strip() == '<STR_LIT>':<EOL><INDENT>ok = True<EOL>break<EOL><DEDENT><DEDENT>if not ok:<EOL><INDENT>self.log('<STR_LIT>' + fpath,level=logging.DEBUG)<EOL>return<EOL><DEDENT>existingmodules = [<EOL>m for m in self.shutit_modules<EOL>if getattr(m, '<STR_LIT>', None) == fpath<EOL>]<EOL>if existingmodules:<EOL><INDENT>self.log('<STR_LIT>' + fpath,level=logging.DEBUG)<EOL>return<EOL><DEDENT>self.log('<STR_LIT>' + fpath,level=logging.DEBUG)<EOL>directory = os.path.dirname(fpath)<EOL>if directory not in sys.path:<EOL><INDENT>sys.path.append(os.path.dirname(fpath))<EOL><DEDENT>mod_name = base64.b32encode(fpath.encode()).decode().replace('<STR_LIT:=>', '<STR_LIT>')<EOL>pymod = imp.load_source(mod_name, fpath)<EOL>targets = [<EOL>('<STR_LIT>', self.shutit_modules), ('<STR_LIT>', self.conn_modules)<EOL>]<EOL>self.build['<STR_LIT:source>'] = {}<EOL>for attr, target in targets:<EOL><INDENT>modulefunc = getattr(pymod, attr, None)<EOL>if not callable(modulefunc):<EOL><INDENT>return<EOL><DEDENT>modules = modulefunc()<EOL>if not isinstance(modules, list):<EOL><INDENT>modules = [modules]<EOL><DEDENT>for module in modules:<EOL><INDENT>setattr(module, '<STR_LIT>', fpath)<EOL>ShutItModule.register(module.__class__)<EOL>target.add(module)<EOL>self.build['<STR_LIT:source>'][fpath] = open(fpath).read()<EOL><DEDENT><DEDENT>
Loads modules from a .py file into ShutIt if there are no modules from this file already. We expect to have a callable 'module/0' which returns one or more module objects. If this doesn't exist we assume that the .py file works in the old style (automatically inserting the module into shutit_global) or it's not a shutit module.
f10347:c2:m92
def config_collection_for_built(self, throw_error=True,silent=False):
shutit_global.shutit_global_object.yield_to_draw()<EOL>self.log('<STR_LIT>',level=logging.DEBUG)<EOL>cfg = self.cfg<EOL>for module_id in self.module_ids():<EOL><INDENT>if (self.is_to_be_built_or_is_installed(self.shutit_map[module_id]) and<EOL>not self.shutit_map[module_id].get_config(self)):<EOL><INDENT>self.fail(module_id + '<STR_LIT>') <EOL><DEDENT>if cfg[module_id]['<STR_LIT>'] and not self.build['<STR_LIT>']:<EOL><INDENT>cfg_file = os.path.dirname(self.shutit_file_map[module_id]) + '<STR_LIT>'<EOL>if os.path.isfile(cfg_file):<EOL><INDENT>self.build['<STR_LIT>'] = True<EOL>config_parser = ConfigParser.ConfigParser()<EOL>config_parser.read(cfg_file)<EOL>for section in config_parser.sections():<EOL><INDENT>if section == module_id:<EOL><INDENT>for option in config_parser.options(section):<EOL><INDENT>override = False<EOL>for mod, opt, val in self.build['<STR_LIT>']:<EOL><INDENT>val = val <EOL>if mod == module_id and opt == option:<EOL><INDENT>override = True<EOL><DEDENT><DEDENT>if override:<EOL><INDENT>continue<EOL><DEDENT>is_bool = isinstance(cfg[module_id][option], bool)<EOL>if is_bool:<EOL><INDENT>value = config_parser.getboolean(section,option)<EOL><DEDENT>else:<EOL><INDENT>value = config_parser.get(section,option)<EOL><DEDENT>if option == '<STR_LIT>':<EOL><INDENT>value = json.loads(value)<EOL><DEDENT>self.get_config(module_id, option, value, forceask=True)<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>passed = True<EOL>for module_id in self.module_ids():<EOL><INDENT>if (cfg[module_id]['<STR_LIT>'] and<EOL>(cfg[module_id]['<STR_LIT>'] and<EOL>self.target['<STR_LIT>'] not in cfg[module_id]['<STR_LIT>'])):<EOL><INDENT>if not self.allowed_image(module_id):<EOL><INDENT>passed = False<EOL>if not silent:<EOL><INDENT>shutit_global.shutit_global_object.shutit_print('<STR_LIT>' + module_id + '<STR_LIT>' + str(cfg[module_id]['<STR_LIT>']) + '<STR_LIT>' + self.target['<STR_LIT>'] + '<STR_LIT>')<EOL><DEDENT><DEDENT><DEDENT><DEDENT>if not passed:<EOL><INDENT>if not throw_error:<EOL><INDENT>return False<EOL><DEDENT>if self.build['<STR_LIT>']:<EOL><INDENT>shutit_global.shutit_global_object.shutit_print('<STR_LIT>')<EOL>shutit_global.shutit_global_object.handle_exit(exit_code=<NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>raise ShutItFailException('<STR_LIT>') <EOL><DEDENT><DEDENT>return True<EOL>
Collect configuration for modules that are being built. When this is called we should know what's being built (ie after dependency resolution).
f10347:c2:m93
def config_collection(self):
shutit_global.shutit_global_object.yield_to_draw()<EOL>self.log('<STR_LIT>',level=logging.DEBUG)<EOL>cfg = self.cfg<EOL>for module_id in self.module_ids():<EOL><INDENT>self.get_config(module_id, '<STR_LIT>', None, boolean=True, forcenone=True)<EOL>self.get_config(module_id, '<STR_LIT>', False, boolean=True)<EOL>self.get_config(module_id, '<STR_LIT>', False, boolean=True)<EOL>self.get_config(module_id, '<STR_LIT>', ["<STR_LIT>"])<EOL>module = self.shutit_map[module_id]<EOL>cfg_file = os.path.dirname(get_module_file(self,module)) + '<STR_LIT>'<EOL>if os.path.isfile(cfg_file):<EOL><INDENT>config_parser = ConfigParser.ConfigParser()<EOL>config_parser.read(cfg_file)<EOL>for section in config_parser.sections():<EOL><INDENT>if section == module_id:<EOL><INDENT>for option in config_parser.options(section):<EOL><INDENT>if option == '<STR_LIT>':<EOL><INDENT>override = False<EOL>for mod, opt, val in self.build['<STR_LIT>']:<EOL><INDENT>val = val <EOL>if mod == module_id and opt == option:<EOL><INDENT>override = True<EOL><DEDENT><DEDENT>if override:<EOL><INDENT>continue<EOL><DEDENT>value = config_parser.get(section,option)<EOL>if option == '<STR_LIT>':<EOL><INDENT>value = json.loads(value)<EOL><DEDENT>self.get_config(module_id, option, value, forceask=True)<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>if cfg[module_id]['<STR_LIT>'] is None:<EOL><INDENT>self.get_config(module_id, '<STR_LIT>', True, boolean=True)<EOL>cfg[module_id]['<STR_LIT>'] = False<EOL><DEDENT>else:<EOL><INDENT>self.get_config(module_id, '<STR_LIT>', False, boolean=True)<EOL><DEDENT><DEDENT>
Collect core config from config files for all seen modules.
f10347:c2:m94
def do_list_modules(self, long_output=None,sort_order=None):
shutit_global.shutit_global_object.yield_to_draw()<EOL>cfg = self.cfg<EOL>table_list = []<EOL>if long_output is None:<EOL><INDENT>long_output = self.list_modules['<STR_LIT>']<EOL><DEDENT>if sort_order is None:<EOL><INDENT>sort_order = self.list_modules['<STR_LIT>']<EOL><DEDENT>if long_output:<EOL><INDENT>table_list.append(["<STR_LIT>","<STR_LIT>","<STR_LIT>","<STR_LIT>","<STR_LIT>","<STR_LIT>"])<EOL><DEDENT>else:<EOL><INDENT>table_list.append(["<STR_LIT>","<STR_LIT>","<STR_LIT>","<STR_LIT>"])<EOL><DEDENT>if sort_order == '<STR_LIT>':<EOL><INDENT>d = {}<EOL>for m in self.shutit_modules:<EOL><INDENT>d.update({m.module_id:m.run_order})<EOL><DEDENT>b = sorted(d.items(), key=operator.itemgetter(<NUM_LIT:1>))<EOL>count = <NUM_LIT:0><EOL>for pair in b:<EOL><INDENT>k = pair[<NUM_LIT:0>]<EOL>for m in self.shutit_modules:<EOL><INDENT>if m.module_id == k:<EOL><INDENT>count += <NUM_LIT:1><EOL>compatible = True<EOL>if not cfg[m.module_id]['<STR_LIT>']:<EOL><INDENT>cfg[m.module_id]['<STR_LIT>'] = True<EOL>compatible = self.determine_compatibility(m.module_id) == <NUM_LIT:0><EOL>cfg[m.module_id]['<STR_LIT>'] = False<EOL><DEDENT>if long_output:<EOL><INDENT>table_list.append([str(count),m.module_id,m.description,str(m.run_order),str(cfg[m.module_id]['<STR_LIT>']),str(compatible)])<EOL><DEDENT>else:<EOL><INDENT>table_list.append([m.module_id,m.description,str(cfg[m.module_id]['<STR_LIT>']),str(compatible)])<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>elif sort_order == '<STR_LIT:id>':<EOL><INDENT>l = []<EOL>for m in self.shutit_modules:<EOL><INDENT>l.append(m.module_id)<EOL><DEDENT>l.sort()<EOL>for k in l:<EOL><INDENT>for m in self.shutit_modules:<EOL><INDENT>if m.module_id == k:<EOL><INDENT>count = <NUM_LIT:1><EOL>compatible = True<EOL>if not cfg[m.module_id]['<STR_LIT>']:<EOL><INDENT>cfg[m.module_id]['<STR_LIT>'] = True<EOL>compatible = self.determine_compatibility(m.module_id) == <NUM_LIT:0><EOL><DEDENT>if long_output:<EOL><INDENT>table_list.append([str(count),m.module_id,m.description,str(m.run_order),str(cfg[m.module_id]['<STR_LIT>']),str(compatible)])<EOL><DEDENT>else:<EOL><INDENT>table_list.append([m.module_id,m.description,str(cfg[m.module_id]['<STR_LIT>']),str(compatible)])<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>table = texttable.Texttable()<EOL>table.add_rows(table_list)<EOL>colwidths = []<EOL>for item in table_list:<EOL><INDENT>for n in range(<NUM_LIT:0>,len(item)):<EOL><INDENT>colwidths.append(<NUM_LIT:10>)<EOL><DEDENT>break<EOL><DEDENT>for item in table_list:<EOL><INDENT>for n in range(<NUM_LIT:0>,len(item)-<NUM_LIT:1>):<EOL><INDENT>if len(str(item[n])) > colwidths[n]:<EOL><INDENT>colwidths[n] = len(str(item[n]))<EOL><DEDENT><DEDENT><DEDENT>table.set_cols_width(colwidths)<EOL>msg = table.draw()<EOL>shutit_global.shutit_global_object.shutit_print('<STR_LIT:\n>' + msg)<EOL>
Display a list of loaded modules. Config items: - shutit.list_modules['long'] If set, also print each module's run order value - shutit.list_modules['sort'] Select the column by which the list is ordered: - id: sort the list by module id - run_order: sort the list by module run order The output is also saved to ['build']['log_config_path']/module_order.txt Dependencies: operator
f10347:c2:m95
def print_config(self, cfg, hide_password=True, history=False, module_id=None):
shutit_global.shutit_global_object.yield_to_draw()<EOL>cp = self.config_parser<EOL>s = '<STR_LIT>'<EOL>keys1 = list(cfg.keys())<EOL>if keys1:<EOL><INDENT>keys1.sort()<EOL><DEDENT>for k in keys1:<EOL><INDENT>if module_id is not None and k != module_id:<EOL><INDENT>continue<EOL><DEDENT>if isinstance(k, str) and isinstance(cfg[k], dict):<EOL><INDENT>s += '<STR_LIT>' + k + '<STR_LIT>'<EOL>keys2 = list(cfg[k].keys())<EOL>if keys2:<EOL><INDENT>keys2.sort()<EOL><DEDENT>for k1 in keys2:<EOL><INDENT>line = '<STR_LIT>'<EOL>line += k1 + '<STR_LIT::>'<EOL>if hide_password and (k1 == '<STR_LIT:password>' or k1 == '<STR_LIT>'):<EOL><INDENT>p = hashlib.sha512(cfg[k][k1]).hexdigest()<EOL>i = <NUM_LIT><EOL>while i > <NUM_LIT:0>:<EOL><INDENT>i -= <NUM_LIT:1><EOL>p = hashlib.sha512(s).hexdigest()<EOL><DEDENT>line += p<EOL><DEDENT>else:<EOL><INDENT>if type(cfg[k][k1] == bool):<EOL><INDENT>line += str(cfg[k][k1])<EOL><DEDENT>elif type(cfg[k][k1] == str):<EOL><INDENT>line += cfg[k][k1]<EOL><DEDENT><DEDENT>if history:<EOL><INDENT>try:<EOL><INDENT>line += (<NUM_LIT:30>-len(line)) * '<STR_LIT:U+0020>' + '<STR_LIT>' + cp.whereset(k, k1)<EOL><DEDENT>except Exception:<EOL><INDENT>line += (<NUM_LIT:30>-len(line)) * '<STR_LIT:U+0020>' + '<STR_LIT>' + "<STR_LIT>"<EOL><DEDENT><DEDENT>s += line + '<STR_LIT:\n>'<EOL><DEDENT><DEDENT><DEDENT>return s<EOL>
Returns a string representing the config of this ShutIt run.
f10347:c2:m96
def process_args(self, args):
shutit_global.shutit_global_object.yield_to_draw()<EOL>assert isinstance(args,ShutItInit), shutit_util.print_debug()<EOL>if args.action == '<STR_LIT:version>':<EOL><INDENT>shutit_global.shutit_global_object.shutit_print('<STR_LIT>' + shutit.shutit_version)<EOL>shutit_global.shutit_global_object.handle_exit(exit_code=<NUM_LIT:0>)<EOL><DEDENT>self.action['<STR_LIT>'] = args.action == '<STR_LIT>'<EOL>self.action['<STR_LIT>'] = args.action == '<STR_LIT>'<EOL>self.action['<STR_LIT>'] = args.action == '<STR_LIT>'<EOL>self.action['<STR_LIT>'] = args.action == '<STR_LIT>'<EOL>self.action['<STR_LIT>'] = args.action == '<STR_LIT>'<EOL>self.action['<STR_LIT>'] = args.action == '<STR_LIT>'<EOL>if not self.logging_setup_done:<EOL><INDENT>self.logfile = args.logfile<EOL>self.loglevel = args.loglevel<EOL>if self.loglevel is None or self.loglevel == '<STR_LIT>':<EOL><INDENT>self.loglevel = '<STR_LIT>'<EOL><DEDENT>self.setup_logging()<EOL><DEDENT>shutit_global.shutit_global_object.setup_panes(action=args.action)<EOL>if self.action['<STR_LIT>']:<EOL><INDENT>self.handle_skeleton(args)<EOL>shutit_global.shutit_global_object.handle_exit()<EOL><DEDENT>elif self.action['<STR_LIT>']:<EOL><INDENT>self.handle_run(args)<EOL>sys.exit(<NUM_LIT:0>)<EOL><DEDENT>elif self.action['<STR_LIT>'] or self.action['<STR_LIT>'] or self.action['<STR_LIT>']:<EOL><INDENT>self.handle_build(args)<EOL><DEDENT>else:<EOL><INDENT>self.fail('<STR_LIT>' + str(self.action))<EOL><DEDENT>self.nocolor = args.nocolor<EOL>
Process the args we have. 'args' is always a ShutItInit object.
f10347:c2:m97
def get_configs(self, configs):
shutit_global.shutit_global_object.yield_to_draw()<EOL>cp = LayerConfigParser()<EOL>fail_str = '<STR_LIT>'<EOL>files = []<EOL>for config_file in configs:<EOL><INDENT>if isinstance(config_file, tuple):<EOL><INDENT>continue<EOL><DEDENT>if not shutit_util.is_file_secure(config_file):<EOL><INDENT>fail_str = fail_str + '<STR_LIT>' + config_file<EOL>files.append(config_file)<EOL><DEDENT><DEDENT>if fail_str != '<STR_LIT>':<EOL><INDENT>if shutit_global.shutit_global_object.interactive > <NUM_LIT:1>:<EOL><INDENT>fail_str = '<STR_LIT>' + fail_str + '<STR_LIT:\n>'<EOL>self.log(fail_str,level=logging.INFO)<EOL>self.log('<STR_LIT>',level=logging.INFO)<EOL>if shutit_global.shutit_global_object.interactive == <NUM_LIT:0> or shutit_util.util_raw_input(default='<STR_LIT:y>') == '<STR_LIT:y>':<EOL><INDENT>for f in files:<EOL><INDENT>self.log('<STR_LIT>' + f,level=logging.INFO)<EOL>os.chmod(f,<NUM_LIT>)<EOL><DEDENT>return self.get_configs(configs)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>for f in files:<EOL><INDENT>self.log('<STR_LIT>' + f,level=logging.INFO)<EOL>os.chmod(f,<NUM_LIT>)<EOL><DEDENT>return self.get_configs(configs)<EOL><DEDENT>self.fail(fail_str) <EOL><DEDENT>for config in configs:<EOL><INDENT>if isinstance(config, tuple):<EOL><INDENT>cp.readfp(config[<NUM_LIT:1>], filename=config[<NUM_LIT:0>])<EOL><DEDENT>else:<EOL><INDENT>cp.read(config)<EOL><DEDENT><DEDENT>self.build['<STR_LIT>'] = cp.get_config_set('<STR_LIT>', '<STR_LIT>')<EOL>return cp<EOL>
Reads config files in, checking their security first (in case passwords/sensitive info is in them).
f10347:c2:m101
def parse_args(self):
shutit_global.shutit_global_object.yield_to_draw()<EOL>actions = ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT:version>']<EOL>if len(sys.argv) == <NUM_LIT:1> or (len(sys.argv) > <NUM_LIT:1> and sys.argv[<NUM_LIT:1>] not in actions<EOL>and '<STR_LIT>' not in sys.argv and '<STR_LIT>' not in sys.argv):<EOL><INDENT>sys.argv.insert(<NUM_LIT:1>, '<STR_LIT>')<EOL><DEDENT>parser = argparse.ArgumentParser(description='<STR_LIT>',prog="<STR_LIT>")<EOL>subparsers = parser.add_subparsers(dest='<STR_LIT:action>', help='''<STR_LIT>''')<EOL>sub_parsers = dict()<EOL>for action in actions:<EOL><INDENT>sub_parsers[action] = subparsers.add_parser(action)<EOL><DEDENT>args_list = sys.argv[<NUM_LIT:1>:]<EOL>for action in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>','<STR_LIT>','<STR_LIT>']:<EOL><INDENT>sub_parsers[action].add_argument('<STR_LIT>', help='<STR_LIT>', default='<STR_LIT>')<EOL>sub_parsers[action].add_argument('<STR_LIT>', help='<STR_LIT>', default="<STR_LIT>")<EOL>sub_parsers[action].add_argument('<STR_LIT>','<STR_LIT>',default='<STR_LIT>', help='<STR_LIT>')<EOL>sub_parsers[action].add_argument('<STR_LIT>','<STR_LIT>',default='<STR_LIT>', help='<STR_LIT>',choices=('<STR_LIT>','<STR_LIT>','<STR_LIT>','<STR_LIT>','<STR_LIT>','<STR_LIT>','<STR_LIT>','<STR_LIT:info>','<STR_LIT>','<STR_LIT:error>','<STR_LIT>'))<EOL>sub_parsers[action].add_argument('<STR_LIT>', help='<STR_LIT>', default=False, action='<STR_LIT>', const=True)<EOL>sub_parsers[action].add_argument('<STR_LIT>','<STR_LIT>', help='<STR_LIT>', default=None, choices=('<STR_LIT>','<STR_LIT>','<STR_LIT>'))<EOL>sub_parsers[action].add_argument('<STR_LIT>', help='<STR_LIT>', const=True, default=False, action='<STR_LIT>')<EOL><DEDENT>for action in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>sub_parsers[action].add_argument('<STR_LIT>', help='<STR_LIT>', default=[], action='<STR_LIT>')<EOL>sub_parsers[action].add_argument('<STR_LIT>', '<STR_LIT>', help='<STR_LIT>', default=[], action='<STR_LIT>', nargs=<NUM_LIT:3>, metavar=('<STR_LIT>', '<STR_LIT>', '<STR_LIT>'))<EOL>sub_parsers[action].add_argument('<STR_LIT>', help='<STR_LIT>', default='<STR_LIT>')<EOL>sub_parsers[action].add_argument('<STR_LIT>', help='''<STR_LIT>''', default=False, const=True, action='<STR_LIT>')<EOL>sub_parsers[action].add_argument('<STR_LIT>', '<STR_LIT>', default=None, help='<STR_LIT>')<EOL>sub_parsers[action].add_argument('<STR_LIT>', help='<STR_LIT>', const=True, default=False, action='<STR_LIT>')<EOL>sub_parsers[action].add_argument('<STR_LIT>', help='<STR_LIT>', default='<STR_LIT:1>')<EOL>sub_parsers[action].add_argument('<STR_LIT>', help='<STR_LIT>', const=True, default=False, action='<STR_LIT>')<EOL>sub_parsers[action].add_argument('<STR_LIT>', help='<STR_LIT>', const=True, default=None, action='<STR_LIT>')<EOL>sub_parsers[action].add_argument('<STR_LIT>', help='<STR_LIT>', const=True, default=False, action='<STR_LIT>')<EOL>sub_parsers[action].add_argument('<STR_LIT>', help='<STR_LIT>', const=True, default=False, action='<STR_LIT>')<EOL><DEDENT>sub_parsers['<STR_LIT>'].add_argument('<STR_LIT>', nargs='<STR_LIT:*>', default=['<STR_LIT>','<STR_LIT>','<STR_LIT>','<STR_LIT>','<STR_LIT>'])<EOL>sub_parsers['<STR_LIT>'].add_argument('<STR_LIT>', help='<STR_LIT>',default='<STR_LIT>')<EOL>sub_parsers['<STR_LIT>'].add_argument('<STR_LIT>', help='<STR_LIT>',default='<STR_LIT>')<EOL>sub_parsers['<STR_LIT>'].add_argument('<STR_LIT>', help='<STR_LIT>', default='<STR_LIT>')<EOL>sub_parsers['<STR_LIT>'].add_argument('<STR_LIT>', help='<STR_LIT>', default='<STR_LIT>')<EOL>sub_parsers['<STR_LIT>'].add_argument('<STR_LIT>', help='<STR_LIT>', nargs='<STR_LIT:?>', default=None)<EOL>sub_parsers['<STR_LIT>'].add_argument('<STR_LIT>', help='<STR_LIT>', default=False, const=True, action='<STR_LIT>')<EOL>sub_parsers['<STR_LIT>'].add_argument('<STR_LIT>', nargs='<STR_LIT:+>', default=None)<EOL>sub_parsers['<STR_LIT>'].add_argument('<STR_LIT>', default=None)<EOL>sub_parsers['<STR_LIT>'].add_argument('<STR_LIT>', default=False, const=True, action='<STR_LIT>')<EOL>sub_parsers['<STR_LIT>'].add_argument('<STR_LIT>', default=None)<EOL>sub_parsers['<STR_LIT>'].add_argument('<STR_LIT>', default=None, const=True, action='<STR_LIT>')<EOL>sub_parsers['<STR_LIT>'].add_argument('<STR_LIT>', default=None, const=True, action='<STR_LIT>')<EOL>sub_parsers['<STR_LIT>'].add_argument('<STR_LIT>', default=None, const=True, action='<STR_LIT>')<EOL>sub_parsers['<STR_LIT>'].add_argument('<STR_LIT>', default=None, const=True, action='<STR_LIT>')<EOL>sub_parsers['<STR_LIT>'].add_argument('<STR_LIT>', help='<STR_LIT>', default='<STR_LIT>')<EOL>sub_parsers['<STR_LIT>'].add_argument('<STR_LIT>','<STR_LIT>', help='<STR_LIT>', const=True, default=False, action='<STR_LIT>')<EOL>sub_parsers['<STR_LIT>'].add_argument('<STR_LIT>', help='<STR_LIT>', const=True, default=False, action='<STR_LIT>')<EOL>sub_parsers['<STR_LIT>'].add_argument('<STR_LIT>', help='<STR_LIT>', const=True, default=False, action='<STR_LIT>')<EOL>sub_parsers['<STR_LIT>'].add_argument('<STR_LIT>', help='<STR_LIT>', const=True, default=False, action='<STR_LIT>')<EOL>sub_parsers['<STR_LIT>'].add_argument('<STR_LIT>', help='<STR_LIT>', default='<STR_LIT>', choices=('<STR_LIT>','<STR_LIT>','<STR_LIT>','<STR_LIT>','<STR_LIT>','<STR_LIT>','<STR_LIT>','<STR_LIT>'))<EOL>sub_parsers['<STR_LIT>'].add_argument('<STR_LIT>', help='<STR_LIT>', default=False, action='<STR_LIT>', const=True)<EOL>sub_parsers['<STR_LIT>'].add_argument('<STR_LIT>','<STR_LIT>', help='<STR_LIT>', default=False, action='<STR_LIT>', const=True)<EOL>sub_parsers['<STR_LIT>'].add_argument('<STR_LIT:-c>','<STR_LIT>', help='<STR_LIT>', default=False, action='<STR_LIT>', const=True)<EOL>sub_parsers['<STR_LIT>'].add_argument('<STR_LIT>', help='<STR_LIT>', nargs=<NUM_LIT:1>, default=-<NUM_LIT:1>)<EOL>sub_parsers['<STR_LIT>'].add_argument('<STR_LIT>', help='<STR_LIT>', default=False, action='<STR_LIT>', const=True)<EOL>sub_parsers['<STR_LIT>'].add_argument('<STR_LIT>', help='<STR_LIT>', default=False, action='<STR_LIT>', const=True)<EOL>sub_parsers['<STR_LIT>'].add_argument('<STR_LIT>', help='<STR_LIT>', const=True, default=False, action='<STR_LIT>')<EOL>sub_parsers['<STR_LIT>'].add_argument('<STR_LIT>', help='<STR_LIT>', const=True, default=False, action='<STR_LIT>')<EOL>sub_parsers['<STR_LIT>'].add_argument('<STR_LIT>', help='<STR_LIT>', const=True, default=False, action='<STR_LIT>')<EOL>sub_parsers['<STR_LIT>'].add_argument('<STR_LIT>', help='<STR_LIT>', default='<STR_LIT:id>', choices=('<STR_LIT:id>','<STR_LIT>'))<EOL>if os.environ.get('<STR_LIT>', None) and args_list[<NUM_LIT:0>] != '<STR_LIT>':<EOL><INDENT>env_args = os.environ['<STR_LIT>'].strip()<EOL>env_args_split = re.split(r'<STR_LIT>', env_args)<EOL>env_args_split = [re.split(r'<STR_LIT>', item) for item in env_args_split]<EOL>env_args_split = [item for sublist in env_args_split for item in sublist]<EOL>env_args_split = [re.split(r'<STR_LIT>', item) for item in env_args_split]<EOL>env_args_split = [item for sublist in env_args_split for item in sublist]<EOL>env_args_split = [item for item in env_args_split if item != '<STR_LIT>']<EOL>env_args_list = ['<STR_LIT>']<EOL>for item in env_args_split:<EOL><INDENT>if item == '<STR_LIT:U+0020>':<EOL><INDENT>env_args_list.append('<STR_LIT>')<EOL><DEDENT>elif item == '<STR_LIT>':<EOL><INDENT>env_args_list[-<NUM_LIT:1>] += '<STR_LIT:U+0020>'<EOL><DEDENT>elif item == '<STR_LIT>':<EOL><INDENT>env_args_list[-<NUM_LIT:1>] += '<STR_LIT:\\>'<EOL><DEDENT>else:<EOL><INDENT>env_args_list[-<NUM_LIT:1>] += item<EOL><DEDENT><DEDENT>args_list[<NUM_LIT:1>:<NUM_LIT:1>] = env_args_list<EOL><DEDENT>args = parser.parse_args(args_list)<EOL>if args.action == '<STR_LIT:version>':<EOL><INDENT>self.process_args(ShutItInit(args.action))<EOL><DEDENT>elif args.action == '<STR_LIT>':<EOL><INDENT>shutit_global.shutit_global_object.delaybeforesend = float(args.delaybeforesend)<EOL>shutit_global.shutit_global_object.prompt_command = args.promptcommand<EOL>self.process_args(ShutItInit(args.action,<EOL>logfile=args.logfile,<EOL>loglevel=args.log,<EOL>nocolor=args.nocolor,<EOL>delivery=args.delivery,<EOL>shutitfiles=args.shutitfiles,<EOL>script=args.script,<EOL>base_image=args.base_image,<EOL>depends=args.depends,<EOL>name=args.name,<EOL>domain=args.domain,<EOL>pattern=args.pattern,<EOL>output_dir=args.output_dir,<EOL>vagrant_ssh_access=args.vagrant_ssh_access,<EOL>vagrant_num_machines=args.vagrant_num_machines,<EOL>vagrant_machine_prefix=args.vagrant_machine_prefix,<EOL>vagrant_docker=args.vagrant_docker,<EOL>vagrant_snapshot=args.vagrant_snapshot,<EOL>vagrant_upload=args.vagrant_upload,<EOL>vagrant_image_name=args.vagrant_image_name))<EOL><DEDENT>elif args.action == '<STR_LIT>':<EOL><INDENT>shutit_global.shutit_global_object.delaybeforesend = float(args.delaybeforesend)<EOL>shutit_global.shutit_global_object.prompt_command = args.promptcommand<EOL>self.process_args(ShutItInit(args.action,<EOL>logfile=args.logfile,<EOL>loglevel=args.log,<EOL>nocolor=args.nocolor,<EOL>shutitfiles=args.shutitfiles,<EOL>echo=args.echo,<EOL>delivery = args.delivery))<EOL><DEDENT>elif args.action == '<STR_LIT>':<EOL><INDENT>shutit_global.shutit_global_object.delaybeforesend = float(args.delaybeforesend)<EOL>shutit_global.shutit_global_object.prompt_command = args.promptcommand<EOL>shutit_global.shutit_global_object.managed_panes = args.pane<EOL>if shutit_global.shutit_global_object.managed_panes:<EOL><INDENT>shutit_global.shutit_global_object.logstream = StringIO()<EOL>if not shutit_global.shutit_global_object.determine_interactive():<EOL><INDENT>self.log('<STR_LIT>',level=logging.WARNING)<EOL>shutit_global.shutit_global_object.managed_panes = False<EOL><DEDENT><DEDENT>self.process_args(ShutItInit(args.action,<EOL>logfile=args.logfile,<EOL>loglevel=args.log,<EOL>nocolor=args.nocolor,<EOL>push=args.push,<EOL>export=args.export,<EOL>save=args.save,<EOL>distro=args.distro,<EOL>mount_docker=args.mount_docker,<EOL>walkthrough=args.walkthrough,<EOL>training=args.training,<EOL>choose_config=args.choose_config,<EOL>config=args.config,<EOL>set=args.set,<EOL>ignorestop=args.ignorestop,<EOL>ignoreimage=args.ignoreimage,<EOL>imageerrorok=args.imageerrorok,<EOL>tag_modules=args.tag_modules,<EOL>image_tag=args.image_tag,<EOL>video=args.video,<EOL>deps_only=args.deps_only,<EOL>echo=args.echo,<EOL>delivery=args.delivery,<EOL>interactive=args.interactive,<EOL>trace=args.trace,<EOL>shutit_module_path=args.shutit_module_path,<EOL>exam=args.exam))<EOL>if args.trace:<EOL><INDENT>def tracefunc(frame, event, arg, indent=[<NUM_LIT:0>]):<EOL><INDENT>indent = indent <EOL>arg = arg <EOL>if event == '<STR_LIT>':<EOL><INDENT>self.log('<STR_LIT>' + frame.f_code.co_name + '<STR_LIT:U+0020>' + str(frame.f_code.co_varnames),level=logging.DEBUG)<EOL><DEDENT>elif event == '<STR_LIT>':<EOL><INDENT>self.log('<STR_LIT>' + frame.f_code.co_name,level=logging.DEBUG)<EOL><DEDENT>return tracefunc<EOL><DEDENT>sys.settrace(tracefunc)<EOL><DEDENT><DEDENT>elif args.action == '<STR_LIT>':<EOL><INDENT>self.process_args(ShutItInit(args.action,<EOL>logfile=args.logfile,<EOL>nocolor=args.nocolor,<EOL>history=args.history))<EOL><DEDENT>elif args.action == '<STR_LIT>':<EOL><INDENT>self.process_args(ShutItInit(args.action,<EOL>logfile=args.logfile,<EOL>nocolor=args.nocolor,<EOL>sort=args.sort,<EOL>long_modules=args.long))<EOL><DEDENT>
r"""Responsible for parsing arguments from the command line. Environment variables: SHUTIT_OPTIONS: Loads command line options from the environment (if set). Behaves like GREP_OPTIONS: - space separated list of arguments - backslash before a space escapes the space separation - backslash before a backslash is interpreted as a single backslash - all other backslashes are treated literally eg ' a\ b c\\ \\d \\\e\' becomes '', 'a b', 'c\', '\d', '\\e\' SHUTIT_OPTIONS is ignored if we are creating a skeleton
f10347:c2:m102
def check_deps(self):
shutit_global.shutit_global_object.yield_to_draw()<EOL>cfg = self.cfg<EOL>self.log('<STR_LIT>', level=logging.DEBUG)<EOL>self.pause_point('<STR_LIT>', print_input=False, level=<NUM_LIT:3>)<EOL>to_build = [<EOL>self.shutit_map[module_id] for module_id in self.shutit_map<EOL>if module_id in cfg and cfg[module_id]['<STR_LIT>']<EOL>]<EOL>for module in to_build:<EOL><INDENT>self.resolve_dependencies(to_build, module)<EOL><DEDENT>def err_checker(errs, triples):<EOL><INDENT>"""<STR_LIT>"""<EOL>new_triples = []<EOL>for err, triple in zip(errs, triples):<EOL><INDENT>if not err:<EOL><INDENT>new_triples.append(triple)<EOL>continue<EOL><DEDENT>found_errs.append(err)<EOL><DEDENT>return new_triples<EOL><DEDENT>found_errs = []<EOL>triples = []<EOL>for depender in to_build:<EOL><INDENT>for dependee_id in depender.depends_on:<EOL><INDENT>triples.append((depender, self.shutit_map.get(dependee_id), dependee_id))<EOL><DEDENT><DEDENT>triples = err_checker([ self.check_dependee_exists(depender, dependee, dependee_id) for depender, dependee, dependee_id in triples ], triples)<EOL>triples = err_checker([ self.check_dependee_build(depender, dependee, dependee_id) for depender, dependee, dependee_id in triples ], triples)<EOL>triples = err_checker([ check_dependee_order(depender, dependee, dependee_id) for depender, dependee, dependee_id in triples ], triples)<EOL>if found_errs:<EOL><INDENT>return [(err,) for err in found_errs]<EOL><DEDENT>self.log('<STR_LIT>', level=logging.DEBUG)<EOL>for module_id in self.module_ids():<EOL><INDENT>module = self.shutit_map[module_id]<EOL>if cfg[module_id]['<STR_LIT>']:<EOL><INDENT>self.log(module_id + '<STR_LIT:U+0020>' + str(module.run_order), level=logging.DEBUG)<EOL><DEDENT><DEDENT>self.log('<STR_LIT:\n>', level=logging.DEBUG)<EOL>return []<EOL>
Dependency checking phase is performed in this method.
f10347:c2:m112
def check_conflicts(self):
shutit_global.shutit_global_object.yield_to_draw()<EOL>cfg = self.cfg<EOL>self.log('<STR_LIT>', level=logging.DEBUG)<EOL>errs = []<EOL>self.pause_point('<STR_LIT>', print_input=False, level=<NUM_LIT:3>)<EOL>for module_id in self.module_ids():<EOL><INDENT>if not cfg[module_id]['<STR_LIT>']:<EOL><INDENT>continue<EOL><DEDENT>conflicter = self.shutit_map[module_id]<EOL>for conflictee in conflicter.conflicts_with:<EOL><INDENT>conflictee_obj = self.shutit_map.get(conflictee)<EOL>if conflictee_obj is None:<EOL><INDENT>continue<EOL><DEDENT>if ((cfg[conflicter.module_id]['<STR_LIT>'] or<EOL>self.is_to_be_built_or_is_installed(conflicter)) and<EOL>(cfg[conflictee_obj.module_id]['<STR_LIT>'] or<EOL>self.is_to_be_built_or_is_installed(conflictee_obj))):<EOL><INDENT>errs.append(('<STR_LIT>' + conflicter.module_id + '<STR_LIT>' + conflictee_obj.module_id,))<EOL><DEDENT><DEDENT><DEDENT>return errs<EOL>
Checks for any conflicts between modules configured to be built.
f10347:c2:m113
def check_ready(self, throw_error=True):
shutit_global.shutit_global_object.yield_to_draw()<EOL>cfg = self.cfg<EOL>self.log('<STR_LIT>', level=logging.DEBUG)<EOL>errs = []<EOL>self.pause_point('<STR_LIT>', print_input=False, level=<NUM_LIT:3>)<EOL>for module_id in self.module_ids():<EOL><INDENT>module = self.shutit_map[module_id]<EOL>self.log('<STR_LIT>' + module_id, level=logging.DEBUG)<EOL>if cfg[module_id]['<STR_LIT>'] and module.module_id not in self.get_current_shutit_pexpect_session_environment().modules_ready and not self.is_installed(module):<EOL><INDENT>self.log('<STR_LIT>' + module_id, level=logging.DEBUG)<EOL>self.login(prompt_prefix=module_id,command=shutit_global.shutit_global_object.bash_startup_command,echo=False)<EOL>revert_dir = os.getcwd()<EOL>self.get_current_shutit_pexpect_session_environment().module_root_dir = os.path.dirname(self.shutit_file_map[module_id])<EOL>self.chdir(self.get_current_shutit_pexpect_session_environment().module_root_dir)<EOL>if not self.is_ready(module) and throw_error:<EOL><INDENT>errs.append((module_id + '<STR_LIT>', self.get_shutit_pexpect_session_from_id('<STR_LIT>')))<EOL><DEDENT>self.logout(echo=False)<EOL>self.chdir(revert_dir)<EOL><DEDENT><DEDENT>return errs<EOL>
Check that all modules are ready to be built, calling check_ready on each of those configured to be built and not already installed (see shutit.is_installed).
f10347:c2:m114
def do_remove(self, loglevel=logging.DEBUG):
shutit_global.shutit_global_object.yield_to_draw()<EOL>cfg = self.cfg<EOL>self.log('<STR_LIT>', level=loglevel)<EOL>self.pause_point('<STR_LIT>', print_input=False, level=<NUM_LIT:3>)<EOL>for module_id in self.module_ids():<EOL><INDENT>module = self.shutit_map[module_id]<EOL>self.log('<STR_LIT>' + module_id, level=logging.DEBUG)<EOL>if cfg[module_id]['<STR_LIT>']:<EOL><INDENT>self.log('<STR_LIT>' + module_id, level=logging.DEBUG)<EOL>self.login(prompt_prefix=module_id,command=shutit_global.shutit_global_object.bash_startup_command,echo=False)<EOL>if not module.remove(self):<EOL><INDENT>self.log(self.print_modules(), level=logging.DEBUG)<EOL>self.fail(module_id + '<STR_LIT>', shutit_pexpect_child=self.get_shutit_pexpect_session_from_id('<STR_LIT>').pexpect_child) <EOL><DEDENT>else:<EOL><INDENT>if self.build['<STR_LIT>'] in ('<STR_LIT>','<STR_LIT>'):<EOL><INDENT>self.send('<STR_LIT>' + shutit_global.shutit_global_object.shutit_state_dir_build_db_dir + '<STR_LIT>' + module.module_id + '<STR_LIT>' + shutit_global.shutit_global_object.shutit_state_dir_build_db_dir + '<STR_LIT>' + module.module_id + '<STR_LIT>' + shutit_global.shutit_global_object.shutit_state_dir_build_db_dir + '<STR_LIT>' + module.module_id + '<STR_LIT>', loglevel=loglevel, echo=False)<EOL>if module.module_id in self.get_current_shutit_pexpect_session_environment().modules_installed:<EOL><INDENT>self.get_current_shutit_pexpect_session_environment().modules_installed.remove(module.module_id)<EOL><DEDENT>self.get_current_shutit_pexpect_session_environment().modules_not_installed.append(module.module_id)<EOL><DEDENT><DEDENT>self.logout(echo=False)<EOL><DEDENT><DEDENT>
Remove modules by calling remove method on those configured for removal.
f10347:c2:m115
def build_module(self, module, loglevel=logging.DEBUG):
shutit_global.shutit_global_object.yield_to_draw()<EOL>cfg = self.cfg<EOL>self.log('<STR_LIT>' + module.module_id + '<STR_LIT>' + str(module.run_order), level=logging.INFO)<EOL>self.build['<STR_LIT>'] = (self.build['<STR_LIT>'] + '<STR_LIT>' + module.module_id + '<STR_LIT>' + str(module.run_order))<EOL>if not module.build(self):<EOL><INDENT>self.fail(module.module_id + '<STR_LIT>', shutit_pexpect_child=self.get_shutit_pexpect_session_from_id('<STR_LIT>').pexpect_child) <EOL><DEDENT>else:<EOL><INDENT>if self.build['<STR_LIT>'] in ('<STR_LIT>','<STR_LIT>'):<EOL><INDENT>self.send('<STR_LIT>' + shutit_global.shutit_global_object.shutit_state_dir_build_db_dir + '<STR_LIT>' + module.module_id + '<STR_LIT>' + shutit_global.shutit_global_object.shutit_state_dir_build_db_dir + '<STR_LIT>' + module.module_id + '<STR_LIT>' + shutit_global.shutit_global_object.shutit_state_dir_build_db_dir + '<STR_LIT>' + module.module_id + '<STR_LIT>', loglevel=loglevel, echo=False)<EOL><DEDENT>self.get_current_shutit_pexpect_session_environment().modules_installed.append(module.module_id)<EOL>if module.module_id in self.get_current_shutit_pexpect_session_environment().modules_not_installed:<EOL><INDENT>self.get_current_shutit_pexpect_session_environment().modules_not_installed.remove(module.module_id)<EOL><DEDENT><DEDENT>self.pause_point('<STR_LIT>' + module.module_id, print_input=True, level=<NUM_LIT:2>)<EOL>self.build['<STR_LIT>'] = (self.build['<STR_LIT>'] + '<STR_LIT>' + module.module_id)<EOL>if cfg[module.module_id]['<STR_LIT>']:<EOL><INDENT>self.log(self.build_report('<STR_LIT>' + module.module_id), level=logging.DEBUG)<EOL><DEDENT>if not cfg[module.module_id]['<STR_LIT>'] and shutit_global.shutit_global_object.interactive >= <NUM_LIT:2>:<EOL><INDENT>shutit_global.shutit_global_object.shutit_print("<STR_LIT>" + "<STR_LIT>" + module.module_id + "<STR_LIT>")<EOL>cfg[module.module_id]['<STR_LIT>'] = (shutit_util.util_raw_input(default='<STR_LIT:y>') == '<STR_LIT:y>')<EOL><DEDENT>if cfg[module.module_id]['<STR_LIT>'] or self.build['<STR_LIT>']:<EOL><INDENT>self.log(module.module_id + '<STR_LIT>',level=logging.INFO)<EOL>self.stop_all(module.run_order)<EOL>self.do_repository_work(str(module.module_id) + '<STR_LIT:_>' + str(module.run_order), password=self.host['<STR_LIT:password>'], docker_executable=self.host['<STR_LIT>'], force=True)<EOL>self.start_all(module.run_order)<EOL><DEDENT>if shutit_global.shutit_global_object.interactive >= <NUM_LIT:2>:<EOL><INDENT>shutit_global.shutit_global_object.shutit_print("<STR_LIT>")<EOL>if shutit_util.util_raw_input(default='<STR_LIT:y>') == '<STR_LIT:y>':<EOL><INDENT>shutit_global.shutit_global_object.interactive = <NUM_LIT:0><EOL><DEDENT><DEDENT>
Build passed-in module.
f10347:c2:m116
def do_build(self):
shutit_global.shutit_global_object.yield_to_draw()<EOL>cfg = self.cfg<EOL>self.log('<STR_LIT>', level=logging.DEBUG)<EOL>module_id_list = self.module_ids()<EOL>if self.build['<STR_LIT>']:<EOL><INDENT>module_id_list_build_only = filter(lambda x: cfg[x]['<STR_LIT>'], module_id_list)<EOL><DEDENT>for module_id in module_id_list:<EOL><INDENT>module = self.shutit_map[module_id]<EOL>self.log('<STR_LIT>' + module.module_id, level=logging.INFO)<EOL>if cfg[module.module_id]['<STR_LIT>']:<EOL><INDENT>if self.build['<STR_LIT>'] not in module.ok_delivery_methods:<EOL><INDENT>self.fail('<STR_LIT>' + module.module_id + '<STR_LIT>' + str(module.ok_delivery_methods) + '<STR_LIT>') <EOL><DEDENT>if self.is_installed(module):<EOL><INDENT>self.build['<STR_LIT>'] = (self.build['<STR_LIT>'] + '<STR_LIT>' + module.module_id + '<STR_LIT>' + str(module.run_order))<EOL><DEDENT>else:<EOL><INDENT>if self.build['<STR_LIT>'] and module_id == module_id_list_build_only[-<NUM_LIT:1>]:<EOL><INDENT>self.build['<STR_LIT>'] = (self.build['<STR_LIT>'] + '<STR_LIT>' + module.module_id + '<STR_LIT>' + str(module.run_order) + '<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>revert_dir = os.getcwd()<EOL>self.get_current_shutit_pexpect_session_environment().module_root_dir = os.path.dirname(self.shutit_file_map[module_id])<EOL>self.chdir(self.get_current_shutit_pexpect_session_environment().module_root_dir)<EOL>self.login(prompt_prefix=module_id,command=shutit_global.shutit_global_object.bash_startup_command,echo=False)<EOL>self.build_module(module)<EOL>self.logout(echo=False)<EOL>self.chdir(revert_dir)<EOL><DEDENT><DEDENT><DEDENT>if self.is_installed(module):<EOL><INDENT>self.log('<STR_LIT>',level=logging.DEBUG)<EOL>if not module.start(self):<EOL><INDENT>self.fail(module.module_id + '<STR_LIT>', shutit_pexpect_child=self.get_shutit_pexpect_session_from_id('<STR_LIT>').pexpect_child)<EOL><DEDENT><DEDENT><DEDENT>
Runs build phase, building any modules that we've determined need building.
f10347:c2:m117
def do_finalize(self):
shutit_global.shutit_global_object.yield_to_draw()<EOL>def _finalize(self):<EOL><INDENT>self.stop_all()<EOL>self.log('<STR_LIT>' + str(self), level=logging.DEBUG)<EOL>for module_id in self.module_ids(rev=True):<EOL><INDENT>if self.is_installed(self.shutit_map[module_id]):<EOL><INDENT>self.login(prompt_prefix=module_id,command=shutit_global.shutit_global_object.bash_startup_command,echo=False)<EOL>if not self.shutit_map[module_id].finalize(self):<EOL><INDENT>self.fail(module_id + '<STR_LIT>', shutit_pexpect_child=self.get_shutit_pexpect_session_from_id('<STR_LIT>').pexpect_child) <EOL><DEDENT>self.logout(echo=False)<EOL><DEDENT><DEDENT><DEDENT>_finalize(self)<EOL>
Runs finalize phase; run after all builds are complete and all modules have been stopped.
f10347:c2:m119
def stop_all(self, run_order=-<NUM_LIT:1>):
shutit_global.shutit_global_object.yield_to_draw()<EOL>for module_id in self.module_ids(rev=True):<EOL><INDENT>shutit_module_obj = self.shutit_map[module_id]<EOL>if run_order == -<NUM_LIT:1> or shutit_module_obj.run_order <= run_order:<EOL><INDENT>if self.is_installed(shutit_module_obj):<EOL><INDENT>if not shutit_module_obj.stop(self):<EOL><INDENT>self.fail('<STR_LIT>' + module_id, shutit_pexpect_child=self.get_shutit_pexpect_session_from_id('<STR_LIT>').shutit_pexpect_child)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>
Runs stop method on all modules less than the passed-in run_order. Used when target is exporting itself mid-build, so we clean up state before committing run files etc.
f10347:c2:m120
def start_all(self, run_order=-<NUM_LIT:1>):
shutit_global.shutit_global_object.yield_to_draw()<EOL>for module_id in self.module_ids():<EOL><INDENT>shutit_module_obj = self.shutit_map[module_id]<EOL>if run_order == -<NUM_LIT:1> or shutit_module_obj.run_order <= run_order:<EOL><INDENT>if self.is_installed(shutit_module_obj):<EOL><INDENT>if not shutit_module_obj.start(self):<EOL><INDENT>self.fail('<STR_LIT>' + module_id, shutit_pexpect_child=self.get_shutit_pexpect_session_from_id('<STR_LIT>').shutit_pexpect_child)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>
Runs start method on all modules less than the passed-in run_order. Used when target is exporting itself mid-build, so we can export a clean target and still depended-on modules running if necessary.
f10347:c2:m121
def is_ready(self, shutit_module_obj):
shutit_global.shutit_global_object.yield_to_draw()<EOL>if shutit_module_obj.module_id in self.get_current_shutit_pexpect_session_environment().modules_ready:<EOL><INDENT>self.log('<STR_LIT>',level=logging.DEBUG)<EOL>return True<EOL><DEDENT>ready = shutit_module_obj.check_ready(self)<EOL>if ready:<EOL><INDENT>self.get_current_shutit_pexpect_session_environment().modules_ready.append(shutit_module_obj.module_id)<EOL>return True<EOL><DEDENT>return False<EOL>
Returns true if this module is ready to be built. Caches the result (as it's assumed not to change during the build).
f10347:c2:m122
def init_shutit_map(self):
shutit_global.shutit_global_object.yield_to_draw()<EOL>modules = self.shutit_modules<EOL>if len([mod for mod in modules if mod.run_order > <NUM_LIT:0>]) < <NUM_LIT:1>:<EOL><INDENT>self.log(modules,level=logging.DEBUG)<EOL>path = '<STR_LIT::>'.join(self.host['<STR_LIT>'])<EOL>self.log('<STR_LIT>',level=logging.INFO)<EOL>if path == '<STR_LIT>':<EOL><INDENT>self.fail('<STR_LIT>') <EOL><DEDENT>elif path == '<STR_LIT:.>':<EOL><INDENT>self.fail('<STR_LIT>') <EOL><DEDENT>else:<EOL><INDENT>self.fail('<STR_LIT>' + path + '<STR_LIT>') <EOL><DEDENT><DEDENT>self.log('<STR_LIT>', level=logging.DEBUG)<EOL>run_orders = {}<EOL>has_core_module = False<EOL>for module in modules:<EOL><INDENT>assert isinstance(module, ShutItModule), shutit_util.print_debug()<EOL>if module.module_id in self.shutit_map:<EOL><INDENT>self.fail('<STR_LIT>' + module.module_id + '<STR_LIT>') <EOL><DEDENT>if module.run_order in run_orders:<EOL><INDENT>self.fail('<STR_LIT>' + str(module.run_order) + '<STR_LIT>' + module.module_id + '<STR_LIT>' + run_orders[module.run_order].module_id + '<STR_LIT>') <EOL><DEDENT>if module.run_order == <NUM_LIT:0>:<EOL><INDENT>has_core_module = True<EOL><DEDENT>self.shutit_map[module.module_id] = run_orders[module.run_order] = module<EOL>self.shutit_file_map[module.module_id] = get_module_file(self, module)<EOL><DEDENT>if not has_core_module:<EOL><INDENT>self.fail('<STR_LIT>')<EOL><DEDENT>
Initializes the module map of shutit based on the modules we have gathered. Checks we have core modules Checks for duplicate module details. Sets up common config. Sets up map of modules.
f10347:c2:m123
def conn_target(self):
shutit_global.shutit_global_object.yield_to_draw()<EOL>conn_module = None<EOL>for mod in self.conn_modules:<EOL><INDENT>if mod.module_id == self.build['<STR_LIT>']:<EOL><INDENT>conn_module = mod<EOL>break<EOL><DEDENT><DEDENT>if conn_module is None:<EOL><INDENT>self.fail('<STR_LIT>' + self.build['<STR_LIT>']) <EOL><DEDENT>conn_module.get_config(self)<EOL>conn_module.build(self)<EOL>
Connect to the target.
f10347:c2:m124
def finalize_target(self):
shutit_global.shutit_global_object.yield_to_draw()<EOL>self.pause_point('<STR_LIT>' + self.shutit_main_dir + '<STR_LIT>', print_input=False, level=<NUM_LIT:3>)<EOL>for mod in self.conn_modules:<EOL><INDENT>if mod.module_id == self.build['<STR_LIT>']:<EOL><INDENT>conn_module = mod<EOL>break<EOL><DEDENT><DEDENT>conn_module.finalize(self)<EOL>
Finalize the target using the core finalize method.
f10347:c2:m125
def resolve_dependencies(self, to_build, depender):
shutit_global.shutit_global_object.yield_to_draw()<EOL>self.log('<STR_LIT>',level=logging.DEBUG)<EOL>cfg = self.cfg<EOL>for dependee_id in depender.depends_on:<EOL><INDENT>dependee = self.shutit_map.get(dependee_id)<EOL>if (dependee and dependee not in to_build<EOL>and cfg[dependee_id]['<STR_LIT>']):<EOL><INDENT>to_build.append(dependee)<EOL>cfg[dependee_id]['<STR_LIT>'] = True<EOL><DEDENT><DEDENT>return True<EOL>
Add any required dependencies.
f10347:c2:m126
def check_dependee_exists(self, depender, dependee, dependee_id):
shutit_global.shutit_global_object.yield_to_draw()<EOL>if dependee is None:<EOL><INDENT>return '<STR_LIT>' + dependee_id + '<STR_LIT>' + str(self.host['<STR_LIT>']) + '<STR_LIT>' + depender.module_id + '<STR_LIT>'<EOL><DEDENT>return '<STR_LIT>'<EOL>
Checks whether a depended-on module is available.
f10347:c2:m127
def check_dependee_build(self, depender, dependee, dependee_id):
shutit_global.shutit_global_object.yield_to_draw()<EOL>cfg = self.cfg<EOL>if not (cfg[dependee.module_id]['<STR_LIT>'] or<EOL>self.is_to_be_built_or_is_installed(dependee)):<EOL><INDENT>return '<STR_LIT>' + depender.module_id + '<STR_LIT>' + dependee_id + '<STR_LIT>'<EOL><DEDENT>return '<STR_LIT>'<EOL>
Checks whether a depended on module is configured to be built.
f10347:c2:m128
def destroy(self):
if self.session_type == '<STR_LIT>':<EOL><INDENT>self.logout()<EOL><DEDENT>elif self.session_type == '<STR_LIT>':<EOL><INDENT>self.logout()<EOL><DEDENT>
Finish up a session.
f10347:c2:m134
def add_shutit_pexpect_session_environment(pexpect_session_environment):
shutit_global.shutit_global_object.shutit_pexpect_session_environments.add(pexpect_session_environment)<EOL>
Adds an environment object to a shutit_pexpect_session object.
f10349:m0
def __init__(self,<EOL>shutit,<EOL>pexpect_session_id,<EOL>command,<EOL>args=None,<EOL>timeout=shutit_global.shutit_global_object.default_timeout,<EOL>maxread=<NUM_LIT>,<EOL>searchwindowsize=None,<EOL>env=None,<EOL>ignore_sighup=False,<EOL>echo=True,<EOL>preexec_fn=None,<EOL>encoding=None,<EOL>codec_errors='<STR_LIT:strict>',<EOL>dimensions=None,<EOL>delaybeforesend=None):
<EOL>if not encoding and shutit_global.shutit_global_object.ispy3: <EOL><INDENT>encoding = shutit_global.shutit_global_object.default_encoding<EOL><DEDENT>assert isinstance(shutit, shutit_class.ShutIt), shutit_util.print_debug()<EOL>self.shutit = shutit<EOL>self.check_exit = True<EOL>self.default_expect = [shutit_global.shutit_global_object.base_prompt]<EOL>self.shell_expect = self.default_expect<EOL>self.in_shell = True<EOL>self.pexpect_session_id = pexpect_session_id<EOL>self.pexpect_session_number = len(shutit_global.get_shutit_pexpect_sessions()) + <NUM_LIT:1><EOL>self.login_stack = ShutItLoginStack(shutit_obj=shutit)<EOL>self.current_environment = None<EOL>args = args or []<EOL>if not delaybeforesend:<EOL><INDENT>delaybeforesend=shutit_global.shutit_global_object.delaybeforesend<EOL><DEDENT>self.pexpect_session_pane = None<EOL>self.session_output_lines = []<EOL>self.pexpect_child = self._spawn_child(command=command,<EOL>args=args,<EOL>timeout=timeout,<EOL>maxread=maxread,<EOL>searchwindowsize=searchwindowsize,<EOL>env=env,<EOL>ignore_sighup=ignore_sighup,<EOL>echo=echo,<EOL>preexec_fn=preexec_fn,<EOL>encoding=encoding,<EOL>codec_errors=codec_errors,<EOL>dimensions=dimensions,<EOL>delaybeforesend=delaybeforesend)<EOL>
spawn a child, and manage the delaybefore send setting to 0
f10349:c0:m0
def _spawn_child(self,<EOL>command,<EOL>args=None,<EOL>timeout=shutit_global.shutit_global_object.default_timeout,<EOL>maxread=<NUM_LIT>,<EOL>searchwindowsize=None,<EOL>env=None,<EOL>ignore_sighup=False,<EOL>echo=True,<EOL>preexec_fn=None,<EOL>encoding=None,<EOL>codec_errors='<STR_LIT:strict>',<EOL>dimensions=None,<EOL>delaybeforesend=shutit_global.shutit_global_object.delaybeforesend):
shutit = self.shutit<EOL>args = args or []<EOL>pexpect_child = pexpect.spawn(command,<EOL>args=args,<EOL>timeout=timeout,<EOL>maxread=maxread,<EOL>searchwindowsize=searchwindowsize,<EOL>env=env,<EOL>ignore_sighup=ignore_sighup,<EOL>echo=echo,<EOL>preexec_fn=preexec_fn,<EOL>encoding=encoding,<EOL>codec_errors=codec_errors,<EOL>dimensions=dimensions)<EOL>pexpect_child.setwinsize(shutit_global.shutit_global_object.pexpect_window_size[<NUM_LIT:0>],shutit_global.shutit_global_object.pexpect_window_size[<NUM_LIT:1>])<EOL>pexpect_child.delaybeforesend=delaybeforesend<EOL>shutit.log('<STR_LIT>' + str(shutit.shutit_pexpect_sessions), level=logging.DEBUG)<EOL>shutit.shutit_pexpect_sessions.update({self.pexpect_session_id:self})<EOL>shutit.log('<STR_LIT>' + str(shutit.shutit_pexpect_sessions), level=logging.DEBUG)<EOL>return pexpect_child<EOL>
spawn a child, and manage the delaybefore send setting to 0
f10349:c0:m2
def sendline(self, sendspec):
assert not sendspec.started, shutit_util.print_debug()<EOL>shutit = self.shutit<EOL>shutit.log('<STR_LIT>' + str(id(self)) + '<STR_LIT>' + str(sendspec.send), level=logging.DEBUG)<EOL>if sendspec.expect:<EOL><INDENT>shutit.log('<STR_LIT>' + str(sendspec.expect), level=logging.DEBUG)<EOL><DEDENT>else:<EOL><INDENT>shutit.log('<STR_LIT>', level=logging.DEBUG)<EOL><DEDENT>try:<EOL><INDENT>if self._check_blocked(sendspec) and sendspec.ignore_background != True:<EOL><INDENT>shutit.log('<STR_LIT>', level=logging.DEBUG)<EOL>return False<EOL><DEDENT>if sendspec.run_in_background:<EOL><INDENT>shutit.log('<STR_LIT>', level=logging.DEBUG)<EOL>shutit_background_command_object = self.login_stack.get_current_login_item().append_background_send(sendspec)<EOL>sendspec.check_exit = False<EOL><DEDENT>if sendspec.nonewline != True:<EOL><INDENT>sendspec.send += '<STR_LIT:\n>'<EOL>sendspec.nonewline = True<EOL><DEDENT>if sendspec.run_in_background:<EOL><INDENT>shutit_background_command_object.run_background_command()<EOL>return True<EOL><DEDENT>self.pexpect_child.send(sendspec.send)<EOL>return False<EOL><DEDENT>except OSError:<EOL><INDENT>self.shutit.fail('<STR_LIT>')<EOL><DEDENT>
Sends line, handling background and newline directives. True means: 'all handled here ok' False means: you'll need to 'expect' the right thing from here.
f10349:c0:m3
def wait(self, cadence=<NUM_LIT:2>, sendspec=None):
shutit = self.shutit<EOL>shutit.log('<STR_LIT>', level=logging.DEBUG)<EOL>if sendspec:<EOL><INDENT>cadence = sendspec.wait_cadence<EOL><DEDENT>shutit.log('<STR_LIT>' + str(self.login_stack), level=logging.DEBUG)<EOL>while True:<EOL><INDENT>res, res_str, background_object = self.login_stack.get_current_login_item().check_background_commands_complete()<EOL>shutit.log('<STR_LIT>' + str(background_object) + '<STR_LIT>' + str(res) + '<STR_LIT>' + str(res_str), level=logging.DEBUG)<EOL>if res:<EOL><INDENT>break<EOL><DEDENT>elif res_str in ('<STR_LIT:S>','<STR_LIT:N>'):<EOL><INDENT>pass<EOL><DEDENT>elif res_str == '<STR_LIT:F>':<EOL><INDENT>assert background_object is not None, shutit_util.print_debug()<EOL>assert isinstance(background_object, ShutItBackgroundCommand), shutit_util.print_debug()<EOL>shutit.log('<STR_LIT>' + str(self.login_stack), level=logging.DEBUG)<EOL>self.pause_point('<STR_LIT>' + background_object.sendspec.original_send + '<STR_LIT>')<EOL>return False<EOL><DEDENT>else:<EOL><INDENT>self.shutit.fail('<STR_LIT>' + res_str) <EOL><DEDENT>time.sleep(cadence)<EOL><DEDENT>shutit.log('<STR_LIT>', level=logging.DEBUG)<EOL>return True<EOL>
Does not return until all background commands are completed.
f10349:c0:m5
def login(self, sendspec):
user = sendspec.user<EOL>command = sendspec.send<EOL>prompt_prefix = sendspec.prompt_prefix<EOL>shutit = self.shutit<EOL>if isinstance(sendspec.password,str):<EOL><INDENT>shutit_global.shutit_global_object.secret_words_set.add(sendspec.password)<EOL><DEDENT>r_id = shutit_util.random_id()<EOL>if prompt_prefix is None:<EOL><INDENT>prompt_prefix = r_id<EOL><DEDENT>if user is None:<EOL><INDENT>user = self.whoami()<EOL>if '<STR_LIT>' not in command:<EOL><INDENT>shutit.log('<STR_LIT>' + user + '<STR_LIT>', level=logging.WARNING)<EOL><DEDENT><DEDENT>if '<STR_LIT:U+0020>' in user:<EOL><INDENT>self.shutit.fail('<STR_LIT>' + user + '<STR_LIT>') <EOL><DEDENT>if self.shutit.build['<STR_LIT>'] == '<STR_LIT>' and command == '<STR_LIT>':<EOL><INDENT>command = '<STR_LIT>'<EOL><DEDENT>if command == '<STR_LIT>' or command == '<STR_LIT>' or command == '<STR_LIT>':<EOL><INDENT>send = command + '<STR_LIT:U+0020>' + user<EOL><DEDENT>else:<EOL><INDENT>send = command<EOL><DEDENT>login_expect = sendspec.expect or shutit_global.shutit_global_object.base_prompt<EOL>general_expect = [login_expect]<EOL>general_expect = general_expect + [user+'<STR_LIT>'+'<STR_LIT>']<EOL>if (sendspec.is_ssh != None and sendspec.is_ssh) or command.find('<STR_LIT>') != -<NUM_LIT:1>:<EOL><INDENT>shutit.log('<STR_LIT>' + str(sendspec.is_ssh) + '<STR_LIT>' + command, level=logging.DEBUG)<EOL>if user+'<STR_LIT:@>' in general_expect:<EOL><INDENT>general_expect.remove(user+'<STR_LIT:@>')<EOL><DEDENT>general_expect.append('<STR_LIT>')<EOL>send_dict={'<STR_LIT>':['<STR_LIT:yes>', False]}<EOL>if sendspec.password is not None:<EOL><INDENT>send_dict.update({'<STR_LIT>':[sendspec.password, True]})<EOL>send_dict.update({r'<STR_LIT>':[sendspec.password, True]})<EOL><DEDENT><DEDENT>else:<EOL><INDENT>send_dict={'<STR_LIT>':['<STR_LIT:yes>', False]}<EOL>if sendspec.password is not None:<EOL><INDENT>send_dict.update({'<STR_LIT>':[sendspec.password, True]})<EOL>send_dict.update({r'<STR_LIT>':[sendspec.password, True]})<EOL>send_dict.update({user+'<STR_LIT:@>':[sendspec.password, True]})<EOL><DEDENT><DEDENT>if user == '<STR_LIT>' and command == '<STR_LIT>':<EOL><INDENT>shutit.log('<STR_LIT>' + user + '<STR_LIT>', level=logging.WARNING)<EOL><DEDENT>self.shutit.handle_note(sendspec.note,command=command + '<STR_LIT>' + user + '<STR_LIT>',training_input=send)<EOL>echo = self.shutit.get_echo_override(sendspec.echo)<EOL>shutit.log('<STR_LIT>' + user, level=logging.DEBUG)<EOL>shutit.log('<STR_LIT>' + send + '<STR_LIT>' + user, level=logging.DEBUG)<EOL>shutit.log('<STR_LIT>' + str(self.login_stack), level=logging.DEBUG)<EOL>check_sudo = False<EOL>if sendspec.password is None and send.strip().find('<STR_LIT>') == <NUM_LIT:0>:<EOL><INDENT>check_sudo = True<EOL><DEDENT>res = self.multisend(ShutItSendSpec(self,<EOL>send=send,<EOL>send_dict=send_dict,<EOL>expect=general_expect,<EOL>check_exit=False,<EOL>timeout=sendspec.timeout,<EOL>fail_on_empty_before=False,<EOL>escape=sendspec.escape,<EOL>echo=echo,<EOL>remove_on_match=True,<EOL>nonewline=sendspec.nonewline,<EOL>check_sudo=check_sudo,<EOL>loglevel=sendspec.loglevel))<EOL>if res == -<NUM_LIT:1>:<EOL><INDENT>assert False, shutit_util.print_debug()<EOL><DEDENT>if prompt_prefix != None:<EOL><INDENT>self.setup_prompt(r_id,prefix=prompt_prefix,capture_exit_code=True)<EOL><DEDENT>else:<EOL><INDENT>self.setup_prompt(r_id,capture_exit_code=True)<EOL><DEDENT>self.login_stack.append(r_id)<EOL>shutit.log('<STR_LIT>' + str(self.login_stack), level=logging.DEBUG)<EOL>if self.send_and_get_output('''<STR_LIT>''', loglevel=logging.DEBUG, echo=False) != '<STR_LIT:0>':<EOL><INDENT>if sendspec.fail_on_fail: <EOL><INDENT>self.shutit.fail('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT><DEDENT>if sendspec.go_home:<EOL><INDENT>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>',<EOL>check_exit=False,<EOL>echo=False,<EOL>ignore_background=True,<EOL>loglevel=sendspec.loglevel))<EOL><DEDENT>self.shutit.handle_note_after(note=sendspec.note,training_input=send)<EOL>return True<EOL>
Logs the user in with the passed-in password and command. Tracks the login. If used, used logout to log out again. Assumes you are root when logging in, so no password required. If not, override the default command for multi-level logins. If passwords are required, see setup_prompt() and revert_prompt() @type param: see shutit_sendspec.ShutItSendSpec @type sendspec: shutit_sendspec.ShutItSendSpec
f10349:c0:m6
def logout_all(self, sendspec):
while self.login_stack.length() > <NUM_LIT:0>:<EOL><INDENT>self.logout(sendspec)<EOL><DEDENT>
Completely logs the user out of all logins in this pexpect session
f10349:c0:m7
def logout(self, sendspec):
<EOL>self.wait()<EOL>shutit = self.shutit<EOL>shutit.handle_note(sendspec.note,training_input=sendspec.send)<EOL>if self.login_stack.length() > <NUM_LIT:0>:<EOL><INDENT>_ = self.login_stack.pop()<EOL>if self.login_stack.length() > <NUM_LIT:0>:<EOL><INDENT>old_prompt_name = self.login_stack.get_current_login_id()<EOL>self.default_expect = shutit.expect_prompts[old_prompt_name]<EOL><DEDENT>else:<EOL><INDENT>shutit.set_default_shutit_pexpect_session_expect()<EOL><DEDENT><DEDENT>else:<EOL><INDENT>shutit.fail('<STR_LIT>', throw_exception=False) <EOL><DEDENT>echo = shutit.get_echo_override(sendspec.echo)<EOL>output = self.send_and_get_output(sendspec.send,<EOL>fail_on_empty_before=False,<EOL>timeout=sendspec.timeout,<EOL>echo=echo,<EOL>loglevel=sendspec.loglevel,<EOL>nonewline=sendspec.nonewline)<EOL>shutit.handle_note_after(note=sendspec.note)<EOL>return output<EOL>
Logs the user out. Assumes that login has been called. If login has never been called, throw an error. @param command: Command to run to log out (default=exit) @param note: See send()
f10349:c0:m8
def setup_prompt(self,<EOL>prompt_name,<EOL>prefix='<STR_LIT:default>',<EOL>capture_exit_code=False,<EOL>loglevel=logging.DEBUG):
shutit = self.shutit<EOL>local_prompt = prefix + '<STR_LIT::>' + shutit_util.random_id() + '<STR_LIT>'<EOL>shutit.expect_prompts[prompt_name] = local_prompt<EOL>shutit.log('<STR_LIT>', level=logging.DEBUG)<EOL>send_str = '<STR_LIT>'<EOL>if capture_exit_code:<EOL><INDENT>send_str = r'<STR_LIT>'<EOL><DEDENT>send_str += """<STR_LIT>""" + str(prompt_name) + """<STR_LIT>""" + str(local_prompt[:<NUM_LIT:2>]) + "<STR_LIT>" + str(local_prompt[<NUM_LIT:2>:]) + """<STR_LIT>""" + shutit_global.shutit_global_object.prompt_command<EOL>self.send(ShutItSendSpec(self,<EOL>send=send_str,<EOL>expect=['<STR_LIT:\r\n>' + shutit.expect_prompts[prompt_name]],<EOL>fail_on_empty_before=False,<EOL>echo=False,<EOL>loglevel=loglevel,<EOL>ignore_background=True))<EOL>shutit.log('<STR_LIT>' + shutit.expect_prompts[prompt_name], level=loglevel)<EOL>self.default_expect = shutit.expect_prompts[prompt_name]<EOL>self.send(ShutItSendSpec(self, send="<STR_LIT>", echo=False, check_exit=False, loglevel=loglevel, ignore_background=True))<EOL>self.send(ShutItSendSpec(self, send="<STR_LIT>", echo=False, check_exit=False, loglevel=loglevel, ignore_background=True))<EOL>self.send(ShutItSendSpec(self, send="""<STR_LIT>""", echo=False, check_exit=False, loglevel=loglevel, ignore_background=True))<EOL>hostname = shutit.send_and_get_output("""<STR_LIT>""", echo=False, loglevel=logging.DEBUG)<EOL>local_prompt_with_hostname = hostname + '<STR_LIT::>' + local_prompt<EOL>shutit.expect_prompts[prompt_name] = local_prompt_with_hostname<EOL>self.default_expect = shutit.expect_prompts[prompt_name]<EOL>self.shell_expect = self.default_expect<EOL>self.send(ShutItSendSpec(self,<EOL>send="""<STR_LIT>""" + shutit.expect_prompts[prompt_name][:<NUM_LIT:2>] + "<STR_LIT>" + shutit.expect_prompts[prompt_name][<NUM_LIT:2>:] + """<STR_LIT:'>""",<EOL>echo=False,<EOL>loglevel=loglevel,<EOL>ignore_background=True))<EOL>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>',<EOL>echo=False,<EOL>loglevel=loglevel,<EOL>ignore_background=True))<EOL>_ = self.init_pexpect_session_environment(prefix)<EOL>return True<EOL>
Use this when you've opened a new shell to set the PS1 to something sane. By default, it sets up the default expect so you don't have to worry about it and can just call shutit.send('a command'). If you want simple login and logout, please use login() and logout() within this module. Typically it would be used in this boilerplate pattern:: shutit.send('su - auser', expect=shutit_global.shutit_global_object.base_prompt, check_exit=False) shutit.setup_prompt('tmp_prompt') shutit.send('some command') [...] shutit.set_default_shutit_pexpect_session_expect() shutit.send('exit') This function is assumed to be called whenever there is a change of environment. @param prompt_name: Reference name for prompt. @param prefix: Prompt prefix. Default: 'default' @param capture_exit_code: Captures the exit code of the previous command into a SHUTIT_EC variable. Useful for when we want to work out whether the login worked. @type prompt_name: string @type prefix: string
f10349:c0:m9
def revert_prompt(self,<EOL>old_prompt_name,<EOL>new_expect=None):
shutit = self.shutit<EOL>expect = new_expect or self.default_expect<EOL>self.send(ShutItSendSpec(self,<EOL>send=('<STR_LIT>') % (old_prompt_name, old_prompt_name),<EOL>expect=expect,<EOL>check_exit=False,<EOL>fail_on_empty_before=False,<EOL>echo=False,<EOL>loglevel=logging.DEBUG,<EOL>ignore_background=True))<EOL>if not new_expect:<EOL><INDENT>shutit.log('<STR_LIT>', level=logging.DEBUG)<EOL>shutit.set_default_shutit_pexpect_session_expect()<EOL><DEDENT>_ = self.init_pexpect_session_environment(old_prompt_name)<EOL>
Reverts the prompt to the previous value (passed-in). It should be fairly rare to need this. Most of the time you would just exit a subshell rather than resetting the prompt. - old_prompt_name - - new_expect - - child - See send()
f10349:c0:m10
def expect(self,<EOL>expect,<EOL>searchwindowsize=None,<EOL>maxread=None,<EOL>timeout=None,<EOL>iteration_n=<NUM_LIT:1>):
if isinstance(expect, str):<EOL><INDENT>expect = [expect]<EOL><DEDENT>if searchwindowsize != None:<EOL><INDENT>old_searchwindowsize = self.pexpect_child.searchwindowsize<EOL>self.pexpect_child.searchwindowsize = searchwindowsize<EOL><DEDENT>if maxread != None:<EOL><INDENT>old_maxread = self.pexpect_child.maxread<EOL>self.pexpect_child.maxread = maxread<EOL><DEDENT>res = self.pexpect_child.expect(expect + [pexpect.TIMEOUT] + [pexpect.EOF], timeout=timeout)<EOL>if searchwindowsize != None:<EOL><INDENT>self.pexpect_child.searchwindowsize = old_searchwindowsize<EOL><DEDENT>if maxread != None:<EOL><INDENT>self.pexpect_child.maxread = old_maxread<EOL><DEDENT>if shutit_global.shutit_global_object.pane_manager and iteration_n == <NUM_LIT:1>:<EOL><INDENT>time_seen = time.time()<EOL>lines_to_add = []<EOL>if isinstance(self.pexpect_child.before, (str,unicode)):<EOL><INDENT>for line_str in self.pexpect_child.before.split('<STR_LIT:\n>'):<EOL><INDENT>lines_to_add.append(line_str)<EOL><DEDENT><DEDENT>if isinstance(self.pexpect_child.after, (str,unicode)):<EOL><INDENT>for line_str in self.pexpect_child.after.split('<STR_LIT:\n>'):<EOL><INDENT>lines_to_add.append(line_str)<EOL><DEDENT><DEDENT>for line in lines_to_add:<EOL><INDENT>self.session_output_lines.append(SessionPaneLine(line_str=line, time_seen=time_seen, line_type='<STR_LIT>'))<EOL><DEDENT><DEDENT>return res<EOL>
Handle child expects, with EOF and TIMEOUT handled iteration_n - Number of times this expect has been called for the send. If 1, (the default) then it gets added to the pane of output (if applicable to this run)
f10349:c0:m11
def replace_container(self, new_target_image_name, go_home=None):
shutit = self.shutit<EOL>shutit.log('<STR_LIT>' + new_target_image_name + '<STR_LIT>', level=logging.DEBUG)<EOL>shutit.log(shutit.print_session_state(), level=logging.DEBUG)<EOL>conn_module = None<EOL>for mod in shutit.conn_modules:<EOL><INDENT>if mod.module_id == shutit.build['<STR_LIT>']:<EOL><INDENT>conn_module = mod<EOL>break<EOL><DEDENT><DEDENT>if conn_module is None:<EOL><INDENT>shutit.fail('''<STR_LIT>''' + shutit.build['<STR_LIT>']) <EOL><DEDENT>container_id = shutit.target['<STR_LIT>']<EOL>conn_module.destroy_container(shutit, '<STR_LIT>', '<STR_LIT>', container_id)<EOL>shutit.target['<STR_LIT>'] = new_target_image_name<EOL>target_child = conn_module.start_container(shutit, self.pexpect_session_id)<EOL>conn_module.setup_target_child(shutit, target_child)<EOL>shutit.log('<STR_LIT>', level=logging.DEBUG)<EOL>shutit.log(shutit.print_session_state(), level=logging.DEBUG)<EOL>target_child = shutit.get_shutit_pexpect_session_from_id('<STR_LIT>')<EOL>if go_home != None:<EOL><INDENT>target_child.login(ShutItSendSpec(self,<EOL>send=shutit_global.shutit_global_object.bash_startup_command,<EOL>check_exit=False,<EOL>echo=False,<EOL>go_home=go_home))<EOL><DEDENT>else:<EOL><INDENT>target_child.login(ShutItSendSpec(self,<EOL>send=shutit_global.shutit_global_object.bash_startup_command,<EOL>check_exit=False,<EOL>echo=False))<EOL><DEDENT>return True<EOL>
Replaces a container. Assumes we are in Docker context.
f10349:c0:m12
def whoami(self,<EOL>note=None,<EOL>loglevel=logging.DEBUG):
shutit = self.shutit<EOL>shutit.handle_note(note)<EOL>res = self.send_and_get_output('<STR_LIT>',<EOL>echo=False,<EOL>loglevel=loglevel).strip()<EOL>if res == '<STR_LIT>':<EOL><INDENT>res = self.send_and_get_output('<STR_LIT>',<EOL>echo=False,<EOL>loglevel=loglevel).strip()<EOL><DEDENT>shutit.handle_note_after(note=note)<EOL>return res<EOL>
Returns the current user by executing "whoami". @param note: See send() @return: the output of "whoami" @rtype: string
f10349:c0:m13
def check_last_exit_values(self,<EOL>send,<EOL>check_exit=True,<EOL>expect=None,<EOL>exit_values=None,<EOL>retry=<NUM_LIT:0>,<EOL>retbool=False):
shutit = self.shutit<EOL>expect = expect or self.default_expect<EOL>if not self.check_exit or not check_exit:<EOL><INDENT>shutit.log('<STR_LIT>', level=logging.DEBUG)<EOL>return True<EOL><DEDENT>if exit_values is None:<EOL><INDENT>exit_values = ['<STR_LIT:0>']<EOL><DEDENT>if isinstance(exit_values, int):<EOL><INDENT>exit_values = [str(exit_values)]<EOL><DEDENT>send_exit_code = '<STR_LIT>'<EOL>shutit.log('<STR_LIT>' + str(send_exit_code), level=logging.DEBUG)<EOL>assert not self.sendline(ShutItSendSpec(self,<EOL>send=send_exit_code,<EOL>ignore_background=True)), shutit_util.print_debug()<EOL>shutit.log('<STR_LIT>' + str(expect), level=logging.DEBUG)<EOL>self.expect(expect,timeout=<NUM_LIT:10>)<EOL>shutit.log('<STR_LIT>' + str(self.pexpect_child.before), level=logging.DEBUG)<EOL>res = shutit.match_string(str(self.pexpect_child.before), '<STR_LIT>')<EOL>if res not in exit_values or res is None: <EOL><INDENT>res_str = res or str(res)<EOL>shutit.log('<STR_LIT>' + str(self.pexpect_child.after), level=logging.DEBUG)<EOL>shutit.log('<STR_LIT>' + str(send) + '<STR_LIT>' + res_str, level=logging.DEBUG)<EOL>msg = ('<STR_LIT>' + send + '<STR_LIT>' + res_str + '<STR_LIT>')<EOL>shutit.build['<STR_LIT>'] += msg<EOL>if retbool:<EOL><INDENT>return False<EOL><DEDENT>elif retry == <NUM_LIT:1> and shutit_global.shutit_global_object.interactive >= <NUM_LIT:1>:<EOL><INDENT>shutit.pause_point(msg + '<STR_LIT>' + res_str + '<STR_LIT>', shutit_pexpect_child=self.pexpect_child, level=<NUM_LIT:0>)<EOL><DEDENT>elif retry == <NUM_LIT:1>:<EOL><INDENT>shutit.fail('<STR_LIT>' + send + '<STR_LIT>' + res_str, throw_exception=False) <EOL><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT><DEDENT>return True<EOL>
Internal function to check the exit value of the shell. Do not use.
f10349:c0:m14
def pause_point(self,<EOL>msg='<STR_LIT>',<EOL>print_input=True,<EOL>resize=True,<EOL>color='<STR_LIT>',<EOL>default_msg=None,<EOL>interact=False,<EOL>wait=-<NUM_LIT:1>):
shutit = self.shutit<EOL>if shutit.build['<STR_LIT>'] and shutit.loglevel not in ('<STR_LIT>',):<EOL><INDENT>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>',<EOL>echo=False,<EOL>record_command=False,<EOL>ignore_background=True))<EOL><DEDENT>if self.in_shell:<EOL><INDENT>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>',<EOL>check_exit=False,<EOL>echo=False,<EOL>record_command=False,<EOL>ignore_background=True))<EOL><DEDENT>if print_input:<EOL><INDENT>if resize and wait < <NUM_LIT:0>:<EOL><INDENT>try:<EOL><INDENT>assert not self.sendline(ShutItSendSpec(self,<EOL>send='<STR_LIT>',<EOL>echo=False,<EOL>ignore_background=True)), shutit_util.print_debug()<EOL><DEDENT>except Exception:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>if default_msg is None:<EOL><INDENT>if not shutit.build['<STR_LIT>'] and not shutit.build['<STR_LIT>'] and not shutit.build['<STR_LIT>'] and not shutit.build['<STR_LIT>'] and self.shutit.loglevel not in ('<STR_LIT>',):<EOL><INDENT>pp_msg = '<STR_LIT>'<EOL>if not interact:<EOL><INDENT>pp_msg += '<STR_LIT>'<EOL><DEDENT>if shutit.build['<STR_LIT>'] == '<STR_LIT>':<EOL><INDENT>pp_msg += '<STR_LIT>'<EOL><DEDENT>shutit.log(shutit_util.colorise(color,'<STR_LIT:\r\n>' + <NUM_LIT>*'<STR_LIT:=>' + '<STR_LIT:\r\n>' + msg + '<STR_LIT:\r\n>' + <NUM_LIT>*'<STR_LIT:=>'+'<STR_LIT:\r\n>' + pp_msg),transient=True, level=logging.CRITICAL)<EOL><DEDENT>else:<EOL><INDENT>shutit.log('<STR_LIT:\r\n>' + (shutit_util.colorise(color, msg)),transient=True, level=logging.critical)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>shutit.log(shutit_util.colorise(color, msg) + '<STR_LIT:\r\n>' + default_msg + '<STR_LIT:\r\n>',transient=True, level=logging.CRITICAL)<EOL><DEDENT>oldlog = self.pexpect_child.logfile<EOL>self.pexpect_child.logfile = None<EOL>if wait > <NUM_LIT:0>:<EOL><INDENT>time.sleep(wait)<EOL><DEDENT>else:<EOL><INDENT>self.pexpect_child.setwinsize(shutit_global.shutit_global_object.root_window_size[<NUM_LIT:0>],shutit_global.shutit_global_object.root_window_size[<NUM_LIT:1>])<EOL>self.expect('<STR_LIT>')<EOL>if not shutit.build['<STR_LIT>'] and self.shutit.loglevel not in ('<STR_LIT>',):<EOL><INDENT>if self.in_shell:<EOL><INDENT>assert not self.sendline(ShutItSendSpec(self,<EOL>send='<STR_LIT>',<EOL>echo=False,<EOL>ignore_background=True)), shutit_util.print_debug()<EOL>self.expect('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>shutit.log('<STR_LIT>', level=logging.DEBUG)<EOL><DEDENT><DEDENT>if interact:<EOL><INDENT>self.pexpect_child.interact()<EOL><DEDENT>try:<EOL><INDENT>if shutit_global.shutit_global_object.ispy3:<EOL><INDENT>self.pexpect_child.interact()<EOL><DEDENT>else:<EOL><INDENT>self.pexpect_child.interact(input_filter=self._pause_input_filter)<EOL><DEDENT>self.handle_pause_point_signals()<EOL><DEDENT>except Exception as e:<EOL><INDENT>shutit.fail('<STR_LIT>' + str(e)) <EOL><DEDENT>if not shutit.build['<STR_LIT>'] and self.shutit.loglevel not in ('<STR_LIT>',):<EOL><INDENT>if self.in_shell:<EOL><INDENT>assert not self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>',<EOL>check_exit=False,<EOL>echo=False,<EOL>ignore_background=True)), shutit_util.print_debug()<EOL><DEDENT>else:<EOL><INDENT>shutit.log('<STR_LIT>', level=logging.DEBUG)<EOL><DEDENT><DEDENT><DEDENT>self.pexpect_child.logfile = oldlog<EOL><DEDENT>else:<EOL><INDENT>pass<EOL><DEDENT>shutit.build['<STR_LIT>'] = False<EOL>return True<EOL>
Inserts a pause in the build session, which allows the user to try things out before continuing. Ignored if we are not in an interactive mode. Designed to help debug the build, or drop to on failure so the situation can be debugged. @param msg: Message to display to user on pause point. @param print_input: Whether to take input at this point (i.e. interact), or simply pause pending any input. Default: True @param resize: If True, try to resize terminal. Default: False @param color: Color to print message (typically 31 for red, 32 for green) @param default_msg: Whether to print the standard blurb @param interact: Interact without mediation, and set up environment. @param wait: Wait a few seconds rather than for input (for video mode) @type msg: string @type print_input: boolean @type resize: boolean @type wait: decimal @return: True if pause point handled ok, else false
f10349:c0:m15
def file_exists(self,<EOL>filename,<EOL>directory=False,<EOL>note=None,<EOL>loglevel=logging.DEBUG):
shutit = self.shutit<EOL>shutit.handle_note(note, '<STR_LIT>' + filename)<EOL>test_type = '<STR_LIT>' if directory is True else '<STR_LIT>' if directory is None else '<STR_LIT>'<EOL>test = '<STR_LIT>' % (test_type, filename)<EOL>output = self.send_and_get_output(test + '<STR_LIT>',<EOL>record_command=False,<EOL>echo=False,<EOL>loglevel=loglevel)<EOL>res = shutit.match_string(output, '<STR_LIT>')<EOL>ret = False<EOL>if res == '<STR_LIT>':<EOL><INDENT>ret = True<EOL><DEDENT>elif res == '<STR_LIT>':<EOL><INDENT>pass<EOL><DEDENT>else: <EOL><INDENT>shutit.log(repr('<STR_LIT>' % (self.pexpect_child.before, self.pexpect_child.after)),transient=True, level=logging.INFO)<EOL>shutit.fail('<STR_LIT>' + output)<EOL><DEDENT>shutit.handle_note_after(note=note)<EOL>return ret<EOL>
Return True if file exists on the target host, else False @param filename: Filename to determine the existence of. @param directory: Indicate that the file is a directory. @param note: See send() @type filename: string @type directory: boolean @rtype: boolean
f10349:c0:m17
def chdir(self,<EOL>path,<EOL>timeout=shutit_global.shutit_global_object.default_timeout,<EOL>note=None,<EOL>loglevel=logging.DEBUG):
shutit = self.shutit<EOL>shutit.handle_note(note, '<STR_LIT>' + path)<EOL>shutit.log('<STR_LIT>' + path + '<STR_LIT:">', level=logging.DEBUG)<EOL>if shutit.build['<STR_LIT>'] in ('<STR_LIT>','<STR_LIT>'):<EOL><INDENT>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>' + path + '<STR_LIT:">',<EOL>timeout=timeout,<EOL>echo=False,<EOL>loglevel=loglevel))<EOL><DEDENT>elif shutit.build['<STR_LIT>'] in ('<STR_LIT>',):<EOL><INDENT>os.chdir(path)<EOL><DEDENT>else:<EOL><INDENT>shutit.fail('<STR_LIT>' + str(shutit.build['<STR_LIT>'])) <EOL><DEDENT>shutit.handle_note_after(note=note)<EOL>return True<EOL>
How to change directory will depend on whether we are in delivery mode bash or docker. @param path: Path to send file to. @param timeout: Timeout on response @param note: See send()
f10349:c0:m18
def get_file_perms(self,<EOL>filename,<EOL>note=None,<EOL>loglevel=logging.DEBUG):
shutit = self.shutit<EOL>shutit.handle_note(note)<EOL>cmd = '<STR_LIT>' + filename<EOL>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT:U+0020>' + cmd,<EOL>check_exit=False,<EOL>echo=False,<EOL>loglevel=loglevel,<EOL>ignore_background=True))<EOL>res = shutit.match_string(self.pexpect_child.before, '<STR_LIT>')<EOL>shutit.handle_note_after(note=note)<EOL>return res<EOL>
Returns the permissions of the file on the target as an octal string triplet. @param filename: Filename to get permissions of. @param note: See send() @type filename: string @rtype: string
f10349:c0:m19
def add_to_bashrc(self,<EOL>line,<EOL>match_regexp=None,<EOL>note=None,<EOL>loglevel=logging.DEBUG):
shutit = self.shutit<EOL>shutit.handle_note(note)<EOL>if not shutit_util.check_regexp(match_regexp):<EOL><INDENT>shutit.fail('<STR_LIT>' + match_regexp) <EOL><DEDENT>if self.whoami() == '<STR_LIT:root>':<EOL><INDENT>shutit.add_line_to_file(line, '<STR_LIT>', match_regexp=match_regexp, loglevel=loglevel)<EOL><DEDENT>else:<EOL><INDENT>shutit.add_line_to_file(line, '<STR_LIT>', match_regexp=match_regexp, loglevel=loglevel)<EOL><DEDENT>shutit.add_line_to_file(line, '<STR_LIT>', match_regexp=match_regexp, loglevel=loglevel)<EOL>return True<EOL>
Takes care of adding a line to everyone's bashrc (/etc/bash.bashrc). @param line: Line to add. @param match_regexp: See add_line_to_file() @param note: See send() @return: See add_line_to_file()
f10349:c0:m20
def is_user_id_available(self,<EOL>user_id,<EOL>note=None,<EOL>loglevel=logging.DEBUG):
shutit = self.shutit<EOL>shutit.handle_note(note)<EOL>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>' + user_id + '<STR_LIT>',<EOL>expect=self.default_expect,<EOL>echo=False,<EOL>loglevel=loglevel,<EOL>ignore_background=True))<EOL>shutit.handle_note_after(note=note)<EOL>if shutit.match_string(self.pexpect_child.before, '<STR_LIT>') == '<STR_LIT:1>':<EOL><INDENT>return False<EOL><DEDENT>return True<EOL>
Determine whether the specified user_id available. @param user_id: User id to be checked. @param note: See send() @type user_id: integer @rtype: boolean @return: True is the specified user id is not used yet, False if it's already been assigned to a user.
f10349:c0:m21
def set_password(self,<EOL>password,<EOL>user='<STR_LIT>',<EOL>note=None):
shutit = self.shutit<EOL>shutit.handle_note(note)<EOL>if isinstance(password, str):<EOL><INDENT>shutit_global.shutit_global_object.secret_words_set.add(password)<EOL><DEDENT>self.install('<STR_LIT>')<EOL>if self.current_environment.install_type == '<STR_LIT>':<EOL><INDENT>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>' + user,<EOL>expect='<STR_LIT>',<EOL>check_exit=False,<EOL>ignore_background=True))<EOL>self.send(ShutItSendSpec(self,<EOL>send=password,<EOL>expect='<STR_LIT>',<EOL>check_exit=False,<EOL>echo=False,<EOL>ignore_background=True<EOL>))<EOL>self.send(ShutItSendSpec(self,<EOL>send=password,<EOL>expect=self.default_expect,<EOL>echo=False,<EOL>ignore_background=True,<EOL>))<EOL><DEDENT>elif self.current_environment.install_type == '<STR_LIT>':<EOL><INDENT>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>' + user,<EOL>expect='<STR_LIT>',<EOL>check_exit=False,<EOL>ignore_background=True))<EOL>self.send(ShutItSendSpec(self,<EOL>send=password,<EOL>expect='<STR_LIT>',<EOL>check_exit=False,<EOL>echo=False,<EOL>ignore_background=True))<EOL>self.send(ShutItSendSpec(self,<EOL>send=password,<EOL>expect=self.default_expect,<EOL>echo=False,<EOL>ignore_background=True))<EOL><DEDENT>else:<EOL><INDENT>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>' + user,<EOL>expect='<STR_LIT>',<EOL>check_exit=False,<EOL>ignore_background=True))<EOL>self.send(ShutItSendSpec(self,<EOL>send=password,<EOL>expect='<STR_LIT>',<EOL>check_exit=False,<EOL>echo=False,<EOL>ignore_background=True))<EOL>self.send(ShutItSendSpec(self,<EOL>send=password,<EOL>expect=self.default_expect,<EOL>echo=False,<EOL>ignore_background=True))<EOL><DEDENT>shutit.handle_note_after(note=note)<EOL>return True<EOL>
Sets the password for the current user or passed-in user. As a side effect, installs the "password" package. @param user: username to set the password for. Defaults to '' (i.e. current user) @param password: password to set for the user @param note: See send()
f10349:c0:m22
def lsb_release(self,<EOL>loglevel=logging.DEBUG):
<EOL>shutit = self.shutit<EOL>d = {}<EOL>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>',<EOL>check_exit=False,<EOL>echo=False,<EOL>loglevel=loglevel,<EOL>ignore_background=True))<EOL>res = shutit.match_string(self.pexpect_child.before, r'<STR_LIT>')<EOL>if isinstance(res, str):<EOL><INDENT>dist_string = res<EOL>d['<STR_LIT>'] = dist_string.lower().strip()<EOL>try:<EOL><INDENT>d['<STR_LIT>'] = (package_map.INSTALL_TYPE_MAP[dist_string.lower()])<EOL><DEDENT>except KeyError:<EOL><INDENT>raise Exception("<STR_LIT>" % dist_string)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>return d<EOL><DEDENT>res = shutit.match_string(self.pexpect_child.before, r'<STR_LIT>')<EOL>if isinstance(res, str):<EOL><INDENT>version_string = res<EOL>d['<STR_LIT>'] = version_string<EOL><DEDENT>return d<EOL>
Get distro information from lsb_release.
f10349:c0:m23
def get_url(self,<EOL>filename,<EOL>locations,<EOL>command='<STR_LIT>',<EOL>timeout=shutit_global.shutit_global_object.default_timeout,<EOL>fail_on_empty_before=True,<EOL>record_command=True,<EOL>exit_values=None,<EOL>retry=<NUM_LIT:3>,<EOL>note=None,<EOL>loglevel=logging.DEBUG):
shutit = self.shutit<EOL>shutit.handle_note(note)<EOL>if not locations or not isinstance(locations, list):<EOL><INDENT>raise ShutItFailException('<STR_LIT>')<EOL><DEDENT>retry_orig = retry<EOL>if not self.command_available(command):<EOL><INDENT>self.install('<STR_LIT>')<EOL>if not self.command_available('<STR_LIT>'):<EOL><INDENT>self.install('<STR_LIT>')<EOL>command = '<STR_LIT>'<EOL>if not self.command_available('<STR_LIT>'):<EOL><INDENT>shutit.fail('<STR_LIT>') <EOL><DEDENT><DEDENT><DEDENT>for location in locations:<EOL><INDENT>retry = retry_orig<EOL>if location[-<NUM_LIT:1>] == '<STR_LIT:/>':<EOL><INDENT>location = location[<NUM_LIT:0>:-<NUM_LIT:1>]<EOL><DEDENT>while retry >= <NUM_LIT:0>:<EOL><INDENT>send = command + '<STR_LIT:U+0020>' + location + '<STR_LIT:/>' + filename + '<STR_LIT>' + filename<EOL>self.send(ShutItSendSpec(self,<EOL>send=send,<EOL>check_exit=False,<EOL>expect=self.default_expect,<EOL>timeout=timeout,<EOL>fail_on_empty_before=fail_on_empty_before,<EOL>record_command=record_command,<EOL>echo=False,<EOL>loglevel=loglevel,<EOL>ignore_background=True))<EOL>if retry == <NUM_LIT:0>:<EOL><INDENT>self.check_last_exit_values(send,<EOL>expect=self.default_expect,<EOL>exit_values=exit_values,<EOL>retbool=False)<EOL><DEDENT>elif not self.check_last_exit_values(send,<EOL>expect=self.default_expect,<EOL>exit_values=exit_values,<EOL>retbool=True):<EOL><INDENT>shutit.log('<STR_LIT>' + send + '<STR_LIT>', level=logging.DEBUG)<EOL>retry -= <NUM_LIT:1><EOL>continue<EOL><DEDENT>shutit.handle_note_after(note=note)<EOL>return True<EOL><DEDENT><DEDENT>return False<EOL>
Handles the getting of a url for you. Example: get_url('somejar.jar', ['ftp://loc.org','http://anotherloc.com/jars']) @param filename: name of the file to download @param locations: list of URLs whence the file can be downloaded @param command: program to use to download the file (Default: wget) @param timeout: See send() @param fail_on_empty_before: See send() @param record_command: See send() @param exit_values: See send() @param retry: How many times to retry the download in case of failure. Default: 3 @param note: See send() @type filename: string @type locations: list of strings @type retry: integer @return: True if the download was completed successfully, False otherwise. @rtype: boolean
f10349:c0:m24
def user_exists(self,<EOL>user,<EOL>note=None,<EOL>loglevel=logging.DEBUG):
shutit = self.shutit<EOL>shutit.handle_note(note)<EOL>exists = False<EOL>if user == '<STR_LIT>':<EOL><INDENT>return exists<EOL><DEDENT>ret = self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>' % user,<EOL>expect=['<STR_LIT>', '<STR_LIT>'],<EOL>echo=False,<EOL>loglevel=loglevel,<EOL>ignore_background=True))<EOL>if ret:<EOL><INDENT>exists = True<EOL><DEDENT>self.expect(self.default_expect)<EOL>shutit.handle_note_after(note=note)<EOL>return exists<EOL>
Returns true if the specified username exists. @param user: username to check for @param note: See send() @type user: string @rtype: boolean
f10349:c0:m25
def package_installed(self,<EOL>package,<EOL>note=None,<EOL>echo=False,<EOL>loglevel=logging.DEBUG):
shutit = self.shutit<EOL>shutit.handle_note(note)<EOL>if self.current_environment.install_type == '<STR_LIT>':<EOL><INDENT>return self.send_and_get_output('<STR_LIT>' + package + """<STR_LIT>""", loglevel=loglevel, echo=echo) == '<STR_LIT:1>'<EOL><DEDENT>elif self.current_environment.install_type == '<STR_LIT>':<EOL><INDENT>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>' + package + '<STR_LIT>',<EOL>echo=echo,<EOL>check_exit=False,<EOL>loglevel=loglevel,<EOL>ignore_background=True))<EOL>return self.check_last_exit_values('<STR_LIT>',retbool=True)<EOL><DEDENT>return False<EOL>
Returns True if we can be sure the package is installed. @param package: Package as a string, eg 'wget'. @param note: See send() @rtype: boolean
f10349:c0:m26
def is_shutit_installed(self,<EOL>module_id,<EOL>note=None,<EOL>loglevel=logging.DEBUG):
<EOL>shutit = self.shutit<EOL>shutit.handle_note(note)<EOL>if not self.current_environment.modules_recorded_cache_valid:<EOL><INDENT>if self.file_exists(shutit_global.shutit_global_object.shutit_state_dir_build_db_dir + '<STR_LIT>',directory=True):<EOL><INDENT>tmpid = shutit_util.random_id()<EOL>cmd = '<STR_LIT>' + shutit_global.shutit_global_object.shutit_state_dir_build_db_dir + r"""<STR_LIT>""" + shutit_global.shutit_global_object.shutit_state_dir_build_db_dir + r"""<STR_LIT>""" + shutit_global.shutit_global_object.shutit_state_dir_build_db_dir + '<STR_LIT:/>' + tmpid<EOL>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT:U+0020>' + cmd,<EOL>echo=False,<EOL>loglevel=loglevel,<EOL>ignore_background=True))<EOL>built = self.send_and_get_output('<STR_LIT>' + shutit_global.shutit_global_object.shutit_state_dir_build_db_dir + '<STR_LIT:/>' + tmpid,<EOL>echo=False,<EOL>loglevel=loglevel).strip()<EOL>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>' + shutit_global.shutit_global_object.shutit_state_dir_build_db_dir + '<STR_LIT:/>' + tmpid,<EOL>echo=False,<EOL>loglevel=loglevel,<EOL>ignore_background=True))<EOL>built_list = built.split('<STR_LIT:\r\n>')<EOL>self.current_environment.modules_recorded = built_list<EOL><DEDENT>self.current_environment.modules_recorded_cache_valid = True<EOL><DEDENT>shutit.handle_note_after(note=note)<EOL>return module_id in self.current_environment.modules_recorded or module_id in self.current_environment.modules_installed<EOL>
Helper proc to determine whether shutit has installed already here by placing a file in the db. @param module_id: Identifying string of shutit module @param note: See send()
f10349:c0:m28
def ls(self,<EOL>directory,<EOL>note=None,<EOL>loglevel=logging.DEBUG):
shutit = self.shutit<EOL>shutit.handle_note(note)<EOL>if not self.file_exists(directory,directory=True):<EOL><INDENT>shutit.fail('<STR_LIT>' + directory + '<STR_LIT>', throw_exception=False) <EOL><DEDENT>files = self.send_and_get_output('<STR_LIT>' + directory,<EOL>echo=False,<EOL>loglevel=loglevel,<EOL>fail_on_empty_before=False)<EOL>files = files.split('<STR_LIT:U+0020>')<EOL>files = filter(bool, files)<EOL>files = [_file.strip() for _file in files]<EOL>f = []<EOL>for _file in files:<EOL><INDENT>spl = _file.split('<STR_LIT:\r>')<EOL>f = f + spl<EOL><DEDENT>files = f<EOL>files = [_file.strip() for _file in files]<EOL>shutit.handle_note_after(note=note)<EOL>return files<EOL>
Helper proc to list files in a directory @param directory: directory to list. If the directory doesn't exist, shutit.fail() is called (i.e. the build fails.) @param note: See send() @type directory: string @rtype: list of strings
f10349:c0:m29
def install(self,<EOL>package,<EOL>options=None,<EOL>timeout=shutit_global.shutit_global_object.default_timeout,<EOL>force=False,<EOL>check_exit=True,<EOL>echo=None,<EOL>reinstall=False,<EOL>run_in_background=False,<EOL>ignore_background=False,<EOL>block_other_commands=True,<EOL>note=None,<EOL>loglevel=logging.INFO):
shutit = self.shutit<EOL>if package.find('<STR_LIT:U+0020>') != -<NUM_LIT:1>:<EOL><INDENT>ok = True<EOL>for p in package.split('<STR_LIT:U+0020>'):<EOL><INDENT>if not self.install(p,options,timeout,force,check_exit,reinstall,note):<EOL><INDENT>ok = False<EOL><DEDENT><DEDENT>return ok<EOL><DEDENT>if note != None:<EOL><INDENT>shutit.handle_note('<STR_LIT>' + package + '<STR_LIT:\n>' + note)<EOL><DEDENT>shutit.log('<STR_LIT>' + package, level=loglevel)<EOL>if options is None: options = {}<EOL>install_type = self.current_environment.install_type<EOL>if install_type == '<STR_LIT:src>':<EOL><INDENT>return True<EOL><DEDENT>elif install_type == '<STR_LIT:none>':<EOL><INDENT>shutit.fail('<STR_LIT>') <EOL><DEDENT>opts = '<STR_LIT>'<EOL>cmd = '<STR_LIT>'<EOL>if self.package_installed(package):<EOL><INDENT>shutit.log(package + '<STR_LIT>', level=loglevel)<EOL>return True<EOL><DEDENT>if install_type == '<STR_LIT>':<EOL><INDENT>if not shutit.get_current_shutit_pexpect_session_environment().build['<STR_LIT>'] and self.whoami() == '<STR_LIT:root>':<EOL><INDENT>opts = '<STR_LIT>'<EOL>if self.shutit.loglevel > logging.DEBUG:<EOL><INDENT>opts += '<STR_LIT>'<EOL><DEDENT>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>' + opts + '<STR_LIT>',<EOL>loglevel=logging.INFO,<EOL>echo=echo,<EOL>run_in_background=False,<EOL>ignore_background=False,<EOL>block_other_commands=True))<EOL>shutit.get_current_shutit_pexpect_session_environment().build['<STR_LIT>'] = True<EOL><DEDENT>cmd += '<STR_LIT>'<EOL>if '<STR_LIT>' in options:<EOL><INDENT>opts = options['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>opts = '<STR_LIT>'<EOL>if self.shutit.loglevel > logging.DEBUG:<EOL><INDENT>opts += '<STR_LIT>'<EOL><DEDENT>if force:<EOL><INDENT>opts += '<STR_LIT>'<EOL><DEDENT>if reinstall:<EOL><INDENT>opts += '<STR_LIT>'<EOL><DEDENT><DEDENT><DEDENT>elif install_type == '<STR_LIT>':<EOL><INDENT>cmd += '<STR_LIT>'<EOL>if '<STR_LIT>' in options:<EOL><INDENT>opts = options['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>if self.shutit.loglevel > logging.DEBUG:<EOL><INDENT>opts += '<STR_LIT>'<EOL><DEDENT>opts += '<STR_LIT>'<EOL><DEDENT>if reinstall:<EOL><INDENT>opts += '<STR_LIT>'<EOL><DEDENT><DEDENT>elif install_type == '<STR_LIT>':<EOL><INDENT>cmd += '<STR_LIT>'<EOL>if '<STR_LIT>' in options:<EOL><INDENT>opts = options['<STR_LIT>']<EOL><DEDENT><DEDENT>elif install_type == '<STR_LIT>':<EOL><INDENT>cmd += '<STR_LIT>'<EOL>if '<STR_LIT>' in options:<EOL><INDENT>opts = options['<STR_LIT>']<EOL><DEDENT><DEDENT>elif install_type == '<STR_LIT>':<EOL><INDENT>cmd += '<STR_LIT>'<EOL>if '<STR_LIT>' in options:<EOL><INDENT>opts = options['<STR_LIT>']<EOL><DEDENT><DEDENT>elif install_type == '<STR_LIT>':<EOL><INDENT>cmd += '<STR_LIT>'<EOL>if '<STR_LIT>' in options:<EOL><INDENT>opts = options['<STR_LIT>']<EOL><DEDENT><DEDENT>elif install_type == '<STR_LIT>':<EOL><INDENT>cmd += '<STR_LIT>'<EOL>if '<STR_LIT>' in options:<EOL><INDENT>opts = options['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>opts += '<STR_LIT>'<EOL><DEDENT><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT>package = package_map.map_packages(self, package, self.current_environment.install_type)<EOL>if package.strip() != '<STR_LIT>':<EOL><INDENT>fails = <NUM_LIT:0><EOL>while True:<EOL><INDENT>pw = self.get_sudo_pass_if_needed(shutit, ignore_brew=True)<EOL>if pw != '<STR_LIT>':<EOL><INDENT>cmd = '<STR_LIT>' + cmd<EOL>res = self.multisend(ShutItSendSpec(self,<EOL>send='<STR_LIT>' % (cmd, opts, package),<EOL>send_dict={'<STR_LIT>':[pw, True]},<EOL>expect=['<STR_LIT>',self.default_expect],<EOL>timeout=timeout,<EOL>check_exit=False,<EOL>loglevel=loglevel,<EOL>echo=echo,<EOL>secret=True))<EOL>if res == -<NUM_LIT:1>:<EOL><INDENT>break<EOL><DEDENT>shutit.log('<STR_LIT>' + str(res), level=logging.DEBUG)<EOL><DEDENT>else:<EOL><INDENT>res = self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>' % (cmd, opts, package),<EOL>expect=['<STR_LIT>',self.default_expect],<EOL>timeout=timeout,<EOL>check_exit=False,<EOL>loglevel=loglevel,<EOL>echo=echo,<EOL>ignore_background=ignore_background,<EOL>run_in_background=run_in_background,<EOL>block_other_commands=block_other_commands))<EOL>if res == -<NUM_LIT:1>:<EOL><INDENT>break<EOL><DEDENT>shutit.log('<STR_LIT>' + str(res), level=logging.DEBUG)<EOL><DEDENT>if res == <NUM_LIT:1>:<EOL><INDENT>break<EOL><DEDENT>else:<EOL><INDENT>fails += <NUM_LIT:1><EOL>if fails >= <NUM_LIT:3>:<EOL><INDENT>shutit.pause_point('<STR_LIT>' + package)<EOL>return False<EOL><DEDENT><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>shutit.log('<STR_LIT>', level=logging.DEBUG)<EOL><DEDENT>shutit.log('<STR_LIT>', level=logging.DEBUG)<EOL>self.reset_terminal()<EOL>shutit.handle_note_after(note=note)<EOL>return True<EOL>
Distro-independent install function. Takes a package name and runs the relevant install function. @param package: Package to install, which is run through package_map @param timeout: Timeout (s) to wait for finish of install. Defaults to 3600. @param options: Dictionary for specific options per install tool. Overrides any arguments passed into this function. @param force: Force if necessary. Defaults to False @param check_exit: If False, failure to install is ok (default True) @param reinstall: Advise a reinstall where possible (default False) @param note: See send() @type package: string @type timeout: integer @type options: dict @type force: boolean @type check_exit: boolean @type reinstall: boolean @return: True if all ok (ie it's installed), else False. @rtype: boolean
f10349:c0:m30
def reset_terminal(self):
shutit = self.shutit<EOL>shutit.log('<STR_LIT>', level=logging.DEBUG)<EOL>self.pexpect_child.sendline()<EOL>time.sleep(<NUM_LIT:0.1>)<EOL>try:<EOL><INDENT>self.pexpect_child.read_nonblocking(size=<NUM_LIT>,timeout=<NUM_LIT:1>)<EOL><DEDENT>except pexpect.TIMEOUT:<EOL><INDENT>pass<EOL><DEDENT>time.sleep(<NUM_LIT:0.1>)<EOL>self.pexpect_child.sendline()<EOL>time.sleep(<NUM_LIT:0.5>)<EOL>self.pexpect_child.read_nonblocking(size=<NUM_LIT:1000>,timeout=<NUM_LIT:1>)<EOL>time.sleep(<NUM_LIT:0.1>)<EOL>self.pexpect_child.sendline()<EOL>time.sleep(<NUM_LIT:0.5>)<EOL>a = self.pexpect_child.read_nonblocking(size=<NUM_LIT:1000>,timeout=<NUM_LIT:1>)<EOL>time.sleep(<NUM_LIT:0.1>)<EOL>self.pexpect_child.sendline()<EOL>time.sleep(<NUM_LIT:0.5>)<EOL>b = self.pexpect_child.read_nonblocking(size=<NUM_LIT:1000>,timeout=<NUM_LIT:1>)<EOL>ld = self.levenshtein_distance(a,b)<EOL>if len(a) == <NUM_LIT:0>:<EOL><INDENT>return False<EOL><DEDENT>if float(ld)/len(a) < <NUM_LIT>:<EOL><INDENT>return True<EOL><DEDENT>return False<EOL>
Resets the terminal to as good a state as we can try. Tries to ensure that we have 'expect'ed the last prompt seen. TODO: see how https://github.com/noahspurrier/pexpect/blob/master/pxssh.py handles this
f10349:c0:m31
def levenshtein_distance(self, a,b):
n, m = len(a), len(b)<EOL>if n > m:<EOL><INDENT>a,b = b,a<EOL>n,m = m,n<EOL><DEDENT>current = range(n+<NUM_LIT:1>)<EOL>for i in range(<NUM_LIT:1>,m+<NUM_LIT:1>):<EOL><INDENT>previous, current = current, [i]+[<NUM_LIT:0>]*n<EOL>for j in range(<NUM_LIT:1>,n+<NUM_LIT:1>):<EOL><INDENT>add, delete = previous[j]+<NUM_LIT:1>, current[j-<NUM_LIT:1>]+<NUM_LIT:1><EOL>change = previous[j-<NUM_LIT:1>]<EOL>if a[j-<NUM_LIT:1>] != b[i-<NUM_LIT:1>]:<EOL><INDENT>change = change + <NUM_LIT:1><EOL><DEDENT>current[j] = min(add, delete, change)<EOL><DEDENT><DEDENT>return current[n]<EOL>
This calculates the Levenshtein distance between a and b.
f10349:c0:m32
def get_memory(self, note=None):
shutit = self.shutit<EOL>shutit.handle_note(note)<EOL>if self.current_environment.distro == '<STR_LIT>':<EOL><INDENT>memavail = self.send_and_get_output("""<STR_LIT>""",<EOL>timeout=<NUM_LIT:3>,<EOL>echo=False)<EOL>memavail = int(memavail)<EOL>memavail *= <NUM_LIT:4><EOL><DEDENT>else:<EOL><INDENT>memavail = self.send_and_get_output("""<STR_LIT>""",<EOL>timeout=<NUM_LIT:3>,<EOL>echo=False)<EOL>if memavail == '<STR_LIT>':<EOL><INDENT>memavail = self.send_and_get_output("""<STR_LIT>""",<EOL>timeout=<NUM_LIT:3>,<EOL>echo=False)<EOL><DEDENT>memavail = int(memavail)<EOL><DEDENT>shutit.handle_note_after(note=note)<EOL>return memavail<EOL>
Returns memory available for use in k as an int
f10349:c0:m33
def remove(self,<EOL>package,<EOL>echo=None,<EOL>options=None,<EOL>timeout=shutit_global.shutit_global_object.default_timeout,<EOL>note=None):
<EOL>shutit = self.shutit<EOL>if note != None:<EOL><INDENT>shutit.handle_note('<STR_LIT>' + package + '<STR_LIT:\n>' + note)<EOL><DEDENT>if options is None: options = {}<EOL>install_type = self.current_environment.install_type<EOL>cmd = '<STR_LIT>'<EOL>if install_type == '<STR_LIT:src>':<EOL><INDENT>return True<EOL><DEDENT>elif install_type == '<STR_LIT:none>':<EOL><INDENT>shutit.fail('<STR_LIT>') <EOL><DEDENT>if install_type == '<STR_LIT>':<EOL><INDENT>cmd += '<STR_LIT>'<EOL>opts = options['<STR_LIT>'] if '<STR_LIT>' in options else '<STR_LIT>'<EOL><DEDENT>elif install_type == '<STR_LIT>':<EOL><INDENT>cmd += '<STR_LIT>'<EOL>opts = options['<STR_LIT>'] if '<STR_LIT>' in options else '<STR_LIT>'<EOL><DEDENT>elif install_type == '<STR_LIT>':<EOL><INDENT>cmd += '<STR_LIT>'<EOL>if '<STR_LIT>' in options:<EOL><INDENT>opts = options['<STR_LIT>']<EOL><DEDENT><DEDENT>elif install_type == '<STR_LIT>':<EOL><INDENT>cmd += '<STR_LIT>'<EOL>opts = options['<STR_LIT>'] if '<STR_LIT>' in options else '<STR_LIT>'<EOL><DEDENT>elif install_type == '<STR_LIT>':<EOL><INDENT>cmd += '<STR_LIT>'<EOL>if '<STR_LIT>' in options:<EOL><INDENT>opts = options['<STR_LIT>']<EOL><DEDENT><DEDENT>elif install_type == '<STR_LIT>':<EOL><INDENT>cmd += '<STR_LIT>'<EOL>if '<STR_LIT>' in options:<EOL><INDENT>opts = options['<STR_LIT>']<EOL><DEDENT><DEDENT>elif install_type == '<STR_LIT>':<EOL><INDENT>cmd += '<STR_LIT>'<EOL>if '<STR_LIT>' in options:<EOL><INDENT>opts = options['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>opts += '<STR_LIT>'<EOL><DEDENT><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT>package = package_map.map_packages(self, package, self.current_environment.install_type)<EOL>pw = self.get_sudo_pass_if_needed(shutit, ignore_brew=True)<EOL>if pw != '<STR_LIT>':<EOL><INDENT>cmd = '<STR_LIT>' + cmd<EOL>res = self.multisend(ShutItSendSpec(self,<EOL>send='<STR_LIT>' % (cmd, opts, package),<EOL>send_dict={'<STR_LIT>':[pw, True]},<EOL>timeout=timeout,<EOL>exit_values=['<STR_LIT:0>','<STR_LIT:100>'],<EOL>echo=echo,<EOL>secret=True))<EOL>if res == -<NUM_LIT:1>:<EOL><INDENT>assert False, shutit_util.print_debug()<EOL><DEDENT><DEDENT>else:<EOL><INDENT>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>' % (cmd, opts, package),<EOL>timeout=timeout,<EOL>exit_values=['<STR_LIT:0>','<STR_LIT:100>'],<EOL>echo=echo,<EOL>ignore_background=False,<EOL>run_in_background=False,<EOL>block_other_commands=True))<EOL><DEDENT>shutit.handle_note_after(note=note)<EOL>return True<EOL>
Distro-independent remove function. Takes a package name and runs relevant remove function. @param package: Package to remove, which is run through package_map. @param options: Dict of options to pass to the remove command, mapped by install_type. @param timeout: See send(). Default: 3600 @param note: See send() @return: True if all ok (i.e. the package was successfully removed), False otherwise. @rtype: boolean
f10349:c0:m34
def send_and_match_output(self,<EOL>send,<EOL>matches,<EOL>retry=<NUM_LIT:3>,<EOL>strip=True,<EOL>note=None,<EOL>echo=False,<EOL>loglevel=logging.DEBUG):
shutit = self.shutit<EOL>shutit.handle_note(note)<EOL>shutit.log('<STR_LIT>' + send + '<STR_LIT>' + str(matches), level=logging.INFO)<EOL>echo = shutit.get_echo_override(echo)<EOL>output = self.send_and_get_output(send,<EOL>retry=retry,<EOL>strip=strip,<EOL>echo=echo,<EOL>loglevel=loglevel)<EOL>if isinstance(matches, str):<EOL><INDENT>matches = [matches]<EOL><DEDENT>shutit.handle_note_after(note=note)<EOL>for match in matches:<EOL><INDENT>if shutit.match_string(output, match) != None:<EOL><INDENT>shutit.log('<STR_LIT>', level=logging.DEBUG)<EOL>return True<EOL><DEDENT><DEDENT>shutit.log('<STR_LIT>', level=logging.DEBUG)<EOL>return False<EOL>
Returns true if the output of the command matches any of the strings in the matches list of regexp strings. Handles matching on a per-line basis and does not cross lines. @param send: See send() @param matches: String - or list of strings - of regexp(s) to check @param retry: Number of times to retry command (default 3) @param strip: Whether to strip output (defaults to True) @param note: See send() @type send: string @type matches: list @type retry: integer @type strip: boolean
f10349:c0:m35
def send_and_get_output(self,<EOL>send,<EOL>timeout=None,<EOL>retry=<NUM_LIT:3>,<EOL>strip=True,<EOL>preserve_newline=False,<EOL>note=None,<EOL>record_command=True,<EOL>echo=None,<EOL>fail_on_empty_before=True,<EOL>check_sudo=True,<EOL>nonewline=False,<EOL>ignore_background=False,<EOL>filter_backspaces=True,<EOL>loglevel=logging.INFO):
shutit = self.shutit<EOL>shutit.handle_note(note, command=str(send))<EOL>shutit.log('<STR_LIT>' + send, level=loglevel)<EOL>echo = shutit.get_echo_override(echo)<EOL>send = shutit.get_send_command(send)<EOL>self.send(ShutItSendSpec(self,<EOL>send=send,<EOL>check_exit=False,<EOL>retry=retry,<EOL>echo=echo,<EOL>timeout=timeout,<EOL>record_command=record_command,<EOL>fail_on_empty_before=fail_on_empty_before,<EOL>check_sudo=check_sudo,<EOL>nonewline=nonewline,<EOL>loglevel=loglevel,<EOL>ignore_background=ignore_background))<EOL>before = self.pexpect_child.before<EOL>if before:<EOL><INDENT>preserve_newline = bool(preserve_newline and before[-<NUM_LIT:1>] == '<STR_LIT:\n>')<EOL><DEDENT>if filter_backspaces:<EOL><INDENT>lines = before.split('<STR_LIT:\n>')<EOL>lines = filter(lambda x: x.find('<STR_LIT>') == -<NUM_LIT:1>, lines)<EOL>before = '<STR_LIT:\n>'.join(lines)<EOL><DEDENT>before = before.strip()<EOL>send = send.strip()<EOL>shutit.log('<STR_LIT>' + before + '<STR_LIT>' + send, level=logging.DEBUG)<EOL>if strip:<EOL><INDENT>ansi_escape = re.compile(r'<STR_LIT>')<EOL>string_with_termcodes = before.strip()<EOL>string_without_termcodes = ansi_escape.sub('<STR_LIT>', string_with_termcodes)<EOL>string_without_termcodes_stripped_no_cr = string_without_termcodes.replace('<STR_LIT>','<STR_LIT>')<EOL>string_without_termcodes_stripped_no_cr = string_without_termcodes.replace('<STR_LIT:\r>','<STR_LIT>')<EOL>if preserve_newline:<EOL><INDENT>before = string_without_termcodes_stripped_no_cr + '<STR_LIT:\n>'<EOL><DEDENT>else:<EOL><INDENT>before = string_without_termcodes_stripped_no_cr<EOL><DEDENT><DEDENT>else:<EOL><INDENT>before = before<EOL><DEDENT>if before.startswith(send):<EOL><INDENT>before = before[len(send):]<EOL>before = before.strip()<EOL><DEDENT>shutit.log('<STR_LIT>' + before, level=logging.DEBUG)<EOL>shutit.log('<STR_LIT>' + before, level=logging.DEBUG)<EOL>if shutit_global.shutit_global_object.ispy3:<EOL><INDENT>shutit.log('<STR_LIT>' + str(base64.b64encode(bytes(before,shutit_global.shutit_global_object.default_encoding))), level=logging.DEBUGV)<EOL><DEDENT>else:<EOL><INDENT>shutit.log('<STR_LIT>' + base64.b64encode(before), level=logging.DEBUGV)<EOL><DEDENT>before = before.replace('<STR_LIT>','<STR_LIT>')<EOL>before = before.split('<STR_LIT>')[<NUM_LIT:0>].strip()<EOL>if shutit_global.shutit_global_object.ispy3:<EOL><INDENT>shutit.log('<STR_LIT>' + str(base64.b64encode(bytes(before,shutit_global.shutit_global_object.default_encoding))), level=logging.DEBUGV)<EOL><DEDENT>else:<EOL><INDENT>shutit.log('<STR_LIT>' + base64.b64encode(bytes(before)), level=logging.DEBUGV)<EOL><DEDENT>shutit.handle_note_after(note=note)<EOL>return before<EOL>
Returns the output of a command run. send() is called, and exit is not checked. @param send: See send() @param retry: Number of times to retry command (default 3) @param strip: Whether to strip output (defaults to True). Strips whitespace and ansi terminal codes @param note: See send() @param echo: See send() @type retry: integer @type strip: boolean
f10349:c0:m36
def get_env_pass(self,user=None,msg=None,note=None):
shutit = self.shutit<EOL>shutit.handle_note(note)<EOL>user = user or self.whoami()<EOL>pw = '<STR_LIT>'<EOL>if self.current_environment.distro == '<STR_LIT>':<EOL><INDENT>return pw<EOL><DEDENT>if user not in self.current_environment.users.keys():<EOL><INDENT>self.current_environment.users.update({user:None})<EOL><DEDENT>if not self.current_environment.users[user] and user != '<STR_LIT:root>':<EOL><INDENT>msg = msg or '<STR_LIT>' + user<EOL>pw = shutit_util.get_input(msg,ispass=True)<EOL>self.current_environment.users[user] = pw<EOL>shutit_global.shutit_global_object.secret_words_set.add(self.current_environment.users[user])<EOL><DEDENT>return pw<EOL>
Gets a password from the user if one is not already recorded for this environment. @param user: username we are getting password for @param msg: message to put out there
f10349:c0:m37
def whoarewe(self,<EOL>note=None,<EOL>loglevel=logging.DEBUG):
shutit = self.shutit<EOL>shutit.handle_note(note)<EOL>res = self.send_and_get_output('<STR_LIT>',<EOL>echo=False,<EOL>loglevel=loglevel).strip()<EOL>shutit.handle_note_after(note=note)<EOL>return res<EOL>
Returns the current group. @param note: See send() @return: the first group found @rtype: string
f10349:c0:m38
def get_distro_info(self, loglevel=logging.DEBUG):
shutit = self.shutit<EOL>install_type = '<STR_LIT>'<EOL>distro = '<STR_LIT>'<EOL>distro_version = '<STR_LIT>'<EOL>if shutit.build['<STR_LIT>'] != '<STR_LIT>':<EOL><INDENT>key = shutit.build['<STR_LIT>']<EOL>distro = shutit.build['<STR_LIT>']<EOL>install_type = package_map.INSTALL_TYPE_MAP[key]<EOL>distro_version = '<STR_LIT>'<EOL>if install_type == '<STR_LIT>' and shutit.build['<STR_LIT>'] in ('<STR_LIT>','<STR_LIT>'):<EOL><INDENT>if not self.command_available('<STR_LIT>'):<EOL><INDENT>if not shutit.get_current_shutit_pexpect_session_environment().build['<STR_LIT>'] and self.whoami() == '<STR_LIT:root>':<EOL><INDENT>shutit.get_current_shutit_pexpect_session_environment().build['<STR_LIT>'] = True<EOL>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>',<EOL>loglevel=loglevel,<EOL>ignore_background=True))<EOL><DEDENT><DEDENT>d = self.lsb_release()<EOL>install_type = d['<STR_LIT>']<EOL>distro = d['<STR_LIT>']<EOL>distro_version = d['<STR_LIT>']<EOL><DEDENT>elif install_type == '<STR_LIT>' and shutit.build['<STR_LIT>'] in ('<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>if self.file_exists('<STR_LIT>'):<EOL><INDENT>output = self.send_and_get_output('<STR_LIT>',<EOL>echo=False,<EOL>loglevel=loglevel)<EOL>if re.match('<STR_LIT>', output.lower()) or re.match('<STR_LIT>', output.lower()) or re.match('<STR_LIT>', output.lower()) or True:<EOL><INDENT>self.send_and_match_output('<STR_LIT>',<EOL>'<STR_LIT>',<EOL>loglevel=loglevel)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if not self.command_available('<STR_LIT>'):<EOL><INDENT>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>',<EOL>loglevel=loglevel,<EOL>ignore_background=True))<EOL><DEDENT><DEDENT>install_type = d['<STR_LIT>']<EOL>distro = d['<STR_LIT>']<EOL>distro_version = d['<STR_LIT>']<EOL><DEDENT>elif install_type == '<STR_LIT>' and shutit.build['<STR_LIT>'] in ('<STR_LIT>','<STR_LIT>'):<EOL><INDENT>if not shutit.get_current_shutit_pexpect_session_environment().build['<STR_LIT>'] and self.whoami() == '<STR_LIT:root>':<EOL><INDENT>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>',<EOL>ignore_background=True,<EOL>loglevel=logging.INFO))<EOL>shutit.get_current_shutit_pexpect_session_environment().build['<STR_LIT>'] = True<EOL><DEDENT>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>',<EOL>ignore_background=True,<EOL>loglevel=loglevel))<EOL>install_type = '<STR_LIT>'<EOL>distro = '<STR_LIT>'<EOL>distro_version = '<STR_LIT:1.0>'<EOL><DEDENT>elif install_type == '<STR_LIT>' and shutit.build['<STR_LIT>'] in ('<STR_LIT>','<STR_LIT>') and self.whoami() == '<STR_LIT:root>':<EOL><INDENT>if not shutit.get_current_shutit_pexpect_session_environment().build['<STR_LIT>']:<EOL><INDENT>shutit.get_current_shutit_pexpect_session_environment().build['<STR_LIT>'] = True<EOL>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>',<EOL>ignore_background=True,<EOL>loglevel=logging.INFO))<EOL><DEDENT>install_type = d['<STR_LIT>']<EOL>distro = d['<STR_LIT>']<EOL>distro_version = '<STR_LIT:1.0>'<EOL><DEDENT>elif install_type == '<STR_LIT>' and shutit.build['<STR_LIT>'] in ('<STR_LIT>','<STR_LIT>'):<EOL><INDENT>if not shutit.get_current_shutit_pexpect_session_environment().build['<STR_LIT>'] and self.whoami() == '<STR_LIT:root>':<EOL><INDENT>pass<EOL><DEDENT>install_type = '<STR_LIT>'<EOL>distro = '<STR_LIT>'<EOL>distro_version = '<STR_LIT:1.0>'<EOL><DEDENT>elif install_type == '<STR_LIT>' and shutit.build['<STR_LIT>'] in ('<STR_LIT>','<STR_LIT>'):<EOL><INDENT>distro = '<STR_LIT>'<EOL>distro_version = '<STR_LIT:1.0>'<EOL><DEDENT><DEDENT>elif self.command_available('<STR_LIT>'):<EOL><INDENT>d = self.lsb_release()<EOL>install_type = d['<STR_LIT>']<EOL>distro = d['<STR_LIT>']<EOL>distro_version = d['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>issue_output = self.send_and_get_output('<STR_LIT>',<EOL>echo=False,<EOL>ignore_background=True,<EOL>loglevel=loglevel).lower()<EOL>if not re.match('<STR_LIT>',issue_output):<EOL><INDENT>for key in package_map.INSTALL_TYPE_MAP:<EOL><INDENT>if issue_output.find(key) != -<NUM_LIT:1>:<EOL><INDENT>distro = key<EOL>install_type = package_map.INSTALL_TYPE_MAP[key]<EOL>break<EOL><DEDENT><DEDENT><DEDENT>elif self.file_exists('<STR_LIT>'):<EOL><INDENT>distro = '<STR_LIT>'<EOL>install_type = '<STR_LIT>'<EOL><DEDENT>if install_type == '<STR_LIT>' or distro == '<STR_LIT>':<EOL><INDENT>if self.file_exists('<STR_LIT>'):<EOL><INDENT>os_name = self.send_and_get_output('<STR_LIT>',<EOL>echo=False,<EOL>ignore_background=True,<EOL>loglevel=loglevel).lower()<EOL>if os_name.find('<STR_LIT>') != -<NUM_LIT:1>:<EOL><INDENT>distro = '<STR_LIT>'<EOL>install_type = '<STR_LIT>'<EOL><DEDENT>elif os_name.find('<STR_LIT>') != -<NUM_LIT:1>:<EOL><INDENT>distro = '<STR_LIT>'<EOL>install_type = '<STR_LIT>'<EOL><DEDENT>elif os_name.find('<STR_LIT>') != -<NUM_LIT:1>:<EOL><INDENT>distro = '<STR_LIT>'<EOL>install_type = '<STR_LIT>'<EOL><DEDENT>elif os_name.find('<STR_LIT>') != -<NUM_LIT:1>:<EOL><INDENT>distro = '<STR_LIT>'<EOL>install_type = '<STR_LIT>'<EOL><DEDENT>elif os_name.find('<STR_LIT>') != -<NUM_LIT:1>:<EOL><INDENT>distro = '<STR_LIT>'<EOL>install_type = '<STR_LIT>'<EOL><DEDENT><DEDENT>else:<EOL><INDENT>uname_output = self.send_and_get_output("<STR_LIT>",<EOL>echo=False,<EOL>ignore_background=True,<EOL>loglevel=loglevel)<EOL>if uname_output == '<STR_LIT>':<EOL><INDENT>distro = '<STR_LIT>'<EOL>install_type = '<STR_LIT>'<EOL>if not self.command_available('<STR_LIT>'):<EOL><INDENT>shutit.fail('<STR_LIT>') <EOL><DEDENT>if not self.file_exists('<STR_LIT>'):<EOL><INDENT>if self.whoami() != '<STR_LIT:root>':<EOL><INDENT>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>',<EOL>echo=False,<EOL>ignore_background=True,<EOL>loglevel=loglevel))<EOL><DEDENT>else:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>for package in ('<STR_LIT>','<STR_LIT>','<STR_LIT>','<STR_LIT>','<STR_LIT>','<STR_LIT>','<STR_LIT>','<STR_LIT>'):<EOL><INDENT>if self.send_and_get_output('<STR_LIT>' + package,<EOL>echo=False,<EOL>loglevel=loglevel) == '<STR_LIT>':<EOL><INDENT>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>' + package,<EOL>ignore_background=True,<EOL>loglevel=loglevel))<EOL><DEDENT><DEDENT>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>',<EOL>echo=False,<EOL>ignore_background=True,<EOL>loglevel=loglevel))<EOL><DEDENT>if uname_output[:<NUM_LIT:6>] == '<STR_LIT>':<EOL><INDENT>distro = '<STR_LIT>'<EOL>install_type = '<STR_LIT>'<EOL><DEDENT><DEDENT>if install_type == '<STR_LIT>' or distro == '<STR_LIT>':<EOL><INDENT>shutit.fail('<STR_LIT>' + '<STR_LIT>', shutit_pexpect_child=self.pexpect_child) <EOL><DEDENT><DEDENT>if install_type == '<STR_LIT>' and shutit.build['<STR_LIT>'] in ('<STR_LIT>','<STR_LIT>'):<EOL><INDENT>if not self.command_available('<STR_LIT>'):<EOL><INDENT>if not shutit.get_current_shutit_pexpect_session_environment().build['<STR_LIT>'] and self.whoami() == '<STR_LIT:root>':<EOL><INDENT>shutit.get_current_shutit_pexpect_session_environment().build['<STR_LIT>'] = True<EOL>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>',<EOL>loglevel=loglevel,<EOL>ignore_background=True))<EOL><DEDENT>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>',<EOL>loglevel=loglevel,<EOL>ignore_background=True))<EOL><DEDENT>d = self.lsb_release()<EOL>install_type = d['<STR_LIT>']<EOL>distro = d['<STR_LIT>']<EOL>distro_version = d['<STR_LIT>']<EOL><DEDENT>elif install_type == '<STR_LIT>' and shutit.build['<STR_LIT>'] in ('<STR_LIT>','<STR_LIT>'):<EOL><INDENT>if self.file_exists('<STR_LIT>'):<EOL><INDENT>output = self.send_and_get_output('<STR_LIT>',<EOL>echo=False,<EOL>loglevel=loglevel)<EOL>if re.match('<STR_LIT>', output.lower()) or re.match('<STR_LIT>', output.lower()) or re.match('<STR_LIT>', output.lower()) or True:<EOL><INDENT>self.send_and_match_output('<STR_LIT>',<EOL>'<STR_LIT>',<EOL>loglevel=loglevel)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if not self.command_available('<STR_LIT>'):<EOL><INDENT>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>',<EOL>ignore_background=True,<EOL>loglevel=loglevel))<EOL><DEDENT><DEDENT>d = self.lsb_release()<EOL>install_type = d['<STR_LIT>']<EOL>distro = d['<STR_LIT>']<EOL>distro_version = d['<STR_LIT>']<EOL><DEDENT>elif install_type == '<STR_LIT>' and shutit.build['<STR_LIT>'] in ('<STR_LIT>','<STR_LIT>'):<EOL><INDENT>if not shutit.get_current_shutit_pexpect_session_environment().build['<STR_LIT>'] and self.whoami() == '<STR_LIT:root>':<EOL><INDENT>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>',<EOL>ignore_background=True,<EOL>loglevel=logging.INFO))<EOL>shutit.get_current_shutit_pexpect_session_environment().build['<STR_LIT>'] = True<EOL><DEDENT>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>',<EOL>ignore_background=True,<EOL>loglevel=loglevel))<EOL>install_type = '<STR_LIT>'<EOL>distro = '<STR_LIT>'<EOL>distro_version = '<STR_LIT:1.0>'<EOL><DEDENT>elif install_type == '<STR_LIT>' and shutit.build['<STR_LIT>'] in ('<STR_LIT>','<STR_LIT>'):<EOL><INDENT>if not shutit.get_current_shutit_pexpect_session_environment().build['<STR_LIT>'] and self.whoami() == '<STR_LIT:root>':<EOL><INDENT>pass<EOL><DEDENT>install_type = '<STR_LIT>'<EOL>distro = '<STR_LIT>'<EOL>distro_version = '<STR_LIT:1.0>'<EOL><DEDENT><DEDENT>self.current_environment.install_type = install_type<EOL>self.current_environment.distro = distro<EOL>self.current_environment.distro_version = distro_version<EOL>return True<EOL>
Get information about which distro we are using, placing it in the environment object. Fails if distro could not be determined. Should be called with the container is started up, and uses as core info as possible. Note: if the install type is apt, it issues the following: - apt-get update - apt-get install -y -qq lsb-release
f10349:c0:m39
def multisend(self, sendspec):
shutit = self.shutit<EOL>shutit.handle_note(sendspec.note)<EOL>expect = sendspec.expect or self.default_expect<EOL>send_iteration = sendspec.send<EOL>expect_list = list(sendspec.send_dict)<EOL>n_breakout_items = <NUM_LIT:0><EOL>shutit.log('<STR_LIT>' + sendspec.send, level=logging.DEBUG)<EOL>if isinstance(expect, str):<EOL><INDENT>shutit.log('<STR_LIT>' + expect + '<STR_LIT>', level=logging.DEBUG)<EOL>expect_list.append(expect)<EOL>n_breakout_items = <NUM_LIT:1><EOL><DEDENT>elif isinstance(expect, list):<EOL><INDENT>shutit.log('<STR_LIT>' + str(expect) + '<STR_LIT>', level=logging.DEBUG)<EOL>for item in expect:<EOL><INDENT>expect_list.append(item)<EOL>n_breakout_items += <NUM_LIT:1><EOL><DEDENT><DEDENT>shutit.log('<STR_LIT>' + str(n_breakout_items), level=logging.DEBUG)<EOL>while True:<EOL><INDENT>res = self.send(ShutItSendSpec(self,<EOL>send=send_iteration,<EOL>expect=expect_list,<EOL>check_exit=sendspec.check_exit,<EOL>fail_on_empty_before=sendspec.fail_on_empty_before,<EOL>timeout=sendspec.timeout,<EOL>record_command=sendspec.record_command,<EOL>exit_values=sendspec.exit_values,<EOL>echo=self.shutit.get_echo_override(sendspec.echo),<EOL>escape=sendspec.escape,<EOL>secret=sendspec.secret,<EOL>check_sudo=sendspec.check_sudo,<EOL>nonewline=sendspec.nonewline,<EOL>ignore_background=sendspec.ignore_background,<EOL>run_in_background=False,<EOL>block_other_commands=True,<EOL>loglevel=sendspec.loglevel))<EOL>if res == -<NUM_LIT:1>:<EOL><INDENT>shutit.log('<STR_LIT>' + str(send_iteration), level=logging.INFO)<EOL>return -<NUM_LIT:1><EOL><DEDENT>if res >= len(expect_list) - n_breakout_items:<EOL><INDENT>break<EOL><DEDENT>else:<EOL><INDENT>next_send = sendspec.send_dict[expect_list[res]][<NUM_LIT:0>]<EOL>if next_send is None:<EOL><INDENT>shutit.log('<STR_LIT>', level=logging.WARNING)<EOL><DEDENT>remove_items = sendspec.send_dict[expect_list[res]][<NUM_LIT:1>]<EOL>send_iteration = next_send<EOL>if sendspec.remove_on_match and remove_items:<EOL><INDENT>shutit.log('<STR_LIT>' + expect_list[res] + '<STR_LIT>', level=logging.DEBUG)<EOL>if isinstance(expect, str):<EOL><INDENT>expect_list = [expect]<EOL><DEDENT>elif isinstance(expect, list):<EOL><INDENT>expect_list = expect<EOL><DEDENT><DEDENT><DEDENT><DEDENT>self.shutit.handle_note_after(note=sendspec.note)<EOL>return res<EOL>
Multisend. Same as send, except it takes multiple sends and expects in a dict that are processed while waiting for the end "expect" argument supplied. @param send: See send() @param send_dict: See ShutItSendSpec @param expect: See send() @param timeout: See send() @param check_exit: See send() @param fail_on_empty_before: See send() @param record_command: See send() @param exit_values: See send() @param escape: See send() @param echo: See send() @param note: See send() @param secret: See send() @param check_sudo: See send() @param remove_on_match See ShutItSendSpec @param loglevel: See send() @return: The pexpect return value (ie which expected string in the list matched). If return is -1, the task was backgrounded. See also multisend. @rtype: int
f10349:c0:m40
def send_and_require(self,<EOL>send,<EOL>regexps,<EOL>not_there=False,<EOL>echo=None,<EOL>note=None,<EOL>loglevel=logging.INFO):
shutit = self.shutit<EOL>echo = shutit.get_echo_override(echo)<EOL>return self.send_until(send,<EOL>regexps,<EOL>not_there=not_there,<EOL>cadence=<NUM_LIT:0>,<EOL>retries=<NUM_LIT:1>,<EOL>echo=echo,<EOL>note=note,<EOL>loglevel=loglevel)<EOL>
Send string and require the item in the output. See send_until
f10349:c0:m41
def send_until(self,<EOL>send,<EOL>regexps,<EOL>not_there=False,<EOL>cadence=<NUM_LIT:2>,<EOL>retries=<NUM_LIT:100>,<EOL>echo=None,<EOL>note=None,<EOL>debug_command=None,<EOL>pause_point_on_fail=True,<EOL>nonewline=False,<EOL>loglevel=logging.INFO):
shutit = self.shutit<EOL>shutit.handle_note(note, command=send + '<STR_LIT>' + str(regexps))<EOL>shutit.log('<STR_LIT>' + send + '<STR_LIT>' + str(regexps), level=loglevel)<EOL>if isinstance(regexps, str):<EOL><INDENT>regexps = [regexps]<EOL><DEDENT>if not isinstance(regexps, list):<EOL><INDENT>shutit.fail('<STR_LIT>') <EOL><DEDENT>while retries > <NUM_LIT:0>:<EOL><INDENT>retries -= <NUM_LIT:1><EOL>echo = shutit.get_echo_override(echo)<EOL>output = self.send_and_get_output(send,<EOL>retry=<NUM_LIT:1>,<EOL>strip=True,<EOL>echo=echo,<EOL>loglevel=loglevel,<EOL>nonewline=nonewline,<EOL>fail_on_empty_before=False)<EOL>shutit.log('<STR_LIT>' + str(regexps) + '<STR_LIT>' + str(retries), level=loglevel)<EOL>if not not_there:<EOL><INDENT>for regexp in regexps:<EOL><INDENT>if not shutit_util.check_regexp(regexp):<EOL><INDENT>shutit.fail('<STR_LIT>' + regexp) <EOL><DEDENT>if shutit.match_string(output, regexp):<EOL><INDENT>return True<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>missing = False<EOL>for regexp in regexps:<EOL><INDENT>if not shutit_util.check_regexp(regexp):<EOL><INDENT>shutit.fail('<STR_LIT>' + regexp) <EOL><DEDENT>if not shutit.match_string(output, regexp):<EOL><INDENT>missing = True<EOL>break<EOL><DEDENT><DEDENT>if missing:<EOL><INDENT>shutit.handle_note_after(note=note)<EOL>return True<EOL><DEDENT><DEDENT>if debug_command is not None:<EOL><INDENT>self.send(ShutItSendSpec(self,<EOL>send=debug_command,<EOL>check_exit=False,<EOL>echo=echo,<EOL>nonewline=nonewline,<EOL>loglevel=loglevel,<EOL>ignore_background=True))<EOL><DEDENT>time.sleep(cadence)<EOL><DEDENT>shutit.handle_note_after(note=note)<EOL>if pause_point_on_fail:<EOL><INDENT>shutit.pause_point('<STR_LIT>' + send + '<STR_LIT>' + str(regexps))<EOL>return True<EOL><DEDENT>return False<EOL>
Send string on a regular cadence until a string is either seen, or the timeout is triggered. @param send: See send() @param regexps: List of regexps to wait for. @param not_there: If True, wait until this a regexp is not seen in the output. If False wait until a regexp is seen in the output (default) @param echo: See send() @param note: See send() @param debug_command: Command to send if the output was not there.
f10349:c0:m42
def change_text(self,<EOL>text,<EOL>fname,<EOL>pattern=None,<EOL>before=False,<EOL>force=False,<EOL>delete=False,<EOL>note=None,<EOL>replace=False,<EOL>line_oriented=True,<EOL>create=True,<EOL>loglevel=logging.DEBUG):
shutit = self.shutit<EOL>shutit.handle_note(note)<EOL>fexists = self.file_exists(fname)<EOL>if not fexists:<EOL><INDENT>if create:<EOL><INDENT>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>' + fname,<EOL>echo=False,<EOL>loglevel=loglevel,<EOL>ignore_background=True))<EOL><DEDENT>else:<EOL><INDENT>shutit.fail(fname + '<STR_LIT>') <EOL><DEDENT><DEDENT>if replace:<EOL><INDENT>if not pattern:<EOL><INDENT>shutit.fail('<STR_LIT>') <EOL><DEDENT>if delete:<EOL><INDENT>shutit.fail('<STR_LIT>') <EOL><DEDENT><DEDENT>if self.command_available('<STR_LIT>'):<EOL><INDENT>if shutit_global.shutit_global_object.ispy3:<EOL><INDENT>ftext = bytes(self.send_and_get_output('<STR_LIT>' + fname,<EOL>echo=False,<EOL>loglevel=loglevel),<EOL>shutit_global.shutit_global_object.default_encoding)<EOL><DEDENT>else:<EOL><INDENT>ftext = self.send_and_get_output('<STR_LIT>' + fname,<EOL>echo=False,<EOL>loglevel=loglevel)<EOL><DEDENT>ftext = base64.b64decode(ftext)<EOL><DEDENT>else:<EOL><INDENT>if shutit_global.shutit_global_object.ispy3:<EOL><INDENT>ftext = bytes(self.send_and_get_output('<STR_LIT>' + fname,<EOL>echo=False,<EOL>loglevel=loglevel),<EOL>shutit_global.shutit_global_object.default_encoding)<EOL>ftext = ftext.replace(bytes('<STR_LIT:\r\n>', shutit_global.shutit_global_object.default_encoding),bytes('<STR_LIT:\n>', shutit_global.shutit_global_object.default_encoding))<EOL><DEDENT>else:<EOL><INDENT>ftext = self.send_and_get_output('<STR_LIT>' + fname,<EOL>echo=False,<EOL>loglevel=loglevel)<EOL>ftext = ftext.replace('<STR_LIT:\r\n>','<STR_LIT:\n>')<EOL><DEDENT><DEDENT>if delete:<EOL><INDENT>if shutit_global.shutit_global_object.ispy3:<EOL><INDENT>loc = ftext.find(bytes(text,shutit_global.shutit_global_object.default_encoding))<EOL><DEDENT>else:<EOL><INDENT>loc = ftext.find(text)<EOL><DEDENT>if loc == -<NUM_LIT:1>:<EOL><INDENT>return None<EOL><DEDENT>else:<EOL><INDENT>new_text = ftext[:loc] + ftext[loc+len(text)+<NUM_LIT:1>:]<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if pattern != None:<EOL><INDENT>if not line_oriented:<EOL><INDENT>if not shutit_util.check_regexp(pattern):<EOL><INDENT>shutit.fail('<STR_LIT>' + pattern) <EOL><DEDENT>if shutit_global.shutit_global_object.ispy3:<EOL><INDENT>sre_match = re.search(bytes(pattern,shutit_global.shutit_global_object.default_encoding),ftext,re.DOTALL|re.MULTILINE)<EOL><DEDENT>else:<EOL><INDENT>sre_match = re.search(pattern,ftext,re.DOTALL|re.MULTILINE)<EOL><DEDENT>if replace:<EOL><INDENT>if sre_match is None:<EOL><INDENT>cut_point = len(ftext)<EOL>newtext1 = ftext[:cut_point]<EOL>newtext2 = ftext[cut_point:]<EOL><DEDENT>else:<EOL><INDENT>cut_point = sre_match.start()<EOL>cut_point_after = sre_match.end()<EOL>newtext1 = ftext[:cut_point]<EOL>newtext2 = ftext[cut_point_after:]<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if sre_match is None:<EOL><INDENT>return None<EOL><DEDENT>elif before:<EOL><INDENT>cut_point = sre_match.start()<EOL>if shutit_global.shutit_global_object.ispy3:<EOL><INDENT>if not force and ftext[cut_point-len(text):].find(bytes(text,shutit_global.shutit_global_object.default_encoding)) > <NUM_LIT:0>:<EOL><INDENT>return None<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if not force and ftext[cut_point-len(text):].find(text) > <NUM_LIT:0>:<EOL><INDENT>return None<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>cut_point = sre_match.end()<EOL>if shutit_global.shutit_global_object.ispy3:<EOL><INDENT>if not force and ftext[cut_point:].find(bytes(text,shutit_global.shutit_global_object.default_encoding)) > <NUM_LIT:0>:<EOL><INDENT>return None<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if not force and ftext[cut_point:].find(text) > <NUM_LIT:0>:<EOL><INDENT>return None<EOL><DEDENT><DEDENT><DEDENT>newtext1 = ftext[:cut_point]<EOL>newtext2 = ftext[cut_point:]<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if shutit_global.shutit_global_object.ispy3:<EOL><INDENT>lines = ftext.split(bytes('<STR_LIT:\n>',shutit_global.shutit_global_object.default_encoding))<EOL><DEDENT>else:<EOL><INDENT>lines = ftext.split('<STR_LIT:\n>')<EOL><DEDENT>cut_point = <NUM_LIT:0><EOL>line_length = <NUM_LIT:0><EOL>matched = False<EOL>if not shutit_util.check_regexp(pattern):<EOL><INDENT>shutit.fail('<STR_LIT>' + pattern) <EOL><DEDENT>for line in lines:<EOL><INDENT>pattern_before='<STR_LIT>'<EOL>pattern_after='<STR_LIT>'<EOL>if not pattern or pattern[<NUM_LIT:0>] != '<STR_LIT>':<EOL><INDENT>pattern_before = '<STR_LIT>'<EOL><DEDENT>if not pattern or pattern[-<NUM_LIT:1>] != '<STR_LIT:$>':<EOL><INDENT>pattern_after = '<STR_LIT>'<EOL><DEDENT>new_pattern = pattern_before+pattern+pattern_after<EOL>if shutit_global.shutit_global_object.ispy3:<EOL><INDENT>match = re.search(bytes(new_pattern,shutit_global.shutit_global_object.default_encoding), line)<EOL><DEDENT>else:<EOL><INDENT>match = re.search(new_pattern, line)<EOL><DEDENT>line_length = len(line)<EOL>if match != None:<EOL><INDENT>matched=True<EOL>break<EOL><DEDENT>cut_point += line_length+<NUM_LIT:1><EOL><DEDENT>if not replace and not matched:<EOL><INDENT>return None<EOL><DEDENT>if replace and not matched:<EOL><INDENT>cut_point = len(ftext)<EOL><DEDENT>elif not replace and not before:<EOL><INDENT>cut_point += line_length<EOL><DEDENT>newtext1 = ftext[:cut_point]<EOL>newtext2 = ftext[cut_point:]<EOL>if replace and matched:<EOL><INDENT>newtext2 = ftext[cut_point+line_length:]<EOL><DEDENT>elif not force:<EOL><INDENT>if shutit_global.shutit_global_object.ispy3:<EOL><INDENT>if before and ftext[cut_point-len(text):].find(bytes(text,shutit_global.shutit_global_object.default_encoding)) > <NUM_LIT:0>:<EOL><INDENT>return None<EOL><DEDENT>if not before and ftext[cut_point:].find(bytes(text,shutit_global.shutit_global_object.default_encoding)) > <NUM_LIT:0>:<EOL><INDENT>return None<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if before and ftext[cut_point-len(text):].find(text) > <NUM_LIT:0>:<EOL><INDENT>return None<EOL><DEDENT>if not before and ftext[cut_point:].find(text) > <NUM_LIT:0>:<EOL><INDENT>return None<EOL><DEDENT><DEDENT><DEDENT>if shutit_global.shutit_global_object.ispy3:<EOL><INDENT>if newtext1 and bytes(newtext1.decode(shutit_global.shutit_global_object.default_encoding)[-<NUM_LIT:1>],shutit_global.shutit_global_object.default_encoding) != bytes('<STR_LIT:\n>',shutit_global.shutit_global_object.default_encoding):<EOL><INDENT>newtext1 += bytes('<STR_LIT:\n>',shutit_global.shutit_global_object.default_encoding)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if newtext1 and newtext1[-<NUM_LIT:1>] != '<STR_LIT:\n>':<EOL><INDENT>newtext1 += '<STR_LIT:\n>'<EOL><DEDENT><DEDENT>if shutit_global.shutit_global_object.ispy3:<EOL><INDENT>if newtext2 and bytes(newtext2.decode(shutit_global.shutit_global_object.default_encoding)[<NUM_LIT:0>],shutit_global.shutit_global_object.default_encoding) != bytes('<STR_LIT:\n>',shutit_global.shutit_global_object.default_encoding):<EOL><INDENT>newtext2 = bytes('<STR_LIT:\n>',shutit_global.shutit_global_object.default_encoding) + newtext2<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if newtext2 and newtext2[<NUM_LIT:0>] != '<STR_LIT:\n>':<EOL><INDENT>newtext2 = '<STR_LIT:\n>' + newtext2<EOL><DEDENT><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>cut_point = len(ftext)<EOL>newtext1 = ftext[:cut_point]<EOL>newtext2 = ftext[cut_point:]<EOL><DEDENT>if shutit_global.shutit_global_object.ispy3:<EOL><INDENT>if newtext2 == b'<STR_LIT>' and text and bytes(text[-<NUM_LIT:1>],shutit_global.shutit_global_object.default_encoding) != bytes('<STR_LIT:\n>',shutit_global.shutit_global_object.default_encoding):<EOL><INDENT>newtext2 = bytes('<STR_LIT:\n>',shutit_global.shutit_global_object.default_encoding)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if newtext2 == '<STR_LIT>' and text and text[-<NUM_LIT:1>] != '<STR_LIT:\n>':<EOL><INDENT>newtext2 = '<STR_LIT:\n>'<EOL><DEDENT><DEDENT>if shutit_global.shutit_global_object.ispy3:<EOL><INDENT>new_text = newtext1 + bytes(text,shutit_global.shutit_global_object.default_encoding) + newtext2<EOL><DEDENT>else:<EOL><INDENT>new_text = newtext1 + text + newtext2<EOL><DEDENT><DEDENT>self.send_file(fname,<EOL>new_text,<EOL>truncate=True,<EOL>loglevel=loglevel)<EOL>shutit.handle_note_after(note=note)<EOL>return True<EOL>
Change text in a file. Returns None if there was no match for the regexp, True if it was matched and replaced, and False if the file did not exist or there was some other problem. @param text: Text to insert. @param fname: Filename to insert text to @param pattern: Regexp for a line to match and insert after/before/replace. If none, put at end of file. @param before: Whether to place the text before or after the matched text. @param force: Force the insertion even if the text is in the file. @param delete: Delete text from file rather than insert @param replace: Replace matched text with passed-in text. If nothing matches, then append. @param note: See send() @param line_oriented: Consider the pattern on a per-line basis (default True). Can match any continuous section of the line, eg 'b.*d' will match the line: 'abcde' If not line_oriented, the regexp is considered on with the flags re.DOTALL, re.MULTILINE enabled
f10349:c0:m43
def remove_line_from_file(self,<EOL>line,<EOL>filename,<EOL>match_regexp=None,<EOL>literal=False,<EOL>note=None,<EOL>loglevel=logging.DEBUG):
shutit = self.shutit<EOL>shutit.handle_note(note)<EOL>tmp_filename = '<STR_LIT>' + shutit_util.random_id()<EOL>if self.file_exists(filename):<EOL><INDENT>if literal:<EOL><INDENT>if match_regexp is None:<EOL><INDENT>self.send(ShutItSendSpec(self,<EOL>send="""<STR_LIT>""" + line + """<STR_LIT>""" + filename + '<STR_LIT>' + tmp_filename,<EOL>exit_values=['<STR_LIT:0>','<STR_LIT:1>'],<EOL>echo=False,<EOL>loglevel=loglevel,<EOL>ignore_background=True))<EOL><DEDENT>else:<EOL><INDENT>if not shutit_util.check_regexp(match_regexp):<EOL><INDENT>shutit.fail('<STR_LIT>' + match_regexp) <EOL><DEDENT>self.send(ShutItSendSpec(self,<EOL>send="""<STR_LIT>""" + match_regexp + """<STR_LIT>""" + filename + '<STR_LIT>' + tmp_filename,<EOL>exit_values=['<STR_LIT:0>','<STR_LIT:1>'],<EOL>echo=False,<EOL>loglevel=loglevel,<EOL>ignore_background=True))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if match_regexp is None:<EOL><INDENT>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>' + line + '<STR_LIT>' + filename + '<STR_LIT>' + tmp_filename,<EOL>exit_values=['<STR_LIT:0>','<STR_LIT:1>'],<EOL>echo=False,<EOL>loglevel=loglevel,<EOL>ignore_background=True))<EOL><DEDENT>else:<EOL><INDENT>if not shutit_util.check_regexp(match_regexp):<EOL><INDENT>shutit.fail('<STR_LIT>' + match_regexp) <EOL><DEDENT>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>' + match_regexp + '<STR_LIT>' + filename + '<STR_LIT>' + tmp_filename,<EOL>exit_values=['<STR_LIT:0>','<STR_LIT:1>'],<EOL>echo=False,<EOL>loglevel=loglevel,<EOL>ignore_background=True))<EOL><DEDENT><DEDENT>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>' + tmp_filename + '<STR_LIT>' + filename,<EOL>check_exit=False,<EOL>echo=False,<EOL>loglevel=loglevel,<EOL>ignore_background=True))<EOL>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>' + tmp_filename,<EOL>exit_values=['<STR_LIT:0>','<STR_LIT:1>'],<EOL>echo=False,<EOL>loglevel=loglevel,<EOL>ignore_background=True))<EOL><DEDENT>shutit.handle_note_after(note=note)<EOL>return True<EOL>
Removes line from file, if it exists. Must be exactly the line passed in to match. Returns True if there were no problems, False if there were. @param line: Line to remove. @param filename Filename to remove it from. @param match_regexp: If supplied, a regexp to look for in the file instead of the line itself, handy if the line has awkward characters in it. @param literal: If true, then simply grep for the exact string without bash interpretation. (Default: False) @param note: See send() @type line: string @type filename: string @type match_regexp: string @type literal: boolean @return: True if the line was matched and deleted, False otherwise. @rtype: boolean
f10349:c0:m44
def send(self, sendspec):
shutit = self.shutit<EOL>shutit.log('<STR_LIT>' + self.pexpect_session_id + '<STR_LIT>' + str(sendspec.send), level=logging.DEBUG)<EOL>if self._check_blocked(sendspec):<EOL><INDENT>shutit.log('<STR_LIT>' + str(sendspec.send) + '<STR_LIT>', level=logging.DEBUG)<EOL>return -<NUM_LIT:1><EOL><DEDENT>shutit = self.shutit<EOL>cfg = shutit.cfg<EOL>sendspec.expect = sendspec.expect or self.default_expect<EOL>assert sendspec.send is not None, shutit_util.print_debug()<EOL>if sendspec.send.strip() == '<STR_LIT>':<EOL><INDENT>sendspec.fail_on_empty_before=False<EOL>sendspec.check_exit=False<EOL><DEDENT>if isinstance(sendspec.expect, dict):<EOL><INDENT>return self.multisend(ShutItSendSpec(self,<EOL>send=sendspec.send,<EOL>send_dict=sendspec.expect,<EOL>expect=shutit.get_default_shutit_pexpect_session_expect(),<EOL>timeout=sendspec.timeout,<EOL>check_exit=sendspec.check_exit,<EOL>fail_on_empty_before=sendspec.fail_on_empty_before,<EOL>record_command=sendspec.record_command,<EOL>exit_values=sendspec.exit_values,<EOL>echo=sendspec.echo,<EOL>note=sendspec.note,<EOL>secret=sendspec.secret,<EOL>check_sudo=sendspec.check_sudo,<EOL>nonewline=sendspec.nonewline,<EOL>loglevel=sendspec.loglevel))<EOL><DEDENT>command_list = sendspec.send.strip().split()<EOL>if sendspec.check_sudo and command_list and command_list[<NUM_LIT:0>] == '<STR_LIT>' and not self.check_sudo():<EOL><INDENT>sudo_pass = self.get_sudo_pass_if_needed(shutit)<EOL>return self.multisend(ShutItSendSpec(self,<EOL>send=sendspec.send,<EOL>send_dict={'<STR_LIT>':[sudo_pass, True]},<EOL>expect=shutit.get_default_shutit_pexpect_session_expect(),<EOL>timeout=sendspec.timeout,<EOL>check_exit=sendspec.check_exit,<EOL>fail_on_empty_before=sendspec.fail_on_empty_before,<EOL>record_command=sendspec.record_command,<EOL>exit_values=sendspec.exit_values,<EOL>echo=sendspec.echo,<EOL>note=sendspec.note,<EOL>check_sudo=False,<EOL>nonewline=sendspec.nonewline,<EOL>loglevel=sendspec.loglevel))<EOL><DEDENT>shutit.handle_note(sendspec.note, command=str(sendspec.send), training_input=str(sendspec.send))<EOL>if sendspec.timeout is None:<EOL><INDENT>sendspec.timeout = <NUM_LIT><EOL><DEDENT>sendspec.echo = shutit.get_echo_override(sendspec.echo)<EOL>if sendspec.assume_gnu:<EOL><INDENT>sendspec.send = shutit.get_send_command(sendspec.send)<EOL><DEDENT>if sendspec.check_exit is None:<EOL><INDENT>if (shutit.build['<STR_LIT>'] != -<NUM_LIT:1> or shutit.build['<STR_LIT>'] is True) or shutit.build['<STR_LIT>'] or shutit.build['<STR_LIT>'] or shutit.build['<STR_LIT>']:<EOL><INDENT>sendspec.check_exit = False<EOL><DEDENT>elif sendspec.expect == shutit.get_default_shutit_pexpect_session_expect():<EOL><INDENT>sendspec.check_exit = shutit.get_default_shutit_pexpect_session_check_exit()<EOL><DEDENT>else:<EOL><INDENT>expect_matches_prompt = False<EOL>for prompt in shutit.expect_prompts:<EOL><INDENT>if prompt == sendspec.expect:<EOL><INDENT>expect_matches_prompt = True<EOL><DEDENT><DEDENT>if not expect_matches_prompt:<EOL><INDENT>sendspec.check_exit = False<EOL><DEDENT>else:<EOL><INDENT>sendspec.check_exit = True<EOL><DEDENT><DEDENT><DEDENT>ok_to_record = False<EOL>if not sendspec.echo and sendspec.record_command is None:<EOL><INDENT>sendspec.record_command = False<EOL><DEDENT>if sendspec.record_command is None or sendspec.record_command:<EOL><INDENT>ok_to_record = True<EOL>for i in cfg.keys():<EOL><INDENT>if isinstance(cfg[i], dict):<EOL><INDENT>for j in cfg[i].keys():<EOL><INDENT>if (j == '<STR_LIT:password>' or j == '<STR_LIT>') and cfg[i][j] == sendspec.send:<EOL><INDENT>shutit.build['<STR_LIT>'].append ('<STR_LIT>')<EOL>ok_to_record = False<EOL>break<EOL><DEDENT><DEDENT><DEDENT>if not ok_to_record or sendspec.send in shutit_global.shutit_global_object.secret_words_set:<EOL><INDENT>sendspec.secret = True<EOL>break<EOL><DEDENT><DEDENT>if ok_to_record:<EOL><INDENT>shutit.build['<STR_LIT>'].append(sendspec.send)<EOL><DEDENT><DEDENT>if sendspec.send != None:<EOL><INDENT>shutit.log('<STR_LIT>', level=logging.DEBUG)<EOL>send_and_expect_summary_msg = '<STR_LIT>'<EOL>if not sendspec.echo and not sendspec.secret:<EOL><INDENT>send_and_expect_summary_msg += '<STR_LIT>' + sendspec.send<EOL><DEDENT>elif not sendspec.echo and sendspec.secret:<EOL><INDENT>send_and_expect_summary_msg += '<STR_LIT>' + sendspec.send<EOL><DEDENT>if not sendspec.secret:<EOL><INDENT>send_and_expect_summary_msg += '<STR_LIT>' + sendspec.send + '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>send_and_expect_summary_msg += '<STR_LIT>'<EOL><DEDENT>send_and_expect_summary_msg += '<STR_LIT>' + str(sendspec.expect) + '<STR_LIT>'<EOL>shutit.log(send_and_expect_summary_msg, level=logging.DEBUG)<EOL><DEDENT>while sendspec.retry > <NUM_LIT:0>:<EOL><INDENT>if sendspec.escape:<EOL><INDENT>escaped_str = "<STR_LIT>"<EOL>_count = <NUM_LIT:7><EOL>for char in sendspec.send:<EOL><INDENT>if char in string.ascii_letters:<EOL><INDENT>escaped_str += char<EOL>_count += <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>escaped_str += shutit_util.get_wide_hex(char)<EOL>_count += <NUM_LIT:4><EOL><DEDENT>if _count > shutit_global.shutit_global_object.line_limit:<EOL><INDENT>escaped_str +=
Send string as a shell command, and wait until the expected output is seen (either a string or any from a list of strings) before returning. The expected string will default to the currently-set default expected string (see get_default_shutit_pexpect_session_expect) Returns the pexpect return value (ie which expected string in the list matched) @return: The pexpect return value (ie which expected string in the list matched). If return is -1, the task was backgrounded. See also multisend. @rtype: int
f10349:c0:m45
def quick_send(self, send, echo=None, loglevel=logging.INFO):
shutit = self.shutit<EOL>shutit.log('<STR_LIT>' + send, level=loglevel)<EOL>res = self.sendline(ShutItSendSpec(self,<EOL>send=send,<EOL>check_exit=False,<EOL>echo=echo,<EOL>fail_on_empty_before=False,<EOL>record_command=False,<EOL>ignore_background=True))<EOL>if not res:<EOL><INDENT>self.expect(self.default_expect)<EOL><DEDENT>
Quick and dirty send that ignores background tasks. Intended for internal use.
f10349:c0:m46
def send_file(self,<EOL>path,<EOL>contents,<EOL>echo=False,<EOL>truncate=False,<EOL>note=None,<EOL>user=None,<EOL>group=None,<EOL>loglevel=logging.INFO,<EOL>encoding=None):
shutit = self.shutit<EOL>shutit.handle_note(note, '<STR_LIT>' + path)<EOL>if shutit_global.shutit_global_object.ispy3:<EOL><INDENT>split_contents = '<STR_LIT>'.join((str(contents[:<NUM_LIT>]).split()))<EOL><DEDENT>else:<EOL><INDENT>split_contents = '<STR_LIT>'.join((contents[:<NUM_LIT>].split()))<EOL><DEDENT>strings_from_file = re.findall("<STR_LIT>", split_contents)<EOL>shutit.log('<STR_LIT>' + '<STR_LIT>'.join(strings_from_file)[:<NUM_LIT:30>] + '<STR_LIT>' + path, level=loglevel)<EOL>if user is None:<EOL><INDENT>user = self.whoami()<EOL><DEDENT>if group is None:<EOL><INDENT>group = self.whoarewe()<EOL><DEDENT>if self.current_environment.environment_id == '<STR_LIT>' and False:<EOL><INDENT>if isinstance(contents, str):<EOL><INDENT>f = open(path,'<STR_LIT:w>')<EOL>if truncate:<EOL><INDENT>f.truncate(<NUM_LIT:0>)<EOL><DEDENT>try:<EOL><INDENT>f.write(contents)<EOL><DEDENT>except (UnicodeDecodeError, TypeError) as e:<EOL><INDENT>shutit.log('<STR_LIT>' + str(e), level=logging.DEBUG)<EOL>if encoding is not None:<EOL><INDENT>f.write(contents.decode(encoding))<EOL><DEDENT>else:<EOL><INDENT>f.write(contents.decode(shutit_global.shutit_global_object.default_encoding))<EOL><DEDENT><DEDENT><DEDENT>elif isinstance(contents, bytes):<EOL><INDENT>f = open(path,'<STR_LIT:w>')<EOL>if truncate:<EOL><INDENT>f.truncate(<NUM_LIT:0>)<EOL><DEDENT>try:<EOL><INDENT>f.write(contents)<EOL><DEDENT>except (UnicodeDecodeError, TypeError) as e:<EOL><INDENT>if encoding is not None:<EOL><INDENT>f.write(contents.decode(encoding))<EOL><DEDENT>else:<EOL><INDENT>f.write(contents.decode(shutit_global.shutit_global_object.default_encoding))<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>shutit.fail('<STR_LIT>' + str(type(contents)) + '<STR_LIT>') <EOL><DEDENT>f.close()<EOL><DEDENT>elif shutit.build['<STR_LIT>'] in ('<STR_LIT>','<STR_LIT>'):<EOL><INDENT>if truncate and self.file_exists(path):<EOL><INDENT>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>' + path,<EOL>echo=False,<EOL>loglevel=loglevel,<EOL>ignore_background=True))<EOL><DEDENT>random_id = shutit_util.random_id()<EOL>if shutit_global.shutit_global_object.ispy3:<EOL><INDENT>if encoding is not None:<EOL><INDENT>b64contents = base64.b64encode(contents.encode(encoding)).decode(encoding)<EOL><DEDENT>else:<EOL><INDENT>if isinstance(contents, str):<EOL><INDENT>b64contents = base64.b64encode(contents.encode(shutit_global.shutit_global_object.default_encoding)).decode(shutit_global.shutit_global_object.default_encoding)<EOL><DEDENT>elif isinstance(contents, bytes):<EOL><INDENT>b64contents = base64.b64encode(contents).decode(shutit_global.shutit_global_object.default_encoding)<EOL><DEDENT>else:<EOL><INDENT>shutit.fail('<STR_LIT>' + str(type(contents)) + '<STR_LIT>') <EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>if encoding is not None:<EOL><INDENT>b64contents = base64.b64encode(contents.encode(encoding))<EOL><DEDENT>else:<EOL><INDENT>b64contents = base64.b64encode(contents.encode(shutit_global.shutit_global_object.default_encoding))<EOL><DEDENT><DEDENT>total_length = len(b64contents)<EOL>position = <NUM_LIT:0><EOL>leap=<NUM_LIT:1000><EOL>while position < total_length:<EOL><INDENT>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT:U+0020>' + shutit.get_command('<STR_LIT>') + '<STR_LIT>' + path + "<STR_LIT:.>" + random_id + "<STR_LIT>" + random_id + """<STR_LIT>""" + b64contents[position:position+leap] + '''<STR_LIT>''' + random_id,<EOL>echo=echo,<EOL>loglevel=loglevel,<EOL>timeout=shutit_global.shutit_global_object.default_timeout,<EOL>ignore_background=True))<EOL>position += leap<EOL><DEDENT>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>' + path + '<STR_LIT:.>' + random_id + '<STR_LIT>' + path,<EOL>echo=echo,<EOL>loglevel=loglevel,<EOL>ignore_background=True))<EOL>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>' + path + '<STR_LIT:.>' + random_id,<EOL>loglevel=loglevel,<EOL>echo=False,<EOL>ignore_background=True))<EOL><DEDENT>else:<EOL><INDENT>host_child = shutit.get_shutit_pexpect_session_from_id('<STR_LIT>').pexpect_child<EOL>path = path.replace('<STR_LIT:U+0020>', r'<STR_LIT>')<EOL>tmpfile = shutit_global.shutit_global_object.shutit_state_dir + '<STR_LIT>' + shutit_util.random_id()<EOL>f = open(tmpfile,'<STR_LIT:wb>')<EOL>f.truncate(<NUM_LIT:0>)<EOL>if isinstance(contents, bytes):<EOL><INDENT>try:<EOL><INDENT>if shutit_global.shutit_global_object.ispy3:<EOL><INDENT>f.write(contents)<EOL><DEDENT>elif encoding is not None:<EOL><INDENT>f.write(contents.encode(encoding))<EOL><DEDENT>else:<EOL><INDENT>f.write(contents.encode(shutit_global.shutit_global_object.default_encoding))<EOL><DEDENT><DEDENT>except (UnicodeDecodeError, TypeError) as e:<EOL><INDENT>f.write(contents)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>if encoding is not None:<EOL><INDENT>f.write(contents.encode(encoding))<EOL><DEDENT>else:<EOL><INDENT>f.write(contents.encode(shutit_global.shutit_global_object.default_encoding))<EOL><DEDENT><DEDENT>except (UnicodeDecodeError, TypeError) as e:<EOL><INDENT>f.write(contents)<EOL><DEDENT><DEDENT>f.close()<EOL>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>' + path,<EOL>loglevel=loglevel,<EOL>echo=echo,<EOL>ignore_background=True))<EOL>if path[<NUM_LIT:0>] != '<STR_LIT:/>':<EOL><INDENT>shutit.send('<STR_LIT>' + tmpfile + '<STR_LIT>' + shutit.host['<STR_LIT>'] + '<STR_LIT>' + shutit.target['<STR_LIT>'] + "<STR_LIT>" + path + "<STR_LIT:'>",<EOL>shutit_pexpect_child=host_child,<EOL>expect=shutit.expect_prompts['<STR_LIT>'],<EOL>loglevel=loglevel,<EOL>echo=echo)<EOL><DEDENT>else:<EOL><INDENT>shutit.send('<STR_LIT>' + tmpfile + '<STR_LIT>' + shutit.host['<STR_LIT>'] + '<STR_LIT>' + shutit.target['<STR_LIT>'] + "<STR_LIT>" + path + "<STR_LIT:'>",<EOL>shutit_pexpect_child=host_child,<EOL>expect=shutit.expect_prompts['<STR_LIT>'],<EOL>loglevel=loglevel,<EOL>echo=echo)<EOL><DEDENT>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>' + user + '<STR_LIT:U+0020>' + path + '<STR_LIT>' + group + '<STR_LIT:U+0020>' + path,<EOL>echo=echo,<EOL>loglevel=loglevel,<EOL>ignore_background=True))<EOL>os.remove(tmpfile)<EOL><DEDENT>shutit.handle_note_after(note=note)<EOL>return True<EOL>
Sends the passed-in string as a file to the passed-in path on the target. @param path: Target location of file on target. @param contents: Contents of file as a string. @param note: See send() @param user: Set ownership to this user (defaults to whoami) @param group: Set group to this user (defaults to first group in groups) @type path: string @type contents: string
f10349:c0:m47
def run_script(self,<EOL>script,<EOL>in_shell=True,<EOL>echo=None,<EOL>note=None,<EOL>loglevel=logging.DEBUG):
shutit = self.shutit<EOL>shutit.handle_note(note, '<STR_LIT>' + str(script))<EOL>shutit.log('<STR_LIT>' + '<STR_LIT>'.join(script.split())[:<NUM_LIT:30>] + '<STR_LIT>', level=logging.INFO)<EOL>lines = script.split('<STR_LIT:\n>')<EOL>while lines and re.match('<STR_LIT>', lines[<NUM_LIT:0>]):<EOL><INDENT>lines = lines[<NUM_LIT:1>:]<EOL><DEDENT>while lines and re.match('<STR_LIT>', lines[-<NUM_LIT:1>]):<EOL><INDENT>lines = lines[:-<NUM_LIT:1>]<EOL><DEDENT>if not lines:<EOL><INDENT>return True<EOL><DEDENT>script = '<STR_LIT:\n>'.join(lines)<EOL>script = textwrap.dedent(script)<EOL>if shutit.build['<STR_LIT>'] in ('<STR_LIT>','<STR_LIT>') and in_shell:<EOL><INDENT>script = ('<STR_LIT>' + script + '<STR_LIT>')<EOL><DEDENT>self.quick_send('<STR_LIT>' + shutit_global.shutit_global_object.shutit_state_dir + '<STR_LIT>' + shutit_global.shutit_global_object.shutit_state_dir + '<STR_LIT>', echo=False)<EOL>self.send_file(shutit_global.shutit_global_object.shutit_state_dir + '<STR_LIT>',<EOL>script,<EOL>echo=False,<EOL>loglevel=loglevel)<EOL>self.quick_send('<STR_LIT>' + shutit_global.shutit_global_object.shutit_state_dir + '<STR_LIT>', echo=False)<EOL>shutit.build['<STR_LIT>'].append('<STR_LIT:U+0020>' + script.replace('<STR_LIT:\n>', '<STR_LIT>'))<EOL>if in_shell:<EOL><INDENT>ret = self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>' + shutit_global.shutit_global_object.shutit_state_dir + '<STR_LIT>' + shutit_global.shutit_global_object.shutit_state_dir + '<STR_LIT>' + shutit_global.shutit_global_object.shutit_state_dir + '<STR_LIT>',<EOL>echo=False,<EOL>loglevel=loglevel))<EOL><DEDENT>else:<EOL><INDENT>ret = self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT:U+0020>' + shutit_global.shutit_global_object.shutit_state_dir + '<STR_LIT>' + shutit_global.shutit_global_object.shutit_state_dir + '<STR_LIT>',<EOL>echo=False,<EOL>loglevel=loglevel))<EOL><DEDENT>shutit.handle_note_after(note=note)<EOL>return ret<EOL>
Run the passed-in string as a script on the target's command line. @param script: String representing the script. It will be de-indented and stripped before being run. @param in_shell: Indicate whether we are in a shell or not. (Default: True) @param note: See send() @type script: string @type in_shell: boolean
f10349:c0:m48
def challenge(self,<EOL>shutit,<EOL>task_desc,<EOL>expect=None,<EOL>hints=None,<EOL>congratulations='<STR_LIT:OK>',<EOL>failed='<STR_LIT>',<EOL>expect_type='<STR_LIT>',<EOL>challenge_type='<STR_LIT>',<EOL>timeout=None,<EOL>check_exit=None,<EOL>fail_on_empty_before=True,<EOL>record_command=True,<EOL>exit_values=None,<EOL>echo=True,<EOL>escape=False,<EOL>pause=<NUM_LIT:1>,<EOL>loglevel=logging.DEBUG,<EOL>follow_on_context=None,<EOL>difficulty=<NUM_LIT:1.0>,<EOL>reduction_per_minute=<NUM_LIT>,<EOL>reduction_per_reset=<NUM_LIT:0>,<EOL>reduction_per_hint=<NUM_LIT:0.5>,<EOL>grace_period=<NUM_LIT:30>,<EOL>new_stage=True,<EOL>final_stage=False,<EOL>num_stages=None):
shutit = self.shutit<EOL>if new_stage and shutit.build['<STR_LIT>']:<EOL><INDENT>if num_stages is None:<EOL><INDENT>num_stages = shutit.build['<STR_LIT>'].num_stages<EOL><DEDENT>elif shutit.build['<STR_LIT>'].num_stages < <NUM_LIT:1>:<EOL><INDENT>shutit.build['<STR_LIT>'].num_stages = num_stages<EOL><DEDENT>elif shutit.build['<STR_LIT>'].num_stages > <NUM_LIT:0>:<EOL><INDENT>shutit.fail('<STR_LIT>') <EOL><DEDENT>curr_stage = str(shutit.build['<STR_LIT>'].curr_stage)<EOL>if num_stages > <NUM_LIT:0>:<EOL><INDENT>task_desc = <NUM_LIT>*'<STR_LIT:*>' + '<STR_LIT:\n>' + '<STR_LIT>' + str(curr_stage) + '<STR_LIT:/>' + str(num_stages) + '<STR_LIT:\n>' + <NUM_LIT>*'<STR_LIT:*>' + '<STR_LIT:\n>' + task_desc<EOL><DEDENT>else:<EOL><INDENT>task_desc = <NUM_LIT>*'<STR_LIT:*>' + '<STR_LIT:\n>' + '<STR_LIT>' + <NUM_LIT>*'<STR_LIT:*>' + '<STR_LIT:\n>' + task_desc<EOL><DEDENT>shutit.build['<STR_LIT>'].new_stage(difficulty=difficulty,<EOL>reduction_per_minute=reduction_per_minute,<EOL>reduction_per_reset=reduction_per_reset,<EOL>reduction_per_hint=reduction_per_hint,<EOL>grace_period=grace_period)<EOL>self.send(ShutItSendSpec(self,<EOL>send='<STR_LIT>',<EOL>check_exit=False,<EOL>ignore_background=True))<EOL><DEDENT>shutit.build['<STR_LIT>'] = True<EOL>preserve_newline = False<EOL>skipped = False<EOL>if expect_type == '<STR_LIT>':<EOL><INDENT>if isinstance(expect, str):<EOL><INDENT>expect = [expect]<EOL><DEDENT>if not isinstance(expect, list):<EOL><INDENT>shutit.fail('<STR_LIT>') <EOL><DEDENT><DEDENT>elif expect_type == '<STR_LIT>':<EOL><INDENT>preserve_newline = True<EOL><DEDENT>elif expect_type == '<STR_LIT>':<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>shutit.fail('<STR_LIT>') <EOL><DEDENT>if hints:<EOL><INDENT>shutit.build['<STR_LIT>'] = hints<EOL><DEDENT>else:<EOL><INDENT>shutit.build['<STR_LIT>'] = []<EOL><DEDENT>if challenge_type == '<STR_LIT>':<EOL><INDENT>help_text = shutit_util.colorise('<STR_LIT>','''<STR_LIT>''')<EOL>ok = False<EOL>while not ok:<EOL><INDENT>shutit.log(shutit_util.colorise('<STR_LIT>','''<STR_LIT>'''),transient=True, level=logging.INFO)<EOL>if hints:<EOL><INDENT>shutit.log(shutit_util.colorise('<STR_LIT>',help_text),transient=True, level=logging.INFO)<EOL><DEDENT>time.sleep(pause)<EOL>send = shutit_util.get_input(task_desc + '<STR_LIT>',color='<STR_LIT>')<EOL>if not send or send.strip() == '<STR_LIT>':<EOL><INDENT>continue<EOL><DEDENT>if send in ('<STR_LIT>','<STR_LIT:h>'):<EOL><INDENT>if hints:<EOL><INDENT>shutit.log(help_text,transient=True, level=logging.CRITICAL)<EOL>shutit.log(shutit_util.colorise('<STR_LIT>',hints.pop()),transient=True, level=logging.CRITICAL)<EOL><DEDENT>else:<EOL><INDENT>shutit.log(help_text,transient=True, level=logging.CRITICAL)<EOL>shutit.log(shutit_util.colorise('<STR_LIT>','<STR_LIT>'),transient=True, level=logging.CRITICAL)<EOL><DEDENT>time.sleep(pause)<EOL>continue<EOL><DEDENT>if send == '<STR_LIT>':<EOL><INDENT>self._challenge_done(shutit, result='<STR_LIT>',follow_on_context=follow_on_context,final_stage=False)<EOL>continue<EOL><DEDENT>if send == '<STR_LIT>':<EOL><INDENT>self._challenge_done(shutit, result='<STR_LIT>',follow_on_context=follow_on_context,final_stage=True)<EOL>shutit_global.shutit_global_object.handle_exit(exit_code=<NUM_LIT:1>)<EOL><DEDENT>if send == '<STR_LIT>':<EOL><INDENT>self._challenge_done(shutit, result='<STR_LIT>',follow_on_context=follow_on_context,final_stage=True)<EOL>shutit.build['<STR_LIT>'] = []<EOL>return True<EOL><DEDENT>output = self.send_and_get_output(send,<EOL>timeout=timeout,<EOL>retry=<NUM_LIT:1>,<EOL>record_command=record_command,<EOL>echo=echo,<EOL>loglevel=loglevel,<EOL>fail_on_empty_before=False,<EOL>preserve_newline=preserve_newline)<EOL>md5sum_output = md5(output).hexdigest()<EOL>shutit.log('<STR_LIT>' + output + '<STR_LIT>' + md5sum_output, level=logging.DEBUG)<EOL>if expect_type == '<STR_LIT>':<EOL><INDENT>output = md5sum_output<EOL>if output == expect:<EOL><INDENT>ok = True<EOL><DEDENT><DEDENT>elif expect_type == '<STR_LIT>':<EOL><INDENT>if output == expect:<EOL><INDENT>ok = True<EOL><DEDENT><DEDENT>elif expect_type == '<STR_LIT>':<EOL><INDENT>for regexp in expect:<EOL><INDENT>if shutit.match_string(output, regexp):<EOL><INDENT>ok = True<EOL>break<EOL><DEDENT><DEDENT><DEDENT>if not ok and failed:<EOL><INDENT>if shutit.build['<STR_LIT>']:<EOL><INDENT>shutit.build['<STR_LIT>'].add_fail()<EOL>shutit.build['<STR_LIT>'].end_timer()<EOL><DEDENT>shutit.log('<STR_LIT>' + shutit_util.colorise('<STR_LIT>','<STR_LIT>') + '<STR_LIT:\n>',transient=True, level=logging.CRITICAL)<EOL>self._challenge_done(shutit, result='<STR_LIT>',final_stage=final_stage)<EOL>continue<EOL><DEDENT><DEDENT><DEDENT>elif challenge_type == '<STR_LIT>':<EOL><INDENT>ok = False<EOL>if hints:<EOL><INDENT>task_desc_new = task_desc + '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>task_desc_new = '<STR_LIT:\r\n>' + task_desc<EOL><DEDENT>while not ok:<EOL><INDENT>if shutit.build['<STR_LIT>'] and new_stage:<EOL><INDENT>shutit.build['<STR_LIT>'].start_timer()<EOL>new_stage = False<EOL><DEDENT>self.pause_point(shutit_util.colorise('<STR_LIT>',task_desc_new),color='<STR_LIT>')<EOL>if shutit_global.shutit_global_object.signal_id == <NUM_LIT:8>:<EOL><INDENT>if shutit.build['<STR_LIT>']:<EOL><INDENT>shutit.build['<STR_LIT>'].add_hint()<EOL><DEDENT>if shutit.build['<STR_LIT>']:<EOL><INDENT>shutit.log(shutit_util.colorise('<STR_LIT>','<STR_LIT>' + shutit.build['<STR_LIT>'].pop(<NUM_LIT:0>)),transient=True, level=logging.CRITICAL)<EOL><DEDENT>else:<EOL><INDENT>shutit.log(shutit_util.colorise('<STR_LIT>','<STR_LIT>' + '<STR_LIT>'),transient=True, level=logging.CRITICAL)<EOL><DEDENT>time.sleep(<NUM_LIT:1>)<EOL>shutit_global.shutit_global_object.signal_id = <NUM_LIT:0><EOL>continue<EOL><DEDENT>elif shutit_global.shutit_global_object.signal_id == <NUM_LIT>:<EOL><INDENT>shutit_global.shutit_global_object.signal_id = <NUM_LIT:0><EOL>continue<EOL><DEDENT>elif shutit_global.shutit_global_object.signal_id == <NUM_LIT:7>:<EOL><INDENT>if shutit.build['<STR_LIT>']:<EOL><INDENT>shutit.build['<STR_LIT>'].add_reset()<EOL><DEDENT>shutit.log(shutit_util.colorise('<STR_LIT>','<STR_LIT>'),transient=True, level=logging.CRITICAL)<EOL>self._challenge_done(shutit, result='<STR_LIT>', follow_on_context=follow_on_context,final_stage=False)<EOL>shutit_global.shutit_global_object.signal_id = <NUM_LIT:0><EOL>target_child = shutit.get_shutit_pexpect_session_from_id('<STR_LIT>')<EOL>return target_child.challenge(<EOL>shutit,<EOL>task_desc=task_desc,<EOL>expect=expect,<EOL>hints=hints,<EOL>congratulations=congratulations,<EOL>failed=failed,<EOL>expect_type=expect_type,<EOL>challenge_type=challenge_type,<EOL>timeout=timeout,<EOL>check_exit=check_exit,<EOL>fail_on_empty_before=fail_on_empty_before,<EOL>record_command=record_command,<EOL>exit_values=exit_values,<EOL>echo=echo,<EOL>escape=escape,<EOL>pause=pause,<EOL>loglevel=loglevel,<EOL>follow_on_context=follow_on_context,<EOL>new_stage=False<EOL>)<EOL><DEDENT>elif shutit_global.shutit_global_object.signal_id == <NUM_LIT>:<EOL><INDENT>if shutit.build['<STR_LIT>']:<EOL><INDENT>shutit.build['<STR_LIT>'].add_skip()<EOL>shutit.build['<STR_LIT>'].end_timer()<EOL><DEDENT>shutit_global.shutit_global_object.signal_id = <NUM_LIT:0><EOL>shutit.log('<STR_LIT>', level=logging.CRITICAL,transient=True)<EOL>skipped=True<EOL>self._challenge_done(shutit, result='<STR_LIT>',follow_on_context=follow_on_context,skipped=True,final_stage=final_stage)<EOL>return True<EOL><DEDENT>elif shutit_global.shutit_global_object.signal_id == <NUM_LIT>:<EOL><INDENT>shutit_global.shutit_global_object.signal_id = <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>shutit.log('<STR_LIT>' + str(shutit_global.shutit_global_object.signal_id), level=logging.CRITICAL,transient=True)<EOL><DEDENT>shutit.log('<STR_LIT>', level=logging.CRITICAL,transient=True)<EOL>check_command = follow_on_context.get('<STR_LIT>')<EOL>output = self.send_and_get_output(check_command,<EOL>timeout=timeout,<EOL>retry=<NUM_LIT:1>,<EOL>record_command=record_command,<EOL>echo=False,<EOL>loglevel=loglevel,<EOL>fail_on_empty_before=False,<EOL>preserve_newline=preserve_newline)<EOL>shutit.log('<STR_LIT>' + output, level=logging.DEBUG)<EOL>md5sum_output = md5(output).hexdigest()<EOL>if expect_type == '<STR_LIT>':<EOL><INDENT>shutit.log('<STR_LIT>' + output + '<STR_LIT>' + md5sum_output, level=logging.DEBUG)<EOL>output = md5sum_output<EOL>if output == expect:<EOL><INDENT>ok = True<EOL><DEDENT><DEDENT>elif expect_type == '<STR_LIT>':<EOL><INDENT>if output == expect:<EOL><INDENT>ok = True<EOL><DEDENT><DEDENT>elif expect_type == '<STR_LIT>':<EOL><INDENT>for regexp in expect:<EOL><INDENT>if shutit.match_string(output,regexp):<EOL><INDENT>ok = True<EOL>break<EOL><DEDENT><DEDENT><DEDENT>if not ok and failed:<EOL><INDENT>shutit.log('<STR_LIT>' + shutit_util.colorise('<STR_LIT>','<STR_LIT>') + '<STR_LIT:\n>',transient=True, level=logging.CRITICAL)<EOL>if shutit.build['<STR_LIT>']:<EOL><INDENT>shutit.build['<STR_LIT>'].add_fail()<EOL>shutit.build['<STR_LIT>'].end_timer()<EOL>self._challenge_done(shutit, result='<STR_LIT>',follow_on_context=follow_on_context,final_stage=final_stage)<EOL>return False<EOL><DEDENT>else:<EOL><INDENT>continue<EOL><DEDENT><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>shutit.fail('<STR_LIT>' + challenge_type + '<STR_LIT>') <EOL><DEDENT>self._challenge_done(shutit,<EOL>result='<STR_LIT>',<EOL>follow_on_context=follow_on_context,<EOL>congratulations=congratulations,<EOL>skipped=skipped,<EOL>final_stage=final_stage)<EOL>if shutit.build['<STR_LIT>']:<EOL><INDENT>shutit.build['<STR_LIT>'].add_ok()<EOL>shutit.build['<STR_LIT>'].end_timer()<EOL><DEDENT>shutit.build['<STR_LIT>'] = []<EOL>return True<EOL>
Set the user a task to complete, success being determined by matching the output. Either pass in regexp(s) desired from the output as a string or a list, or an md5sum of the output wanted. @param follow_on_context On success, move to this context. A dict of information about that context. context = the type of context, eg docker, bash ok_container_name = if passed, send user to this container reset_container_name = if resetting, send user to this container @param challenge_type Behaviour of challenge made to user command = check for output of single command golf = user gets a pause point, and when leaving, command follow_on_context['check_command'] is run to check the output
f10349:c0:m49
def _create_command_file(self, expect, send):
shutit = self.shutit<EOL>random_id = shutit_util.random_id()<EOL>fname = shutit_global.shutit_global_object.shutit_state_dir + '<STR_LIT>' + random_id<EOL>working_str = send<EOL>assert not self.sendline(ShutItSendSpec(self,<EOL>send='<STR_LIT>'+ fname,<EOL>ignore_background=True)), shutit_util.print_debug()<EOL>self.expect(expect)<EOL>size = shutit_global.shutit_global_object.line_limit<EOL>while working_str:<EOL><INDENT>curr_str = working_str[:size]<EOL>working_str = working_str[size:]<EOL>assert not self.sendline(ShutItSendSpec(self,<EOL>send='<STR_LIT:U+0020>' + shutit.get_command('<STR_LIT>') + '''<STR_LIT>''' + fname + """<STR_LIT>""" + random_id + """<STR_LIT>""" + curr_str + """<STR_LIT>""" + random_id,<EOL>ignore_background=True)), shutit_util.print_debug()<EOL>self.expect(expect)<EOL><DEDENT>assert not self.sendline(ShutItSendSpec(self,<EOL>send='<STR_LIT>' + fname,<EOL>ignore_background=True)), shutit_util.print_debug()<EOL>self.expect(expect)<EOL>return fname<EOL>
Internal function. Do not use. Takes a long command, and puts it in an executable file ready to run. Returns the filename.
f10349:c0:m54
def _pause_input_filter(self, input_string):
<EOL>shutit = self.shutit<EOL>if len(input_string) == <NUM_LIT:1>:<EOL><INDENT>if ord(input_string) == <NUM_LIT> and shutit.build['<STR_LIT>'] == '<STR_LIT>':<EOL><INDENT>shutit.log('<STR_LIT>', level=logging.INFO)<EOL>shutit.do_repository_work('<STR_LIT>', password=shutit.host['<STR_LIT:password>'], docker_executable=shutit.host['<STR_LIT>'], force=True)<EOL>shutit.log('<STR_LIT>', level=logging.CRITICAL)<EOL><DEDENT>elif ord(input_string) == <NUM_LIT:4>:<EOL><INDENT>shutit.log("""<STR_LIT>""", level=logging.INFO, transient=True)<EOL>return '<STR_LIT>'<EOL><DEDENT>elif ord(input_string) == <NUM_LIT:8>:<EOL><INDENT>shutit_global.shutit_global_object.signal_id = <NUM_LIT:8><EOL>return '<STR_LIT>'<EOL><DEDENT>elif ord(input_string) == <NUM_LIT:7>:<EOL><INDENT>shutit_global.shutit_global_object.signal_id = <NUM_LIT:7><EOL>return '<STR_LIT>'<EOL><DEDENT>elif ord(input_string) == <NUM_LIT:16>:<EOL><INDENT>shutit_global.shutit_global_object.signal_id = <NUM_LIT:16><EOL>if shutit.build['<STR_LIT>'] and self.shutit.loglevel not in ('<STR_LIT>','<STR_LIT>'):<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>return '<STR_LIT>'<EOL><DEDENT>elif ord(input_string) == <NUM_LIT>:<EOL><INDENT>shutit_global.shutit_global_object.signal_id = <NUM_LIT><EOL>if not shutit.build['<STR_LIT>'] and self.shutit.loglevel not in ('<STR_LIT>',):<EOL><INDENT>shutit.log('<STR_LIT>',transient=True, level=logging.CRITICAL)<EOL>shutit_global.shutit_global_object.handle_exit(exit_code=<NUM_LIT:1>)<EOL><DEDENT><DEDENT>elif ord(input_string) == <NUM_LIT>:<EOL><INDENT>shutit_global.shutit_global_object.signal_id = <NUM_LIT><EOL>return '<STR_LIT>'<EOL><DEDENT>elif ord(input_string) == <NUM_LIT>:<EOL><INDENT>shutit_global.shutit_global_object.signal_id = <NUM_LIT><EOL>return '<STR_LIT>'<EOL><DEDENT><DEDENT>return input_string<EOL>
Input filter for pause point to catch special keystrokes
f10349:c0:m56