コード例 #1
0
def main():
    """Compile *.tf.py files and run Terraform"""
    to_process = [
        ent for ent in os.listdir(os.getcwd()) if ent.endswith(".tf.py")
    ]

    if len(to_process) == 0:
        print(
            "terraformpy - Error loading config: No .tf.py files found in %s" %
            os.getcwd())
        sys.exit(1)

    print("terraformpy - Processing: %s" % ", ".join(to_process))

    # all we need to do is import our files
    # the nature of resource declaration will register all of the objects for us to compile
    for filename in to_process:
        imp.load_source(filename[:-6], filename)

    # now 'compile' everything that was registered, and write it out the tf.json file
    print("terraformpy - Writing main.tf.json")
    with open("main.tf.json", "w") as fd:
        json.dump(compile(), fd, indent=4)
        fd.write("\n")

    if len(sys.argv) > 1:
        print("terraformpy - Running terraform: %s" % " ".join(sys.argv[1:]))
        # replace ourself with terraform
        os.execvp("terraform", ["terraform"] + sys.argv[1:])
コード例 #2
0
ファイル: config.py プロジェクト: act-labs/terraform-python
    def generate_config(self):
        self.insure_dir_exists()
        for conf in self.config:
            conf.config()
            with open("{}/{}.yaml".format(self.state_dir, conf.name),
                      'w') as file:
                yaml.dump(conf.dict(), file)

        print("terraformpy - Writing main.tf.json")

        with open("{}/main.tf.json".format(self.state_dir), "w") as fd:
            json.dump(compile(), fd, indent=4, sort_keys=True)
コード例 #3
0
def main():
    """Compile *.tf.py files and run Terraform"""
    to_process = [ent for ent in os.listdir(os.getcwd()) if ent.endswith(".tf.py")]

    if len(to_process) == 0:
        print(
            "terraformpy - Error loading config: No .tf.py files found in %s"
            % os.getcwd()
        )
        sys.exit(1)

    print("terraformpy - Processing: %s" % ", ".join(to_process))

    # all we need to do is import our files
    # the nature of resource declaration will register all of the objects for us to compile
    for filename in to_process:
        imp.load_source(filename[:-6], filename)

    # now 'compile' everything that was registered, and write it out the tf.json file
    print("terraformpy - Writing main.tf.json")
    with open("main.tf.json", "w") as fd:
        json.dump(compile(), fd, indent=4, sort_keys=True)

#     if "plan" in sys.argv[1:] or "apply" in sys.argv[1:]:
#         print("Trying to compile modules")
#         try:
#             cwd = os.getcwd()
#             modules_dir = f'{cwd}/.terraform/modules'
#             os.chdir(modules_dir)
#             modules = os.listdir()
#             for module in modules:
#                 os.chdir(f'{modules_dir}/{module}')
#                 to_process = [ent for ent in os.listdir(os.getcwd()) if ent.endswith(".tf.py")]
#                 for filename in to_process:
#                     imp.load_source(filename[:-6], filename)
#                 with open("main.tf.json", "w") as fd:
#                     json.dump(compile(), fd, indent=4, sort_keys=True)
#         except:
#             pass


    if len(sys.argv) > 1:
        print("terraformpy - Running terraform: %s" % " ".join(sys.argv[1:]))
        # replace ourself with terraform
        os.execvp("terraform", ["terraform"] + sys.argv[1:])