Skip to content

Commit

Permalink
Moving external dependency abs-fuu to local dep (#525)
Browse files Browse the repository at this point in the history
* Update CXX version to 17

* Added requirement for C++17

* Removed external dependency abs-fuu

* updating install deps script

* Removed deprecated function use and moved to Windows API. Also learned about HN

* Addressed PR comments

* Fixed compiler error

* fixed inclusion of a string library

* Addressing windows build errors
  • Loading branch information
nihemstr committed Sep 14, 2023
1 parent d707681 commit ead5b99
Show file tree
Hide file tree
Showing 12 changed files with 303 additions and 62 deletions.
3 changes: 2 additions & 1 deletion cmake/agentRules.cmake
Expand Up @@ -3,7 +3,8 @@

macro (compileAsC99)
set (CMAKE_C_STANDARD 99)
set (CMAKE_CXX_STANDARD 11)
set (CMAKE_CXX_STANDARD 17)
set (CMAKE_CXX_STANDARD_REQUIRED ON)
endmacro (compileAsC99)

macro (disableRTTI)
Expand Down
74 changes: 28 additions & 46 deletions scripts/install-deps.sh
Expand Up @@ -45,6 +45,10 @@ install_aduc_deps=false
install_azure_iot_sdk=false
azure_sdk_ref=LTS_08_2023

# ADUC Diagnostics Deps
azure_storage_sdk_branch_ref=main
azure_storage_sdk_tag_ref=azure-core_1.6.0
install_azure_storage_sdk=false
# ADUC Test Deps

install_catch2=false
Expand All @@ -54,9 +58,6 @@ install_swupdate=false
default_swupdate_ref=2021.11
swupdate_ref=$default_swupdate_ref

install_azure_blob_storage_file_upload_utility=false
azure_blob_storage_file_upload_utility_ref=main

install_cmake=false
supported_cmake_version='3.23.2'
install_cmake_version="$supported_cmake_version"
Expand Down Expand Up @@ -87,7 +88,7 @@ catch2_cc=""
catch2_cxx=""

# Dependencies packages
aduc_packages=('git' 'make' 'build-essential' 'cmake' 'ninja-build' 'libcurl4-openssl-dev' 'libssl-dev' 'uuid-dev' 'python2.7' 'lsb-release' 'curl' 'wget' 'pkg-config')
aduc_packages=('git' 'make' 'build-essential' 'cmake' 'ninja-build' 'libcurl4-openssl-dev' 'libssl-dev' 'libxml2-dev' 'uuid-dev' 'python2.7' 'lsb-release' 'curl' 'wget' 'pkg-config')
static_analysis_packages=('clang' 'clang-tidy' 'cppcheck')
compiler_packages=('gcc' 'g++')

Expand All @@ -109,10 +110,9 @@ print_help() {
echo " Implies --install-azure-iot-sdk and --install-catch2."
echo " When used with --install-packages will also install the package dependencies."
echo "--install-azure-iot-sdk Install the Azure IoT C SDK from source."
echo "--install-azure-storage-sdk Install the Azure SDK for CPP from source."
echo "--azure-iot-sdk-ref <ref> Install the Azure IoT C SDK from a specific branch or tag."
echo " Default is public-preview."
echo "--install-abs-file-upload-utility Install the Azure Blob Storage File Upload Utility from source."
echo "--abs-file-upload-utility-ref <ref> Install the Azure Blob Storage File Upload Utility from a specific branch or tag."
echo "--install-catch2 Install Catch2 from source."
echo "--install-cmake Installs supported version of cmake from installer if on ubuntu, else installs it from source."
echo "--install-shellcheck Installs supported version of shellcheck."
Expand Down Expand Up @@ -524,36 +524,27 @@ do_install_do() {
fi
}

do_install_azure_blob_storage_file_upload_utility() {
echo "Installing azure-blob-storage-file-upload-utility from source."
local abs_fuu_dir=$work_folder/azure_blob_storage_file_upload_utility
do_install_azure_storage_sdk() {
echo "Installing azure-storage-sdk"
local azure_storage_sdk_dir=$work_folder/azure_storage_sdk_dir

if [[ -d $abs_fuu_dir ]]; then
$SUDO rm -rf $abs_fuu_dir || return 1
if [[ -d $azure_storage_sdk_dir ]]; then
$SUDO rm -rf $azure_storage_sdk_dir || return
fi

local azure_storage_cpplite_url
local azure_storage_sdk_url
if [[ $use_ssh == "true" ]]; then
azure_storage_cpplite_url=git@github.com:Azure/azure-blob-storage-file-upload-utility.git
azure_storage_sdk_url=git@github.com:Azure/azure-sdk-for-cpp.git
else
azure_storage_cpplite_url=https://github.com/Azure/azure-blob-storage-file-upload-utility.git
azure_storage_sdk_url=https://github.com/Azure/azure-sdk-for-cpp.git
fi

echo -e "Cloning Azure Blob Storage File Upload Uility ...\n\tBranch: $azure_blob_storage_file_upload_utility_ref\n\t Folder: $abs_fuu_dir"
mkdir -p $abs_fuu_dir || return
pushd $abs_fuu_dir > /dev/null || return
git clone --recursive --single-branch --branch $azure_blob_storage_file_upload_utility_ref --depth 1 $azure_storage_cpplite_url . || return

echo -e "Installing Azure Blob Storage File Upload Utiltiy dependencies..."
echo -e "Building Azure Storage SDK ...\n\tBranch: $azure_storage_sdk_branch_ref\n\t Folder: $azure_storage_sdk_dir"
mkdir -p $azure_storage_sdk_dir || return
pushd $azure_storage_sdk_dir > /dev/null || return
git clone --recursive --single-branch --branch $azure_storage_sdk_branch_ref $azure_storage_sdk_url . || return

# Note added to make sure that install-deps.sh is executable
chmod u+x ./scripts/install-deps.sh

# Note we can skip the azure iot sdk installation because it is guaranteed that it will already be installed.
./scripts/install-deps.sh -a --skip-azure-iot-sdk-install

mkdir cmake || return
pushd cmake > /dev/null || return
git checkout tags/$azure_storage_sdk_tag_ref

local azure_blob_storage_file_upload_utility_cmake_options
if [[ $keep_source_code == "true" ]]; then
Expand All @@ -563,19 +554,15 @@ do_install_azure_blob_storage_file_upload_utility() {
azure_blob_storage_file_upload_utility_cmake_options+=("-DCMAKE_BUILD_TYPE:STRING=Release")
fi

azure_blob_storage_file_upload_utility_cmake_options+=("-DOPENSSL_ROOT_DIR=$openssl_dir_path")

echo -e "Building Azure Blob Storage File Upload Uility ...\n\tBranch: $azure_blob_storage_file_upload_utility_ref\n\t"
cmake "${azure_blob_storage_file_upload_utility_cmake_options[@]}" .. || return
cmake "${azure_blob_storage_file_upload_utility_cmake_options[@]}" . || return

cmake --build . || return 1
cmake --build . || return
$SUDO cmake --build . --target install || return

popd > /dev/null || return
popd > /dev/null || return

if [[ $keep_source_code != "true" ]]; then
$SUDO rm -rf $abs_fuu_dir || return 1
$SUDO rm -rf $azure_storage_sdk_dir || return
fi
}

Expand Down Expand Up @@ -830,18 +817,13 @@ while [[ $1 != "" ]]; do
--install-azure-iot-sdk)
install_azure_iot_sdk=true
;;
--install-azure-storage-sdk)
install_azure_storage_sdk=true
;;
--azure-iot-sdk-ref)
shift
azure_sdk_ref=$1
;;
--install-abs-file-upload-utility)
shift
install_azure_blob_storage_file_upload_utility=true
;;
--abs-file-upload-utility-ref)
shift
azure_blob_storage_file_upload_utility_ref=$1
;;
--install-catch2)
install_catch2=true
;;
Expand Down Expand Up @@ -958,7 +940,7 @@ if [[ $install_aduc_deps == "true" ]]; then
install_cmake=true
install_azure_iot_sdk=true
install_catch2=true
install_azure_blob_storage_file_upload_utility=true
install_azure_storage_sdk=true
fi

