Пример #1
0
    def run(self, args, unknown_args):
        check_branch(args)
        environment = get_environment(args.env_name)
        commcare_branch = self._confirm_commcare_branch(environment,
                                                        args.commcare_branch,
                                                        quiet=args.quiet)
        if args.component == 'commcare':
            print(color_summary("You are about to deploy commcare"))
            print(color_summary("branch: {}".format(commcare_branch)))
            if ask('Deploy commcare?', quiet=args.quiet):
                print(
                    color_notice("Formplayer will not be deployed right now,"))
                print(
                    color_notice(
                        "but we recommend deploying formplayer about once a month as well."
                    ))
                print(
                    color_notice(
                        "It causes about 1 minute of service interruption to Web Apps and App Preview,"
                    ))
                print(color_notice("but keeps these services up to date."))
                print(
                    color_notice(
                        "You can do so by running `commcare-cloud <env> deploy formplayer`"
                    ))

                self.deploy_commcare(environment, commcare_branch, args,
                                     unknown_args)
        elif args.component == 'formplayer':
            self._announce_formplayer_deploy_start(environment)
            self.deploy_formplayer(environment, args, unknown_args)
Пример #2
0
    def run(self, args, unknown_args):
        check_branch(args)
        environment = get_environment(args.env_name)
        deploy_revs = self._confirm_deploy_revs(environment, args, quiet=args.quiet)

        deploy_component = args.component
        if deploy_component == None:
            deploy_component = 'both' if environment.meta_config.always_deploy_formplayer else 'commcare'

        if deploy_component in ['commcare', 'both']:
            print(color_summary("You are about to deploy the following code:"))
            with indent():
                for name, rev in deploy_revs:
                    print(color_summary("{}: {}".format(name, rev)))
            if ask('Continue with deploy?', quiet=args.quiet):
                if deploy_component != 'both':
                    _warn_no_formplayer()
                self.deploy_commcare(environment, deploy_revs, args, unknown_args)
        if deploy_component in ['formplayer', 'both']:
            if deploy_component != 'both':
                if args.commcare_rev:
                    print(color_warning('--commcare-rev does not apply to a formplayer deploy and will be ignored'))
                if args.fab_settings:
                    print(color_warning('--set does not apply to a formplayer deploy and will be ignored'))
            self._announce_formplayer_deploy_start(environment)
            self.deploy_formplayer(environment, args, unknown_args)
Пример #3
0
    def run(self, args, unknown_args):
        check_branch(args)
        environment = get_environment(args.env_name)

        deploy_component = args.component
        if deploy_component is None:
            deploy_component = 'both' if environment.meta_config.always_deploy_formplayer else 'commcare'

        if deploy_component in ['commcare', 'both']:
            if deploy_component != 'both':
                _warn_no_formplayer()
            return deploy_commcare(environment, args, unknown_args)
        if deploy_component in ['formplayer', 'both']:
            if deploy_component != 'both':
                if args.commcare_rev:
                    print(
                        color_warning(
                            '--commcare-rev does not apply to a formplayer deploy and will be ignored'
                        ))
                if args.fab_settings:
                    print(
                        color_warning(
                            '--set does not apply to a formplayer deploy and will be ignored'
                        ))
            return deploy_formplayer(environment, args)
Пример #4
0
    def run(self, args, unknown_args):
        check_branch(args)
        environment = get_environment(args.env_name)
        if args.resume:
            try:
                # use cached env to ensure consistency with last deploy
                cached_fab_env = retrieve_cached_deploy_env(environment.deploy_env)
            except Exception:
                print(color_error('Unable to resume deploy, please start anew'))
            else:
                environment = cached_fab_env.ccc_environment

        deploy_component = args.component
        if not deploy_component:
            deploy_component = ['commcare']
            if environment.meta_config.always_deploy_formplayer:
                deploy_component.append('formplayer')

        rc = 0
        if 'commcare' in deploy_component:
            if 'formplayer' not in deploy_component:
                _warn_no_formplayer()
            rc = deploy_commcare(environment, args, unknown_args)
        if 'formplayer' in deploy_component:
            if 'commcare' not in deploy_component:
                if args.commcare_rev:
                    print(color_warning('--commcare-rev does not apply to a formplayer deploy and will be ignored'))
                if args.fab_settings:
                    print(color_warning('--set does not apply to a formplayer deploy and will be ignored'))
            if rc:
                print(color_error("Skipping formplayer because commcare failed"))
            else:
                rc = deploy_formplayer(environment, args)
        return rc
