如何保持terraform的remote-exec shell持久化?

时间:2019-11-07 15:55:37

标签: jenkins terraform

美好的一天,

我正在从带Terraform的vSphere上的模板中配置VM。虚拟机启动后,文件供应商将复制本地静态内容(图片)。完成后,远程执行预配置程序将执行以下操作:

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产生的外壳消失了,app.py也是如此。我尝试在后台启动start_app.sh,尝试对它(前景和背景)进行sudoing无济于事。行为相同。如果我直接在remote-exec块内启动python /home/foo/app.py而不是调用start_app.sh,则TF永远不会退出外壳,而我的Jenkins构建将永远旋转。

我认为这没有什么区别,但是为了完整起见,当Git将Webhook发送给Jenkins时,将应用我的TF VMware计划。詹金斯将TF计划作为管道阶段的一部分进行调用。这是Jenkins的控制台输出:

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的remote-exec完成后,如何保持TF产生的外壳持久化?

1 个答案:

答案 0 :(得分:0)

好的-我发现了一个非常糟糕的骇客。 Terraform不再运行remote-exec,它只是使用文件供应器推送python Flask应用程序及其关联的静态内容。在詹金斯内部,我有一个看起来像这样的舞台:

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应用程序将继续在远程计算机上运行。好极了!它仅用于实验室演示,而不是我考虑要在产品中进行的操作。我会按照建议使用自定义模板。