How to remove files inside a container using the Docker API - rest

Please advise, how to remove files inside a container using the Docker API?
I have tried to do it this way, but it doesn't work
curl --location --request POST 'http://192.168.1.55:5555/containers/{id}/exec' \
--header 'Content-Type: application/json' \
--data-raw '{
"Cmd": [
"/bin/sh",
"-c",
"'rm -rf /files/*'"
]
}'

According to the Docker API doc "To exec a command in a container, you first need to create an exec instance, then start it. These two API endpoints are wrapped up in a single command-line command, docker exec." In this way, after getting the response you must run new POST request /exec/{id}/start

Related

Malformed command in kubectl run

I have a Kubernetes cluster on which is deployed a replica of this image (but it's not important for the sake of the question).
Now, in Powershell I need to perform one REST request against the pod. In particular I have to send a PUT request to the endpoint http://<POD_IP>:18681/PCCIS/V1/Service/Properties/Servers passing this JSON payload:
{"servers":[{"port":"18682","address":"10.244.1.29"}]}
What I was trying to do is perform this action from within a curlimages/curl pod, just to be able to reach the pod's network. This is my attempt:
$PrizmDocPodIP="10.244.1.29"
$JsonPostBody='{"servers":[{"port":"18682","address":"10.244.1.29"}]}'
$cmd = "curl -i --header 'Content-Type: application/json' --request PUT --data '$JsonPostBody' http://$($PrizmDocPodIP):18681/PCCIS/V1/Service/Properties/Servers"
kubectl run -i --tty --rm curl --image=curlimages/curl --restart=Never "$cmd"
Unexpectedly, the command output this error:
curl: (3) nested brace in URL position 82:
curl -i --header 'Content-Type: application/json' --request PUT --data '{servers:[{port:18682,address:10.244.1.29}]}' http://10.244.1.29:18681/PCCIS/V1/Service/Properties/Servers
As you can see, the problem I have is that the string command I pass is not interpreted the right way and moreover the payload loses all the doublequotes of the JSON, that are important for the curl command to propertly work.
One another test I made is with this command (the echo is used to show what the sh command really would execute)
kubectl run -i --tty --rm curl --image=curlimages/curl --restart=Never -- sh -c "echo $cmd"
And the output is
curl -i --header Content-Type: application/json --request PUT --data {servers:[{port:18682,address:10.244.1.29}]} http://10.244.1.29:18681/PCCIS/V1/Service/Properties/Servers
Showing that the cmd Powershell variable loses its doublequotes when passed to kubectl run
What should be the right syntax to pass the $cmd command as is (preserving all its quotes and chars)?
I found the solution after some searching and trials.
First, I have to escape the double quotes in the $JsonPostBody variable.
$PrizmDocPodIP="10.244.1.29"
$JsonPostBody='{\"servers\":[{\"port\":\"18682\",\"address\":\"10.244.1.29\"}]}'
The other error is that the curlimages/curl Docker image executes the kubectl run arguments as the curl's arguments, so I don't have to use the curl command:
kubectl run -i --tty --rm curl --image=curlimages/curl --restart=Never -- -i --header 'Content-Type: application/json' --request PUT --data $JsonPostBody http://$($PrizmDocPodIP):18681/PCCIS/V1/Service/Properties/Servers
This outputs
HTTP/1.1 200 OK
[1mDate[0m: Wed, 08 Jul 2020 13:23:30 GMT
[1mConnection[0m: keep-alive
1mContent-Length[0m: 0

Github api tag is not created

I tried to create a tag using Github API. I made a POST request to /repos/:owner/:repo/git/tags, and I get this result:
HTTP/1.1 201 Created
But unfortunately no tag was created. The new tag simply does not exist.
What do I wrong?
The tagging didn't work for me. It shows created, but nothing appears on github. However, I managed to achieve tagging by creating pre-release. Which is not ideal, but still better than nothing:
curl --location --request POST
'https://<giturl>/repos/{owner}/{repo}/releases' \
--header 'Authorization: Basic xxx' \
--header 'Content-Type: application/vnd.github.v3+json' \
--data-raw '{
"tag_name": "v0.0.1",
"target_commitish": "master",
"name": "v0.0.1",
"body": "This is for Release v0.0.1 of the product",
"draft": false,
"prerelease": true}'
There are two types of tags -- annotated and lightweight, you can check the difference here.
As Github API puts, /repos/:owner/:repo/git/tags only created an annotated tag object, and then you should manually create a refrence with the sha of this tag object by calling create refrence api:
curl \
-X POST \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/OWNER/REPO/git/refs \
-d '{"ref":"refs/tags/tagName","sha":"the sha of tag object"}'
In another case, if you only want to add a lightweight tag to one commit, you should directly call create refrence api without the first step:
curl \
-X POST \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/OWNER/REPO/git/refs \
-d '{"ref":"refs/tags/tagName","sha":"the sha of the commit that you want to tag"}'

How to delete grafana dashboard using curl command?

I am trying to delete grafana dashboard using curl command, but it is not deleting, the below curl command I tried to delete the dashboard,
curl -XPOST -d '{"name":"mydashboard"}' http://localhost:3000/api/dashboards/db -u admin:admin -H "Content-Type: application/json"
Run the below command,
curl -k -X DELETE -u admin:admin http://localhost:3000/api/dashboards/db/dashboardslug_name
where dashboardslug_name is the name while save the dashboard and not the dashboard title displaying in the web url.

Google Cloud SQL import database programatically (without prompt)

I want to export a database and import the output into another database programatically. This is what I have so far:
gcloud sql export sql instance_name gs://bucketname/db.gz --database=db_name
gcloud sql databases create new_db --instance=instance_name
gcloud sql import sql instance_name gs://bucketname/db.gz --database=new_db
Created database [new_db].
instance: instance_name
Data from [gs://bucketname/db.gz]
will be imported to [instance_name].
Do you want to continue (Y/n)
As you can see the prompt is the issue.
How can I import it without being prompted? Is there another way to import an export?
You can use --quiet, -q parameter when running your gcloud command as shown below:
gcloud sql import sql instance_name gs://bucketname/db.gz --database=new_db -q
The gcloud Reference official documentation contains the following explanation about this parameter in case you want to take a look on it:
--quiet, -q
Disable all interactive prompts when running gcloud commands. If input
is required, defaults will be used, or an error will be raised.
Overrides the default core/disable_prompts property value for this
command invocation. Must be used at the beginning of commands. This is
equivalent to setting the environment variable
CLOUDSDK_CORE_DISABLE_PROMPTS to 1.
Additionally, you can perform the import/export tasks by using cURL API calls as an alternative option; In this way, you just need to send the authorized requests to the service.
*Importing:
ACCESS_TOKEN="$(gcloud auth application-default print-access-token)"
curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
--header 'Content-Type: application/json' \
--data '{"importContext":
{"fileType": "SQL",
"uri": "gs://[BUCKET_NAME]/[PATH_TO_DUMP_FILE]",
"database": "[DATABASE_NAME]" }}' \
-X POST \
https://www.googleapis.com/sql/v1beta4/projects/[PROJECT-ID]/instances/[INSTANCE_NAME]/import
*Exporting:
ACCESS_TOKEN="$(gcloud auth application-default print-access-token)" curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
--header 'Content-Type: application/json' \
--data '{"exportContext":
{"fileType": "SQL",
"uri": "gs://<BUCKET_NAME>/<PATH_TO_DUMP_FILE>",
"databases": ["<DATABASE_NAME1>", "<DATABASE_NAME2>"] }}' \
-X POST \
https://www.googleapis.com/sql/v1beta4/projects/[PROJECT-ID]/instances/[INSTANCE_NAME]/export

Algolia: Delete multiple records from dashboard

How can I delete multiple records at once? Is it possible to select all, say "products" post_type, and delete it or it has to be one by one? (I'm not trying to clear all the records)
Algolia's dashboard is not designed to be a complete graphical interface on top of the API, it's mostly here for convenience, understanding and testing purposes, not complete management of the data.
As soon as you start being limited by the dashboard, you should probably write a small script to achieve what you're trying to do.
Fortunately, it's been designed to be as easy as possible.
With PHP, here's how it would look like:
First, let's create a small folder to hold the script.
mkdir /tmp/clear-algolia && cd /tmp/clear-algolia
If you don't have composer yet, you can simply install it in the current folder by launching the commands described here.
If you've just installed it and want to just use it just for this session:
alias composer=php composer.phar
Then install Algolia using composer:
composer require algolia/algoliasearch-client-php
Write a small script along those lines:
<?php
// removeSpecific.php
require __DIR__ . '/vendor/autoload.php';
$client = new \AlgoliaSearch\Client("YOUR_APP_ID", "YOUR_ADMIN_API_KEY");
$index = $client->initIndex('YOUR_INDEX');
$index->deleteByQuery('', [ 'filters' => 'post_type:products' ]);
?>
Then run it:
php removeSpecific.php
And you're good to go! Next time you want to do an operation on your index, you'll only have to change the last line of the script to achieve what you want.
You can use the REST API.
It can be easier or faster to do it with PostMan.
Here you can check a simple request: https://www.algolia.com/doc/rest-api/search/#delete-by
To first check what you are deleting, you can use:
curl --location --request POST 'https://[AplicationID]-
dsn.algolia.net/1/indexes/[IndexName]/query' \
--header 'X-Algolia-Application-Id: XXXXXXXXXXXX' \
--header 'X-Algolia-API-Key: XXXXXXXXXXXXXXXXXXXXXXXX' \
--header 'Content-Type: application/json' \
--data-raw '{
"params":"numericFilters=id<=9000"
}'
And to delete the records you can use:
curl --location --request POST
'https://[AplicationID].algolia.net/1/indexes/[IndexName]/deleteByQuery' \
--header 'X-Algolia-Application-Id: XXXXXXXXXXXX' \
--header 'X-Algolia-API-Key: XXXXXXXXXXXXXXXXXXXXX' \
--header 'Content-Type: application/json' \
--data-raw '{
"params":"numericFilters=id<=8000"
}'
The "params" should receive a Search Parameter, you can find a list here: https://www.algolia.com/doc/api-reference/search-api-parameters/