{
  "$schema": "./.schemas/page.schema.json",
  "title": "Proxmox maintenance",
  "icon": "wrench.and.screwdriver",
  "sidebar": false,
  "sources": {
    "host-control": {
      "auth": {
        "name": "Authorization",
        "type": "header"
      },
      "derive": [
        {
          "name": "can_control",
          "value": {
            "else": {
              "template": "no"
            },
            "if": {
              "any": [
                {
                  "equals": 1,
                  "path": "permissions.data[\"/nodes/pve1\"][\"Sys.Modify\"]"
                },
                {
                  "equals": 1,
                  "path": "permissions.data[\"/nodes/pve1\"][\"Sys.PowerMgmt\"]"
                }
              ]
            },
            "then": {
              "template": "yes"
            }
          }
        }
      ],
      "every": "1m",
      "requests": {
        "permissions": "/api2/json/access/permissions?path=/nodes/pve1",
        "time": "/api2/json/nodes/pve1/time"
      },
      "secret": "proxmox",
      "url": "https://proxmox.example.com:8006"
    },
    "maintenance": {
      "auth": {
        "name": "Authorization",
        "type": "header"
      },
      "derive": [
        {
          "name": "installed",
          "value": {
            "each": "versions.data",
            "fields": {
              "candidate": {
                "first": "Version",
                "of": "rows"
              },
              "installed": {
                "first": "OldVersion",
                "of": "rows"
              },
              "package": "key"
            },
            "group": "Package",
            "where": {
              "equals": "Installed",
              "path": "CurrentState"
            }
          }
        },
        {
          "name": "changes",
          "value": {
            "each": "installed",
            "fields": {
              "candidate": "candidate",
              "installed": "installed",
              "package": "package"
            },
            "where": {
              "all": [
                {
                  "not": {
                    "path": "installed"
                  },
                  "path": "candidate"
                },
                {
                  "not": "",
                  "path": "installed"
                }
              ]
            }
          }
        },
        {
          "name": "updates",
          "value": {
            "count": "changes"
          }
        },
        {
          "name": "running_kernel",
          "value": {
            "first": "RunningKernel",
            "of": "versions.data",
            "where": {
              "equals": "proxmox-ve",
              "path": "Package"
            }
          }
        },
        {
          "name": "newest_kernel",
          "value": {
            "first": "Package",
            "of": "versions.data",
            "where": {
              "all": [
                {
                  "equals": "Installed",
                  "path": "CurrentState"
                },
                {
                  "contains": "-pve-signed",
                  "path": "Package"
                }
              ]
            }
          }
        }
      ],
      "every": "1m",
      "requests": {
        "time": "/api2/json/nodes/pve1/time",
        "versions": "/api2/json/nodes/pve1/apt/versions"
      },
      "secret": "proxmox",
      "url": "https://proxmox.example.com:8006"
    }
  },
  "cards": [
    {
      "actions": [
        {
          "body": {
            "notify": 0
          },
          "completion": {
            "done": {
              "equals": "stopped",
              "path": "data.status"
            },
            "interval": 2,
            "path": "/api2/json/nodes/pve1/tasks/{data}/status",
            "success": {
              "equals": "OK",
              "path": "data.exitstatus"
            },
            "timeout": 300
          },
          "icon": "arrow.triangle.2.circlepath",
          "label": "Refresh package index",
          "path": "/api2/json/nodes/pve1/apt/update",
          "when": {
            "equals": 1,
            "path": "permissions.data[\"/nodes/pve1\"][\"Sys.Modify\"]"
          }
        },
        "|",
        {
          "body": {
            "command": "reboot"
          },
          "confirm": {
            "message": "Restart pve1? All guests stop first.",
            "type": "pve1"
          },
          "icon": "arrow.clockwise",
          "label": "Restart host",
          "path": "/api2/json/nodes/pve1/status",
          "when": {
            "equals": 1,
            "path": "permissions.data[\"/nodes/pve1\"][\"Sys.PowerMgmt\"]"
          }
        },
        {
          "body": {
            "command": "shutdown"
          },
          "confirm": {
            "message": "Shut down pve1? Restart needs independent access.",
            "type": "pve1"
          },
          "icon": "power",
          "label": "Shut down host",
          "path": "/api2/json/nodes/pve1/status",
          "when": {
            "equals": 1,
            "path": "permissions.data[\"/nodes/pve1\"][\"Sys.PowerMgmt\"]"
          }
        }
      ],
      "actions_placement": "corner",
      "id": "controls",
      "label": "Controls",
      "source": {
        "ref": "host-control",
        "when": {
          "equals": "yes",
          "path": "can_control"
        }
      },
      "type": "text"
    },
    {
      "id": "maintenance",
      "items": [
        {
          "key": "Package changes",
          "url": "https://proxmox.example.com:8006/#v1:0:=node%2Fpve1:4:=apt::",
          "value": "—"
        },
        {
          "key": "Running kernel",
          "value": "—"
        },
        {
          "key": "Newest installed",
          "value": "—"
        },
        {
          "key": "Checked",
          "value": "—"
        }
      ],
      "label": "Software status",
      "source": {
        "map": {
          "items[0].value": {
            "else": {
              "template": "Unavailable"
            },
            "if": {
              "gt": 0,
              "value": {
                "count": "versions.data"
              }
            },
            "then": {
              "template": "{updates} Proxmox packages"
            }
          },
          "items[1].value": "running_kernel",
          "items[2].value": "newest_kernel",
          "items[3].value": {
            "template": "{time.data.time|ago}"
          }
        },
        "ref": "maintenance"
      },
      "type": "facts"
    },
    {
      "id": "packages",
      "items": [],
      "label": "Package changes",
      "max_items": 4,
      "source": {
        "map": {
          "items": {
            "each": "changes",
            "fields": {
              "detail": {
                "template": "{installed} → {candidate}"
              },
              "text": "package"
            }
          }
        },
        "ref": "maintenance"
      },
      "type": "list"
    }
  ]
}
