コード例 #1
0
 def take_action(self, parsed_args):
     environment = self.app.options.environment
     e = SecretsEnvironment(environment)
     if parsed_args.tmpdir:
         if not e.environment_exists() and not parsed_args.create:
             return (f"[-] environment '{str(e)}' does not exist; "
                     "use '--create' to create it")
         tmpdir = e.get_tmpdir_path(create_path=parsed_args.create)
         self._print(tmpdir, parsed_args.json)
     else:
         base_path = e.get_environment_path()
         subdir = parsed_args.subdir
         full_path = base_path if subdir is None \
             else os.path.join(base_path, *subdir)
         if not os.path.exists(full_path) and parsed_args.create:
             mode = 0o700
             os.makedirs(full_path, mode)
             if self.app_args.verbose_level > 1:
                 self.logger.info("[+] created %s", full_path)
         if parsed_args.exists:
             # Just check existance and return result
             exists = os.path.exists(full_path)
             if self.app_args.verbose_level > 1:
                 status = "exists" if exists else "does not exist"
                 self.logger.info("[+] environment path '%s' %s", full_path,
                                  status)
             return 0 if exists else 1
         else:
             self._print(full_path, parsed_args.json)
コード例 #2
0
    def take_action(self, parsed_args):
        e = SecretsEnvironment(environment=self.app.options.environment)
        tmpdir = e.get_tmpdir_path()
        backend_file = os.path.join(os.getcwd(), 'tfbackend.tf')
        tfstate_file = os.path.join(tmpdir, 'terraform.tfstate')
        backend_text = textwrap.dedent("""\
            terraform {{
              backend "local" {{
              path = "{tfstate_file}"
              }}
            }}
            """.format(tfstate_file=tfstate_file))

        if parsed_args.path:
            self.logger.debug('[+] showing terraform state file path')
            print(tfstate_file)
        else:
            self.logger.debug('[+] setting up terraform backend')
            if os.path.exists(backend_file):
                self.logger.debug("[+] updating '%s'", backend_file)
            else:
                self.logger.debug("[+] creating '%s'", backend_file)
            with open(backend_file, 'w') as f:
                f.write(backend_text)