cf2tf icon indicating copy to clipboard operation
cf2tf copied to clipboard

InvalidGitRepositoryError

Open jannyg opened this issue 2 years ago • 4 comments

Tried the latest release with the fix for #19, but now I get this error message:

// Converting cf.cfn to Terraform!
 existing repo found.
Traceback (most recent call last):
  File "/usr/local/bin/cf2tf", line 8, in <module>
    sys.exit(cli())
  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/cf2tf/app.py", line 41, in cli
    search_manger = code.search_manager()
  File "/usr/local/lib/python3.9/site-packages/cf2tf/terraform/code.py", line 53, in search_manager
    repo = get_code()
  File "/usr/local/lib/python3.9/site-packages/cf2tf/terraform/code.py", line 72, in get_code
    repo = Repo(repo_path)
  File "/usr/local/lib/python3.9/site-packages/git/repo/base.py", line 224, in __init__
    self.working_dir: Optional[PathLike] = self._working_tree_dir or self.common_dir
  File "/usr/local/lib/python3.9/site-packages/git/repo/base.py", line 307, in common_dir
    raise InvalidGitRepositoryError()
git.exc.InvalidGitRepositoryError

The cloned repo to /tmp/terraform_src seems to not have a Git config and running git init in that folder resulted in the following error:

// Converting cf.cfn to Terraform!
 existing repo found.
Traceback (most recent call last):
  File "/usr/local/bin/cf2tf", line 8, in <module>
    sys.exit(cli())
  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/cf2tf/app.py", line 44, in cli
    config = TemplateConverter(cf_template, search_manger).convert()
  File "/usr/local/lib/python3.9/site-packages/cf2tf/convert.py", line 63, in convert
    tf_resources = self.convert_to_tf(self.manifest)
  File "/usr/local/lib/python3.9/site-packages/cf2tf/convert.py", line 115, in convert_to_tf
    tf_resources.extend(converter(resources))
  File "/usr/local/lib/python3.9/site-packages/cf2tf/convert.py", line 273, in convert_resources
    docs_path = self.search_manager.find(resource_type)
  File "/usr/local/lib/python3.9/site-packages/cf2tf/terraform/code.py", line 39, in find
    resource_name, ranking, doc_path = process.extractOne(
TypeError: cannot unpack non-iterable NoneType object

A removal of the /tmp/terraform_src resulting in a fresh clone fixed the issue. Maybe there could be a check for this folder and clone it again if it fails the checks?

jannyg avatar Jun 29 '22 07:06 jannyg

@jannyg Yeah I think there is a basic check to see if /tmp/terraform_src exists, but that's it. It shouldn't be hard to find a way in the git package to check if the folder is a valid git repository. Something like git status or git remote -v. I will look into this. Thanks!

shadycuz avatar Jun 29 '22 12:06 shadycuz

Seeing the same sort of error about InvalidGitRepositoryError (weirdly this was working on my laptop, so maybe the versions changed?)

Robs-Mac-Pro:CloudFormation robweaver$ cf2tf vpcpeer.yaml 
// Converting vpcpeer.yaml to Terraform!
 existing repo found.
Traceback (most recent call last):
  File "/usr/local/bin/cf2tf", line 8, in <module>
    sys.exit(cli())
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/cf2tf/app.py", line 41, in cli
    search_manger = code.search_manager()
  File "/usr/local/lib/python3.10/site-packages/cf2tf/terraform/code.py", line 54, in search_manager
    repo = get_code()
  File "/usr/local/lib/python3.10/site-packages/cf2tf/terraform/code.py", line 73, in get_code
    repo = Repo(repo_path)
  File "/usr/local/lib/python3.10/site-packages/git/repo/base.py", line 224, in __init__
    self.working_dir: Optional[PathLike] = self._working_tree_dir or self.common_dir
  File "/usr/local/lib/python3.10/site-packages/git/repo/base.py", line 307, in common_dir
    raise InvalidGitRepositoryError()
git.exc.InvalidGitRepositoryError
Robs-Mac-Pro:CloudFormation robweaver$ cf2tf glassfish.yaml 
// Converting glassfish.yaml to Terraform!
 existing repo found.
Traceback (most recent call last):
  File "/usr/local/bin/cf2tf", line 8, in <module>
    sys.exit(cli())
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/cf2tf/app.py", line 41, in cli
    search_manger = code.search_manager()
  File "/usr/local/lib/python3.10/site-packages/cf2tf/terraform/code.py", line 54, in search_manager
    repo = get_code()
  File "/usr/local/lib/python3.10/site-packages/cf2tf/terraform/code.py", line 73, in get_code
    repo = Repo(repo_path)
  File "/usr/local/lib/python3.10/site-packages/git/repo/base.py", line 224, in __init__
    self.working_dir: Optional[PathLike] = self._working_tree_dir or self.common_dir
  File "/usr/local/lib/python3.10/site-packages/git/repo/base.py", line 307, in common_dir
    raise InvalidGitRepositoryError()
git.exc.InvalidGitRepositoryError

robomnis avatar Aug 28 '22 14:08 robomnis

I had to do the same with removing the temp folder as mentioned. Took me a bit to figure out how to run tempfile.gettempdir() to figure out where it was, but works after deleting that folder.

python -c 'import tempfile; print(tempfile.gettempdir())'

Tangentially related, now that it works, the TF file that is created ends up including the message about the existing repo being found

 existing repo found.

data "aws_region" "current" {
}


data "aws_caller_identity" "current" {
}

robomnis avatar Aug 28 '22 14:08 robomnis

I think what we need to do here is two things.

  1. We can make sure that we are doing a better job of detecting if the repository exists and is functioning, instead of just detecting the temp folder and then assuming it's working.
  2. We could put the temp directory path in the log output, so if we do have trouble we know where we need to go to fix it.

Part of the reason I never worked on this is that it doesn't seem to happen all that often and I had wanted to replace the Terraform source code download with a new process that just looks up the correct file from their repo and get the raw version of it. This kind of on-the-fly retriever would be much better for most users who only want to convert a single small to medium size template. While the download source code method would be much better for those users who want to convert multiple templates or really large templates.

shadycuz avatar Apr 14 '23 13:04 shadycuz