Change vmsize for a azure cloud service in octopus deploy
A common cases when running a test/staging and production environment using Azure Cloud Service is that you don’t want to use the same vmsize for all environments. It can actual be very expensive. To solve this we need to-do some variable substitution in the azure cloud service package. Doing variable substitution on ServiceConfiguration.XXX.cscfg is done by default when using the built in template “Deploy an Azure Cloud Service” but it doesn’t support variable substitution on ServiceDefinition.csde by default where the vmsize is defined. To accomplish this we need to create a pre-deployment PowerShell script.
1. In you process step based on “Deploy an Azure Cloud Service” click on enable features
2. Check mark “Custom deployment scripts” and apply the change
3. You can now add script for pre-deployment, deployment, and post-deployment.
4. The last thing is to add the variable Deploy.WebApiRoleVMSize setting the vmsize you want for different scope or environments.
Add this scrip to the “Pre-deployment” section and replace “NNN” with the name of the webrole.
write-host "Starting Service Definition rewrite" $def = "ServiceDefinition\ServiceDefinition.csdef" $csdeffile = Get-Item $def [xml]$csdefcontent = get-content $csdeffile #Find the webrole we want in a multi-webrole cloud service. Assume this exists $webrole = $csdefcontent.ServiceDefinition.WebRole | where {$_.name -eq "NNNN"} ##Update the vmsize. Add if not available $webrole.SetAttribute("vmsize",$OctopusParameters['Deploy.WebApiRoleVMSize']) write-host "Using VMSize '" $OctopusParameters['Deploy.WebApiRoleVMSize'] "' for NNNNN" #Save the changes $csdefcontent.Save($csdeffile.FullName) write-host "Service Definition rewrite successful" |
Finding compilation error for aspx files
Some time ago I did a bigger refactoring in many of our *.aspx files. The big problem was not the refactoring itself but to find all small typos and similar. If you make a typo or a syntax error in a *.cs file you will get a compilation error when building the application, but that is not the case for *.aspx files. So to increase confidence of my work I decided to use the aspnet_compiler that is used to pre compile a site for performance reasons. It was so successful that we decided to add it to our build infrastructure to catch all of this errors early.
Example (just replace the C:\apps\MySite to your output directory when building your site/app.
C:\Program Files (x86)\Microsoft Visual Studio 11.0>aspnet_compiler -p C:\apps\MySite -v /
Dynamically load and execute JavaScript method
This is a simple solution to load a JavaScript dynamically and call a method when done. For more advance solution take a look at requirejs.
1. Check what protocol we are currently using to ensure we are using a secure connection if current page is using secure connection to load our new JavaScript file.
2. Create a new script element
3. Create a callback function with a simple counter that will ensure that we only execute the new method once
4. Attach the new created callback function to onreadystatechange to be used be Internet Explorer and onload for other browsers. In onreadystatechange we add additional checks of the state to ensure we are only calling the callback when the dynamically script is completely loaded.
5. The last thing that is need is to add the new created script element to the header of current page.
var src; if (document.location.protocol === ""https:"") { src = 'https://mysite.com/thescript.js';} else { src = 'https//mysite.com/thescript.js'; } var script = document.createElement('script'); script.src = src; script.type = 'text/javascript'; var head = document.getElemenscriptByTagName('head')[0]; var called= 0; var callback = function() { if( called== 0) { called++; MethodInTheScript(); } }; // Inter explorer script.onreadystatechange = function() { if (this.readyState == 'complete') { callback(); } } //Other browsers script.onload = callback; head.appendChild(script); |
Easy usage of databases in PHP
When I started with development for PHP I did offend run into writing specific MySQL database code over and over in all my files. After suddenly running into something called Postegres SQL I quickly learned that this was not a good approach. I did also learn that it was really hard to debug database error with default error information, especially after the site was deployed for public usage.
So I decided to write the kill class that would solve all this small problems. The result was a simple database wrapper class container the most important functionality as:
– Connect
– Query
– Get next record in result (Perfect suited to be used with while loops)
– Get number of affected rows for SELECT, UPDATE, DELETE, INSERT
– Optimize table
– Clear result to free up memory
– Close connection
Get next record snippet
function next_record() { $this->Record = mysql_fetch_array($this->Query_ID); $this->Errno = mysql_errno(); $this->Error = mysql_error(); $stat = is_array($this->Record); if (!$stat) { mysql_free_result($this->Query_ID); $this->Query_ID = 0; } else { $this->Row += 1; } return $this->Record; } |
I also added some error handling that enable verbose error on the page during development mode and verbose error message with mail after deployment. The error result contains information as:
– General error message
– Error number and error text from the SQL server
– HTTP referrer
– Requested URI
– GET data
– POST data
And since I did use my class a lot for system with user authentication I did add user ID and user name for current user getting the error.
I hope this code will help someone out there to not end up with my problem and reinvent the wheel again.
MySQL version:db_inc.zip
PostrgressSQL version:db_inc_pg.zip
Usage example of the class
$db = new DB(); $db->connect(); $db->query("SELECT * FROM Blog ORDER BY Date"); while($data = $db->next_record()) { $id = $data['ID'] // Do more stuff } |