Skip to content

Commit

Permalink
Merge pull request #1 from Mrpye/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Mrpye authored Mar 25, 2023
2 parents bce9380 + 0f06e37 commit 49a099d
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 7 deletions.
35 changes: 29 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Check out the CLI documentation [here](./documents/cimpex.md)
Export and image fromm a registry with no authentication

``` bash
curl --location --request POST 'http://localhost:9020/export' \
curl --location --request POST 'http://localhost:8080/export' \
--header 'Content-Type: application/json' \
--data-raw '{
"target":"cimpex:v1.0.0",
Expand All @@ -105,11 +105,11 @@ curl --location --request POST 'http://localhost:9020/export' \
Export from a registry with authentication
```bash
curl --location --request POST 'http://172.16.10.237:9020/export' \
curl --location --request POST 'http://localhost:8080/export' \
--header 'Authorization: Basic YWRtaW46cGFzc3dvcmQ=' \
--header 'Content-Type: application/json' \
--data-raw '{
"target":"172.19.2.152/library/cimpex:v1.0.0",
"target":"127.0.0.1/library/cimpex:v1.0.0",
"tar":"cimpex-v1-0-0.tar",
"ignore_ssl":true
}
Expand All @@ -121,7 +121,7 @@ curl --location --request POST 'http://172.16.10.237:9020/export' \

## Export images from a registry
``` bash
curl --location --request POST 'http://localhost:9020/exports' \
curl --location --request POST 'http://localhost:8080/exports' \
--header 'Content-Type: application/json' \
--data-raw '[{
"target":"cimpex:v1.0.0",
Expand Down Expand Up @@ -174,12 +174,35 @@ curl --location --request POST 'localhost:8080/import' \
- ignore_ssl (Ignore ssl cert)
</details>
<details>
<summary>Import images to a registry from the BASE_FOLDER
folder</summary>
Import an images and imports to the target registry
```bash
curl --location 'localhost:8080/imports' \
--header 'Authorization: Basic YWRtaW46cGFzc3dvcmQ=' \
--header 'Content-Type: application/json' \
--data '{
"target":"127.0.0.1/library/",
"ignore_ssl":true
}'
```
## Json Payload
- target (the docker registry)
- ignore_ssl (Ignore ssl cert)
</details>
<details>
<summary>List images in the folder</summary>
```bash
curl --location --request POST 'localhost:8080/list' \
curl --location --request POST ''http://localhost:8080/list' \
--header 'Content-Type: application/json'
```
</details>
Expand All @@ -189,7 +212,7 @@ curl --location --request POST 'localhost:8080/list' \
<summary>Test is alive</summary>

```bash
curl --location --request GET 'localhost:8080/'
curl --location --request GET ''http://localhost:8080/'
```
Return OK
</details>
Expand Down
Binary file added charts/cimpex-0.1.0.tgz
Binary file not shown.
40 changes: 40 additions & 0 deletions docs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,43 @@
}
}
},
"/imports": {
"post": {
"produces": [
"application/json"
],
"summary": "Import the tar files in the directory",
"operationId": "post-import-list-docker-images",
"parameters": [
{
"description": "query params",
"name": "request",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/body_types.ImportExportRequest"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/body_types.PackageInfo"
}
}
},
"404": {
"description": "error",
"schema": {
"type": "string"
}
}
}
}
},
"/list": {
"post": {
"produces": [
Expand Down Expand Up @@ -185,6 +222,9 @@
"image_name_tag": {
"type": "string"
},
"status": {
"type": "string"
},
"tar_path": {
"type": "string"
}
Expand Down
26 changes: 26 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ definitions:
properties:
image_name_tag:
type: string
status:
type: string
tar_path:
type: string
type: object
Expand Down Expand Up @@ -107,6 +109,30 @@ paths:
schema:
type: string
summary: Import Docker Image to Registry from tar file
/imports:
post:
operationId: post-import-list-docker-images
parameters:
- description: query params
in: body
name: request
required: true
schema:
$ref: '#/definitions/body_types.ImportExportRequest'
produces:
- application/json
responses:
"200":
description: OK
schema:
items:
$ref: '#/definitions/body_types.PackageInfo'
type: array
"404":
description: error
schema:
type: string
summary: Import the tar files in the directory
/list:
post:
operationId: post-list-docker-images
Expand Down
44 changes: 44 additions & 0 deletions documents/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ cimpex is a CLI application written in Golang that gives the ability import and
| POST | /export | [post export docker image](#post-export-docker-image) | Export Docker Image from Registry to tar file |
| POST | /exports | [post exports docker images](#post-exports-docker-images) | Exports Docker Images from Registry to tar file |
| POST | /import | [post import docker image](#post-import-docker-image) | Import Docker Image to Registry from tar file |
| POST | /imports | [post import list docker images](#post-import-list-docker-images) | Import the tar files in the directory |
| POST | /list | [post list docker images](#post-list-docker-images) | List the docker images tar files in the directory |


Expand Down Expand Up @@ -198,6 +199,48 @@ Status: Not Found



### <span id="post-import-list-docker-images"></span> Import the tar files in the directory (*post-import-list-docker-images*)

```
POST /imports
```

#### Produces
* application/json

#### Parameters

| Name | Source | Type | Go type | Separator | Required | Default | Description |
|------|--------|------|---------|-----------| :------: |---------|-------------|
| request | `body` | [BodyTypesImportExportRequest](#body-types-import-export-request) | `models.BodyTypesImportExportRequest` | || | query params |

#### All responses
| Code | Status | Description | Has headers | Schema |
|------|--------|-------------|:-----------:|--------|
| [200](#post-import-list-docker-images-200) | OK | OK | | [schema](#post-import-list-docker-images-200-schema) |
| [404](#post-import-list-docker-images-404) | Not Found | error | | [schema](#post-import-list-docker-images-404-schema) |

#### Responses


##### <span id="post-import-list-docker-images-200"></span> 200 - OK
Status: OK

###### <span id="post-import-list-docker-images-200-schema"></span> Schema



[][BodyTypesPackageInfo](#body-types-package-info)

##### <span id="post-import-list-docker-images-404"></span> 404 - error
Status: Not Found

###### <span id="post-import-list-docker-images-404-schema"></span> Schema





### <span id="post-list-docker-images"></span> List the docker images tar files in the directory (*post-list-docker-images*)

```
Expand Down Expand Up @@ -265,6 +308,7 @@ Status: Not Found
| Name | Type | Go type | Required | Default | Description | Example |
|------|------|---------|:--------:| ------- |-------------|---------|
| image_name_tag | string| `string` | | | | |
| status | string| `string` | | | | |
| tar_path | string| `string` | | | | |


