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