(feature): init

This commit is contained in:
Maksym Sadovnychyy 2024-08-17 22:11:46 +02:00
commit b37342cda1
58 changed files with 2067 additions and 0 deletions

63
.gitattributes vendored Normal file
View File

@ -0,0 +1,63 @@
###############################################################################
# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto
###############################################################################
# Set default behavior for command prompt diff.
#
# This is need for earlier builds of msysgit that does not have it on by
# default for csharp files.
# Note: This is only used by command line
###############################################################################
#*.cs diff=csharp
###############################################################################
# Set the merge driver for project and solution files
#
# Merging from the command prompt will add diff markers to the files if there
# are conflicts (Merging from VS is not affected by the settings below, in VS
# the diff markers are never inserted). Diff markers may cause the following
# file extensions to fail to load in VS. An alternative would be to treat
# these files as binary and thus will always conflict and require user
# intervention with every merge. To do so, just uncomment the entries below
###############################################################################
#*.sln merge=binary
#*.csproj merge=binary
#*.vbproj merge=binary
#*.vcxproj merge=binary
#*.vcproj merge=binary
#*.dbproj merge=binary
#*.fsproj merge=binary
#*.lsproj merge=binary
#*.wixproj merge=binary
#*.modelproj merge=binary
#*.sqlproj merge=binary
#*.wwaproj merge=binary
###############################################################################
# behavior for image files
#
# image files are treated as binary by default.
###############################################################################
#*.jpg binary
#*.png binary
#*.gif binary
###############################################################################
# diff behavior for common document formats
#
# Convert binary document formats to text before diffing them. This feature
# is only available from the command line. Turn it on by uncommenting the
# entries below.
###############################################################################
#*.doc diff=astextplain
#*.DOC diff=astextplain
#*.docx diff=astextplain
#*.DOCX diff=astextplain
#*.dot diff=astextplain
#*.DOT diff=astextplain
#*.pdf diff=astextplain
#*.PDF diff=astextplain
#*.rtf diff=astextplain
#*.RTF diff=astextplain

266
.gitignore vendored Normal file
View File

@ -0,0 +1,266 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/
# Visual Studio 2015 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
# NUNIT
*.VisualState.xml
TestResult.xml
# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c
# DNX
project.lock.json
project.fragment.lock.json
artifacts/
*_i.c
*_p.c
*_i.h
*.ilk
*.meta
*.obj
*.pch
*.pdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc
# Chutzpah Test files
_Chutzpah*
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb
# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap
# TFS 2012 Local Workspace
$tf/
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# JustCode is a .NET coding add-in
.JustCode
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
# MightyMoose
*.mm.*
AutoTest.Net/
# Web workbench (sass)
.sass-cache/
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# TODO: Comment the next line if you want to checkin your web deploy settings
# but database connection strings (with potential passwords) will be unencrypted
#*.pubxml
*.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/
# NuGet Packages
*.nupkg
# The packages folder can be ignored because of Package Restore
**/packages/*
# except build/, which is used as an MSBuild target.
!**/packages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/packages/repositories.config
# NuGet v3's project.json files produces more ignoreable files
*.nuget.props
*.nuget.targets
# Microsoft Azure Build Output
csx/
*.build.csdef
# Microsoft Azure Emulator
ecf/
rcf/
# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!*.[Cc]ache/
# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.jfm
*.pfx
*.publishsettings
node_modules/
orleans.codegen.cs
.directory
# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
# SQL Server files
*.mdf
*.ldf
# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
# Microsoft Fakes
FakesAssemblies/
# GhostDoc plugin setting file
*.GhostDoc.xml
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
# Visual Studio 6 build log
*.plg
# Visual Studio 6 workspace options file
*.opt
# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions
# Paket dependency manager
.paket/paket.exe
paket-files/
# FAKE - F# Make
.fake/
# JetBrains Rider
.idea/
*.sln.iml
# CodeRush
.cr/
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
**/*docker-compose/LetsEncryptServer/acme
**/*docker-compose/LetsEncryptServer/cache

21
LICENSE.md Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2024 Maksym Sadovnychyy (MAKS-IT)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# Podman Client DotNet
Podman API .NET cilent implementation based on podman latest [documentation](https://docs.podman.io/en/latest/)

View File

@ -0,0 +1,36 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace MaksIT.PodmanClientDotNet.Extensions;
public static class ObjectExtensions {
/// <summary>
/// Converts object to json string
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static string ToJson<T>(this T? obj) => ToJson(obj, null);
/// <summary>
/// Converts object to json string
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <param name="converters"></param>
/// <returns></returns>
public static string ToJson<T>(T? obj, List<JsonConverter>? converters) {
if (obj == null)
return "{}";
var options = new JsonSerializerOptions {
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
};
converters?.ForEach(x => options.Converters.Add(x));
return JsonSerializer.Serialize(obj, options);
}
}

View File

@ -0,0 +1,35 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace MaksIT.PodmanClientDotNet.Extensions;
public static partial class StringExtensions {
/// <summary>
/// Converts JSON string to object
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="s"></param>
/// <returns></returns>
public static T? ToObject<T>(this string s) => ToObjectCore<T>(s, null);
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="s"></param>
/// <param name="converters"></param>
/// <returns></returns>
public static T? ToObject<T>(this string s, List<JsonConverter> converters) => ToObjectCore<T>(s, converters);
private static T? ToObjectCore<T>(string s, List<JsonConverter>? converters) {
var options = new JsonSerializerOptions {
PropertyNameCaseInsensitive = true
};
converters?.ForEach(x => options.Converters.Add(x));
return JsonSerializer.Deserialize<T>(s, options);
}
}

View File

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class AutoUserNsOptions {
public List<IDMapping> AdditionalGIDMappings { get; set; }
public List<IDMapping> AdditionalUIDMappings { get; set; }
public string GroupFile { get; set; }
public int InitialSize { get; set; }
public string PasswdFile { get; set; }
public int Size { get; set; }
}
}

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class BindOptions {
public bool CreateMountpoint { get; set; }
public bool NonRecursive { get; set; }
public string Propagation { get; set; }
public bool ReadOnlyForceRecursive { get; set; }
public bool ReadOnlyNonRecursive { get; set; }
}
}

