From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <f.ebner@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id B5F94769BA
 for <pve-devel@lists.proxmox.com>; Fri, 16 Jul 2021 14:36:38 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id A524C10B16
 for <pve-devel@lists.proxmox.com>; Fri, 16 Jul 2021 14:36:08 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id 720A110B06
 for <pve-devel@lists.proxmox.com>; Fri, 16 Jul 2021 14:36:07 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 4B48642109
 for <pve-devel@lists.proxmox.com>; Fri, 16 Jul 2021 14:36:07 +0200 (CEST)
From: Fabian Ebner <f.ebner@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Fri, 16 Jul 2021 14:36:02 +0200
Message-Id: <20210716123602.82090-3-f.ebner@proxmox.com>
X-Mailer: git-send-email 2.30.2
In-Reply-To: <20210716123602.82090-1-f.ebner@proxmox.com>
References: <20210716123602.82090-1-f.ebner@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.493 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pve-devel] [PATCH proxmox-apt 2/2] standard repo detection: handle
 alternative URI for PVE repos
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Fri, 16 Jul 2021 12:36:38 -0000

For PVE, URIs without the final "/pve" are also valid.

Reported in the community forum:
https://forum.proxmox.com/threads/pve-7-0-9-no-proxmox-ve-repository-enabled.92427/

Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
---
 src/repositories/repository.rs             | 19 +++++-----
 src/repositories/standard.rs               | 42 ++++++++++++++++------
 tests/repositories.rs                      | 14 ++++++++
 tests/sources.list.d.expected/pve-alt.list |  8 +++++
 tests/sources.list.d/pve-alt.list          |  6 ++++
 5 files changed, 70 insertions(+), 19 deletions(-)
 create mode 100644 tests/sources.list.d.expected/pve-alt.list
 create mode 100644 tests/sources.list.d/pve-alt.list

diff --git a/src/repositories/repository.rs b/src/repositories/repository.rs
index 4e1ea6e..b56ec47 100644
--- a/src/repositories/repository.rs
+++ b/src/repositories/repository.rs
@@ -271,14 +271,17 @@ impl APTRepository {
 
     /// Checks if the repository is the one referenced by the handle.
     pub fn is_referenced_repository(&self, handle: APTRepositoryHandle, product: &str) -> bool {
-        let (package_type, uri, component) = handle.info(product);
-
-        self.types.contains(&package_type)
-            && self
-                .uris
-                .iter()
-                .any(|self_uri| self_uri.trim_end_matches('/') == uri)
-            && self.components.contains(&component)
+        let (package_type, handle_uris, component) = handle.info(product);
+
+        let mut found_uri = false;
+
+        for uri in self.uris.iter() {
+            let uri = uri.trim_end_matches('/');
+
+            found_uri = found_uri || handle_uris.iter().any(|handle_uri| handle_uri == uri);
+        }
+
+        self.types.contains(&package_type) && found_uri && self.components.contains(&component)
     }
 
     /// Check if a variant of the given suite is configured in this repository
diff --git a/src/repositories/standard.rs b/src/repositories/standard.rs
index 463e735..a287f91 100644
--- a/src/repositories/standard.rs
+++ b/src/repositories/standard.rs
@@ -163,42 +163,62 @@ impl APTRepositoryHandle {
         }
     }
 
