通过REST API创建Octopus机器(PowerShell)
我试图通过REST API创建一台机器,通过PowerShell调用它。通过REST API创建Octopus机器(PowerShell)
根据documentation,只需要机器的名称和指纹来创建机器。但是,一切都我尝试导致无论是405还是500错误:
$newMachine = @{Name='machineName';Thumbprint='asdvblsafghkvadc';} | ConvertTo-Json
$webClient.UploadString('http://server/octopus/api/machines','POST',$newMachine)
### OR
$newMachine = @{Name='machineName';Thumbprint='asdvblsafghkvadc';Roles='[web-server]';EnvironmentIds='[Environment-12]'; | ConvertTo-Json
$webClient.UploadString('http://server/octopus/api/machines','POST',$newMachine)
创建环境和编辑机很好地工作,我不能让这台机器创造了下来。
要从Octopus REST API创建机器,需要导入某些库。 DalmiroGrañas概括这在第一系列的博客文章对interfacing with Octopus through PowerShell
他不涵盖虽然创造的机器,这是我不得不从库文档弄清楚自己在MachineResource class
下面是代码我结束使用创建机器:
#Adding libraries
Add-Type -Path 'C:\OctopusLibraries\Newtonsoft.Json.dll'
Add-Type -Path 'C:\OctopusLibraries\Octopus.Client.dll'
Add-Type -Path 'C:\OctopusLibraries\Octopus.Platform.dll'
#Setting variables
$apikey = 'API-QBGAAZEUMSUKJVSADFSDFA5Y2FLC'
$OctopusURI = 'http://OctopusServer/octopus/api/'
#Creating a connection
$endpoint = new-object Octopus.Client.OctopusServerEndpoint $OctopusURI,$apikey
$repository = new-object Octopus.Client.OctopusRepository $endpoint
#Set the machine properties
$Properties = @{Name="MachineName";Thumbprint="1AE1B6F81A30C2C5771AC5B234S4FE975";EnvironmentIds="Environments-65";Roles="web-server";URI="https://MyServer:10933/";CommunicationStyle="TentaclePassive"}
$envObj = New-Object Octopus.Client.Model.MachineResource -Property $Properties
$repository.Machines.Create($envObj)
通常是当您将触手代理添加到您的服务器时,立即想要将触手注册到所需的八达通服务器。我会给你提供我用来完成这个任务的PowerShell代码,但是它可能值得读the Octopus blog post on how they create their tentacle army。他们提供PowerShell,这是如何做到这一点的好开始。
注意:我有一段恐怖的时间让触手在八达通服务器注册后连接。让它工作的秘诀? Stop the tentacle service and start it again.
重读您的帖子后,它看起来像你想触手添加到八达通服务器外或触手安装本身之后。你能澄清吗?我认为至少环境和鱿鱼也是必需的。尝试添加鱿鱼。
我们的环境无法连接到互联网,它们完全被沙箱化。通过用户界面注册环境时,Octopus打开了一个与我们环境的连接。问题是环境无法打开与八达通服务器的连接。我已经尝试从环境本身做到这一点,在您提供的链接中调用'tentacle.exe register-with'失败。因此,我需要使用REST API向服务器注册机器 – ShaneC
该帖子由ShaneC提到它有点老了。我建议任何人尝试在Powershell中执行此操作来检查我开始为Octopus Deploy编写的模块。
模块网站:http://octoposh.net/
使用模块添加机器的例子:https://github.com/Dalmirog/OctoPosh/wiki/Creating-Resources#machines
模块仍然是一个进展中的工作,但我最近收到的是一个相当不错的反馈。
一些帮助和反馈社会,我敢肯定,这将是最终成为真正的好:)
我需要这个星期做这个工作,这里是代码的例子您正在寻找(您将需要PowerShell v3。0使用调用-RestMethod,下载Windows 7在这里:https://www.microsoft.com/en-us/download/details.aspx?id=34595 [MS去前间谍软件):
$headers = @{"X-Octopus-ApiKey"="<YOUR OCTOPUS API KEY>"}
$url = "http://<YOUR OCTOPUS SERVER>/api/"
$envname = "<YOUR ENVIRONMENT NAME FROM OCTOPUS>"
$machine = "<YOUR MACHINE NAME>"
$dropPath = "\\<DROP MACHINE>\drop"
$appPath = "C:\Application"
$octopusPath = "C:\Octopus"
$roles = {"role1", "role2"}
# Get list of Environments
$environments = Invoke-RestMethod ($url + "environments/all") -Headers $headers -Method Get
# Choose the specific Environment by Name
$environmentId = $environments | ? { $_.Name -eq $envname }
# Fix up slashes
$dropPath = $dropPath.Replace("\","\\")
$appPath = $appPath.Replace("\","\\")
$octopusPath = $octopusPath.Replace("\","\\")
# build request
$body = "{""Endpoint"":{""CommunicationStyle"":""OfflineDrop"",""DropFolderPath"":""" + $dropPath + """,""ApplicationsDirectory"":""" + $appPath + """,""OctopusWorkingDirectory"":""" + $octopusPath + """},""Status"":""Unknown"",""Name"":""" + $machine + """,""EnvironmentIds"":[""" + $environmentId + """],""Roles"":[" + $roles + "]}"
# send request
$added = Invoke-RestMethod ($url + "machines") -Headers $headers -Body $body -Method Post
# show result
echo $added
我以前在工作中类似的代码,我测试过,虽然我还没有得到八达通运行家(这不是一个伟大的产品,我不得不使用它,并给出选择,我会使用备用版本控制,意味着[特别是Team Foundation和vNext构建系统],UI不太好,API并不是很好,它以发布管理的形式增加的价值对于已经在TeamFoundation中工作的人来说是有问题的,因为他们为开发人员提供了更容易获取的所有内容)。
仍然有人采取了它,它不会消失。
感谢您发布答案。我*认为*这些库包含在您的代码段中。我的错。 – osij2is