{
    "description": "Images used in CI to build packetfence packages",
    "min_packer_version": "1.5.0",
    "variables": {
        "builder_prefix": "pfbuild",
        "buildpkg_user": "registry.gitlab.com/orange-opensource/gitlab-buildpkg",
        "pf_root": "../..",
        "prov_dir": "provisionners_pfbuild",
        "files_dir": "files",
        "tmp_dir": "/tmp",
        "yum_cache_dir": "/var/cache/yum",
        "dnf_cache_dir": "/var/cache/dnf",
        "ansible_roles_path": "{{user `prov_dir`}}/roles",
        "ansible_collections_path": "{{user `prov_dir`}}",
        "ansible_extra_vars": "{{env `ANSIBLE_EXTRA_VARS`}}",
        "ansible_user": "root",
        "ansible_centos_group": "{{env `ANSIBLE_CENTOS_GROUP`}}",
        "ansible_centos8_group": "{{env `ANSIBLE_CENTOS8_GROUP`}}",
        "ansible_debian_group": "{{env `ANSIBLE_DEBIAN_GROUP`}}",
        "ansible_rubygems_group": "{{env `ANSIBLE_RUBYGEMS_GROUP`}}",
        "go_version": "{{env `GOVERSION`}}",
        "pf_minor_release": "{{ env `PF_MINOR_RELEASE` }}",
        "docker_tags": "{{env `DOCKER_TAGS`}}",
        "docker_user": "{{env `REGISTRY_USER`}}",
        "docker_password": "{{env `REGISTRY_PASSWORD`}}",
        "docker_registry": "{{env `REGISTRY`}}",
        "docker_registry_url": "{{env `REGISTRY_URL`}}"
    },
    "builders": [
        {
            "name": "{{user `builder_prefix`}}-centos-8",
            "type": "docker",
            "image": "{{user `buildpkg_user`}}/centos:8",
            "run_command": ["-d", "-i", "-t", "--", "{{.Image}}"],
            "commit": true,
            "changes": [
                "ENV PATH /usr/local/go/bin:$PATH"
            ]
        },
        {
            "name": "{{user `builder_prefix`}}-bullseye",
            "type": "docker",
            "image": "{{user `buildpkg_user`}}/debian:bullseye",
            "run_command": ["-d", "-i", "-t", "--", "{{.Image}}"],
            "commit": true,
            "changes": [
                "ENV PATH /usr/local/go/bin:$PATH"
            ]
        }
    ],
    "provisioners": [
        {
            "type": "shell",
            "script": "{{user `prov_dir`}}/shell/use-deprecated-repos.sh",
            "only": ["{{user `builder_prefix`}}-centos-8"]
        },
        {
            "type": "ansible",
            "playbook_file": "{{user `prov_dir`}}/site.yml",
            "galaxy_file": "{{user `prov_dir`}}/requirements.yml",
            "galaxy_force_install": true,
            "roles_path": "{{user `ansible_roles_path`}}",
            "collections_path": "{{user `ansible_collections_path`}}",
            "inventory_directory": "{{user `prov_dir`}}/inventory",
            "extra_arguments": ["--extra-vars", "{{user `ansible_extra_vars`}}"],
            "ansible_env_vars": ["PF_MINOR_RELEASE={{ user `pf_minor_release`}}", "ANSIBLE_ROLES_PATH={{user `prov_dir`}}/roles", "ANSIBLE_COLLECTIONS_PATH={{user `prov_dir`}}/"],
            "groups": ["{{user `ansible_centos_group`}}","{{user `ansible_centos8_group`}}"],
            "user": "{{user `ansible_user`}}",
            "only": ["{{user `builder_prefix`}}-centos-8"]
        },
        {
            "type": "ansible",
            "playbook_file": "{{user `prov_dir`}}/site.yml",
            "galaxy_file": "{{user `prov_dir`}}/requirements.yml",
            "galaxy_force_install": true,
            "roles_path": "{{user `ansible_roles_path`}}",
            "collections_path": "{{user `ansible_collections_path`}}",
            "inventory_directory": "{{user `prov_dir`}}/inventory",
            "extra_arguments": ["--extra-vars", "{{user `ansible_extra_vars`}}"],
            "ansible_env_vars": ["PF_MINOR_RELEASE={{ user `pf_minor_release`}}", "ANSIBLE_ROLES_PATH={{user `prov_dir`}}/roles", "ANSIBLE_COLLECTIONS_PATH={{user `prov_dir`}}/"],
            "groups": ["{{user `ansible_debian_group`}}"],
            "user": "{{user `ansible_user`}}",
            "only": ["{{user `builder_prefix`}}-bullseye"]
        },
        {
            "type": "shell",
            "script": "{{user `prov_dir`}}/shell/install-nodejs-el8.sh",
            "only": ["{{user `builder_prefix`}}-centos-8"]
        },
        {
            "type": "file",
            "source": "{{user `pf_root`}}/rpm",
            "destination": "{{user `tmp_dir`}}",
            "only": ["{{user `builder_prefix`}}-centos-8"
                    ]
        },
        {
            "type": "file",
            "source": "{{user `pf_root`}}/debian",
            "destination": "{{user `tmp_dir`}}",
            "only": ["{{user `builder_prefix`}}-bullseye"]
        },
        {
            "type": "shell",
            "inline": ["ci-build-pkg {{user `tmp_dir`}}"],
            "environment_vars": ["GITLAB_CI=true",
                                 "PKG_BUILD_SUFFIX_MODE=none",
                                 "CI_BUILDPKG_DEP_ONLY=yes",
                                 "CI_COMMIT_REF_NAME=fakebranch"
                                ]
        },
        {
            "type": "shell",
            "script": "{{user `pf_root`}}/addons/dev-helpers/setup-go-env.sh",
            "environment_vars": ["GOVERSION={{user `go_version`}}"]
        },
        {
            "type": "ansible",
            "playbook_file": "{{user `prov_dir`}}/site.yml",
            "inventory_directory": "{{user `prov_dir`}}/inventory",
            "extra_arguments": ["--extra-vars", "{{user `ansible_extra_vars`}}"],
            "user": "{{user `ansible_user`}}",
            "groups": ["{{user `ansible_rubygems_group`}}"]
        },
        {
            "type": "shell",
            "inline": ["rm -rf {{user `tmp_dir`}}/rpm",
                       "dnf clean all",
                       "rm -rf {{user `dnf_cache_dir`}}/*"
                      ],
            "only": ["{{user `builder_prefix`}}-centos-8"]
        },
        {
            "type": "shell",
            "inline": ["rm -rf {{user `tmp_dir`}}/debian",
                       "apt-get clean"
                      ],
            "only": ["{{user `builder_prefix`}}-bullseye"]
        }

    ],
    "post-processors": [
        [
            {
                "type": "docker-tag",
                "name": "set-tag",
                "only": ["{{user `builder_prefix`}}-centos-8"],
                "repository": "{{user `docker_registry_url`}}/{{user `builder_prefix`}}-centos-8",
                "tags": "{{user `docker_tags`}}"
            },
            {
                "type": "docker-push",
                "name": "push-tag",
                "only": ["{{user `builder_prefix`}}-centos-8"],
                "login": true,
                "login_username": "{{user `docker_user`}}",
                "login_password": "{{user `docker_password`}}",
                "login_server": "{{user `docker_registry`}}"
            }
        ],
        [
            {
                "type": "docker-tag",
                "name": "set-tag",
                "only": ["{{user `builder_prefix`}}-bullseye"],
                "repository": "{{user `docker_registry_url`}}/{{user `builder_prefix`}}-debian-bullseye",
                "tags": "{{user `docker_tags`}}"
            },
            {
                "type": "docker-push",
                "name": "push-tag",
                "only": ["{{user `builder_prefix`}}-bullseye"],
                "login": true,
                "login_username": "{{user `docker_user`}}",
                "login_password": "{{user `docker_password`}}",
                "login_server": "{{user `docker_registry`}}"
            }
        ]
    ]
}
