Conditional undeploy on glassfish - deployment

I'm learning Jenkins these days and I faced a situation. I need to execute conditional undeploy on glassfish. I want to undeploy an application only if it exsits in the server. Is there a way to do this in a single command line?

To do so, I used the followin Bash code:
apps=`asadmin list-applications -t --user=admin --passwordfile=password.txt`
for app in $apps
do
if [ $app = "the_name_of_your_app" ]
then
asadmin --user=admin --passwordfile=password.txt undeploy the_name_of_your_app
fi
done;
PS: the content of password.txt is a single line: AS_ADMIN_PASSWORD=admin
I hope it will help someone someday =)

Related

Wildfly as service: How to log just once?

I'm running wildfly as a service on Linux.
I used the well-written instruction on http://developer-should-know.tumblr.com/post/112230363742/how-to-install-wildfly-as-a-service-on-linux that is based on a script [wildflyhome/bin/init.d/wildfly-init-redhat.sh] contained in the wildfly distribution. This script uses the declaration
JBOSS_CONSOLE_LOG="/var/log/wildfly/console.log"
Problem: This configuration logs twice: Firstly in server.log (in wildflyhome/standalone/log) and secondly in console.log. This wastes storage (and maybe some performance).
Therefore I set
JBOSS_CONSOLE_LOG="wildflyhome/standalone/log/server.log"
But now each log entry is written twice into server.log -:)
Question: How can I configure wildfly such that it logs just once ?
You can remove the console-handler from the servers configuration. By default WildFly logs to the stdout and the server.log. The JBOSS_CONSOLE_LOG="/var/log/wildfly/console.log" is seeing the output from stdout.
To remove the console handler you can execute the following CLI command
/subsystem=logging/root-logger=ROOT:remove-handler(name=CONSOLE)
If you want you could also remove the console-handler itself.
/subsystem=logging/console-handler=CONSOLE:remove
I had similar problem with Windows Service on Wildfly 11.0.0.Final. Wildfly service created two additional log files. Example:
wildfly-stderr.2017-11-22.log
wildfly-stdout.2017-11-22.log
It saved all logs both to stdout file and to server.log.
I couldn't turn of console-handlers due to Spring Boot logging issues on old version. Instead, i edited service.bat and changed this lines:
if "%STDOUT%"=="" set STDOUT=auto
if "%STDERR%"=="" set STDERR=auto
To this:
if "%STDOUT%"=="" set STDOUT=""
if "%STDERR%"=="" set STDERR=""
It looks that after this logging work correctly(remember to uninstall and install service once again). Now it saves logs only to server.log. I tested this for a while and don't see any missing logs.
Regarding the question whether the service scripts need the consol.log, I would say "yes" the default init.d scripts do need the console handler, because it greps the output to figure out when the service is up and running:
cat /dev/null > "$JBOSS_CONSOLE_LOG"
if [ "$JBOSS_MODE" = "standalone" ]; then
start-stop-daemon --start --user "$JBOSS_USER" \
--chuid "$JBOSS_USER" --chdir "$JBOSS_HOME" --pidfile "$JBOSS_PIDFILE" \
--exec "$JBOSS_SCRIPT" -- -c $JBOSS_CONFIG $JBOSS_OPTS >> "$JBOSS_CONSOLE_LOG" 2>&1 &
else
start-stop-daemon --start --user "$JBOSS_USER" \
--chuid "$JBOSS_USER" --chdir "$JBOSS_HOME" --pidfile "$JBOSS_PIDFILE" \
--exec "$JBOSS_SCRIPT" -- --domain-config=$JBOSS_DOMAIN_CONFIG \
--host-config=$JBOSS_HOST_CONFIG $JBOSS_OPTS >> "$JBOSS_CONSOLE_LOG" 2>&1 &
fi
count=0
launched=0
until [ $count -gt $STARTUP_WAIT ]
do
grep 'JBAS015874:' "$JBOSS_CONSOLE_LOG" > /dev/null
if [ $? -eq 0 ] ; then
launched=1
break
fi
sleep 1
count=$((count + 1));
done
Looking at it, I would say storage is not a major issue, since the script copies /dev/null into the log every time Wildfly starts. And since it greps for a given code 'JBAS015874' to know the server is up, unless you want to delete your entire server.log on every boot, you are going to have to rewrite that bit too (or it will find this string from previous startups!).
So, unless you want to rewrite all the init scripts, I'd just live with it.
Applications should not log to standard out anyways. The only thing I see there after wildfly is up and running are uncaught runtime exceptions...
In the root-logger under domain:logging:3.0 subsystem, Comment out the
<handler name="CONSOLE"/> as below
<!--handler name="CONSOLE"/-->
Afterward, restart your Wildfly Service. The stdout log won't be written to any longer. Hope this helps