View File

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class BlockIO {
public int LeafWeight { get; set; }
public List<ThrottleDevice> ThrottleReadBpsDevice { get; set; }
public List<ThrottleDevice> ThrottleReadIopsDevice { get; set; }
public List<ThrottleDevice> ThrottleWriteBpsDevice { get; set; }
public List<ThrottleDevice> ThrottleWriteIopsDevice { get; set; }
public int Weight { get; set; }
public List<WeightDevice> WeightDevice { get; set; }
}
}

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class CPU {
public int Burst { get; set; }
public string Cpus { get; set; }
public int Idle { get; set; }
public string Mems { get; set; }
public int Period { get; set; }
public int Quota { get; set; }
public int RealtimePeriod { get; set; }
public int RealtimeRuntime { get; set; }
public int Shares { get; set; }
}
}

View File

@ -0,0 +1,129 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models.Container {
public class CreateContainerRequest {
public Dictionary<string, string> Annotations { get; set; }
public string ApparmorProfile { get; set; }
public string BaseHostsFile { get; set; }
public List<string> CapAdd { get; set; }
public List<string> CapDrop { get; set; }
public string CgroupParent { get; set; }
public Namespace Cgroupns { get; set; }
public string CgroupsMode { get; set; }
public List<string> ChrootDirectories { get; set; }
public List<string> CNINetworks { get; set; }
public List<string> Command { get; set; }
public string ConmonPidFile { get; set; }
public List<string> ContainerCreateCommand { get; set; }
public bool? CreateWorkingDir { get; set; }
public List<string> DependencyContainers { get; set; }
public List<LinuxDeviceCgroup> DeviceCgroupRule { get; set; }
public List<LinuxDevice> Devices { get; set; }
public List<string> DevicesFrom { get; set; }
public List<string> DNSOption { get; set; }
public List<string> DNSSearch { get; set; }
public List<string> DNSServer { get; set; }
public List<string> Entrypoint { get; set; }
public Dictionary<string, string> Env { get; set; }
public bool? EnvHost { get; set; }
public List<string> EnvMerge { get; set; }
public Dictionary<ushort, string> Expose { get; set; }
public string GroupEntry { get; set; }
public List<string> Groups { get; set; }
public long? HealthCheckOnFailureAction { get; set; }
public Schema2HealthConfig HealthConfig { get; set; }
public List<LinuxDevice> HostDeviceList { get; set; }
public List<string> HostAdd { get; set; }
public string Hostname { get; set; }
public List<string> HostUsers { get; set; }
public bool? EnvHTTPProxy { get; set; }
public IDMappingOptions IDMappings { get; set; }
public string Image { get; set; }
public string ImageArch { get; set; }
public string ImageOS { get; set; }
public string ImageVariant { get; set; }
public string ImageVolumeMode { get; set; }
public List<ImageVolume> ImageVolumes { get; set; }
public bool? Init { get; set; }
public string InitContainerType { get; set; }
public string InitPath { get; set; }
public LinuxIntelRdt IntelRdt { get; set; }
public Namespace Ipcns { get; set; }
public bool? LabelNested { get; set; }
public Dictionary<string, string> Labels { get; set; }
public LogConfigLibpod LogConfiguration { get; set; }
public bool? ManagePassword { get; set; }
public List<string> Mask { get; set; }
public List<Mount> Mounts { get; set; }
public string Name { get; set; }
public Namespace Netns { get; set; }
public Dictionary<string, string> NetworkOptions { get; set; }
public Dictionary<string, NetworkSettings> Networks { get; set; }
public bool? NoNewPrivileges { get; set; }
public string OciRuntime { get; set; }
public long? OomScoreAdj { get; set; }
public List<OverlayVolume> OverlayVolumes { get; set; }
public string PasswdEntry { get; set; }
public LinuxPersonality Personality { get; set; }
public Namespace Pidns { get; set; }
public string Pod { get; set; }
public List<PortMapping> Portmappings { get; set; }
public bool? Privileged { get; set; }
public List<string> ProcfsOpts { get; set; }
public bool? PublishImagePorts { get; set; }
public List<POSIXRlimit> RLimits { get; set; }
public string RawImageName { get; set; }
public bool? ReadOnlyFilesystem { get; set; }
public bool? ReadWriteTmpfs { get; set; }
public bool? Remove { get; set; }
public LinuxResources ResourceLimits { get; set; }
public string RestartPolicy { get; set; }
public ulong? RestartTries { get; set; }
public string Rootfs { get; set; }
public string RootfsMapping { get; set; }
public bool? RootfsOverlay { get; set; }
public string RootfsPropagation { get; set; }
public string SdnotifyMode { get; set; }
public string SeccompPolicy { get; set; }
public string SeccompProfilePath { get; set; }
public Dictionary<string, string> SecretEnv { get; set; }
public List<SecretProp> Secrets { get; set; }
public List<string> SelinuxOpts { get; set; }
public long? ShmSize { get; set; }
public long? ShmSizeSystemd { get; set; }
public StartupHealthConfig StartupHealthConfig { get; set; }
public bool? Stdin { get; set; }
public long? StopSignal { get; set; }
public ulong? StopTimeout { get; set; }
public Dictionary<string, string> StorageOpts { get; set; }
public Dictionary<string, string> Sysctl { get; set; }
public string Systemd { get; set; }
public bool? Terminal { get; set; }
public Dictionary<string, ulong> ThrottleReadBpsDevice { get; set; }
public Dictionary<string, ulong> ThrottleReadIopsDevice { get; set; }
public Dictionary<string, ulong> ThrottleWriteBpsDevice { get; set; }
public Dictionary<string, ulong> ThrottleWriteIopsDevice { get; set; }
public ulong? Timeout { get; set; }
public string Timezone { get; set; }
public string Umask { get; set; }
public Dictionary<string, string> Unified { get; set; }
public List<string> Unmask { get; set; }
public List<string> Unsetenv { get; set; }
public bool? Unsetenvall { get; set; }
public bool? UseImageHosts { get; set; }
public bool? UseImageResolvConf { get; set; }
public string User { get; set; }
public Namespace Userns { get; set; }
public Namespace Utsns { get; set; }
public bool? Volatile { get; set; }
public List<NamedVolume> Volumes { get; set; }
public List<string> VolumesFrom { get; set; }
public Dictionary<string, ulong> WeightDevice { get; set; }
public string WorkDir { get; set; }
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models.Container {
public class CreateContainerResponse {
public string Id { get; set; }
public string[] Warnings { get; set; }
}
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models.Container {
public class DeleteContainerResponse {
public string Err { get; set; }
public string Id { get; set; }
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class DriverConfig {
public string Name { get; set; }
public Dictionary<string, string> Options { get; set; }
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class ErrorResponse {
public string Cause { get; set; }
public string Message { get; set; }
public int Response { get; set; }
}
}

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models.Exec
{
public class CreateExecRequest
{
public bool AttachStderr { get; set; }
public bool AttachStdin { get; set; }
public bool AttachStdout { get; set; }
public string[] Cmd { get; set; }
public string DetachKeys { get; set; }
public string[] Env { get; set; }
public bool Privileged { get; set; }
public bool Tty { get; set; }
public string User { get; set; }
public string WorkingDir { get; set; }
}
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models.Exec {
public class CreateExecResponse {
public string Id { get; set; } // The ID of the exec instance
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models.Exec {
public class InspectExecResponse {
public bool Running { get; set; }
public int ExitCode { get; set; }
public string ProcessConfig { get; set; } // Additional fields can be added based on your needs
}
}

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models.Exec {
public class StartExecRequest {
public bool Detach { get; set; }
public bool Tty { get; set; }
public int? Height { get; set; } // Optional, nullable if not provided
public int? Width { get; set; } // Optional, nullable if not provided
}
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class HugepageLimit {
public long Limit { get; set; }
public string PageSize { get; set; }
}
}

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class IDMapping {
public int ContainerId { get; set; }
public int HostId { get; set; }
public int Size { get; set; }
}
}

View File

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class IDMappingOptions {
public bool AutoUserNs { get; set; }
public AutoUserNsOptions AutoUserNsOpts { get; set; }
public List<IDMapping> GIDMap { get; set; }
public bool HostGIDMapping { get; set; }
public bool HostUIDMapping { get; set; }
public List<IDMapping> UIDMap { get; set; }
}
}

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models.Image {
public class ImagePullStatusResponse {
public string Status { get; set; }
public string Id { get; set; }
public string Progress { get; set; }
public ProgressDetail ProgressDetail { get; set; }
}
}

View File

@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models.Image {
public class ImagePullStreamResponse {
public string Stream { get; set; }
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class ImageVolume {
public string Destination { get; set; }
public bool ReadWrite { get; set; }
public string Source { get; set; }
public string SubPath { get; set; }
}
}

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class IntelRdt {
public string ClosId { get; set; }
public bool EnableCMT { get; set; }
public bool EnableMBM { get; set; }
public string L3CacheSchema { get; set; }
public string MemBwSchema { get; set; }
}
}

View File

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class LinuxDevice {
public int FileMode { get; set; }
public int Gid { get; set; }
public int Major { get; set; }
public int Minor { get; set; }
public string Path { get; set; }
public string Type { get; set; }
public int Uid { get; set; }
}
}

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class LinuxDeviceCgroup {
public string Access { get; set; }
public bool Allow { get; set; }
public int Major { get; set; }
public int Minor { get; set; }
public string Type { get; set; }
}
}

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class LinuxIntelRdt {
public string ClosID { get; set; }
public bool EnableCMT { get; set; }
public bool EnableMBM { get; set; }
public string L3CacheSchema { get; set; }
public string MemBwSchema { get; set; }
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class LinuxPersonality {
public string Domain { get; set; }
public List<string> Flags { get; set; }
}
}

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class LinuxResources {
public BlockIO BlockIO { get; set; }
public CPU CPU { get; set; }
public List<LinuxDeviceCgroup> Devices { get; set; }
public List<HugepageLimit> HugepageLimits { get; set; }
public Memory Memory { get; set; }
public Network Network { get; set; }
public Pids Pids { get; set; }
public Dictionary<string, RdmaResource> Rdma { get; set; }
public Dictionary<string, string> Unified { get; set; }
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class LogConfigLibpod {
public string Driver { get; set; }
public Dictionary<string, string> Options { get; set; }
public string Path { get; set; }
public long Size { get; set; }
}
}

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class Memory {
public bool CheckBeforeUpdate { get; set; }
public bool DisableOOMKiller { get; set; }
public long Kernel { get; set; }
public long KernelTCP { get; set; }
public long Limit { get; set; }
public long Reservation { get; set; }
public long Swap { get; set; }
public int Swappiness { get; set; }
public bool UseHierarchy { get; set; }
}
}

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class Mount {
public BindOptions BindOptions { get; set; }
public string Consistency { get; set; }
public bool ReadOnly { get; set; }
public string Source { get; set; }
public string Target { get; set; }
public TmpfsOptions TmpfsOptions { get; set; }
public string Type { get; set; }
public VolumeOptions VolumeOptions { get; set; }
}
}

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class NamedVolume {
public string Dest { get; set; }
public bool IsAnonymous { get; set; }
public string Name { get; set; }
public List<string> Options { get; set; }
public string SubPath { get; set; }
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class Namespace {
public string Nsmode { get; set; }
public string Value { get; set; }
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class Network {
public int ClassID { get; set; }
public List<NetworkPriority> Priorities { get; set; }
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class NetworkPriority {
public string Name { get; set; }
public int Priority { get; set; }
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class NetworkSettings {
public List<string> Aliases { get; set; }
public string InterfaceName { get; set; }
public List<string> StaticIps { get; set; }
public string StaticMac { get; set; }
}
}

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class OverlayVolume {
public string Destination { get; set; }
public List<string> Options { get; set; }
public string Source { get; set; }
}
}

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class POSIXRlimit {
public long Hard { get; set; }
public long Soft { get; set; }
public string Type { get; set; }
}
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class Pids {
public int Limit { get; set; }
}
}

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class PortMapping {
public int ContainerPort { get; set; }
public string HostIp { get; set; }
public int HostPort { get; set; }
public string Protocol { get; set; }
public int Range { get; set; }
}
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class ProgressDetail {
public long? Current { get; set; }
public long? Total { get; set; }
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class RdmaResource {
public int HcaHandles { get; set; }
public int HcaObjects { get; set; }
}
}

View File

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class Schema2HealthConfig {
public long Interval { get; set; }
public int Retries { get; set; }
public long StartInterval { get; set; }
public long StartPeriod { get; set; }
public List<string> Test { get; set; }
public long Timeout { get; set; }
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class SecretProp {
public string Key { get; set; }
public string Secret { get; set; }
}
}

View File

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class StartupHealthConfig {
public long Interval { get; set; }
public int Retries { get; set; }
public long StartInterval { get; set; }
public long StartPeriod { get; set; }
public int Successes { get; set; }
public List<string> Test { get; set; }
public long Timeout { get; set; }
}
}

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class ThrottleDevice {
public int Major { get; set; }
public int Minor { get; set; }
public long Rate { get; set; }
}
}

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class TmpfsOptions {
public int Mode { get; set; }
public List<string> Options { get; set; }
public long SizeBytes { get; set; }
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class VolumeOptions {
public DriverConfig DriverConfig { get; set; }
public Dictionary<string, string> Labels { get; set; }
public bool NoCopy { get; set; }
public string Subpath { get; set; }
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.PodmanClientDotNet.Models {
public class WeightDevice {
public int LeafWeight { get; set; }
public int Major { get; set; }
public int Minor { get; set; }
public int Weight { get; set; }
}
}

View File

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>MaksIT.$(MSBuildProjectName.Replace(" ", "_"))</RootNamespace>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,19 @@
namespace MaksIT.PodmanClientDotNet {
public partial class PodmanClient {
private readonly HttpClient _httpClient;
public PodmanClient(string baseAddress, int timeOut) {
_httpClient = new HttpClient();
_httpClient.BaseAddress = new Uri(baseAddress);
_httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
_httpClient.Timeout = TimeSpan.FromMinutes(timeOut);
}
public PodmanClient(HttpClient httpClient) {
_httpClient = httpClient;
_httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
_httpClient.Timeout = TimeSpan.FromMinutes(60);
}
}
}

View File

@ -0,0 +1,503 @@
using System.Text;
using System.Text.Json;
using MaksIT.PodmanClientDotNet.Extensions;
using MaksIT.PodmanClientDotNet.Models;
using MaksIT.PodmanClientDotNet.Models.Container;
namespace MaksIT.PodmanClientDotNet {
public partial class PodmanClient {
public async Task<CreateContainerResponse> CreateContainerAsync(
string name,
string image,
List<string> command = null,
Dictionary<string, string> env = null,
bool? remove = null,
bool? stdin = null,
bool? terminal = null,
List<Mount> mounts = null,
bool? privileged = null,
string hostname = null,
Namespace netns = null,
List<PortMapping> portMappings = null,
string restartPolicy = null,
ulong? stopTimeout = null,
List<string> capAdd = null,
List<string> capDrop = null,
List<string> dnsServers = null,
List<string> dnsSearch = null,
List<string> dnsOptions = null,
bool? publishImagePorts = null,
List<string> cniNetworks = null,
Dictionary<string, string> labels = null,
bool? readOnlyFilesystem = null,
List<POSIXRlimit> rLimits = null,
List<LinuxDevice> devices = null,
string ociRuntime = null,
string pod = null,
bool? noNewPrivileges = null,
string cgroupsMode = null,
Dictionary<string, string> storageOpts = null,
bool? unsetenvall = null,
Dictionary<string, string> secretEnv = null,
string timezone = null,
Dictionary<string, string> sysctl = null,
string seccompProfilePath = null,
string seccompPolicy = null,
Dictionary<string, string> annotations = null,
string apparmorProfile = null,
string baseHostsFile = null,
string cgroupParent = null,
Namespace cgroupns = null,
List<string> chrootDirectories = null,
string conmonPidFile = null,
List<string> containerCreateCommand = null,
bool? createWorkingDir = null,
List<string> dependencyContainers = null,
List<LinuxDeviceCgroup> deviceCgroupRule = null,
List<string> devicesFrom = null,
List<string> entrypoint = null,
bool? envHost = null,
List<string> envMerge = null,
Dictionary<ushort, string> expose = null,
string groupEntry = null,
List<string> groups = null,
long? healthCheckOnFailureAction = null,
Schema2HealthConfig healthConfig = null,
List<LinuxDevice> hostDeviceList = null,
List<string> hostAdd = null,
List<string> hostUsers = null,
bool? envHTTPProxy = null,
IDMappingOptions idMappings = null,
string imageArch = null,
string imageOS = null,
string imageVariant = null,
string imageVolumeMode = null,
List<ImageVolume> imageVolumes = null,
bool? init = null,
string initContainerType = null,
string initPath = null,
LinuxIntelRdt intelRdt = null,
Namespace ipcns = null,
bool? labelNested = null,
LogConfigLibpod logConfiguration = null,
bool? managePassword = null,
List<string> mask = null,
Dictionary<string, string> networkOptions = null,
Dictionary<string, NetworkSettings> networks = null,
long? oomScoreAdj = null,
List<OverlayVolume> overlayVolumes = null,
string passwdEntry = null,
LinuxPersonality personality = null,
Namespace pidns = null,
string rawImageName = null,
bool? readWriteTmpfs = null,
LinuxResources resourceLimits = null,
ulong? restartTries = null,
string rootfs = null,
string rootfsMapping = null,
bool? rootfsOverlay = null,
string rootfsPropagation = null,
string sdnotifyMode = null,
List<SecretProp> secrets = null,
List<string> selinuxOpts = null,
long? shmSize = null,
long? shmSizeSystemd = null,
StartupHealthConfig startupHealthConfig = null,
long? stopSignal = null,
string systemd = null,
Dictionary<string, ulong> throttleReadBpsDevice = null,
Dictionary<string, ulong> throttleReadIopsDevice = null,
Dictionary<string, ulong> throttleWriteBpsDevice = null,
Dictionary<string, ulong> throttleWriteIopsDevice = null,
ulong? timeout = null,
string umask = null,
Dictionary<string, string> unified = null,
List<string> unmask = null,
bool? useImageHosts = null,
bool? useImageResolvConf = null,
string user = null,
Namespace userns = null,
Namespace utsns = null,
bool? volatileFlag = null,
List<NamedVolume> volumes = null,
List<string> volumesFrom = null,
Dictionary<string, ulong> weightDevice = null,
string workDir = null
) {
var createContainerParameters = new CreateContainerRequest {
Name = name,
Image = image,
Command = command,
Env = env,
WorkDir = workDir,
Remove = remove,
Stdin = stdin,
Terminal = terminal,
Mounts = mounts,
Privileged = privileged,
Hostname = hostname,
Netns = netns,
Portmappings = portMappings,
RestartPolicy = restartPolicy,
StopTimeout = stopTimeout,
CapAdd = capAdd,
CapDrop = capDrop,
DNSServer = dnsServers,
DNSSearch = dnsSearch,
DNSOption = dnsOptions,
PublishImagePorts = publishImagePorts,
CNINetworks = cniNetworks,
Labels = labels,
ReadOnlyFilesystem = readOnlyFilesystem,
RLimits = rLimits,
Devices = devices,
OciRuntime = ociRuntime,
Pod = pod,
NoNewPrivileges = noNewPrivileges,
CgroupsMode = cgroupsMode,
StorageOpts = storageOpts,
Unmask = unmask,
Unsetenvall = unsetenvall,
SecretEnv = secretEnv,
Timezone = timezone,
Sysctl = sysctl,
SeccompProfilePath = seccompProfilePath,
SeccompPolicy = seccompPolicy,
Annotations = annotations,
ApparmorProfile = apparmorProfile,
BaseHostsFile = baseHostsFile,
CgroupParent = cgroupParent,
Cgroupns = cgroupns,
ChrootDirectories = chrootDirectories,
ConmonPidFile = conmonPidFile,
ContainerCreateCommand = containerCreateCommand,
CreateWorkingDir = createWorkingDir,
DependencyContainers = dependencyContainers,
DeviceCgroupRule = deviceCgroupRule,
DevicesFrom = devicesFrom,
Entrypoint = entrypoint,
EnvHost = envHost,
EnvMerge = envMerge,
Expose = expose,
GroupEntry = groupEntry,
Groups = groups,
HealthCheckOnFailureAction = healthCheckOnFailureAction,
HealthConfig = healthConfig,
HostDeviceList = hostDeviceList,
HostAdd = hostAdd,
HostUsers = hostUsers,
EnvHTTPProxy = envHTTPProxy,
IDMappings = idMappings,
ImageArch = imageArch,
ImageOS = imageOS,
ImageVariant = imageVariant,
ImageVolumeMode = imageVolumeMode,
ImageVolumes = imageVolumes,
Init = init,
InitContainerType = initContainerType,
InitPath = initPath,
IntelRdt = intelRdt,
Ipcns = ipcns,
LabelNested = labelNested,
LogConfiguration = logConfiguration,
ManagePassword = managePassword,
Mask = mask,
NetworkOptions = networkOptions,
Networks = networks,
OomScoreAdj = oomScoreAdj,
OverlayVolumes = overlayVolumes,
PasswdEntry = passwdEntry,
Personality = personality,
Pidns = pidns,
RawImageName = rawImageName,
ReadWriteTmpfs = readWriteTmpfs,
ResourceLimits = resourceLimits,
RestartTries = restartTries,
Rootfs = rootfs,
RootfsMapping = rootfsMapping,
RootfsOverlay = rootfsOverlay,
RootfsPropagation = rootfsPropagation,
SdnotifyMode = sdnotifyMode,
Secrets = secrets,
SelinuxOpts = selinuxOpts,
ShmSize = shmSize,
ShmSizeSystemd = shmSizeSystemd,
StartupHealthConfig = startupHealthConfig,
StopSignal = stopSignal,
Systemd = systemd,
ThrottleReadBpsDevice = throttleReadBpsDevice,
ThrottleReadIopsDevice = throttleReadIopsDevice,
ThrottleWriteBpsDevice = throttleWriteBpsDevice,
ThrottleWriteIopsDevice = throttleWriteIopsDevice,
Timeout = timeout,
Umask = umask,
Unified = unified,
UseImageHosts = useImageHosts,
UseImageResolvConf = useImageResolvConf,
User = user,
Userns = userns,
Utsns = utsns,
Volatile = volatileFlag,
Volumes = volumes,
VolumesFrom = volumesFrom,
WeightDevice = weightDevice
};
var jsonContent = new StringContent(JsonSerializer.Serialize(createContainerParameters), Encoding.UTF8, "application/json");
var response = await _httpClient.PostAsync("/v1.41/libpod/containers/create", jsonContent);
if (response.IsSuccessStatusCode) {
var jsonResponse = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<CreateContainerResponse>(jsonResponse);
}
else {
var errorContent = await response.Content.ReadAsStringAsync();
var errorDetails = JsonSerializer.Deserialize<ErrorResponse>(errorContent);
switch (response.StatusCode) {
case System.Net.HttpStatusCode.BadRequest:
Console.WriteLine($"Bad parameter in request: {errorDetails?.Message}");
break;
case System.Net.HttpStatusCode.NotFound:
Console.WriteLine($"No such container: {errorDetails?.Message}");
break;
case System.Net.HttpStatusCode.Conflict:
Console.WriteLine($"Conflict error in operation: {errorDetails?.Message}");
break;
case System.Net.HttpStatusCode.InternalServerError:
Console.WriteLine($"Internal server error: {errorDetails?.Message}");
break;
default:
Console.WriteLine($"Error creating container: {errorDetails?.Message}");
break;
}
response.EnsureSuccessStatusCode();
return null;
}
}
public async Task StartContainerAsync(string containerId, string detachKeys = "ctrl-p,ctrl-q") {
var response = await _httpClient.PostAsync(
$"/v1.41/libpod/containers/{containerId}/start?detachKeys={Uri.EscapeDataString(detachKeys)}", null);
switch (response.StatusCode) {
case System.Net.HttpStatusCode.NoContent:
Console.WriteLine("Container started successfully.");
break;
case System.Net.HttpStatusCode.NotModified:
Console.WriteLine("Container was already started.");
break;
case System.Net.HttpStatusCode.NotFound:
var errorContent404 = await response.Content.ReadAsStringAsync();
var errorDetails404 = errorContent404.ToObject<ErrorResponse>();
Console.WriteLine($"Container not found: {errorDetails404?.Message}");
break;
case System.Net.HttpStatusCode.InternalServerError:
var errorContent500 = await response.Content.ReadAsStringAsync();
var errorDetails500 = errorContent500.ToObject<ErrorResponse>();
Console.WriteLine($"Internal server error: {errorDetails500?.Message}");
break;
default:
if ((int)response.StatusCode >= 400) {
var errorContent = await response.Content.ReadAsStringAsync();
var errorDetails = errorContent.ToObject<ErrorResponse>();
Console.WriteLine($"Error starting container: {errorDetails?.Message}");
}
break;
}
response.EnsureSuccessStatusCode();
}
public async Task StopContainerAsync(string containerId, int timeout = 10, bool ignoreAlreadyStopped = false) {
var queryParams = $"?timeout={timeout}&Ignore={ignoreAlreadyStopped.ToString().ToLower()}";
var response = await _httpClient.PostAsync($"/v1.41/libpod/containers/{containerId}/stop{queryParams}", null);
if (response.IsSuccessStatusCode) {
if (response.StatusCode == System.Net.HttpStatusCode.NoContent) {
Console.WriteLine("Container stopped successfully.");
}
else if (response.StatusCode == System.Net.HttpStatusCode.NotModified) {
Console.WriteLine("Container was already stopped.");
}
}
else {
var errorContent = await response.Content.ReadAsStringAsync();
var errorDetails = JsonSerializer.Deserialize<ErrorResponse>(errorContent);
switch (response.StatusCode) {
case System.Net.HttpStatusCode.NotFound:
Console.WriteLine($"No such container: {errorDetails?.Message}");
break;
case System.Net.HttpStatusCode.InternalServerError:
Console.WriteLine($"Internal server error: {errorDetails?.Message}");
break;
default:
Console.WriteLine($"Error stopping container: {errorDetails?.Message}");
break;
}
response.EnsureSuccessStatusCode();
}
}
public async Task ForceDeleteContainerAsync(string containerId, bool deleteVolumes = false, int timeout = 10) {
var queryParams = $"?force=true&v={deleteVolumes.ToString().ToLower()}&timeout={timeout}";
var response = await _httpClient.DeleteAsync($"/v1.41/libpod/containers/{containerId}{queryParams}");
if (response.IsSuccessStatusCode) {
if (response.StatusCode == System.Net.HttpStatusCode.NoContent) {
Console.WriteLine("Container force deleted successfully.");
}
else if (response.StatusCode == System.Net.HttpStatusCode.OK) {
var responseContent = await response.Content.ReadAsStringAsync();
var deleteResponses = JsonSerializer.Deserialize<DeleteContainerResponse[]>(responseContent);
foreach (var deleteResponse in deleteResponses) {
if (string.IsNullOrEmpty(deleteResponse.Err)) {
Console.WriteLine($"Container {deleteResponse.Id} deleted successfully.");
}
else {
Console.WriteLine($"Error deleting container {deleteResponse.Id}: {deleteResponse.Err}");
}
}
}
}
else {
var errorContent = await response.Content.ReadAsStringAsync();
var errorDetails = JsonSerializer.Deserialize<ErrorResponse>(errorContent);
switch (response.StatusCode) {
case System.Net.HttpStatusCode.BadRequest:
Console.WriteLine($"Bad parameter in request: {errorDetails?.Message}");
break;
case System.Net.HttpStatusCode.NotFound:
Console.WriteLine($"No such container: {errorDetails?.Message}");
break;
case System.Net.HttpStatusCode.Conflict:
Console.WriteLine($"Conflict error: {errorDetails?.Message}");
break;
case System.Net.HttpStatusCode.InternalServerError:
Console.WriteLine($"Internal server error: {errorDetails?.Message}");
break;
default:
Console.WriteLine($"Error deleting container: {errorDetails?.Message}");
break;
}
response.EnsureSuccessStatusCode();
}
}
public async Task DeleteContainerAsync(string containerId, bool depend = false, bool ignore = false, int timeout = 10) {
var queryParams = $"?depend={depend.ToString().ToLower()}&ignore={ignore.ToString().ToLower()}&timeout={timeout}";
var response = await _httpClient.DeleteAsync($"/v1.41/libpod/containers/{containerId}{queryParams}");
if (response.IsSuccessStatusCode) {
if (response.StatusCode == System.Net.HttpStatusCode.NoContent) {
Console.WriteLine("Container deleted successfully.");
}
else if (response.StatusCode == System.Net.HttpStatusCode.OK) {
var responseContent = await response.Content.ReadAsStringAsync();
var deleteResponses = JsonSerializer.Deserialize<DeleteContainerResponse[]>(responseContent);
foreach (var deleteResponse in deleteResponses) {
if (string.IsNullOrEmpty(deleteResponse.Err)) {
Console.WriteLine($"Container {deleteResponse.Id} deleted successfully.");
}
else {
Console.WriteLine($"Error deleting container {deleteResponse.Id}: {deleteResponse.Err}");
}
}
}
}
else {
var errorContent = await response.Content.ReadAsStringAsync();
var errorDetails = JsonSerializer.Deserialize<ErrorResponse>(errorContent);
switch (response.StatusCode) {
case System.Net.HttpStatusCode.BadRequest:
Console.WriteLine($"Bad parameter in request: {errorDetails?.Message}");
break;
case System.Net.HttpStatusCode.NotFound:
Console.WriteLine($"No such container: {errorDetails?.Message}");
break;
case System.Net.HttpStatusCode.Conflict:
Console.WriteLine($"Conflict error: {errorDetails?.Message}");
break;
case System.Net.HttpStatusCode.InternalServerError:
Console.WriteLine($"Internal server error: {errorDetails?.Message}");
break;
default:
Console.WriteLine($"Error deleting container: {errorDetails?.Message}");
break;
}
response.EnsureSuccessStatusCode(); // Throws an exception if the response indicates an error
}
}
public async Task ExtractArchiveToContainerAsync(string containerId, Stream tarStream, string path, bool pause = true) {
var content = new StreamContent(tarStream);
content.Headers.Add("Content-Type", "application/x-tar");
var queryParams = $"?path={Uri.EscapeDataString(path)}&pause={pause.ToString().ToLower()}";
var response = await _httpClient.PutAsync($"/v1.41/libpod/containers/{containerId}/archive{queryParams}", content);
if (response.IsSuccessStatusCode) {
Console.WriteLine("Files copied successfully to the container.");
}
else {
var errorContent = await response.Content.ReadAsStringAsync();
var errorDetails = JsonSerializer.Deserialize<ErrorResponse>(errorContent);
switch (response.StatusCode) {
case System.Net.HttpStatusCode.BadRequest:
Console.WriteLine($"Bad parameter in request: {errorDetails?.Message}");
break;
case System.Net.HttpStatusCode.Forbidden:
Console.WriteLine($"The container root filesystem is read-only: {errorDetails?.Message}");
break;
case System.Net.HttpStatusCode.NotFound:
Console.WriteLine($"No such container: {errorDetails?.Message}");
break;
case System.Net.HttpStatusCode.InternalServerError:
Console.WriteLine($"Internal server error: {errorDetails?.Message}");
break;
default:
Console.WriteLine($"Error copying files: {errorDetails?.Message}");
break;
}
response.EnsureSuccessStatusCode();
}
}
}
}

View File

@ -0,0 +1,170 @@
using System.Text;
using System.Text.Json;
using MaksIT.PodmanClientDotNet.Extensions;
using MaksIT.PodmanClientDotNet.Models.Exec;
using MaksIT.PodmanClientDotNet.Models;
namespace MaksIT.PodmanClientDotNet {
public partial class PodmanClient {
public async Task<CreateExecResponse> CreateExecAsync(
string containerName,
string[] cmd,
bool attachStderr = true,
bool attachStdin = false,
bool attachStdout = true,
string detachKeys = null,
string[] env = null,
bool privileged = false,
bool tty = false,
string user = null,
string workingDir = null
) {
// Construct the request object
var execRequest = new CreateExecRequest {
AttachStderr = attachStderr,
AttachStdin = attachStdin,
AttachStdout = attachStdout,
Cmd = cmd,
DetachKeys = detachKeys,
Env = env,
Privileged = privileged,
Tty = tty,
User = user,
WorkingDir = workingDir
};
// Serialize the request object to JSON
var jsonRequest = execRequest.ToJson();
var content = new StringContent(jsonRequest, Encoding.UTF8, "application/json");
// Create the request URL
var requestUrl = $"/containers/{Uri.EscapeDataString(containerName)}/exec";
// Send the POST request
var response = await _httpClient.PostAsync(requestUrl, content);
if (response.IsSuccessStatusCode) {
var jsonResponse = await response.Content.ReadAsStringAsync();
return jsonResponse.ToObject<CreateExecResponse>();
}
else {
var jsonResponse = await response.Content.ReadAsStringAsync();
var errorResponse = JsonSerializer.Deserialize<ErrorResponse>(jsonResponse);
// Handle different response codes
switch (response.StatusCode) {
case System.Net.HttpStatusCode.NotFound:
Console.WriteLine($"No such container: {errorResponse?.Message}");
break;
case System.Net.HttpStatusCode.Conflict:
Console.WriteLine($"Conflict error: {errorResponse?.Message}");
break;
case System.Net.HttpStatusCode.InternalServerError:
Console.WriteLine($"Internal server error: {errorResponse?.Message}");
break;
default:
Console.WriteLine($"Error creating exec instance: {errorResponse?.Message}");
break;
}
response.EnsureSuccessStatusCode();
return null;
}
}
public async Task StartExecAsync(
string execId,
bool detach = false,
bool tty = false,
int? height = null,
int? width = null,
string outputFilePath = "exec_output.log"
) {
outputFilePath = Path.Combine(Path.GetTempPath(), outputFilePath);
// Construct the request object
var startExecRequest = new StartExecRequest {
Detach = detach,
Tty = tty,
Height = height,
Width = width
};
// Serialize the request object to JSON
var jsonRequest = JsonSerializer.Serialize(startExecRequest);
var content = new StringContent(jsonRequest, Encoding.UTF8, "application/json");
// Create the request URL
var requestUrl = $"/exec/{Uri.EscapeDataString(execId)}/start";
// Send the POST request
var response = await _httpClient.PostAsync(requestUrl, content);
if (response.IsSuccessStatusCode) {
// Write the response stream directly to a file
using (var responseStream = await response.Content.ReadAsStreamAsync())
using (var fileStream = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write)) {
await responseStream.CopyToAsync(fileStream);
}
var test = File.ReadAllText(outputFilePath);
Console.WriteLine($"Exec instance started and output written to {outputFilePath}");
}
else {
var jsonResponse = await response.Content.ReadAsStringAsync();
var errorResponse = JsonSerializer.Deserialize<ErrorResponse>(jsonResponse);
// Handle different response codes
switch (response.StatusCode) {
case System.Net.HttpStatusCode.NotFound:
Console.WriteLine($"No such exec instance: {errorResponse?.Message}");
break;
case System.Net.HttpStatusCode.Conflict:
Console.WriteLine($"Conflict error: {errorResponse?.Message}");
break;
case System.Net.HttpStatusCode.InternalServerError:
Console.WriteLine($"Internal server error: {errorResponse?.Message}");
break;
default:
Console.WriteLine($"Error starting exec instance: {errorResponse?.Message}");
break;
}
response.EnsureSuccessStatusCode(); // Throws an exception if the response indicates an error
}
}
public async Task<InspectExecResponse?> InspectExecAsync(string execId) {
var requestUrl = $"/exec/{Uri.EscapeDataString(execId)}/json";
var response = await _httpClient.GetAsync(requestUrl);
if (response.IsSuccessStatusCode) {
var jsonResponse = await response.Content.ReadAsStringAsync();
return jsonResponse.ToObject<InspectExecResponse>();
}
else {
var jsonResponse = await response.Content.ReadAsStringAsync();
var errorResponse = JsonSerializer.Deserialize<ErrorResponse>(jsonResponse);
switch (response.StatusCode) {
case System.Net.HttpStatusCode.NotFound:
Console.WriteLine($"No such exec instance: {errorResponse?.Message}");
return null;
case System.Net.HttpStatusCode.InternalServerError:
Console.WriteLine($"Internal server error: {errorResponse?.Message}");
break;
default:
Console.WriteLine($"Error inspecting exec instance: {errorResponse?.Message}");
break;
}
return null;
}
}
}
}

View File

@ -0,0 +1,107 @@
using System.Text.Json;
using MaksIT.PodmanClientDotNet.Models.Image;
using MaksIT.PodmanClientDotNet.Models;
namespace MaksIT.PodmanClientDotNet {
public partial class PodmanClient {
public async Task<List<ImagePullStatusResponse>> PullImageAsync(string reference, bool tlsVerify = true, bool quiet = false, string policy = "always", string arch = null, string os = null, string variant = null, bool allTags = false, string authHeader = null) {
var query = $"reference={Uri.EscapeDataString(reference)}&tlsVerify={tlsVerify}&quiet={quiet}&policy={Uri.EscapeDataString(policy)}";
if (!string.IsNullOrEmpty(arch)) query += $"&Arch={Uri.EscapeDataString(arch)}";
if (!string.IsNullOrEmpty(os)) query += $"&OS={Uri.EscapeDataString(os)}";
if (!string.IsNullOrEmpty(variant)) query += $"&Variant={Uri.EscapeDataString(variant)}";
if (allTags) query += "&allTags=true";
if (!string.IsNullOrEmpty(authHeader)) {
_httpClient.DefaultRequestHeaders.Add("X-Registry-Auth", authHeader);
}
var response = await _httpClient.PostAsync($"/v1.41/libpod/images/pull?{query}", null);
var imagePullResponses = new List<ImagePullStatusResponse>();
if (response.IsSuccessStatusCode) {
using var responseStream = await response.Content.ReadAsStreamAsync();
using var reader = new StreamReader(responseStream);
string line;
while ((line = await reader.ReadLineAsync()) != null) {
if (line.Contains("\"status\"")) {
// The line contains status information
var statusResponse = JsonSerializer.Deserialize<ImagePullStatusResponse>(line);
Console.WriteLine($"Status: {statusResponse.Status}");
}
else if (line.Contains("\"id\"") || line.Contains("\"images\"")) {
// The line contains image ID information
var imageResponse = JsonSerializer.Deserialize<ImagePullStatusResponse>(line);
if (imageResponse != null) {
imagePullResponses.Add(imageResponse);
}
}
}
return imagePullResponses;
}
else {
var errorContent = await response.Content.ReadAsStringAsync();
var errorDetails = JsonSerializer.Deserialize<ErrorResponse>(errorContent);
switch (response.StatusCode) {
case System.Net.HttpStatusCode.BadRequest:
Console.WriteLine($"Bad request: {errorDetails?.Message}");
break;
case System.Net.HttpStatusCode.InternalServerError:
Console.WriteLine($"Internal server error: {errorDetails?.Message}");
break;
default:
Console.WriteLine($"Error pulling image: {errorDetails?.Message}");
break;
}
response.EnsureSuccessStatusCode();
return null;
}
}
public async Task TagImageAsync(string image, string repo, string tag) {
var response = await _httpClient.PostAsync($"/v1.41/libpod/images/{image}/tag?repo={Uri.EscapeDataString(repo)}&tag={Uri.EscapeDataString(tag)}", null);
if (response.IsSuccessStatusCode) {
if (response.StatusCode == System.Net.HttpStatusCode.Created) {
Console.WriteLine("Image tagged successfully.");
}
}
else {
var errorContent = await response.Content.ReadAsStringAsync();
var errorDetails = JsonSerializer.Deserialize<ErrorResponse>(errorContent);
switch (response.StatusCode) {
case System.Net.HttpStatusCode.BadRequest:
Console.WriteLine($"Bad parameter in request: {errorDetails?.Message}");
break;
case System.Net.HttpStatusCode.NotFound:
Console.WriteLine($"No such image: {errorDetails?.Message}");
break;
case System.Net.HttpStatusCode.Conflict:
Console.WriteLine($"Conflict error in operation: {errorDetails?.Message}");
break;
case System.Net.HttpStatusCode.InternalServerError:
Console.WriteLine($"Internal server error: {errorDetails?.Message}");
break;
default:
Console.WriteLine($"Error tagging image: {errorDetails?.Message}");
break;
}
response.EnsureSuccessStatusCode();
}
}
}
}

View File

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34902.65
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PodmanClientDotNet", "PodmanClient\PodmanClientDotNet.csproj", "{0833C90F-6BF3-40E4-A035-B6D6C81DB9D7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0833C90F-6BF3-40E4-A035-B6D6C81DB9D7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0833C90F-6BF3-40E4-A035-B6D6C81DB9D7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0833C90F-6BF3-40E4-A035-B6D6C81DB9D7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0833C90F-6BF3-40E4-A035-B6D6C81DB9D7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {CFC10A81-CC42-4585-B549-1C2214AC18DA}
EndGlobalSection
EndGlobal