60 changes: 59 additions & 1 deletion modules/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,59 @@ func postListImages(c *gin.Context) {

}

// @Summary Import the tar files in the directory
// @ID post-import-list-docker-images
// @Produce json
// @Param request body body_types.ImportExportRequest.request true true "query params"
// @Success 200 {object} []body_types.PackageInfo.response
// @Failure 404 {string} string "error"
// @Router /imports [post]
func postImportImagesFromFolder(c *gin.Context) {

var importRequest body_types.ImportExportRequest

// Call BindJSON to bind the received JSON to
if err := c.BindJSON(&importRequest); err != nil {
c.IndentedJSON(http.StatusBadRequest, "Bad payload")
return
}
token := strings.Split(c.Request.Header["Authorization"][0], " ")[1]
dec := helper.DecodeB64(token)
parts := strings.Split(dec, ":")
if parts[0] == "" || parts[1] == "" {
c.IndentedJSON(http.StatusUnauthorized, "Missing username or password")
return
}

reg := registry.CreateDockerRegistry(parts[0], parts[1], importRequest.IgnoreSSL)

files, err := helper.WalkMatch(web_base_folder, "*.tar")
if err != nil {
c.IndentedJSON(http.StatusCreated, err.Error())
return
}

var results []body_types.PackageInfo

for _, o := range files {
name_tag, _ := reg.GetImageNameTag(path.Join(web_base_folder, o))
err := reg.Upload(importRequest.Target+name_tag, path.Join(web_base_folder, o))
if err != nil {
if name_tag != "" {
results = append(results, body_types.PackageInfo{TarPath: o, ImageName: name_tag, Status: err.Error()})
}
} else {
if name_tag != "" {
results = append(results, body_types.PackageInfo{TarPath: o, ImageName: name_tag, Status: "OK"})
}
}

}

c.IndentedJSON(http.StatusCreated, results)

}

// Function to start web server
func StartWebServer(ip string, port string, base_folder string) {
//****************
Expand All @@ -201,6 +254,7 @@ func StartWebServer(ip string, port string, base_folder string) {

router.GET("/docs/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
router.POST("/import", postImport)
router.POST("/imports", postImportImagesFromFolder)
router.POST("/export", postExport)
router.POST("/exports", postExports)
router.POST("/list", postListImages)
Expand All @@ -219,6 +273,10 @@ func StartWebServer(ip string, port string, base_folder string) {
web_base_folder = os.Getenv("BASE_FOLDER")
}

router.Run(web_ip + ":" + web_port)
//****************
//Start the server
//****************
fmt.Printf("Web-Server started %s:%s", web_ip, web_port)
router.Run(fmt.Sprintf("%s:%s", web_ip, web_port))

}
1 change: 1 addition & 0 deletions modules/body_types/body_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ type ImportExportRequest struct {
type PackageInfo struct {
ImageName string `json:"image_name_tag"`
TarPath string `json:"tar_path"`
Status string `json:"status"`
}

0 comments on commit 49a099d

Please sign in to comment.