首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何保持terraform的远程执行shell持久化?

如何保持terraform的远程执行shell持久化?
EN

Stack Overflow用户
提问于 2019-11-07 23:55:38
回答 1查看 411关注 0票数 0

日安,

我正在使用Terraform从vSphere上的模板配置虚拟机。一旦VM启动,文件提供程序就会复制本地静态内容(图片)。完成此操作后,远程执行置备程序将执行以下操作:

代码语言:javascript
复制
provisioner "remote-exec" {
     inline = [
       "mkdir /home/foo/static",
       "mv /home/foo/logo.jpg /home/foo/static/",
       "echo 'python /home/foo/app.py 2>&1 &' > /home/foo/start_app.sh",
       "chmod u+x /home/foo/start_app.sh",
       "/home/foo/start_app.sh 2>&1",
       "sleep 60"
     ]
}

app.py是一个Python Flask项目。代码可以很好地启动和提供内容。大约60秒。当我的睡眠计时器超时时,我怀疑Terraform产生的shell会终止,app.py也会终止。我试着在后台启动start_app.sh,我试着去做(前台和后台)都没有用。同样的行为。如果我直接在远程执行块中启动python /home/foo/app.py,而不是调用start_app.sh,那么TF永远不会退出外壳,而我的Jenkins构建将永远旋转。

我不认为这有什么不同,但为了完整,我的TF VMware计划是在Git向Jenkins发送webhook时应用的。Jenkins调用TF计划作为管道阶段的一部分。以下是Jenkins的控制台输出:

代码语言:javascript
复制
vsphere_virtual_machine.vm[1]: Provisioning with 'file'...
vsphere_virtual_machine.vm[1]: Provisioning with 'remote-exec'...
vsphere_virtual_machine.vm[1] (remote-exec): Connecting to remote host via SSH...
vsphere_virtual_machine.vm[1] (remote-exec):   Host: 1.1.1.200
vsphere_virtual_machine.vm[1] (remote-exec):   User: foo
vsphere_virtual_machine.vm[1] (remote-exec):   Password: true
vsphere_virtual_machine.vm[1] (remote-exec):   Private key: false
vsphere_virtual_machine.vm[1] (remote-exec):   SSH Agent: false
vsphere_virtual_machine.vm[1] (remote-exec):   Checking Host Key: false
vsphere_virtual_machine.vm[1] (remote-exec): Connected!
vsphere_virtual_machine.vm[1] (remote-exec):  * Serving Flask app "app" (lazy loading)
vsphere_virtual_machine.vm[1] (remote-exec):  * Environment: production
vsphere_virtual_machine.vm[1] (remote-exec):    WARNING: This is a development server. Do not use it in a production deployment.
vsphere_virtual_machine.vm[1] (remote-exec): [2m   Use a production WSGI server inst
vsphere_virtual_machine.vm[1] (remote-exec):  * Debug mode: off
vsphere_virtual_machine.vm[1] (remote-exec):  * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
vsphere_virtual_machine.vm.1: Still creating... (8m30s elapsed)
vsphere_virtual_machine.vm.0: Still creating... (8m30s elapsed)
vsphere_virtual_machine.vm.1: Still creating... (8m40s elapsed)
vsphere_virtual_machine.vm.0: Still creating... (8m40s elapsed)
vsphere_virtual_machine.vm.1: Still creating... (8m50s elapsed)
vsphere_virtual_machine.vm.0: Still creating... (8m50s elapsed)
vsphere_virtual_machine.vm.1: Still creating... (9m0s elapsed)
vsphere_virtual_machine.vm.0: Still creating... (9m0s elapsed)
vsphere_virtual_machine.vm.1: Still creating... (9m10s elapsed)
vsphere_virtual_machine.vm.0: Still creating... (9m10s elapsed)
vsphere_virtual_machine.vm[0]: Creation complete after 9m15s (ID: 4228d941-a19c-361d-073a-4441cde5973e)

在TF的远程执行完成后,如何使TF产生的shell保持持久?

EN

回答 1

Stack Overflow用户

发布于 2019-11-08 01:40:25

好的--我发现了一个非常可怕的黑客。Terraform不再运行remote-exec,它只是使用文件提供程序推送我的python Flask应用程序及其相关的静态内容。在Jenkins内部,我有一个舞台,看起来像这样:

代码语言:javascript
复制
def remote = [:]
remote.name = "1.1.1.200"
remote.host = "1.1.1.200"
remote.allowAnyHosts = true

node {
    withCredentials([usernamePassword(credentialsId: 'sshUserAccount', passwordVariable: 'password', usernameVariable: 'userName')]) {
        remote.user = userName
        remote.password = password
        stage("SSH Steps Rocks!") {
            try {
              timeout(time: 1, unit: 'MINUTES') {
                sshCommand remote: remote, command: '/home/foo/start_app.sh &' 
              }
            } catch (err){
               currentBuild.result = 'SUCCESS'
              }
        }
   }
}

一旦Jenkins SSH线程在一分钟后退出,我的python应用程序就会继续在远程机器上运行。耶!这只是一个实验室演示,不是我在prod中会考虑做的事情。我会按照建议使用自定义模板。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58752498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档