is there a way to create shortcuts for a list of windows commands?

Not very sure if this is the right way of asking, but I was wondering if it is at all possible to "bunch/combine" these commands:
cd %webapps%
cls
mvn clean install
copy etc\environment\dev\yao\env.conf.bat webapps-dist\target\classes
yes
cd %jboss%
run.bat -c server -b 0.0.0.0
shutdown.bat -S
into ONE command like: %runserver% or something like that?
Also, is there a way to get rid of the prompt when I try to copy a file?
I literally do this every time I reinstall my apps, so a shortcut would be amazing.
I already just copy and paste these commands, but I'm lazy so I want shorter commands.
If you are running these commands from the same location, you can make a batch file and just run the batch file.

Starting XAMPP with batch only if it isn't already running

I am writing a batch for a new deployment of my company's software.. Here is what I have so far...
wscript.exe "invisible.vbs" "apache_start.bat" /wait
wscript.exe "invisible.vbs" "mysql_start.bat" /wait
"C:\Program Files\Internet Explorer\iexplore.exe" http://localhost
So as you can see, this script should start apache, then start mysql and then open the default page with IE.
The problem is if the user runs this script twice, it runs apache and mysql twice and loads two seperate instances. The solution I need is a way to check to see if the processes are already running and, if not, run the two wscript commands. I am absolutely horrible with shell, so please try to give specific responses! I am a software engineer, not a sysadmin. Thanks for the help!
As a software engineer I think you have a leg up on scripting over some sysadmins...
Using PowerShell would make this easy. Use the following template to execute the services - you'll need to use it twice, and follow up with launching IE as above.
If ((Get-Process mysqlprocessname.exe)) {Write-Host Skipping MySQL}
Else { Start-Process ...}
This is going to take a few minutes for you to research the best way of starting a process with PowerShell. Also, you might want to pipe Start-Process to Out-Null so the script waits to start IE and Apache.
Others may want to chime in with a simpler way from a batch file.
For XAMPP, there is a pv.exe file in the apache/bin folder that XAMPP uses to see if a service is running. Look at WorldDrknss' answer in this thread for some great info: http://www.apachefriends.org/f/viewtopic.php?p=80047
The code to solve your problem is to modify your mysql_start.bat file to this:
#echo off
apache\bin\pv mysqld.exe %1 >nul
if ERRORLEVEL 1 goto Process_NotFound
echo MySQL is running
goto END
:Process_NotFound
echo Process %1 is not running
mysql\bin\mysqld.exe --defaults-file=mysql\bin\my.ini --standalone --console
goto finish
:finish
That will check if mysqld.exe is running. If it is, it just echos that out. If not, it starts the service.

Remove script itself

I would like my script to remove itself automatically since its work is done. I have added the line below to the end of my script:
unlink($0);
For some reasons, it's not working. Could you please advise what I should do? Probably, there is another approach or I could add an error message to find out why it's not removed?
Thank you.
I don't know why you can't. It works in my machine. Maybe your file system locks the file when it's running.
And you please try this:
exec "rm -f '$0'";
which replace the current Perl process with the rm one. This should release your script and has it removed.
at the end of your script:
system("sh -c 'sleep 1; rm -f $0' &");
Bit hacky, but why do you need to delete your script? Surely that means something has created it, why not have the creator delete it too?
You can't do that. Your script is still running so the file will be in use, that's why it can't delete itself.
You need to call an external script to delete the first script after it has died.

Starting with Zend Tutorial - Zend_DB_Adapter throws Exception: "SQLSTATE[HY000] [2002] No such file or directory"

