def _clone(args, src_dir): if os.path.exists(src_dir): raise CommandError("Cannot clone into %s, path already exists" % src_dir) Dodo.run( ["git", "clone", args.git_url, os.path.basename(src_dir)] + (["--depth", args.depth] if args.depth else []), cwd=os.path.dirname(src_dir), ) if args.branch: Dodo.run(["git", "checkout", args.branch], cwd=src_dir)
def check_setuptools(): if not Dodo.run([_python_path(), "-c", "import setuptools"]): _report_error("\n" + bordered( "Error: your python version does not have setuptools installed.\n" + "Check the settings.python option in %s" % Paths().global_config_filename())) sys.exit(1)
def _copy_extra_dirs(local_dir, extra_dirs): # todo: convert extra dirs config mapping into a list of key=val for extra_dir in extra_dirs or []: extra_dir_name, extra_dir_path = extra_dir.split("=") local_path = os.path.join(local_dir, extra_dir_name) if os.path.exists(local_path): raise CommandError("Cannot copy to existing path: " + local_path) if not os.path.exists(extra_dir_path): raise CommandError("Cannot copy from non-existing path: " + extra_dir_path) Dodo.run(["cp", "-rf", extra_dir_path, local_path]) if os.path.abspath(local_dir).startswith(os.path.abspath(extra_dir_path)): rp = os.path.relpath(local_dir, extra_dir_path) dead_path = os.path.join(local_path, rp) if os.path.exists(dead_path): Dodo.run(["rm", "-rf", dead_path])
def _install_package(package, as_directory, install_commands_function, add_to_defaults): """Install the dir with the global commands.""" package_dirname = as_directory or package.replace('-', '_') dest_dir = os.path.join(Paths().global_commands_dir(), package_dirname) defaults_dest_dir = os.path.join(Paths().default_commands_dir(), package_dirname) if add_to_defaults and os.path.exists(defaults_dest_dir): _report_error("Error: already installed: %s" % defaults_dest_dir) return False if not install_commands_function(dest_dir): return False if add_to_defaults: if not os.path.exists(dest_dir): _report_error("Error: not found: %s" % dest_dir) return False Dodo.run(["ln", "-s", dest_dir, defaults_dest_dir]) return True
def _copy_defaults(args, shared_config_dir): config_dir = os.path.join(args.project_dir, ".dodo_commands") has_printed_header = False for filename in glob.glob(os.path.join(shared_config_dir, "*")): dest_path = os.path.join(config_dir, os.path.basename(filename)) if os.path.exists(dest_path): if args.confirm: if not has_printed_header: has_printed_header = True print( "\nCopying shared environment files to your local environment...\n" ) print( "Warning, destination path already exists: %s. Overwrite it?" % dest_path) elif args.use_force: print("Overwriting existing path: %s" % dest_path) else: raise CommandError( "Destination path %s already exists. " % dest_path + "Use the --confirm or --force flag to overwrite it.") Dodo.run(["cp", "-rf", filename, dest_path])
def _install_commands_from_path(path, dest_dir, mv=False): """Install the dir with the global commands.""" if not os.path.exists(path): alt_path = os.path.join(Paths().extra_dir(), path) if os.path.exists(alt_path): path = alt_path else: _report_error("Error: path not found: %s" % path) return False if os.path.exists(dest_dir): _report_error("Error: already installed: %s" % dest_dir) return False if mv: Dodo.run(["mv", path, dest_dir]) else: try: Dodo.run(["ln", "-s", os.path.abspath(path), dest_dir]) except Exception: _report_error("Error: could not create a symlink in %s." % dest_dir) return True
from dodo_commands import Dodo from dodo_commands.framework.decorator_scope import DecoratorScope from dodo_commands.framework.util import to_arg_list from dodo_docker_commands.decorators.docker import invert_path def _args(): Dodo.parser.add_argument("yarn_args", nargs="?") Dodo.parser.add_argument("--name") Dodo.parser.add_argument("--local", "-l", action="store_true") args = Dodo.parse_args() args.yarn = "yarn" args.cwd = Dodo.get_config("/NODE/cwd") return args if Dodo.is_main(__name__, safe=True): args = _args() if args.name: Dodo.get_config("/DOCKER").setdefault("options", {}).setdefault( "yarn", {})["name"] = args.name if args.local: with DecoratorScope("docker", remove=True): Dodo.run([args.yarn, *to_arg_list(args.yarn_args)], cwd=invert_path(args.cwd)) else: Dodo.run([args.yarn, *to_arg_list(args.yarn_args)], cwd=args.cwd)
def _remove_extra_dirs(local_dir, extra_dirs): for extra_dir in extra_dirs or []: extra_dir_name, extra_dir_path = extra_dir.split("=") local_path = os.path.join(local_dir, extra_dir_name) Dodo.run(["rm", "-rf", local_path]) if Dodo.is_main(__name__): args = _args() Dodo.safe = len(args.extra_dirs) == 0 _copy_extra_dirs(args.build_dir, args.extra_dirs) try: Dodo.run( [ "docker", "build", "-t", args.docker_image, "-f", args.docker_file, *to_arg_list(args.build_args), ".", ], cwd=args.build_dir, ) # noqa finally: _remove_extra_dirs(args.build_dir, args.extra_dirs)
Dodo.parser.add_argument("--cat", action="store_true") Dodo.parser.add_argument("--edit", action="store_true") args = Dodo.parse_args() args.cwd = Dodo.get_config("/MAKE/cwd") args.file = Dodo.get_config("/MAKE/file", "Makefile") global_config = load_global_config_parser() args.editor = global_config_get(global_config, "settings", "editor") # Raise an error if something is not right if False: raise CommandError("Oops") return args # Use safe=False if the script makes changes other than through Dodo.run if Dodo.is_main(__name__, safe=True): args = _args() if args.cat: Dodo.run(["cat", args.file], cwd=args.cwd) elif args.edit: with DecoratorScope("docker", remove=True): Dodo.run([args.editor, args.file], cwd=invert_path(args.cwd)) else: file_args = ["-f", args.file] if args.file != "Makefile" else [] Dodo.run(["make", *file_args, *to_arg_list(args.make_args)], cwd=args.cwd)
from argparse import ArgumentParser from dodo_commands import Dodo def _args(): # Create the parser parser = ArgumentParser(description='') parser.add_argument('version') # Use the parser to create the command arguments args = Dodo.parse_args(parser, config_args=[]) args.cwd = Dodo.get('/ROOT/src_dir') return args # Use safe=False if the script makes changes other than through Dodo.run if Dodo.is_main(__name__, safe=True): args = _args() Dodo.run(['git', 'tag', '-a', args.version, '-m', args.version], cwd=args.cwd) print("Done. Now run\ngit push origin " + args.version)
def commit_ssh_server(target_container_name, target_docker_image): # commit the container Dodo.run(['docker', 'commit', target_container_name, target_docker_image]) # stop the container Dodo.run(['docker', 'stop', target_container_name])
else: salt_master_container_name = 'salt_master_deploying_to_%s' % args.host_name roster_filename = "roster" salt_master_filename = _write_salt_master_file( args.salt_top_dir, os.path.join(_srv_salt_src_dir(), args.top_dir)) if args.verbose: with open(salt_master_filename) as ifs: print('I have created the following temporary Salt master file:\n') print(ifs.read()) if args.bash: cmd = ['/bin/bash'] else: cmd = ([ 'salt-ssh', # '-l', 'debug', '-i', '--roster-file=./%s' % os.path.basename(roster_filename), args.host_name ] + (['state.sls', 'debug'] if args.debug else ['state.apply'])) _create_docker_options(args.salt_src_dir, salt_master_filename) with DecoratorScope('docker'): Dodo.run(cmd, cwd=os.path.join(_srv_salt_src_dir(), args.top_dir)) os.unlink(salt_master_filename) if args.target_docker_image: commit_ssh_server(target_container_name, args.target_docker_image) os.unlink(roster_filename)
from dodo_commands import Dodo def _args(): parser = ArgumentParser() parser.add_argument("where") parser.add_argument("what") parser.add_argument("--pattern", default="*") parser.add_argument("--replace") args = Dodo.parse_args(parser) return args def _replace(where, what, replace_with): for filepath in glob.iglob(os.path.join(where, "**/" + args.pattern), recursive=True): with open(filepath) as file: s = file.read() s2 = s.replace(what, replace_with) if s != s2: with open(filepath, "w") as file: file.write(s2) args = _args() if Dodo.is_main(__name__, safe=not args.replace): if args.replace: _replace(args.where, args.what, args.replace) else: Dodo.run(["grep", "-rnw", args.where, "-e", "'{}'".format(args.what)])
def _remove_extra_dirs(local_dir, extra_dirs): for extra_dir in extra_dirs or []: extra_dir_name, extra_dir_path = extra_dir.split("=") local_path = os.path.join(local_dir, extra_dir_name) Dodo.run(["rm", "-rf", local_path])
from argparse import ArgumentParser from dodo_commands import Dodo from dodo_commands.framework.global_config import load_global_config_parser def _args(): parser = ArgumentParser(description="") parser.add_argument("--url") # Parse the arguments. args = Dodo.parse_args(parser) args.file_browser = load_global_config_parser().get( "settings", "file_browser") args.browser = load_global_config_parser().get("settings", "browser") args.project_dir = Dodo.get("/ROOT/project_dir") return args # Use safe=False if the script makes changes other than through Dodo.run if Dodo.is_main(__name__, safe=True): args = _args() if args.url: url = Dodo.get("/URLS/" + args.url) Dodo.run([args.browser, url]) else: Dodo.run([args.file_browser, args.project_dir])
from dodo_commands import Dodo from dodo_commands.framework.util import to_arg_list def _args(): Dodo.parser.add_argument("script") Dodo.parser.add_argument("script_args", nargs="?") args = Dodo.parse_args() args.python = Dodo.get_config("/PYTHON/python") args.cwd = Dodo.get_config("/PYTHON/cwd") return args if Dodo.is_main(__name__): args = _args() Dodo.run([args.python, args.script, *to_arg_list(args.script_args)], cwd=args.cwd)
"The options --key and --val should always be used together") args.editor = load_global_config_parser().get("settings", "config_editor") args.config_dir = Dodo.get("/ROOT/config_dir") return args if Dodo.is_main(__name__, safe=("--key" not in sys.argv)): try: args = _args() except configparser.NoOptionError as e: raise CommandError("{error}. Please check {filename}".format( error=str(e), filename=Paths().global_config_filename())) config = ConfigIO().load() if args.key and args.val: key = Key(config, args.key) key.set(args.val) ConfigIO().save(config) sys.exit(0) def add_global_config_filename(layer_paths): return R.concat(layer_paths, [Paths().global_config_filename()]) x = Dodo.get_container().layers.get_ordered_layer_paths() x = add_global_config_filename(x) yaml_filenames = x Dodo.run(args.editor.split() + yaml_filenames, cwd=".")
Dodo.parser.add_argument("url") Dodo.parser.add_argument("script_args") args = Dodo.parse_args() return args # Use safe=False if the script makes changes other than through Dodo.run if Dodo.is_main(__name__, safe=True): args = _args() tmp_dir = os.path.join(os.path.expanduser("~"), ".dodo_commands", "tmp") package_dir = os.path.join(tmp_dir, "exec_scripts") if ".commands.yaml" in args.url: last_slash = args.url.rfind("/") url = args.url[:last_slash] cmd_name = args.url[last_slash + 1:] else: url = args.url cmd_name = os.path.splitext(os.path.basename(url))[0] Dodo.run(["mkdir", "-p", package_dir]) Dodo.run(["touch", os.path.join(package_dir, "__init__.py")]) Dodo.run( ["wget", url, "-O", os.path.join(package_dir, os.path.basename(url))]) command_map = get_command_map(get_command_dirs([package_dir], [])) sys.argv = sys.argv[1:] execute_script(command_map, cmd_name)
def _args(): parser = ArgumentParser(description="Run typescript compiler") parser.add_argument("src_dir_name") parser.add_argument("--watch", action="store_true") # Parse the arguments. args = Dodo.parse_args(parser, config_args=[]) args.src_dir_map = Dodo.get("/TSC/src_dir_map") args.out_dir = Dodo.get("/TSC/out_dir", None) args.node_modules_dir = Dodo.get("/NODE/node_modules_dir") return args # Use safe=False if the script makes changes other than through Dodo.run if Dodo.is_main(__name__, safe=True): args = _args() src_dir = args.src_dir_map[args.src_dir_name] out_dir_args = ([ "--outDir", os.path.join(args.out_dir, os.path.basename(src_dir)) ] if args.out_dir else ["--outDir", "dist"]) watch_args = ["--watch"] if args.watch else [] Dodo.run( [ os.path.join(args.node_modules_dir, ".bin/tsc"), *watch_args, *out_dir_args ], cwd=src_dir, )
from dodo_commands import Dodo from dodo_commands.framework.util import to_arg_list def _args(): Dodo.parser.description = "Run something on the commandline" Dodo.parser.add_argument("--cwd") Dodo.parser.add_argument("script_args") args = Dodo.parse_args() return args # Use safe=False if the script makes changes other than through Dodo.run if Dodo.is_main(__name__, safe=True): args = _args() Dodo.run(to_arg_list(args.script_args), cwd=args.cwd or None)
if Dodo.is_main(__name__, safe=True): args = _args() project_name = Dodo.get_config('/ROOT/project_name') cmd = 'root@{host}:/srv/{projectName}/dumps'.format( host=args.host, projectName=project_name) dumps_dir = '/srv/%s/dumps' % project_name merge_into_config( Dodo.get_config('/DOCKER').setdefault('options', {}), {Dodo.command_name: add_ssh_agent_args({'is_interactive': False})}) dump_fns = Dodo.run([ 'ssh', 'root@%s' % args.host, 'ls', os.path.join(dumps_dir, '*.sql'), ], capture=True).split() dump_fns = sorted([x for x in dump_fns if x], key=_get_timestamp) class Picker(ChoicePicker): def print_choices(self, choices): for idx, dump_fn in enumerate(choices): print("%d: %s" % (idx + 1, dump_fn)) def question(self): return 'Select files to download (e.g. 1,3-4): ' picker = Picker()
from dodo_commands import Dodo from dodo_commands.framework.config import Paths def _args(): Dodo.parser.add_argument("--alt", help="Run an alternative git command") Dodo.parser.add_argument("--message", "-m", dest="message", help="The commit message") args = Dodo.parse_args() args.cwd = Paths().config_dir() return args if Dodo.is_main(__name__, safe=True): args = _args() if args.alt: Dodo.run(["git", *args.alt.split()], cwd=args.cwd) else: if not os.path.exists(os.path.join(args.cwd, ".git")): Dodo.run(["git", "init"], cwd=args.cwd) Dodo.run(["git", "add", "-A"], cwd=args.cwd) Dodo.run( ["git", "commit", "-m", args.message or "Update configuration"], cwd=args.cwd, )
import os from argparse import ArgumentParser from dodo_commands import Dodo def _args(): parser = ArgumentParser() args = Dodo.parse_args(parser) args.rtop = os.path.join(Dodo.get_config('/SERVER/node_modules_dir'), '.bin', 'rtop') args.cwd = Dodo.get_config('/WEBPACK/webpack_dir') return args if Dodo.is_main(__name__, safe=True): args = _args() Dodo.run([args.rtop], cwd=args.cwd)
from dodo_docker_commands.decorators.docker import invert_path def _args(): Dodo.parser.description = "Runs nps" Dodo.parser.add_argument("--cat", action="store_true") Dodo.parser.add_argument("--edit", action="store_true") Dodo.parser.add_argument("nps_args", nargs="?") args = Dodo.parse_args() args.cwd = Dodo.get_config("/NODE/cwd") args.nps = Dodo.get_config("/NODE/nps", "nps") global_config = load_global_config_parser() args.editor = global_config_get(global_config, "settings", "editor") return args # Use safe=False if the script makes changes other than through Dodo.run if Dodo.is_main(__name__, safe=True): args = _args() if args.cat: Dodo.run(["cat", "package-scripts.js"], cwd=args.cwd) elif args.edit: with DecoratorScope("docker", remove=True): Dodo.run([args.editor, "package-scripts.js"], cwd=invert_path(args.cwd)) else: Dodo.run([args.nps, *to_arg_list(args.nps_args)], cwd=args.cwd)
parser.add_argument( "--name", help=("Override the name of the started docker container")) parser.add_argument("--command") args = Dodo.parse_args(parser) return args if Dodo.is_main(__name__): args = _args() docker_options = DockerDecorator.merged_options(Dodo.get, args.service) if args.image: docker_options["image"] = Dodo.get( "/DOCKER_IMAGES/%s/image" % args.image, args.image) elif args.image_name: docker_options["image"] = args.image_name if args.name: docker_options["name"] = args.name else: docker_options["name"] = args.service Dodo.get()["DOCKER_OPTIONS"] = {Dodo.command_name: docker_options} with DecoratorScope("docker"): Dodo.run( [args.command] if args.command else ["sh"], cwd=docker_options.get("cwd", "/"), )
args = Dodo.parse_args(parser) return args def _containers(): result = [] for line in docker("ps", "--format", "{{.ID}} {{.Names}} {{.Image}}").split("\n"): if line: cid, name, image = line.split() result.append(dict(name=name, cid=cid, image=image)) return result if Dodo.is_main(__name__): args = _args() class Picker(ChoicePicker): def print_choices(self, choices): for idx, container in enumerate(choices): print("%d - %s" % (idx + 1, container["name"])) def question(self): return "Select a container: " picker = Picker(_containers()) picker.pick() for container in picker.get_choices(): Dodo.run(["docker", "kill", container["cid"]], )
def try_ssh(target_ip): Dodo.run([ 'ssh', 'root@%s' % target_ip, '-oStrictHostKeyChecking=no', '-oUserKnownHostsFile=/dev/null', 'echo' ])
import os from dodo_commands import Dodo def _args(): Dodo.parser.add_argument("number", nargs="?") Dodo.parser.add_argument("--list", action="store_true") Dodo.parser.add_argument("--group", "-g", default="default") args = Dodo.parse_args() args.dirs = Dodo.get("/DIAL", {}) return args if Dodo.is_main(__name__): args = _args() if args.list: Dodo.run(["dodo", "print-config", "DIAL"]) else: dirs = args.dirs.get(args.group, {}) if args.group else args.dirs for k, v in dirs.items(): if args.number == str(k): dial_dir = os.path.expanduser(os.path.expandvars(v)) print(dial_dir)
args = _args() for i in range(2): try: # Find agent container id try: container_id = (docker['ps', '-a'] | grep['ssh-agent'] | awk['{print $1}'])()[:-1] except: container_id = None # Stop command if args.command in ('stop', 'restart') and container_id: Dodo.run([ 'docker', 'run', '--rm', '--volumes-from=ssh-agent', '-it', args.ssh_agent_image_name, 'ssh-add', '-D' ], quiet=True) # noqa Dodo.run(['docker', 'rm', '-f', container_id]) if args.command in ('stop', ): sys.exit(0) elif args.command == 'status': print("running" if container_id else "stopped") elif args.command in ('start', 'restart'): # If container is already running, exit. if container_id: raise CommandError( "A container named 'ssh-agent' is already running.")
raise CommandError("Container not found: %s" % args.find) elif not args.name: containers = _containers() print("0 - exit") for idx, container in enumerate(containers): print("%d - %s" % (idx + 1, container)) print("\nSelect a container: ") choice = int(raw_input()) - 1 if choice == -1: sys.exit(0) args.name = containers[choice] if not args.cmd: default_shell = Dodo.get("/DOCKER/default_shell", "sh") docker_options = DockerDecorator.merged_options( Dodo.get, "docker-exec") args.cmd = docker_options.get("shell", default_shell) Dodo.run([ "docker", "exec", "-i", "-t", ] + (["--user", args.user] if args.user else []) + [ args.name, *args.cmd.split(), ], )