def jexec(self, cmd, jid): ''' Runs a command within a jail :param cmd: String :param jid: String jail id :return: command output :example: ``` - freebsd.terminal.jexec - "echo 'running within jail'" - "2" ``` ''' jexec_cmd = "set -e; sudo -E /usr/sbin/jexec %s %s -c '%s'" % (jid, '/bin/sh', cmd) if self.verbose: print message('info',"[jexec] %s" % jexec_cmd,debug=self.debug) print "" session = shell(jexec_cmd) if not session.get('code') == 0: raise Exception(session.get('stdout')) return session.get('stdout')
def manifest(self, file, silent, debug, **defaults): ''' This allows you to link a external manifests into your script :param file: String, path to file :param silent: Boolean :param debug: Boolean ``` - system.include.manifest: - '/path/to/manifest.yaml' - False - False - VAR1: "SOMEVALUE" ``` ''' print message('info',"[include] @ %s" % file,debug=self.debug) for key, value in defaults.iteritems(): print message('info',"%s: %s" % (key,value),debug=self.debug) self.EnvManager.set(key, value) with Run(file,silent,debug=debug) as main_actions: main_actions.add_envs(**defaults) main_actions.set_envs() main_actions.execute_actions()
def clone(self, user, new_local_path, remote, **defaults): ''' Clone a git repo :param user: String, username :param new_local_path: String, full path and desired dir name :param remote: String, git repo :param branch: define the branch to checkout :param depth: to perform a shallow clone :example: ``` - scm.git.clone: - "root" - "/tmp/goephor" - "[email protected]:eng-tools/goephor" ``` ''' print message('info', "[clone] @ %s -> %s" % (remote, new_local_path), debug=self.debug) for key, value in defaults.iteritems(): print message('info', "%s=%s" % (key, value), debug=self.debug) print "" repo = Repo_actions(new_local_path, user=user) if not defaults.get('branch'): defaults['branch'] = 'master' has_cloned = repo.clone(remote, **defaults) if has_cloned: return True else: raise Exception('Unable to Clone %s' % remote)
def get_key(self, data, key, **defaults): ''' Get a key from json string :param data: String :param key: String :return: String, value :note: Can add future support for different data formats :example: ``` - string.utils.get_key: - Somejsonhere - somekey - set_env: SOMEVAL ``` ''' if self.is_json(data): data = json.loads(data) value = self.traverse(data, key) if self.verbose: print message('info', "[value] is [%s]" % (str(value)), debug=self.debug) if not value: return None return value else: raise Exception('Must be well formed json string')
def clone(self, user, new_local_path, remote, **defaults): ''' Clone a git repo :param user: String, username :param new_local_path: String, full path and desired dir name :param remote: String, git repo :param branch: define the branch to checkout :param depth: to perform a shallow clone :example: ``` - scm.git.clone: - "root" - "/tmp/goephor" - "[email protected]:eng-tools/goephor" ``` ''' print message('info',"[clone] @ %s -> %s" % (remote, new_local_path),debug=self.debug) for key, value in defaults.iteritems(): print message('info',"%s=%s" % (key, value),debug=self.debug) print "" repo = Repo_actions(new_local_path, user=user) if not defaults.get('branch'): defaults['branch']='master' has_cloned = repo.clone(remote,**defaults) if has_cloned: return True else: raise Exception('Unable to Clone %s' % remote)
def latest_commit(self, user, local_path, branch, info_type, **defaults): ''' get the latest commit info from a branch :param user: String, username :param local_path: String, full path and desired dir name :param branch: String :param info_type: String, sha1, message, author :return: String :example: ``` - scm.git.latest_commit: - "root" - "/tmp/goephor" - "refactor" - "sha1" ``` ''' repo = Repo_actions(local_path, user=user) branch_obj = Branch_actions(repo) commit_obj = Commit_actions(repo) branch_is = branch_obj.checkout(branch) if not branch_is: raise Exception('Unable to checkout %s' % branch) latest = commit_obj.latest() latest = latest.get(info_type,None) if self.verbose: print message('info',"[latest_commit] %s is %s" % (info_type,latest),debug=self.debug) return latest
def traverse(self, data, key): ''' Private recursively traverse nested json data :param data: Nested dict/list :param key: String :return: value ''' values = [] if isinstance(data, list): for items in data: value = self.traverse(items, key) values.append(''.join(value)) return ''.join(values) elif isinstance(data, dict): for k, v in data.iteritems(): if self.verbose: print message('info', "%s: %s" % (k, v), debug=self.debug) if k == key: return str(v) if isinstance(v, list) or isinstance(v, dict): value = self.traverse(v, key) values.append(''.join(value)) return ''.join(values) else: if values: return values
def jls(self, hostname, return_type, **defaults): ''' Runs the jls command :param hostname: String :param return_type: String options: path,jid,ip4.addr uses jls -n <opt.name> :return: String of return_type :example: ``` - freebsd.terminal.jls - "eng-sea-build10" - "jid" ``` ''' session = shell('/usr/sbin/jls -n name %s' % (return_type)) if not session.get('code') == 0: raise Exception(session.get('stdout')) output = session.get("stdout").split("\n") for line in output: if hostname in line: jail_line = re.split('\s+', line) print message('info',str(jail_line),debug=self.debug) if return_type in jail_line[1]: jail_info = jail_line[1].split('=')[1] print message('info',jail_info,debug=self.debug) return jail_info else: error = "invalid return_type %s" % (return_type) raise Exception(error) error = "jls command failure" raise Exception(error)
def latest_commit(self, user, local_path, branch, info_type, **defaults): ''' get the latest commit info from a branch :param user: String, username :param local_path: String, full path and desired dir name :param branch: String :param info_type: String, sha1, message, author :return: String :example: ``` - scm.git.latest_commit: - "root" - "/tmp/goephor" - "refactor" - "sha1" ``` ''' repo = Repo_actions(local_path, user=user) branch_obj = Branch_actions(repo) commit_obj = Commit_actions(repo) branch_is = branch_obj.checkout(branch) if not branch_is: raise Exception('Unable to checkout %s' % branch) latest = commit_obj.latest() latest = latest.get(info_type, None) if self.verbose: print message('info', "[latest_commit] %s is %s" % (info_type, latest), debug=self.debug) return latest
def _str_to_dict(self, data): ''' Convert json string to dict :param data: String :return: Dictionary ''' if self.verbose: print message('info',"[_str_to_dict] %s" % (data),debug=self.debug) data = json.loads(data) return data
def println(self, msg_type, text, **defaults): ''' Generic print line :param msg_type: String: header, info, success, warning, fail, error :param text: String ``` - string.utils.println: - 'info' - "HELLO WORLD" ``` ''' print message(msg_type, "%s" % (text), debug=self.debug)
def _str_to_dict(self, data): ''' Convert json string to dict :param data: String :return: Dictionary ''' if self.verbose: print message('info', "[_str_to_dict] %s" % (data), debug=self.debug) data = json.loads(data) return data
def date(self, prefix, **defaults): ''' get the current date :param prefix: %m/%d/%y" :example: ``` - release.utils.date: - '%m/%d/%y' ``` ''' date = datetime.datetime.now().strftime(prefix) if self.verbose: print message('info', "[date] %s" % (str(date)), debug=self.debug) return date
def has_path(self,path_str,**defaults): ''' Checks if a pth exists :param path_str: String :return: boolean :example: ``` - environment.utils.has_path: - "/some/path" - set_env: "VAR1" ``` ''' output = os.path.exists(path_str) if self.verbose: print message('info',"[has_path] %s @ %s" % (output,path_str),debug=self.debug) return output
def date(self, prefix, **defaults): ''' get the current date :param prefix: %m/%d/%y" :example: ``` - release.utils.date: - '%m/%d/%y' ``` ''' date = datetime.datetime.now().strftime(prefix) if self.verbose: print message('info',"[date] %s" % (str(date)),debug=self.debug) return date
def unset(self, key, **defaults): ''' Unset an environment variable :param key: String :param value: String :example: ``` - environment.env.unset: - "VAR1" ``` ''' self.EnvManager.unset(key) if self.verbose: print message('info',"[unset] %s" % (key),debug=self.debug)
def read(self, path, **defaults): ''' Reads in a custom receipt and generates environment variables :param defaults: additional params :note: Consumes only files from a custom receipt :example: ``` - receipt.maker.read: - "receipt.yaml" ``` ''' print message('info',"[read] %s" % path,debug=self.debug) data = self._to_dict(path) for key, value in data.iteritems(): self.EnvManager.set(key, value) if self.verbose: print message('info',"[set] %s=%s" % (key, value),debug=self.debug)
def delete(self, local_path, **defaults): ''' Delete a local repo :param local_path: String :example: ``` - scm.git.delete: - "/tmp/goephor" ``` ''' repo = Repo_actions(local_path) has_attached = repo.attach(local_path) if has_attached: session = shell("rm -rf %s" % (local_path)) if session.get('code') == 0: return True else: print message('info',"[Pass] not a repo @ %s " % (local_path),debug=self.debug)
def set(self, key, value, **defaults): ''' Set an environment variable :param key: String :param value: String :example: ``` - environment.env.set: - "VAR1" - "some value" ``` ''' self.EnvManager.set(key, value) if self.verbose: print message('info',"[set] %s=%s" % (key, value),debug=self.debug)
def replace(self, text, old, new, **defaults): ''' String replace on environment variable :param text: String :param old: substring :param new: replaced value ``` - string.utils.replace: - "variable" - "old substring" - "new substring" ``` ''' new_str = text.replace(old, new) if self.verbose: print message('info', "[replace] %s -> %s" % (text, new_str), debug=self.debug) return new_str
def on_actions(self, path, **defaults): ''' This creates a receipt of all actions in the chain :param path: String, system path to put receipt :param defaults: additional params :example: ``` - receipt.maker.on_actions: - "./receipt.yaml" ``` ''' print message('info', "[on_actions] %s" % (path), debug=self.debug) receipt = {} receipt["results"] = [] for action in self.action_manager.chain: result = action.get_receipt() receipt.get("results").append(result) self._to_file(receipt, path)
def custom(self, path, **defaults): ''' Create a custom receipt from key/value pairs in defaults :param path: String, system path to put receipt :param defaults: additional params :example: ``` - receipt.maker.custom: - "./receipt.yaml" - var1: "SOMEVALUE1" - var2: "SOMEVALUE2" - var3: "SOMEVALUE3" ``` ''' print message('info', "[custom] %s" % (path), debug=self.debug) for key, value in defaults.iteritems(): print message('info', "%s: %s" % (key, value), debug=self.debug) self._to_file(defaults, path)
def on_actions(self, path, **defaults): ''' This creates a receipt of all actions in the chain :param path: String, system path to put receipt :param defaults: additional params :example: ``` - receipt.maker.on_actions: - "./receipt.yaml" ``` ''' print message('info',"[on_actions] %s" % (path),debug=self.debug) receipt = {} receipt["results"] = [] for action in self.action_manager.chain: result = action.get_receipt() receipt.get("results").append(result) self._to_file(receipt, path)
def custom(self, path, **defaults): ''' Create a custom receipt from key/value pairs in defaults :param path: String, system path to put receipt :param defaults: additional params :example: ``` - receipt.maker.custom: - "./receipt.yaml" - var1: "SOMEVALUE1" - var2: "SOMEVALUE2" - var3: "SOMEVALUE3" ``` ''' print message('info',"[custom] %s" % (path),debug=self.debug) for key, value in defaults.iteritems(): print message('info',"%s: %s" % (key, value),debug=self.debug) self._to_file(defaults, path)
def rsync(self, user, rsa_private_path, server, src, dest, option, **defaults): ''' Perform an rsync :param user: String :param rsa_private_path: String :param server: String :param src: String, source dir :param dest: String, dest dir :param options: String, push,pull :example: ``` - system.terminal.rsync: - "root" - "~/.ssh/id_rsa" - "Some.Server.Name" - "/tmp/remote" - "/tmp/local" - "pull" ``` ''' if self.verbose: print message('info',"[rsync] %s [%s] -> [%s]" % (option, src, dest),debug=self.debug) session = rsync(server, src, dest, user=user, rsa_private=rsa_private_path, option=option, excludes=defaults.get('excludes',None), verbose=defaults.get('verbose',False)) if not session.get('code') == 0: raise Exception(session.get('stdout')) return session.get('stdout')
def add(self, path, json_str, **defaults): ''' Add to an existing receipt :param path: String, path to existing file :param json_str: String, using json syntax add to receipt :param to_json: Boolean, write file out as json :note: json syntax, {hello:{world}} :example: ``` - receipt.maker.add: - "./custom.yaml" - '{"HELLO":["WORLD","05/10/14"]}' ``` ''' data = None print message('info', "[add] %s" % path, debug=self.debug) data = self._to_dict(path) json_dict = self._str_to_dict(json_str) data.update(json_dict) self._to_file(data, path)
def custom_json(self, path, data,**defaults): ''' produces output file from json data :param path: String :param data: String :example: ``` - receipt.maker.custom_json: - path/put/file - somejsonhere ``` ''' if self.is_json(data): data = json.loads(data) if self.verbose: print message('info',"[custom_json] @ %s" % (path),debug=self.debug) print message('info'," %s" % (str(data)),debug=self.debug) self._to_file(data, path) else: raise Exception('Must be well formed json string')
def read(self, path, **defaults): ''' Reads in a custom receipt and generates environment variables :param defaults: additional params :note: Consumes only files from a custom receipt :example: ``` - receipt.maker.read: - "receipt.yaml" ``` ''' print message('info', "[read] %s" % path, debug=self.debug) data = self._to_dict(path) for key, value in data.iteritems(): self.EnvManager.set(key, value) if self.verbose: print message('info', "[set] %s=%s" % (key, value), debug=self.debug)
def delete(self, local_path, **defaults): ''' Delete a local repo :param local_path: String :example: ``` - scm.git.delete: - "/tmp/goephor" ``` ''' repo = Repo_actions(local_path) has_attached = repo.attach(local_path) if has_attached: session = shell("rm -rf %s" % (local_path)) if session.get('code') == 0: return True else: print message('info', "[Pass] not a repo @ %s " % (local_path), debug=self.debug)
def pad(self, text, fill, amount, **defaults): ''' Provides generic padding to numbers and strings :param text: String :param fill: Char :param amount: Int :return: String :example: ``` - release.utils.pad: - '9' - '0' - 3 - set_env: "PAD" ``` ''' output = text.rjust(amount,fill) if self.verbose: print message('info',"[pad] is %s" % (str(output)),debug=self.debug) return output
def _to_file(self, data, path): ''' Private, convert dict to file :param path: String ''' file_type = path.rsplit(".", 1)[1] with open(path, 'w') as file: if 'json' in file_type: if self.verbose: print message('info', "[_to_file] json", debug=self.debug) file.write(json.dumps(data, indent=4, sort_keys=True)) elif 'txt' in file_type: if self.verbose: print message('info', "[_to_file] txt", debug=self.debug) for key, value in data.iteritems(): pair = "%s=%s\n" % (key, value) file.write(pair) else: if self.verbose: print message('info', "[_to_file] yaml", debug=self.debug) file.write( yaml.dump(data, default_flow_style=False, allow_unicode=True))
def _to_file(self, data, path): ''' Private, convert dict to file :param path: String ''' file_type = path.rsplit(".", 1)[1] with open(path, 'w') as file: if 'json' in file_type: if self.verbose: print message('info',"[_to_file] json",debug=self.debug) file.write(json.dumps(data, indent=4, sort_keys=True)) elif 'txt' in file_type: if self.verbose: print message('info',"[_to_file] txt",debug=self.debug) for key, value in data.iteritems(): pair = "%s=%s\n" % (key, value) file.write(pair) else: if self.verbose: print message('info',"[_to_file] yaml",debug=self.debug) file.write(yaml.dump(data, default_flow_style=False, allow_unicode=True))
def custom_json(self, path, data, **defaults): ''' produces output file from json data :param path: String :param data: String :example: ``` - receipt.maker.custom_json: - path/put/file - somejsonhere ``` ''' if self.is_json(data): data = json.loads(data) if self.verbose: print message('info', "[custom_json] @ %s" % (path), debug=self.debug) print message('info', " %s" % (str(data)), debug=self.debug) self._to_file(data, path) else: raise Exception('Must be well formed json string')
def manifest(self, file, silent, debug, **defaults): ''' This allows you to link a external manifests into your script :param file: String, path to file :param silent: Boolean :param debug: Boolean ``` - system.include.manifest: - '/path/to/manifest.yaml' - False - False - VAR1: "SOMEVALUE" ``` ''' print message('info', "[include] @ %s" % file, debug=self.debug) for key, value in defaults.iteritems(): print message('info', "%s: %s" % (key, value), debug=self.debug) self.EnvManager.set(key, value) with Run(file, silent, debug=debug) as main_actions: main_actions.add_envs(**defaults) main_actions.set_envs() main_actions.execute_actions()
def add(self, path, json_str, **defaults): ''' Add to an existing receipt :param path: String, path to existing file :param json_str: String, using json syntax add to receipt :param to_json: Boolean, write file out as json :note: json syntax, {hello:{world}} :example: ``` - receipt.maker.add: - "./custom.yaml" - '{"HELLO":["WORLD","05/10/14"]}' ``` ''' data = None print message('info',"[add] %s" % path,debug=self.debug) data = self._to_dict(path) json_dict = self._str_to_dict(json_str) data.update(json_dict) self._to_file(data, path)
def substring(self, text, regex, **defaults): ''' Allows you to parse strings using regex :param text: String :param regex: String :return: String :example: ``` - string.utils.substring: - "SOME_STRING" - "S(.+?)G" - set_env: SOMEVAL ``` ''' sub = re.search(regex, text) substr = None if sub: substr = sub.group(1) if self.verbose: print message('info', "[substring] is %s" % (substr), debug=self.debug) return substr
def rsync(self, user, rsa_private_path, server, src, dest, option, **defaults): ''' Perform an rsync :param user: String :param rsa_private_path: String :param server: String :param src: String, source dir :param dest: String, dest dir :param options: String, push,pull :example: ``` - system.terminal.rsync: - "root" - "~/.ssh/id_rsa" - "Some.Server.Name" - "/tmp/remote" - "/tmp/local" - "pull" ``` ''' if self.verbose: print message('info', "[rsync] %s [%s] -> [%s]" % (option, src, dest), debug=self.debug) session = rsync(server, src, dest, user=user, rsa_private=rsa_private_path, option=option, excludes=defaults.get('excludes', None), verbose=defaults.get('verbose', False)) if not session.get('code') == 0: raise Exception(session.get('stdout')) return session.get('stdout')
def HAS_TOKEN(self, token, output): ''' Check if a string exists in some output :param token: String :param output: String :return: Boolean ``` - condition.statement.HAS_TOKEN: - ${token} - ${output} -set_env: VAR1 ``` ''' output = output.split('\n') for line in output: if token in line: if self.verbose: print message('info', "[Found] %s" % (token), debug=self.debug) return True if self.verbose: print message('info', "[Not Found] %s" % (token), debug=self.debug) return False
def _configparser(self, path): ''' Private, adds configparser values to environment ''' cfg = ConfigParser.SafeConfigParser() cfg.read(path) sections = cfg.sections() if not sections: raise Exception('Unable to read config @ %s' % (path)) for section in sections: for pairs in cfg.items(section): key = pairs[0] value = self.action_manager.EnvManager._sanitize(pairs[1]) self.action_manager.EnvManager.set(key, value) msg = message('info', "[set] %s=" % (key), debug=self.debug) print "%s%s" % (msg, value)
def _configparser(self,path): ''' Private, adds configparser values to environment ''' cfg = ConfigParser.SafeConfigParser() cfg.read(path) sections = cfg.sections() if not sections: raise Exception('Unable to read config @ %s' % (path)) for section in sections: for pairs in cfg.items(section): key = pairs[0] value = self.action_manager.EnvManager._sanitize(pairs[1]) self.action_manager.EnvManager.set(key,value) msg = message('info',"[set] %s=" % (key),debug=self.debug) print "%s%s" % (msg,value)
def next(self, path, new_release): ''' Get the next available release from Release.json for a given build :param path: String, path to Releases.json :param new_release: String, release name 7.1.1 :note: will only use first three positions :example: ``` - release.utils.next: - './Release.json' - '7.1.1' - set_env: "NEXT_REL" ``` ''' if len(new_release.split('.')) > 3: new_split = new_release.rsplit('.', 1) new_release = new_split[0] print message('info', "[info] compare using %s" % new_release, debug=self.debug) file_type = path.rsplit(".", 1)[1] try: with open(path) as file: if 'json' in file_type: releases = json.loads(file.read()) else: releases = yaml.load(file) except Exception: error = "unable to read %s" % (path) raise Exception(error) minor = 0 for name, values in releases.iteritems(): if self.compare(new_release, name): print message('info', "[match] %s" % name, debug=self.debug) old_minor = int(name.rsplit('.', 1)[1]) if minor < old_minor: minor = old_minor if minor >= 0: next = "%s.%s" % (new_release, str(minor + 1)) else: next = "%s.%s" % (new_release, str(minor)) print message('info', "[next] %s" % (next), debug=self.debug) return next
def next(self, path, new_release): ''' Get the next available release from Release.json for a given build :param path: String, path to Releases.json :param new_release: String, release name 7.1.1 :note: will only use first three positions :example: ``` - release.utils.next: - './Release.json' - '7.1.1' - set_env: "NEXT_REL" ``` ''' if len(new_release.split('.')) > 3: new_split = new_release.rsplit('.', 1) new_release = new_split[0] print message('info',"[info] compare using %s" % new_release,debug=self.debug) file_type = path.rsplit(".", 1)[1] try: with open(path) as file: if 'json' in file_type: releases = json.loads(file.read()) else: releases = yaml.load(file) except Exception: error = "unable to read %s" % (path) raise Exception(error) minor = -1 for name, values in releases.iteritems(): if self.compare(new_release, name): print message('info',"[match] %s" % name,debug=self.debug) old_minor = int(name.rsplit('.', 1)[1]) if minor < old_minor: minor = old_minor if minor >= -1: next = "%s.%s" % (new_release, str(minor+1)) else: next = "%s.%s" % (new_release, str(minor)) print message('info',"[next] %s" % (next),debug=self.debug) return next
def send(self, req_type, base_url, url_ext, **defaults): ''' Performs a http restful call :param type: String, PUT,GET :param base_url: String :param url_ext: String :param params: json string :param data: json string :param silent: boolean :example: ``` - http.rest.send: - "GET" - "https://build.west.isilon.com" - "api/branch" - params: '{"name":"${BRANCH_NAME}"}' - data: '{"name":"${BRANCH_NAME}"}' ``` ''' silent = False if defaults.get('silent', None) is True: silent = True defaults.pop("silent", None) if self.verbose: print message('info', "[send] %s @ %s/%s" % (req_type, base_url, url_ext), debug=self.debug) for key, value in defaults.iteritems(): print message('info', "%s: %s" % (key, value), debug=self.debug) session = Restful(base_url) output = session.send(req_type, url_ext, **defaults) if self.verbose: if not silent: print message('info', output, debug=self.debug) if output.get('code') >= 300: error = "[%s] http request failed @ %s/%s" % (str( output.get('code')), base_url, url_ext) raise Exception(error) return output.get('response')
def send(self, req_type, base_url, url_ext, **defaults): ''' Performs a http restful call :param type: String, PUT,GET :param base_url: String :param url_ext: String :param params: json string :param data: json string :param silent: boolean :example: ``` - http.rest.send: - "GET" - "https://build.west.isilon.com" - "api/branch" - params: '{"name":"${BRANCH_NAME}"}' - data: '{"name":"${BRANCH_NAME}"}' ``` ''' silent = False if defaults.get('silent',None) is True: silent = True defaults.pop("silent", None) if self.verbose: print message('info',"[send] %s @ %s/%s" % (req_type,base_url,url_ext),debug=self.debug) for key,value in defaults.iteritems(): print message('info',"%s: %s" % (key,value),debug=self.debug) session = Restful(base_url) output = session.send(req_type, url_ext,**defaults) if self.verbose: if not silent: print message('info',output,debug=self.debug) if output.get('code') >= 300: error = "[%s] http request failed @ %s/%s" % (str(output.get('code')),base_url,url_ext) raise Exception(error) return output.get('response')
def _to_dict(self, path): ''' Private, Load a file in and output a dict :param path: String :param is_string: String path is a string do not load file :return: Dictionary ''' file_type = path.rsplit(".", 1)[1] with open(path) as file: if 'json' in file_type: if self.verbose: print message('info', "[_to_dict] json", debug=self.debug) data = json.loads(file.read()) return data else: if self.verbose: print message('info', "[_to_dict] yaml", debug=self.debug) data = yaml.load(file) return data print message('info', "[None]", debug=self.debug) return None
def _to_dict(self, path): ''' Private, Load a file in and output a dict :param path: String :param is_string: String path is a string do not load file :return: Dictionary ''' file_type = path.rsplit(".", 1)[1] with open(path) as file: if 'json' in file_type: if self.verbose: print message('info',"[_to_dict] json",debug=self.debug) data = json.loads(file.read()) return data else: if self.verbose: print message('info',"[_to_dict] yaml",debug=self.debug) data = yaml.load(file) return data print message('info',"[None]",debug=self.debug) return None
def runme(self, var1, var2, **defaults): ''' This is an example of setting up an action :param var1: String :param var2: String :return: runme_output :example: ``` - example.example.runme: - "hello" - "world" ``` ''' print message('info',"This var [%s] was passed in " % var1,debug=self.debug) print message('info',"This var [%s] was passed in " % var2,debug=self.debug) print "\nhere are the defaults:" for key, value in defaults.iteritems(): print message('info' "%s : %s" % (key, value),debug=self.debug) return "runme_output"
def runme(self, var1, var2, **defaults): ''' This is an example of setting up an action :param var1: String :param var2: String :return: runme_output :example: ``` - example.example.runme: - "hello" - "world" ``` ''' print message('info', "This var [%s] was passed in " % var1, debug=self.debug) print message('info', "This var [%s] was passed in " % var2, debug=self.debug) print "\nhere are the defaults:" for key, value in defaults.iteritems(): print message('info' "%s : %s" % (key, value), debug=self.debug) return "runme_output"
def IF(self, arg1, operator, arg2, THEN=[], ELSE=[]): ''' Represents an if statement :param arg1: int,str :param operator: String, follows python rules :param arg12 int,str :param THEN: List, several other actions :param ELSE: List, several other actions :example: ``` - condition.statement.IF: - "${var1}" - "==" - "${var2}" - THEN: - system.terminal.shell: - "echo 'THEN IS HAPPENING'" - ELSE: - system.terminal.shell: - "echo 'ELSE IS HAPPENING'" ``` ''' if arg1.isdigit() and arg2.isdigit(): statem = "%d %s %d" % (int(arg1), operator, int(arg2)) else: statem = "'%s' %s '%s'" % (arg1, operator, arg2) if self.verbose: print message('header', "\n[eval] %s" % statem, debug=self.debug) if eval(statem): if self.verbose: print message('header', "\n[THEN]", debug=self.debug) self.add_obj(THEN) else: if self.verbose: print message('header', "\n[ELSE]", debug=self.debug) self.add_obj(ELSE)