aboutsummaryrefslogtreecommitdiff
path: root/src/garage/cli_v2/layout.rs
blob: d44771c72d202bd87ba437a50e5588239e16d6ed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
use bytesize::ByteSize;
use format_table::format_table;

use garage_util::error::*;

use garage_api::admin::api::*;

use crate::cli::layout as cli_v1;
use crate::cli::structs::*;
use crate::cli_v2::*;

impl Cli {
	pub async fn layout_command_dispatch(&self, cmd: LayoutOperation) -> Result<(), Error> {
		match cmd {
			LayoutOperation::Assign(assign_opt) => self.cmd_assign_role(assign_opt).await,
			LayoutOperation::Remove(remove_opt) => self.cmd_remove_role(remove_opt).await,
			LayoutOperation::Apply(apply_opt) => self.cmd_apply_layout(apply_opt).await,
			LayoutOperation::Revert(revert_opt) => self.cmd_revert_layout(revert_opt).await,

			// TODO
			LayoutOperation::Show => {
				cli_v1::cmd_show_layout(&self.system_rpc_endpoint, self.rpc_host).await
			}
			LayoutOperation::Config(config_opt) => {
				cli_v1::cmd_config_layout(&self.system_rpc_endpoint, self.rpc_host, config_opt)
					.await
			}
			LayoutOperation::History => {
				cli_v1::cmd_layout_history(&self.system_rpc_endpoint, self.rpc_host).await
			}
			LayoutOperation::SkipDeadNodes(assume_sync_opt) => {
				cli_v1::cmd_layout_skip_dead_nodes(
					&self.system_rpc_endpoint,
					self.rpc_host,
					assume_sync_opt,
				)
				.await
			}
		}
	}

	pub async fn cmd_assign_role(&self, opt: AssignRoleOpt) -> Result<(), Error> {
		let status = self.api_request(GetClusterStatusRequest).await?;
		let layout = self.api_request(GetClusterLayoutRequest).await?;

		let all_node_ids_iter = status
			.nodes
			.iter()
			.map(|x| x.id.as_str())
			.chain(layout.roles.iter().map(|x| x.id.as_str()));

		let mut actions = vec![];

		for node in opt.replace.iter() {
			let id = find_matching_node(all_node_ids_iter.clone(), &node)?;

			actions.push(NodeRoleChange {
				id,
				action: NodeRoleChangeEnum::Remove { remove: true },
			});
		}

		for node in opt.node_ids.iter() {
			let id = find_matching_node(all_node_ids_iter.clone(), &node)?;

			let current = get_staged_or_current_role(&id, &layout);

			let zone = opt
				.zone
				.clone()
				.or_else(|| current.as_ref().map(|c| c.zone.clone()))
				.ok_or_message("Please specify a zone with the -z flag")?;

			let capacity = if opt.gateway {
				if opt.capacity.is_some() {
					return Err(Error::Message("Please specify only -c or -g".into()));
				}
				None
			} else if let Some(cap) = opt.capacity {
				Some(cap.as_u64())
			} else {
				current.as_ref().ok_or_message("Please specify a capacity with the -c flag, or set node explicitly as gateway with -g")?.capacity
			};

			let tags = if !opt.tags.is_empty() {
				opt.tags.clone()
			} else if let Some(cur) = current.as_ref() {
				cur.tags.clone()
			} else {
				vec![]
			};

			actions.push(NodeRoleChange {
				id,
				action: NodeRoleChangeEnum::Update {
					zone,
					capacity,
					tags,
				},
			});
		}

		self.api_request(UpdateClusterLayoutRequest(actions))
			.await?;

		println!("Role changes are staged but not yet committed.");
		println!("Use `garage layout show` to view staged role changes,");
		println!("and `garage layout apply` to enact staged changes.");
		Ok(())
	}

	pub async fn cmd_remove_role(&self, opt: RemoveRoleOpt) -> Result<(), Error> {
		let status = self.api_request(GetClusterStatusRequest).await?;
		let layout = self.api_request(GetClusterLayoutRequest).await?;

		let all_node_ids_iter = status
			.nodes
			.iter()
			.map(|x| x.id.as_str())
			.chain(layout.roles.iter().map(|x| x.id.as_str()));

		let id = find_matching_node(all_node_ids_iter.clone(), &opt.node_id)?;

		let actions = vec![NodeRoleChange {
			id,
			action: NodeRoleChangeEnum::Remove { remove: true },
		}];

		self.api_request(UpdateClusterLayoutRequest(actions))
			.await?;

		println!("Role removal is staged but not yet committed.");
		println!("Use `garage layout show` to view staged role changes,");
		println!("and `garage layout apply` to enact staged changes.");
		Ok(())
	}

