def testModeLocal( self ): # We switch to remote and switch back to local assert cuisine.mode(cuisine.MODE_LOCAL) cuisine.mode_remote() assert not cuisine.mode(cuisine.MODE_LOCAL) cuisine.mode_local() assert cuisine.mode(cuisine.MODE_LOCAL) # We use the mode changer to switch to remote temporarily with cuisine.mode_remote(): assert not cuisine.mode(cuisine.MODE_LOCAL) assert cuisine.mode(cuisine.MODE_LOCAL) # We go into local from local with cuisine.mode_local(): assert cuisine.mode(cuisine.MODE_LOCAL)
def testModeLocal(self): # We switch to remote and switch back to local assert cuisine.mode(cuisine.MODE_LOCAL) cuisine.mode_remote() assert not cuisine.mode(cuisine.MODE_LOCAL) cuisine.mode_local() assert cuisine.mode(cuisine.MODE_LOCAL) # We use the mode changer to switch to remote temporarily with cuisine.mode_remote(): assert not cuisine.mode(cuisine.MODE_LOCAL) assert cuisine.mode(cuisine.MODE_LOCAL) # We go into local from local with cuisine.mode_local(): assert cuisine.mode(cuisine.MODE_LOCAL)
def get_ssh_keys(): '''Get ssh certificates to connect to target servers''' puts(green('Getting ssh certificates')) local_original_mode = cuisine.is_local() cuisine.mode_local() if not cuisine.dir_exists(LOCAL_CERT_PATH): local('mkdir -p ' + LOCAL_CERT_BASE_PATH) local('cd %s && git clone %s && cd %s' % (LOCAL_CERT_BASE_PATH, CERT_REPO_URL, LOCAL_CERT_PATH)) else: local('cd %s && git pull' % (LOCAL_CERT_PATH)) if not local_original_mode: cuisine.mode_remote()
def main(): options = CollectlParseOptions() if options.check_database_connection: PostgresCollectlSqlDumper(options) if options.remote_host: env.user = options.remote_user env.host_string = options.remote_host env.key_filename = options.ssh_key env.disable_known_hosts = True mode_user() else: print "Mode local" mode_local() if options.scan: for host in options.hosts: print "Handling host %s" % host CollectlDirectoryScanner(options, host).execute()
def build(args): """ Build the specified module with specified arguments. @type module: module @type args: list of arguments """ # Build the command line. parser = _create_parser() #No args passed. #if not args: #todo: execute default task. # parser.print_help() # print("\n\n"+_CREDIT_LINE) # exit # Parse arguments. args = parser.parse_args(args) cuisine.mode_local() if args.version: print('brick_wall_build %s' % __version__) sys.exit(0) #load build file as a module if not path.isfile(args.file): print("Build file '%s' does not exist. Please specify a buld file\n" % args.file) parser.print_help() sys.exit(1) module = imp.load_source( path.splitext(path.basename(args.file))[0], args.file) cuisine.dir_ensure(State.tmp_path) cuisine.dir_ensure(State.artifact_path) # Run task and all its dependencies. if args.list_tasks: print_tasks(module, args.file) elif not args.tasks: if not _run_default_task(module): parser.print_help() print("\n") print_tasks(module, args.file) else: _run_from_task_names(module, args.tasks)
def deploy_bundle(local_path, deploy_path, file_name='bunle', branch='master', remote='bundle'): """ deploys Git bundle and setup repository usage from command line: fab deploy_bundle:<local-path>,<deploy-path> """ mode_local() local_bundle_dir = os.path.join(os.path.join(local_path, 'temp')) local_bundle_file = os.path.join(local_bundle_dir, file_name) bundle_file_names = git_bundle(local_path, local_bundle_file) if not bundle_file_names: return mode_remote() remote_bundle_dir = os.path.join(deploy_path, 'temp') dir_ensure(remote_bundle_dir) main_bundle_file = None for bundle_file_name in bundle_file_names: local_bundle_file = os.path.join(local_bundle_dir, bundle_file_name) remote_bundle_file = os.path.join(remote_bundle_dir, bundle_file_name) if not main_bundle_file: main_bundle_file = remote_bundle_file file_upload(remote_bundle_file, local_bundle_file) git_unbundle(main_bundle_file, deploy_path, branch, force=True, remote=remote)
def build(args): """ Build the specified module with specified arguments. @type module: module @type args: list of arguments """ # Build the command line. parser = _create_parser() #No args passed. #if not args: #todo: execute default task. # parser.print_help() # print("\n\n"+_CREDIT_LINE) # exit # Parse arguments. args = parser.parse_args(args) cuisine.mode_local() if args.version: print('brick_wall_build %s' % __version__) sys.exit(0) #load build file as a module if not path.isfile(args.file): print("Build file '%s' does not exist. Please specify a buld file\n" % args.file) parser.print_help() sys.exit(1) module = imp.load_source(path.splitext(path.basename(args.file))[0], args.file) cuisine.dir_ensure(State.tmp_path) cuisine.dir_ensure(State.artifact_path) # Run task and all its dependencies. if args.list_tasks: print_tasks(module, args.file) elif not args.tasks: if not _run_default_task(module): parser.print_help() print("\n") print_tasks(module, args.file) else: _run_from_task_names(module,args.tasks)
import pytest import re from brick_wall_build import _brick_wall_build, main, set_tmp_path, set_artifact_path import sys if sys.version.startswith("3."): from io import StringIO as SOut else: from StringIO import StringIO as SOut import os from os import path import imp import cuisine cuisine.mode_local() cuisine.run('rm -rf datasets/*') cuisine.dir_ensure('datasets') def fpath(mod): return path.splitext(mod.__file__)[0] + '.py' def simulate_dynamic_module_load(mod): file_path = fpath(mod) dynamically_loaded_mod = imp.load_source( path.splitext(path.basename(file_path))[0], file_path) return dynamically_loaded_mod def reset_build_file(mod):
file_sig = hashlib.sha256(f.read()).hexdigest() assert sig == file_sig class Packages(unittest.TestCase): def testInstall( self ): pass #with cuisine.mode_sudo(): # cuisine.package_ensure("tmux") class SSHKeys(unittest.TestCase): def testKeygen( self ): pass # if cuisine.ssh_keygen(USER): # print "SSH keys already there" # else: # print "SSH keys created" def testAuthorize( self ): key = "ssh-dss XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX= user@cuisine""" cuisine.ssh_authorize(USER, key) # FIXME: Should check that the key is present, and only one if __name__ == "__main__": # We bypass fabric as we want the tests to be run locally cuisine.mode_local() unittest.main() # EOF
def wrapper(*a, **kw): if not env.host_string: with mode_local(): return f(*a, **kw) else: return f(*a, **kw)
# Ensure that the box got installed self.assertIn(boxname, run('vagrant box list').splitlines()) # Bring up a box based on the newly installed box, and test it for # the things we just installed tempdir = tempfile.mkdtemp() ctx = VagrantContext(tempdir) ctx.rewrite_vagrantfile(''' Vagrant::Config.run do |config| config.vm.box = '%s' end ''' % boxname) with ctx.connect(), settings(warn_only=True): for pkg in test_packages: self.assertIn('installed', run('dpkg -s %s | grep Status' % pkg)) self.assertEqual(run('cat %s' % test_file), test_file_content) # TODO: cleanup - remove base box and derived box def testMeta(self): # TODO: test meta features like basebox.up(), basebox.halt(), etc. pass if __name__ == "__main__": env.host_string = '%s@localhost' % os.environ['USER'] with mode_local(): unittest.main()
def main(args=None): parser = argparse.ArgumentParser( add_help=False, description=""" Command line utility for packaging and installing vagrant boxes built by executing fabric tasks. %(prog)s is implemented as a thin wrapper around fabric's 'fab' command. It creates a temporary vagrant environment, configures fabric to connect to it, then hands off execution to fabric, which runs its tasks against the environment. When finished, it packages and/or installs the built box according to the arguments provided.""", ) meta = parser.add_argument_group(title="optional arguments") meta.add_argument( "-h", "--help", action="store_true", help="""Show this help message and exit. For help with fabric options, run 'fab --help'.""", ) meta.add_argument("-V", "--version", action="store_true", help="""show program's version number and exit""") meta.add_argument("--log-level", help="Level for logging program output", default="warning", type=log_level) # Primary arguments for basebox command. main = parser.add_argument_group( title="build arguments", description="""Arguments to configure building, installation, and packaging""", ) main.add_argument( "--base", help="""Vagrant base box to build with. Defaults to vagrant's precise64 box, also available at https://files.vagrantup.com/precise64.box""", default="https://files.vagrantup.com/precise64.box", ) main.add_argument( "--vagrantfile-template", # help='Jinja template for rendering Vagrantfile to build with', help=argparse.SUPPRESS, default=TEMPLATE_ENV.get_template("Vagrantfile.default"), type=TEMPLATE_ENV.get_template, ) main.add_argument("--package-as", metavar="PACKAGE_FILE", help="Package file to write the build result to.") main.add_argument( "--package-vagrantfile", help="""Specify how/whether to set the output package Vagrantfile. The default is 'inherit', which packages with the Vagrantfile of the base box if it exists. Specifying 'none' will create the package without a Vagrantfile. If a file path or raw string is provided, its contents will be used as the output package Vagrantfile. """, default="inherit", type=package_vagrantfile, metavar="(VFILE_PATH|VFILE_STRING|inherit|none)", ) main.add_argument("--install-as", help="Install the built box to vagrant as BOXNAME", metavar="BOXNAME") # Arguments shared with fabric shared = parser.add_argument_group( title="shared arguments", description="""These arguments are arguments to fabric, but %(prog)s also uses them to configure its environment. They are passed through to fabric when it executes.""", ) shared.add_argument( "-i", metavar="PATH", help="""Path to SSH private key file. May be repeated. Added to the generated Vagrantfile as the 'config.ssh.private_key_path' option. """, ), shared.add_argument( "-u", "--user", help="""Username to use when connecting to vagrant hosts. Added to the generated Vagrantfile as the 'config.ssh.username' option. """, ), shared.add_argument( "--port", help="""SSH connection port. Added to the generated Vagrantfile as the 'config.ssh.port' option""", ) # OK, this one doesn't do anything extra, but we still want to sanity-check # the fabfile before executing. shared.add_argument("-f", "--fabfile", help=argparse.SUPPRESS) # The 'hosts' parameter works a bit differently in basebox - each hostname # must be a valid identifier name and can be assigned roles by listing them # in the format 'host:role1,role2'. # # >>> basebox -H box1:web box2:db,cache main.add_argument("-H", "--hosts", help="", nargs="+", type=host_with_roles, action="append") args, fab_args = parser.parse_known_args(args=args) # Set log level so everything after this can emit proper logs LOG.level = args.log_level # Remove the separator from the fabric arguments if "--" in fab_args: fab_args.remove("--") # Flatten host-role entries and map bidirectionally host_roles = {} roledefs = {} for host, roles in [tpl for sublist in args.hosts for tpl in sublist]: if roles: LOG.info("Host <%s> specified with roles: %s" % (host, roles)) else: LOG.info("Host <%s> specified with no roles." % host) host_roles[host] = roles for role in roles: roledefs.setdefault(role, []).append(host) # Add hosts to fabric args fab_args[:0] = ["--hosts", ",".join(host_roles.keys())] # Duplicate shared args back into the fabric argument list for action in shared._group_actions: if getattr(args, action.dest, None): flag = ("-%s" if len(action.dest) == 1 else "--%s") % action.dest fab_args[:0] = [flag, getattr(args, action.dest)] LOG.info("Checking fabric parameters: %s" % fab_args) if args.help: parser.print_help() elif args.version: print_version() else: if not (args.install_as or args.package_as): print "No action specified (you should use --install-as or --package-as)." else: # Replace sys.argv to simulate calling fabric as a CLI script argv_original = sys.argv stderr_original = sys.stderr sys.argv = ["fab"] + fab_args sys.stderr = StringIO.StringIO() # Sanity-check fabric arguments before doing anything heavy try: fabric.main.parse_options() except SystemExit: print ("An error was encountered while trying to parse the " "arguments to fabric:") print "" print os.linesep.join("\t%s" % line for line in sys.stderr.getvalue().splitlines()[2:]) print "" print "Please check the syntax and try again." raise finally: sys.stderr = stderr_original # Check fabfile resolution fabfile_original = fabric.state.env.fabfile if args.fabfile: fabric.state.env.fabfile = args.fabfile if not fabric.main.find_fabfile(): print ( "Fabric couldn't find any fabfiles! (You may want to " "change directories or specify the -f option)" ) raise SystemExit fabric.state.env.fabfile = fabfile_original # Render input Vagrantfile with appropriate context vfile_ctx = { "base": args.base, "hosts": host_roles.keys(), "ssh": {"username": args.user, "private_key_path": args.i, "port": args.port}, } mode_local() with tempbox( base=args.base, vfile_template=args.vagrantfile_template, vfile_template_context=vfile_ctx ) as default_box: context = default_box.context # Fabricate an SSH config for the vagrant environment and add # it to fabric.state.env ssh_conf = ssh.SSHConfig() ssh_configs = ssh_conf._config # Add a host entry to SSH config for each vagrant box for box_name in context.list_boxes(): box = context[box_name] box.up() ssh_settings = box.ssh_config() ssh_settings.update({"host": box_name, "stricthostkeychecking": "no"}) ssh_configs.append(ssh_settings) fabric.api.settings.use_ssh_config = True fabric.api.env._ssh_config = ssh_conf # Configure roledefs fabric.state.env.roledefs = roledefs # Hand execution over to fabric with mode_remote(): try: fabric.main.main() except SystemExit as e: # Raised when fabric finishes. if e.code is not 0: LOG.error("Fabric exited with error code %s" % e.code) raise pass vfile = resolve_package_vagrantfile(args.package_vagrantfile) context.package(vagrantfile=vfile, install_as=args.install_as, output=args.package_as) sys.argv = argv_original
def test_method(): with cs.mode_local() : print(green("hello")+red("world")+blue("desu")+yellow("testest"))
def main(args=None): parser = argparse.ArgumentParser(add_help=False, description=''' Command line utility for packaging and installing vagrant boxes built by executing fabric tasks. %(prog)s is implemented as a thin wrapper around fabric's 'fab' command. It creates a temporary vagrant environment, configures fabric to connect to it, then hands off execution to fabric, which runs its tasks against the environment. When finished, it packages and/or installs the built box according to the arguments provided.''') meta = parser.add_argument_group(title='optional arguments') meta.add_argument( '-h', '--help', action='store_true', help='''Show this help message and exit. For help with fabric options, run 'fab --help'.''') meta.add_argument('-V', '--version', action='store_true', help='''show program's version number and exit''') meta.add_argument('--log-level', help='Level for logging program output', default='warning', type=log_level) # Primary arguments for basebox command. main = parser.add_argument_group( title='build arguments', description='''Arguments to configure building, installation, and packaging''') main.add_argument( '--base', help='''Vagrant base box to build with. Defaults to vagrant's precise64 box, also available at https://files.vagrantup.com/precise64.box''', default='https://files.vagrantup.com/precise64.box') main.add_argument( '--vagrantfile-template', # help='Jinja template for rendering Vagrantfile to build with', help=argparse.SUPPRESS, default=TEMPLATE_ENV.get_template('Vagrantfile.default'), type=TEMPLATE_ENV.get_template) main.add_argument('--package-as', metavar='PACKAGE_FILE', help='Package file to write the build result to.') main.add_argument( '--package-vagrantfile', help='''Specify how/whether to set the output package Vagrantfile. The default is 'inherit', which packages with the Vagrantfile of the base box if it exists. Specifying 'none' will create the package without a Vagrantfile. If a file path or raw string is provided, its contents will be used as the output package Vagrantfile. ''', default='inherit', type=package_vagrantfile, metavar='(VFILE_PATH|VFILE_STRING|inherit|none)') main.add_argument('--install-as', help='Install the built box to vagrant as BOXNAME', metavar='BOXNAME') # Arguments shared with fabric shared = parser.add_argument_group( title='shared arguments', description='''These arguments are arguments to fabric, but %(prog)s also uses them to configure its environment. They are passed through to fabric when it executes.''') shared.add_argument( '-i', metavar='PATH', help='''Path to SSH private key file. May be repeated. Added to the generated Vagrantfile as the 'config.ssh.private_key_path' option. '''), shared.add_argument( '-u', '--user', help='''Username to use when connecting to vagrant hosts. Added to the generated Vagrantfile as the 'config.ssh.username' option. '''), shared.add_argument( '--port', help='''SSH connection port. Added to the generated Vagrantfile as the 'config.ssh.port' option''') # OK, this one doesn't do anything extra, but we still want to sanity-check # the fabfile before executing. shared.add_argument('-f', '--fabfile', help=argparse.SUPPRESS) # The 'hosts' parameter works a bit differently in basebox - each hostname # must be a valid identifier name and can be assigned roles by listing them # in the format 'host:role1,role2'. # # >>> basebox -H box1:web box2:db,cache main.add_argument('-H', '--hosts', help='', nargs='+', type=host_with_roles, action='append') args, fab_args = parser.parse_known_args(args=args) # Set log level so everything after this can emit proper logs LOG.level = args.log_level # Remove the separator from the fabric arguments if '--' in fab_args: fab_args.remove('--') # Flatten host-role entries and map bidirectionally host_roles = {} roledefs = {} for host, roles in [tpl for sublist in args.hosts for tpl in sublist]: if roles: LOG.info('Host <%s> specified with roles: %s' % (host, roles)) else: LOG.info('Host <%s> specified with no roles.' % host) host_roles[host] = roles for role in roles: roledefs.setdefault(role, []).append(host) # Add hosts to fabric args fab_args[:0] = ['--hosts', ','.join(host_roles.keys())] # Duplicate shared args back into the fabric argument list for action in shared._group_actions: if getattr(args, action.dest, None): flag = ('-%s' if len(action.dest) == 1 else '--%s') % action.dest fab_args[:0] = [flag, getattr(args, action.dest)] LOG.info('Checking fabric parameters: %s' % fab_args) if args.help: parser.print_help() elif args.version: print_version() else: if not (args.install_as or args.package_as): print 'No action specified (you should use --install-as or --package-as).' else: # Replace sys.argv to simulate calling fabric as a CLI script argv_original = sys.argv stderr_original = sys.stderr sys.argv = ['fab'] + fab_args sys.stderr = StringIO.StringIO() # Sanity-check fabric arguments before doing anything heavy try: fabric.main.parse_options() except SystemExit: print( 'An error was encountered while trying to parse the ' 'arguments to fabric:') print '' print os.linesep.join( '\t%s' % line for line in sys.stderr.getvalue().splitlines()[2:]) print '' print 'Please check the syntax and try again.' raise finally: sys.stderr = stderr_original # Check fabfile resolution fabfile_original = fabric.state.env.fabfile if args.fabfile: fabric.state.env.fabfile = args.fabfile if not fabric.main.find_fabfile(): print( "Fabric couldn't find any fabfiles! (You may want to " "change directories or specify the -f option)") raise SystemExit fabric.state.env.fabfile = fabfile_original # Render input Vagrantfile with appropriate context vfile_ctx = { 'base': args.base, 'hosts': host_roles.keys(), 'ssh': { 'username': args.user, 'private_key_path': args.i, 'port': args.port } } mode_local() with tempbox(base=args.base, vfile_template=args.vagrantfile_template, vfile_template_context=vfile_ctx) as default_box: context = default_box.context # Fabricate an SSH config for the vagrant environment and add # it to fabric.state.env ssh_conf = ssh.SSHConfig() ssh_configs = ssh_conf._config # Add a host entry to SSH config for each vagrant box for box_name in context.list_boxes(): box = context[box_name] box.up() ssh_settings = box.ssh_config() ssh_settings.update({ 'host': box_name, 'stricthostkeychecking': 'no' }) ssh_configs.append(ssh_settings) fabric.api.settings.use_ssh_config = True fabric.api.env._ssh_config = ssh_conf # Configure roledefs fabric.state.env.roledefs = roledefs # Hand execution over to fabric with mode_remote(): try: fabric.main.main() except SystemExit as e: # Raised when fabric finishes. if e.code is not 0: LOG.error('Fabric exited with error code %s' % e.code) raise pass vfile = resolve_package_vagrantfile(args.package_vagrantfile) context.package(vagrantfile=vfile, install_as=args.install_as, output=args.package_as) sys.argv = argv_original