# Set implied options for packages only.
Expand Down Expand Up @@ -1049,8 +1031,8 @@ if [[ $install_packages_only == "false" ]]; then
do_install_do || $ret
fi

if [[ $install_azure_blob_storage_file_upload_utility == "true" ]]; then
do_install_azure_blob_storage_file_upload_utility || $ret
if [[ $install_azure_storage_sdk == "true" ]]; then
do_install_azure_storage_sdk || $ret
fi
fi

Expand Down
7 changes: 5 additions & 2 deletions src/CMakeLists.txt
@@ -1,5 +1,9 @@
cmake_minimum_required (VERSION 3.5)

include (agentRules)

compileasc99 ()

if (ADUC_TRACE_TARGET_DEPS)
set_property (GLOBAL PROPERTY GLOBAL_DEPENDS_DEBUG_MODE 1)
endif ()
Expand Down Expand Up @@ -38,8 +42,7 @@ if (MSVC)

# Avoid "warning LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs; use /NODEFAULTLIB:library"
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
set (CMAKE_EXE_LINKER_FLAGS
"${CMAKE_EXE_LINKER_FLAGS} /NODEFAULTLIB:msvcrt")
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /NODEFAULTLIB:msvcrt")
else ()
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /NODEFAULTLIB:libcmt")
endif ()
Expand Down
Expand Up @@ -11,14 +11,13 @@ add_library (diagnostics_component::${target_name} ALIAS ${target_name})

