예제 #1
0
파일: all.py 프로젝트: lost-theory/cuisine
	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)
예제 #2
0
파일: all.py 프로젝트: postmates/cuisine
 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)
예제 #3
0
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()
예제 #4
0
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)
예제 #5
0
파일: cli.py 프로젝트: pombredanne/basebox
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
예제 #6
0
 def __init__(self, context, vm=None, **ssh_config_overrides):
     overrides = context._connection_settings(vm=vm, **ssh_config_overrides)
     self.original_settings = dict(env)
     env.update(overrides)
     mode_remote()
예제 #7
0
파일: cli.py 프로젝트: gregbanks/basebox
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
예제 #8
0
 def __init__(self, context, vm=None, **ssh_config_overrides):
     overrides = context._connection_settings(vm=vm, **ssh_config_overrides)
     self.original_settings = dict(env)
     env.update(overrides)
     mode_remote()