A container image for a web-site preview proxy for Zuul
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.cc 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
  2. * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  3. *
  4. * Copyright (C) 2019 Red Hat, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #include <config.h>
  20. #include <pthread.h>
  21. #include <cpprest/http_client.h>
  22. #include <bits/stdc++.h>
  23. using namespace std;
  24. vector<string> split(const string &in)
  25. {
  26. istringstream stream(in);
  27. vector<string> parts;
  28. string part;
  29. while (getline(stream, part, '.')) {
  30. parts.push_back(part);
  31. }
  32. return parts;
  33. }
  34. class Cache {
  35. // A queue of hostname, URL pairs. The head of the queue is always
  36. // the most recently accessed entry, the tail is the least.
  37. list<pair<const string, const string>> queue;
  38. // A map of hostname -> iterator that points into the queue, for
  39. // quick lookup.
  40. unordered_map<string, list<pair<const string, const string>>::iterator> map;
  41. // The maximum size of the cache.
  42. const uint32_t size;
  43. public:
  44. Cache(uint s)
  45. : queue {}, map {}, size{s}
  46. { }
  47. // Lookup the hostname in the cache and return the URL if present.
  48. // If the entry is present, it is moved to the head of the queue.
  49. optional<const string> get(const string &key)
  50. {
  51. auto location = map.find(key);
  52. if (location == map.end())
  53. return {};
  54. auto val = *(location->second);
  55. queue.erase(location->second);
  56. queue.push_front(val);
  57. //cout << "get push " << val.second << endl;
  58. return val.second;
  59. }
  60. // Add an entry to the cache. If the cache is full, drop the least
  61. // recently used entry.
  62. void put(const string &key, const string &value)
  63. {
  64. auto location = map.find(key);
  65. if (location != map.end())
  66. return;
  67. if (queue.size() == size) {
  68. auto last = queue.back();
  69. //cout << "put pop " << last.second << endl;
  70. queue.pop_back();
  71. map.erase(last.first);
  72. }
  73. //cout << "put push " << value << endl;
  74. queue.push_front(make_pair(key, value));
  75. map[key] = queue.begin();
  76. }
  77. };
  78. int main(int, char**)
  79. {
  80. web::http::client::http_client client("https://zuul.opendev.org");
  81. string hostname;
  82. Cache cache{2};
  83. while (getline(cin, hostname)) {
  84. // Expected hostname:
  85. // site.75031cad206c4014ad7a3387091d15ab.openstack.preview.opendev.org
  86. // Apache will drop "preview.opendev.org", so our expected input will be:
  87. // site.75031cad206c4014ad7a3387091d15ab.openstack
  88. // site.7c16d914db5a4c4b91cd9a31d119dd48.openstack
  89. // site.688b70499b9a41a08f498ed6e932960c.openstack
  90. // site.dbefc23dcc594577a8bfa4db4f9b0a8f.openstack
  91. auto val = cache.get(hostname);
  92. if (val.has_value()) {
  93. cout << val.value() << endl;
  94. continue;
  95. }
  96. auto parts = split(hostname);
  97. if (parts.size() < 3) {
  98. cout << "not enough args" << endl;
  99. continue;
  100. }
  101. auto artifact = parts[0];
  102. auto buildid = parts[1];
  103. auto tenant = parts[2];
  104. /*
  105. cout << artifact << endl
  106. << buildid << endl
  107. << tenant << endl;
  108. */
  109. // 75031cad206c4014ad7a3387091d15ab
  110. try {
  111. auto uri = web::uri_builder("/api/tenant/" + tenant + "/build");
  112. uri.append_path(buildid);
  113. auto response = client.request(
  114. web::http::methods::GET, uri.to_string()).get();
  115. // body is a web::json::value
  116. // cout << response.status_code() << endl;
  117. auto body = response.extract_json().get();
  118. //cout << body.serialize() << endl;
  119. // TODO: use artifact
  120. // body["log_url"].as_string() returns a const std::string&
  121. cout << body["log_url"].as_string() << endl;
  122. cache.put(hostname, body["log_url"].as_string());
  123. } catch (...) {
  124. cout << "error" << endl;
  125. }
  126. }
  127. }