Can not execute connection to Postgres via Jenkins Groovy - jenkins-pipeline

Using Scriptler can not connect to Postgresql via Jenkins groovy, neither via executing psql command, nor via using jdbc:
psql
command = """
PGPASSWORD=1111\
psql -h xxxx.rds.amazonaws.com\
-U master -d yyy -c "select * from table"
"""
proc = command.execute()
proc.waitFor()
return proc.in.text
I receive the error
Cannot run program "PGPASSWORD=1111": error=2, No such file or directory
jdbc
import groovy.sql.Sql
def dbUrl = "jdbc:postgresql://xxxx.rds.amazonaws.com/yyy"
def dbUser = "master"
def dbPassword = "1111"
def dbDriver = "org.postgresql.jdbcDriver"
def sql = Sql.newInstance(dbUrl, dbUser, dbPassword, dbDriver)
it returns
java.lang.ClassNotFoundException: org.postgresql.jdbcDriver
I installed plugins database, PostgreSQL API Plugin & database-postgresql. Jenkins v.2.176.1

So your first attempt via command.execute() will not work because you are trying to use shell command syntax and you're not running a shell.
The 2nd one will not work because you have to tell Groovy where to find the postgress jdbc library. You may be able to do this with Groovy Grape.
Personally I would do psql command using a shell step.

Related

puppet-mongodb module doesn't show Mongo shell errors in puppet-agent run

We use the voxpupuli/puppet-mongodb module to create and manage Mongo data bases and users.
As usual we add necessary data to manifests and Hiera and after that the module form a command which it runs on the client side during a puppet-agent run.
We can see the executing command from the puppet-agent debug logs for instance this command create a new DB with the name unixtest_db:
Executing: '/usr/bin/mongo unixtest_db2 --quiet --host 127.0.0.1:27017 --eval load('/root/.mongorc.js'); db.dummyData.insert({"created_by_puppet": 1})'
The problem is the module never report any errors occurred during the commands execution.
For example consider the next hiera code:
tele2_mongodb::mongodb_db:
'unixtest_db': # DB name
user : unixtest
password : >
ENC[PKCS7,....]
auth_mechanism : scram_sha_1
roles :
- dbOwner
As result we have the next command executing and results:
Executing: '/usr/bin/mongo unixtest_db2 --quiet --host 127.0.0.1:27017 --eval load('/root/.mongorc.js'); db.dummyData.insert({"created_by_puppet": 1})'
Notice: /Stage[main]/Tele2_mongodb/Mongodb::Db[unixtest_db]/Mongodb_database[unixtest_db]/ensure: created
Now we can connect to the DB with the user credential mentioned in hiera:
# mongo -u unixtest -p password unixtest_db
MongoDB shell version v5.0.9 connecting to: mongodb://127.0.0.1:27017/unixtest_db?
...
unixtesttst:PRIMARY>
Next we change the DB name in Hiera to all capital letters:
tele2_mongodb::mongodb_db:
'UNIXTEST_DB': # DB name
In puppet agent output we see identical:
Executing: '/usr/bin/mongo UNIXTEST_DB --quiet --host 127.0.0.1:27017 --eval load('/root/.mongorc.js'); db.dummyData.insert({"created_by_puppet": 1})'
Notice: /Stage[main]/Tele2_mongodb/Mongodb::Db[UNIXTEST_DB]/Mongodb_database[UNIXTEST_DB]/ensure: created
But the database is not created, it's not in the db.adminCommand('listDatabases') output (the 1st one with all small letters still exist).
And if we run the command manually on the OS console, we see the error message:
# /usr/bin/mongo UNIXTEST_DB --host 127.0.0.1:27017 --eval " load('/root/.mongorc.js'); db.dummyData.insert({"created_by_puppet": 1})"
MongoDB shell version v5.0.9
...
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 13297,
"errmsg" : "db already exists with different case already have: [unixtest_db] trying to create [UNIXTEST_DB]"
}
})
So my question is how to make the puppet also report errors if command execution failed instead of fake notification everything went smoothly.
I have the same problem for DB changes, users and passwords. If something went wrong we never know that from the puppet output.
Any ideas how to fix it?

Empty response when running REST application on docker with Mac OS

