Skip to content

Commit

Permalink
Modifications made to the docker client (danielsuo#9)
Browse files Browse the repository at this point in the history
* Created inc director

code changes pending

* Added an empty example.c in example directory

* Create c_cpp_properties.json

* Modified gitignore to ignore .vscode and reoved .vscode.

* Removed C extern statement from the Header.

* Modified the project folder structure

Main CMake modified to add .deb package creation options.

* Update README.md

* Update README.md

As example directory is being added, removing the example from readme.md

* Added a compile script for compilation

Created the CMakeLists.txt for the source directory for building the .so for cdockerinterface

* deb package creation added

Example executable added

compile script added

* Reduced the CMake to one level.

Moved the compile to a scripts directory

simplified the naming of the .so and the executable

folder structure of the project simplified

* Update README.md

Changed the name of the .so and executable.

* cpp compatibility

Added the cpp compatibility which was removed earlier

* Update README.md

* Modified naming from cdocker to docker

1. In cmake project name

2. In .so name

3. .deb package name modified to docker_c_sdk

4. executable name changed to docker_example

* Update README.md

* Update README.md
  • Loading branch information
schaffung authored Apr 17, 2020
1 parent e968b6e commit f30a529
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 60 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

# IDE
.idea
.vscode

# CMake
cmake-build-debug
Expand Down
81 changes: 74 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,82 @@
# cmake version
cmake_minimum_required(VERSION 3.5)

#cmake policy
cmake_policy(SET CMP0015 NEW)

# Project name.
project(docker)

set(CMAKE_C_STANDARD 99)
set(DOCKER_INCLUDE_DIR src PARENT_SCOPE)
# Setting options.
option(EXAMPLE_BUILD "Build using examples" ON)

set( CMAKE_INSTALL_LIBDIR /usr/lib )
set( CMAKE_INSTALL_BINDIR /usr/bin )

# Set target name and source files
file (GLOB DOCKER_SOURCE_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} src/**)
find_package(CURL REQUIRED)

add_library(${PROJECT_NAME} ${DOCKER_SOURCE_FILES})

# Install docker.h to the /usr/include directory
install(FILES inc/docker.h PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE DESTINATION /usr/include)

# Including the headers to be used for building the SDK.
include_directories(${CMAKE_CURRENT_LIST_DIR}/inc)

# The source files.
SET(SRC_FILES
${CMAKE_CURRENT_LIST_DIR}/src/docker.c
)

# The cdocker will be created as a .so
add_library( docker SHARED ${SRC_FILES})

# Linking with dependencies.
target_link_libraries(
${PROJECT_NAME}
curl
docker

-lcurl
)

# Installing in the target file as a library.
INSTALL(
TARGETS docker
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
COMPONENT library
)

# If example build is enabled.
if(EXAMPLE_BUILD)
include_directories(${CMAKE_SOURCE_DIR}/inc)

SET( SRC_FILES
${CMAKE_CURRENT_LIST_DIR}/example/example.c
)

add_executable(docker_example ${SRC_FILES})

target_link_libraries(
docker_example
docker
)

install(TARGETS docker_example RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
endif(EXAMPLE_BUILD)

# Adding deb package build options.
set(CPACK_PACKAGE_NAME "docker_c_sdk")
set(CDOCKER_VERSION_MAJOR 1)
set(CDOCKER_VERSION_MINOR 0)
set(CDOCKER_VERSION_PATCH 0)

set(CPACK_PACKAGE_VERSION_MAJOR "${CDOCKER_VERSION_MAJOR}")
set(CPACK_PACKAGE_VERSION_MINOR "${CDOCKER_VERSION_MINOR}")
set(CPACK_PACKAGE_VERSION_PATCH "${CDOCKER_VERSION_PATCH}")

set(CPACK_DEBIAN_PACKAGE_SHIPBUILDS ON)

set(CPACK_GENERATOR "DEB")

set(CPACK_DEBIAN_ARCHITECTURE ${CMAKE_SYSTEM_PROCESSOR})
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "schaffung")

include(CPack)
63 changes: 10 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,60 +10,17 @@ A minimalist SDK for accessing the [Docker Engine API](https://docs.docker.com/e
- ```docker_buffer``` gets response data
- ```docker_destroy``` destroys docker object and frees any associated memory

The library is pretty low-level for now (i.e., doesn't support parsing ```JSON``` responses or have any functions higher-level than ```post``` or ```get```). An example application:

```
#include "docker.h"
int main() {
fprintf(stderr, "curl_version: %s\n", curl_version());
DOCKER *docker = docker_init("v1.25");
if (docker) {
CURLcode response = docker_post(docker, "http://v1.25/containers/create",
"{\"Image\": \"alpine\", \"Cmd\": [\"echo\", \"hello world\"]}");
if (response == CURLE_OK) {
fprintf(stderr, "%s\n", docker_buffer(docker));
}
response = docker_get(docker, "http://v1.25/images/json");
## Building the Code
- Run the compile script to build the code. By default the example will be part of the build process. ( If anybody wants to not build the example,
they can manually edit the base CMakeLists. In future, the compile script will accomodate the option.)
- If the build is successful, then a deb package will be created in the build directory. Use the command ```sudo dpkg -i <deb package name>```
to install the package.
- This will add libdocker.so to the /usr/lib directory and the example executable docker_example in /usr/bin
- Also this will add the docker.h header file to /usr/include. If anybody wants to just use the library for dev purpose, they can include it directly.
- Use the sudo command to run the executable created.
- To remove the package ( i.e. the .so file, the docker.h header and the example executable in future ), use the command ```sudo dpkg -r docker_c_sdk```

if (response == CURLE_OK) {
fprintf(stderr, "%s\n", docker_buffer(docker));
}
docker_destroy(docker);
} else {
fprintf(stderr, "ERROR: Failed to get get a docker client!\n");
}
return 0;
}
```

## Adding as CMake subdirectory dependency
Add the following lines to your ```CMakeLists.txt```.
```
# Let's say the libdocker directory is lib/libdocker
set(LIBDOCKER_DIR lib/libdocker)
# Add subdirectory to compile the static library
add_subdirectory(${LIBDOCKER_DIR})
# Make sure CMake knows where the header files are
target_include_directories(
${PROJECT_NAME} PUBLIC
${CMAKE_CURRENT_LIST_DIR}/${LIBDOCKER_DIR}/${DOCKER_INCLUDE_DIR}
)
# Link in the static library
target_link_libraries(
${PROJECT_NAME}
docker
)
```
The library is pretty low-level for now (i.e., doesn't support parsing ```JSON``` responses or have any functions higher-level than ```post``` or ```get```). An example application:

## Multi-threaded applications
Should be fine, but be sure to have a look at ```curl```'s page on the subject (link [here](https://curl.haxx.se/libcurl/c/threadsafe.html)).
27 changes: 27 additions & 0 deletions example/example.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <stdio.h>
#include "docker.h"


int main()
{
DOCKER *docker = docker_init("v1.25");
CURLcode response;

if (docker)
{
printf("The following are the Docker images present in the system.\n");
response = docker_get(docker, "http://v1.25/images/json");
if (response == CURLE_OK)
{
fprintf(stderr, "%s\n", docker_buffer(docker));
}

docker_destroy(docker);
}
else
{
fprintf(stderr, "ERROR: Failed to get get a docker client!\n");
}

return 0;
}
File renamed without changes.
16 changes: 16 additions & 0 deletions scripts/compile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

if [ -d ../build ]; then
rm -rf ../build
fi

mkdir ../build
cd ../build

cmake .. && cmake --build . && cpack

if [ $? -eq 0 ]; then
echo "Success"
else
echo "Failure"
fi

0 comments on commit f30a529

Please sign in to comment.