summaryrefslogtreecommitdiffstats
path: root/src/tools/rbd/Schedule.cc
blob: 15dda3aee7ef940941d2d89b9ee07d4d55824865 (plain)
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "common/Formatter.h"
#include "common/TextTable.h"
#include "common/ceph_json.h"
#include "tools/rbd/ArgumentTypes.h"
#include "tools/rbd/Schedule.h"
#include "tools/rbd/Utils.h"

#include <iostream>
#include <regex>

namespace rbd {

namespace at = argument_types;
namespace po = boost::program_options;

namespace {

int parse_schedule_name(const std::string &name, bool allow_images,
                        std::string *pool_name, std::string *namespace_name,
                        std::string *image_name) {
  // parse names like:
  // '', 'rbd/', 'rbd/ns/', 'rbd/image', 'rbd/ns/image'
  std::regex pattern("^(?:([^/]+)/(?:(?:([^/]+)/|)(?:([^/@]+))?)?)?$");
  std::smatch match;
  if (!std::regex_match(name, match, pattern)) {
    return -EINVAL;
  }

  if (match[1].matched) {
    *pool_name = match[1];
  } else {
    *pool_name = "-";
  }

  if (match[2].matched) {
    *namespace_name = match[2];
  } else if (match[3].matched) {
    *namespace_name = "";
  } else {
    *namespace_name = "-";
  }

  if (match[3].matched) {
    if (!allow_images) {
        return -EINVAL;
    }
    *image_name = match[3];
  } else {
    *image_name = "-";
  }

  return 0;
}

} // anonymous namespace

void add_level_spec_options(po::options_description *options,
                            bool allow_image) {
  at::add_pool_option(options, at::ARGUMENT_MODIFIER_NONE);
  at::add_namespace_option(options, at::ARGUMENT_MODIFIER_NONE);
  if (allow_image) {
    at::add_image_option(options, at::ARGUMENT_MODIFIER_NONE);
  }
}

int get_level_spec_args(const po::variables_map &vm,
                        std::map<std::string, std::string> *args) {
  if (vm.count(at::IMAGE_NAME)) {
    std::string pool_name;
    std::string namespace_name;
    std::string image_name;

    int r = utils::extract_spec(vm[at::IMAGE_NAME].as<std::string>(),
                                &pool_name, &namespace_name, &image_name,
                                nullptr, utils::SPEC_VALIDATION_FULL);
    if (r < 0) {
      return r;
    }

    if (!pool_name.empty()) {
      if (vm.count(at::POOL_NAME)) {
        std::cerr << "rbd: pool is specified both via pool and image options"
                  << std::endl;
        return -EINVAL;
      }
      if (vm.count(at::NAMESPACE_NAME)) {
        std::cerr << "rbd: namespace is specified both via namespace and image"
                  << " options" << std::endl;
        return -EINVAL;
      }
    }

    if (vm.count(at::POOL_NAME)) {
      pool_name = vm[at::POOL_NAME].as<std::string>();
    }

    if (vm.count(at::NAMESPACE_NAME)) {
      namespace_name = vm[at::NAMESPACE_NAME].as<std::string>();
    }

    if (namespace_name.empty()) {
      (*args)["level_spec"] = pool_name + "/" + image_name;
    } else {
      (*args)["level_spec"] = pool_name + "/" + namespace_name + "/" +
        image_name;
    }
    return 0;
  }

  if (vm.count(at::NAMESPACE_NAME)) {
    std::string pool_name;
    std::string namespace_name;

    if (vm.count(at::POOL_NAME)) {
      pool_name = vm[at::POOL_NAME].as<std::string>();
    }

    namespace_name = vm[at::NAMESPACE_NAME].as<std::string>();

    (*args)["level_spec"] = pool_name + "/" + namespace_name + "/";

    return 0;
  }

  if (vm.count(at::POOL_NAME)) {
    std::string pool_name = vm[at::POOL_NAME].as<std::string>();

    (*args)["level_spec"] = pool_name + "/";

    return 0;
  }

  (*args)["level_spec"] = "";

  return 0;
}

void normalize_level_spec_args(std::map<std::string, std::string> *args) {
  std::map<std::string, std::string> raw_args;
  std::swap(raw_args, *args);

  auto default_pool_name = utils::get_default_pool_name();
  for (auto [key, value] : raw_args) {
    if (key == "level_spec" && !value.empty() && value[0] == '/') {
      value = default_pool_name + value;
    }

    (*args)[key] = value;
  }
}

void add_schedule_options(po::options_description *positional,
                          bool mandatory) {
  if (mandatory) {
    positional->add_options()
      ("interval", "schedule interval");
  } else {
    positional->add_options()
      ("interval", po::value<std::string>()->default_value(""),
       "schedule interval");
  }
  positional->add_options()
    ("start-time", po::value<std::string>()->default_value(""),
     "schedule start time");
}

int get_schedule_args(const po::variables_map &vm, bool mandatory,
                      std::map<std::string, std::string> *args) {
  size_t arg_index = 0;

  std::string interval = utils::get_positional_argument(vm, arg_index++);
  if (interval.empty()) {
    if (mandatory) {
      std::cerr << "rbd: missing 'interval' argument" << std::endl;
      return -EINVAL;
    }
    return 0;
  }
  (*args)["interval"] = interval;

  std::string start_time = utils::get_positional_argument(vm, arg_index++);
  if (!start_time.empty()) {
    (*args)["start_time"] = start_time;
  }

  return 0;
}

int Schedule::parse(json_spirit::mValue &schedule_val) {
  if (schedule_val.type() != json_spirit::array_type) {
    std::cerr << "rbd: unexpected schedule JSON received: "
              << "schedule is not array" << std::endl;
    return -EBADMSG;
  }

  try {
    for (auto &item_val : schedule_val.get_array()) {
      if (item_val.type() != json_spirit::obj_type) {
        std::cerr << "rbd: unexpected schedule JSON received: "
                  << "schedule item is not object" << std::endl;
        return -EBADMSG;
      }

      auto &item = item_val.get_obj();

      if (item["interval"].type() != json_spirit::str_type) {
        std::cerr << "rbd: unexpected schedule JSON received: "
                  << "interval is not string" << std::endl;
        return -EBADMSG;
      }
      auto interval = item["interval"].get_str();

      std::string start_time;
      if (item["start_time"].type() == json_spirit::str_type) {
        start_time = item["start_time"].get_str();
      }

      items.push_back({interval, start_time});
    }

  } catch (std::runtime_error &) {
    std::cerr << "rbd: invalid schedule JSON received" << std::endl;
    return -EBADMSG;
  }

  return 0;
}

void Schedule::dump(ceph::Formatter *f) {
  f->open_array_section("items");
  for (auto &item : items) {
    f->open_object_section("item");
    f->dump_string("interval", item.first);
    f->dump_string("start_time", item.second);
    f->close_section(); // item
  }
  f->close_section(); // items
}

std::ostream& operator<<(std::ostream& os, Schedule &s) {
  std::string delimiter;
  for (auto &item : s.items) {
    os << delimiter << "every " << item.first;
    if (!item.second.empty()) {
      os << " starting at " << item.second;
    }
    delimiter = ", ";
  }
  return os;
}

int ScheduleList::parse(const std::string &list) {
  json_spirit::mValue json_root;
  if (!json_spirit::read(list, json_root)) {
    std::cerr << "rbd: invalid schedule list JSON received" << std::endl;
    return -EBADMSG;
  }

  try {
    for (auto &[id, schedule_val] : json_root.get_obj()) {
      if (schedule_val.type() != json_spirit::obj_type) {
        std::cerr << "rbd: unexpected schedule list JSON received: "
                  << "schedule_val is not object" << std::endl;
        return -EBADMSG;
      }
      auto &schedule = schedule_val.get_obj();
      if (schedule["name"].type() != json_spirit::str_type) {
        std::cerr << "rbd: unexpected schedule list JSON received: "
                  << "schedule name is not string" << std::endl;
        return -EBADMSG;
      }
      auto name = schedule["name"].get_str();

      if (schedule["schedule"].type() != json_spirit::array_type) {
        std::cerr << "rbd: unexpected schedule list JSON received: "
                  << "schedule is not array" << std::endl;
        return -EBADMSG;
      }

      Schedule s;
      int r = s.parse(schedule["schedule"]);
      if (r < 0) {
        return r;
      }
      schedules[name] = s;
    }
  } catch (std::runtime_error &) {
    std::cerr << "rbd: invalid schedule list JSON received" << std::endl;
    return -EBADMSG;
  }

  return 0;
}

Schedule *ScheduleList::find(const std::string &name) {
  auto it = schedules.find(name);
  if (it == schedules.end()) {
    return nullptr;
  }

  return &it->second;
}

void ScheduleList::dump(ceph::Formatter *f) {
  f->open_array_section("schedules");
  for (auto &[name, s] : schedules) {
    std::string pool_name;
    std::string namespace_name;
    std::string image_name;

    int r = parse_schedule_name(name, allow_images, &pool_name, &namespace_name,
                                &image_name);
    if (r < 0) {
      continue;
    }

    f->open_object_section("schedule");
    f->dump_string("pool", pool_name);
    f->dump_string("namespace", namespace_name);
    if (allow_images) {
      f->dump_string("image", image_name);
    }
    s.dump(f);
    f->close_section();
  }
  f->close_section();
}

std::ostream& operator<<(std::ostream& os, ScheduleList &l) {
  TextTable tbl;
  tbl.define_column("POOL", TextTable::LEFT, TextTable::LEFT);
  tbl.define_column("NAMESPACE", TextTable::LEFT, TextTable::LEFT);
  if (l.allow_images) {
    tbl.define_column("IMAGE", TextTable::LEFT, TextTable::LEFT);
  }
  tbl.define_column("SCHEDULE", TextTable::LEFT, TextTable::LEFT);

  for (auto &[name, s] : l.schedules) {
    std::string pool_name;
    std::string namespace_name;
    std::string image_name;

    int r = parse_schedule_name(name, l.allow_images, &pool_name,
                                &namespace_name, &image_name);
    if (r < 0) {
      continue;
    }

    std::stringstream ss;
    ss << s;

    tbl << pool_name << namespace_name;
    if (l.allow_images) {
      tbl << image_name;
    }
    tbl << ss.str() << TextTable::endrow;
  }

  os << tbl;
  return os;
}

} // namespace rbd