]> git.proxmox.com Git - proxmox-apt.git/blob - src/repositories/standard.rs
clippy fixes
[proxmox-apt.git] / src / repositories / standard.rs
1 use std::fmt::Display;
2
3 use anyhow::{bail, Error};
4 use serde::{Deserialize, Serialize};
5
6 use crate::repositories::repository::{
7 APTRepository, APTRepositoryFileType, APTRepositoryPackageType,
8 };
9
10 use proxmox_schema::api;
11
12 #[api(
13 properties: {
14 handle: {
15 description: "Handle referencing a standard repository.",
16 type: String,
17 },
18 },
19 )]
20 #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
21 #[serde(rename_all = "kebab-case")]
22 /// Reference to a standard repository and configuration status.
23 pub struct APTStandardRepository {
24 /// Handle referencing a standard repository.
25 pub handle: APTRepositoryHandle,
26
27 /// Configuration status of the associated repository.
28 #[serde(skip_serializing_if = "Option::is_none")]
29 pub status: Option<bool>,
30
31 /// Display name of the repository.
32 pub name: String,
33
34 /// Description of the repository.
35 pub description: String,
36 }
37
38 #[api]
39 #[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq, Eq)]
40 #[serde(rename_all = "kebab-case")]
41 /// Handles for Proxmox repositories.
42 pub enum APTRepositoryHandle {
43 /// The enterprise repository for production use.
44 Enterprise,
45 /// The repository that can be used without subscription.
46 NoSubscription,
47 /// The test repository.
48 Test,
49 /// Ceph Quincy repository.
50 CephQuincy,
51 /// Ceph Quincy test repository.
52 CephQuincyTest,
53 /// Ceph Pacific repository.
54 CephPacific,
55 /// Ceph Pacific test repository.
56 CephPacificTest,
57 /// Ceph Octoput repository.
58 CephOctopus,
59 /// Ceph Octoput test repository.
60 CephOctopusTest,
61 }
62
63 impl From<APTRepositoryHandle> for APTStandardRepository {
64 fn from(handle: APTRepositoryHandle) -> Self {
65 APTStandardRepository {
66 handle,
67 status: None,
68 name: handle.name(),
69 description: handle.description(),
70 }
71 }
72 }
73
74 impl TryFrom<&str> for APTRepositoryHandle {
75 type Error = Error;
76
77 fn try_from(string: &str) -> Result<Self, Error> {
78 match string {
79 "enterprise" => Ok(APTRepositoryHandle::Enterprise),
80 "no-subscription" => Ok(APTRepositoryHandle::NoSubscription),
81 "test" => Ok(APTRepositoryHandle::Test),
82 "ceph-quincy" => Ok(APTRepositoryHandle::CephQuincy),
83 "ceph-quincy-test" => Ok(APTRepositoryHandle::CephQuincyTest),
84 "ceph-pacific" => Ok(APTRepositoryHandle::CephPacific),
85 "ceph-pacific-test" => Ok(APTRepositoryHandle::CephPacificTest),
86 "ceph-octopus" => Ok(APTRepositoryHandle::CephOctopus),
87 "ceph-octopus-test" => Ok(APTRepositoryHandle::CephOctopusTest),
88 _ => bail!("unknown repository handle '{}'", string),
89 }
90 }
91 }
92
93 impl Display for APTRepositoryHandle {
94 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
95 match self {
96 APTRepositoryHandle::Enterprise => write!(f, "enterprise"),
97 APTRepositoryHandle::NoSubscription => write!(f, "no-subscription"),
98 APTRepositoryHandle::Test => write!(f, "test"),
99 APTRepositoryHandle::CephQuincy => write!(f, "ceph-quincy"),
100 APTRepositoryHandle::CephQuincyTest => write!(f, "ceph-quincy-test"),
101 APTRepositoryHandle::CephPacific => write!(f, "ceph-pacific"),
102 APTRepositoryHandle::CephPacificTest => write!(f, "ceph-pacific-test"),
103 APTRepositoryHandle::CephOctopus => write!(f, "ceph-octopus"),
104 APTRepositoryHandle::CephOctopusTest => write!(f, "ceph-octopus-test"),
105 }
106 }
107 }
108
109 impl APTRepositoryHandle {
110 /// Get the description for the repository.
111 pub fn description(self) -> String {
112 match self {
113 APTRepositoryHandle::Enterprise => {
114 "This is the default, stable, and recommended repository, available for all \
115 Proxmox subscription users."
116 }
117 APTRepositoryHandle::NoSubscription => {
118 "This is the recommended repository for testing and non-production use. \
119 Its packages are not as heavily tested and validated as the production ready \
120 enterprise repository. You don't need a subscription key to access this repository."
121 }
122 APTRepositoryHandle::Test => {
123 "This repository contains the latest packages and is primarily used for test labs \
124 and by developers to test new features."
125 }
126 APTRepositoryHandle::CephQuincy => {
127 "This repository holds the main Proxmox Ceph Quincy packages."
128 }
129 APTRepositoryHandle::CephQuincyTest => {
130 "This repository contains the Ceph Quincy packages before they are moved to the \
131 main repository."
132 }
133 APTRepositoryHandle::CephPacific => {
134 "This repository holds the main Proxmox Ceph Pacific packages."
135 }
136 APTRepositoryHandle::CephPacificTest => {
137 "This repository contains the Ceph Pacific packages before they are moved to the \
138 main repository."
139 }
140 APTRepositoryHandle::CephOctopus => {
141 "This repository holds the main Proxmox Ceph Octopus packages."
142 }
143 APTRepositoryHandle::CephOctopusTest => {
144 "This repository contains the Ceph Octopus packages before they are moved to the \
145 main repository."
146 }
147 }
148 .to_string()
149 }
150
151 /// Get the display name of the repository.
152 pub fn name(self) -> String {
153 match self {
154 APTRepositoryHandle::Enterprise => "Enterprise",
155 APTRepositoryHandle::NoSubscription => "No-Subscription",
156 APTRepositoryHandle::Test => "Test",
157 APTRepositoryHandle::CephQuincy => "Ceph Quincy",
158 APTRepositoryHandle::CephQuincyTest => "Ceph Quincy Test",
159 APTRepositoryHandle::CephPacific => "Ceph Pacific",
160 APTRepositoryHandle::CephPacificTest => "Ceph Pacific Test",
161 APTRepositoryHandle::CephOctopus => "Ceph Octopus",
162 APTRepositoryHandle::CephOctopusTest => "Ceph Octopus Test",
163 }
164 .to_string()
165 }
166
167 /// Get the standard file path for the repository referenced by the handle.
168 pub fn path(self, product: &str) -> String {
169 match self {
170 APTRepositoryHandle::Enterprise => {
171 format!("/etc/apt/sources.list.d/{}-enterprise.list", product)
172 }
173 APTRepositoryHandle::NoSubscription => "/etc/apt/sources.list".to_string(),
174 APTRepositoryHandle::Test => "/etc/apt/sources.list".to_string(),
175 APTRepositoryHandle::CephQuincy => "/etc/apt/sources.list.d/ceph.list".to_string(),
176 APTRepositoryHandle::CephQuincyTest => "/etc/apt/sources.list.d/ceph.list".to_string(),
177 APTRepositoryHandle::CephPacific => "/etc/apt/sources.list.d/ceph.list".to_string(),
178 APTRepositoryHandle::CephPacificTest => "/etc/apt/sources.list.d/ceph.list".to_string(),
179 APTRepositoryHandle::CephOctopus => "/etc/apt/sources.list.d/ceph.list".to_string(),
180 APTRepositoryHandle::CephOctopusTest => "/etc/apt/sources.list.d/ceph.list".to_string(),
181 }
182 }
183
184 /// Get package type, possible URIs and the component associated with the handle.
185 ///
186 /// The first URI is the preferred one.
187 pub fn info(self, product: &str) -> (APTRepositoryPackageType, Vec<String>, String) {
188 match self {
189 APTRepositoryHandle::Enterprise => (
190 APTRepositoryPackageType::Deb,
191 match product {
192 "pve" => vec![
193 "https://enterprise.proxmox.com/debian/pve".to_string(),
194 "https://enterprise.proxmox.com/debian".to_string(),
195 ],
196 _ => vec![format!("https://enterprise.proxmox.com/debian/{}", product)],
197 },
198 format!("{}-enterprise", product),
199 ),
200 APTRepositoryHandle::NoSubscription => (
201 APTRepositoryPackageType::Deb,
202 match product {
203 "pve" => vec![
204 "http://download.proxmox.com/debian/pve".to_string(),
205 "http://download.proxmox.com/debian".to_string(),
206 ],
207 _ => vec![format!("http://download.proxmox.com/debian/{}", product)],
208 },
209 format!("{}-no-subscription", product),
210 ),
211 APTRepositoryHandle::Test => (
212 APTRepositoryPackageType::Deb,
213 match product {
214 "pve" => vec![
215 "http://download.proxmox.com/debian/pve".to_string(),
216 "http://download.proxmox.com/debian".to_string(),
217 ],
218 _ => vec![format!("http://download.proxmox.com/debian/{}", product)],
219 },
220 format!("{}test", product),
221 ),
222 APTRepositoryHandle::CephQuincy => (
223 APTRepositoryPackageType::Deb,
224 vec!["http://download.proxmox.com/debian/ceph-quincy".to_string()],
225 "main".to_string(),
226 ),
227 APTRepositoryHandle::CephQuincyTest => (
228 APTRepositoryPackageType::Deb,
229 vec!["http://download.proxmox.com/debian/ceph-quincy".to_string()],
230 "test".to_string(),
231 ),
232 APTRepositoryHandle::CephPacific => (
233 APTRepositoryPackageType::Deb,
234 vec!["http://download.proxmox.com/debian/ceph-pacific".to_string()],
235 "main".to_string(),
236 ),
237 APTRepositoryHandle::CephPacificTest => (
238 APTRepositoryPackageType::Deb,
239 vec!["http://download.proxmox.com/debian/ceph-pacific".to_string()],
240 "test".to_string(),
241 ),
242 APTRepositoryHandle::CephOctopus => (
243 APTRepositoryPackageType::Deb,
244 vec!["http://download.proxmox.com/debian/ceph-octopus".to_string()],
245 "main".to_string(),
246 ),
247 APTRepositoryHandle::CephOctopusTest => (
248 APTRepositoryPackageType::Deb,
249 vec!["http://download.proxmox.com/debian/ceph-octopus".to_string()],
250 "test".to_string(),
251 ),
252 }
253 }
254
255 /// Get the standard repository referenced by the handle.
256 ///
257 /// An URI in the result is not '/'-terminated (under the assumption that no valid
258 /// product name is).
259 pub fn to_repository(self, product: &str, suite: &str) -> APTRepository {
260 let (package_type, uris, component) = self.info(product);
261
262 APTRepository {
263 types: vec![package_type],
264 uris: vec![uris.into_iter().next().unwrap()],
265 suites: vec![suite.to_string()],
266 components: vec![component],
267 options: vec![],
268 comment: String::new(),
269 file_type: APTRepositoryFileType::List,
270 enabled: true,
271 }
272 }
273 }