11 Oct

Add EC2 to ECS Cluster

I have had a lot of problems with ecs-agent always connecting to the “default” cluster rather than the one I want it to connect to. All the other instructions missed one vital step – removing ecs-agent from docker and reinstalling – that way it re-reads the ECS config file – without this step I could not get ecs-agent to connect to anything other than “default” cluster.

To put an EC2 ecs-agent into a specific cluster do the following:

Edit the ecs configuration file at /etc/ecs/ecs.config: sudo vi /etc/ecs/ecs.config
Add: ECS_CLUSTER=YourCluster
Close the editor “:wq”

Stop ecs-agent: sudo docker stop ecs-agent

Remove ecs-agent from docker: sudo docker rm ecs-agent

Remove the ecs-agent checkpoint file: sudo rm /var/lib/ecs/data/ecs_agent_data.json

Optional – remove the ECS log files: sudo rm /var/log/ecs/*

Reinstall ecs-agent using the command at https://github.com/aws/amazon-ecs-agent – it was (at writing):

sudo docker run --name ecs-agent \
    --detach=true \
    --restart=on-failure:10 -d \
    --volume=/var/run/docker.sock:/var/run/docker.sock \
    --volume=/var/log/ecs:/log \
    --volume=/var/lib/ecs/data:/data \
    --net=host \
    --env-file=/etc/ecs/ecs.config \
    --env=ECS_LOGFILE=/log/ecs-agent.log \
    --env=ECS_DATADIR=/data/ \
    --env=ECS_ENABLE_TASK_IAM_ROLE=true \
    --env=ECS_ENABLE_TASK_IAM_ROLE_NETWORK_HOST=true \
    amazon/amazon-ecs-agent:latest

Start ecs-agent: sudo docker start ecs-agent

If you are using an ECS Optimized image you will need to add the container auto-start by: sudo vi /etc/init/ecs-agent.conf
Add the lines:

description "ecs-agent container"
author "Me"
start on filesystem and started docker
stop on runlevel [!2345]
respawn
script
/usr/bin/docker start -a ecs-agent
end script
05 Sep

WordPress Transients – Storing False

Storing information in transients is a great way to speed up WordPress, especially if you’re using a memory cache instead of the options table. However, storing false in transients is difficult, because false means there is no transient in the cache. So how do you store false in WordPress transients?

For a great introduction to transients, see https://css-tricks.com/the-deal-with-wordpress-transients/

Why would you want to store false? Generally you would want to store false when it is a legitimate value – for example, when looking for the permalink for a slug, if the slug doesn’t exist then false is a valid return, and you wouldn’t want to query the database every time you looked up the missing slug.… Read the rest

08 Apr

Issues Creating and Downloading File in IE from PHP

Took a while to fix this one. The problem is you create a download button in HTML that leads to a PHP program that creates a file that Internet Explorer should download. The code works fine in Firefox just not IE.

The original code was something like:
{code type=php}
header(“Pragma: no-cache”);
header(“Expires: 0”);

header(“Content-disposition: attachment; filename=\”export.xls\”;”);
header(“Content-Type: application/vnd.ms-excel”);
{/code}
Which works in Firefox but not IE. The new code is:
{code type=php}header(“Pragma: “);
header(“Cache-Control: “);

header(“Content-disposition: attachment; filename=\”export.xls\”;”);
header(“Content-Type: application/vnd.ms-excel”);
{/code}
And that works fine.

22 Jul

PHP UTF-8 and simplexml

Recently the page checking code was breaking when used on a site. The XML returned from the supplier was being read using file_get_contents() and passed straight into simplexml_load_string() which was then kicking out a UTF-8 Encoding Error.

The way around this is to use utf8_encode() to ensure the string is encoded into UTF-8 before passing to simplexml_load_string(). In this instance my code goes like:

{code type=php}
$FileContents = file_get_contents( $URL ) ;
if ( $FileContents !== false )
{
   $xml  = simplexml_load_string( utf8_encode( $FileContents ) ) ;
}
{/code}

29 Dec

jQuery – Add a default button to a page

Normally in HTML you can add a ‘input type=”submit”‘ to a page and it acts as a default button. However this only works if the input tag is within a ‘form’. I was using AJAX and found that adding a form caused the AJAX to fail. Instead I ended up with the following code:

$(‘input’).keyup(function(e) {
//alert(e.keyCode);
if(e.keyCode == 13) {
$(‘#btnSubmit’).click() ;
}
});

This adds to all input fields a keyup function that checks for the enter key and if found calls the click function on the button I want to use as the default button – in this case it’s an input button called ‘btnSubmit’.… Read the rest

07 Nov

Save HTML Table to Excel using jQuery

After a few hours work looking into this, this turned out really easy to implement using jQuery and server side PHP programming as follows:

On the client side the following HTML code needs adding to the page (note this is older code – for newer code follow link at bottom):

<form action="/SaveToExcel.php" method="post" target="_blank"
onsubmit='$("#datatodisplay").val( $("&lt;div&gt;").append( $("#ReportTable").eq(0).clone() ).html() )'>
<input  type="image" src="/images/icons/Floppy-48x48.png" width="12" height="12" >
<input type="hidden" id="datatodisplay" name="datatodisplay" />
</form>

Note – variables used above – ReportTable is the id of the table you want to save and datatodisplay is a hidden variable used to post the table to the server.… Read the rest