Need help integrating SOTI MobiControl into our DevOps pipeline

Solved
T
teddy
ORECO

Hello everyone,

I'm looking to integrate SOTI MobiControl into our DevOps pipeline, specifically aiming to implement CI/CD by uploading an APK file via the POST /packages API. Unfortunately, I've been stuck for several days now and could really use your help.

Here's my current script:


#!/bin/bash

# Fetching the APK
curl -u [USERNAME]:[PASSWORD] -o "/home/[USERNAME]/app_name.apk" "https://nexus.example.com/repository/raw/apk/env_dev/release/app_name.apk"

# Setting base variables
base_url="https://soti.example.com/MobiControl/api"
auth="Basic [BASE64_ENCODED_CREDENTIALS]"
content_type="multipart/related"
content_type2="multipart/related; boundary=content"
apk_path="/home/[USERNAME]/app_name.apk"

# Getting access token
access_token=$(curl -s -X POST "$base_url/token" \
    -H "Authorization: $auth" \
    -H "Content-Type: $content_type" \
    -d "grant_type=password&username=[USERNAME]&password=[PASSWORD]" \
    | jq -r '.access_token')

echo "Access Token: $access_token"

# Sending application download request to MobiControl
curl -X POST "$base_url/packages" \
  -H "Authorization: Bearer $access_token" \
  -H "Content-Type: $content_type2" \
  -d @- <<EOF
--content
Content-Type: application/vnd.android.application.metadata+json

{ "DeviceFamily" : "AndroidPlus", "PackageName": "package", "PackageVersion": "1.0"}
--content
Content-Type: application/vnd.android.application
Content-Disposition: attachment; filename="app_name.apk"
--content--
EOF

However, the server returns the following message: "Internal Server Error".

For your information, I'm using version: 2024.0.1.1020.

Any help would be greatly appreciated!

a year ago
SOTI MobiControl
ANSWERS
ZC
Zafer Cigdem
a year ago

Hi Teddy,

Do you face a problem for /packages API call?

If yes, for test purpose, can you please try to use /packages/v2  (instead of V1) API call?

Thanks

Zafer

T
teddy
a year ago

Thank you, Zafer Cigdem. I hadn't noticed that there was a version 2 of the /packages API.

I feel a bit silly =) I'm experimenting with this version and will let you know how it goes.

ZC
Zafer Cigdem
a year ago

AFAIK, there is no Version_2 of this API call earlier. I hope it'll solve your issue. Please let us know how it goes. 

Best of luck :)

zafer

T
teddy
a year ago

Okay, I'm still stuck even with the package/v2. The server responds to me: "Internal Server Error".

In the server logs, I have:


************************************************************************************************************
* Exception: IOException: Unexpected end of MIME multipart stream. MIME multipart message is not complete. *
************************************************************************************************************


if somebody can help me ?

Does it work via the API page then instead of your script?

T
teddy
a year ago

Yes, it works very well with the API page, even better it automatically retrieves the name and version for me.

ZC
Zafer Cigdem
a year ago

Hi Teddy,

Can you run any other API from the same server/PC that you are trying to execute this packageV2 API. I'm wondering whether it's about any restriction or not. 

if it's an easy test and possible, you can try running your API on MC server and see whether you face same issue or not. 

Other than the code, you may also test using postman or such to fasten the test process and narrow-down the issue.

I hope this helps

T
teddy
a year ago

Hi Zafer,


It's not a permission issue because I'm able to push the API with PowerShell and the following script:

####################DOWNLOAD APK########################            
            # Définir les informations d'identification
            $username = "admin"
            $password = "GzUGq&KsGSac@3FJj7gN9b"

            # URL du fichier APK
            $url = "https://nexus.oreco.fr:8443/repository/raw-solera/apk/env_dev/release/app-env_dev-release.apk"

            # Chemin où enregistrer le fichier APK localement
            $localPath = "C:\Users\adm-tdavid\Documents\solera-dev\apk\app-env_dev-release.apk"

            # Créer les informations d'identification au format requis
            $credPair = "${username}:${password}"
            $base64CredPair = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($credPair))
            $authHeader = "Basic $base64CredPair"

            # Télécharger le fichier APK
            Invoke-WebRequest -Uri $url -Headers @{ Authorization = $authHeader } -OutFile $localPath
