Skip to content

Commit f577b39

Browse files
committed
[feat.] Support uploading 'output' to registry while merging layers
Signed-off-by: Yifan Yuan <[email protected]>
1 parent a117098 commit f577b39

8 files changed

Lines changed: 140 additions & 59 deletions

File tree

src/overlaybd/lsmt/test/test.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,12 @@ void test_combo(const IMemoryIndex *indexes[], size_t ni, const SegmentMapping s
246246
auto ret = memcmp(pm, stdrst, nrst * sizeof(stdrst[0]));
247247
EXPECT_EQ(ret, 0);
248248
}
249+
LOG_INFO("make RO index of ci");
250+
auto ro_idx = ci.make_read_only_index();
251+
if (ro_idx->size() == nrst) {
252+
auto ret = memcmp(ro_idx->buffer(), stdrst, nrst * sizeof(stdrst[0]));
253+
EXPECT_EQ(ret, 0);
254+
}
249255

250256
ci.backing_index();
251257
// delete mi;
@@ -599,20 +605,27 @@ TEST_F(FileTest3, stack_files) {
599605
LOG_INFO("RO valid data: `", stat.valid_data_size);
600606
merged = lfs->open(fn_merged, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
601607
EXPECT_EQ(lower->flatten(merged), 0);
602-
cout << "verifying flattened layer of lowers" << endl;
608+
// auto localfile = lfs->open(fn_merged, O_RDONLY);
609+
struct stat st;
610+
merged->fstat(&st);
611+
cout << "verifying flattened layer of lowers, st_size: "<<st.st_size << endl;
603612
verify_file(fn_merged);
604613
delete merged;
605614
cout << "generating a RW layer by randwrite()" << endl;
606615
auto upper = create_file_rw();
607616
auto file = stack_files(upper, lower, 0, true);
608617
randwrite(file, FLAGS_nwrites);
609618
verify_file(file);
610-
cout << "verifying flattened layer of stacked layers" << endl;
611619

612620
merged = lfs->open(fn_merged, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
613621
file->flatten(merged);
622+
merged->fstat(&st);
623+
cout << "verifying flattened layer of stacked layers, st_size: "<<st.st_size << endl;
624+
merged->close();
625+
614626
verify_file(fn_merged);
615627
delete file;
628+
delete merged;
616629
}
617630

618631
TEST_F(FileTest3, seek_data) {

src/overlaybd/registryfs/registryfs.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,14 @@ photon::fs::IFileSystem *new_registryfs_v2(PasswordCB callback,
4343
const char *customized_ua = nullptr);
4444

4545
photon::fs::IFile* new_registry_uploader(photon::fs::IFile *lfile,
46-
std::string &upload_url,
47-
std::string &username, std::string &password,
46+
const std::string &upload_url,
47+
const std::string &username,
48+
const std::string &password,
4849
uint64_t timeout,
4950
ssize_t upload_bs = -1,
5051
const char *cert_file = nullptr,
5152
const char *key_file = nullptr);
53+
54+
int registry_uploader_fini(photon::fs::IFile *uploader, std::string &digest);
55+
5256
}

src/overlaybd/registryfs/registryfs_v2.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "registryfs.h"
1818

1919

20+
#include <cerrno>
2021
#include <sys/stat.h>
2122
#include <sys/types.h>
2223
#include <unistd.h>
@@ -561,8 +562,8 @@ class RegistryUploader : public VirtualFile {
561562
uint64_t m_timeout = -1;
562563
estring m_token;
563564

564-
RegistryUploader(IFile *lfile, std::string &upload_url, std::string &username,
565-
std::string &password, uint64_t timeout, ssize_t upload_bs,
565+
RegistryUploader(IFile *lfile, const std::string &upload_url, const std::string &username,
566+
const std::string &password, uint64_t timeout, ssize_t upload_bs,
566567
photon::net::TLSContext *ctx)
567568
: m_local_file(lfile), m_origin_upload_url(upload_url), m_username(username), m_password(password),
568569
m_timeout(timeout), m_tls_ctx(ctx) {
@@ -829,15 +830,15 @@ class RegistryUploader : public VirtualFile {
829830
LOG_INFO(VALUE(m_upload_url));
830831
return 0;
831832
}
832-
LOG_ERROR_RETURN(0, -1, "failed to get upload url, code=`", op.status_code);
833+
LOG_ERROR_RETURN(0, -1, "failed to get upload url [`], code=`", m_upload_url, op.status_code);
833834
}
834835

835836
protected:
836837
photon::net::TLSContext *m_tls_ctx;
837838
};
838839

839-
IFile *new_registry_uploader(IFile *lfile, std::string &upload_url, std::string &username,
840-
std::string &password, uint64_t timeout, ssize_t upload_bs,
840+
IFile *new_registry_uploader(IFile *lfile, const std::string &upload_url, const std::string &username,
841+
const std::string &password, uint64_t timeout, ssize_t upload_bs,
841842
const char *cert_file, const char *key_file) {
842843
auto ctx = new_tls_context_from_file(cert_file, key_file);
843844
if (!ctx) {
@@ -893,3 +894,14 @@ photon::net::TLSContext *new_tls_context_from_file(const char *cert_file, const
893894
}
894895
return photon::net::new_tls_context(cert_str.c_str(), key_str.c_str(), nullptr);
895896
}
897+
898+
int registry_uploader_fini(photon::fs::IFile *uploader, std::string &digest) {
899+
if (uploader == nullptr) {
900+
LOG_ERRNO_RETURN(0, EINVAL, "invalid pointer");
901+
}
902+
if (uploader->fsync() < 0) {
903+
LOG_ERRNO_RETURN(0, -1, "failed to upload blob");
904+
}
905+
digest = ((RegistryUploader *) uploader)->m_sha256sum;
906+
return 0;
907+
}

src/tools/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ add_executable(overlaybd-zfile overlaybd-zfile.cpp)
1616
target_include_directories(overlaybd-zfile PUBLIC ${PHOTON_INCLUDE_DIR})
1717
target_link_libraries(overlaybd-zfile photon_static overlaybd_lib)
1818

19-
add_executable(overlaybd-apply overlaybd-apply.cpp comm_func.cpp)
19+
add_executable(overlaybd-apply overlaybd-apply.cpp)
2020
target_include_directories(overlaybd-apply PUBLIC ${PHOTON_INCLUDE_DIR} ${rapidjson_SOURCE_DIR}/include)
2121
target_link_libraries(overlaybd-apply photon_static overlaybd_lib overlaybd_image_lib checksum_lib)
2222
set_target_properties(overlaybd-apply PROPERTIES INSTALL_RPATH "/opt/overlaybd/lib")
2323

24-
add_executable(turboOCI-apply turboOCI-apply.cpp comm_func.cpp)
24+
add_executable(turboOCI-apply turboOCI-apply.cpp)
2525
target_include_directories(turboOCI-apply PUBLIC ${PHOTON_INCLUDE_DIR} ${rapidjson_SOURCE_DIR}/include)
2626
target_link_libraries(turboOCI-apply photon_static overlaybd_lib overlaybd_image_lib)
2727
set_target_properties(turboOCI-apply PROPERTIES INSTALL_RPATH "/opt/overlaybd/lib")

src/tools/comm_func.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <photon/fs/subfs.h>
2121
#include <photon/fs/localfs.h>
2222
#include <photon/fs/extfs/extfs.h>
23+
#include "../overlaybd/registryfs/registryfs.h"
2324
#include "../image_service.h"
2425
#include "../image_file.h"
2526
#include "../overlaybd/tar/erofs/liberofs.h"
@@ -91,3 +92,23 @@ photon::fs::IFileSystem *create_erofs_fs(photon::fs::IFile *imgfile, uint64_t bl
9192
{
9293
return erofs_create_fs(imgfile, blksz);
9394
}
95+
IFile *create_uploader(ZFile::CompressArgs *zfile_args, IFile *src,
96+
const string &upload_url, const string &cred_file_path, uint64_t timeout_minute, uint64_t upload_bs_KB,
97+
const string &tls_key_path, const string &tls_cert_path
98+
){
99+
100+
zfile_args->overwrite_header = false;
101+
LOG_INFO("upload to `", upload_url);
102+
std::string username, password;
103+
if (load_cred_from_file(cred_file_path, upload_url, username, password) < 0) {
104+
fprintf(stderr, "failed to read upload cred file\n");
105+
exit(-1);
106+
}
107+
auto upload_builder = new_registry_uploader(src, upload_url, username, password,
108+
timeout_minute*60*1000*1000, upload_bs_KB*1024, tls_cert_path.c_str(), tls_key_path.c_str());
109+
if (upload_builder == nullptr) {
110+
fprintf(stderr, "failed to init upload\n");
111+
exit(-1);
112+
}
113+
return upload_builder;
114+
}

src/tools/comm_func.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ photon::fs::IFile *open_file(const char *fn, int flags, mode_t mode = 0, photon:
4141
int create_overlaybd(const std::string &srv_config, const std::string &dev_config,
4242
ImageService *&, photon::fs::IFile *&);
4343

44+
photon::fs::IFile *create_uploader(ZFile::CompressArgs *zfile_args, IFile *src,
45+
const std::string &upload_url, const std::string &cred_file_path, uint64_t timeout_minute, uint64_t upload_bs_KB,
46+
const std::string &tls_key_path, const std::string &tls_cert_path);
47+
4448
photon::fs::IFileSystem *create_ext4fs(photon::fs::IFile *imgfile, bool mkfs,
4549
bool enable_buffer, const char* root);
4650

src/tools/overlaybd-commit.cpp

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,38 @@
2121
#include "../overlaybd/lsmt/file.h"
2222
#include "../overlaybd/zfile/zfile.h"
2323
#include "../overlaybd/tar/tar_file.h"
24-
#include "../overlaybd/registryfs/registryfs.h"
25-
#include "../image_service.h"
2624
#include <errno.h>
2725
#include <fcntl.h>
2826
#include <inttypes.h>
29-
#include <memory>
3027
#include <string>
3128
#include <stdio.h>
3229
#include <stdlib.h>
3330
#include <sys/stat.h>
3431
#include <sys/types.h>
3532
#include <unistd.h>
3633
#include "CLI11.hpp"
34+
#include "comm_func.h"
35+
#include "../overlaybd/registryfs/registryfs.h"
36+
3737

3838
using namespace std;
3939
using namespace LSMT;
4040
using namespace photon::fs;
4141

42+
string commit_msg;
43+
string uuid, parent_uuid;
44+
std::string algorithm;
45+
int block_size = -1;
46+
std::string data_file_path, index_file_path, commit_file_path, remote_mapping_file;
47+
bool compress_zfile = false;
48+
bool build_turboOCI = false;
49+
bool build_fastoci = false;
50+
bool tar = false, rm_old = false, seal = false, commit_sealed = false;
51+
bool verbose = false;
52+
int compress_threads = 1;
53+
std::string upload_url, cred_file_path, tls_key_path, tls_cert_path;
54+
ssize_t upload_bs = 262144;
55+
4256

4357
IFile *open_file(IFileSystem *fs, const char *fn, int flags, mode_t mode = 0) {
4458
auto file = fs->open(fn, flags, mode);
@@ -50,19 +64,6 @@ IFile *open_file(IFileSystem *fs, const char *fn, int flags, mode_t mode = 0) {
5064
}
5165

5266
int main(int argc, char **argv) {
53-
string commit_msg;
54-
string uuid, parent_uuid;
55-
std::string algorithm;
56-
int block_size = -1;
57-
std::string data_file_path, index_file_path, commit_file_path, remote_mapping_file;
58-
bool compress_zfile = false;
59-
bool build_turboOCI = false;
60-
bool build_fastoci = false;
61-
bool tar = false, rm_old = false, seal = false, commit_sealed = false;
62-
bool verbose = false;
63-
int compress_threads = 1;
64-
std::string upload_url, cred_file_path;
65-
ssize_t upload_bs = 262144;
6667

6768
CLI::App app{"this is overlaybd-commit"};
6869
app.add_option("-m", commit_msg, "add some custom message if needed");
@@ -87,10 +88,18 @@ int main(int argc, char **argv) {
8788
app.add_option("--upload", upload_url, "registry upload url");
8889
app.add_option("--upload_bs", upload_bs, "block size for upload, in KB");
8990
app.add_option("--cred_file_path", cred_file_path, "cred file path for registryfs")->type_name("FILEPATH")->check(CLI::ExistingFile);
91+
app.add_option("--tls_key_path", tls_key_path, "TLSKeyPairPath for private Registry")->type_name("FILEPATH")->check(CLI::ExistingFile);
92+
app.add_option("--tls_cert_path", tls_cert_path, "TLSCertPath for private Registry")->type_name("FILEPATH")->check(CLI::ExistingFile);
93+
94+
9095

9196
CLI11_PARSE(app, argc, argv);
9297
build_turboOCI = build_turboOCI || build_fastoci;
9398
set_log_output_level(verbose ? 0 : 1);
99+
if (tar && (upload_url.empty() == false)){
100+
fprintf(stderr, "unsupport option with '-t' and '--upload' at the same time.");
101+
exit(-1);
102+
}
94103
photon::init(photon::INIT_EVENT_DEFAULT, photon::INIT_IO_DEFAULT);
95104
DEFER({photon::fini();});
96105

@@ -164,21 +173,10 @@ int main(int argc, char **argv) {
164173
zfile_args->overwrite_header = true;
165174

166175
if (!upload_url.empty()) {
167-
zfile_args->overwrite_header = false;
168-
LOG_INFO("upload to `", upload_url);
169-
std::string username, password;
170-
if (load_cred_from_file(cred_file_path, upload_url, username, password) < 0) {
171-
fprintf(stderr, "failed to read upload cred file\n");
172-
exit(-1);
173-
}
174-
upload_builder = new_registry_uploader(out, upload_url, username, password, 2UL*60*1000*1000, upload_bs*1024);
175-
if (upload_builder == nullptr) {
176-
fprintf(stderr, "failed to init upload\n");
177-
exit(-1);
178-
}
176+
LOG_INFO("enable upload. URL: `, upload_bs: `, tls_key_path: `, tls_cert_path: `", upload_url, upload_bs, tls_key_path, tls_cert_path);
177+
upload_builder = create_uploader(zfile_args, out, upload_url, cred_file_path, 2, upload_bs, tls_key_path, tls_cert_path);
179178
out = upload_builder;
180179
}
181-
182180
zfile_builder = ZFile::new_zfile_builder(out, zfile_args, false);
183181
out = zfile_builder;
184182
} else {
@@ -213,12 +211,12 @@ int main(int argc, char **argv) {
213211
if (zfile_args) {
214212
delete zfile_args;
215213
}
216-
217-
if (upload_builder != nullptr && upload_builder->fsync() < 0) {
218-
fprintf(stderr, "failed to commit or upload");
219-
return -1;
220-
}
221-
214+
string digest = "";
215+
if (upload_builder != nullptr && registry_uploader_fini(upload_builder, digest) != 0){
216+
fprintf(stderr, "failed to commit or upload\n");
217+
exit(-1);
218+
};
219+
fprintf(stderr, "%s\n", digest.c_str());
222220
delete upload_builder;
223221
delete fout;
224222
delete fin;

0 commit comments

Comments
 (0)