Пример #5
0
 def run(self, args, unknown_args, always_skip_check=False):
     environment = get_environment(args.env_name)
     environment.create_generated_yml()
     ansible_context = AnsibleContext(args)
     check_branch(args)
     run_ansible_playbook(environment, args.playbook, ansible_context,
                          args.skip_check, args.quiet, always_skip_check,
                          args.limit, args.use_factory_auth, unknown_args)
Пример #6
0
 def run(self, args, unknown_args, always_skip_check=False, respect_ansible_skip=True):
     environment = get_environment(args.env_name)
     environment.create_generated_yml()
     ansible_context = AnsibleContext(args)
     check_branch(args)
     return run_ansible_playbook(
         environment, args.playbook, ansible_context, args.skip_check, args.quiet,
         always_skip_check, args.limit, args.use_factory_auth, unknown_args,
         respect_ansible_skip=respect_ansible_skip,
     )
Пример #7
0
 def run(self, args, unknown_args):
     check_branch(args)
     fab_args = []
     if args.fab_command:
         fab_args.append(args.fab_command)
     fab_args.extend(unknown_args)
     if args.l:
         fab_args.append('-l')
     else:
         env = get_environment(args.env_name)
         fab_args.extend(['--disable-known-hosts',
                          '--system-known-hosts', env.paths.known_hosts])
     # Create known_hosts file if it doesn't exist
     known_hosts_file = env.paths.known_hosts
     if not os.path.isfile(known_hosts_file):
         open(known_hosts_file, 'a').close()
     return exec_fab_command(args.env_name, *fab_args)
Пример #8
0
 def run(self, args, unknown_args):
     check_branch(args)
     fab_args = []
     if args.fab_command:
         fab_args.append(args.fab_command)
     fab_args.extend(unknown_args)
     if args.l:
         fab_args.append('-l')
     else:
         env = get_environment(args.env_name)
         fab_args.extend([
             '--disable-known-hosts', '--system-known-hosts',
             env.paths.known_hosts
         ])
     # Create known_hosts file if it doesn't exist
     known_hosts_file = env.paths.known_hosts
     if not os.path.isfile(known_hosts_file):
         open(known_hosts_file, 'a').close()
     return exec_fab_command(args.env_name, *fab_args)
Пример #9
0
    def run(self, args, unknown_args):
        check_branch(args)
        environment = get_environment(args.env_name)
        commcare_rev = self._confirm_commcare_rev(environment, args.commcare_rev, quiet=args.quiet)

        deploy_component = args.component
        if deploy_component == None:
            deploy_component = 'both' if environment.meta_config.always_deploy_formplayer else 'commcare'

        if deploy_component in ['commcare', 'both']:
            print(color_summary("You are about to deploy commcare from {}".format(commcare_rev)))
            if ask('Deploy commcare?', quiet=args.quiet):
                if deploy_component != 'both':
                    _warn_no_formplayer()
                self.deploy_commcare(environment, commcare_rev, args, unknown_args)
        if deploy_component in ['formplayer', 'both']:
            if deploy_component != 'both':
                if args.commcare_rev:
                    print(color_warning('--commcare-rev does not apply to a formplayer deploy and will be ignored'))
                if args.fab_settings:
                    print(color_warning('--set does not apply to a formplayer deploy and will be ignored'))
            self._announce_formplayer_deploy_start(environment)
            self.deploy_formplayer(environment, args, unknown_args)