target_include_directories (${target_name} PUBLIC inc)

find_package (azure-storage-blobs-cpp CONFIG REQUIRED)
find_package (Parson REQUIRED)

target_link_aziotsharedutil (${target_name} PRIVATE)

if (NOT WIN32)
target_link_libraries (${target_name} PRIVATE azure_blob_storage_file_upload_utility
Azure::azure-storage-blobs)
diagnostic_utils::file_upload_utility)
endif ()

target_link_libraries (
Expand Down
Expand Up @@ -18,18 +18,17 @@ typedef struct tagBlobStorageInfo
STRING_HANDLE storageSasCredential; //!< Combined SAS URI and SAS Token for connecting to storage
} BlobStorageInfo;

static bool AzureBlobStorageFileUploadUtility_UploadFilesToContainer(
const BlobStorageInfo* blobInfo, const int maxConcurrency, VECTOR_HANDLE fileNames, const char* directoryPath)
static bool FileUploadUtility_UploadFilesToContainer(
const BlobStorageInfo* blobInfo, VECTOR_HANDLE fileNames, const char* directoryPath)
{
UNREFERENCED_PARAMETER(blobInfo);
UNREFERENCED_PARAMETER(maxConcurrency);
UNREFERENCED_PARAMETER(fileNames);
UNREFERENCED_PARAMETER(directoryPath);

return false;
}
#else
# include <azure_blob_storage_file_upload_utility.h>
# include <file_upload_utility.h>
#endif

#include <diagnostics_config_utils.h>
Expand Down Expand Up @@ -188,8 +187,7 @@ Diagnostics_Result DiagnosticsWorkflow_UploadFilesForComponent(
goto done;
}

