Script the Registration of an SSH Target

Hi,

Im trying to use the bash script to add an octopus SSH target, but it is failing

--2018-10-17 11:26:09--  http://server.example.com/api/machines
Resolving server.example.com (server.example.com)... 192.168.132.5
Connecting to server.example..com (server.example.com)|192.168.132.5|:80... connected.
HTTP request sent, awaiting response... 500 Internal Server Error
2018-10-17 11:26:09 ERROR 500: Internal Server Error.

Added machine "el-linux" () - Launching health check task
--2018-10-17 11:26:09--  http://server.example.com/api/tasks
Resolving server.example.com (server.example.com)... 192.168.132.5
Connecting to server.example.com (server.example.com)|192.168.132.5|:80... connected.
HTTP request sent, awaiting response... 201 Created
Length: unspecified [application/json]
Saving to: ‘STDOUT’

    [ <=>                                                                                                                                                                                                ] 1,383       --.-K/s   in 0s

2018-10-17 11:26:09 (6.01 MB/s) - written to stdout [1383]

Im using the bash script from
https://help.octopusdeploy.com/discussions/questions/9544-script-the-registration-of-an-ssh-target

Hi Helen,

Thanks for getting in touch and I’m sorry to hear that you’re having these issues.

Could you check the Octopus server log at the time you were trying to add the SSH target as that should have some further clues as to what caused the 500: Internal Error.

Thank you and best regards,
Henrik

2018-10-18 14:36:31.3261 6684 67 ERROR Unhandled error on request: http://test–octopus01.mydomain.com/api/machines b4556787887hdgfh64657575 by puppet-sa-dq : Error reading JObject from JsonReader. Current JsonReader item is not an object: String. Path ‘Endpoint’, line 1, position 33.
Newtonsoft.Json.JsonReaderException: Error reading JObject from JsonReader. Current JsonReader item is not an object: String. Path ‘Endpoint’, line 1, position 33.
at Newtonsoft.Json.Linq.JObject.Load(JsonReader reader, JsonLoadSettings settings)
at Octopus.Core.Serialization.InheritedClassConverter2.ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.DeserializeConvertable(JsonConverter converter, JsonReader reader, Type objectType, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) at Octopus.Server.Web.Infrastructure.NancyContextExtensions.Bind(Request request, Type modelType) at Octopus.Server.Web.Infrastructure.NancyContextExtensions.BindAndValidate(NancyContext context, IModelValidatorLocator validatorLocator, Type modelType) at Octopus.Server.Web.Infrastructure.NancyContextExtensions.BindAndValidate[TModel](NancyContext context, IModelValidatorLocator validatorLocator) at Octopus.Server.Web.Infrastructure.Api.Responder1.BindAndValidateTModel
at Octopus.Server.Web.Infrastructure.Api.CreateResponseDescriptor2.Responder.ExecuteRegistered() at Octopus.Server.Web.Infrastructure.Api.Responder1.Respond(TDescriptor options, NancyContext context)
at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
at Octopus.Server.Web.Infrastructure.OctopusNancyModule.<>c__DisplayClass14_0.<get_Routes>b__1(Object x)
at Nancy.Routing.Route.<>c__DisplayClass4.b__3(Object parameters, CancellationToken context)

Hi Helen,

It looks like there was a missing { before CommunicationStyle in the data for the --post-data argument. If you add that missing { to your script (so it looks something like this "{\"Endpoint\": {\"CommunicationStyle\":\"Ssh\",\"AccountId\":\"$accountId\",\"Host\":\"$localip\",\"Port\":\"22\",\"Fingerprint\":\"$fingerprint\"},\"EnvironmentIds\":[\"$environmentId\"],\"Name\":\"$machineName\",\"Roles\":[\"linux\"]}") it should work successfully.

If it still is throwing errors, please provide the script you’re using and I’ll have a look and see what is causing the issue.

Thank you and best regards,
Henrik

Hi,

Im using the bash script from
https://help.octopusdeploy.com/discussions/questions/9544-script-the-registration-of-an-ssh-target

Hi Helen,

That script was just a getting started point for how to add a machine to Octopus from an SSH target so it had a few bugs in it.

I’ve modified the script and tested it on a Ubuntu 16.04 target connecting to an Octopus instance running 2018.8.6, please see below:

serverUrl=""
apiKey=""
localIp=$(ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}')
fingerprint=$(ssh-keygen -E md5 -lf /etc/ssh/ssh_host_rsa_key.pub | cut -d' ' -f2 | cut -d':' -f2- | awk '{ print $1}')
environmentName=""
accountName=""
machineName=""

environmentId=$(wget -nv --header="X-Octopus-ApiKey: $apiKey" -O- ${serverUrl}/api/environments/all | jq ".[] | select(.Name==\"${environmentName}\") | .Id" -r)
echo Environment \"$environmentName\" '('$environmentId')'

accountId=$(wget -nv --header="X-Octopus-ApiKey: $apiKey" -O- ${serverUrl}/api/accounts/all | jq ".[] | select(.Name==\"${accountName}\") | .Id" -r)
echo Account \"$accountName\" '('$accountId')'

machineId=$(wget -nv --header="X-Octopus-ApiKey: $apiKey" --post-data "{\"Endpoint\": {\"CommunicationStyle\":\"Ssh\",\"AccountId\":\"$accountId\",\"Host\":\"$localIp\",\"Port\":\"22\",\"Fingerprint\":\"$fingerprint\"},\"EnvironmentIds\":[\"$environmentId\"],\"Name\":\"$machineName\",\"Roles\":[\"linux\"]}" -O- ${serverUrl}/api/machines | jq ".Id" -r)
if [ -n "$machineId" ]
then
	echo Added machine \"$machineName\" '('$machineId')'
	wget -nv --header="X-Octopus-ApiKey: $apiKey" --post-data "{\"Name\":\"Health\",\"Description\":\"Check $machineName health\",\"Arguments\":{\"Timeout\":\"00:05:00\",\"MachineIds\":[\"$machineId\"]}}" -O-  ${serverUrl}/api/tasks | jq ".Id" -r
fi

I hope that helps.

Thank you and best regards,
Henrik

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.