Gradle Liquibase SSH隧道创建到Aurora Postgresql

jtoj6r0c  于 2022-11-14  发布在  PostgreSQL
关注(0)|答案(1)|浏览(153)

我需要你的帮助,
我需要使用liquibase连接到AWS Aurora Postgresql,它已经配置为本地计算机,工作正常,但ssh配置有问题。
我使用的是id 'org.hidetake.ssh' version '2.10.1',而id 'org.liquibase.gradle' version '2.0.4'我可以直接在主机上运行命令,例如获取下面的日期execute ('date'),但不知道为什么liquibase在使用Unexpected error running Liquibase: liquibase.exception.DatabaseException: liquibase.exception.DatabaseException: Connection could not be created to jdbc:postgresql://xxxx.rds.amazonaws.com:5432/postgres with driver org.postgresql.Driver. The connection attempt failed.时会失败
这是我房子,Gradle设置:

ssh.settings {
knownHosts = allowAnyHosts
logging = 'stdout'
identity = file("${System.properties['user.home']}/myfolder/.ssh/id_rsa")}

remotes {
  dev {
    host = 'xxx.xxx.xxx.xxx'
    port = 22
    user = 'ec2-user'
    identity = file("${System.properties['user.home']}/myfolder/.ssh/id_rsa")
  }
}

ssh.run {
        session(remotes.dev) {
            forwardLocalPort port: 5432, hostPort: 5432
            execute ('date')

            liquibase {
                activities {
                    main {
                        //changeLogFile changeLog
                        url 'jdbc:postgresql://xxxx.rds.amazonaws.com:5432/postgres'
                        username feedSqlUserDev
                        password feedSqlUserPasswordDev
                        logLevel 'debug'
                    }
                }
            }
        }
    }

你能帮我一下吗,我做错了什么?

nmpmafwu

nmpmafwu1#

在运行liquibase更新之前也必须连接到SSH堡垒主机。我的解决方案是基于插件作者的https://github.com/int128/gradle-ssh-plugin/issues/246答案。
以下是我的设置:

ssh.settings {
    knownHosts = allowAnyHosts
    logging = 'stdout'
    identity = file("${System.properties['user.home']}/.ssh/id_rsa")
}

remotes {
    bastion {
        host = '<hostname>'
        user = '<username>'
    }
}
liquibase {
    activities {
        main {
            changeLogFile '...'
            url 'jdbc:postgresql://localhost:5438/***'
            username '***'
            password '***'
            driver 'org.postgresql.Driver'
        }
    }
}
task('sshTunnelStart') {
    doFirst {
        project.ext.ready = new CountDownLatch(1)
        project.ext.done = new CountDownLatch(1)
        Thread.start {
            ssh.run {
                session(remotes.bastion) {
                    forwardLocalPort port: 5438,
                            host: '<real db hostname>',
                            hostPort: 5432
                    project.ready.countDown()
                    project.done.await(5, TimeUnit.MINUTES) // liquibase update timeout
                }
            }
        }
        ready.await(10, TimeUnit.SECONDS) // start tunnel timeout
    }
}
task('sshTunnelStop') {
    doLast {
        // teardown tunnel
        project.done.countDown()
    }
}
update.dependsOn(sshTunnelStart)
update.finalizedBy(sshTunnelStop)

请注意,在liquibase配置中,我使用localhost:5438,因为它是转发到远程的本地端口。稍后,同一端口在forwardLocalPort中用作“port”参数。“host”参数设置为远程数据库主机,“hostPort”相应地为数据库端口。配置的最后一部分添加了任务之间的依赖关系,以更新liquibase并启动/停止隧道。

相关问题