	pub async fn cmd_apply_layout(&self, apply_opt: ApplyLayoutOpt) -> Result<(), Error> {
		let missing_version_error = r#"
Please pass the new layout version number to ensure that you are writing the correct version of the cluster layout.
To know the correct value of the new layout version, invoke `garage layout show` and review the proposed changes.
        "#;

		let req = ApplyClusterLayoutRequest {
			version: apply_opt.version.ok_or_message(missing_version_error)?,
		};
		let res = self.api_request(req).await?;

		for line in res.message.iter() {
			println!("{}", line);
		}

		println!("New cluster layout with updated role assignment has been applied in cluster.");
		println!("Data will now be moved around between nodes accordingly.");

		Ok(())
	}

	pub async fn cmd_revert_layout(&self, revert_opt: RevertLayoutOpt) -> Result<(), Error> {
		if !revert_opt.yes {
			return Err(Error::Message(
				"Please add the --yes flag to run the layout revert operation".into(),
			));
		}

		self.api_request(RevertClusterLayoutRequest).await?;

		println!("All proposed role changes in cluster layout have been canceled.");
		Ok(())
	}
}

// --------------------------
// ---- helper functions ----
// --------------------------

pub fn capacity_string(v: Option<u64>) -> String {
	match v {
		Some(c) => ByteSize::b(c).to_string_as(false),
		None => "gateway".to_string(),
	}
}

pub fn get_staged_or_current_role(
	id: &str,
	layout: &GetClusterLayoutResponse,
) -> Option<NodeRoleResp> {
	for node in layout.staged_role_changes.iter() {
		if node.id == id {
			return match &node.action {
				NodeRoleChangeEnum::Remove { .. } => None,
				NodeRoleChangeEnum::Update {
					zone,
					capacity,
					tags,
				} => Some(NodeRoleResp {
					id: id.to_string(),
					zone: zone.to_string(),
					capacity: *capacity,
					tags: tags.clone(),
				}),
			};
		}
	}

	for node in layout.roles.iter() {
		if node.id == id {
			return Some(node.clone());
		}
	}

	None
}

pub fn find_matching_node<'a>(
	cand: impl std::iter::Iterator<Item = &'a str>,
	pattern: &'a str,
) -> Result<String, Error> {
	let mut candidates = vec![];
	for c in cand {
		if c.starts_with(pattern) && !candidates.contains(&c) {
			candidates.push(c);
		}
	}
	if candidates.len() != 1 {
		Err(Error::Message(format!(
			"{} nodes match '{}'",
			candidates.len(),
			pattern,
		)))
	} else {
		Ok(candidates[0].to_string())
	}
}

pub fn print_staging_role_changes(layout: &GetClusterLayoutResponse) -> bool {
	let has_role_changes = !layout.staged_role_changes.is_empty();

	// TODO!! Layout parameters
	let has_layout_changes = false;

	if has_role_changes || has_layout_changes {
		println!();
		println!("==== STAGED ROLE CHANGES ====");
		if has_role_changes {
			let mut table = vec!["ID\tTags\tZone\tCapacity".to_string()];
			for change in layout.staged_role_changes.iter() {
				match &change.action {
					NodeRoleChangeEnum::Update {
						tags,
						zone,
						capacity,
					} => {
						let tags = tags.join(",");
						table.push(format!(
							"{:.16}\t{}\t{}\t{}",
							change.id,
							tags,
							zone,
							capacity_string(*capacity),
						));
					}
					NodeRoleChangeEnum::Remove { .. } => {
						table.push(format!("{:.16}\tREMOVED", change.id));
					}
				}
			}
			format_table(table);
			println!();
		}
		//TODO
		/*
		if has_layout_changes {
			println!(
				"Zone redundancy: {}",
				staging.parameters.get().zone_redundancy
			);
		}
		*/
		true
	} else {
		false
	}
}