if (!AzureBlobStorageFileUploadUtility_UploadFilesToContainer(
&blobInfo, 1, fileNames, STRING_c_str(logComponent->logPath)))
if (!FileUploadUtility_UploadFilesToContainer(&blobInfo, fileNames, STRING_c_str(logComponent->logPath)))
{
result = Diagnostics_Result_UploadFailed;
Log_Warn(
Expand Down
1 change: 1 addition & 0 deletions src/diagnostics_component/utils/CMakeLists.txt
Expand Up @@ -2,4 +2,5 @@ cmake_minimum_required (VERSION 3.5)

add_subdirectory (config_utils)
add_subdirectory (file_info_utils)
add_subdirectory (file_upload_utils)
add_subdirectory (operation_id_utils)
24 changes: 24 additions & 0 deletions src/diagnostics_component/utils/file_upload_utils/CMakeLists.txt
@@ -0,0 +1,24 @@
cmake_minimum_required (VERSION 3.13)

set (target_name file_upload_utility)

add_library (${target_name} STATIC src/file_upload_utility.cpp src/blob_storage_helper.cpp
src/blob_storage_helper.hpp)
add_library (diagnostic_utils::${target_name} ALIAS ${target_name})

find_package (azure_c_shared_utility REQUIRED)
find_package (azure-storage-blobs-cpp CONFIG REQUIRED)

include (agentRules)

include (find_curl_and_import_libcurl)

find_curl_and_import_libcurl ()

target_include_directories (${target_name} PUBLIC inc)

target_link_libraries (
${target_name}
PUBLIC aziotsharedutil
PRIVATE aduc::c_utils aduc::exception_utils Azure::azure-storage-blobs Azure::azure-core
CURL::libcurl)
@@ -0,0 +1,33 @@
/**
* @file file_upload_utility.h
* @brief Defines the interface for interacting with Azure Blob Storage and uploading files
*
* @copyright Copyright (c) Microsoft Corp.
*/
#ifndef FILE_UPLOAD_UTILITY_H
#define FILE_UPLOAD_UTILITY_H

#include <aduc/c_utils.h>
#include <azure_c_shared_utility/strings.h>
#include <azure_c_shared_utility/vector.h>
#include <stdbool.h>
#include <stdlib.h>

EXTERN_C_BEGIN

/**
* @brief Struct that contains the information for uploading a set of blobs to Azure Blob Storage
*/
typedef struct tagBlobStorageInfo
{
STRING_HANDLE
virtualDirectoryPath; //!< Virtual hierarchy for the blobs
STRING_HANDLE storageSasCredential; //!< Combined SAS URI and SAS Token for connecting to storage
} BlobStorageInfo;

bool FileUploadUtility_UploadFilesToContainer(
const BlobStorageInfo* blobInfo, VECTOR_HANDLE fileNames, const char* directoryPath);

EXTERN_C_END

#endif // FILE_UPLOAD_UTILITY_H
@@ -0,0 +1,99 @@
/**
* @file blob_storage_helper.cpp
* @brief Implements the interface for interacting with Azure Blob Storage
*
* @copyright Copyright (c) Microsoft Corp.
*/
#include "blob_storage_helper.hpp"

#include <aduc/exception_utils.hpp>
#include <azure_c_shared_utility/string_token.h>
#include <azure_c_shared_utility/urlencode.h>
#include <cstring>
#include <fstream>

/**
* @brief Creates the blob storage client using the information in @p blobInfo and then constructs the object
* @param blobInfo information related to the blob storage account
* @param maxConcurrency the maximum number of threads that can be working at one time for the client connection
*/
AzureBlobStorageHelper::AzureBlobStorageHelper(const BlobStorageInfo& blobInfo)
{
if (blobInfo.storageSasCredential == nullptr)
{
throw std::invalid_argument("Container name invalid");
}

client = std::make_unique<Azure::Storage::Blobs::BlobContainerClient>(STRING_c_str(blobInfo.storageSasCredential));
}

/**
* @brief strips the file name from the @p filePath
* @param fileName a properly formed name for the file who's path is being constructed- must include extension if the file has one
* @param directoryPath a properly formed path to the directory where the file exists
* @returns the filePath, or ""
*/
std::string
AzureBlobStorageHelper::CreatePathFromFileAndDirectory(const std::string& fileName, const std::string& directoryPath)
{
if (fileName.empty() || directoryPath.empty())
{
throw std::invalid_argument(__FUNCTION__);
}

bool needsSeperator = false;
if (directoryPath.find_last_of('/') != directoryPath.length() - 1)
{
needsSeperator = true;
}

if (needsSeperator)
{
return directoryPath + "/" + fileName;
}
return directoryPath + fileName;
}

/**
* @brief Uploads all the files listed in @p files using the storage account associated with this object
* @param fileNames vector of file names to upload
* @param directoryPath path to the directory where @p fileNames can be found
* @param virtualDirectory a properly formatted virtual directory (ending in '/') to be used when uploading the files
* @returns true on success; false on any failure
*/
bool AzureBlobStorageHelper::UploadFilesToContainer(
VECTOR_HANDLE fileNames, const std::string& directoryPath, const std::string& virtualDirectory)
{
if (VECTOR_size(fileNames) == 0)
{
throw std::invalid_argument("blobInfo.storageSasCredential");
}

std::string virtualDirectoryPath = virtualDirectory;
if (!virtualDirectoryPath.empty() && virtualDirectoryPath[virtualDirectoryPath.length() - 1] != '/')
{
virtualDirectoryPath += "/";
}

size_t fileNameSize = VECTOR_size(fileNames);
for (unsigned int i = 0; i < fileNameSize; ++i)
{
auto fileNameHandle = static_cast<const STRING_HANDLE*>(VECTOR_element(fileNames, i));
const char* fileName = STRING_c_str(*fileNameHandle);

ADUC::ExceptionUtils::CallVoidMethodAndHandleExceptions(
[fileName, directoryPath, virtualDirectoryPath, this]() -> void {
std::string filePath = CreatePathFromFileAndDirectory(fileName, directoryPath);

Azure::Core::IO::FileBodyStream fileStream(filePath);

std::string blobName = virtualDirectoryPath + fileName;

std::vector<std::pair<std::string, std::string>> metadata;

this->client->UploadBlob(blobName, fileStream);
});
}

return true;
}

0 comments on commit ead5b99

Please sign in to comment.