-    /// Get package type, URI and the component associated with the handle.
-    pub fn info(self, product: &str) -> (APTRepositoryPackageType, String, String) {
+    /// Get package type, possible URIs and the component associated with the handle.
+    ///
+    /// The first URI is the preferred one.
+    pub fn info(self, product: &str) -> (APTRepositoryPackageType, Vec<String>, String) {
         match self {
             APTRepositoryHandle::Enterprise => (
                 APTRepositoryPackageType::Deb,
-                format!("https://enterprise.proxmox.com/debian/{}", product),
+                match product {
+                    "pve" => vec![
+                        "https://enterprise.proxmox.com/debian/pve".to_string(),
+                        "https://enterprise.proxmox.com/debian".to_string(),
+                    ],
+                    _ => vec![format!("https://enterprise.proxmox.com/debian/{}", product)],
+                },
                 format!("{}-enterprise", product),
             ),
             APTRepositoryHandle::NoSubscription => (
                 APTRepositoryPackageType::Deb,
-                format!("http://download.proxmox.com/debian/{}", product),
+                match product {
+                    "pve" => vec![
+                        "http://download.proxmox.com/debian/pve".to_string(),
+                        "http://download.proxmox.com/debian".to_string(),
+                    ],
+                    _ => vec![format!("http://download.proxmox.com/debian/{}", product)],
+                },
                 format!("{}-no-subscription", product),
             ),
             APTRepositoryHandle::Test => (
                 APTRepositoryPackageType::Deb,
-                format!("http://download.proxmox.com/debian/{}", product),
+                match product {
+                    "pve" => vec![
+                        "http://download.proxmox.com/debian/pve".to_string(),
+                        "http://download.proxmox.com/debian".to_string(),
+                    ],
+                    _ => vec![format!("http://download.proxmox.com/debian/{}", product)],
+                },
                 format!("{}test", product),
             ),
             APTRepositoryHandle::CephPacific => (
                 APTRepositoryPackageType::Deb,
-                "http://download.proxmox.com/debian/ceph-pacific".to_string(),
+                vec!["http://download.proxmox.com/debian/ceph-pacific".to_string()],
                 "main".to_string(),
             ),
             APTRepositoryHandle::CephPacificTest => (
                 APTRepositoryPackageType::Deb,
-                "http://download.proxmox.com/debian/ceph-pacific".to_string(),
+                vec!["http://download.proxmox.com/debian/ceph-pacific".to_string()],
                 "test".to_string(),
             ),
             APTRepositoryHandle::CephOctopus => (
                 APTRepositoryPackageType::Deb,
-                "http://download.proxmox.com/debian/ceph-octopus".to_string(),
+                vec!["http://download.proxmox.com/debian/ceph-octopus".to_string()],
                 "main".to_string(),
             ),
             APTRepositoryHandle::CephOctopusTest => (
                 APTRepositoryPackageType::Deb,
-                "http://download.proxmox.com/debian/ceph-octopus".to_string(),
+                vec!["http://download.proxmox.com/debian/ceph-octopus".to_string()],
                 "test".to_string(),
             ),
         }
@@ -209,11 +229,11 @@ impl APTRepositoryHandle {
     /// An URI in the result is not '/'-terminated (under the assumption that no valid
     /// product name is).
     pub fn to_repository(self, product: &str, suite: &str) -> APTRepository {
-        let (package_type, uri, component) = self.info(product);
+        let (package_type, uris, component) = self.info(product);
 
         APTRepository {
             types: vec![package_type],
-            uris: vec![uri],
+            uris: vec![uris.into_iter().next().unwrap()],
             suites: vec![suite.to_string()],
             components: vec![component],
             options: vec![],
diff --git a/tests/repositories.rs b/tests/repositories.rs
index 67b0255..6435671 100644
--- a/tests/repositories.rs
+++ b/tests/repositories.rs
@@ -358,5 +358,19 @@ fn test_standard_repositories() -> Result<(), Error> {
 
     assert_eq!(std_repos, expected);
 
+    let pve_alt_list = read_dir.join("pve-alt.list");
+    let mut file = APTRepositoryFile::new(&pve_alt_list)?.unwrap();
+    file.parse()?;
+
+    let file_vec = vec![file];
+
+    expected[0].status = Some(true);
+    expected[1].status = Some(true);
+    expected[2].status = Some(false);
+
+    let std_repos = standard_repositories("pve", &file_vec);
+
+    assert_eq!(std_repos, expected);
+
     Ok(())
 }
diff --git a/tests/sources.list.d.expected/pve-alt.list b/tests/sources.list.d.expected/pve-alt.list
new file mode 100644
index 0000000..f4834e7
--- /dev/null
+++ b/tests/sources.list.d.expected/pve-alt.list
@@ -0,0 +1,8 @@
+deb https://enterprise.proxmox.com/debian bullseye pve-enterprise
+
+deb http://download.proxmox.com/debian/ bullseye pve-no-subscription
+
+# deb http://download.proxmox.com/debian bullseye pvetest
+
+deb-src http://download.proxmox.com/debian bullseye pvetest
+
diff --git a/tests/sources.list.d/pve-alt.list b/tests/sources.list.d/pve-alt.list
new file mode 100644
index 0000000..0afd422
--- /dev/null
+++ b/tests/sources.list.d/pve-alt.list
@@ -0,0 +1,6 @@
+deb https://enterprise.proxmox.com/debian bullseye pve-enterprise
+
+deb http://download.proxmox.com/debian/ bullseye pve-no-subscription
+
+# deb http://download.proxmox.com/debian bullseye pvetest
+deb-src http://download.proxmox.com/debian bullseye pvetest
-- 
2.30.2