I've create a simple scala application(akka-http REST service) using SBT.
This is the application main class:
object UserApiWebService extends App {
override def main(args: Array[String]): Unit = {
implicit val system: ActorSystem = ActorSystem("user-api-system")
implicit val executionContext: ExecutionContext = system.dispatcher
implicit val materializer: ActorMaterializer = ActorMaterializer()
val userApiRoute = new UserApiRoute
val userApiRoutes = new UserApiRoutes(userApiRoute)
val config = ConfigFactory.load()
val host = config.getString("http.host")
val port = config.getInt("http.port")
println(s"Starting server on $host:$port..")
Http().bindAndHandle(userApiRoutes.routes, host, port)
println(s"Server started on $host:$port..") }
}
and my application.conf contains http.host = "127.0.0.1" and http.port = 9000
When I run the application in local running sbt run all works great.
So I decided to try docker and create a container for my Akka application.
I'm using the sbt DockerPlugin and running the command sbt docker:publishDocker it creates the docker image on local machine.
So I've started the docker container using the command
docker run -p 9000:9000 --name user-api user-api:0.1.0-SNAPSHOT
and I can see that the container is correctly running.
If I check if the API-REST application is working correctly using the command:
curl -XGET 127.0.0.1:9000/user/34
I get curl: (52) Empty reply from server as a response.
If I try the same command after entered in the container using docker exec -it a556b8846340 /bin/bash I get the correct response.
I'm working on a mac using macOS 10.12.6 and docker version 17.09.1-ce.
Anybody can help me?
Since you are running on Mac, you need docker-machine ip to talk to local VM (reference).
So your curl needs to be updated to curl -X GET http://$(docker-machine ip):9000/user/34

groovy oracle - No suitable driver found

Groovy 2.4.12
Oracle Express 11.2.0.2
ojdbc6.jar
I've just installed Oracle Express created a new user with all privileges. I can connect to my xe instance from SQL Developer, so I know its running.
Groovy console, I've added ojdbc6.jar to the classpath and now trying this...
import groovy.sql.Sql;
def cl = Class.forName('oracle.jdbc.OracleDriver')
println cl // outputs 'class oracle.jdbc.OracleDriver'
def db = [
url: 'jdbc:oracle:thin:#localhost:1521:xe',
user: 'me',
password: 'me',
driver: 'oracle.jdbc.OracleDriver']
def sql = Sql.newInstance(db.url, db.user, db.password, db.driver)
I get the SQLException "No suitable driver found for jdbc:oracle:thin:#//localhost:1521/xe".
For the connect string I've also tried jdbc:oracle:thin:#localhost:1521:xe and I've also tried oracle.jdbc.driver.OracleDriver for both loading and the driver parameter.
Is this combo not possible or have I missed something obvious?
EDIT:
Correct connect string is the no-slashes format.
Turns out #tim_yates was almost there.
Needed this...
def f = new File('c:\\oraclexe\\app\\oracle\\product\\11.2.0\\server\\jdbc\\lib\\ojdbc6.jar')
this.getClass().classLoader.rootLoader.addURL(f.toURL())
No idea why MySQL worked without this!

How to do configuration of PostgreSQL database in DHIS2?

I've downloaded and executed DHIS-2 in the Windows operating system. I want to connect with a PostgreSQL database. I'm trying the below configuration settings in the dhis.conf file but it's not working.
connection.dialect = org.hibernate.dialect.PostgreSQLDialect
connection.driver_class = org.postgresql.Driver
connection.url = jdbc:postgresql:dhis2
connection.username = dhis
connection.password = dhis
connection.schema = update
encryption.password = abcd
It's showing me following error message.
HTTP ERROR: 503
Problem accessing /. Reason:
Service Unavailable
Please verify that the database name, username and password is correct.
You can test this on the command line / terminal with the following command:
psql -d dhis2 -U dhis -p
Then enter your password. If you cannot connect, one or more of database name, username and password are incorrect.

Connecting to Oracle DB using Ruby

I am stuck with connecting to Oracle DB, have read lots of stuff but no help on result.
I have remote Oracle DB, I am connecting to it using DBVisualizer setting connection like this:
DB Type : Oracle
Driver (jdbc) : Oracle thin
Database URL: jdbc:oracle:thin:#10.10.100.10:1521/VVV.LOCALDOMAIN
UserIdf: SomeUser
Pass: SomePass
Connection works ok.
What I do in Ruby is :
require 'oci8'
require 'dbi'
...
conn = OCI8.new('SomeUser','SomePass','//10.10.100.10:1521/VVV.LOCALDOMAIN')
...
What I get is:
ORA-12545: Connect failed because target host or object does not exist
oci8.c:360:in oci8lib.so
the third parameter needs to be the TNS hostname, if you use SQL plus it is also the third parameter in the connectstring, you can find it also in the tnsnames.ora file in the oracle maps
in SQLPlus : connect user/password#hostname;
in oci8 : conn = OCI8.new('SomeUser','SomePass',hostname)
Here a working sample, obfuscated the parameters of course
require 'oci8'
oci = OCI8.new('****','***','****.***')
oci.exec('select * from table') do |record|
puts record.join(',')
end

Resources