Remove Docker images from privat repo with python - python

I need to run delete command on python for remove images from docker privat regestry. First of all i need to get correct digest sha56. I got from topic command for find digest.
I am using request library end. In terminal i use command
curl -u Username:Password -sS -H 'Accept: application/vnd.docker.distribution.manifest.v2+json' -o /dev/null -w '%header{Docker-Content-Digest}' https://regestry/v2/[repo]/manifests/[tag].
i try to use
headers={'Accept':'application/vnd.docker.distribution.manifest.v2+json' }
r=requests.get('https://registry/v2/[repo]/manifests/[tag]', auth=('USername', 'Password'))
print(r.headers['Docker-Content-Digest'])
But i got different digests and i can not delete final image.
Can you help me how i can use first command in python for execution.

Related

How to send '-X POST' using 'requests' library in Python?

I am trying to replicate shell command:
curl -u [staff_email]:[api_key] -F "case[attachments][0]=#/path/to/file1.ext" -F "case[attachments][1]=#/path/to/file2.ext" -F "case[content]=I need help" -F "case[subject]=I need help" -F "case[user_email]=user#domain.ru" -F "case[user_full_name]=FullName" -F "case[language_id]=2" -F "case[custom_fields][cf_44]=3" -X POST https://[domain].omnidesk.ru/api/cases.json
into Python code using Requests library:
import requests
What is a proper syntax for this example (curl -X POST)?
You can use requests.post, as explained here
for the attachments part (your first -F), read here
for the auth part (-u): here

OpenSSL error:02001002:system library:fopen:No such file or directory no key found, wrong pass phrase, or wrong file format, git bash vs cmd

I have an application which exposes the urls using mutual Authentication. Now I am writing a python script which uses Popen to run the curl command to connect to the application and gets me the required data. But when I run the python script I get following error.
curl: (58) could not load PEM client certificate, OpenSSL error error:02001002:system library:fopen:No such file or directory, (no key found, wrong pass phrase, or wrong file format?)
I am running the application on windows 7 machine. I have curl and openssl installed. The command that is run is given below
curl -v https://localhost:9400/<URL> -H "Connection:close" --cacert 'C:/local_cert/root.crt' --cert 'C:/local_cert/client.crt' --key 'C:/local_cert/client.key' --pass client_key_passwd
Now for testing I ran the same command in Git Bash for windows. I got the result successfully.
But when I run the same command in Git Cmd for windows or Windows Cmd I get the same above error.
I have checked the paths to cert are correct, they are in PEM format, I have openssl and curl installed.For some reasons I cannot use Requests or urllib3 python pacakges and only can use curl. The above make me believe that there is some setting that Windows Cmd and Git Cmd for windows is missing some settings but I am not sure what it may be.
After trying lot of things I finally figured out the answer. The error said no file found, wrong passphrase or wrong format. Since the command worked in git bash I was sure that its not a issue with file or passphrase. Concentrating on no file found I found below link
Windows PATH to posix path conversion in bash
which gave me an idea that may be the way I am specifying the path is incorrect depending on which version of curl we are using. So after trying various combination I found that if you use plain curl in git bash following both cmd will work
curl -v https://localhost:9400/<URL> -H "Connection:close" --cacert 'C:/local_cert/root.crt' --cert 'C:/local_cert/client.crt' --key 'C:/local_cert/client.key' --pass client_key_passwd
and
curl -v https://localhost:9400/<URL> -H "Connection:close" --cacert C:/local_cert/root.crt --cert C:/local_cert/client.crt --key C:/local_cert/client.key --pass client_key_passwd
But in windows Cmd or when calling curl from python only following cmd will work
curl -v https://localhost:9400/<URL> -H "Connection:close" --cacert C:/local_cert/root.crt --cert C:/local_cert/client.crt --key C:/local_cert/client.key --pass client_key_passwd
So In nutshell it was a issue with quotes because the way your curl utility is called and which version of curl is used (compiled for windows or not) the interpretation of quotes will be different.

putting binary data python sharemy3d