I have started to learn Zend Framework with the Book "Zend Framework in Action" in German.
Right there where it starts to get interesting, my PHP Unit Test throws this Error:
"Zend_Db_Adapter_Exception: SQLSTATE[HY000] [2002] No such file or directory"
I can't find any hints through Google searches. I did everything like it is in the book.
Can anyone give me a hint as to where to search for the fault?
Is this a common beginner mistake?
I would say that you have a problem connecting from PHP to MySQL...
Something like PHP trying to find some socket file, and not finding it, maybe ?
(I've had this problem a couple of times -- not sure the error I got was exactly this one, though)
If you are running some Linux-based system, there should be a my.cnf file somewhere, that is used to configure MySQL -- on my Ubuntu, it's in /etc/mysql/.
In this file, there might be something like this :
socket = /var/run/mysqld/mysqld.sock
PHP need to use the same file -- and, depending on your distribution, the default file might not be the same as the one that MySQL uses.
In this case, adding these lines to your php.ini file might help :
mysql.default_socket = /var/run/mysqld/mysqld.sock
mysqli.default_socket = /var/run/mysqld/mysqld.sock
pdo_mysql.default_socket = /var/run/mysqld/mysqld.sock
(You'll need to restart Apache so the modification to php.ini is taken into account)
The last one should be enough for PDO, which is used by Zend Framework -- but the two previous ones will not do any harm, and can be useful for other applications.
If this doesn't help : can you connect to your database using PDO, in another script, that's totally independant of Zend Framework ?
i.e. does something like this work (quoting) :
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
If no, the problem is definitly not with ZF, and is a configuration / installation problem of PHP.
If yes... Well, it means you have a problem with ZF, and you'll need to give us more informations about your setup (like your DSN, for instance ? )
Do not assume your unix_socket which would be different from one to another, try to find it.
First of all, get your unix_socket location.
$ mysql -u root -p
Enter your mysql password and login your mysql server from command line.
mysql> show variables like '%sock%';
+---------------+---------------------------------------+
| Variable_name | Value |
+---------------+---------------------------------------+
| socket | /opt/local/var/run/mysql5/mysqld.sock |
+---------------+---------------------------------------+
Your unix_soket could be diffrent.
Then change your php.ini, find your php.ini file from
<? phpinfo();
You maybe install many php with different version, so please don't assume your php.ini file location, get it from your 'phpinfo';
Change your php.ini:
mysql.default_socket = /opt/local/var/run/mysql5/mysqld.sock
mysqli.default_socket = /opt/local/var/run/mysql5/mysqld.sock
pdo_mysql.default_socket = /opt/local/var/run/mysql5/mysqld.sock
Then restart your apache or php-fpm.
Try setting host=127.0.0.1 on your db settings file, it worked for me! :)
Hope it helps!
i had this problem when running the magento indexer in osx.
and yes its related to php problem when connecting to mysql through pdo
in mac osx xampp, to fix this you have create symbolic link to directory /var/mysql, here is how
cd /var/mysql && sudo ln -s /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock
if the directory /var/mysql doesnt exist, we must create it with
sudo mkdir /var/mysql
Im also suffered from this problem & simply, by adding port number after the ip address saved me.
$dsn = 'mysql:dbname=sms_messenger;host=127.0.0.1:8889';
$user = 'root';
$password = 'root';
This works for Mac OS
If you don't know your mysql port number, then
You can easily find the port number on MAMP home page
OR
Type following command while running the MAMP server to switch the terminal into mysql
OMBP:mamp Omal$ /Applications/MAMP/Library/bin/mysql --host=localhost -uroot -proot
Then type
mysql> SHOW GLOBAL VARIABLES LIKE 'PORT';
This error because mysql is trying to connect via wrong socket file
try this command for MAMP servers
cd /var/mysql && sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock
or
cd /tmp && sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock
and this commands for XAMPP servers
cd /var/mysql && sudo ln -s /Applications/XAMPP/tmp/mysql/mysql.sock
or
cd /tmp && sudo ln -s /Applications/XAMPP/tmp/mysql/mysql.sock
It looks like mysql service is either not working or stopped.
you can start it by using below command (in Ubuntu):
service mysql start
It should work! If you are using any other operating system than Ubuntu then use appropriate way to start mysql