Пример #10
0
    def run(self, args, unknown_args, always_skip_check=False):
        environment = get_environment(args.env_name)
        environment.create_generated_yml()
        ansible_context = AnsibleContext(args)
        check_branch(args)
        public_vars = environment.public_vars
        ask_vault_pass = public_vars.get('commcare_cloud_use_vault', True)

        def ansible_playbook(environment, playbook, *cmd_args):
            cmd_parts = (
                'ansible-playbook',
                os.path.join(ANSIBLE_DIR,
                             '{playbook}'.format(playbook=playbook)),
                '-i',
                environment.paths.inventory_ini,
                '-e',
                '@{}'.format(environment.paths.vault_yml),
                '-e',
                '@{}'.format(environment.paths.public_yml),
                '-e',
                '@{}'.format(environment.paths.generated_yml),
                '--diff',
            ) + cmd_args

            if not has_arg(unknown_args, '-u', '--user'):
                cmd_parts += ('-u', 'ansible')

            if not has_arg(unknown_args, '-f', '--forks'):
                cmd_parts += ('--forks', '15')

            known_hosts_filepath = environment.paths.known_hosts
            if os.path.exists(known_hosts_filepath):
                cmd_parts += ("--ssh-common-args='-o=UserKnownHostsFile=%s'" %
                              (known_hosts_filepath, ), )

            if has_arg(unknown_args, '-D', '--diff') or has_arg(
                    unknown_args, '-C', '--check'):
                puts(
                    colored.red(
                        "Options --diff and --check not allowed. Please remove -D, --diff, -C, --check."
                    ))
                puts(
                    "These ansible-playbook options are managed automatically by commcare-cloud and cannot be set manually."
                )
                return 2  # exit code

            if ask_vault_pass:
                cmd_parts += ('--vault-password-file=/bin/cat', )

            cmd_parts += get_common_ssh_args(public_vars)
            cmd = ' '.join(shlex_quote(arg) for arg in cmd_parts)
            print_command(cmd)
            if ask_vault_pass:
                environment.get_ansible_vault_password()
            p = subprocess.Popen(cmd,
                                 stdin=subprocess.PIPE,
                                 shell=True,
                                 env=ansible_context.env_vars)
            if ask_vault_pass:
                p.communicate(input='{}\n'.format(
                    environment.get_ansible_vault_password()))
            else:
                p.communicate()
            return p.returncode

        def run_check():
            return ansible_playbook(environment, args.playbook, '--check',
                                    *unknown_args)

        def run_apply():
            return ansible_playbook(environment, args.playbook, *unknown_args)

        exit_code = 0

        if always_skip_check:
            user_wants_to_apply = ask(
                'This command will apply without running the check first. Continue?',
                quiet=args.quiet)
        elif args.skip_check:
            user_wants_to_apply = ask(
                'Do you want to apply without running the check first?',
                quiet=args.quiet)
        else:
            exit_code = run_check()
            if exit_code == 1:
                # this means there was an error before ansible was able to start running
                return exit_code
            elif exit_code == 0:
                puts(
                    colored.green(
                        u"✓ Check completed with status code {}".format(
                            exit_code)))
                user_wants_to_apply = ask(
                    'Do you want to apply these changes?', quiet=args.quiet)
            else:
                puts(
                    colored.red(u"✗ Check failed with status code {}".format(
                        exit_code)))
                user_wants_to_apply = ask(
                    'Do you want to try to apply these changes anyway?',
                    quiet=args.quiet)

        if user_wants_to_apply:
            exit_code = run_apply()
            if exit_code == 0:
                puts(
                    colored.green(
                        u"✓ Apply completed with status code {}".format(
                            exit_code)))
            else:
                puts(
                    colored.red(u"✗ Apply failed with status code {}".format(
                        exit_code)))

        return exit_code