I'm working with ShareMy3D API and came across a problem.
I have a file upload link and need to execute
curl -X PUT -T - "[UPLOAD_URL]" < PATH_TO_LOCAL_FILE
My specific CURL command:
curl -X PUT -T - https://storage.googleapis.com/sharemy3d-central-us-1-produc‌​tion/alotofstuff < /home/max/Documents/file.obj
After I execute it I always get:
curl: (6) Could not resolve host: <
curl: (3) <url> malformed
Are there other ways to PUT binary files?
All I had to do was execute curl -T file.obj https://example.com. With -T (same as --upload-file) curl will use PUT for HTTP

How to post a list of files using Curl

I need to curl multiple files to a website as a list, but I'm having trouble doing so.
I've tried:
curl -F name=fn1 -F filedata=#fn1.jpg -F name=fn2 -F filedata=#fn2.jpg
But that didn't work, so I tried:
curl -F "files[]=#fn1.jpg&files[]=fn2.jpg"
But that also didn't work.
I'm trying to get the files on the server side using the python request lib, so something like:
files = request.files.getlist('files[]')
But this call always fails, how do I post a list of files using curl?
I figured it out, the syntax is this :
curl -F files=#$WALLPAPERS/0FYIE5G.jpg \
-F files=#$WALLPAPERS/3GVmZmQ.png http://example.com/upload
And on the receiving end :
files = request.files.getlist('files')
Pretty simple actually.

How to upload a file to Atlassian Confluence page using curl

I am trying to upload an .xls file to a Confluence wiki page following the guidelines given in the Remote API documentation :
https://developer.atlassian.com/confdev/confluence-server-rest-api/confluence-rest-api-examples#ConfluenceRESTAPIExamples-Uploadanattachment
curl -v -S -u admin:admin -X POST -H "X-Atlassian-Token: nocheck" -F "file=#myfile.txt" -F "comment=this is my file" "http://localhost:8080/confluence/rest/api/content/3604482/child/attachment" | python -mjson.tool
This is what I am doing:
curl -v -S -u username:password -X POST -H "X-Atlassian-Token: nocheck" -F "file=#/path/to/local/excelsheet.xls" https://<Confluence server>/display/page
I omitted the Python -mjson.tool as it says 'No JSON object could be decoded' and it didn't make sense as I am not posting JSON.
However the above curl command is not working for me. I see the html of the target page on my console and the file doesn't get uploaded. I tried modifying the curl command in several ways but nothing worked.
Also for the URL of the page I am trying to upload to, it doesn't have any contentID as suggested in the documentation. The target URL is a page which accepts attachments and shows the uploaded file list.
Could someone please point out where am I going wrong? I don't have much experience with Curl.
David Vonka answer is correct except for header "X-Atlassian-Token" value. It must be "no-check" (instead of "nocheck")
So corrected command is:
curl -v -S -X POST -H "X-Atlassian-Token: no-check" -F "file_0=#<file name>" -F "comment_0=<upload comment>" "http://<server>:<port>/<context>/pages/doattachfile.action?pageId=<page id>&os_username=<username>&os_password=<password>"
NOTE: replace all the <...> placeholders with your values
you need to use REST API in url: .../confluence/rest/api/content/$PAGE_ID/child/attachment and now you are using url of view page.
I do not think that confluence rest api allows file upload. Please do this instead
curl -v -S -X POST -H "X-Atlassian-Token: nocheck" -F "file_0=#<file name>" -F "comment_0=<upload comment>" "http://<server>:<port>/<context>/pages/doattachfile.action?pageId=<page id>&os_username=<username>&os_password=<password>"
replace all the <...> placeholders with your values
The API POST syntax should be corrected from "https://ConfluenceServer/display/page" to the correct res/api/content syntax eg: "https://companyName.atlassian.net/display/rest/api/content/pageIDxxxxx":
curl -v -S -X POST -H "X-Atlassian-Token: no-check" -F "file_0=#<file name>" -F "comment_0=<upload comment>" https://<companyName>.atlassian.net/display/rest/api/content/<pageID15398762>/child/attachment
The pageID can be found in the URL display page. For example:
https://companyName.atlassian.net/display/spaces/DEV/pages/pageID15398762/Page+Title+Name
For more API syntax details please refer to this documentation:
https://docs.atlassian.com/atlassian-confluence/REST/6.6.0/#content-createContent

Categories