#########################################################




####################Récup JETON SOTI#####################
            $headers = @{
                "Authorization" = "Basic MmRhN2FhYzBhYjNlNDlkMjhlZjcwNDVlYWQ4MzBiZmU6QGRtaW5vcmUxMg=="
                "Content-Type" = "application/x-www-form-urlencoded"
                }

            # Effectuer la requête POST pour obtenir le jeton
            $response = Invoke-WebRequest -Uri "https://soti.oreco.fr/MobiControl/api/token" -Method Post -Headers $headers -Body $body2
            echo  $response

            # Extraire le contenu de la réponse et le convertir en JSON
            $jsonResponse = $response.Content | ConvertFrom-Json
            echo $jsonRespons

            # Extraire l'access_token du JSON
            $accessToken = $jsonResponse.access_token

            #echo $accessToken
###########################################################

#####################ENOVIE APK SOTI#######################
            # Maintenant, vous pouvez continuer avec le reste de votre script
            $Header2 = @{}
            $Header2["Authorization"] = "Bearer $accessToken"
            $Header2["Accept"] = "application/json"
            $boundary = "mobicontrol_boundary"
            $BinaryString = [IO.File]::ReadAllBytes($localPath)  # Utilise le chemin local du fichier téléchargé
            $LF = "`r`n"
            $body = "--${boundary}${LF}"
            $body += "Content-Type: application/vnd.soti.mobicontrol.packagearchive.metadata+json${LF}"
            $body += '{
                "PackageName":"SOLERA-DEV-TEST", "PackageVersion":"1.0", "PackagePlatform":"Android",
                "PackageFiles": [{ "FileName":"app-env_dev-release.apk", "FileSourceType":"Binary" } ], 
            }'
            $body += $LF
            $body += "--${boundary}${LF}"
            $body += "Content-Type: application/octet-stream${LF}"
            $body += "Content-Type-Encoding: Binary${LF}"
            $body += 'Content-Disposition: attachment; filename="app-env_dev-release.apk"' + $LF
            $body += $LF
            $body += $BinaryString
            $body += "${LF}--${boundary}--${LF}"

            #####DEBUG######
                    # Obtenir les 200 premiers caractères
                    #$premiers200 = $body.Substring(0, [math]::Min(500, $body.Length))

                    # Obtenir les 200 derniers caractères
                    #$derniers200 = $body.Substring([math]::Max(0, $body.Length - 400))

                    # Exporter dans un fichier ou afficher
                    #$premiers200 + "`n" + $derniers200 | Out-File -FilePath "output.txt"
            ################

            $response = Invoke-WebRequest -Uri ("https://soti.oreco.fr/mobicontrol/api/packages/v2") -ContentType "multipart/related; boundary=$boundary" -Method POST -Headers $Header2 -Body $Body -Verbose
            echo $response
###########################################################
Solution
ZC
Zafer Cigdem
a year ago

Hi @Teddy,

Sorry I was OOO, and just back. It looks like you mark a solution, just curiosity, what was the solution for your case? thanks 

Zafer

T
teddy
a year ago

I used GOlang and ansible because with curl, it was impossible to send the APK file. It couldn't upload the binary file because it had too many characters for curl.

ZC
Zafer Cigdem
a year ago

I've been used PowerShell and Python for APIs and did not face any issue. It's good to know, thank you :)

V
VPMod@SOTI.net
a year ago

Hi Teddy


Thanks for posting on SOTI Pulse.

Thank you, Zafer and Rafael for responding to the post, your expertise and willingness to help are greatly appreciated.

We are glad to know that your issue has been resolved. If you have any additional questions or concerns, please don't hesitate to reach out. We're dedicated to providing assistance and support.

Technical Support, SOTI Inc.