GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — wsjcpp-docker-api/src/wsjcpp_docker_api.cpp at master · wsjcpp/wsjcpp-docker-api Expand file tree/ Copy pathwsjcpp_docker_api.cppMore file actions 270 lines (222 loc) · 8.48 KB / Copy pathwsjcpp_docker_api.cppFile metadata and controls270 lines (222 loc) · 8.48 KB 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 #include "wsjcpp_docker_api.h" #include <wsjcpp_core.h> #include <curl/curl.h> #include <json.hpp> // --------------------------------------------------------------------- // WsjcppDockerImage WsjcppDockerImage::WsjcppDockerImage() { TAG = "WsjcppDockerImage"; m_sId = ""; m_sParentId = ""; m_nCreated = 0; m_nSharedSize = 0; m_nSize = 0; m_nVirtualSize = 0; } // --------------------------------------------------------------------- bool WsjcppDockerImage::fillFromJson(const nlohmann::json &jsonResponse) { m_jsonOriginal = jsonResponse; auto it = jsonResponse.begin(); for (; it != jsonResponse.end(); ++it) { std::string sKey = it.key(); if (sKey == "ParentId") { m_sParentId = it.value(); } else if (sKey == "Id") { m_sId = it.value(); } else if (sKey == "Containers") { m_nContainers = it.value(); } else if (sKey == "SharedSize") { m_nSharedSize = it.value(); } else if (sKey == "VirtualSize") { m_nVirtualSize = it.value(); } else if (sKey == "Size") { m_nSize = it.value(); } else if (sKey == "Created") { m_nCreated = it.value(); } else if (sKey == "RepoTags") { m_vRepoTags.clear(); nlohmann::json arr = it.value(); for (int i = 0; i < arr.size(); i++) { std::string sTag = arr.at(i); m_vRepoTags.push_back(sTag); } } else if (sKey == "RepoDigests") { m_vRepoDigests.clear(); nlohmann::json arr = it.value(); for (int i = 0; i < arr.size(); i++) { std::string sDigest = arr.at(i); m_vRepoDigests.push_back(sDigest); } } else if (sKey == "Labels") { nlohmann::json val = it.value(); if (!val.is_null()) { auto it2 = val.begin(); for (; it2 != val.end(); ++it2) { std::string sKey2 = it2.key(); std::string sValue2 = it2.value(); m_mapLabels[sKey2] = sValue2; } } } else { WsjcppLog::warn(TAG, "Ignored key images: " + sKey); std::cout << it.key() << " | " << it.value() << "\n"; } } return true; } // --------------------------------------------------------------------- nlohmann::json WsjcppDockerImage::getJsonOriginal() { return m_jsonOriginal; } // --------------------------------------------------------------------- std::string WsjcppDockerImage::getId() { return m_sId; } // --------------------------------------------------------------------- std::string WsjcppDockerImage::getParentId() { return m_sParentId; } // --------------------------------------------------------------------- long WsjcppDockerImage::getCreated() { return m_nCreated; } // --------------------------------------------------------------------- std::map<std::string, std::string> WsjcppDockerImage::getLabels() { return m_mapLabels; } // --------------------------------------------------------------------- std::vector<std::string> WsjcppDockerImage::getRepoDigests() { return m_vRepoDigests; } // --------------------------------------------------------------------- std::vector<std::string> WsjcppDockerImage::getRepoTags() { return m_vRepoTags; } // --------------------------------------------------------------------- long WsjcppDockerImage::getSharedSize() { return m_nSharedSize; } // --------------------------------------------------------------------- long WsjcppDockerImage::getSize() { return m_nSize; } // --------------------------------------------------------------------- long WsjcppDockerImage::getVirtualSize() { return m_nVirtualSize; } // --------------------------------------------------------------------- long WsjcppDockerImage::getContainers() { return m_nContainers; } // --------------------------------------------------------------------- // WsjcppDockerApi WsjcppDockerApi::WsjcppDockerApi() { m_sApiVersionPrefix = "/v1.40/"; m_bConneted = false; TAG = "WsjcppDockerApi(" + m_sApiVersionPrefix + ")"; } // --------------------------------------------------------------------- std::string WsjcppDockerApi::getDefaultUnixSocketPath() { return "/var/run/docker.sock"; } // --------------------------------------------------------------------- bool WsjcppDockerApi::isConnected() { return m_bConneted; } // --------------------------------------------------------------------- bool WsjcppDockerApi::doConnectUnixSocketPath(const std::string &sDockerUnixSocketPath) { m_sDockerUnixSocketPath = sDockerUnixSocketPath; std::string sResponse; if (!requestGet(makeUrl("/containers/json?all=0"), sResponse)) { m_bConneted = false; } else { m_bConneted = true; } return m_bConneted; } // --------------------------------------------------------------------- /* bool WsjcppDockerApi::getContainers(std::vector<WsjcppDockerContainer> &vContainers, std::string &sError) { if (!m_bConneted) { sError = "Not connected"; return false; } // TODO curl with request get // GET /containers/json return true; } */ // --------------------------------------------------------------------- bool WsjcppDockerApi::getImages(std::vector<WsjcppDockerImage> &vImages, std::string &sError) { if (!m_bConneted) { sError = "Not connected"; return false; } std::string sResponse = ""; if (!requestGet(makeUrl("/images/json?all=0"), sResponse)) { sError = "Something wrong"; return false; } // WsjcppLog::info(TAG, "sResponse = " + sResponse); nlohmann::json jsonResponse = nlohmann::json::parse(sResponse); int nSize = jsonResponse.size(); for (int i = 0; i < nSize; i++) { nlohmann::json jsonImage = jsonResponse.at(i); // WsjcppLog::info(TAG, "image = " + jsonImage.dump(4)); WsjcppDockerImage image; image.fillFromJson(jsonImage); vImages.push_back(image); } return true; } // --------------------------------------------------------------------- static size_t WsjcppDockerApi_CurlWrite_CallbackFunc_DataToStr(void *contents, size_t size, size_t nmemb, void *userp) { ((std::string*)userp)->append((char*)contents, size * nmemb); return size * nmemb; } // --------------------------------------------------------------------- std::string WsjcppDockerApi::makeUrl(const std::string & sPath) { return "http://localhost/" + m_sApiVersionPrefix + sPath; } // --------------------------------------------------------------------- bool WsjcppDockerApi::requestGet(const std::string &sURL, std::string &sResponse) { WsjcppLog::info(TAG, "sURL: '" + sURL + "'"); std::string sUserAgent = "wsjcpp-docker-api/?"; std::string readBuffer; CURL *curl; CURLcode res; curl = curl_easy_init(); if (curl) { // curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); //only for https // curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); //only for https curl_easy_setopt(curl, CURLOPT_URL, sURL.c_str()); curl_easy_setopt(curl, CURLOPT_UNIX_SOCKET_PATH, m_sDockerUnixSocketPath.c_str()); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WsjcppDockerApi_CurlWrite_CallbackFunc_DataToStr); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); curl_easy_setopt(curl, CURLOPT_USERAGENT, sUserAgent.c_str()); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 50L); // curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); res = curl_easy_perform(curl); if (res != CURLE_OK) { WsjcppLog::err(TAG, "Curl failed, reason " + std::string(curl_easy_strerror(res))); curl_easy_cleanup(curl); return false; } else { long response_code; curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code); if (response_code != 200) { WsjcppLog::info(TAG, "end " + std::to_string(response_code)); // TODO remove file curl_easy_cleanup(curl); return false; } } // always cleanup curl_easy_cleanup(curl); } sResponse = readBuffer; // WsjcppLog::info(TAG, "Response: " + readBuffer); return true; } // --------------------------------------------------------------------- |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |